2 * linux/drivers/mmc/core/sd.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/stat.h>
16 #include <linux/pm_runtime.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21 #include <linux/mmc/sd.h>
29 static const unsigned int tran_exp[] = {
30 10000, 100000, 1000000, 10000000,
34 static const unsigned char tran_mant[] = {
35 0, 10, 12, 13, 15, 20, 25, 30,
36 35, 40, 45, 50, 55, 60, 70, 80,
39 static const unsigned int tacc_exp[] = {
40 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
43 static const unsigned int tacc_mant[] = {
44 0, 10, 12, 13, 15, 20, 25, 30,
45 35, 40, 45, 50, 55, 60, 70, 80,
48 #define UNSTUFF_BITS(resp,start,size) \
50 const int __size = size; \
51 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
52 const int __off = 3 - ((start) / 32); \
53 const int __shft = (start) & 31; \
56 __res = resp[__off] >> __shft; \
57 if (__size + __shft > 32) \
58 __res |= resp[__off-1] << ((32 - __shft) % 32); \
63 * Given the decoded CSD structure, decode the raw CID to our CID structure.
65 void mmc_decode_cid(struct mmc_card *card)
67 u32 *resp = card->raw_cid;
69 memset(&card->cid, 0, sizeof(struct mmc_cid));
72 * SD doesn't currently have a version field so we will
73 * have to assume we can parse this.
75 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
76 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
77 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
78 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
79 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
80 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
81 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
82 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
83 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
84 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
85 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
86 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
88 card->cid.year += 2000; /* SD cards year offset */
92 * Given a 128-bit response, decode to our card CSD structure.
94 static int mmc_decode_csd(struct mmc_card *card)
96 struct mmc_csd *csd = &card->csd;
97 unsigned int e, m, csd_struct;
98 u32 *resp = card->raw_csd;
100 csd_struct = UNSTUFF_BITS(resp, 126, 2);
102 switch (csd_struct) {
104 m = UNSTUFF_BITS(resp, 115, 4);
105 e = UNSTUFF_BITS(resp, 112, 3);
106 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
107 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
109 m = UNSTUFF_BITS(resp, 99, 4);
110 e = UNSTUFF_BITS(resp, 96, 3);
111 csd->max_dtr = tran_exp[e] * tran_mant[m];
112 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
114 e = UNSTUFF_BITS(resp, 47, 3);
115 m = UNSTUFF_BITS(resp, 62, 12);
116 csd->capacity = (1 + m) << (e + 2);
118 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
119 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
120 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
121 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
122 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
123 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
124 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
126 if (UNSTUFF_BITS(resp, 46, 1)) {
128 } else if (csd->write_blkbits >= 9) {
129 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
130 csd->erase_size <<= csd->write_blkbits - 9;
135 * This is a block-addressed SDHC or SDXC card. Most
136 * interesting fields are unused and have fixed
137 * values. To avoid getting tripped by buggy cards,
138 * we assume those fixed values ourselves.
140 mmc_card_set_blockaddr(card);
142 csd->tacc_ns = 0; /* Unused */
143 csd->tacc_clks = 0; /* Unused */
145 m = UNSTUFF_BITS(resp, 99, 4);
146 e = UNSTUFF_BITS(resp, 96, 3);
147 csd->max_dtr = tran_exp[e] * tran_mant[m];
148 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
149 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
151 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
152 if (csd->c_size >= 0xFFFF)
153 mmc_card_set_ext_capacity(card);
155 m = UNSTUFF_BITS(resp, 48, 22);
156 csd->capacity = (1 + m) << 10;
158 csd->read_blkbits = 9;
159 csd->read_partial = 0;
160 csd->write_misalign = 0;
161 csd->read_misalign = 0;
162 csd->r2w_factor = 4; /* Unused */
163 csd->write_blkbits = 9;
164 csd->write_partial = 0;
168 pr_err("%s: unrecognised CSD structure version %d\n",
169 mmc_hostname(card->host), csd_struct);
173 card->erase_size = csd->erase_size;
179 * Given a 64-bit response, decode to our card SCR structure.
181 static int mmc_decode_scr(struct mmc_card *card)
183 struct sd_scr *scr = &card->scr;
184 unsigned int scr_struct;
187 resp[3] = card->raw_scr[1];
188 resp[2] = card->raw_scr[0];
190 scr_struct = UNSTUFF_BITS(resp, 60, 4);
191 if (scr_struct != 0) {
192 pr_err("%s: unrecognised SCR structure version %d\n",
193 mmc_hostname(card->host), scr_struct);
197 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
198 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
199 if (scr->sda_vsn == SCR_SPEC_VER_2)
200 /* Check if Physical Layer Spec v3.0 is supported */
201 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
203 if (UNSTUFF_BITS(resp, 55, 1))
204 card->erased_byte = 0xFF;
206 card->erased_byte = 0x0;
209 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
214 * Fetch and process SD Status register.
216 static int mmc_read_ssr(struct mmc_card *card)
218 unsigned int au, es, et, eo;
222 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
223 pr_warning("%s: card lacks mandatory SD Status "
224 "function.\n", mmc_hostname(card->host));
228 ssr = kmalloc(64, GFP_KERNEL);
232 err = mmc_app_sd_status(card, ssr);
234 pr_warning("%s: problem reading SD Status "
235 "register.\n", mmc_hostname(card->host));
240 for (i = 0; i < 16; i++)
241 ssr[i] = be32_to_cpu(ssr[i]);
243 /* SD3.0 increases max AU size to 64MB (0xF) from 4MB (0x9) */
244 max_au = card->scr.sda_spec3 ? 0xF : 0x9;
247 * UNSTUFF_BITS only works with four u32s so we have to offset the
248 * bitfield positions accordingly.
250 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
251 if (au > 0 && au <= max_au) {
252 card->ssr.au = 1 << (au + 4);
253 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
254 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
255 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
257 card->ssr.erase_timeout = (et * 1000) / es;
258 card->ssr.erase_offset = eo * 1000;
261 pr_warning("%s: SD Status: Invalid Allocation Unit "
262 "size.\n", mmc_hostname(card->host));
270 * Fetches and decodes switch information
272 static int mmc_read_switch(struct mmc_card *card)
277 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
280 if (!(card->csd.cmdclass & CCC_SWITCH)) {
281 pr_warning("%s: card lacks mandatory switch "
282 "function, performance might suffer.\n",
283 mmc_hostname(card->host));
289 status = kmalloc(64, GFP_KERNEL);
291 pr_err("%s: could not allocate a buffer for "
292 "switch capabilities.\n",
293 mmc_hostname(card->host));
298 * Find out the card's support bits with a mode 0 operation.
299 * The argument does not matter, as the support bits do not
300 * change with the arguments.
302 err = mmc_sd_switch(card, 0, 0, 0, status);
305 * If the host or the card can't do the switch,
306 * fail more gracefully.
308 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
311 pr_warning("%s: problem reading Bus Speed modes.\n",
312 mmc_hostname(card->host));
318 if (status[13] & SD_MODE_HIGH_SPEED)
319 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
321 if (card->scr.sda_spec3) {
322 card->sw_caps.sd3_bus_mode = status[13];
323 /* Driver Strengths supported by the card */
324 card->sw_caps.sd3_drv_type = status[9];
334 * Test if the card supports high-speed mode and, if so, switch to it.
336 int mmc_sd_switch_hs(struct mmc_card *card)
341 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
344 if (!(card->csd.cmdclass & CCC_SWITCH))
347 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
350 if (card->sw_caps.hs_max_dtr == 0)
355 status = kmalloc(64, GFP_KERNEL);
357 pr_err("%s: could not allocate a buffer for "
358 "switch capabilities.\n", mmc_hostname(card->host));
362 err = mmc_sd_switch(card, 1, 0, 1, status);
366 if ((status[16] & 0xF) != 1) {
367 pr_warning("%s: Problem switching card "
368 "into high-speed mode!\n",
369 mmc_hostname(card->host));
381 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
383 int host_drv_type = SD_DRIVER_TYPE_B;
384 int card_drv_type = SD_DRIVER_TYPE_B;
389 * If the host doesn't support any of the Driver Types A,C or D,
390 * or there is no board specific handler then default Driver
393 if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
394 | MMC_CAP_DRIVER_TYPE_D)))
397 if (!card->host->ops->select_drive_strength)
400 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
401 host_drv_type |= SD_DRIVER_TYPE_A;
403 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
404 host_drv_type |= SD_DRIVER_TYPE_C;
406 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
407 host_drv_type |= SD_DRIVER_TYPE_D;
409 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
410 card_drv_type |= SD_DRIVER_TYPE_A;
412 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
413 card_drv_type |= SD_DRIVER_TYPE_C;
415 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
416 card_drv_type |= SD_DRIVER_TYPE_D;
419 * The drive strength that the hardware can support
420 * depends on the board design. Pass the appropriate
421 * information and let the hardware specific code
422 * return what is possible given the options
424 mmc_host_clk_hold(card->host);
425 drive_strength = card->host->ops->select_drive_strength(
426 card->sw_caps.uhs_max_dtr,
427 host_drv_type, card_drv_type);
428 mmc_host_clk_release(card->host);
430 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
434 if ((status[15] & 0xF) != drive_strength) {
435 pr_warning("%s: Problem setting drive strength!\n",
436 mmc_hostname(card->host));
440 mmc_set_driver_type(card->host, drive_strength);
445 static void sd_update_bus_speed_mode(struct mmc_card *card)
448 * If the host doesn't support any of the UHS-I modes, fallback on
451 if (!mmc_host_uhs(card->host)) {
452 card->sd_bus_speed = 0;
456 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
457 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
458 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
459 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
460 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
461 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
462 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
463 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
464 SD_MODE_UHS_SDR50)) {
465 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
466 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
467 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
468 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
469 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
470 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
471 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
472 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
473 SD_MODE_UHS_SDR12)) {
474 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
478 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
481 unsigned int timing = 0;
483 switch (card->sd_bus_speed) {
484 case UHS_SDR104_BUS_SPEED:
485 timing = MMC_TIMING_UHS_SDR104;
486 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
488 case UHS_DDR50_BUS_SPEED:
489 timing = MMC_TIMING_UHS_DDR50;
490 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
492 case UHS_SDR50_BUS_SPEED:
493 timing = MMC_TIMING_UHS_SDR50;
494 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
496 case UHS_SDR25_BUS_SPEED:
497 timing = MMC_TIMING_UHS_SDR25;
498 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
500 case UHS_SDR12_BUS_SPEED:
501 timing = MMC_TIMING_UHS_SDR12;
502 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
508 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
512 if ((status[16] & 0xF) != card->sd_bus_speed)
513 pr_warning("%s: Problem setting bus speed mode!\n",
514 mmc_hostname(card->host));
516 mmc_set_timing(card->host, timing);
517 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
523 /* Get host's max current setting at its current voltage */
524 static u32 sd_get_host_max_current(struct mmc_host *host)
526 u32 voltage, max_current;
528 voltage = 1 << host->ios.vdd;
530 case MMC_VDD_165_195:
531 max_current = host->max_current_180;
535 max_current = host->max_current_300;
539 max_current = host->max_current_330;
548 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
550 int current_limit = SD_SET_CURRENT_NO_CHANGE;
555 * Current limit switch is only defined for SDR50, SDR104, and DDR50
556 * bus speed modes. For other bus speed modes, we do not change the
559 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
560 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
561 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
565 * Host has different current capabilities when operating at
566 * different voltages, so find out its max current first.
568 max_current = sd_get_host_max_current(card->host);
571 * We only check host's capability here, if we set a limit that is
572 * higher than the card's maximum current, the card will be using its
573 * maximum current, e.g. if the card's maximum current is 300ma, and
574 * when we set current limit to 200ma, the card will draw 200ma, and
575 * when we set current limit to 400/600/800ma, the card will draw its
576 * maximum 300ma from the host.
578 if (max_current >= 800)
579 current_limit = SD_SET_CURRENT_LIMIT_800;
580 else if (max_current >= 600)
581 current_limit = SD_SET_CURRENT_LIMIT_600;
582 else if (max_current >= 400)
583 current_limit = SD_SET_CURRENT_LIMIT_400;
584 else if (max_current >= 200)
585 current_limit = SD_SET_CURRENT_LIMIT_200;
587 if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
588 err = mmc_sd_switch(card, 1, 3, current_limit, status);
592 if (((status[15] >> 4) & 0x0F) != current_limit)
593 pr_warning("%s: Problem setting current limit!\n",
594 mmc_hostname(card->host));
602 * UHS-I specific initialization procedure
604 static int mmc_sd_init_uhs_card(struct mmc_card *card)
609 if (!card->scr.sda_spec3)
612 if (!(card->csd.cmdclass & CCC_SWITCH))
615 status = kmalloc(64, GFP_KERNEL);
617 pr_err("%s: could not allocate a buffer for "
618 "switch capabilities.\n", mmc_hostname(card->host));
622 /* Set 4-bit bus width */
623 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
624 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
625 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
629 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
633 * Select the bus speed mode depending on host
634 * and card capability.
636 sd_update_bus_speed_mode(card);
638 /* Set the driver strength for the card */
639 err = sd_select_driver_type(card, status);
643 /* Set current limit for the card */
644 err = sd_set_current_limit(card, status);
648 /* Set bus speed mode of the card */
649 err = sd_set_bus_speed_mode(card, status);
654 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
655 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
657 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
658 (card->sd_bus_speed == UHS_SDR50_BUS_SPEED ||
659 card->sd_bus_speed == UHS_SDR104_BUS_SPEED)) {
660 mmc_host_clk_hold(card->host);
661 err = card->host->ops->execute_tuning(card->host,
662 MMC_SEND_TUNING_BLOCK);
663 mmc_host_clk_release(card->host);
672 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
673 card->raw_cid[2], card->raw_cid[3]);
674 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
675 card->raw_csd[2], card->raw_csd[3]);
676 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
677 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
678 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
679 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
680 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
681 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
682 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
683 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
684 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
685 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
688 static struct attribute *sd_std_attrs[] = {
693 &dev_attr_erase_size.attr,
694 &dev_attr_preferred_erase_size.attr,
695 &dev_attr_fwrev.attr,
696 &dev_attr_hwrev.attr,
697 &dev_attr_manfid.attr,
699 &dev_attr_oemid.attr,
700 &dev_attr_serial.attr,
704 static struct attribute_group sd_std_attr_group = {
705 .attrs = sd_std_attrs,
708 static const struct attribute_group *sd_attr_groups[] = {
713 struct device_type sd_type = {
714 .groups = sd_attr_groups,
718 * Fetch CID from card.
720 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
730 pr_warning("%s: Skipping voltage switch\n",
735 * Since we're changing the OCR value, we seem to
736 * need to tell some cards to go back to the idle
737 * state. We wait 1ms to give cards time to
743 * If SD_SEND_IF_COND indicates an SD 2.0
744 * compliant card and we should set bit 30
745 * of the ocr to indicate that we can handle
746 * block-addressed SDHC cards.
748 err = mmc_send_if_cond(host, ocr);
753 * If the host supports one of UHS-I modes, request the card
754 * to switch to 1.8V signaling level. If the card has failed
755 * repeatedly to switch however, skip this.
757 if (retries && mmc_host_uhs(host))
761 * If the host can supply more than 150mA at current voltage,
762 * XPC should be set to 1.
764 max_current = sd_get_host_max_current(host);
765 if (max_current > 150)
768 err = mmc_send_app_op_cond(host, ocr, rocr);
773 * In case CCS and S18A in the response is set, start Signal Voltage
774 * Switch procedure. SPI mode doesn't support CMD11.
776 if (!mmc_host_is_spi(host) && rocr &&
777 ((*rocr & 0x41000000) == 0x41000000)) {
778 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
780 if (err == -EAGAIN) {
789 if (mmc_host_is_spi(host))
790 err = mmc_send_cid(host, cid);
792 err = mmc_all_send_cid(host, cid);
797 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
802 * Fetch CSD from card.
804 err = mmc_send_csd(card, card->raw_csd);
808 err = mmc_decode_csd(card);
815 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
822 * Fetch SCR from card.
824 err = mmc_app_send_scr(card, card->raw_scr);
828 err = mmc_decode_scr(card);
833 * Fetch and process SD Status register.
835 err = mmc_read_ssr(card);
839 /* Erase init depends on CSD and SSR */
840 mmc_init_erase(card);
843 * Fetch switch information from card.
845 err = mmc_read_switch(card);
851 * For SPI, enable CRC as appropriate.
852 * This CRC enable is located AFTER the reading of the
853 * card registers because some SDHC cards are not able
854 * to provide valid CRCs for non-512-byte blocks.
856 if (mmc_host_is_spi(host)) {
857 err = mmc_spi_set_crc(host, use_spi_crc);
863 * Check if read-only switch is active.
868 if (host->ops->get_ro) {
869 mmc_host_clk_hold(card->host);
870 ro = host->ops->get_ro(host);
871 mmc_host_clk_release(card->host);
875 pr_warning("%s: host does not "
876 "support reading read-only "
877 "switch. assuming write-enable.\n",
880 mmc_card_set_readonly(card);
887 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
889 unsigned max_dtr = (unsigned int)-1;
891 if (mmc_card_highspeed(card)) {
892 if (max_dtr > card->sw_caps.hs_max_dtr)
893 max_dtr = card->sw_caps.hs_max_dtr;
894 } else if (max_dtr > card->csd.max_dtr) {
895 max_dtr = card->csd.max_dtr;
901 void mmc_sd_go_highspeed(struct mmc_card *card)
903 mmc_card_set_highspeed(card);
904 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
908 * Handle the detection and initialisation of a card.
910 * In the case of a resume, "oldcard" will contain the card
911 * we're trying to reinitialise.
913 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
914 struct mmc_card *oldcard)
916 struct mmc_card *card;
922 WARN_ON(!host->claimed);
924 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
929 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
935 * Allocate card structure.
937 card = mmc_alloc_card(host, &sd_type);
939 return PTR_ERR(card);
942 card->type = MMC_TYPE_SD;
943 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
947 * For native busses: get card RCA and quit open drain mode.
949 if (!mmc_host_is_spi(host)) {
950 err = mmc_send_relative_addr(host, &card->rca);
956 err = mmc_sd_get_csd(host, card);
960 mmc_decode_cid(card);
964 * Select card, as all following commands rely on that.
966 if (!mmc_host_is_spi(host)) {
967 err = mmc_select_card(card);
972 err = mmc_sd_setup_card(host, card, oldcard != NULL);
976 /* Initialization sequence for UHS-I cards */
977 if (rocr & SD_ROCR_S18A) {
978 err = mmc_sd_init_uhs_card(card);
982 /* Card is an ultra-high-speed card */
983 mmc_card_set_uhs(card);
986 * Attempt to change to high-speed (if supported)
988 err = mmc_sd_switch_hs(card);
990 mmc_sd_go_highspeed(card);
997 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1000 * Switch to wider bus (if supported).
1002 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1003 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1004 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1008 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1017 mmc_remove_card(card);
1023 * Host is being removed. Free up the current card.
1025 static void mmc_sd_remove(struct mmc_host *host)
1028 BUG_ON(!host->card);
1030 mmc_remove_card(host->card);
1035 * Card detection - card is alive.
1037 static int mmc_sd_alive(struct mmc_host *host)
1039 return mmc_send_status(host->card, NULL);
1043 * Card detection callback from host.
1045 static void mmc_sd_detect(struct mmc_host *host)
1050 BUG_ON(!host->card);
1052 mmc_get_card(host->card);
1055 * Just check if our card has been removed.
1057 err = _mmc_detect_card_removed(host);
1059 mmc_put_card(host->card);
1062 mmc_sd_remove(host);
1064 mmc_claim_host(host);
1065 mmc_detach_bus(host);
1066 mmc_power_off(host);
1067 mmc_release_host(host);
1071 static int _mmc_sd_suspend(struct mmc_host *host)
1076 BUG_ON(!host->card);
1078 mmc_claim_host(host);
1080 if (mmc_card_suspended(host->card))
1083 if (!mmc_host_is_spi(host))
1084 err = mmc_deselect_cards(host);
1085 host->card->state &= ~MMC_STATE_HIGHSPEED;
1087 mmc_power_off(host);
1088 mmc_card_set_suspended(host->card);
1092 mmc_release_host(host);
1097 * Callback for suspend
1099 static int mmc_sd_suspend(struct mmc_host *host)
1103 err = _mmc_sd_suspend(host);
1105 pm_runtime_disable(&host->card->dev);
1106 pm_runtime_set_suspended(&host->card->dev);
1113 * This function tries to determine if the same card is still present
1114 * and, if so, restore all state to it.
1116 static int _mmc_sd_resume(struct mmc_host *host)
1121 BUG_ON(!host->card);
1123 mmc_claim_host(host);
1125 if (!mmc_card_suspended(host->card))
1128 mmc_power_up(host, host->card->ocr);
1129 err = mmc_sd_init_card(host, host->card->ocr, host->card);
1130 mmc_card_clr_suspended(host->card);
1133 mmc_release_host(host);
1138 * Callback for resume
1140 static int mmc_sd_resume(struct mmc_host *host)
1144 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1145 err = _mmc_sd_resume(host);
1146 pm_runtime_set_active(&host->card->dev);
1147 pm_runtime_mark_last_busy(&host->card->dev);
1149 pm_runtime_enable(&host->card->dev);
1155 * Callback for runtime_suspend.
1157 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1161 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1164 err = _mmc_sd_suspend(host);
1166 pr_err("%s: error %d doing aggessive suspend\n",
1167 mmc_hostname(host), err);
1173 * Callback for runtime_resume.
1175 static int mmc_sd_runtime_resume(struct mmc_host *host)
1179 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1182 err = _mmc_sd_resume(host);
1184 pr_err("%s: error %d doing aggessive resume\n",
1185 mmc_hostname(host), err);
1190 static int mmc_sd_power_restore(struct mmc_host *host)
1194 host->card->state &= ~MMC_STATE_HIGHSPEED;
1195 mmc_claim_host(host);
1196 ret = mmc_sd_init_card(host, host->card->ocr, host->card);
1197 mmc_release_host(host);
1202 static const struct mmc_bus_ops mmc_sd_ops = {
1203 .remove = mmc_sd_remove,
1204 .detect = mmc_sd_detect,
1207 .power_restore = mmc_sd_power_restore,
1208 .alive = mmc_sd_alive,
1209 .shutdown = mmc_sd_suspend,
1212 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1213 .remove = mmc_sd_remove,
1214 .detect = mmc_sd_detect,
1215 .runtime_suspend = mmc_sd_runtime_suspend,
1216 .runtime_resume = mmc_sd_runtime_resume,
1217 .suspend = mmc_sd_suspend,
1218 .resume = mmc_sd_resume,
1219 .power_restore = mmc_sd_power_restore,
1220 .alive = mmc_sd_alive,
1221 .shutdown = mmc_sd_suspend,
1224 static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1226 const struct mmc_bus_ops *bus_ops;
1228 if (!mmc_card_is_removable(host))
1229 bus_ops = &mmc_sd_ops_unsafe;
1231 bus_ops = &mmc_sd_ops;
1232 mmc_attach_bus(host, bus_ops);
1236 * Starting point for SD card init.
1238 int mmc_attach_sd(struct mmc_host *host)
1244 WARN_ON(!host->claimed);
1246 err = mmc_send_app_op_cond(host, 0, &ocr);
1250 mmc_sd_attach_bus_ops(host);
1251 if (host->ocr_avail_sd)
1252 host->ocr_avail = host->ocr_avail_sd;
1255 * We need to get OCR a different way for SPI.
1257 if (mmc_host_is_spi(host)) {
1260 err = mmc_spi_read_ocr(host, 0, &ocr);
1265 rocr = mmc_select_voltage(host, ocr);
1268 * Can we support the voltage(s) of the card(s)?
1276 * Detect and init the card.
1278 err = mmc_sd_init_card(host, rocr, NULL);
1282 mmc_release_host(host);
1283 err = mmc_add_card(host->card);
1284 mmc_claim_host(host);
1291 mmc_release_host(host);
1292 mmc_remove_card(host->card);
1294 mmc_claim_host(host);
1296 mmc_detach_bus(host);
1298 pr_err("%s: error %d whilst initialising SD card\n",
1299 mmc_hostname(host), err);