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 #ifdef CONFIG_MMC_EMBEDDED_SDIO
28 #include <linux/mmc/sdio_ids.h>
31 static int sdio_read_fbr(struct sdio_func *func)
36 ret = mmc_io_rw_direct(func->card, 0, 0,
37 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
44 ret = mmc_io_rw_direct(func->card, 0, 0,
45 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
56 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
59 struct sdio_func *func;
61 BUG_ON(fn > SDIO_MAX_FUNCS);
63 func = sdio_alloc_func(card);
69 ret = sdio_read_fbr(func);
73 ret = sdio_read_func_cis(func);
77 card->sdio_func[fn - 1] = func;
83 * It is okay to remove the function here even though we hold
84 * the host lock as we haven't registered the device yet.
86 sdio_remove_func(func);
90 static int sdio_read_cccr(struct mmc_card *card)
96 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
98 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
102 cccr_vsn = data & 0x0f;
104 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
105 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
106 mmc_hostname(card->host), cccr_vsn);
110 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
112 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
116 if (data & SDIO_CCCR_CAP_SMB)
117 card->cccr.multi_block = 1;
118 if (data & SDIO_CCCR_CAP_LSC)
119 card->cccr.low_speed = 1;
120 if (data & SDIO_CCCR_CAP_4BLS)
121 card->cccr.wide_bus = 1;
123 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
124 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
128 if (data & SDIO_POWER_SMPC)
129 card->cccr.high_power = 1;
132 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
133 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
137 if (data & SDIO_SPEED_SHS)
138 card->cccr.high_speed = 1;
145 static int sdio_enable_wide(struct mmc_card *card)
150 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
153 if (card->cccr.low_speed && !card->cccr.wide_bus)
156 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
160 ctrl |= SDIO_BUS_WIDTH_4BIT;
162 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
166 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
172 * Host is being removed. Free up the current card.
174 static void mmc_sdio_remove(struct mmc_host *host)
181 for (i = 0;i < host->card->sdio_funcs;i++) {
182 if (host->card->sdio_func[i]) {
183 sdio_remove_func(host->card->sdio_func[i]);
184 host->card->sdio_func[i] = NULL;
188 mmc_remove_card(host->card);
193 * Card detection callback from host.
195 static void mmc_sdio_detect(struct mmc_host *host)
202 mmc_claim_host(host);
205 * Just check if our card has been removed.
207 err = mmc_select_card(host->card);
209 mmc_release_host(host);
212 mmc_sdio_remove(host);
214 mmc_claim_host(host);
215 mmc_detach_bus(host);
216 mmc_release_host(host);
221 static const struct mmc_bus_ops mmc_sdio_ops = {
222 .remove = mmc_sdio_remove,
223 .detect = mmc_sdio_detect,
228 * Starting point for SDIO card init.
230 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
234 struct mmc_card *card;
237 WARN_ON(!host->claimed);
239 mmc_attach_bus(host, &mmc_sdio_ops);
242 * Sanity check the voltages that the card claims to
246 printk(KERN_WARNING "%s: card claims to support voltages "
247 "below the defined range. These will be ignored.\n",
252 if (ocr & MMC_VDD_165_195) {
253 printk(KERN_WARNING "%s: SDIO card claims to support the "
254 "incompletely defined 'low voltage range'. This "
255 "will be ignored.\n", mmc_hostname(host));
256 ocr &= ~MMC_VDD_165_195;
259 host->ocr = mmc_select_voltage(host, ocr);
262 * Can we support the voltage(s) of the card(s)?
270 * Inform the card of the voltage
272 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
277 * For SPI, enable CRC as appropriate.
279 if (mmc_host_is_spi(host)) {
280 err = mmc_spi_set_crc(host, use_spi_crc);
286 * The number of functions on the card is encoded inside
289 funcs = (ocr & 0x70000000) >> 28;
291 #ifdef CONFIG_MMC_EMBEDDED_SDIO
292 if (host->embedded_sdio_data.funcs)
293 funcs = host->embedded_sdio_data.num_funcs;
297 * Allocate card structure.
299 card = mmc_alloc_card(host, NULL);
305 card->type = MMC_TYPE_SDIO;
306 card->sdio_funcs = funcs;
311 * For native busses: set card RCA and quit open drain mode.
313 if (!mmc_host_is_spi(host)) {
314 err = mmc_send_relative_addr(host, &card->rca);
318 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
322 * Select card, as all following commands rely on that.
324 if (!mmc_host_is_spi(host)) {
325 err = mmc_select_card(card);
331 * Read the common registers.
334 #ifdef CONFIG_MMC_EMBEDDED_SDIO
335 if (host->embedded_sdio_data.cccr)
336 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
339 err = sdio_read_cccr(card);
342 #ifdef CONFIG_MMC_EMBEDDED_SDIO
346 #ifdef CONFIG_MMC_EMBEDDED_SDIO
347 if (host->embedded_sdio_data.cis)
348 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
352 * Read the common CIS tuples.
354 err = sdio_read_common_cis(card);
357 #ifdef CONFIG_MMC_EMBEDDED_SDIO
361 * No support for high-speed yet, so just set
362 * the card's maximum speed.
364 mmc_set_clock(host, card->cis.max_dtr);
367 * Switch to wider bus (if supported).
369 err = sdio_enable_wide(card);
374 * Initialize (but don't add) all present functions.
376 for (i = 0;i < funcs;i++) {
377 #ifdef CONFIG_MMC_EMBEDDED_SDIO
378 if (host->embedded_sdio_data.funcs) {
379 struct sdio_func *tmp;
381 tmp = sdio_alloc_func(host->card);
385 card->sdio_func[i] = tmp;
386 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
387 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
388 tmp->vendor = card->cis.vendor;
389 tmp->device = card->cis.device;
392 err = sdio_init_func(host->card, i + 1);
395 #ifdef CONFIG_MMC_EMBEDDED_SDIO
400 mmc_release_host(host);
403 * First add the card to the driver model...
405 err = mmc_add_card(host->card);
410 * ...then the SDIO functions.
412 for (i = 0;i < funcs;i++) {
413 err = sdio_add_func(host->card->sdio_func[i]);
422 /* Remove without lock if the device has been added. */
423 mmc_sdio_remove(host);
424 mmc_claim_host(host);
426 /* And with lock if it hasn't been added. */
428 mmc_sdio_remove(host);
430 mmc_detach_bus(host);
431 mmc_release_host(host);
433 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
434 mmc_hostname(host), err);