MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc / core / sd.c
blob3f40460e12a0df62cb5338fe3c61f0824dd21787
1 /*
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>
20 #include "core.h"
21 #include "sysfs.h"
22 #include "bus.h"
23 #include "mmc_ops.h"
24 #include "sd_ops.h"
26 static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
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 #if 0 // mask by Victor Yu. 12-03-2008
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; \
59 #else
60 #define UNSTUFF_BITS(resp,start,size) \
61 ({ \
62 const int __size = size; \
63 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
64 const int __off = ((start) / 32); \
65 const int __shft = (start) & 31; \
66 u32 __res; \
68 __res = resp[__off] >> __shft; \
69 if (__size + __shft > 32) \
70 __res |= resp[__off+1] << ((32 - __shft) % 32); \
71 __res & __mask; \
73 #endif
76 * Given the decoded CSD structure, decode the raw CID to our CID structure.
78 static void mmc_decode_cid(struct mmc_card *card)
80 u32 *resp = card->raw_cid;
82 memset(&card->cid, 0, sizeof(struct mmc_cid));
85 * SD doesn't currently have a version field so we will
86 * have to assume we can parse this.
88 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
89 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
90 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
91 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
92 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
93 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
94 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
95 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
96 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
97 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
98 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
99 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
101 card->cid.year += 2000; /* SD cards year offset */
105 * Given a 128-bit response, decode to our card CSD structure.
107 static int mmc_decode_csd(struct mmc_card *card)
109 struct mmc_csd *csd = &card->csd;
110 unsigned int e, m, csd_struct;
111 u32 *resp = card->raw_csd;
113 csd_struct = UNSTUFF_BITS(resp, 126, 2);
115 switch (csd_struct) {
116 case 0:
117 m = UNSTUFF_BITS(resp, 115, 4);
118 e = UNSTUFF_BITS(resp, 112, 3);
119 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
120 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
122 m = UNSTUFF_BITS(resp, 99, 4);
123 e = UNSTUFF_BITS(resp, 96, 3);
124 csd->max_dtr = tran_exp[e] * tran_mant[m];
125 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
127 e = UNSTUFF_BITS(resp, 47, 3);
128 m = UNSTUFF_BITS(resp, 62, 12);
129 csd->capacity = (1 + m) << (e + 2);
131 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
132 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
133 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
134 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
135 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
136 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
137 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
138 break;
139 case 1:
141 * This is a block-addressed SDHC card. Most
142 * interesting fields are unused and have fixed
143 * values. To avoid getting tripped by buggy cards,
144 * we assume those fixed values ourselves.
146 mmc_card_set_blockaddr(card);
148 csd->tacc_ns = 0; /* Unused */
149 csd->tacc_clks = 0; /* Unused */
151 m = UNSTUFF_BITS(resp, 99, 4);
152 e = UNSTUFF_BITS(resp, 96, 3);
153 csd->max_dtr = tran_exp[e] * tran_mant[m];
154 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
156 m = UNSTUFF_BITS(resp, 48, 22);
157 csd->capacity = (1 + m) << 10;
159 csd->read_blkbits = 9;
160 csd->read_partial = 0;
161 csd->write_misalign = 0;
162 csd->read_misalign = 0;
163 csd->r2w_factor = 4; /* Unused */
164 csd->write_blkbits = 9;
165 csd->write_partial = 0;
166 break;
167 default:
168 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
169 mmc_hostname(card->host), csd_struct);
170 return -EINVAL;
173 return 0;
177 * Given a 64-bit response, decode to our card SCR structure.
179 static int mmc_decode_scr(struct mmc_card *card)
181 struct sd_scr *scr = &card->scr;
182 unsigned int scr_struct;
183 u32 resp[4];
185 BUG_ON(!mmc_card_sd(card));
187 resp[3] = card->raw_scr[1];
188 resp[2] = card->raw_scr[0];
189 #if 1 // add by Victor Yu. 12-03-2008
190 resp[0] = 0;
191 resp[1] = 0;
192 #endif
194 scr_struct = UNSTUFF_BITS(resp, 60, 4);
195 if (scr_struct != 0) {
196 printk(KERN_ERR "%s: unrecognised SCR structure version %d\n",
197 mmc_hostname(card->host), scr_struct);
198 return -EINVAL;
201 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
202 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
204 return 0;
208 * Fetches and decodes switch information
210 static int mmc_read_switch(struct mmc_card *card)
212 int err;
213 u8 *status;
215 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
216 return MMC_ERR_NONE;
218 if (!(card->csd.cmdclass & CCC_SWITCH)) {
219 printk(KERN_WARNING "%s: card lacks mandatory switch "
220 "function, performance might suffer.\n",
221 mmc_hostname(card->host));
222 return MMC_ERR_NONE;
225 err = MMC_ERR_FAILED;
227 status = kmalloc(64, GFP_KERNEL);
228 if (!status) {
229 printk(KERN_ERR "%s: could not allocate a buffer for "
230 "switch capabilities.\n", mmc_hostname(card->host));
231 return err;
234 err = mmc_sd_switch(card, 0, 0, 1, status);
235 if (err != MMC_ERR_NONE) {
236 printk(KERN_WARNING "%s: problem reading switch "
237 "capabilities, performance might suffer.\n",
238 mmc_hostname(card->host));
239 err = MMC_ERR_NONE;
240 goto out;
243 if (status[13] & 0x02)
244 card->sw_caps.hs_max_dtr = 50000000;
246 out:
247 kfree(status);
249 return err;
253 * Test if the card supports high-speed mode and, if so, switch to it.
255 static int mmc_switch_hs(struct mmc_card *card)
257 int err;
258 u8 *status;
260 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
261 return MMC_ERR_NONE;
263 if (!(card->csd.cmdclass & CCC_SWITCH))
264 return MMC_ERR_NONE;
266 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
267 return MMC_ERR_NONE;
269 if (card->sw_caps.hs_max_dtr == 0)
270 return MMC_ERR_NONE;
272 err = MMC_ERR_FAILED;
274 status = kmalloc(64, GFP_KERNEL);
275 if (!status) {
276 printk(KERN_ERR "%s: could not allocate a buffer for "
277 "switch capabilities.\n", mmc_hostname(card->host));
278 return err;
281 err = mmc_sd_switch(card, 1, 0, 1, status);
282 if (err != MMC_ERR_NONE)
283 goto out;
285 if ((status[16] & 0xF) != 1) {
286 printk(KERN_WARNING "%s: Problem switching card "
287 "into high-speed mode!\n",
288 mmc_hostname(card->host));
289 } else {
290 mmc_card_set_highspeed(card);
291 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
294 out:
295 kfree(status);
297 return err;
301 * Handle the detection and initialisation of a card.
303 * In the case of a resume, "curcard" will contain the card
304 * we're trying to reinitialise.
306 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
307 struct mmc_card *oldcard)
309 struct mmc_card *card;
310 int err;
311 u32 cid[4];
312 unsigned int max_dtr;
314 BUG_ON(!host);
315 BUG_ON(!host->claimed);
318 * Since we're changing the OCR value, we seem to
319 * need to tell some cards to go back to the idle
320 * state. We wait 1ms to give cards time to
321 * respond.
323 mmc_go_idle(host);
326 * If SD_SEND_IF_COND indicates an SD 2.0
327 * compliant card and we should set bit 30
328 * of the ocr to indicate that we can handle
329 * block-addressed SDHC cards.
331 err = mmc_send_if_cond(host, ocr);
332 if (err == MMC_ERR_NONE)
333 ocr |= 1 << 30;
335 err = mmc_send_app_op_cond(host, ocr, NULL);
336 if (err != MMC_ERR_NONE){
337 printk(KERN_WARNING "mmc_send_app_op_cond error");
338 goto err;
342 * Fetch CID from card.
344 err = mmc_all_send_cid(host, cid);
345 if (err != MMC_ERR_NONE){
346 printk(KERN_WARNING "mmc_all_send_cid error");
347 goto err;
350 if (oldcard) {
351 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0){
352 printk(KERN_WARNING "mmc oldcard memcmp error");
353 goto err;
356 card = oldcard;
357 } else {
359 * Allocate card structure.
361 card = mmc_alloc_card(host);
362 if (IS_ERR(card)){
363 printk(KERN_WARNING "mmc_alloc_card error");
364 goto err;
367 card->type = MMC_TYPE_SD;
368 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
372 * Set card RCA.
374 err = mmc_send_relative_addr(host, &card->rca);
375 if (err != MMC_ERR_NONE)
376 goto free_card;
378 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
380 if (!oldcard) {
382 * Fetch CSD from card.
384 err = mmc_send_csd(card, card->raw_csd);
385 if (err != MMC_ERR_NONE)
386 goto free_card;
388 err = mmc_decode_csd(card);
389 if (err < 0)
390 goto free_card;
392 mmc_decode_cid(card);
396 * Select card, as all following commands rely on that.
398 err = mmc_select_card(card);
399 if (err != MMC_ERR_NONE)
400 goto free_card;
402 if (!oldcard) {
404 * Fetch SCR from card.
406 err = mmc_app_send_scr(card, card->raw_scr);
407 if (err != MMC_ERR_NONE)
408 goto free_card;
410 err = mmc_decode_scr(card);
411 if (err < 0)
412 goto free_card;
415 * Fetch switch information from card.
417 err = mmc_read_switch(card);
418 if (err != MMC_ERR_NONE)
419 goto free_card;
423 * Attempt to change to high-speed (if supported)
425 err = mmc_switch_hs(card);
426 if (err != MMC_ERR_NONE)
427 goto free_card;
430 * Compute bus speed.
432 max_dtr = (unsigned int)-1;
434 if (mmc_card_highspeed(card)) {
435 if (max_dtr > card->sw_caps.hs_max_dtr)
436 max_dtr = card->sw_caps.hs_max_dtr;
437 } else if (max_dtr > card->csd.max_dtr) {
438 max_dtr = card->csd.max_dtr;
441 mmc_set_clock(host, max_dtr);
444 * Switch to wider bus (if supported).
446 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
447 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
448 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
449 if (err != MMC_ERR_NONE)
450 goto free_card;
452 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
456 * Check if read-only switch is active.
458 if (!oldcard) {
459 if (!host->ops->get_ro) {
460 printk(KERN_WARNING "%s: host does not "
461 "support reading read-only "
462 "switch. assuming write-enable.\n",
463 mmc_hostname(host));
464 } else {
465 if (host->ops->get_ro(host))
466 mmc_card_set_readonly(card);
470 if (!oldcard)
471 host->card = card;
473 return MMC_ERR_NONE;
475 free_card:
476 if (!oldcard)
477 mmc_remove_card(card);
478 err:
480 return MMC_ERR_FAILED;
484 * Host is being removed. Free up the current card.
486 static void mmc_sd_remove(struct mmc_host *host)
488 BUG_ON(!host);
489 BUG_ON(!host->card);
491 mmc_remove_card(host->card);
492 host->card = NULL;
496 * Card detection callback from host.
498 static void mmc_sd_detect(struct mmc_host *host)
500 int err;
502 BUG_ON(!host);
503 BUG_ON(!host->card);
505 mmc_claim_host(host);
508 * Just check if our card has been removed.
510 err = mmc_send_status(host->card, NULL);
512 mmc_release_host(host);
514 if (err != MMC_ERR_NONE) {
515 mmc_sd_remove(host);
517 mmc_claim_host(host);
518 mmc_detach_bus(host);
519 mmc_release_host(host);
523 MMC_ATTR_FN(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
524 card->raw_cid[2], card->raw_cid[3]);
525 MMC_ATTR_FN(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
526 card->raw_csd[2], card->raw_csd[3]);
527 MMC_ATTR_FN(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
528 MMC_ATTR_FN(date, "%02d/%04d\n", card->cid.month, card->cid.year);
529 MMC_ATTR_FN(fwrev, "0x%x\n", card->cid.fwrev);
530 MMC_ATTR_FN(hwrev, "0x%x\n", card->cid.hwrev);
531 MMC_ATTR_FN(manfid, "0x%06x\n", card->cid.manfid);
532 MMC_ATTR_FN(name, "%s\n", card->cid.prod_name);
533 MMC_ATTR_FN(oemid, "0x%04x\n", card->cid.oemid);
534 MMC_ATTR_FN(serial, "0x%08x\n", card->cid.serial);
536 static struct device_attribute mmc_sd_dev_attrs[] = {
537 MMC_ATTR_RO(cid),
538 MMC_ATTR_RO(csd),
539 MMC_ATTR_RO(scr),
540 MMC_ATTR_RO(date),
541 MMC_ATTR_RO(fwrev),
542 MMC_ATTR_RO(hwrev),
543 MMC_ATTR_RO(manfid),
544 MMC_ATTR_RO(name),
545 MMC_ATTR_RO(oemid),
546 MMC_ATTR_RO(serial),
547 __ATTR_NULL,
551 * Adds sysfs entries as relevant.
553 static int mmc_sd_sysfs_add(struct mmc_host *host, struct mmc_card *card)
555 int ret;
557 ret = mmc_add_attrs(card, mmc_sd_dev_attrs);
558 if (ret < 0)
559 return ret;
561 return 0;
565 * Removes the sysfs entries added by mmc_sysfs_add().
567 static void mmc_sd_sysfs_remove(struct mmc_host *host, struct mmc_card *card)
569 mmc_remove_attrs(card, mmc_sd_dev_attrs);
572 #ifdef CONFIG_MMC_UNSAFE_RESUME
575 * Suspend callback from host.
577 static void mmc_sd_suspend(struct mmc_host *host)
579 BUG_ON(!host);
580 BUG_ON(!host->card);
582 mmc_claim_host(host);
583 mmc_deselect_cards(host);
584 host->card->state &= ~MMC_STATE_HIGHSPEED;
585 mmc_release_host(host);
589 * Resume callback from host.
591 * This function tries to determine if the same card is still present
592 * and, if so, restore all state to it.
594 static void mmc_sd_resume(struct mmc_host *host)
596 int err;
598 BUG_ON(!host);
599 BUG_ON(!host->card);
601 mmc_claim_host(host);
602 err = mmc_sd_init_card(host, host->ocr, host->card);
603 mmc_release_host(host);
605 if (err != MMC_ERR_NONE) {
606 mmc_sd_remove(host);
608 mmc_claim_host(host);
609 mmc_detach_bus(host);
610 mmc_release_host(host);
615 #else
617 #define mmc_sd_suspend NULL
618 #define mmc_sd_resume NULL
620 #endif
622 static const struct mmc_bus_ops mmc_sd_ops = {
623 .remove = mmc_sd_remove,
624 .detect = mmc_sd_detect,
625 .sysfs_add = mmc_sd_sysfs_add,
626 .sysfs_remove = mmc_sd_sysfs_remove,
627 .suspend = mmc_sd_suspend,
628 .resume = mmc_sd_resume,
632 * Starting point for SD card init.
634 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
636 int err;
638 BUG_ON(!host);
639 BUG_ON(!host->claimed);
641 mmc_attach_bus(host, &mmc_sd_ops);
644 * Sanity check the voltages that the card claims to
645 * support.
647 if (ocr & 0x7F) {
648 printk(KERN_WARNING "%s: card claims to support voltages "
649 "below the defined range. These will be ignored.\n",
650 mmc_hostname(host));
651 ocr &= ~0x7F;
654 if (ocr & MMC_VDD_165_195) {
655 printk(KERN_WARNING "%s: SD card claims to support the "
656 "incompletely defined 'low voltage range'. This "
657 "will be ignored.\n", mmc_hostname(host));
658 ocr &= ~MMC_VDD_165_195;
661 host->ocr = mmc_select_voltage(host, ocr);
664 * Can we support the voltage(s) of the card(s)?
666 if (!host->ocr) {
667 err = -EINVAL;
668 printk(KERN_WARNING "mmc_valtage_support_error");
669 goto err;
673 * Detect and init the card.
675 err = mmc_sd_init_card(host, host->ocr, NULL);
676 if (err != MMC_ERR_NONE){
677 printk(KERN_WARNING "mmc_sd_init_card error");
678 goto err;
681 mmc_release_host(host);
683 err = mmc_add_card(host->card);
684 if (err)
685 goto remove_card;
687 return 0;
689 remove_card:
690 mmc_remove_card(host->card);
691 host->card = NULL;
692 mmc_claim_host(host);
693 err:
694 mmc_detach_bus(host);
695 mmc_release_host(host);
697 printk(KERN_ERR "%s: error %d whilst initialising SD card\n",
698 mmc_hostname(host), err);
700 return 0;