1 /*
2 * linux/drivers/mmc/core/sd.c
3 *
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.
7 *
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.
11 */
12
13 #include <linux/err.h>
14
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/mmc.h>
18 #include <linux/mmc/sd.h>
19
20 #include "core.h"
21 #include "bus.h"
22 #include "mmc_ops.h"
23 #include "sd_ops.h"
24
25 static const unsigned int tran_exp[] = {
26 10000, 100000, 1000000, 10000000,
27 0, 0, 0, 0
28 };
29
30 static const unsigned char tran_mant[] = {
31 0, 10, 12, 13, 15, 20, 25, 30,
32 35, 40, 45, 50, 55, 60, 70, 80,
33 };
34
35 static const unsigned int tacc_exp[] = {
36 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
37 };
38
39 static const unsigned int tacc_mant[] = {
40 0, 10, 12, 13, 15, 20, 25, 30,
41 35, 40, 45, 50, 55, 60, 70, 80,
42 };
43
44 #define UNSTUFF_BITS(resp,start,size) \
45 ({ \
46 const int __size = size; \
47 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
48 const int __off = 3 - ((start) / 32); \
49 const int __shft = (start) & 31; \
50 u32 __res; \
51 \
52 __res = resp[__off] >> __shft; \
53 if (__size + __shft > 32) \
54 __res |= resp[__off-1] << ((32 - __shft) % 32); \
55 __res & __mask; \
56 })
57
58 /*
59 * Given the decoded CSD structure, decode the raw CID to our CID structure.
60 */
mmc_decode_cid(struct mmc_card * card)61 static void mmc_decode_cid(struct mmc_card *card)
62 {
63 u32 *resp = card->raw_cid;
64
65 memset(&card->cid, 0, sizeof(struct mmc_cid));
66
67 /*
68 * SD doesn't currently have a version field so we will
69 * have to assume we can parse this.
70 */
71 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
72 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
73 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
74 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
75 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
76 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
77 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
78 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
79 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
80 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
81 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
82 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
83
84 card->cid.year += 2000; /* SD cards year offset */
85 }
86
87 /*
88 * Given a 128-bit response, decode to our card CSD structure.
89 */
mmc_decode_csd(struct mmc_card * card)90 static int mmc_decode_csd(struct mmc_card *card)
91 {
92 struct mmc_csd *csd = &card->csd;
93 unsigned int e, m, csd_struct;
94 u32 *resp = card->raw_csd;
95
96 csd_struct = UNSTUFF_BITS(resp, 126, 2);
97
98 switch (csd_struct) {
99 case 0:
100 m = UNSTUFF_BITS(resp, 115, 4);
101 e = UNSTUFF_BITS(resp, 112, 3);
102 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
103 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
104
105 m = UNSTUFF_BITS(resp, 99, 4);
106 e = UNSTUFF_BITS(resp, 96, 3);
107 csd->max_dtr = tran_exp[e] * tran_mant[m];
108 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
109
110 e = UNSTUFF_BITS(resp, 47, 3);
111 m = UNSTUFF_BITS(resp, 62, 12);
112 csd->capacity = (1 + m) << (e + 2);
113
114 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
115 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
116 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
117 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
118 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
119 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
120 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
121 break;
122 case 1:
123 /*
124 * This is a block-addressed SDHC card. Most
125 * interesting fields are unused and have fixed
126 * values. To avoid getting tripped by buggy cards,
127 * we assume those fixed values ourselves.
128 */
129 mmc_card_set_blockaddr(card);
130
131 csd->tacc_ns = 0; /* Unused */
132 csd->tacc_clks = 0; /* Unused */
133
134 m = UNSTUFF_BITS(resp, 99, 4);
135 e = UNSTUFF_BITS(resp, 96, 3);
136 csd->max_dtr = tran_exp[e] * tran_mant[m];
137 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
138
139 m = UNSTUFF_BITS(resp, 48, 22);
140 csd->capacity = (1 + m) << 10;
141
142 csd->read_blkbits = 9;
143 csd->read_partial = 0;
144 csd->write_misalign = 0;
145 csd->read_misalign = 0;
146 csd->r2w_factor = 4; /* Unused */
147 csd->write_blkbits = 9;
148 csd->write_partial = 0;
149 break;
150 default:
151 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
152 mmc_hostname(card->host), csd_struct);
153 return -EINVAL;
154 }
155
156 return 0;
157 }
158
159 /*
160 * Given a 64-bit response, decode to our card SCR structure.
161 */
mmc_decode_scr(struct mmc_card * card)162 static int mmc_decode_scr(struct mmc_card *card)
163 {
164 struct sd_scr *scr = &card->scr;
165 unsigned int scr_struct;
166 u32 resp[4];
167
168 resp[3] = card->raw_scr[1];
169 resp[2] = card->raw_scr[0];
170
171 scr_struct = UNSTUFF_BITS(resp, 60, 4);
172 if (scr_struct != 0) {
173 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
174 mmc_hostname(card->host), scr_struct);
175 return -EINVAL;
176 }
177
178 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
179 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
180
181 return 0;
182 }
183
184 /*
185 * Fetches and decodes switch information
186 */
mmc_read_switch(struct mmc_card * card)187 static int mmc_read_switch(struct mmc_card *card)
188 {
189 int err;
190 u8 *status;
191
192 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
193 return 0;
194
195 if (!(card->csd.cmdclass & CCC_SWITCH)) {
196 printk(KERN_WARNING "%s: card lacks mandatory switch "
197 "function, performance might suffer.\n",
198 mmc_hostname(card->host));
199 return 0;
200 }
201
202 err = -EIO;
203
204 status = kmalloc(64, GFP_KERNEL);
205 if (!status) {
206 printk(KERN_ERR "%s: could not allocate a buffer for "
207 "switch capabilities.\n", mmc_hostname(card->host));
208 return -ENOMEM;
209 }
210
211 err = mmc_sd_switch(card, 0, 0, 1, status);
212 if (err) {
213 /*
214 * We all hosts that cannot perform the command
215 * to fail more gracefully
216 */
217 if (err != -EINVAL)
218 goto out;
219
220 printk(KERN_WARNING "%s: problem reading switch "
221 "capabilities, performance might suffer.\n",
222 mmc_hostname(card->host));
223 err = 0;
224
225 goto out;
226 }
227
228 if (status[13] & 0x02)
229 card->sw_caps.hs_max_dtr = 50000000;
230
231 out:
232 kfree(status);
233
234 return err;
235 }
236
237 /*
238 * Test if the card supports high-speed mode and, if so, switch to it.
239 */
mmc_switch_hs(struct mmc_card * card)240 static int mmc_switch_hs(struct mmc_card *card)
241 {
242 int err;
243 u8 *status;
244
245 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
246 return 0;
247
248 if (!(card->csd.cmdclass & CCC_SWITCH))
249 return 0;
250
251 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
252 return 0;
253
254 if (card->sw_caps.hs_max_dtr == 0)
255 return 0;
256
257 err = -EIO;
258
259 status = kmalloc(64, GFP_KERNEL);
260 if (!status) {
261 printk(KERN_ERR "%s: could not allocate a buffer for "
262 "switch capabilities.\n", mmc_hostname(card->host));
263 return -ENOMEM;
264 }
265
266 err = mmc_sd_switch(card, 1, 0, 1, status);
267 if (err)
268 goto out;
269
270 if ((status[16] & 0xF) != 1) {
271 printk(KERN_WARNING "%s: Problem switching card "
272 "into high-speed mode!\n",
273 mmc_hostname(card->host));
274 } else {
275 mmc_card_set_highspeed(card);
276 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
277 }
278
279 out:
280 kfree(status);
281
282 return err;
283 }
284
285 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
286 card->raw_cid[2], card->raw_cid[3]);
287 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
288 card->raw_csd[2], card->raw_csd[3]);
289 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
290 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
291 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
292 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
293 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
294 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
295 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
296 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
297
298
299 static struct attribute *sd_std_attrs[] = {
300 &dev_attr_cid.attr,
301 &dev_attr_csd.attr,
302 &dev_attr_scr.attr,
303 &dev_attr_date.attr,
304 &dev_attr_fwrev.attr,
305 &dev_attr_hwrev.attr,
306 &dev_attr_manfid.attr,
307 &dev_attr_name.attr,
308 &dev_attr_oemid.attr,
309 &dev_attr_serial.attr,
310 NULL,
311 };
312
313 static struct attribute_group sd_std_attr_group = {
314 .attrs = sd_std_attrs,
315 };
316
317 static struct attribute_group *sd_attr_groups[] = {
318 &sd_std_attr_group,
319 NULL,
320 };
321
322 static struct device_type sd_type = {
323 .groups = sd_attr_groups,
324 };
325
326 /*
327 * Handle the detection and initialisation of a card.
328 *
329 * In the case of a resume, "oldcard" will contain the card
330 * we're trying to reinitialise.
331 */
mmc_sd_init_card(struct mmc_host * host,u32 ocr,struct mmc_card * oldcard)332 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
333 struct mmc_card *oldcard)
334 {
335 struct mmc_card *card;
336 int err;
337 u32 cid[4];
338 unsigned int max_dtr;
339 #ifdef CONFIG_MMC_PARANOID_SD_INIT
340 int retries;
341 #endif
342 BUG_ON(!host);
343 WARN_ON(!host->claimed);
344
345 /*
346 * Since we're changing the OCR value, we seem to
347 * need to tell some cards to go back to the idle
348 * state. We wait 1ms to give cards time to
349 * respond.
350 */
351 mmc_go_idle(host);
352
353 /*
354 * If SD_SEND_IF_COND indicates an SD 2.0
355 * compliant card and we should set bit 30
356 * of the ocr to indicate that we can handle
357 * block-addressed SDHC cards.
358 */
359 err = mmc_send_if_cond(host, ocr);
360 if (!err)
361 ocr |= 1 << 30;
362
363 err = mmc_send_app_op_cond(host, ocr, NULL);
364 if (err)
365 goto err;
366
367 /*
368 * For SPI, enable CRC as appropriate.
369 */
370 if (mmc_host_is_spi(host)) {
371 err = mmc_spi_set_crc(host, use_spi_crc);
372 if (err)
373 goto err;
374 }
375
376 /*
377 * Fetch CID from card.
378 */
379 if (mmc_host_is_spi(host))
380 err = mmc_send_cid(host, cid);
381 else
382 err = mmc_all_send_cid(host, cid);
383 if (err)
384 goto err;
385
386 if (oldcard) {
387 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
388 err = -ENOENT;
389 goto err;
390 }
391
392 card = oldcard;
393 } else {
394 /*
395 * Allocate card structure.
396 */
397 card = mmc_alloc_card(host, &sd_type);
398 if (IS_ERR(card)) {
399 err = PTR_ERR(card);
400 goto err;
401 }
402
403 card->type = MMC_TYPE_SD;
404 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
405 }
406
407 /*
408 * For native busses: get card RCA and quit open drain mode.
409 */
410 if (!mmc_host_is_spi(host)) {
411 err = mmc_send_relative_addr(host, &card->rca);
412 if (err)
413 goto free_card;
414
415 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
416 }
417
418 if (!oldcard) {
419 /*
420 * Fetch CSD from card.
421 */
422 err = mmc_send_csd(card, card->raw_csd);
423 if (err)
424 goto free_card;
425
426 err = mmc_decode_csd(card);
427 if (err)
428 goto free_card;
429
430 mmc_decode_cid(card);
431 }
432
433 /*
434 * Select card, as all following commands rely on that.
435 */
436 if (!mmc_host_is_spi(host)) {
437 err = mmc_select_card(card);
438 if (err)
439 goto free_card;
440 }
441
442 if (!oldcard) {
443 /*
444 * Fetch SCR from card.
445 */
446 err = mmc_app_send_scr(card, card->raw_scr);
447 if (err)
448 goto free_card;
449
450 err = mmc_decode_scr(card);
451 if (err < 0)
452 goto free_card;
453 /*
454 * Fetch switch information from card.
455 */
456 #ifdef CONFIG_MMC_PARANOID_SD_INIT
457 for (retries = 1; retries <= 3; retries++) {
458 err = mmc_read_switch(card);
459 if (!err) {
460 if (retries > 1) {
461 printk(KERN_WARNING
462 "%s: recovered\n",
463 mmc_hostname(host));
464 }
465 break;
466 } else {
467 printk(KERN_WARNING
468 "%s: read switch failed (attempt %d)\n",
469 mmc_hostname(host), retries);
470 }
471 }
472 #else
473 err = mmc_read_switch(card);
474 #endif
475
476 if (err)
477 goto free_card;
478 }
479
480 /*
481 * Attempt to change to high-speed (if supported)
482 */
483 err = mmc_switch_hs(card);
484 if (err)
485 goto free_card;
486
487 /*
488 * Compute bus speed.
489 */
490 max_dtr = (unsigned int)-1;
491
492 if (mmc_card_highspeed(card)) {
493 if (max_dtr > card->sw_caps.hs_max_dtr)
494 max_dtr = card->sw_caps.hs_max_dtr;
495 } else if (max_dtr > card->csd.max_dtr) {
496 max_dtr = card->csd.max_dtr;
497 }
498
499 mmc_set_clock(host, max_dtr);
500
501 /*
502 * Switch to wider bus (if supported).
503 */
504 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
505 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
506 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
507 if (err)
508 goto free_card;
509
510 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
511 }
512
513 /*
514 * Check if read-only switch is active.
515 */
516 if (!oldcard) {
517 if (!host->ops->get_ro || host->ops->get_ro(host) < 0) {
518 printk(KERN_WARNING "%s: host does not "
519 "support reading read-only "
520 "switch. assuming write-enable.\n",
521 mmc_hostname(host));
522 } else {
523 if (host->ops->get_ro(host) > 0)
524 mmc_card_set_readonly(card);
525 }
526 }
527
528 if (!oldcard)
529 host->card = card;
530
531 return 0;
532
533 free_card:
534 if (!oldcard)
535 mmc_remove_card(card);
536 err:
537
538 return err;
539 }
540
541 /*
542 * Host is being removed. Free up the current card.
543 */
mmc_sd_remove(struct mmc_host * host)544 static void mmc_sd_remove(struct mmc_host *host)
545 {
546 BUG_ON(!host);
547 BUG_ON(!host->card);
548
549 mmc_remove_card(host->card);
550 host->card = NULL;
551 }
552
553 /*
554 * Card detection callback from host.
555 */
mmc_sd_detect(struct mmc_host * host)556 static void mmc_sd_detect(struct mmc_host *host)
557 {
558 int err = 0;
559 #ifdef CONFIG_MMC_PARANOID_SD_INIT
560 int retries = 5;
561 #endif
562
563 BUG_ON(!host);
564 BUG_ON(!host->card);
565
566 mmc_claim_host(host);
567
568 /*
569 * Just check if our card has been removed.
570 */
571 #ifdef CONFIG_MMC_PARANOID_SD_INIT
572 while(retries) {
573 err = mmc_send_status(host->card, NULL);
574 if (err) {
575 retries--;
576 udelay(5);
577 continue;
578 }
579 break;
580 }
581 if (!retries) {
582 printk(KERN_ERR "%s(%s): Unable to re-detect card (%d)\n",
583 __func__, mmc_hostname(host), err);
584 }
585 #else
586 err = mmc_send_status(host->card, NULL);
587 #endif
588 mmc_release_host(host);
589
590 if (err) {
591 mmc_sd_remove(host);
592
593 mmc_claim_host(host);
594 mmc_detach_bus(host);
595 mmc_release_host(host);
596 }
597 }
598
599 #ifdef CONFIG_MMC_UNSAFE_RESUME
600
601 /*
602 * Suspend callback from host.
603 */
mmc_sd_suspend(struct mmc_host * host)604 static void mmc_sd_suspend(struct mmc_host *host)
605 {
606 BUG_ON(!host);
607 BUG_ON(!host->card);
608
609 mmc_claim_host(host);
610 if (!mmc_host_is_spi(host))
611 mmc_deselect_cards(host);
612 host->card->state &= ~MMC_STATE_HIGHSPEED;
613 mmc_release_host(host);
614 }
615
616 /*
617 * Resume callback from host.
618 *
619 * This function tries to determine if the same card is still present
620 * and, if so, restore all state to it.
621 */
mmc_sd_resume(struct mmc_host * host)622 static void mmc_sd_resume(struct mmc_host *host)
623 {
624 int err;
625 #ifdef CONFIG_MMC_PARANOID_SD_INIT
626 int retries;
627 #endif
628
629 BUG_ON(!host);
630 BUG_ON(!host->card);
631
632 mmc_claim_host(host);
633 #ifdef CONFIG_MMC_PARANOID_SD_INIT
634 retries = 5;
635 while (retries) {
636 err = mmc_sd_init_card(host, host->ocr, host->card);
637
638 if (err) {
639 printk(KERN_ERR "%s: Re-init card rc = %d (retries = %d)\n",
640 mmc_hostname(host), err, retries);
641 mdelay(5);
642 retries--;
643 continue;
644 }
645 break;
646 }
647 #else
648 err = mmc_sd_init_card(host, host->ocr, host->card);
649 #endif
650 mmc_release_host(host);
651
652 if (err) {
653 mmc_sd_remove(host);
654
655 mmc_claim_host(host);
656 mmc_detach_bus(host);
657 mmc_release_host(host);
658 }
659
660 }
661
662 #else
663
664 #define mmc_sd_suspend NULL
665 #define mmc_sd_resume NULL
666
667 #endif
668
669 static const struct mmc_bus_ops mmc_sd_ops = {
670 .remove = mmc_sd_remove,
671 .detect = mmc_sd_detect,
672 .suspend = mmc_sd_suspend,
673 .resume = mmc_sd_resume,
674 };
675
676 /*
677 * Starting point for SD card init.
678 */
mmc_attach_sd(struct mmc_host * host,u32 ocr)679 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
680 {
681 int err;
682 #ifdef CONFIG_MMC_PARANOID_SD_INIT
683 int retries;
684 #endif
685
686 BUG_ON(!host);
687 WARN_ON(!host->claimed);
688
689 mmc_attach_bus(host, &mmc_sd_ops);
690
691 /*
692 * We need to get OCR a different way for SPI.
693 */
694 if (mmc_host_is_spi(host)) {
695 mmc_go_idle(host);
696
697 err = mmc_spi_read_ocr(host, 0, &ocr);
698 if (err)
699 goto err;
700 }
701
702 /*
703 * Sanity check the voltages that the card claims to
704 * support.
705 */
706 if (ocr & 0x7F) {
707 printk(KERN_WARNING "%s: card claims to support voltages "
708 "below the defined range. These will be ignored.\n",
709 mmc_hostname(host));
710 ocr &= ~0x7F;
711 }
712
713 if (ocr & MMC_VDD_165_195) {
714 printk(KERN_WARNING "%s: SD card claims to support the "
715 "incompletely defined 'low voltage range'. This "
716 "will be ignored.\n", mmc_hostname(host));
717 ocr &= ~MMC_VDD_165_195;
718 }
719
720 host->ocr = mmc_select_voltage(host, ocr);
721
722 /*
723 * Can we support the voltage(s) of the card(s)?
724 */
725 if (!host->ocr) {
726 err = -EINVAL;
727 goto err;
728 }
729
730 /*
731 * Detect and init the card.
732 */
733 #ifdef CONFIG_MMC_PARANOID_SD_INIT
734 retries = 5;
735 while (retries) {
736 err = mmc_sd_init_card(host, host->ocr, NULL);
737 if (err) {
738 retries--;
739 continue;
740 }
741 break;
742 }
743
744 if (!retries) {
745 printk(KERN_ERR "%s: mmc_sd_init_card() failure (err = %d)\n",
746 mmc_hostname(host), err);
747 goto err;
748 }
749 #else
750 err = mmc_sd_init_card(host, host->ocr, NULL);
751 if (err)
752 goto err;
753 #endif
754
755 mmc_release_host(host);
756
757 err = mmc_add_card(host->card);
758 if (err)
759 goto remove_card;
760
761 return 0;
762
763 remove_card:
764 mmc_remove_card(host->card);
765 host->card = NULL;
766 mmc_claim_host(host);
767 err:
768 mmc_detach_bus(host);
769 mmc_release_host(host);
770
771 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
772 mmc_hostname(host), err);
773
774 return err;
775 }
776
777