2 * linux/drivers/mmc/sdio.c
4 * Copyright 2006-2007 Pierre Ossman
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
12 #include <linux/err.h>
14 #include <linux/mmc/host.h>
15 #include <linux/mmc/card.h>
16 #include <linux/mmc/sdio.h>
17 #include <linux/mmc/sdio_func.h>
27 static int sdio_read_fbr(struct sdio_func *func)
32 ret = mmc_io_rw_direct(func->card, 0, 0,
33 func->num * 0x100 + SDIO_FBR_STD_IF, 0, &data);
40 ret = mmc_io_rw_direct(func->card, 0, 0,
41 func->num * 0x100 + SDIO_FBR_STD_IF_EXT, 0, &data);
52 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
55 struct sdio_func *func;
57 BUG_ON(fn > SDIO_MAX_FUNCS);
59 func = sdio_alloc_func(card);
65 ret = sdio_read_fbr(func);
69 ret = sdio_read_cis(func);
73 card->sdio_func[fn - 1] = func;
79 * It is okay to remove the function here even though we hold
80 * the host lock as we haven't registered the device yet.
82 sdio_remove_func(func);
86 static int sdio_read_cccr(struct mmc_card *card)
92 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
94 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
98 cccr_vsn = data & 0x0f;
100 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
101 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
102 mmc_hostname(card->host), cccr_vsn);
106 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
108 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
112 if (data & SDIO_CCCR_CAP_SMB)
113 card->cccr.multi_block = 1;
114 if (data & SDIO_CCCR_CAP_LSC)
115 card->cccr.low_speed = 1;
116 if (data & SDIO_CCCR_CAP_4BLS)
117 card->cccr.wide_bus = 1;
119 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
120 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
124 if (data & SDIO_POWER_SMPC)
125 card->cccr.high_power = 1;
128 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
129 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
133 if (data & SDIO_SPEED_SHS)
134 card->cccr.high_speed = 1;
142 * Host is being removed. Free up the current card.
144 static void mmc_sdio_remove(struct mmc_host *host)
151 for (i = 0;i < host->card->sdio_funcs;i++) {
152 if (host->card->sdio_func[i]) {
153 sdio_remove_func(host->card->sdio_func[i]);
154 host->card->sdio_func[i] = NULL;
158 mmc_remove_card(host->card);
163 * Card detection callback from host.
165 static void mmc_sdio_detect(struct mmc_host *host)
172 mmc_claim_host(host);
175 * Just check if our card has been removed.
177 err = mmc_select_card(host->card);
179 mmc_release_host(host);
182 mmc_sdio_remove(host);
184 mmc_claim_host(host);
185 mmc_detach_bus(host);
186 mmc_release_host(host);
191 static const struct mmc_bus_ops mmc_sdio_ops = {
192 .remove = mmc_sdio_remove,
193 .detect = mmc_sdio_detect,
198 * Starting point for SDIO card init.
200 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
204 struct mmc_card *card;
207 BUG_ON(!host->claimed);
209 mmc_attach_bus(host, &mmc_sdio_ops);
212 * Sanity check the voltages that the card claims to
216 printk(KERN_WARNING "%s: card claims to support voltages "
217 "below the defined range. These will be ignored.\n",
222 if (ocr & MMC_VDD_165_195) {
223 printk(KERN_WARNING "%s: SDIO card claims to support the "
224 "incompletely defined 'low voltage range'. This "
225 "will be ignored.\n", mmc_hostname(host));
226 ocr &= ~MMC_VDD_165_195;
229 host->ocr = mmc_select_voltage(host, ocr);
232 * Can we support the voltage(s) of the card(s)?
240 * Inform the card of the voltage
242 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
247 * The number of functions on the card is encoded inside
250 funcs = (ocr & 0x70000000) >> 28;
253 * Allocate card structure.
255 card = mmc_alloc_card(host);
261 card->type = MMC_TYPE_SDIO;
262 card->sdio_funcs = funcs;
269 err = mmc_send_relative_addr(host, &card->rca);
273 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
276 * Select card, as all following commands rely on that.
278 err = mmc_select_card(card);
283 * Read the common registers.
285 err = sdio_read_cccr(card);
290 * Initialize (but don't add) all present functions.
292 for (i = 0;i < funcs;i++) {
293 err = sdio_init_func(host->card, i + 1);
298 mmc_release_host(host);
301 * First add the card to the driver model...
303 err = mmc_add_card(host->card);
308 * ...then the SDIO functions.
310 for (i = 0;i < funcs;i++) {
311 err = sdio_add_func(host->card->sdio_func[i]);
320 /* Remove without lock if the device has been added. */
321 mmc_sdio_remove(host);
322 mmc_claim_host(host);
324 /* And with lock if it hasn't been added. */
326 mmc_sdio_remove(host);
328 mmc_detach_bus(host);
329 mmc_release_host(host);
331 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
332 mmc_hostname(host), err);