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>
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
27 static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000,
32 static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80,
37 static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41 static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80,
46 #define UNSTUFF_BITS(resp,start,size) \
48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \
54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
63 void mmc_decode_cid(struct mmc_card *card)
65 u32 *resp = card->raw_cid;
67 memset(&card->cid, 0, sizeof(struct mmc_cid));
70 * SD doesn't currently have a version field so we will
71 * have to assume we can parse this.
73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
83 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
84 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
86 card->cid.year += 2000; /* SD cards year offset */
90 * Given a 128-bit response, decode to our card CSD structure.
92 static int mmc_decode_csd(struct mmc_card *card)
94 struct mmc_csd *csd = &card->csd;
95 unsigned int e, m, csd_struct;
96 u32 *resp = card->raw_csd;
98 csd_struct = UNSTUFF_BITS(resp, 126, 2);
100 switch (csd_struct) {
102 m = UNSTUFF_BITS(resp, 115, 4);
103 e = UNSTUFF_BITS(resp, 112, 3);
104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
107 m = UNSTUFF_BITS(resp, 99, 4);
108 e = UNSTUFF_BITS(resp, 96, 3);
109 csd->max_dtr = tran_exp[e] * tran_mant[m];
110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
112 e = UNSTUFF_BITS(resp, 47, 3);
113 m = UNSTUFF_BITS(resp, 62, 12);
114 csd->capacity = (1 + m) << (e + 2);
116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
124 if (UNSTUFF_BITS(resp, 46, 1)) {
126 } else if (csd->write_blkbits >= 9) {
127 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
128 csd->erase_size <<= csd->write_blkbits - 9;
133 * This is a block-addressed SDHC card. Most
134 * interesting fields are unused and have fixed
135 * values. To avoid getting tripped by buggy cards,
136 * we assume those fixed values ourselves.
138 mmc_card_set_blockaddr(card);
140 csd->tacc_ns = 0; /* Unused */
141 csd->tacc_clks = 0; /* Unused */
143 m = UNSTUFF_BITS(resp, 99, 4);
144 e = UNSTUFF_BITS(resp, 96, 3);
145 csd->max_dtr = tran_exp[e] * tran_mant[m];
146 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
148 m = UNSTUFF_BITS(resp, 48, 22);
149 csd->capacity = (1 + m) << 10;
151 csd->read_blkbits = 9;
152 csd->read_partial = 0;
153 csd->write_misalign = 0;
154 csd->read_misalign = 0;
155 csd->r2w_factor = 4; /* Unused */
156 csd->write_blkbits = 9;
157 csd->write_partial = 0;
161 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
162 mmc_hostname(card->host), csd_struct);
166 card->erase_size = csd->erase_size;
172 * Given a 64-bit response, decode to our card SCR structure.
174 static int mmc_decode_scr(struct mmc_card *card)
176 struct sd_scr *scr = &card->scr;
177 unsigned int scr_struct;
180 resp[3] = card->raw_scr[1];
181 resp[2] = card->raw_scr[0];
183 scr_struct = UNSTUFF_BITS(resp, 60, 4);
184 if (scr_struct != 0) {
185 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
186 mmc_hostname(card->host), scr_struct);
190 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
191 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
193 if (UNSTUFF_BITS(resp, 55, 1))
194 card->erased_byte = 0xFF;
196 card->erased_byte = 0x0;
202 * Fetch and process SD Status register.
204 static int mmc_read_ssr(struct mmc_card *card)
206 unsigned int au, es, et, eo;
210 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
211 printk(KERN_WARNING "%s: card lacks mandatory SD Status "
212 "function.\n", mmc_hostname(card->host));
216 ssr = kmalloc(64, GFP_KERNEL);
220 err = mmc_app_sd_status(card, ssr);
222 printk(KERN_WARNING "%s: problem reading SD Status "
223 "register.\n", mmc_hostname(card->host));
228 for (i = 0; i < 16; i++)
229 ssr[i] = be32_to_cpu(ssr[i]);
232 * UNSTUFF_BITS only works with four u32s so we have to offset the
233 * bitfield positions accordingly.
235 au = UNSTUFF_BITS(ssr, 428 - 384, 4);
236 if (au > 0 || au <= 9) {
237 card->ssr.au = 1 << (au + 4);
238 es = UNSTUFF_BITS(ssr, 408 - 384, 16);
239 et = UNSTUFF_BITS(ssr, 402 - 384, 6);
240 eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
242 card->ssr.erase_timeout = (et * 1000) / es;
243 card->ssr.erase_offset = eo * 1000;
246 printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
247 "size.\n", mmc_hostname(card->host));
255 * Fetches and decodes switch information
257 static int mmc_read_switch(struct mmc_card *card)
262 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
265 if (!(card->csd.cmdclass & CCC_SWITCH)) {
266 printk(KERN_WARNING "%s: card lacks mandatory switch "
267 "function, performance might suffer.\n",
268 mmc_hostname(card->host));
274 status = kmalloc(64, GFP_KERNEL);
276 printk(KERN_ERR "%s: could not allocate a buffer for "
277 "switch capabilities.\n", mmc_hostname(card->host));
281 err = mmc_sd_switch(card, 0, 0, 1, status);
283 /* If the host or the card can't do the switch,
284 * fail more gracefully. */
290 printk(KERN_WARNING "%s: problem reading switch "
291 "capabilities, performance might suffer.\n",
292 mmc_hostname(card->host));
298 if (status[13] & 0x02)
299 card->sw_caps.hs_max_dtr = 50000000;
308 * Test if the card supports high-speed mode and, if so, switch to it.
310 int mmc_sd_switch_hs(struct mmc_card *card)
315 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
318 if (!(card->csd.cmdclass & CCC_SWITCH))
321 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
324 if (card->sw_caps.hs_max_dtr == 0)
329 status = kmalloc(64, GFP_KERNEL);
331 printk(KERN_ERR "%s: could not allocate a buffer for "
332 "switch capabilities.\n", mmc_hostname(card->host));
336 err = mmc_sd_switch(card, 1, 0, 1, status);
340 if ((status[16] & 0xF) != 1) {
341 printk(KERN_WARNING "%s: Problem switching card "
342 "into high-speed mode!\n",
343 mmc_hostname(card->host));
355 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
356 card->raw_cid[2], card->raw_cid[3]);
357 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
358 card->raw_csd[2], card->raw_csd[3]);
359 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
360 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
361 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
362 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
363 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
364 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
365 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
366 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
367 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
368 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
371 static struct attribute *sd_std_attrs[] = {
376 &dev_attr_erase_size.attr,
377 &dev_attr_preferred_erase_size.attr,
378 &dev_attr_fwrev.attr,
379 &dev_attr_hwrev.attr,
380 &dev_attr_manfid.attr,
382 &dev_attr_oemid.attr,
383 &dev_attr_serial.attr,
387 static struct attribute_group sd_std_attr_group = {
388 .attrs = sd_std_attrs,
391 static const struct attribute_group *sd_attr_groups[] = {
396 struct device_type sd_type = {
397 .groups = sd_attr_groups,
401 * Fetch CID from card.
403 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid)
408 * Since we're changing the OCR value, we seem to
409 * need to tell some cards to go back to the idle
410 * state. We wait 1ms to give cards time to
416 * If SD_SEND_IF_COND indicates an SD 2.0
417 * compliant card and we should set bit 30
418 * of the ocr to indicate that we can handle
419 * block-addressed SDHC cards.
421 err = mmc_send_if_cond(host, ocr);
425 err = mmc_send_app_op_cond(host, ocr, NULL);
429 if (mmc_host_is_spi(host))
430 err = mmc_send_cid(host, cid);
432 err = mmc_all_send_cid(host, cid);
437 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
442 * Fetch CSD from card.
444 err = mmc_send_csd(card, card->raw_csd);
448 err = mmc_decode_csd(card);
455 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
462 * Fetch SCR from card.
464 err = mmc_app_send_scr(card, card->raw_scr);
468 err = mmc_decode_scr(card);
473 * Fetch and process SD Status register.
475 err = mmc_read_ssr(card);
479 /* Erase init depends on CSD and SSR */
480 mmc_init_erase(card);
483 * Fetch switch information from card.
485 err = mmc_read_switch(card);
491 * For SPI, enable CRC as appropriate.
492 * This CRC enable is located AFTER the reading of the
493 * card registers because some SDHC cards are not able
494 * to provide valid CRCs for non-512-byte blocks.
496 if (mmc_host_is_spi(host)) {
497 err = mmc_spi_set_crc(host, use_spi_crc);
503 * Check if read-only switch is active.
508 if (host->ops->get_ro)
509 ro = host->ops->get_ro(host);
512 printk(KERN_WARNING "%s: host does not "
513 "support reading read-only "
514 "switch. assuming write-enable.\n",
517 mmc_card_set_readonly(card);
524 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
526 unsigned max_dtr = (unsigned int)-1;
528 if (mmc_card_highspeed(card)) {
529 if (max_dtr > card->sw_caps.hs_max_dtr)
530 max_dtr = card->sw_caps.hs_max_dtr;
531 } else if (max_dtr > card->csd.max_dtr) {
532 max_dtr = card->csd.max_dtr;
538 void mmc_sd_go_highspeed(struct mmc_card *card)
540 mmc_card_set_highspeed(card);
541 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
545 * Handle the detection and initialisation of a card.
547 * In the case of a resume, "oldcard" will contain the card
548 * we're trying to reinitialise.
550 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
551 struct mmc_card *oldcard)
553 struct mmc_card *card;
558 WARN_ON(!host->claimed);
560 err = mmc_sd_get_cid(host, ocr, cid);
565 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
571 * Allocate card structure.
573 card = mmc_alloc_card(host, &sd_type);
575 return PTR_ERR(card);
577 card->type = MMC_TYPE_SD;
578 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
582 * For native busses: get card RCA and quit open drain mode.
584 if (!mmc_host_is_spi(host)) {
585 err = mmc_send_relative_addr(host, &card->rca);
589 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
593 err = mmc_sd_get_csd(host, card);
597 mmc_decode_cid(card);
601 * Select card, as all following commands rely on that.
603 if (!mmc_host_is_spi(host)) {
604 err = mmc_select_card(card);
609 err = mmc_sd_setup_card(host, card, oldcard != NULL);
614 * Attempt to change to high-speed (if supported)
616 err = mmc_sd_switch_hs(card);
618 mmc_sd_go_highspeed(card);
625 mmc_set_clock(host, mmc_sd_get_max_clock(card));
628 * Switch to wider bus (if supported).
630 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
631 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
632 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
636 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
644 mmc_remove_card(card);
650 * Host is being removed. Free up the current card.
652 static void mmc_sd_remove(struct mmc_host *host)
657 mmc_remove_card(host->card);
662 * Card detection callback from host.
664 static void mmc_sd_detect(struct mmc_host *host)
671 mmc_claim_host(host);
674 * Just check if our card has been removed.
676 err = mmc_send_status(host->card, NULL);
678 mmc_release_host(host);
683 mmc_claim_host(host);
684 mmc_detach_bus(host);
685 mmc_release_host(host);
690 * Suspend callback from host.
692 static int mmc_sd_suspend(struct mmc_host *host)
697 mmc_claim_host(host);
698 if (!mmc_host_is_spi(host))
699 mmc_deselect_cards(host);
700 host->card->state &= ~MMC_STATE_HIGHSPEED;
701 mmc_release_host(host);
707 * Resume callback from host.
709 * This function tries to determine if the same card is still present
710 * and, if so, restore all state to it.
712 static int mmc_sd_resume(struct mmc_host *host)
719 mmc_claim_host(host);
720 err = mmc_sd_init_card(host, host->ocr, host->card);
721 mmc_release_host(host);
726 static int mmc_sd_power_restore(struct mmc_host *host)
730 host->card->state &= ~MMC_STATE_HIGHSPEED;
731 mmc_claim_host(host);
732 ret = mmc_sd_init_card(host, host->ocr, host->card);
733 mmc_release_host(host);
738 static const struct mmc_bus_ops mmc_sd_ops = {
739 .remove = mmc_sd_remove,
740 .detect = mmc_sd_detect,
743 .power_restore = mmc_sd_power_restore,
746 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
747 .remove = mmc_sd_remove,
748 .detect = mmc_sd_detect,
749 .suspend = mmc_sd_suspend,
750 .resume = mmc_sd_resume,
751 .power_restore = mmc_sd_power_restore,
754 static void mmc_sd_attach_bus_ops(struct mmc_host *host)
756 const struct mmc_bus_ops *bus_ops;
758 if (!mmc_card_is_removable(host))
759 bus_ops = &mmc_sd_ops_unsafe;
761 bus_ops = &mmc_sd_ops;
762 mmc_attach_bus(host, bus_ops);
766 * Starting point for SD card init.
768 int mmc_attach_sd(struct mmc_host *host)
774 WARN_ON(!host->claimed);
776 err = mmc_send_app_op_cond(host, 0, &ocr);
780 mmc_sd_attach_bus_ops(host);
781 if (host->ocr_avail_sd)
782 host->ocr_avail = host->ocr_avail_sd;
785 * We need to get OCR a different way for SPI.
787 if (mmc_host_is_spi(host)) {
790 err = mmc_spi_read_ocr(host, 0, &ocr);
796 * Sanity check the voltages that the card claims to
800 printk(KERN_WARNING "%s: card claims to support voltages "
801 "below the defined range. These will be ignored.\n",
806 if ((ocr & MMC_VDD_165_195) &&
807 !(host->ocr_avail_sd & MMC_VDD_165_195)) {
808 printk(KERN_WARNING "%s: SD card claims to support the "
809 "incompletely defined 'low voltage range'. This "
810 "will be ignored.\n", mmc_hostname(host));
811 ocr &= ~MMC_VDD_165_195;
814 host->ocr = mmc_select_voltage(host, ocr);
817 * Can we support the voltage(s) of the card(s)?
825 * Detect and init the card.
827 err = mmc_sd_init_card(host, host->ocr, NULL);
831 mmc_release_host(host);
832 err = mmc_add_card(host->card);
833 mmc_claim_host(host);
840 mmc_release_host(host);
841 mmc_remove_card(host->card);
843 mmc_claim_host(host);
845 mmc_detach_bus(host);
847 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
848 mmc_hostname(host), err);