mmc: sdio: ensure mmc_sdio_detect is powered
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mmc / core / sdio.c
blobc3ad1058cd318627b8290c756c6a2f2e0d2d8027
1 /*
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>
13 #include <linux/pm_runtime.h>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/sdio.h>
18 #include <linux/mmc/sdio_func.h>
20 #include "core.h"
21 #include "bus.h"
22 #include "sd.h"
23 #include "sdio_bus.h"
24 #include "mmc_ops.h"
25 #include "sd_ops.h"
26 #include "sdio_ops.h"
27 #include "sdio_cis.h"
29 static int sdio_read_fbr(struct sdio_func *func)
31 int ret;
32 unsigned char data;
34 ret = mmc_io_rw_direct(func->card, 0, 0,
35 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
36 if (ret)
37 goto out;
39 data &= 0x0f;
41 if (data == 0x0f) {
42 ret = mmc_io_rw_direct(func->card, 0, 0,
43 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
44 if (ret)
45 goto out;
48 func->class = data;
50 out:
51 return ret;
54 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
56 int ret;
57 struct sdio_func *func;
59 BUG_ON(fn > SDIO_MAX_FUNCS);
61 func = sdio_alloc_func(card);
62 if (IS_ERR(func))
63 return PTR_ERR(func);
65 func->num = fn;
67 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
68 ret = sdio_read_fbr(func);
69 if (ret)
70 goto fail;
72 ret = sdio_read_func_cis(func);
73 if (ret)
74 goto fail;
75 } else {
76 func->vendor = func->card->cis.vendor;
77 func->device = func->card->cis.device;
78 func->max_blksize = func->card->cis.blksize;
81 card->sdio_func[fn - 1] = func;
83 return 0;
85 fail:
87 * It is okay to remove the function here even though we hold
88 * the host lock as we haven't registered the device yet.
90 sdio_remove_func(func);
91 return ret;
94 static int sdio_read_cccr(struct mmc_card *card)
96 int ret;
97 int cccr_vsn;
98 unsigned char data;
100 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
102 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
103 if (ret)
104 goto out;
106 cccr_vsn = data & 0x0f;
108 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
109 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
110 mmc_hostname(card->host), cccr_vsn);
111 return -EINVAL;
114 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
116 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
117 if (ret)
118 goto out;
120 if (data & SDIO_CCCR_CAP_SMB)
121 card->cccr.multi_block = 1;
122 if (data & SDIO_CCCR_CAP_LSC)
123 card->cccr.low_speed = 1;
124 if (data & SDIO_CCCR_CAP_4BLS)
125 card->cccr.wide_bus = 1;
127 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
128 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
129 if (ret)
130 goto out;
132 if (data & SDIO_POWER_SMPC)
133 card->cccr.high_power = 1;
136 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
137 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
138 if (ret)
139 goto out;
141 if (data & SDIO_SPEED_SHS)
142 card->cccr.high_speed = 1;
145 out:
146 return ret;
149 static int sdio_enable_wide(struct mmc_card *card)
151 int ret;
152 u8 ctrl;
154 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
155 return 0;
157 if (card->cccr.low_speed && !card->cccr.wide_bus)
158 return 0;
160 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
161 if (ret)
162 return ret;
164 ctrl |= SDIO_BUS_WIDTH_4BIT;
166 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
167 if (ret)
168 return ret;
170 return 1;
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)
181 int ret;
182 u8 ctrl;
184 if (!card->cccr.disable_cd)
185 return 0;
187 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
188 if (ret)
189 return ret;
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)
202 int ret;
203 u8 ctrl;
205 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
206 return 0;
208 if (card->cccr.low_speed && !card->cccr.wide_bus)
209 return 0;
211 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
212 if (ret)
213 return ret;
215 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
216 return 0;
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);
222 if (ret)
223 return ret;
225 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
227 return 0;
231 static int sdio_enable_4bit_bus(struct mmc_card *card)
233 int err;
235 if (card->type == MMC_TYPE_SDIO)
236 return sdio_enable_wide(card);
238 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
239 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
240 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
241 if (err)
242 return err;
243 } else
244 return 0;
246 err = sdio_enable_wide(card);
247 if (err <= 0)
248 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
250 return err;
255 * Test if the card supports high-speed mode and, if so, switch to it.
257 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
259 int ret;
260 u8 speed;
262 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
263 return 0;
265 if (!card->cccr.high_speed)
266 return 0;
268 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
269 if (ret)
270 return ret;
272 if (enable)
273 speed |= SDIO_SPEED_EHS;
274 else
275 speed &= ~SDIO_SPEED_EHS;
277 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
278 if (ret)
279 return ret;
281 return 1;
285 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
287 static int sdio_enable_hs(struct mmc_card *card)
289 int ret;
291 ret = mmc_sdio_switch_hs(card, true);
292 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
293 return ret;
295 ret = mmc_sd_switch_hs(card);
296 if (ret <= 0)
297 mmc_sdio_switch_hs(card, false);
299 return ret;
302 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
304 unsigned max_dtr;
306 if (mmc_card_highspeed(card)) {
308 * The SDIO specification doesn't mention how
309 * the CIS transfer speed register relates to
310 * high-speed, but it seems that 50 MHz is
311 * mandatory.
313 max_dtr = 50000000;
314 } else {
315 max_dtr = card->cis.max_dtr;
318 if (card->type == MMC_TYPE_SD_COMBO)
319 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
321 return max_dtr;
325 * Handle the detection and initialisation of a card.
327 * In the case of a resume, "oldcard" will contain the card
328 * we're trying to reinitialise.
330 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
331 struct mmc_card *oldcard, int powered_resume)
333 struct mmc_card *card;
334 int err;
336 BUG_ON(!host);
337 WARN_ON(!host->claimed);
340 * Inform the card of the voltage
342 if (!powered_resume) {
343 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
344 if (err)
345 goto err;
349 * For SPI, enable CRC as appropriate.
351 if (mmc_host_is_spi(host)) {
352 err = mmc_spi_set_crc(host, use_spi_crc);
353 if (err)
354 goto err;
358 * Allocate card structure.
360 card = mmc_alloc_card(host, NULL);
361 if (IS_ERR(card)) {
362 err = PTR_ERR(card);
363 goto err;
366 if (ocr & R4_MEMORY_PRESENT
367 && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) {
368 card->type = MMC_TYPE_SD_COMBO;
370 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
371 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
372 mmc_remove_card(card);
373 return -ENOENT;
375 } else {
376 card->type = MMC_TYPE_SDIO;
378 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
379 mmc_remove_card(card);
380 return -ENOENT;
385 * Call the optional HC's init_card function to handle quirks.
387 if (host->ops->init_card)
388 host->ops->init_card(host, card);
391 * For native busses: set card RCA and quit open drain mode.
393 if (!powered_resume && !mmc_host_is_spi(host)) {
394 err = mmc_send_relative_addr(host, &card->rca);
395 if (err)
396 goto remove;
398 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
402 * Read CSD, before selecting the card
404 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
405 err = mmc_sd_get_csd(host, card);
406 if (err)
407 return err;
409 mmc_decode_cid(card);
413 * Select card, as all following commands rely on that.
415 if (!powered_resume && !mmc_host_is_spi(host)) {
416 err = mmc_select_card(card);
417 if (err)
418 goto remove;
421 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
423 * This is non-standard SDIO device, meaning it doesn't
424 * have any CIA (Common I/O area) registers present.
425 * It's host's responsibility to fill cccr and cis
426 * structures in init_card().
428 mmc_set_clock(host, card->cis.max_dtr);
430 if (card->cccr.high_speed) {
431 mmc_card_set_highspeed(card);
432 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
435 goto finish;
439 * Read the common registers.
441 err = sdio_read_cccr(card);
442 if (err)
443 goto remove;
446 * Read the common CIS tuples.
448 err = sdio_read_common_cis(card);
449 if (err)
450 goto remove;
452 if (oldcard) {
453 int same = (card->cis.vendor == oldcard->cis.vendor &&
454 card->cis.device == oldcard->cis.device);
455 mmc_remove_card(card);
456 if (!same)
457 return -ENOENT;
459 card = oldcard;
462 if (card->type == MMC_TYPE_SD_COMBO) {
463 err = mmc_sd_setup_card(host, card, oldcard != NULL);
464 /* handle as SDIO-only card if memory init failed */
465 if (err) {
466 mmc_go_idle(host);
467 if (mmc_host_is_spi(host))
468 /* should not fail, as it worked previously */
469 mmc_spi_set_crc(host, use_spi_crc);
470 card->type = MMC_TYPE_SDIO;
471 } else
472 card->dev.type = &sd_type;
476 * If needed, disconnect card detection pull-up resistor.
478 err = sdio_disable_cd(card);
479 if (err)
480 goto remove;
483 * Switch to high-speed (if supported).
485 err = sdio_enable_hs(card);
486 if (err > 0)
487 mmc_sd_go_highspeed(card);
488 else if (err)
489 goto remove;
492 * Change to the card's maximum speed.
494 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
497 * Switch to wider bus (if supported).
499 err = sdio_enable_4bit_bus(card);
500 if (err > 0)
501 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
502 else if (err)
503 goto remove;
505 finish:
506 if (!oldcard)
507 host->card = card;
508 return 0;
510 remove:
511 if (!oldcard)
512 mmc_remove_card(card);
514 err:
515 return err;
519 * Host is being removed. Free up the current card.
521 static void mmc_sdio_remove(struct mmc_host *host)
523 int i;
525 BUG_ON(!host);
526 BUG_ON(!host->card);
528 for (i = 0;i < host->card->sdio_funcs;i++) {
529 if (host->card->sdio_func[i]) {
530 sdio_remove_func(host->card->sdio_func[i]);
531 host->card->sdio_func[i] = NULL;
535 mmc_remove_card(host->card);
536 host->card = NULL;
540 * Card detection callback from host.
542 static void mmc_sdio_detect(struct mmc_host *host)
544 int err;
546 BUG_ON(!host);
547 BUG_ON(!host->card);
549 /* Make sure card is powered before detecting it */
550 err = pm_runtime_get_sync(&host->card->dev);
551 if (err < 0)
552 goto out;
554 mmc_claim_host(host);
557 * Just check if our card has been removed.
559 err = mmc_select_card(host->card);
561 mmc_release_host(host);
563 out:
564 if (err) {
565 mmc_sdio_remove(host);
567 mmc_claim_host(host);
568 mmc_detach_bus(host);
569 mmc_release_host(host);
572 /* Tell PM core that we're done */
573 pm_runtime_put(&host->card->dev);
577 * SDIO suspend. We need to suspend all functions separately.
578 * Therefore all registered functions must have drivers with suspend
579 * and resume methods. Failing that we simply remove the whole card.
581 static int mmc_sdio_suspend(struct mmc_host *host)
583 int i, err = 0;
585 for (i = 0; i < host->card->sdio_funcs; i++) {
586 struct sdio_func *func = host->card->sdio_func[i];
587 if (func && sdio_func_present(func) && func->dev.driver) {
588 const struct dev_pm_ops *pmops = func->dev.driver->pm;
589 if (!pmops || !pmops->suspend || !pmops->resume) {
590 /* force removal of entire card in that case */
591 err = -ENOSYS;
592 } else
593 err = pmops->suspend(&func->dev);
594 if (err)
595 break;
598 while (err && --i >= 0) {
599 struct sdio_func *func = host->card->sdio_func[i];
600 if (func && sdio_func_present(func) && func->dev.driver) {
601 const struct dev_pm_ops *pmops = func->dev.driver->pm;
602 pmops->resume(&func->dev);
606 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
607 mmc_claim_host(host);
608 sdio_disable_wide(host->card);
609 mmc_release_host(host);
612 return err;
615 static int mmc_sdio_resume(struct mmc_host *host)
617 int i, err;
619 BUG_ON(!host);
620 BUG_ON(!host->card);
622 /* Basic card reinitialization. */
623 mmc_claim_host(host);
624 err = mmc_sdio_init_card(host, host->ocr, host->card,
625 (host->pm_flags & MMC_PM_KEEP_POWER));
626 if (!err && host->sdio_irqs)
627 mmc_signal_sdio_irq(host);
628 mmc_release_host(host);
631 * If the card looked to be the same as before suspending, then
632 * we proceed to resume all card functions. If one of them returns
633 * an error then we simply return that error to the core and the
634 * card will be redetected as new. It is the responsibility of
635 * the function driver to perform further tests with the extra
636 * knowledge it has of the card to confirm the card is indeed the
637 * same as before suspending (same MAC address for network cards,
638 * etc.) and return an error otherwise.
640 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
641 struct sdio_func *func = host->card->sdio_func[i];
642 if (func && sdio_func_present(func) && func->dev.driver) {
643 const struct dev_pm_ops *pmops = func->dev.driver->pm;
644 err = pmops->resume(&func->dev);
648 return err;
651 static int mmc_sdio_power_restore(struct mmc_host *host)
653 int ret;
655 BUG_ON(!host);
656 BUG_ON(!host->card);
658 mmc_claim_host(host);
659 ret = mmc_sdio_init_card(host, host->ocr, host->card,
660 (host->pm_flags & MMC_PM_KEEP_POWER));
661 if (!ret && host->sdio_irqs)
662 mmc_signal_sdio_irq(host);
663 mmc_release_host(host);
665 return ret;
668 static const struct mmc_bus_ops mmc_sdio_ops = {
669 .remove = mmc_sdio_remove,
670 .detect = mmc_sdio_detect,
671 .suspend = mmc_sdio_suspend,
672 .resume = mmc_sdio_resume,
673 .power_restore = mmc_sdio_power_restore,
678 * Starting point for SDIO card init.
680 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
682 int err;
683 int i, funcs;
684 struct mmc_card *card;
686 BUG_ON(!host);
687 WARN_ON(!host->claimed);
689 mmc_attach_bus(host, &mmc_sdio_ops);
692 * Sanity check the voltages that the card claims to
693 * support.
695 if (ocr & 0x7F) {
696 printk(KERN_WARNING "%s: card claims to support voltages "
697 "below the defined range. These will be ignored.\n",
698 mmc_hostname(host));
699 ocr &= ~0x7F;
702 host->ocr = mmc_select_voltage(host, ocr);
705 * Can we support the voltage(s) of the card(s)?
707 if (!host->ocr) {
708 err = -EINVAL;
709 goto err;
713 * Detect and init the card.
715 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
716 if (err)
717 goto err;
718 card = host->card;
721 * Let runtime PM core know our card is active
723 err = pm_runtime_set_active(&card->dev);
724 if (err)
725 goto remove;
728 * Enable runtime PM for this card
730 pm_runtime_enable(&card->dev);
733 * The number of functions on the card is encoded inside
734 * the ocr.
736 funcs = (ocr & 0x70000000) >> 28;
737 card->sdio_funcs = 0;
740 * Initialize (but don't add) all present functions.
742 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
743 err = sdio_init_func(host->card, i + 1);
744 if (err)
745 goto remove;
748 * Enable Runtime PM for this func
750 pm_runtime_enable(&card->sdio_func[i]->dev);
753 mmc_release_host(host);
756 * First add the card to the driver model...
758 err = mmc_add_card(host->card);
759 if (err)
760 goto remove_added;
763 * ...then the SDIO functions.
765 for (i = 0;i < funcs;i++) {
766 err = sdio_add_func(host->card->sdio_func[i]);
767 if (err)
768 goto remove_added;
771 return 0;
774 remove_added:
775 /* Remove without lock if the device has been added. */
776 mmc_sdio_remove(host);
777 mmc_claim_host(host);
778 remove:
779 /* And with lock if it hasn't been added. */
780 if (host->card)
781 mmc_sdio_remove(host);
782 err:
783 mmc_detach_bus(host);
784 mmc_release_host(host);
786 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
787 mmc_hostname(host), err);
789 return err;