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>
15 #include <linux/mmc/host.h>
16 #include <linux/mmc/card.h>
17 #include <linux/mmc/mmc.h>
18 #include <linux/mmc/sd.h>
25 static const unsigned int tran_exp
[] = {
26 10000, 100000, 1000000, 10000000,
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,
35 static const unsigned int tacc_exp
[] = {
36 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
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,
44 #ifndef CONFIG_ARCH_MOXART
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); \
59 #define UNSTUFF_BITS(resp, start, size) \
61 const int __size = size; \
62 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
63 const int __off = ((start) / 32); \
64 const int __shft = (start) & 31; \
67 __res = resp[__off] >> __shft; \
68 if (__size + __shft > 32) \
69 __res |= resp[__off+1] << ((32 - __shft) % 32); \
75 * Given the decoded CSD structure, decode the raw CID to our CID structure.
77 static void mmc_decode_cid(struct mmc_card
*card
)
79 u32
*resp
= card
->raw_cid
;
81 memset(&card
->cid
, 0, sizeof(struct mmc_cid
));
84 * SD doesn't currently have a version field so we will
85 * have to assume we can parse this.
87 card
->cid
.manfid
= UNSTUFF_BITS(resp
, 120, 8);
88 card
->cid
.oemid
= UNSTUFF_BITS(resp
, 104, 16);
89 card
->cid
.prod_name
[0] = UNSTUFF_BITS(resp
, 96, 8);
90 card
->cid
.prod_name
[1] = UNSTUFF_BITS(resp
, 88, 8);
91 card
->cid
.prod_name
[2] = UNSTUFF_BITS(resp
, 80, 8);
92 card
->cid
.prod_name
[3] = UNSTUFF_BITS(resp
, 72, 8);
93 card
->cid
.prod_name
[4] = UNSTUFF_BITS(resp
, 64, 8);
94 card
->cid
.hwrev
= UNSTUFF_BITS(resp
, 60, 4);
95 card
->cid
.fwrev
= UNSTUFF_BITS(resp
, 56, 4);
96 card
->cid
.serial
= UNSTUFF_BITS(resp
, 24, 32);
97 card
->cid
.year
= UNSTUFF_BITS(resp
, 12, 8);
98 card
->cid
.month
= UNSTUFF_BITS(resp
, 8, 4);
100 card
->cid
.year
+= 2000; /* SD cards year offset */
104 * Given a 128-bit response, decode to our card CSD structure.
106 static int mmc_decode_csd(struct mmc_card
*card
)
108 struct mmc_csd
*csd
= &card
->csd
;
109 unsigned int e
, m
, csd_struct
;
110 u32
*resp
= card
->raw_csd
;
112 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
114 switch (csd_struct
) {
116 m
= UNSTUFF_BITS(resp
, 115, 4);
117 e
= UNSTUFF_BITS(resp
, 112, 3);
118 csd
->tacc_ns
= (tacc_exp
[e
] * tacc_mant
[m
] + 9) / 10;
119 csd
->tacc_clks
= UNSTUFF_BITS(resp
, 104, 8) * 100;
121 m
= UNSTUFF_BITS(resp
, 99, 4);
122 e
= UNSTUFF_BITS(resp
, 96, 3);
123 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
124 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
126 e
= UNSTUFF_BITS(resp
, 47, 3);
127 m
= UNSTUFF_BITS(resp
, 62, 12);
128 csd
->capacity
= (1 + m
) << (e
+ 2);
130 csd
->read_blkbits
= UNSTUFF_BITS(resp
, 80, 4);
131 csd
->read_partial
= UNSTUFF_BITS(resp
, 79, 1);
132 csd
->write_misalign
= UNSTUFF_BITS(resp
, 78, 1);
133 csd
->read_misalign
= UNSTUFF_BITS(resp
, 77, 1);
134 csd
->r2w_factor
= UNSTUFF_BITS(resp
, 26, 3);
135 csd
->write_blkbits
= UNSTUFF_BITS(resp
, 22, 4);
136 csd
->write_partial
= UNSTUFF_BITS(resp
, 21, 1);
140 * This is a block-addressed SDHC card. Most
141 * interesting fields are unused and have fixed
142 * values. To avoid getting tripped by buggy cards,
143 * we assume those fixed values ourselves.
145 mmc_card_set_blockaddr(card
);
147 csd
->tacc_ns
= 0; /* Unused */
148 csd
->tacc_clks
= 0; /* Unused */
150 m
= UNSTUFF_BITS(resp
, 99, 4);
151 e
= UNSTUFF_BITS(resp
, 96, 3);
152 csd
->max_dtr
= tran_exp
[e
] * tran_mant
[m
];
153 csd
->cmdclass
= UNSTUFF_BITS(resp
, 84, 12);
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;
167 printk(KERN_ERR
"%s: unrecognised CSD structure version %d\n",
168 mmc_hostname(card
->host
), csd_struct
);
176 * Given a 64-bit response, decode to our card SCR structure.
178 static int mmc_decode_scr(struct mmc_card
*card
)
180 struct sd_scr
*scr
= &card
->scr
;
181 unsigned int scr_struct
;
184 resp
[3] = card
->raw_scr
[1];
185 resp
[2] = card
->raw_scr
[0];
187 scr_struct
= UNSTUFF_BITS(resp
, 60, 4);
188 if (scr_struct
!= 0) {
189 printk(KERN_ERR
"%s: unrecognised SCR structure version %d\n",
190 mmc_hostname(card
->host
), scr_struct
);
194 scr
->sda_vsn
= UNSTUFF_BITS(resp
, 56, 4);
195 scr
->bus_widths
= UNSTUFF_BITS(resp
, 48, 4);
201 * Fetches and decodes switch information
203 static int mmc_read_switch(struct mmc_card
*card
)
208 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
211 if (!(card
->csd
.cmdclass
& CCC_SWITCH
)) {
212 printk(KERN_WARNING
"%s: card lacks mandatory switch "
213 "function, performance might suffer.\n",
214 mmc_hostname(card
->host
));
220 status
= kmalloc(64, GFP_KERNEL
);
222 printk(KERN_ERR
"%s: could not allocate a buffer for "
223 "switch capabilities.\n", mmc_hostname(card
->host
));
227 err
= mmc_sd_switch(card
, 0, 0, 1, status
);
229 /* If the host or the card can't do the switch,
230 * fail more gracefully. */
236 printk(KERN_WARNING
"%s: problem reading switch "
237 "capabilities, performance might suffer.\n",
238 mmc_hostname(card
->host
));
244 if (status
[13] & 0x02)
245 card
->sw_caps
.hs_max_dtr
= 50000000;
254 * Test if the card supports high-speed mode and, if so, switch to it.
256 static int mmc_switch_hs(struct mmc_card
*card
)
261 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
264 if (!(card
->csd
.cmdclass
& CCC_SWITCH
))
267 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
270 if (card
->sw_caps
.hs_max_dtr
== 0)
275 status
= kmalloc(64, GFP_KERNEL
);
277 printk(KERN_ERR
"%s: could not allocate a buffer for "
278 "switch capabilities.\n", mmc_hostname(card
->host
));
282 err
= mmc_sd_switch(card
, 1, 0, 1, status
);
286 if ((status
[16] & 0xF) != 1) {
287 printk(KERN_WARNING
"%s: Problem switching card "
288 "into high-speed mode!\n",
289 mmc_hostname(card
->host
));
291 mmc_card_set_highspeed(card
);
292 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
301 MMC_DEV_ATTR(cid
, "%08x%08x%08x%08x\n", card
->raw_cid
[0], card
->raw_cid
[1],
302 card
->raw_cid
[2], card
->raw_cid
[3]);
303 MMC_DEV_ATTR(csd
, "%08x%08x%08x%08x\n", card
->raw_csd
[0], card
->raw_csd
[1],
304 card
->raw_csd
[2], card
->raw_csd
[3]);
305 MMC_DEV_ATTR(scr
, "%08x%08x\n", card
->raw_scr
[0], card
->raw_scr
[1]);
306 MMC_DEV_ATTR(date
, "%02d/%04d\n", card
->cid
.month
, card
->cid
.year
);
307 MMC_DEV_ATTR(fwrev
, "0x%x\n", card
->cid
.fwrev
);
308 MMC_DEV_ATTR(hwrev
, "0x%x\n", card
->cid
.hwrev
);
309 MMC_DEV_ATTR(manfid
, "0x%06x\n", card
->cid
.manfid
);
310 MMC_DEV_ATTR(name
, "%s\n", card
->cid
.prod_name
);
311 MMC_DEV_ATTR(oemid
, "0x%04x\n", card
->cid
.oemid
);
312 MMC_DEV_ATTR(serial
, "0x%08x\n", card
->cid
.serial
);
315 static struct attribute
*sd_std_attrs
[] = {
320 &dev_attr_fwrev
.attr
,
321 &dev_attr_hwrev
.attr
,
322 &dev_attr_manfid
.attr
,
324 &dev_attr_oemid
.attr
,
325 &dev_attr_serial
.attr
,
329 static struct attribute_group sd_std_attr_group
= {
330 .attrs
= sd_std_attrs
,
333 static const struct attribute_group
*sd_attr_groups
[] = {
338 static struct device_type sd_type
= {
339 .groups
= sd_attr_groups
,
343 * Handle the detection and initialisation of a card.
345 * In the case of a resume, "oldcard" will contain the card
346 * we're trying to reinitialise.
348 static int mmc_sd_init_card(struct mmc_host
*host
, u32 ocr
,
349 struct mmc_card
*oldcard
)
351 struct mmc_card
*card
;
354 unsigned int max_dtr
;
357 WARN_ON(!host
->claimed
);
360 * Since we're changing the OCR value, we seem to
361 * need to tell some cards to go back to the idle
362 * state. We wait 1ms to give cards time to
368 * If SD_SEND_IF_COND indicates an SD 2.0
369 * compliant card and we should set bit 30
370 * of the ocr to indicate that we can handle
371 * block-addressed SDHC cards.
373 err
= mmc_send_if_cond(host
, ocr
);
377 err
= mmc_send_app_op_cond(host
, ocr
, NULL
);
382 * Fetch CID from card.
384 if (mmc_host_is_spi(host
))
385 err
= mmc_send_cid(host
, cid
);
387 err
= mmc_all_send_cid(host
, cid
);
392 if (memcmp(cid
, oldcard
->raw_cid
, sizeof(cid
)) != 0) {
400 * Allocate card structure.
402 card
= mmc_alloc_card(host
, &sd_type
);
408 card
->type
= MMC_TYPE_SD
;
409 memcpy(card
->raw_cid
, cid
, sizeof(card
->raw_cid
));
413 * For native busses: get card RCA and quit open drain mode.
415 if (!mmc_host_is_spi(host
)) {
416 err
= mmc_send_relative_addr(host
, &card
->rca
);
420 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
425 * Fetch CSD from card.
427 err
= mmc_send_csd(card
, card
->raw_csd
);
431 err
= mmc_decode_csd(card
);
435 mmc_decode_cid(card
);
439 * Select card, as all following commands rely on that.
441 if (!mmc_host_is_spi(host
)) {
442 err
= mmc_select_card(card
);
449 * Fetch SCR from card.
451 err
= mmc_app_send_scr(card
, card
->raw_scr
);
455 err
= mmc_decode_scr(card
);
460 * Fetch switch information from card.
462 err
= mmc_read_switch(card
);
468 * For SPI, enable CRC as appropriate.
469 * This CRC enable is located AFTER the reading of the
470 * card registers because some SDHC cards are not able
471 * to provide valid CRCs for non-512-byte blocks.
473 if (mmc_host_is_spi(host
)) {
474 err
= mmc_spi_set_crc(host
, use_spi_crc
);
480 * Attempt to change to high-speed (if supported)
482 err
= mmc_switch_hs(card
);
489 max_dtr
= (unsigned int)-1;
491 if (mmc_card_highspeed(card
)) {
492 if (max_dtr
> card
->sw_caps
.hs_max_dtr
)
493 max_dtr
= card
->sw_caps
.hs_max_dtr
;
494 } else if (max_dtr
> card
->csd
.max_dtr
) {
495 max_dtr
= card
->csd
.max_dtr
;
498 mmc_set_clock(host
, max_dtr
);
501 * Switch to wider bus (if supported).
503 if ((host
->caps
& MMC_CAP_4_BIT_DATA
) &&
504 (card
->scr
.bus_widths
& SD_SCR_BUS_WIDTH_4
)) {
505 err
= mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_4
);
509 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
513 * Check if read-only switch is active.
516 if (!host
->ops
->get_ro
|| host
->ops
->get_ro(host
) < 0) {
517 printk(KERN_WARNING
"%s: host does not "
518 "support reading read-only "
519 "switch. assuming write-enable.\n",
522 if (host
->ops
->get_ro(host
) > 0)
523 mmc_card_set_readonly(card
);
534 mmc_remove_card(card
);
541 * Host is being removed. Free up the current card.
543 static void mmc_sd_remove(struct mmc_host
*host
)
548 mmc_remove_card(host
->card
);
553 * Card detection callback from host.
555 static void mmc_sd_detect(struct mmc_host
*host
)
562 mmc_claim_host(host
);
565 * Just check if our card has been removed.
567 err
= mmc_send_status(host
->card
, NULL
);
569 mmc_release_host(host
);
574 mmc_claim_host(host
);
575 mmc_detach_bus(host
);
576 mmc_release_host(host
);
581 * Suspend callback from host.
583 static int mmc_sd_suspend(struct mmc_host
*host
)
588 mmc_claim_host(host
);
589 if (!mmc_host_is_spi(host
))
590 mmc_deselect_cards(host
);
591 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
592 mmc_release_host(host
);
598 * Resume callback from host.
600 * This function tries to determine if the same card is still present
601 * and, if so, restore all state to it.
603 static int mmc_sd_resume(struct mmc_host
*host
)
610 mmc_claim_host(host
);
611 err
= mmc_sd_init_card(host
, host
->ocr
, host
->card
);
612 mmc_release_host(host
);
617 static void mmc_sd_power_restore(struct mmc_host
*host
)
619 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
620 mmc_claim_host(host
);
621 mmc_sd_init_card(host
, host
->ocr
, host
->card
);
622 mmc_release_host(host
);
625 #ifdef CONFIG_MMC_UNSAFE_RESUME
627 static const struct mmc_bus_ops mmc_sd_ops
= {
628 .remove
= mmc_sd_remove
,
629 .detect
= mmc_sd_detect
,
630 .suspend
= mmc_sd_suspend
,
631 .resume
= mmc_sd_resume
,
632 .power_restore
= mmc_sd_power_restore
,
635 static void mmc_sd_attach_bus_ops(struct mmc_host
*host
)
637 mmc_attach_bus(host
, &mmc_sd_ops
);
642 static const struct mmc_bus_ops mmc_sd_ops
= {
643 .remove
= mmc_sd_remove
,
644 .detect
= mmc_sd_detect
,
647 .power_restore
= mmc_sd_power_restore
,
650 static const struct mmc_bus_ops mmc_sd_ops_unsafe
= {
651 .remove
= mmc_sd_remove
,
652 .detect
= mmc_sd_detect
,
653 .suspend
= mmc_sd_suspend
,
654 .resume
= mmc_sd_resume
,
655 .power_restore
= mmc_sd_power_restore
,
658 static void mmc_sd_attach_bus_ops(struct mmc_host
*host
)
660 const struct mmc_bus_ops
*bus_ops
;
662 if (host
->caps
& MMC_CAP_NONREMOVABLE
)
663 bus_ops
= &mmc_sd_ops_unsafe
;
665 bus_ops
= &mmc_sd_ops
;
666 mmc_attach_bus(host
, bus_ops
);
672 * Starting point for SD card init.
674 int mmc_attach_sd(struct mmc_host
*host
, u32 ocr
)
679 WARN_ON(!host
->claimed
);
681 mmc_sd_attach_bus_ops(host
);
684 * We need to get OCR a different way for SPI.
686 if (mmc_host_is_spi(host
)) {
689 err
= mmc_spi_read_ocr(host
, 0, &ocr
);
695 * Sanity check the voltages that the card claims to
699 printk(KERN_WARNING
"%s: card claims to support voltages "
700 "below the defined range. These will be ignored.\n",
705 if (ocr
& MMC_VDD_165_195
) {
706 printk(KERN_WARNING
"%s: SD card claims to support the "
707 "incompletely defined 'low voltage range'. This "
708 "will be ignored.\n", mmc_hostname(host
));
709 ocr
&= ~MMC_VDD_165_195
;
712 host
->ocr
= mmc_select_voltage(host
, ocr
);
715 * Can we support the voltage(s) of the card(s)?
723 * Detect and init the card.
725 err
= mmc_sd_init_card(host
, host
->ocr
, NULL
);
729 mmc_release_host(host
);
731 err
= mmc_add_card(host
->card
);
738 mmc_remove_card(host
->card
);
740 mmc_claim_host(host
);
742 mmc_detach_bus(host
);
743 mmc_release_host(host
);
745 printk(KERN_ERR
"%s: error %d whilst initialising SD card\n",
746 mmc_hostname(host
), err
);