code style scripts/checkpatch.pl (linux-3.9-rc1) formatting
[linux-2.6.32.60-moxart.git] / drivers / mmc / core / sd.c
blob824ef9228995d97316bfabfafb649f9d78cbc318
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 "bus.h"
22 #include "mmc_ops.h"
23 #include "sd_ops.h"
25 static const unsigned int tran_exp[] = {
26 10000, 100000, 1000000, 10000000,
27 0, 0, 0, 0
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) \
46 ({ \
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; \
51 u32 __res; \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
58 #else
59 #define UNSTUFF_BITS(resp, start, size) \
60 ({ \
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; \
65 u32 __res; \
67 __res = resp[__off] >> __shft; \
68 if (__size + __shft > 32) \
69 __res |= resp[__off+1] << ((32 - __shft) % 32); \
70 __res & __mask; \
72 #endif
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) {
115 case 0:
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);
137 break;
138 case 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;
165 break;
166 default:
167 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
168 mmc_hostname(card->host), csd_struct);
169 return -EINVAL;
172 return 0;
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;
182 u32 resp[4];
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);
191 return -EINVAL;
194 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
195 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
197 return 0;
201 * Fetches and decodes switch information
203 static int mmc_read_switch(struct mmc_card *card)
205 int err;
206 u8 *status;
208 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
209 return 0;
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));
215 return 0;
218 err = -EIO;
220 status = kmalloc(64, GFP_KERNEL);
221 if (!status) {
222 printk(KERN_ERR "%s: could not allocate a buffer for "
223 "switch capabilities.\n", mmc_hostname(card->host));
224 return -ENOMEM;
227 err = mmc_sd_switch(card, 0, 0, 1, status);
228 if (err) {
229 /* If the host or the card can't do the switch,
230 * fail more gracefully. */
231 if ((err != -EINVAL)
232 && (err != -ENOSYS)
233 && (err != -EFAULT))
234 goto out;
236 printk(KERN_WARNING "%s: problem reading switch "
237 "capabilities, performance might suffer.\n",
238 mmc_hostname(card->host));
239 err = 0;
241 goto out;
244 if (status[13] & 0x02)
245 card->sw_caps.hs_max_dtr = 50000000;
247 out:
248 kfree(status);
250 return err;
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)
258 int err;
259 u8 *status;
261 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
262 return 0;
264 if (!(card->csd.cmdclass & CCC_SWITCH))
265 return 0;
267 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
268 return 0;
270 if (card->sw_caps.hs_max_dtr == 0)
271 return 0;
273 err = -EIO;
275 status = kmalloc(64, GFP_KERNEL);
276 if (!status) {
277 printk(KERN_ERR "%s: could not allocate a buffer for "
278 "switch capabilities.\n", mmc_hostname(card->host));
279 return -ENOMEM;
282 err = mmc_sd_switch(card, 1, 0, 1, status);
283 if (err)
284 goto out;
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));
290 } else {
291 mmc_card_set_highspeed(card);
292 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
295 out:
296 kfree(status);
298 return err;
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[] = {
316 &dev_attr_cid.attr,
317 &dev_attr_csd.attr,
318 &dev_attr_scr.attr,
319 &dev_attr_date.attr,
320 &dev_attr_fwrev.attr,
321 &dev_attr_hwrev.attr,
322 &dev_attr_manfid.attr,
323 &dev_attr_name.attr,
324 &dev_attr_oemid.attr,
325 &dev_attr_serial.attr,
326 NULL,
329 static struct attribute_group sd_std_attr_group = {
330 .attrs = sd_std_attrs,
333 static const struct attribute_group *sd_attr_groups[] = {
334 &sd_std_attr_group,
335 NULL,
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;
352 int err;
353 u32 cid[4];
354 unsigned int max_dtr;
356 BUG_ON(!host);
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
363 * respond.
365 mmc_go_idle(host);
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);
374 if (!err)
375 ocr |= 1 << 30;
377 err = mmc_send_app_op_cond(host, ocr, NULL);
378 if (err)
379 goto err;
382 * Fetch CID from card.
384 if (mmc_host_is_spi(host))
385 err = mmc_send_cid(host, cid);
386 else
387 err = mmc_all_send_cid(host, cid);
388 if (err)
389 goto err;
391 if (oldcard) {
392 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
393 err = -ENOENT;
394 goto err;
397 card = oldcard;
398 } else {
400 * Allocate card structure.
402 card = mmc_alloc_card(host, &sd_type);
403 if (IS_ERR(card)) {
404 err = PTR_ERR(card);
405 goto err;
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);
417 if (err)
418 goto free_card;
420 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
423 if (!oldcard) {
425 * Fetch CSD from card.
427 err = mmc_send_csd(card, card->raw_csd);
428 if (err)
429 goto free_card;
431 err = mmc_decode_csd(card);
432 if (err)
433 goto free_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);
443 if (err)
444 goto free_card;
447 if (!oldcard) {
449 * Fetch SCR from card.
451 err = mmc_app_send_scr(card, card->raw_scr);
452 if (err)
453 goto free_card;
455 err = mmc_decode_scr(card);
456 if (err < 0)
457 goto free_card;
460 * Fetch switch information from card.
462 err = mmc_read_switch(card);
463 if (err)
464 goto free_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);
475 if (err)
476 goto free_card;
480 * Attempt to change to high-speed (if supported)
482 err = mmc_switch_hs(card);
483 if (err)
484 goto free_card;
487 * Compute bus speed.
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);
506 if (err)
507 goto free_card;
509 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
513 * Check if read-only switch is active.
515 if (!oldcard) {
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",
520 mmc_hostname(host));
521 } else {
522 if (host->ops->get_ro(host) > 0)
523 mmc_card_set_readonly(card);
527 if (!oldcard)
528 host->card = card;
530 return 0;
532 free_card:
533 if (!oldcard)
534 mmc_remove_card(card);
535 err:
537 return err;
541 * Host is being removed. Free up the current card.
543 static void mmc_sd_remove(struct mmc_host *host)
545 BUG_ON(!host);
546 BUG_ON(!host->card);
548 mmc_remove_card(host->card);
549 host->card = NULL;
553 * Card detection callback from host.
555 static void mmc_sd_detect(struct mmc_host *host)
557 int err;
559 BUG_ON(!host);
560 BUG_ON(!host->card);
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);
571 if (err) {
572 mmc_sd_remove(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)
585 BUG_ON(!host);
586 BUG_ON(!host->card);
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);
594 return 0;
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)
605 int err;
607 BUG_ON(!host);
608 BUG_ON(!host->card);
610 mmc_claim_host(host);
611 err = mmc_sd_init_card(host, host->ocr, host->card);
612 mmc_release_host(host);
614 return err;
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);
640 #else
642 static const struct mmc_bus_ops mmc_sd_ops = {
643 .remove = mmc_sd_remove,
644 .detect = mmc_sd_detect,
645 .suspend = NULL,
646 .resume = NULL,
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;
664 else
665 bus_ops = &mmc_sd_ops;
666 mmc_attach_bus(host, bus_ops);
669 #endif
672 * Starting point for SD card init.
674 int mmc_attach_sd(struct mmc_host *host, u32 ocr)
676 int err;
678 BUG_ON(!host);
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)) {
687 mmc_go_idle(host);
689 err = mmc_spi_read_ocr(host, 0, &ocr);
690 if (err)
691 goto err;
695 * Sanity check the voltages that the card claims to
696 * support.
698 if (ocr & 0x7F) {
699 printk(KERN_WARNING "%s: card claims to support voltages "
700 "below the defined range. These will be ignored.\n",
701 mmc_hostname(host));
702 ocr &= ~0x7F;
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)?
717 if (!host->ocr) {
718 err = -EINVAL;
719 goto err;
723 * Detect and init the card.
725 err = mmc_sd_init_card(host, host->ocr, NULL);
726 if (err)
727 goto err;
729 mmc_release_host(host);
731 err = mmc_add_card(host->card);
732 if (err)
733 goto remove_card;
735 return 0;
737 remove_card:
738 mmc_remove_card(host->card);
739 host->card = NULL;
740 mmc_claim_host(host);
741 err:
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);
748 return err;