allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / mmc / core / sd.c
blob918477c490b0207bbc5a81733d0289b7f3b8ff76
1 /*
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>
18 #include <linux/mmc/sd.h>
20 #include "core.h"
21 #include "sysfs.h"
22 #include "mmc_ops.h"
23 #include "sd_ops.h"
25 #include "core.h"
27 static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000,
29 0, 0, 0, 0
32 static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80,
37 static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41 static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80,
46 #define UNSTUFF_BITS(resp,start,size) \
47 ({ \
48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \
52 u32 __res; \
54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
57 __res & __mask; \
61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
63 static void mmc_decode_cid(struct mmc_card *card)
65 u32 *resp = card->raw_cid;
67 memset(&card->cid, 0, sizeof(struct mmc_cid));
70 * SD doesn't currently have a version field so we will
71 * have to assume we can parse this.
73 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
74 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
81 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
82 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
83 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
84 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
86 card->cid.year += 2000; /* SD cards year offset */
90 * Given a 128-bit response, decode to our card CSD structure.
92 static int mmc_decode_csd(struct mmc_card *card)
94 struct mmc_csd *csd = &card->csd;
95 unsigned int e, m, csd_struct;
96 u32 *resp = card->raw_csd;
98 csd_struct = UNSTUFF_BITS(resp, 126, 2);
100 switch (csd_struct) {
101 case 0:
102 m = UNSTUFF_BITS(resp, 115, 4);
103 e = UNSTUFF_BITS(resp, 112, 3);
104 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
105 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
107 m = UNSTUFF_BITS(resp, 99, 4);
108 e = UNSTUFF_BITS(resp, 96, 3);
109 csd->max_dtr = tran_exp[e] * tran_mant[m];
110 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
112 e = UNSTUFF_BITS(resp, 47, 3);
113 m = UNSTUFF_BITS(resp, 62, 12);
114 csd->capacity = (1 + m) << (e + 2);
116 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
117 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
118 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
119 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
120 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
121 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
122 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
123 break;
124 case 1:
126 * This is a block-addressed SDHC card. Most
127 * interesting fields are unused and have fixed
128 * values. To avoid getting tripped by buggy cards,
129 * we assume those fixed values ourselves.
131 mmc_card_set_blockaddr(card);
133 csd->tacc_ns = 0; /* Unused */
134 csd->tacc_clks = 0; /* Unused */
136 m = UNSTUFF_BITS(resp, 99, 4);
137 e = UNSTUFF_BITS(resp, 96, 3);
138 csd->max_dtr = tran_exp[e] * tran_mant[m];
139 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
141 m = UNSTUFF_BITS(resp, 48, 22);
142 csd->capacity = (1 + m) << 10;
144 csd->read_blkbits = 9;
145 csd->read_partial = 0;
146 csd->write_misalign = 0;
147 csd->read_misalign = 0;
148 csd->r2w_factor = 4; /* Unused */
149 csd->write_blkbits = 9;
150 csd->write_partial = 0;
151 break;
152 default:
153 printk("%s: unrecognised CSD structure version %d\n",
154 mmc_hostname(card->host), csd_struct);
155 return -EINVAL;
158 return 0;
162 * Given a 64-bit response, decode to our card SCR structure.
164 static int mmc_decode_scr(struct mmc_card *card)
166 struct sd_scr *scr = &card->scr;
167 unsigned int scr_struct;
168 u32 resp[4];
170 BUG_ON(!mmc_card_sd(card));
172 resp[3] = card->raw_scr[1];
173 resp[2] = card->raw_scr[0];
175 scr_struct = UNSTUFF_BITS(resp, 60, 4);
176 if (scr_struct != 0) {
177 printk("%s: unrecognised SCR structure version %d\n",
178 mmc_hostname(card->host), scr_struct);
179 return -EINVAL;
182 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
183 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
185 return 0;
189 * Fetches and decodes switch information
191 static int mmc_read_switch(struct mmc_card *card)
193 int err;
194 u8 *status;
196 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
197 return MMC_ERR_NONE;
199 if (!(card->csd.cmdclass & CCC_SWITCH)) {
200 printk(KERN_WARNING "%s: card lacks mandatory switch "
201 "function, performance might suffer.\n",
202 mmc_hostname(card->host));
203 return MMC_ERR_NONE;
206 err = MMC_ERR_FAILED;
208 status = kmalloc(64, GFP_KERNEL);
209 if (!status) {
210 printk("%s: could not allocate a buffer for switch "
211 "capabilities.\n",
212 mmc_hostname(card->host));
213 return err;
216 err = mmc_sd_switch(card, 0, 0, 1, status);
217 if (err != MMC_ERR_NONE) {
218 printk(KERN_WARNING "%s: problem reading switch "
219 "capabilities, performance might suffer.\n",
220 mmc_hostname(card->host));
221 err = MMC_ERR_NONE;
222 goto out;
225 if (status[13] & 0x02)
226 card->sw_caps.hs_max_dtr = 50000000;
228 out:
229 kfree(status);
231 return err;
235 * Test if the card supports high-speed mode and, if so, switch to it.
237 static int mmc_switch_hs(struct mmc_card *card)
239 int err;
240 u8 *status;
242 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
243 return MMC_ERR_NONE;
245 if (!(card->csd.cmdclass & CCC_SWITCH))
246 return MMC_ERR_NONE;
248 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
249 return MMC_ERR_NONE;
251 if (card->sw_caps.hs_max_dtr == 0)
252 return MMC_ERR_NONE;
254 err = MMC_ERR_FAILED;
256 status = kmalloc(64, GFP_KERNEL);
257 if (!status) {
258 printk("%s: could not allocate a buffer for switch "
259 "capabilities.\n",
260 mmc_hostname(card->host));
261 return err;
264 err = mmc_sd_switch(card, 1, 0, 1, status);
265 if (err != MMC_ERR_NONE)
266 goto out;
268 if ((status[16] & 0xF) != 1) {
269 printk(KERN_WARNING "%s: Problem switching card "
270 "into high-speed mode!\n",
271 mmc_hostname(card->host));
272 } else {
273 mmc_card_set_highspeed(card);
274 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
277 out:
278 kfree(status);
280 return err;
284 * Handle the detection and initialisation of a card.
286 * In the case of a resume, "curcard" will contain the card
287 * we're trying to reinitialise.
289 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
290 struct mmc_card *oldcard)
292 struct mmc_card *card;
293 int err;
294 u32 cid[4];
295 unsigned int max_dtr;
297 BUG_ON(!host);
298 BUG_ON(!host->claimed);
301 * Since we're changing the OCR value, we seem to
302 * need to tell some cards to go back to the idle
303 * state. We wait 1ms to give cards time to
304 * respond.
306 mmc_go_idle(host);
309 * If SD_SEND_IF_COND indicates an SD 2.0
310 * compliant card and we should set bit 30
311 * of the ocr to indicate that we can handle
312 * block-addressed SDHC cards.
314 err = mmc_send_if_cond(host, ocr);
315 if (err == MMC_ERR_NONE)
316 ocr |= 1 << 30;
318 err = mmc_send_app_op_cond(host, ocr, NULL);
319 if (err != MMC_ERR_NONE)
320 goto err;
323 * Fetch CID from card.
325 err = mmc_all_send_cid(host, cid);
326 if (err != MMC_ERR_NONE)
327 goto err;
329 if (oldcard) {
330 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
331 goto err;
333 card = oldcard;
334 } else {
336 * Allocate card structure.
338 card = mmc_alloc_card(host);
339 if (IS_ERR(card))
340 goto err;
342 card->type = MMC_TYPE_SD;
343 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
347 * Set card RCA.
349 err = mmc_send_relative_addr(host, &card->rca);
350 if (err != MMC_ERR_NONE)
351 goto free_card;
353 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
355 if (!oldcard) {
357 * Fetch CSD from card.
359 err = mmc_send_csd(card, card->raw_csd);
360 if (err != MMC_ERR_NONE)
361 goto free_card;
363 err = mmc_decode_csd(card);
364 if (err < 0)
365 goto free_card;
367 mmc_decode_cid(card);
371 * Select card, as all following commands rely on that.
373 err = mmc_select_card(card);
374 if (err != MMC_ERR_NONE)
375 goto free_card;
377 if (!oldcard) {
379 * Fetch SCR from card.
381 err = mmc_app_send_scr(card, card->raw_scr);
382 if (err != MMC_ERR_NONE)
383 goto free_card;
385 err = mmc_decode_scr(card);
386 if (err < 0)
387 goto free_card;
390 * Fetch switch information from card.
392 err = mmc_read_switch(card);
393 if (err != MMC_ERR_NONE)
394 goto free_card;
398 * Attempt to change to high-speed (if supported)
400 err = mmc_switch_hs(card);
401 if (err != MMC_ERR_NONE)
402 goto free_card;
405 * Compute bus speed.
407 max_dtr = (unsigned int)-1;
409 if (mmc_card_highspeed(card)) {
410 if (max_dtr > card->sw_caps.hs_max_dtr)
411 max_dtr = card->sw_caps.hs_max_dtr;
412 } else if (max_dtr > card->csd.max_dtr) {
413 max_dtr = card->csd.max_dtr;
416 mmc_set_clock(host, max_dtr);
419 * Switch to wider bus (if supported).
421 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
422 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
423 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
424 if (err != MMC_ERR_NONE)
425 goto free_card;
427 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
431 * Check if read-only switch is active.
433 if (!oldcard) {
434 if (!host->ops->get_ro) {
435 printk(KERN_WARNING "%s: host does not "
436 "support reading read-only "
437 "switch. assuming write-enable.\n",
438 mmc_hostname(host));
439 } else {
440 if (host->ops->get_ro(host))
441 mmc_card_set_readonly(card);
445 if (!oldcard)
446 host->card = card;
448 return MMC_ERR_NONE;
450 free_card:
451 if (!oldcard)
452 mmc_remove_card(card);
453 err:
455 return MMC_ERR_FAILED;
459 * Host is being removed. Free up the current card.
461 static void mmc_sd_remove(struct mmc_host *host)
463 BUG_ON(!host);
464 BUG_ON(!host->card);
466 mmc_remove_card(host->card);
467 host->card = NULL;
471 * Card detection callback from host.
473 static void mmc_sd_detect(struct mmc_host *host)
475 int err;
477 BUG_ON(!host);
478 BUG_ON(!host->card);
480 mmc_claim_host(host);
483 * Just check if our card has been removed.
485 err = mmc_send_status(host->card, NULL);
487 mmc_release_host(host);
489 if (err != MMC_ERR_NONE) {
490 mmc_remove_card(host->card);
491 host->card = NULL;
493 mmc_claim_host(host);
494 mmc_detach_bus(host);
495 mmc_release_host(host);
499 #ifdef CONFIG_MMC_UNSAFE_RESUME
502 * Suspend callback from host.
504 static void mmc_sd_suspend(struct mmc_host *host)
506 BUG_ON(!host);
507 BUG_ON(!host->card);
509 mmc_claim_host(host);
510 mmc_deselect_cards(host);
511 host->card->state &= ~MMC_STATE_HIGHSPEED;
512 mmc_release_host(host);
516 * Resume callback from host.
518 * This function tries to determine if the same card is still present
519 * and, if so, restore all state to it.
521 static void mmc_sd_resume(struct mmc_host *host)
523 int err;
525 BUG_ON(!host);
526 BUG_ON(!host->card);
528 mmc_claim_host(host);
530 err = mmc_sd_init_card(host, host->ocr, host->card);
531 if (err != MMC_ERR_NONE) {
532 mmc_remove_card(host->card);
533 host->card = NULL;
535 mmc_detach_bus(host);
538 mmc_release_host(host);
541 #else
543 #define mmc_sd_suspend NULL
544 #define mmc_sd_resume NULL
546 #endif
548 static const struct mmc_bus_ops mmc_sd_ops = {
549 .remove = mmc_sd_remove,
550 .detect = mmc_sd_detect,
551 .suspend = mmc_sd_suspend,
552 .resume = mmc_sd_resume,
556 * Starting point for SD card init.
558 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
560 int err;
562 BUG_ON(!host);
563 BUG_ON(!host->claimed);
565 mmc_attach_bus(host, &mmc_sd_ops);
568 * Sanity check the voltages that the card claims to
569 * support.
571 if (ocr & 0x7F) {
572 printk(KERN_WARNING "%s: card claims to support voltages "
573 "below the defined range. These will be ignored.\n",
574 mmc_hostname(host));
575 ocr &= ~0x7F;
578 if (ocr & MMC_VDD_165_195) {
579 printk(KERN_WARNING "%s: SD card claims to support the "
580 "incompletely defined 'low voltage range'. This "
581 "will be ignored.\n", mmc_hostname(host));
582 ocr &= ~MMC_VDD_165_195;
585 host->ocr = mmc_select_voltage(host, ocr);
588 * Can we support the voltage(s) of the card(s)?
590 if (!host->ocr)
591 goto err;
594 * Detect and init the card.
596 err = mmc_sd_init_card(host, host->ocr, NULL);
597 if (err != MMC_ERR_NONE)
598 goto err;
600 mmc_release_host(host);
602 err = mmc_register_card(host->card);
603 if (err)
604 goto reclaim_host;
606 return 0;
608 reclaim_host:
609 mmc_claim_host(host);
610 mmc_remove_card(host->card);
611 host->card = NULL;
612 err:
613 mmc_detach_bus(host);
614 mmc_release_host(host);
616 return 0;