2 * linux/drivers/mmc/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>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/mmc.h>
18 #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 static 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);
126 * This is a block-addressed SDHC card. Most
127 * interesting fields are unused and have fixed
128 * values. To avoid getting tripped by buggy cards,
129 * we assume those fixed values ourselves.
131 mmc_card_set_blockaddr(card);
133 csd->tacc_ns = 0; /* Unused */
134 csd->tacc_clks = 0; /* Unused */
136 m = UNSTUFF_BITS(resp, 99, 4);
137 e = UNSTUFF_BITS(resp, 96, 3);
138 csd->max_dtr = tran_exp[e] * tran_mant[m];
139 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
141 m = UNSTUFF_BITS(resp, 48, 22);
142 csd->capacity = (1 + m) << 10;
144 csd->read_blkbits = 9;
145 csd->read_partial = 0;
146 csd->write_misalign = 0;
147 csd->read_misalign = 0;
148 csd->r2w_factor = 4; /* Unused */
149 csd->write_blkbits = 9;
150 csd->write_partial = 0;
153 printk("%s: unrecognised CSD structure version %d\n",
154 mmc_hostname(card->host), csd_struct);
162 * Given a 64-bit response, decode to our card SCR structure.
164 static int mmc_decode_scr(struct mmc_card *card)
166 struct sd_scr *scr = &card->scr;
167 unsigned int scr_struct;
170 BUG_ON(!mmc_card_sd(card));
172 resp[3] = card->raw_scr[1];
173 resp[2] = card->raw_scr[0];
175 scr_struct = UNSTUFF_BITS(resp, 60, 4);
176 if (scr_struct != 0) {
177 printk("%s: unrecognised SCR structure version %d\n",
178 mmc_hostname(card->host), scr_struct);
182 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
183 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
189 * Fetches and decodes switch information
191 static int mmc_read_switch(struct mmc_card *card)
196 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
199 if (!(card->csd.cmdclass & CCC_SWITCH)) {
200 printk(KERN_WARNING "%s: card lacks mandatory switch "
201 "function, performance might suffer.\n",
202 mmc_hostname(card->host));
206 err = MMC_ERR_FAILED;
208 status = kmalloc(64, GFP_KERNEL);
210 printk("%s: could not allocate a buffer for switch "
212 mmc_hostname(card->host));
216 err = mmc_sd_switch(card, 0, 0, 1, status);
217 if (err != MMC_ERR_NONE) {
218 printk(KERN_WARNING "%s: problem reading switch "
219 "capabilities, performance might suffer.\n",
220 mmc_hostname(card->host));
225 if (status[13] & 0x02)
226 card->sw_caps.hs_max_dtr = 50000000;
235 * Test if the card supports high-speed mode and, if so, switch to it.
237 static int mmc_switch_hs(struct mmc_card *card)
242 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
245 if (!(card->csd.cmdclass & CCC_SWITCH))
248 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
251 if (card->sw_caps.hs_max_dtr == 0)
254 err = MMC_ERR_FAILED;
256 status = kmalloc(64, GFP_KERNEL);
258 printk("%s: could not allocate a buffer for switch "
260 mmc_hostname(card->host));
264 err = mmc_sd_switch(card, 1, 0, 1, status);
265 if (err != MMC_ERR_NONE)
268 if ((status[16] & 0xF) != 1) {
269 printk(KERN_WARNING "%s: Problem switching card "
270 "into high-speed mode!\n",
271 mmc_hostname(card->host));
273 mmc_card_set_highspeed(card);
274 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
284 * Handle the detection and initialisation of a card.
286 * In the case of a resume, "curcard" will contain the card
287 * we're trying to reinitialise.
289 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
290 struct mmc_card *oldcard)
292 struct mmc_card *card;
295 unsigned int max_dtr;
298 BUG_ON(!host->claimed);
301 * Since we're changing the OCR value, we seem to
302 * need to tell some cards to go back to the idle
303 * state. We wait 1ms to give cards time to
309 * If SD_SEND_IF_COND indicates an SD 2.0
310 * compliant card and we should set bit 30
311 * of the ocr to indicate that we can handle
312 * block-addressed SDHC cards.
314 err = mmc_send_if_cond(host, ocr);
315 if (err == MMC_ERR_NONE)
318 err = mmc_send_app_op_cond(host, ocr, NULL);
319 if (err != MMC_ERR_NONE)
323 * Fetch CID from card.
325 err = mmc_all_send_cid(host, cid);
326 if (err != MMC_ERR_NONE)
330 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
336 * Allocate card structure.
338 card = mmc_alloc_card(host);
342 card->type = MMC_TYPE_SD;
343 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
349 err = mmc_send_relative_addr(host, &card->rca);
350 if (err != MMC_ERR_NONE)
353 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
357 * Fetch CSD from card.
359 err = mmc_send_csd(card, card->raw_csd);
360 if (err != MMC_ERR_NONE)
363 err = mmc_decode_csd(card);
367 mmc_decode_cid(card);
371 * Select card, as all following commands rely on that.
373 err = mmc_select_card(card);
374 if (err != MMC_ERR_NONE)
379 * Fetch SCR from card.
381 err = mmc_app_send_scr(card, card->raw_scr);
382 if (err != MMC_ERR_NONE)
385 err = mmc_decode_scr(card);
390 * Fetch switch information from card.
392 err = mmc_read_switch(card);
393 if (err != MMC_ERR_NONE)
398 * Attempt to change to high-speed (if supported)
400 err = mmc_switch_hs(card);
401 if (err != MMC_ERR_NONE)
407 max_dtr = (unsigned int)-1;
409 if (mmc_card_highspeed(card)) {
410 if (max_dtr > card->sw_caps.hs_max_dtr)
411 max_dtr = card->sw_caps.hs_max_dtr;
412 } else if (max_dtr > card->csd.max_dtr) {
413 max_dtr = card->csd.max_dtr;
416 mmc_set_clock(host, max_dtr);
419 * Switch to wider bus (if supported).
421 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
422 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
423 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
424 if (err != MMC_ERR_NONE)
427 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
431 * Check if read-only switch is active.
434 if (!host->ops->get_ro) {
435 printk(KERN_WARNING "%s: host does not "
436 "support reading read-only "
437 "switch. assuming write-enable.\n",
440 if (host->ops->get_ro(host))
441 mmc_card_set_readonly(card);
452 mmc_remove_card(card);
455 return MMC_ERR_FAILED;
459 * Host is being removed. Free up the current card.
461 static void mmc_sd_remove(struct mmc_host *host)
466 mmc_remove_card(host->card);
471 * Card detection callback from host.
473 static void mmc_sd_detect(struct mmc_host *host)
480 mmc_claim_host(host);
483 * Just check if our card has been removed.
485 err = mmc_send_status(host->card, NULL);
487 mmc_release_host(host);
489 if (err != MMC_ERR_NONE) {
490 mmc_remove_card(host->card);
493 mmc_claim_host(host);
494 mmc_detach_bus(host);
495 mmc_release_host(host);
499 #ifdef CONFIG_MMC_UNSAFE_RESUME
502 * Suspend callback from host.
504 static void mmc_sd_suspend(struct mmc_host *host)
509 mmc_claim_host(host);
510 mmc_deselect_cards(host);
511 host->card->state &= ~MMC_STATE_HIGHSPEED;
512 mmc_release_host(host);
516 * Resume callback from host.
518 * This function tries to determine if the same card is still present
519 * and, if so, restore all state to it.
521 static void mmc_sd_resume(struct mmc_host *host)
528 mmc_claim_host(host);
530 err = mmc_sd_init_card(host, host->ocr, host->card);
531 if (err != MMC_ERR_NONE) {
532 mmc_remove_card(host->card);
535 mmc_detach_bus(host);
538 mmc_release_host(host);
543 #define mmc_sd_suspend NULL
544 #define mmc_sd_resume NULL
548 static const struct mmc_bus_ops mmc_sd_ops = {
549 .remove = mmc_sd_remove,
550 .detect = mmc_sd_detect,
551 .suspend = mmc_sd_suspend,
552 .resume = mmc_sd_resume,
556 * Starting point for SD card init.
558 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
563 BUG_ON(!host->claimed);
565 mmc_attach_bus(host, &mmc_sd_ops);
568 * Sanity check the voltages that the card claims to
572 printk(KERN_WARNING "%s: card claims to support voltages "
573 "below the defined range. These will be ignored.\n",
578 if (ocr & MMC_VDD_165_195) {
579 printk(KERN_WARNING "%s: SD card claims to support the "
580 "incompletely defined 'low voltage range'. This "
581 "will be ignored.\n", mmc_hostname(host));
582 ocr &= ~MMC_VDD_165_195;
585 host->ocr = mmc_select_voltage(host, ocr);
588 * Can we support the voltage(s) of the card(s)?
594 * Detect and init the card.
596 err = mmc_sd_init_card(host, host->ocr, NULL);
597 if (err != MMC_ERR_NONE)
600 mmc_release_host(host);
602 err = mmc_register_card(host->card);
609 mmc_claim_host(host);
610 mmc_remove_card(host->card);
613 mmc_detach_bus(host);
614 mmc_release_host(host);