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 ret = sdio_read_fbr(func);
69 ret = sdio_read_func_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;
141 static int sdio_enable_wide(struct mmc_card *card)
146 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
149 if (card->cccr.low_speed && !card->cccr.wide_bus)
152 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
156 ctrl |= SDIO_BUS_WIDTH_4BIT;
158 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
162 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
168 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
169 * of the card. This may be required on certain setups of boards,
170 * controllers and embedded sdio device which do not need the card's
171 * pull-up. As a result, card detection is disabled and power is saved.
173 static int sdio_disable_cd(struct mmc_card *card)
178 if (!card->cccr.disable_cd)
181 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
185 ctrl |= SDIO_BUS_CD_DISABLE;
187 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
191 * Devices that remain active during a system suspend are
192 * put back into 1-bit mode.
194 static int sdio_disable_wide(struct mmc_card *card)
199 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
202 if (card->cccr.low_speed && !card->cccr.wide_bus)
205 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
209 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
212 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
213 ctrl |= SDIO_BUS_ASYNC_INT;
215 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
219 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
225 * Test if the card supports high-speed mode and, if so, switch to it.
227 static int sdio_enable_hs(struct mmc_card *card)
232 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
235 if (!card->cccr.high_speed)
238 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
242 speed |= SDIO_SPEED_EHS;
244 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
248 mmc_card_set_highspeed(card);
249 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
255 * Handle the detection and initialisation of a card.
257 * In the case of a resume, "oldcard" will contain the card
258 * we're trying to reinitialise.
260 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
261 struct mmc_card *oldcard, int powered_resume)
263 struct mmc_card *card;
267 WARN_ON(!host->claimed);
270 * Inform the card of the voltage
272 if (!powered_resume) {
273 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
279 * For SPI, enable CRC as appropriate.
281 if (mmc_host_is_spi(host)) {
282 err = mmc_spi_set_crc(host, use_spi_crc);
288 * Allocate card structure.
290 card = mmc_alloc_card(host, NULL);
296 card->type = MMC_TYPE_SDIO;
299 * For native busses: set card RCA and quit open drain mode.
301 if (!powered_resume && !mmc_host_is_spi(host)) {
302 err = mmc_send_relative_addr(host, &card->rca);
306 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
310 * Select card, as all following commands rely on that.
312 if (!powered_resume && !mmc_host_is_spi(host)) {
313 err = mmc_select_card(card);
319 * Read the common registers.
321 err = sdio_read_cccr(card);
326 * Read the common CIS tuples.
328 err = sdio_read_common_cis(card);
333 int same = (card->cis.vendor == oldcard->cis.vendor &&
334 card->cis.device == oldcard->cis.device);
335 mmc_remove_card(card);
345 * Switch to high-speed (if supported).
347 err = sdio_enable_hs(card);
352 * Change to the card's maximum speed.
354 if (mmc_card_highspeed(card)) {
356 * The SDIO specification doesn't mention how
357 * the CIS transfer speed register relates to
358 * high-speed, but it seems that 50 MHz is
361 mmc_set_clock(host, 50000000);
363 mmc_set_clock(host, card->cis.max_dtr);
367 * Switch to wider bus (if supported).
369 err = sdio_enable_wide(card);
379 mmc_remove_card(card);
386 * Host is being removed. Free up the current card.
388 static void mmc_sdio_remove(struct mmc_host *host)
395 for (i = 0;i < host->card->sdio_funcs;i++) {
396 if (host->card->sdio_func[i]) {
397 sdio_remove_func(host->card->sdio_func[i]);
398 host->card->sdio_func[i] = NULL;
402 mmc_remove_card(host->card);
407 * Card detection callback from host.
409 static void mmc_sdio_detect(struct mmc_host *host)
416 mmc_claim_host(host);
419 * Just check if our card has been removed.
421 err = mmc_select_card(host->card);
423 mmc_release_host(host);
426 mmc_sdio_remove(host);
428 mmc_claim_host(host);
429 mmc_detach_bus(host);
430 mmc_release_host(host);
435 * SDIO suspend. We need to suspend all functions separately.
436 * Therefore all registered functions must have drivers with suspend
437 * and resume methods. Failing that we simply remove the whole card.
439 static int mmc_sdio_suspend(struct mmc_host *host)
443 for (i = 0; i < host->card->sdio_funcs; i++) {
444 struct sdio_func *func = host->card->sdio_func[i];
445 if (func && sdio_func_present(func) && func->dev.driver) {
446 const struct dev_pm_ops *pmops = func->dev.driver->pm;
447 if (!pmops || !pmops->suspend || !pmops->resume) {
448 /* force removal of entire card in that case */
451 err = pmops->suspend(&func->dev);
456 while (err && --i >= 0) {
457 struct sdio_func *func = host->card->sdio_func[i];
458 if (func && sdio_func_present(func) && func->dev.driver) {
459 const struct dev_pm_ops *pmops = func->dev.driver->pm;
460 pmops->resume(&func->dev);
464 if (!err && host->pm_flags & MMC_PM_KEEP_POWER) {
465 mmc_claim_host(host);
466 sdio_disable_wide(host->card);
467 mmc_release_host(host);
473 static int mmc_sdio_resume(struct mmc_host *host)
480 /* Basic card reinitialization. */
481 mmc_claim_host(host);
482 err = mmc_sdio_init_card(host, host->ocr, host->card,
483 (host->pm_flags & MMC_PM_KEEP_POWER));
485 /* We may have switched to 1-bit mode during suspend. */
486 err = sdio_enable_wide(host->card);
487 if (!err && host->sdio_irqs)
488 mmc_signal_sdio_irq(host);
489 mmc_release_host(host);
492 * If the card looked to be the same as before suspending, then
493 * we proceed to resume all card functions. If one of them returns
494 * an error then we simply return that error to the core and the
495 * card will be redetected as new. It is the responsibility of
496 * the function driver to perform further tests with the extra
497 * knowledge it has of the card to confirm the card is indeed the
498 * same as before suspending (same MAC address for network cards,
499 * etc.) and return an error otherwise.
501 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
502 struct sdio_func *func = host->card->sdio_func[i];
503 if (func && sdio_func_present(func) && func->dev.driver) {
504 const struct dev_pm_ops *pmops = func->dev.driver->pm;
505 err = pmops->resume(&func->dev);
512 static const struct mmc_bus_ops mmc_sdio_ops = {
513 .remove = mmc_sdio_remove,
514 .detect = mmc_sdio_detect,
515 .suspend = mmc_sdio_suspend,
516 .resume = mmc_sdio_resume,
521 * Starting point for SDIO card init.
523 int mmc_attach_sdio(struct mmc_host *host, u32 ocr)
527 struct mmc_card *card;
530 WARN_ON(!host->claimed);
532 mmc_attach_bus(host, &mmc_sdio_ops);
535 * Sanity check the voltages that the card claims to
539 printk(KERN_WARNING "%s: card claims to support voltages "
540 "below the defined range. These will be ignored.\n",
545 host->ocr = mmc_select_voltage(host, ocr);
548 * Can we support the voltage(s) of the card(s)?
556 * Detect and init the card.
558 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
564 * The number of functions on the card is encoded inside
567 funcs = (ocr & 0x70000000) >> 28;
568 card->sdio_funcs = 0;
571 * If needed, disconnect card detection pull-up resistor.
573 err = sdio_disable_cd(card);
578 * Initialize (but don't add) all present functions.
580 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
581 err = sdio_init_func(host->card, i + 1);
586 mmc_release_host(host);
589 * First add the card to the driver model...
591 err = mmc_add_card(host->card);
596 * ...then the SDIO functions.
598 for (i = 0;i < funcs;i++) {
599 err = sdio_add_func(host->card->sdio_func[i]);
608 /* Remove without lock if the device has been added. */
609 mmc_sdio_remove(host);
610 mmc_claim_host(host);
612 /* And with lock if it hasn't been added. */
614 mmc_sdio_remove(host);
616 mmc_detach_bus(host);
617 mmc_release_host(host);
619 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
620 mmc_hostname(host), err);