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>
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 static 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 void 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);
125 * This is a block-addressed SDHC card. Most
126 * interesting fields are unused and have fixed
127 * values. To avoid getting tripped by buggy cards,
128 * we assume those fixed values ourselves.
130 mmc_card_set_blockaddr(card);
132 csd->tacc_ns = 0; /* Unused */
133 csd->tacc_clks = 0; /* Unused */
135 m = UNSTUFF_BITS(resp, 99, 4);
136 e = UNSTUFF_BITS(resp, 96, 3);
137 csd->max_dtr = tran_exp[e] * tran_mant[m];
138 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
140 m = UNSTUFF_BITS(resp, 48, 22);
141 csd->capacity = (1 + m) << 10;
143 csd->read_blkbits = 9;
144 csd->read_partial = 0;
145 csd->write_misalign = 0;
146 csd->read_misalign = 0;
147 csd->r2w_factor = 4; /* Unused */
148 csd->write_blkbits = 9;
149 csd->write_partial = 0;
152 printk("%s: unrecognised CSD structure version %d\n",
153 mmc_hostname(card->host), csd_struct);
154 mmc_card_set_bad(card);
160 * Given a 64-bit response, decode to our card SCR structure.
162 static void mmc_decode_scr(struct mmc_card *card)
164 struct sd_scr *scr = &card->scr;
165 unsigned int scr_struct;
168 BUG_ON(!mmc_card_sd(card));
170 resp[3] = card->raw_scr[1];
171 resp[2] = card->raw_scr[0];
173 scr_struct = UNSTUFF_BITS(resp, 60, 4);
174 if (scr_struct != 0) {
175 printk("%s: unrecognised SCR structure version %d\n",
176 mmc_hostname(card->host), scr_struct);
177 mmc_card_set_bad(card);
181 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
182 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
186 * Fetches and decodes switch information
188 static int mmc_read_switch(struct mmc_card *card)
193 err = MMC_ERR_FAILED;
195 status = kmalloc(64, GFP_KERNEL);
197 printk("%s: could not allocate a buffer for switch "
199 mmc_hostname(card->host));
203 err = mmc_sd_switch(card, 0, 0, 1, status);
204 if (err != MMC_ERR_NONE) {
206 * Card not supporting high-speed will ignore the
213 if (status[13] & 0x02)
214 card->sw_caps.hs_max_dtr = 50000000;
223 * Test if the card supports high-speed mode and, if so, switch to it.
225 static int mmc_switch_hs(struct mmc_card *card)
230 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
233 if (card->sw_caps.hs_max_dtr == 0)
236 err = MMC_ERR_FAILED;
238 status = kmalloc(64, GFP_KERNEL);
240 printk("%s: could not allocate a buffer for switch "
242 mmc_hostname(card->host));
246 err = mmc_sd_switch(card, 1, 0, 1, status);
247 if (err != MMC_ERR_NONE)
250 if ((status[16] & 0xF) != 1) {
251 printk(KERN_WARNING "%s: Problem switching card "
252 "into high-speed mode!\n",
253 mmc_hostname(card->host));
255 mmc_card_set_highspeed(card);
256 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
266 * Host is being removed. Free up the current card.
268 static void mmc_sd_remove(struct mmc_host *host)
273 mmc_remove_card(host->card);
278 * Card detection callback from host.
280 static void mmc_sd_detect(struct mmc_host *host)
287 mmc_claim_host(host);
290 * Just check if our card has been removed.
292 err = mmc_send_status(host->card, NULL);
294 mmc_release_host(host);
296 if (err != MMC_ERR_NONE) {
297 mmc_remove_card(host->card);
300 mmc_claim_host(host);
301 mmc_detach_bus(host);
302 mmc_release_host(host);
306 static const struct mmc_bus_ops mmc_sd_ops = {
307 .remove = mmc_sd_remove,
308 .detect = mmc_sd_detect,
312 * Starting point for SD card init.
314 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
316 struct mmc_card *card;
319 unsigned int max_dtr;
322 BUG_ON(!host->claimed);
324 mmc_attach_bus(host, &mmc_sd_ops);
327 * Sanity check the voltages that the card claims to
331 printk(KERN_WARNING "%s: card claims to support voltages "
332 "below the defined range. These will be ignored.\n",
337 if (ocr & MMC_VDD_165_195) {
338 printk(KERN_WARNING "%s: SD card claims to support the "
339 "incompletely defined 'low voltage range'. This "
340 "will be ignored.\n", mmc_hostname(host));
341 ocr &= ~MMC_VDD_165_195;
344 host->ocr = mmc_select_voltage(host, ocr);
347 * Can we support the voltage(s) of the card(s)?
353 * Since we're changing the OCR value, we seem to
354 * need to tell some cards to go back to the idle
355 * state. We wait 1ms to give cards time to
361 * If SD_SEND_IF_COND indicates an SD 2.0
362 * compliant card and we should set bit 30
363 * of the ocr to indicate that we can handle
364 * block-addressed SDHC cards.
366 err = mmc_send_if_cond(host, host->ocr);
367 if (err == MMC_ERR_NONE)
368 ocr = host->ocr | (1 << 30);
370 mmc_send_app_op_cond(host, ocr, NULL);
373 * Fetch CID from card.
375 err = mmc_all_send_cid(host, cid);
376 if (err != MMC_ERR_NONE)
380 * Allocate card structure.
382 card = mmc_alloc_card(host);
386 card->type = MMC_TYPE_SD;
387 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
392 err = mmc_send_relative_addr(host, &card->rca);
393 if (err != MMC_ERR_NONE)
396 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
399 * Fetch CSD from card.
401 err = mmc_send_csd(card, card->raw_csd);
402 if (err != MMC_ERR_NONE)
405 mmc_decode_csd(card);
406 mmc_decode_cid(card);
409 * Fetch SCR from card.
411 err = mmc_select_card(card);
412 if (err != MMC_ERR_NONE)
415 err = mmc_app_send_scr(card, card->raw_scr);
416 if (err != MMC_ERR_NONE)
419 mmc_decode_scr(card);
422 * Fetch switch information from card.
424 err = mmc_read_switch(card);
425 if (err != MMC_ERR_NONE)
429 * Attempt to change to high-speed (if supported)
431 err = mmc_switch_hs(card);
432 if (err != MMC_ERR_NONE)
438 max_dtr = (unsigned int)-1;
440 if (mmc_card_highspeed(card)) {
441 if (max_dtr > card->sw_caps.hs_max_dtr)
442 max_dtr = card->sw_caps.hs_max_dtr;
443 } else if (max_dtr > card->csd.max_dtr) {
444 max_dtr = card->csd.max_dtr;
447 mmc_set_clock(host, max_dtr);
450 * Switch to wider bus (if supported).
452 if ((host->caps && MMC_CAP_4_BIT_DATA) &&
453 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
454 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
455 if (err != MMC_ERR_NONE)
458 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
463 mmc_release_host(host);
465 err = mmc_register_card(card);
472 mmc_claim_host(host);
474 mmc_remove_card(card);
477 mmc_detach_bus(host);
478 mmc_release_host(host);