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>
26 static const unsigned int tran_exp
[] = {
27 10000, 100000, 1000000, 10000000,
31 static const unsigned char tran_mant
[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
36 static const unsigned int tacc_exp
[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
40 static const unsigned int tacc_mant
[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
45 #define UNSTUFF_BITS(resp,start,size) \
47 const int __size = size; \
48 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
49 const int __off = 3 - ((start) / 32); \
50 const int __shft = (start) & 31; \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
62 void mmc_decode_cid(struct mmc_card
*card
)
64 u32
*resp
= card
->raw_cid
;
66 memset(&card
->cid
, 0, sizeof(struct mmc_cid
));
69 * SD doesn't currently have a version field so we will
70 * have to assume we can parse this.
72 card
->cid
.manfid
= UNSTUFF_BITS(resp
, 120, 8);
73 card
->cid
.oemid
= UNSTUFF_BITS(resp
, 104, 16);
74 card
->cid
.prod_name
[0] = UNSTUFF_BITS(resp
, 96, 8);
75 card
->cid
.prod_name
[1] = UNSTUFF_BITS(resp
, 88, 8);
76 card
->cid
.prod_name
[2] = UNSTUFF_BITS(resp
, 80, 8);
77 card
->cid
.prod_name
[3] = UNSTUFF_BITS(resp
, 72, 8);
78 card
->cid
.prod_name
[4] = UNSTUFF_BITS(resp
, 64, 8);
79 card
->cid
.hwrev
= UNSTUFF_BITS(resp
, 60, 4);
80 card
->cid
.fwrev
= UNSTUFF_BITS(resp
, 56, 4);
81 card
->cid
.serial
= UNSTUFF_BITS(resp
, 24, 32);
82 card
->cid
.year
= UNSTUFF_BITS(resp
, 12, 8);
83 card
->cid
.month
= UNSTUFF_BITS(resp
, 8, 4);
85 card
->cid
.year
+= 2000; /* SD cards year offset */
89 * Given a 128-bit response, decode to our card CSD structure.
91 static int mmc_decode_csd(struct mmc_card
*card
)
93 struct mmc_csd
*csd
= &card
->csd
;
94 unsigned int e
, m
, csd_struct
;
95 u32
*resp
= card
->raw_csd
;
97 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
101 m
= UNSTUFF_BITS(resp
, 115, 4);
102 e
= UNSTUFF_BITS(resp
, 112, 3);
103 csd
->tacc_ns
= (tacc_exp
[e
] * tacc_mant
[m
] + 9) / 10;
104 csd
->tacc_clks
= UNSTUFF_BITS(resp
, 104, 8) * 100;
106 m
= UNSTUFF_BITS(resp
, 99, 4);
107 e
= UNSTUFF_BITS(resp
, 96, 3);
108 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
109 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
111 e
= UNSTUFF_BITS(resp
, 47, 3);
112 m
= UNSTUFF_BITS(resp
, 62, 12);
113 csd
->capacity
= (1 + m
) << (e
+ 2);
115 csd
->read_blkbits
= UNSTUFF_BITS(resp
, 80, 4);
116 csd
->read_partial
= UNSTUFF_BITS(resp
, 79, 1);
117 csd
->write_misalign
= UNSTUFF_BITS(resp
, 78, 1);
118 csd
->read_misalign
= UNSTUFF_BITS(resp
, 77, 1);
119 csd
->r2w_factor
= UNSTUFF_BITS(resp
, 26, 3);
120 csd
->write_blkbits
= UNSTUFF_BITS(resp
, 22, 4);
121 csd
->write_partial
= UNSTUFF_BITS(resp
, 21, 1);
123 if (UNSTUFF_BITS(resp
, 46, 1)) {
125 } else if (csd
->write_blkbits
>= 9) {
126 csd
->erase_size
= UNSTUFF_BITS(resp
, 39, 7) + 1;
127 csd
->erase_size
<<= csd
->write_blkbits
- 9;
132 * This is a block-addressed SDHC card. Most
133 * interesting fields are unused and have fixed
134 * values. To avoid getting tripped by buggy cards,
135 * we assume those fixed values ourselves.
137 mmc_card_set_blockaddr(card
);
139 csd
->tacc_ns
= 0; /* Unused */
140 csd
->tacc_clks
= 0; /* Unused */
142 m
= UNSTUFF_BITS(resp
, 99, 4);
143 e
= UNSTUFF_BITS(resp
, 96, 3);
144 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
145 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
147 m
= UNSTUFF_BITS(resp
, 48, 22);
148 csd
->capacity
= (1 + m
) << 10;
150 csd
->read_blkbits
= 9;
151 csd
->read_partial
= 0;
152 csd
->write_misalign
= 0;
153 csd
->read_misalign
= 0;
154 csd
->r2w_factor
= 4; /* Unused */
155 csd
->write_blkbits
= 9;
156 csd
->write_partial
= 0;
160 printk(KERN_ERR
"%s: unrecognised CSD structure version %d\n",
161 mmc_hostname(card
->host
), csd_struct
);
165 card
->erase_size
= csd
->erase_size
;
171 * Given a 64-bit response, decode to our card SCR structure.
173 static int mmc_decode_scr(struct mmc_card
*card
)
175 struct sd_scr
*scr
= &card
->scr
;
176 unsigned int scr_struct
;
179 resp
[3] = card
->raw_scr
[1];
180 resp
[2] = card
->raw_scr
[0];
182 scr_struct
= UNSTUFF_BITS(resp
, 60, 4);
183 if (scr_struct
!= 0) {
184 printk(KERN_ERR
"%s: unrecognised SCR structure version %d\n",
185 mmc_hostname(card
->host
), scr_struct
);
189 scr
->sda_vsn
= UNSTUFF_BITS(resp
, 56, 4);
190 scr
->bus_widths
= UNSTUFF_BITS(resp
, 48, 4);
192 if (UNSTUFF_BITS(resp
, 55, 1))
193 card
->erased_byte
= 0xFF;
195 card
->erased_byte
= 0x0;
201 * Fetch and process SD Status register.
203 static int mmc_read_ssr(struct mmc_card
*card
)
205 unsigned int au
, es
, et
, eo
;
209 if (!(card
->csd
.cmdclass
& CCC_APP_SPEC
)) {
210 printk(KERN_WARNING
"%s: card lacks mandatory SD Status "
211 "function.\n", mmc_hostname(card
->host
));
215 ssr
= kmalloc(64, GFP_KERNEL
);
219 err
= mmc_app_sd_status(card
, ssr
);
221 printk(KERN_WARNING
"%s: problem reading SD Status "
222 "register.\n", mmc_hostname(card
->host
));
227 for (i
= 0; i
< 16; i
++)
228 ssr
[i
] = be32_to_cpu(ssr
[i
]);
231 * UNSTUFF_BITS only works with four u32s so we have to offset the
232 * bitfield positions accordingly.
234 au
= UNSTUFF_BITS(ssr
, 428 - 384, 4);
235 if (au
> 0 || au
<= 9) {
236 card
->ssr
.au
= 1 << (au
+ 4);
237 es
= UNSTUFF_BITS(ssr
, 408 - 384, 16);
238 et
= UNSTUFF_BITS(ssr
, 402 - 384, 6);
239 eo
= UNSTUFF_BITS(ssr
, 400 - 384, 2);
241 card
->ssr
.erase_timeout
= (et
* 1000) / es
;
242 card
->ssr
.erase_offset
= eo
* 1000;
245 printk(KERN_WARNING
"%s: SD Status: Invalid Allocation Unit "
246 "size.\n", mmc_hostname(card
->host
));
254 * Fetches and decodes switch information
256 static int mmc_read_switch(struct mmc_card
*card
)
261 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
264 if (!(card
->csd
.cmdclass
& CCC_SWITCH
)) {
265 printk(KERN_WARNING
"%s: card lacks mandatory switch "
266 "function, performance might suffer.\n",
267 mmc_hostname(card
->host
));
273 status
= kmalloc(64, GFP_KERNEL
);
275 printk(KERN_ERR
"%s: could not allocate a buffer for "
276 "switch capabilities.\n", mmc_hostname(card
->host
));
280 err
= mmc_sd_switch(card
, 0, 0, 1, status
);
282 /* If the host or the card can't do the switch,
283 * fail more gracefully. */
289 printk(KERN_WARNING
"%s: problem reading switch "
290 "capabilities, performance might suffer.\n",
291 mmc_hostname(card
->host
));
297 if (status
[13] & 0x02)
298 card
->sw_caps
.hs_max_dtr
= 50000000;
307 * Test if the card supports high-speed mode and, if so, switch to it.
309 int mmc_sd_switch_hs(struct mmc_card
*card
)
314 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
317 if (!(card
->csd
.cmdclass
& CCC_SWITCH
))
320 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
323 if (card
->sw_caps
.hs_max_dtr
== 0)
328 status
= kmalloc(64, GFP_KERNEL
);
330 printk(KERN_ERR
"%s: could not allocate a buffer for "
331 "switch capabilities.\n", mmc_hostname(card
->host
));
335 err
= mmc_sd_switch(card
, 1, 0, 1, status
);
339 if ((status
[16] & 0xF) != 1) {
340 printk(KERN_WARNING
"%s: Problem switching card "
341 "into high-speed mode!\n",
342 mmc_hostname(card
->host
));
354 MMC_DEV_ATTR(cid
, "%08x%08x%08x%08x\n", card
->raw_cid
[0], card
->raw_cid
[1],
355 card
->raw_cid
[2], card
->raw_cid
[3]);
356 MMC_DEV_ATTR(csd
, "%08x%08x%08x%08x\n", card
->raw_csd
[0], card
->raw_csd
[1],
357 card
->raw_csd
[2], card
->raw_csd
[3]);
358 MMC_DEV_ATTR(scr
, "%08x%08x\n", card
->raw_scr
[0], card
->raw_scr
[1]);
359 MMC_DEV_ATTR(date
, "%02d/%04d\n", card
->cid
.month
, card
->cid
.year
);
360 MMC_DEV_ATTR(erase_size
, "%u\n", card
->erase_size
<< 9);
361 MMC_DEV_ATTR(preferred_erase_size
, "%u\n", card
->pref_erase
<< 9);
362 MMC_DEV_ATTR(fwrev
, "0x%x\n", card
->cid
.fwrev
);
363 MMC_DEV_ATTR(hwrev
, "0x%x\n", card
->cid
.hwrev
);
364 MMC_DEV_ATTR(manfid
, "0x%06x\n", card
->cid
.manfid
);
365 MMC_DEV_ATTR(name
, "%s\n", card
->cid
.prod_name
);
366 MMC_DEV_ATTR(oemid
, "0x%04x\n", card
->cid
.oemid
);
367 MMC_DEV_ATTR(serial
, "0x%08x\n", card
->cid
.serial
);
370 static struct attribute
*sd_std_attrs
[] = {
375 &dev_attr_erase_size
.attr
,
376 &dev_attr_preferred_erase_size
.attr
,
377 &dev_attr_fwrev
.attr
,
378 &dev_attr_hwrev
.attr
,
379 &dev_attr_manfid
.attr
,
381 &dev_attr_oemid
.attr
,
382 &dev_attr_serial
.attr
,
386 static struct attribute_group sd_std_attr_group
= {
387 .attrs
= sd_std_attrs
,
390 static const struct attribute_group
*sd_attr_groups
[] = {
395 struct device_type sd_type
= {
396 .groups
= sd_attr_groups
,
400 * Fetch CID from card.
402 int mmc_sd_get_cid(struct mmc_host
*host
, u32 ocr
, u32
*cid
)
407 * Since we're changing the OCR value, we seem to
408 * need to tell some cards to go back to the idle
409 * state. We wait 1ms to give cards time to
415 * If SD_SEND_IF_COND indicates an SD 2.0
416 * compliant card and we should set bit 30
417 * of the ocr to indicate that we can handle
418 * block-addressed SDHC cards.
420 err
= mmc_send_if_cond(host
, ocr
);
424 err
= mmc_send_app_op_cond(host
, ocr
, NULL
);
428 if (mmc_host_is_spi(host
))
429 err
= mmc_send_cid(host
, cid
);
431 err
= mmc_all_send_cid(host
, cid
);
436 int mmc_sd_get_csd(struct mmc_host
*host
, struct mmc_card
*card
)
441 * Fetch CSD from card.
443 err
= mmc_send_csd(card
, card
->raw_csd
);
447 err
= mmc_decode_csd(card
);
454 int mmc_sd_setup_card(struct mmc_host
*host
, struct mmc_card
*card
,
461 * Fetch SCR from card.
463 err
= mmc_app_send_scr(card
, card
->raw_scr
);
467 err
= mmc_decode_scr(card
);
472 * Fetch and process SD Status register.
474 err
= mmc_read_ssr(card
);
478 /* Erase init depends on CSD and SSR */
479 mmc_init_erase(card
);
482 * Fetch switch information from card.
484 err
= mmc_read_switch(card
);
490 * For SPI, enable CRC as appropriate.
491 * This CRC enable is located AFTER the reading of the
492 * card registers because some SDHC cards are not able
493 * to provide valid CRCs for non-512-byte blocks.
495 if (mmc_host_is_spi(host
)) {
496 err
= mmc_spi_set_crc(host
, use_spi_crc
);
502 * Check if read-only switch is active.
507 if (host
->ops
->get_ro
)
508 ro
= host
->ops
->get_ro(host
);
511 printk(KERN_WARNING
"%s: host does not "
512 "support reading read-only "
513 "switch. assuming write-enable.\n",
516 mmc_card_set_readonly(card
);
523 unsigned mmc_sd_get_max_clock(struct mmc_card
*card
)
525 unsigned max_dtr
= (unsigned int)-1;
527 if (mmc_card_highspeed(card
)) {
528 if (max_dtr
> card
->sw_caps
.hs_max_dtr
)
529 max_dtr
= card
->sw_caps
.hs_max_dtr
;
530 } else if (max_dtr
> card
->csd
.max_dtr
) {
531 max_dtr
= card
->csd
.max_dtr
;
537 void mmc_sd_go_highspeed(struct mmc_card
*card
)
539 mmc_card_set_highspeed(card
);
540 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
544 * Handle the detection and initialisation of a card.
546 * In the case of a resume, "oldcard" will contain the card
547 * we're trying to reinitialise.
549 static int mmc_sd_init_card(struct mmc_host
*host
, u32 ocr
,
550 struct mmc_card
*oldcard
)
552 struct mmc_card
*card
;
557 WARN_ON(!host
->claimed
);
559 err
= mmc_sd_get_cid(host
, ocr
, cid
);
564 if (memcmp(cid
, oldcard
->raw_cid
, sizeof(cid
)) != 0)
570 * Allocate card structure.
572 card
= mmc_alloc_card(host
, &sd_type
);
574 return PTR_ERR(card
);
576 card
->type
= MMC_TYPE_SD
;
577 memcpy(card
->raw_cid
, cid
, sizeof(card
->raw_cid
));
581 * For native busses: get card RCA and quit open drain mode.
583 if (!mmc_host_is_spi(host
)) {
584 err
= mmc_send_relative_addr(host
, &card
->rca
);
588 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
592 err
= mmc_sd_get_csd(host
, card
);
596 mmc_decode_cid(card
);
600 * Select card, as all following commands rely on that.
602 if (!mmc_host_is_spi(host
)) {
603 err
= mmc_select_card(card
);
608 err
= mmc_sd_setup_card(host
, card
, oldcard
!= NULL
);
613 * Attempt to change to high-speed (if supported)
615 err
= mmc_sd_switch_hs(card
);
617 mmc_sd_go_highspeed(card
);
624 mmc_set_clock(host
, mmc_sd_get_max_clock(card
));
627 * Switch to wider bus (if supported).
629 if ((host
->caps
& MMC_CAP_4_BIT_DATA
) &&
630 (card
->scr
.bus_widths
& SD_SCR_BUS_WIDTH_4
)) {
631 err
= mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_4
);
635 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
643 mmc_remove_card(card
);
649 * Host is being removed. Free up the current card.
651 static void mmc_sd_remove(struct mmc_host
*host
)
656 mmc_remove_card(host
->card
);
661 * Card detection callback from host.
663 static void mmc_sd_detect(struct mmc_host
*host
)
670 mmc_claim_host(host
);
673 * Just check if our card has been removed.
675 err
= mmc_send_status(host
->card
, NULL
);
677 mmc_release_host(host
);
682 mmc_claim_host(host
);
683 mmc_detach_bus(host
);
684 mmc_release_host(host
);
689 * Suspend callback from host.
691 static int mmc_sd_suspend(struct mmc_host
*host
)
696 mmc_claim_host(host
);
697 if (!mmc_host_is_spi(host
))
698 mmc_deselect_cards(host
);
699 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
700 mmc_release_host(host
);
706 * Resume callback from host.
708 * This function tries to determine if the same card is still present
709 * and, if so, restore all state to it.
711 static int mmc_sd_resume(struct mmc_host
*host
)
718 mmc_claim_host(host
);
719 err
= mmc_sd_init_card(host
, host
->ocr
, host
->card
);
720 mmc_release_host(host
);
725 static int mmc_sd_power_restore(struct mmc_host
*host
)
729 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
730 mmc_claim_host(host
);
731 ret
= mmc_sd_init_card(host
, host
->ocr
, host
->card
);
732 mmc_release_host(host
);
737 static const struct mmc_bus_ops mmc_sd_ops
= {
738 .remove
= mmc_sd_remove
,
739 .detect
= mmc_sd_detect
,
742 .power_restore
= mmc_sd_power_restore
,
745 static const struct mmc_bus_ops mmc_sd_ops_unsafe
= {
746 .remove
= mmc_sd_remove
,
747 .detect
= mmc_sd_detect
,
748 .suspend
= mmc_sd_suspend
,
749 .resume
= mmc_sd_resume
,
750 .power_restore
= mmc_sd_power_restore
,
753 static void mmc_sd_attach_bus_ops(struct mmc_host
*host
)
755 const struct mmc_bus_ops
*bus_ops
;
757 if (!mmc_card_is_removable(host
))
758 bus_ops
= &mmc_sd_ops_unsafe
;
760 bus_ops
= &mmc_sd_ops
;
761 mmc_attach_bus(host
, bus_ops
);
765 * Starting point for SD card init.
767 int mmc_attach_sd(struct mmc_host
*host
)
773 WARN_ON(!host
->claimed
);
775 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
779 mmc_sd_attach_bus_ops(host
);
780 if (host
->ocr_avail_sd
)
781 host
->ocr_avail
= host
->ocr_avail_sd
;
784 * We need to get OCR a different way for SPI.
786 if (mmc_host_is_spi(host
)) {
789 err
= mmc_spi_read_ocr(host
, 0, &ocr
);
795 * Sanity check the voltages that the card claims to
799 printk(KERN_WARNING
"%s: card claims to support voltages "
800 "below the defined range. These will be ignored.\n",
805 if ((ocr
& MMC_VDD_165_195
) &&
806 !(host
->ocr_avail_sd
& MMC_VDD_165_195
)) {
807 printk(KERN_WARNING
"%s: SD card claims to support the "
808 "incompletely defined 'low voltage range'. This "
809 "will be ignored.\n", mmc_hostname(host
));
810 ocr
&= ~MMC_VDD_165_195
;
813 host
->ocr
= mmc_select_voltage(host
, ocr
);
816 * Can we support the voltage(s) of the card(s)?
824 * Detect and init the card.
826 err
= mmc_sd_init_card(host
, host
->ocr
, NULL
);
830 mmc_release_host(host
);
831 err
= mmc_add_card(host
->card
);
832 mmc_claim_host(host
);
839 mmc_release_host(host
);
840 mmc_remove_card(host
->card
);
842 mmc_claim_host(host
);
844 mmc_detach_bus(host
);
846 printk(KERN_ERR
"%s: error %d whilst initialising SD card\n",
847 mmc_hostname(host
), err
);