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 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
40 ret = mmc_io_rw_direct(func->card, 0, 0,
41 SDIO_FBR_BASE(func->num) + 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 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
66 ret = sdio_read_fbr(func);
70 ret = sdio_read_func_cis(func);
74 func->vendor = func->card->cis.vendor;
75 func->device = func->card->cis.device;
76 func->max_blksize = func->card->cis.blksize;
79 card->sdio_func[fn - 1] = func;
85 * It is okay to remove the function here even though we hold
86 * the host lock as we haven't registered the device yet.
88 sdio_remove_func(func);
92 static int sdio_read_cccr(struct mmc_card *card)
98 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
100 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
104 cccr_vsn = data & 0x0f;
106 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
107 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
108 mmc_hostname(card->host), cccr_vsn);
112 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
114 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
118 if (data & SDIO_CCCR_CAP_SMB)
119 card->cccr.multi_block = 1;
120 if (data & SDIO_CCCR_CAP_LSC)
121 card->cccr.low_speed = 1;
122 if (data & SDIO_CCCR_CAP_4BLS)
123 card->cccr.wide_bus = 1;
125 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
126 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
130 if (data & SDIO_POWER_SMPC)
131 card->cccr.high_power = 1;
134 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
135 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
139 if (data & SDIO_SPEED_SHS)
140 card->cccr.high_speed = 1;
147 static int sdio_enable_wide(struct mmc_card *card)
152 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
155 if (card->cccr.low_speed && !card->cccr.wide_bus)
158 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
162 ctrl |= SDIO_BUS_WIDTH_4BIT;
164 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
168 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
174 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
175 * of the card. This may be required on certain setups of boards,
176 * controllers and embedded sdio device which do not need the card's
177 * pull-up. As a result, card detection is disabled and power is saved.
179 static int sdio_disable_cd(struct mmc_card *card)
184 if (!card->cccr.disable_cd)
187 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
191 ctrl |= SDIO_BUS_CD_DISABLE;
193 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
197 * Devices that remain active during a system suspend are
198 * put back into 1-bit mode.
200 static int sdio_disable_wide(struct mmc_card *card)
205 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
208 if (card->cccr.low_speed && !card->cccr.wide_bus)
211 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
215 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
218 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
219 ctrl |= SDIO_BUS_ASYNC_INT;
221 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
225 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
231 * Test if the card supports high-speed mode and, if so, switch to it.
233 static int sdio_enable_hs(struct mmc_card *card)
238 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
241 if (!card->cccr.high_speed)
244 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
248 speed |= SDIO_SPEED_EHS;
250 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
254 mmc_card_set_highspeed(card);
255 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
261 * Handle the detection and initialisation of a card.
263 * In the case of a resume, "oldcard" will contain the card
264 * we're trying to reinitialise.
266 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
267 struct mmc_card *oldcard, int powered_resume)
269 struct mmc_card *card;
273 WARN_ON(!host->claimed);
276 * Inform the card of the voltage
278 if (!powered_resume) {
279 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
285 * For SPI, enable CRC as appropriate.
287 if (mmc_host_is_spi(host)) {
288 err = mmc_spi_set_crc(host, use_spi_crc);
294 * Allocate card structure.
296 card = mmc_alloc_card(host, NULL);
302 card->type = MMC_TYPE_SDIO;
305 * Call the optional HC's init_card function to handle quirks.
307 if (host->ops->init_card)
308 host->ops->init_card(host, card);
311 * For native busses: set card RCA and quit open drain mode.
313 if (!powered_resume && !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 (!powered_resume && !mmc_host_is_spi(host)) {
325 err = mmc_select_card(card);
330 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
332 * This is non-standard non-removable SDIO device,
333 * meaning it doesn't have any CIA (Common I/O area)
334 * registers present. It's host's responsibility to fill
335 * cccr and cis structures in init_card().
337 mmc_set_clock(host, card->cis.max_dtr);
339 if (card->cccr.high_speed) {
340 mmc_card_set_highspeed(card);
341 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
348 * Read the common registers.
350 err = sdio_read_cccr(card);
355 * Read the common CIS tuples.
357 err = sdio_read_common_cis(card);
362 int same = (card->cis.vendor == oldcard->cis.vendor &&
363 card->cis.device == oldcard->cis.device);
364 mmc_remove_card(card);
374 * Switch to high-speed (if supported).
376 err = sdio_enable_hs(card);
381 * Change to the card's maximum speed.
383 if (mmc_card_highspeed(card)) {
385 * The SDIO specification doesn't mention how
386 * the CIS transfer speed register relates to
387 * high-speed, but it seems that 50 MHz is
390 mmc_set_clock(host, 50000000);
392 mmc_set_clock(host, card->cis.max_dtr);
396 * Switch to wider bus (if supported).
398 err = sdio_enable_wide(card);
409 mmc_remove_card(card);
416 * Host is being removed. Free up the current card.
418 static void mmc_sdio_remove(struct mmc_host *host)
425 for (i = 0;i < host->card->sdio_funcs;i++) {
426 if (host->card->sdio_func[i]) {
427 sdio_remove_func(host->card->sdio_func[i]);
428 host->card->sdio_func[i] = NULL;
432 mmc_remove_card(host->card);
437 * Card detection callback from host.
439 static void mmc_sdio_detect(struct mmc_host *host)
446 mmc_claim_host(host);
449 * Just check if our card has been removed.
451 err = mmc_select_card(host->card);
453 mmc_release_host(host);
456 mmc_sdio_remove(host);
458 mmc_claim_host(host);
459 mmc_detach_bus(host);
460 mmc_release_host(host);
465 * SDIO suspend. We need to suspend all functions separately.
466 * Therefore all registered functions must have drivers with suspend
467 * and resume methods. Failing that we simply remove the whole card.
469 static int mmc_sdio_suspend(struct mmc_host *host)
473 for (i = 0; i < host->card->sdio_funcs; i++) {
474 struct sdio_func *func = host->card->sdio_func[i];
475 if (func && sdio_func_present(func) && func->dev.driver) {
476 const struct dev_pm_ops *pmops = func->dev.driver->pm;
477 if (!pmops || !pmops->suspend || !pmops->resume) {
478 /* force removal of entire card in that case */
481 err = pmops->suspend(&func->dev);
486 while (err && --i >= 0) {
487 struct sdio_func *func = host->card->sdio_func[i];
488 if (func && sdio_func_present(func) && func->dev.driver) {
489 const struct dev_pm_ops *pmops = func->dev.driver->pm;
490 pmops->resume(&func->dev);
494 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
495 mmc_claim_host(host);
496 sdio_disable_wide(host->card);
497 mmc_release_host(host);
503 static int mmc_sdio_resume(struct mmc_host *host)
510 /* Basic card reinitialization. */
511 mmc_claim_host(host);
512 err = mmc_sdio_init_card(host, host->ocr, host->card,
513 (host->pm_flags & MMC_PM_KEEP_POWER));
515 /* We may have switched to 1-bit mode during suspend. */
516 err = sdio_enable_wide(host->card);
517 if (!err && host->sdio_irqs)
518 mmc_signal_sdio_irq(host);
519 mmc_release_host(host);
522 * If the card looked to be the same as before suspending, then
523 * we proceed to resume all card functions. If one of them returns
524 * an error then we simply return that error to the core and the
525 * card will be redetected as new. It is the responsibility of
526 * the function driver to perform further tests with the extra
527 * knowledge it has of the card to confirm the card is indeed the
528 * same as before suspending (same MAC address for network cards,
529 * etc.) and return an error otherwise.
531 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
532 struct sdio_func *func = host->card->sdio_func[i];
533 if (func && sdio_func_present(func) && func->dev.driver) {
534 const struct dev_pm_ops *pmops = func->dev.driver->pm;
535 err = pmops->resume(&func->dev);
542 static const struct mmc_bus_ops mmc_sdio_ops = {
543 .remove = mmc_sdio_remove,
544 .detect = mmc_sdio_detect,
545 .suspend = mmc_sdio_suspend,
546 .resume = mmc_sdio_resume,
551 * Starting point for SDIO card init.
553 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
557 struct mmc_card *card;
560 WARN_ON(!host->claimed);
562 mmc_attach_bus(host, &mmc_sdio_ops);
565 * Sanity check the voltages that the card claims to
569 printk(KERN_WARNING "%s: card claims to support voltages "
570 "below the defined range. These will be ignored.\n",
575 host->ocr = mmc_select_voltage(host, ocr);
578 * Can we support the voltage(s) of the card(s)?
586 * Detect and init the card.
588 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
594 * The number of functions on the card is encoded inside
597 funcs = (ocr & 0x70000000) >> 28;
598 card->sdio_funcs = 0;
601 * If needed, disconnect card detection pull-up resistor.
603 err = sdio_disable_cd(card);
608 * Initialize (but don't add) all present functions.
610 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
611 err = sdio_init_func(host->card, i + 1);
616 mmc_release_host(host);
619 * First add the card to the driver model...
621 err = mmc_add_card(host->card);
626 * ...then the SDIO functions.
628 for (i = 0;i < funcs;i++) {
629 err = sdio_add_func(host->card->sdio_func[i]);
638 /* Remove without lock if the device has been added. */
639 mmc_sdio_remove(host);
640 mmc_claim_host(host);
642 /* And with lock if it hasn't been added. */
644 mmc_sdio_remove(host);
646 mmc_detach_bus(host);
647 mmc_release_host(host);
649 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
650 mmc_hostname(host), err);