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 #define UNSTUFF_BITS(resp,start,size) \
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; \
52 __res = resp[__off] >> __shft; \
53 if (__size + __shft > 32) \
54 __res |= resp[__off-1] << ((32 - __shft) % 32); \
59 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 static void mmc_decode_cid(struct mmc_card
*card
)
63 u32
*resp
= card
->raw_cid
;
65 memset(&card
->cid
, 0, sizeof(struct mmc_cid
));
68 * SD doesn't currently have a version field so we will
69 * have to assume we can parse this.
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);
84 card
->cid
.year
+= 2000; /* SD cards year offset */
88 * Given a 128-bit response, decode to our card CSD structure.
90 static int mmc_decode_csd(struct mmc_card
*card
)
92 struct mmc_csd
*csd
= &card
->csd
;
93 unsigned int e
, m
, csd_struct
;
94 u32
*resp
= card
->raw_csd
;
96 csd_struct
= UNSTUFF_BITS(resp
, 126, 2);
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;
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);
110 e
= UNSTUFF_BITS(resp
, 47, 3);
111 m
= UNSTUFF_BITS(resp
, 62, 12);
112 csd
->capacity
= (1 + m
) << (e
+ 2);
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);
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.
129 mmc_card_set_blockaddr(card
);
131 csd
->tacc_ns
= 0; /* Unused */
132 csd
->tacc_clks
= 0; /* Unused */
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);
139 m
= UNSTUFF_BITS(resp
, 48, 22);
140 csd
->capacity
= (1 + m
) << 10;
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;
151 printk(KERN_ERR
"%s: unrecognised CSD structure version %d\n",
152 mmc_hostname(card
->host
), csd_struct
);
160 * Given a 64-bit response, decode to our card SCR structure.
162 static int mmc_decode_scr(struct mmc_card
*card
)
164 struct sd_scr
*scr
= &card
->scr
;
165 unsigned int scr_struct
;
168 resp
[3] = card
->raw_scr
[1];
169 resp
[2] = card
->raw_scr
[0];
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
);
178 scr
->sda_vsn
= UNSTUFF_BITS(resp
, 56, 4);
179 scr
->bus_widths
= UNSTUFF_BITS(resp
, 48, 4);
185 * Fetches and decodes switch information
187 static int mmc_read_switch(struct mmc_card
*card
)
192 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
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
));
204 status
= kmalloc(64, GFP_KERNEL
);
206 printk(KERN_ERR
"%s: could not allocate a buffer for "
207 "switch capabilities.\n", mmc_hostname(card
->host
));
211 err
= mmc_sd_switch(card
, 0, 0, 1, status
);
214 * We all hosts that cannot perform the command
215 * to fail more gracefully
220 printk(KERN_WARNING
"%s: problem reading switch "
221 "capabilities, performance might suffer.\n",
222 mmc_hostname(card
->host
));
228 if (status
[13] & 0x02)
229 card
->sw_caps
.hs_max_dtr
= 50000000;
238 * Test if the card supports high-speed mode and, if so, switch to it.
240 static int mmc_switch_hs(struct mmc_card
*card
)
245 if (card
->scr
.sda_vsn
< SCR_SPEC_VER_1
)
248 if (!(card
->csd
.cmdclass
& CCC_SWITCH
))
251 if (!(card
->host
->caps
& MMC_CAP_SD_HIGHSPEED
))
254 if (card
->sw_caps
.hs_max_dtr
== 0)
259 status
= kmalloc(64, GFP_KERNEL
);
261 printk(KERN_ERR
"%s: could not allocate a buffer for "
262 "switch capabilities.\n", mmc_hostname(card
->host
));
266 err
= mmc_sd_switch(card
, 1, 0, 1, status
);
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
));
275 mmc_card_set_highspeed(card
);
276 mmc_set_timing(card
->host
, MMC_TIMING_SD_HS
);
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
);
299 static struct attribute
*sd_std_attrs
[] = {
304 &dev_attr_fwrev
.attr
,
305 &dev_attr_hwrev
.attr
,
306 &dev_attr_manfid
.attr
,
308 &dev_attr_oemid
.attr
,
309 &dev_attr_serial
.attr
,
313 static struct attribute_group sd_std_attr_group
= {
314 .attrs
= sd_std_attrs
,
317 static struct attribute_group
*sd_attr_groups
[] = {
322 static struct device_type sd_type
= {
323 .groups
= sd_attr_groups
,
327 * Handle the detection and initialisation of a card.
329 * In the case of a resume, "curcard" will contain the card
330 * we're trying to reinitialise.
332 static int mmc_sd_init_card(struct mmc_host
*host
, u32 ocr
,
333 struct mmc_card
*oldcard
)
335 struct mmc_card
*card
;
338 unsigned int max_dtr
;
341 WARN_ON(!host
->claimed
);
344 * Since we're changing the OCR value, we seem to
345 * need to tell some cards to go back to the idle
346 * state. We wait 1ms to give cards time to
352 * If SD_SEND_IF_COND indicates an SD 2.0
353 * compliant card and we should set bit 30
354 * of the ocr to indicate that we can handle
355 * block-addressed SDHC cards.
357 err
= mmc_send_if_cond(host
, ocr
);
361 err
= mmc_send_app_op_cond(host
, ocr
, NULL
);
366 * For SPI, enable CRC as appropriate.
368 if (mmc_host_is_spi(host
)) {
369 err
= mmc_spi_set_crc(host
, use_spi_crc
);
375 * Fetch CID from card.
377 if (mmc_host_is_spi(host
))
378 err
= mmc_send_cid(host
, cid
);
380 err
= mmc_all_send_cid(host
, cid
);
385 if (memcmp(cid
, oldcard
->raw_cid
, sizeof(cid
)) != 0) {
393 * Allocate card structure.
395 card
= mmc_alloc_card(host
, &sd_type
);
401 card
->type
= MMC_TYPE_SD
;
402 memcpy(card
->raw_cid
, cid
, sizeof(card
->raw_cid
));
406 * For native busses: get card RCA and quit open drain mode.
408 if (!mmc_host_is_spi(host
)) {
409 err
= mmc_send_relative_addr(host
, &card
->rca
);
413 mmc_set_bus_mode(host
, MMC_BUSMODE_PUSHPULL
);
418 * Fetch CSD from card.
420 err
= mmc_send_csd(card
, card
->raw_csd
);
424 err
= mmc_decode_csd(card
);
428 mmc_decode_cid(card
);
432 * Select card, as all following commands rely on that.
434 if (!mmc_host_is_spi(host
)) {
435 err
= mmc_select_card(card
);
442 * Fetch SCR from card.
444 err
= mmc_app_send_scr(card
, card
->raw_scr
);
448 err
= mmc_decode_scr(card
);
453 * Fetch switch information from card.
455 err
= mmc_read_switch(card
);
461 * Attempt to change to high-speed (if supported)
463 err
= mmc_switch_hs(card
);
470 max_dtr
= (unsigned int)-1;
472 if (mmc_card_highspeed(card
)) {
473 if (max_dtr
> card
->sw_caps
.hs_max_dtr
)
474 max_dtr
= card
->sw_caps
.hs_max_dtr
;
475 } else if (max_dtr
> card
->csd
.max_dtr
) {
476 max_dtr
= card
->csd
.max_dtr
;
479 mmc_set_clock(host
, max_dtr
);
482 * Switch to wider bus (if supported).
484 if ((host
->caps
& MMC_CAP_4_BIT_DATA
) &&
485 (card
->scr
.bus_widths
& SD_SCR_BUS_WIDTH_4
)) {
486 err
= mmc_app_set_bus_width(card
, MMC_BUS_WIDTH_4
);
490 mmc_set_bus_width(host
, MMC_BUS_WIDTH_4
);
494 * Check if read-only switch is active.
497 if (!host
->ops
->get_ro
) {
498 printk(KERN_WARNING
"%s: host does not "
499 "support reading read-only "
500 "switch. assuming write-enable.\n",
503 if (host
->ops
->get_ro(host
))
504 mmc_card_set_readonly(card
);
515 mmc_remove_card(card
);
522 * Host is being removed. Free up the current card.
524 static void mmc_sd_remove(struct mmc_host
*host
)
529 mmc_remove_card(host
->card
);
534 * Card detection callback from host.
536 static void mmc_sd_detect(struct mmc_host
*host
)
543 mmc_claim_host(host
);
546 * Just check if our card has been removed.
548 err
= mmc_send_status(host
->card
, NULL
);
550 mmc_release_host(host
);
555 mmc_claim_host(host
);
556 mmc_detach_bus(host
);
557 mmc_release_host(host
);
561 #ifdef CONFIG_MMC_UNSAFE_RESUME
564 * Suspend callback from host.
566 static void mmc_sd_suspend(struct mmc_host
*host
)
571 mmc_claim_host(host
);
572 if (!mmc_host_is_spi(host
))
573 mmc_deselect_cards(host
);
574 host
->card
->state
&= ~MMC_STATE_HIGHSPEED
;
575 mmc_release_host(host
);
579 * Resume callback from host.
581 * This function tries to determine if the same card is still present
582 * and, if so, restore all state to it.
584 static void mmc_sd_resume(struct mmc_host
*host
)
591 mmc_claim_host(host
);
592 err
= mmc_sd_init_card(host
, host
->ocr
, host
->card
);
593 mmc_release_host(host
);
598 mmc_claim_host(host
);
599 mmc_detach_bus(host
);
600 mmc_release_host(host
);
607 #define mmc_sd_suspend NULL
608 #define mmc_sd_resume NULL
612 static const struct mmc_bus_ops mmc_sd_ops
= {
613 .remove
= mmc_sd_remove
,
614 .detect
= mmc_sd_detect
,
615 .suspend
= mmc_sd_suspend
,
616 .resume
= mmc_sd_resume
,
620 * Starting point for SD card init.
622 int mmc_attach_sd(struct mmc_host
*host
, u32 ocr
)
627 WARN_ON(!host
->claimed
);
629 mmc_attach_bus(host
, &mmc_sd_ops
);
632 * We need to get OCR a different way for SPI.
634 if (mmc_host_is_spi(host
)) {
637 err
= mmc_spi_read_ocr(host
, 0, &ocr
);
643 * Sanity check the voltages that the card claims to
647 printk(KERN_WARNING
"%s: card claims to support voltages "
648 "below the defined range. These will be ignored.\n",
653 if (ocr
& MMC_VDD_165_195
) {
654 printk(KERN_WARNING
"%s: SD card claims to support the "
655 "incompletely defined 'low voltage range'. This "
656 "will be ignored.\n", mmc_hostname(host
));
657 ocr
&= ~MMC_VDD_165_195
;
660 host
->ocr
= mmc_select_voltage(host
, ocr
);
663 * Can we support the voltage(s) of the card(s)?
671 * Detect and init the card.
673 err
= mmc_sd_init_card(host
, host
->ocr
, NULL
);
677 mmc_release_host(host
);
679 err
= mmc_add_card(host
->card
);
686 mmc_remove_card(host
->card
);
688 mmc_claim_host(host
);
690 mmc_detach_bus(host
);
691 mmc_release_host(host
);
693 printk(KERN_ERR
"%s: error %d whilst initialising SD card\n",
694 mmc_hostname(host
), err
);