aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mmc
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/mmc')
-rw-r--r--drivers/mmc/core/Makefile2
-rw-r--r--drivers/mmc/core/core.c817
-rw-r--r--drivers/mmc/core/core.h37
-rw-r--r--drivers/mmc/core/mmc.c430
-rw-r--r--drivers/mmc/core/sd.c431
5 files changed, 1035 insertions, 682 deletions
diff --git a/drivers/mmc/core/Makefile b/drivers/mmc/core/Makefile
index 5977abf3e41b..1075b02ae754 100644
--- a/drivers/mmc/core/Makefile
+++ b/drivers/mmc/core/Makefile
@@ -7,5 +7,5 @@ ifeq ($(CONFIG_MMC_DEBUG),y)
7endif 7endif
8 8
9obj-$(CONFIG_MMC) += mmc_core.o 9obj-$(CONFIG_MMC) += mmc_core.o
10mmc_core-y := core.o sysfs.o mmc_ops.o sd_ops.o 10mmc_core-y := core.o sysfs.o mmc.o mmc_ops.o sd.o sd_ops.o
11 11
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 310be2fe1944..75333a2461df 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -32,36 +32,8 @@
32#include "mmc_ops.h" 32#include "mmc_ops.h"
33#include "sd_ops.h" 33#include "sd_ops.h"
34 34
35#define CMD_RETRIES 3 35extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
36 36extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
37/*
38 * OCR Bit positions to 10s of Vdd mV.
39 */
40static const unsigned short mmc_ocr_bit_to_vdd[] = {
41 150, 155, 160, 165, 170, 180, 190, 200,
42 210, 220, 230, 240, 250, 260, 270, 280,
43 290, 300, 310, 320, 330, 340, 350, 360
44};
45
46static const unsigned int tran_exp[] = {
47 10000, 100000, 1000000, 10000000,
48 0, 0, 0, 0
49};
50
51static const unsigned char tran_mant[] = {
52 0, 10, 12, 13, 15, 20, 25, 30,
53 35, 40, 45, 50, 55, 60, 70, 80,
54};
55
56static const unsigned int tacc_exp[] = {
57 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
58};
59
60static const unsigned int tacc_mant[] = {
61 0, 10, 12, 13, 15, 20, 25, 30,
62 35, 40, 45, 50, 55, 60, 70, 80,
63};
64
65 37
66/** 38/**
67 * mmc_request_done - finish processing an MMC request 39 * mmc_request_done - finish processing an MMC request
@@ -303,6 +275,10 @@ void mmc_release_host(struct mmc_host *host)
303 275
304EXPORT_SYMBOL(mmc_release_host); 276EXPORT_SYMBOL(mmc_release_host);
305 277
278/*
279 * Internal function that does the actual ios call to the host driver,
280 * optionally printing some debug output.
281 */
306static inline void mmc_set_ios(struct mmc_host *host) 282static inline void mmc_set_ios(struct mmc_host *host)
307{ 283{
308 struct mmc_ios *ios = &host->ios; 284 struct mmc_ios *ios = &host->ios;
@@ -316,6 +292,9 @@ static inline void mmc_set_ios(struct mmc_host *host)
316 host->ops->set_ios(host, ios); 292 host->ops->set_ios(host, ios);
317} 293}
318 294
295/*
296 * Control chip select pin on a host.
297 */
319void mmc_set_chip_select(struct mmc_host *host, int mode) 298void mmc_set_chip_select(struct mmc_host *host, int mode)
320{ 299{
321 host->ios.chip_select = mode; 300 host->ios.chip_select = mode;
@@ -323,10 +302,43 @@ void mmc_set_chip_select(struct mmc_host *host, int mode)
323} 302}
324 303
325/* 304/*
305 * Sets the host clock to the highest possible frequency that
306 * is below "hz".
307 */
308void mmc_set_clock(struct mmc_host *host, unsigned int hz)
309{
310 WARN_ON(hz < host->f_min);
311
312 if (hz > host->f_max)
313 hz = host->f_max;
314
315 host->ios.clock = hz;
316 mmc_set_ios(host);
317}
318
319/*
320 * Change the bus mode (open drain/push-pull) of a host.
321 */
322void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
323{
324 host->ios.bus_mode = mode;
325 mmc_set_ios(host);
326}
327
328/*
329 * Change data bus width of a host.
330 */
331void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
332{
333 host->ios.bus_width = width;
334 mmc_set_ios(host);
335}
336
337/*
326 * Mask off any voltages we don't support and select 338 * Mask off any voltages we don't support and select
327 * the lowest voltage 339 * the lowest voltage
328 */ 340 */
329static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 341u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
330{ 342{
331 int bit; 343 int bit;
332 344
@@ -347,235 +359,19 @@ static u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
347 return ocr; 359 return ocr;
348} 360}
349 361
350#define UNSTUFF_BITS(resp,start,size) \
351 ({ \
352 const int __size = size; \
353 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
354 const int __off = 3 - ((start) / 32); \
355 const int __shft = (start) & 31; \
356 u32 __res; \
357 \
358 __res = resp[__off] >> __shft; \
359 if (__size + __shft > 32) \
360 __res |= resp[__off-1] << ((32 - __shft) % 32); \
361 __res & __mask; \
362 })
363
364/*
365 * Given the decoded CSD structure, decode the raw CID to our CID structure.
366 */
367static void mmc_decode_cid(struct mmc_card *card)
368{
369 u32 *resp = card->raw_cid;
370
371 memset(&card->cid, 0, sizeof(struct mmc_cid));
372
373 if (mmc_card_sd(card)) {
374 /*
375 * SD doesn't currently have a version field so we will
376 * have to assume we can parse this.
377 */
378 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
379 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
380 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
381 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
382 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
383 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
384 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
385 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
386 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
387 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
388 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
389 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
390
391 card->cid.year += 2000; /* SD cards year offset */
392 } else {
393 /*
394 * The selection of the format here is based upon published
395 * specs from sandisk and from what people have reported.
396 */
397 switch (card->csd.mmca_vsn) {
398 case 0: /* MMC v1.0 - v1.2 */
399 case 1: /* MMC v1.4 */
400 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
401 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
402 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
403 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
404 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
405 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
406 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
407 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
408 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
409 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
410 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
411 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
412 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
413 break;
414
415 case 2: /* MMC v2.0 - v2.2 */
416 case 3: /* MMC v3.1 - v3.3 */
417 case 4: /* MMC v4 */
418 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
419 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
420 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
421 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
422 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
423 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
424 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
425 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
426 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
427 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
428 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
429 break;
430
431 default:
432 printk("%s: card has unknown MMCA version %d\n",
433 mmc_hostname(card->host), card->csd.mmca_vsn);
434 mmc_card_set_bad(card);
435 break;
436 }
437 }
438}
439
440/*
441 * Given a 128-bit response, decode to our card CSD structure.
442 */
443static void mmc_decode_csd(struct mmc_card *card)
444{
445 struct mmc_csd *csd = &card->csd;
446 unsigned int e, m, csd_struct;
447 u32 *resp = card->raw_csd;
448
449 if (mmc_card_sd(card)) {
450 csd_struct = UNSTUFF_BITS(resp, 126, 2);
451
452 switch (csd_struct) {
453 case 0:
454 m = UNSTUFF_BITS(resp, 115, 4);
455 e = UNSTUFF_BITS(resp, 112, 3);
456 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
457 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
458
459 m = UNSTUFF_BITS(resp, 99, 4);
460 e = UNSTUFF_BITS(resp, 96, 3);
461 csd->max_dtr = tran_exp[e] * tran_mant[m];
462 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
463
464 e = UNSTUFF_BITS(resp, 47, 3);
465 m = UNSTUFF_BITS(resp, 62, 12);
466 csd->capacity = (1 + m) << (e + 2);
467
468 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
469 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
470 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
471 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
472 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
473 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
474 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
475 break;
476 case 1:
477 /*
478 * This is a block-addressed SDHC card. Most
479 * interesting fields are unused and have fixed
480 * values. To avoid getting tripped by buggy cards,
481 * we assume those fixed values ourselves.
482 */
483 mmc_card_set_blockaddr(card);
484
485 csd->tacc_ns = 0; /* Unused */
486 csd->tacc_clks = 0; /* Unused */
487
488 m = UNSTUFF_BITS(resp, 99, 4);
489 e = UNSTUFF_BITS(resp, 96, 3);
490 csd->max_dtr = tran_exp[e] * tran_mant[m];
491 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
492
493 m = UNSTUFF_BITS(resp, 48, 22);
494 csd->capacity = (1 + m) << 10;
495
496 csd->read_blkbits = 9;
497 csd->read_partial = 0;
498 csd->write_misalign = 0;
499 csd->read_misalign = 0;
500 csd->r2w_factor = 4; /* Unused */
501 csd->write_blkbits = 9;
502 csd->write_partial = 0;
503 break;
504 default:
505 printk("%s: unrecognised CSD structure version %d\n",
506 mmc_hostname(card->host), csd_struct);
507 mmc_card_set_bad(card);
508 return;
509 }
510 } else {
511 /*
512 * We only understand CSD structure v1.1 and v1.2.
513 * v1.2 has extra information in bits 15, 11 and 10.
514 */
515 csd_struct = UNSTUFF_BITS(resp, 126, 2);
516 if (csd_struct != 1 && csd_struct != 2) {
517 printk("%s: unrecognised CSD structure version %d\n",
518 mmc_hostname(card->host), csd_struct);
519 mmc_card_set_bad(card);
520 return;
521 }
522
523 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
524 m = UNSTUFF_BITS(resp, 115, 4);
525 e = UNSTUFF_BITS(resp, 112, 3);
526 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
527 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
528
529 m = UNSTUFF_BITS(resp, 99, 4);
530 e = UNSTUFF_BITS(resp, 96, 3);
531 csd->max_dtr = tran_exp[e] * tran_mant[m];
532 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
533
534 e = UNSTUFF_BITS(resp, 47, 3);
535 m = UNSTUFF_BITS(resp, 62, 12);
536 csd->capacity = (1 + m) << (e + 2);
537
538 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
539 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
540 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
541 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
542 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
543 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
544 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
545 }
546}
547
548/* 362/*
549 * Given a 64-bit response, decode to our card SCR structure. 363 * Select timing parameters for host.
550 */ 364 */
551static void mmc_decode_scr(struct mmc_card *card) 365void mmc_set_timing(struct mmc_host *host, unsigned int timing)
552{ 366{
553 struct sd_scr *scr = &card->scr; 367 host->ios.timing = timing;
554 unsigned int scr_struct; 368 mmc_set_ios(host);
555 u32 resp[4];
556
557 BUG_ON(!mmc_card_sd(card));
558
559 resp[3] = card->raw_scr[1];
560 resp[2] = card->raw_scr[0];
561
562 scr_struct = UNSTUFF_BITS(resp, 60, 4);
563 if (scr_struct != 0) {
564 printk("%s: unrecognised SCR structure version %d\n",
565 mmc_hostname(card->host), scr_struct);
566 mmc_card_set_bad(card);
567 return;
568 }
569
570 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
571 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
572} 369}
573 370
574/* 371/*
575 * Allocate a new MMC card 372 * Allocate a new MMC card
576 */ 373 */
577static struct mmc_card * 374struct mmc_card *mmc_alloc_card(struct mmc_host *host)
578mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
579{ 375{
580 struct mmc_card *card; 376 struct mmc_card *card;
581 377
@@ -584,7 +380,6 @@ mmc_alloc_card(struct mmc_host *host, u32 *raw_cid)
584 return ERR_PTR(-ENOMEM); 380 return ERR_PTR(-ENOMEM);
585 381
586 mmc_init_card(card, host); 382 mmc_init_card(card, host);
587 memcpy(card->raw_cid, raw_cid, sizeof(card->raw_cid));
588 383
589 return card; 384 return card;
590} 385}
@@ -634,406 +429,66 @@ static void mmc_power_off(struct mmc_host *host)
634} 429}
635 430
636/* 431/*
637 * Discover the card by requesting its CID. 432 * Assign a mmc bus handler to a host. Only one bus handler may control a
638 * 433 * host at any given time.
639 * Create a mmc_card entry for the discovered card, assigning
640 * it an RCA, and save the raw CID for decoding later.
641 */ 434 */
642static void mmc_discover_card(struct mmc_host *host) 435void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
643{ 436{
644 unsigned int err; 437 unsigned long flags;
645 u32 cid[4];
646
647 BUG_ON(host->card);
648
649 err = mmc_all_send_cid(host, cid);
650 if (err != MMC_ERR_NONE) {
651 printk(KERN_ERR "%s: error requesting CID: %d\n",
652 mmc_hostname(host), err);
653 return;
654 }
655
656 host->card = mmc_alloc_card(host, cid);
657 if (IS_ERR(host->card)) {
658 err = PTR_ERR(host->card);
659 host->card = NULL;
660 return;
661 }
662
663 if (host->mode == MMC_MODE_SD) {
664 host->card->type = MMC_TYPE_SD;
665
666 err = mmc_send_relative_addr(host, &host->card->rca);
667 if (err != MMC_ERR_NONE)
668 mmc_card_set_dead(host->card);
669 else {
670 if (!host->ops->get_ro) {
671 printk(KERN_WARNING "%s: host does not "
672 "support reading read-only "
673 "switch. assuming write-enable.\n",
674 mmc_hostname(host));
675 } else {
676 if (host->ops->get_ro(host))
677 mmc_card_set_readonly(host->card);
678 }
679 }
680 } else {
681 host->card->type = MMC_TYPE_MMC;
682 host->card->rca = 1;
683
684 err = mmc_set_relative_addr(host->card);
685 if (err != MMC_ERR_NONE)
686 mmc_card_set_dead(host->card);
687 }
688}
689
690static void mmc_read_csd(struct mmc_host *host)
691{
692 int err;
693
694 if (!host->card)
695 return;
696 if (mmc_card_dead(host->card))
697 return;
698
699 err = mmc_send_csd(host->card, host->card->raw_csd);
700 if (err != MMC_ERR_NONE) {
701 mmc_card_set_dead(host->card);
702 return;
703 }
704
705 mmc_decode_csd(host->card);
706 mmc_decode_cid(host->card);
707}
708
709static void mmc_process_ext_csd(struct mmc_host *host)
710{
711 int err;
712 u8 *ext_csd;
713
714 if (!host->card)
715 return;
716 if (mmc_card_dead(host->card))
717 return;
718 if (mmc_card_sd(host->card))
719 return;
720 if (host->card->csd.mmca_vsn < CSD_SPEC_VER_4)
721 return;
722
723 /*
724 * As the ext_csd is so large and mostly unused, we don't store the
725 * raw block in mmc_card.
726 */
727 ext_csd = kmalloc(512, GFP_KERNEL);
728 if (!ext_csd) {
729 printk("%s: could not allocate a buffer to receive the ext_csd."
730 "mmc v4 cards will be treated as v3.\n",
731 mmc_hostname(host));
732 return;
733 }
734
735 err = mmc_send_ext_csd(host->card, ext_csd);
736 if (err != MMC_ERR_NONE) {
737 if (host->card->csd.capacity == (4096 * 512)) {
738 printk(KERN_ERR "%s: unable to read EXT_CSD "
739 "on a possible high capacity card. "
740 "Card will be ignored.\n",
741 mmc_hostname(host));
742 mmc_card_set_dead(host->card);
743 } else {
744 printk(KERN_WARNING "%s: unable to read "
745 "EXT_CSD, performance might "
746 "suffer.\n",
747 mmc_hostname(host));
748 }
749 goto out;
750 }
751
752 host->card->ext_csd.sectors =
753 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
754 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
755 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
756 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
757 if (host->card->ext_csd.sectors)
758 mmc_card_set_blockaddr(host->card);
759
760 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
761 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
762 host->card->ext_csd.hs_max_dtr = 52000000;
763 break;
764 case EXT_CSD_CARD_TYPE_26:
765 host->card->ext_csd.hs_max_dtr = 26000000;
766 break;
767 default:
768 /* MMC v4 spec says this cannot happen */
769 printk("%s: card is mmc v4 but doesn't support "
770 "any high-speed modes.\n",
771 mmc_hostname(host));
772 goto out;
773 }
774
775 if (host->caps & MMC_CAP_MMC_HIGHSPEED) {
776 /* Activate highspeed support. */
777 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
778 EXT_CSD_HS_TIMING, 1);
779 if (err != MMC_ERR_NONE) {
780 printk("%s: failed to switch card to mmc v4 "
781 "high-speed mode.\n",
782 mmc_hostname(host));
783 goto out;
784 }
785
786 mmc_card_set_highspeed(host->card);
787
788 host->ios.timing = MMC_TIMING_MMC_HS;
789 mmc_set_ios(host);
790 }
791 438
792 /* Check for host support for wide-bus modes. */ 439 BUG_ON(!host);
793 if (host->caps & MMC_CAP_4_BIT_DATA) { 440 BUG_ON(!ops);
794 /* Activate 4-bit support. */
795 err = mmc_switch(host->card, MMC_SWITCH_MODE_WRITE_BYTE,
796 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4 |
797 EXT_CSD_CMD_SET_NORMAL);
798 if (err != MMC_ERR_NONE) {
799 printk("%s: failed to switch card to "
800 "mmc v4 4-bit bus mode.\n",
801 mmc_hostname(host));
802 goto out;
803 }
804 441
805 host->ios.bus_width = MMC_BUS_WIDTH_4; 442 BUG_ON(!host->claimed);
806 mmc_set_ios(host);
807 }
808 443
809out: 444 spin_lock_irqsave(&host->lock, flags);
810 kfree(ext_csd);
811}
812 445
813static void mmc_read_scr(struct mmc_host *host) 446 BUG_ON(host->bus_ops);
814{ 447 BUG_ON(host->bus_refs);
815 int err;
816 448
817 if (!host->card) 449 host->bus_ops = ops;
818 return; 450 host->bus_refs = 1;
819 if (mmc_card_dead(host->card)) 451 host->bus_dead = 0;
820 return;
821 if (!mmc_card_sd(host->card))
822 return;
823
824 err = mmc_app_send_scr(host->card, host->card->raw_scr);
825 if (err != MMC_ERR_NONE) {
826 mmc_card_set_dead(host->card);
827 return;
828 }
829 452
830 mmc_decode_scr(host->card); 453 spin_unlock_irqrestore(&host->lock, flags);
831} 454}
832 455
833static void mmc_read_switch_caps(struct mmc_host *host) 456/*
457 * Remove the current bus handler from a host. Assumes that there are
458 * no interesting cards left, so the bus is powered down.
459 */
460void mmc_detach_bus(struct mmc_host *host)
834{ 461{
835 int err; 462 unsigned long flags;
836 unsigned char *status;
837
838 if (!(host->caps & MMC_CAP_SD_HIGHSPEED))
839 return;
840
841 if (!host->card)
842 return;
843 if (mmc_card_dead(host->card))
844 return;
845 if (!mmc_card_sd(host->card))
846 return;
847 if (host->card->scr.sda_vsn < SCR_SPEC_VER_1)
848 return;
849
850 status = kmalloc(64, GFP_KERNEL);
851 if (!status) {
852 printk(KERN_WARNING "%s: Unable to allocate buffer for "
853 "reading switch capabilities.\n",
854 mmc_hostname(host));
855 return;
856 }
857
858 err = mmc_sd_switch(host->card, SD_SWITCH_CHECK,
859 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
860 if (err != MMC_ERR_NONE) {
861 printk("%s: unable to read switch capabilities, "
862 "performance might suffer.\n",
863 mmc_hostname(host));
864 goto out;
865 }
866
867 if (status[13] & 0x02)
868 host->card->sw_caps.hs_max_dtr = 50000000;
869 463
870 err = mmc_sd_switch(host->card, SD_SWITCH_SET, 464 BUG_ON(!host);
871 SD_SWITCH_GRP_ACCESS, SD_SWITCH_ACCESS_HS, status);
872 if (err != MMC_ERR_NONE || (status[16] & 0xF) != 1) {
873 printk(KERN_WARNING "%s: Problem switching card "
874 "into high-speed mode!\n",
875 mmc_hostname(host));
876 goto out;
877 }
878 465
879 mmc_card_set_highspeed(host->card); 466 BUG_ON(!host->claimed);
467 BUG_ON(!host->bus_ops);
880 468
881 host->ios.timing = MMC_TIMING_SD_HS; 469 spin_lock_irqsave(&host->lock, flags);
882 mmc_set_ios(host);
883 470
884out: 471 host->bus_dead = 1;
885 kfree(status);
886}
887 472
888static unsigned int mmc_calculate_clock(struct mmc_host *host) 473 spin_unlock_irqrestore(&host->lock, flags);
889{
890 unsigned int max_dtr = host->f_max;
891
892 if (host->card && !mmc_card_dead(host->card)) {
893 if (mmc_card_highspeed(host->card) && mmc_card_sd(host->card)) {
894 if (max_dtr > host->card->sw_caps.hs_max_dtr)
895 max_dtr = host->card->sw_caps.hs_max_dtr;
896 } else if (mmc_card_highspeed(host->card) && !mmc_card_sd(host->card)) {
897 if (max_dtr > host->card->ext_csd.hs_max_dtr)
898 max_dtr = host->card->ext_csd.hs_max_dtr;
899 } else if (max_dtr > host->card->csd.max_dtr) {
900 max_dtr = host->card->csd.max_dtr;
901 }
902 }
903 474
904 pr_debug("%s: selected %d.%03dMHz transfer rate\n", 475 mmc_power_off(host);
905 mmc_hostname(host),
906 max_dtr / 1000000, (max_dtr / 1000) % 1000);
907 476
908 return max_dtr; 477 mmc_bus_put(host);
909} 478}
910 479
911/* 480/*
912 * Check whether cards we already know about are still present. 481 * Cleanup when the last reference to the bus operator is dropped.
913 * We do this by requesting status, and checking whether a card
914 * responds.
915 *
916 * A request for status does not cause a state change in data
917 * transfer mode.
918 */ 482 */
919static void mmc_check_card(struct mmc_card *card) 483void __mmc_release_bus(struct mmc_host *host)
920{ 484{
921 int err; 485 BUG_ON(!host);
922 486 BUG_ON(host->bus_refs);
923 BUG_ON(!card); 487 BUG_ON(!host->bus_dead);
924 488
925 err = mmc_send_status(card, NULL); 489 host->bus_ops = NULL;
926 if (err == MMC_ERR_NONE)
927 return;
928
929 mmc_card_set_dead(card);
930} 490}
931 491
932static void mmc_setup(struct mmc_host *host)
933{
934 int err;
935 u32 ocr;
936
937 host->mode = MMC_MODE_SD;
938
939 mmc_power_up(host);
940 mmc_go_idle(host);
941
942 err = mmc_send_if_cond(host, host->ocr_avail);
943 if (err != MMC_ERR_NONE) {
944 return;
945 }
946 err = mmc_send_app_op_cond(host, 0, &ocr);
947
948 /*
949 * If we fail to detect any SD cards then try
950 * searching for MMC cards.
951 */
952 if (err != MMC_ERR_NONE) {
953 host->mode = MMC_MODE_MMC;
954
955 err = mmc_send_op_cond(host, 0, &ocr);
956 if (err != MMC_ERR_NONE)
957 return;
958 }
959
960 host->ocr = mmc_select_voltage(host, ocr);
961
962 if (host->ocr == 0)
963 return;
964
965 /*
966 * Since we're changing the OCR value, we seem to
967 * need to tell some cards to go back to the idle
968 * state. We wait 1ms to give cards time to
969 * respond.
970 */
971 mmc_go_idle(host);
972
973 /*
974 * Send the selected OCR multiple times... until the cards
975 * all get the idea that they should be ready for CMD2.
976 * (My SanDisk card seems to need this.)
977 */
978 if (host->mode == MMC_MODE_SD) {
979 /*
980 * If SD_SEND_IF_COND indicates an SD 2.0
981 * compliant card and we should set bit 30
982 * of the ocr to indicate that we can handle
983 * block-addressed SDHC cards.
984 */
985 err = mmc_send_if_cond(host, host->ocr);
986 if (err == MMC_ERR_NONE)
987 ocr = host->ocr | (1 << 30);
988
989 mmc_send_app_op_cond(host, ocr, NULL);
990 } else {
991 /* The extra bit indicates that we support high capacity */
992 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
993 }
994
995 mmc_discover_card(host);
996
997 /*
998 * Ok, now switch to push-pull mode.
999 */
1000 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1001 mmc_set_ios(host);
1002
1003 mmc_read_csd(host);
1004
1005 if (host->card && !mmc_card_dead(host->card)) {
1006 err = mmc_select_card(host->card);
1007 if (err != MMC_ERR_NONE)
1008 mmc_card_set_dead(host->card);
1009 }
1010
1011 /*
1012 * The card is in 1 bit mode by default so
1013 * we only need to change if it supports the
1014 * wider version.
1015 */
1016 if (host->card && !mmc_card_dead(host->card) &&
1017 mmc_card_sd(host->card) &&
1018 (host->card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) &&
1019 (host->card->host->caps & MMC_CAP_4_BIT_DATA)) {
1020 err = mmc_app_set_bus_width(host->card, SD_BUS_WIDTH_4);
1021 if (err != MMC_ERR_NONE)
1022 mmc_card_set_dead(host->card);
1023 else {
1024 host->ios.bus_width = MMC_BUS_WIDTH_4;
1025 mmc_set_ios(host);
1026 }
1027 }
1028
1029 if (host->mode == MMC_MODE_SD) {
1030 mmc_read_scr(host);
1031 mmc_read_switch_caps(host);
1032 } else
1033 mmc_process_ext_csd(host);
1034}
1035
1036
1037/** 492/**
1038 * mmc_detect_change - process change of state on a MMC socket 493 * mmc_detect_change - process change of state on a MMC socket
1039 * @host: host which changed state. 494 * @host: host which changed state.
@@ -1060,62 +515,49 @@ static void mmc_rescan(struct work_struct *work)
1060{ 515{
1061 struct mmc_host *host = 516 struct mmc_host *host =
1062 container_of(work, struct mmc_host, detect.work); 517 container_of(work, struct mmc_host, detect.work);
518 u32 ocr;
519 int err;
1063 520
1064 mmc_claim_host(host); 521 mmc_bus_get(host);
1065
1066 /*
1067 * Check for removed card and newly inserted ones. We check for
1068 * removed cards first so we can intelligently re-select the VDD.
1069 */
1070 if (host->card) {
1071 mmc_check_card(host->card);
1072
1073 mmc_release_host(host);
1074
1075 if (mmc_card_dead(host->card)) {
1076 mmc_remove_card(host->card);
1077 host->card = NULL;
1078 }
1079
1080 goto out;
1081 }
1082
1083 mmc_setup(host);
1084 522
1085 if (host->card && !mmc_card_dead(host->card)) { 523 if (host->bus_ops == NULL) {
1086 /* 524 /*
1087 * (Re-)calculate the fastest clock rate which the 525 * Only we can add a new handler, so it's safe to
1088 * attached cards and the host support. 526 * release the lock here.
1089 */ 527 */
1090 host->ios.clock = mmc_calculate_clock(host); 528 mmc_bus_put(host);
1091 mmc_set_ios(host);
1092 }
1093 529
1094 mmc_release_host(host); 530 mmc_claim_host(host);
1095 531
1096 /* 532 mmc_power_up(host);
1097 * If this is a new and good card, register it. 533 mmc_go_idle(host);
1098 */
1099 if (host->card && !mmc_card_dead(host->card)) {
1100 if (mmc_register_card(host->card))
1101 mmc_card_set_dead(host->card);
1102 }
1103 534
1104 /* 535 mmc_send_if_cond(host, host->ocr_avail);
1105 * If this card is dead, destroy it.
1106 */
1107 if (host->card && mmc_card_dead(host->card)) {
1108 mmc_remove_card(host->card);
1109 host->card = NULL;
1110 }
1111 536
1112out: 537 err = mmc_send_app_op_cond(host, 0, &ocr);
1113 /* 538 if (err == MMC_ERR_NONE) {
1114 * If we discover that there are no cards on the 539 if (mmc_attach_sd(host, ocr))
1115 * bus, turn off the clock and power down. 540 mmc_power_off(host);
1116 */ 541 } else {
1117 if (!host->card) 542 /*
1118 mmc_power_off(host); 543 * If we fail to detect any SD cards then try
544 * searching for MMC cards.
545 */
546 err = mmc_send_op_cond(host, 0, &ocr);
547 if (err == MMC_ERR_NONE) {
548 if (mmc_attach_mmc(host, ocr))
549 mmc_power_off(host);
550 } else {
551 mmc_power_off(host);
552 mmc_release_host(host);
553 }
554 }
555 } else {
556 if (host->bus_ops->detect && !host->bus_dead)
557 host->bus_ops->detect(host);
558
559 mmc_bus_put(host);
560 }
1119} 561}
1120 562
1121 563
@@ -1190,10 +632,18 @@ void mmc_remove_host(struct mmc_host *host)
1190 632
1191 mmc_flush_scheduled_work(); 633 mmc_flush_scheduled_work();
1192 634
1193 if (host->card) { 635 mmc_bus_get(host);
1194 mmc_remove_card(host->card); 636 if (host->bus_ops && !host->bus_dead) {
1195 host->card = NULL; 637 if (host->bus_ops->remove)
638 host->bus_ops->remove(host);
639
640 mmc_claim_host(host);
641 mmc_detach_bus(host);
642 mmc_release_host(host);
1196 } 643 }
644 mmc_bus_put(host);
645
646 BUG_ON(host->card);
1197 647
1198 mmc_power_off(host); 648 mmc_power_off(host);
1199 mmc_remove_host_sysfs(host); 649 mmc_remove_host_sysfs(host);
@@ -1225,10 +675,15 @@ int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1225{ 675{
1226 mmc_flush_scheduled_work(); 676 mmc_flush_scheduled_work();
1227 677
1228 if (host->card) { 678 mmc_bus_get(host);
1229 mmc_remove_card(host->card); 679 if (host->bus_ops && !host->bus_dead) {
1230 host->card = NULL; 680 if (host->bus_ops->remove)
681 host->bus_ops->remove(host);
682 mmc_detach_bus(host);
1231 } 683 }
684 mmc_bus_put(host);
685
686 BUG_ON(host->card);
1232 687
1233 mmc_power_off(host); 688 mmc_power_off(host);
1234 689
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index 1c1066342fba..fad8edc38099 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -15,7 +15,44 @@
15 15
16#define MMC_CMD_RETRIES 3 16#define MMC_CMD_RETRIES 3
17 17
18struct mmc_bus_ops {
19 void (*remove)(struct mmc_host *);
20 void (*detect)(struct mmc_host *);
21};
22
23void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
24void mmc_detach_bus(struct mmc_host *host);
25
26void __mmc_release_bus(struct mmc_host *host);
27
28static inline void mmc_bus_get(struct mmc_host *host)
29{
30 unsigned long flags;
31
32 spin_lock_irqsave(&host->lock, flags);
33 host->bus_refs++;
34 spin_unlock_irqrestore(&host->lock, flags);
35}
36
37static inline void mmc_bus_put(struct mmc_host *host)
38{
39 unsigned long flags;
40
41 spin_lock_irqsave(&host->lock, flags);
42 host->bus_refs--;
43 if ((host->bus_refs == 0) && host->bus_ops)
44 __mmc_release_bus(host);
45 spin_unlock_irqrestore(&host->lock, flags);
46}
47
18void mmc_set_chip_select(struct mmc_host *host, int mode); 48void mmc_set_chip_select(struct mmc_host *host, int mode);
49void mmc_set_clock(struct mmc_host *host, unsigned int hz);
50void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
51void mmc_set_bus_width(struct mmc_host *host, unsigned int width);
52u32 mmc_select_voltage(struct mmc_host *host, u32 ocr);
53void mmc_set_timing(struct mmc_host *host, unsigned int timing);
54
55struct mmc_card *mmc_alloc_card(struct mmc_host *host);
19 56
20static inline void mmc_delay(unsigned int ms) 57static inline void mmc_delay(unsigned int ms)
21{ 58{
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
new file mode 100644
index 000000000000..c528017d6128
--- /dev/null
+++ b/drivers/mmc/core/mmc.c
@@ -0,0 +1,430 @@
1/*
2 * linux/drivers/mmc/mmc.c
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
7 *
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.
11 */
12
13#include <linux/err.h>
14
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
17#include <linux/mmc/mmc.h>
18
19#include "core.h"
20#include "sysfs.h"
21#include "mmc_ops.h"
22
23static const unsigned int tran_exp[] = {
24 10000, 100000, 1000000, 10000000,
25 0, 0, 0, 0
26};
27
28static const unsigned char tran_mant[] = {
29 0, 10, 12, 13, 15, 20, 25, 30,
30 35, 40, 45, 50, 55, 60, 70, 80,
31};
32
33static const unsigned int tacc_exp[] = {
34 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
35};
36
37static const unsigned int tacc_mant[] = {
38 0, 10, 12, 13, 15, 20, 25, 30,
39 35, 40, 45, 50, 55, 60, 70, 80,
40};
41
42#define UNSTUFF_BITS(resp,start,size) \
43 ({ \
44 const int __size = size; \
45 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
46 const int __off = 3 - ((start) / 32); \
47 const int __shft = (start) & 31; \
48 u32 __res; \
49 \
50 __res = resp[__off] >> __shft; \
51 if (__size + __shft > 32) \
52 __res |= resp[__off-1] << ((32 - __shft) % 32); \
53 __res & __mask; \
54 })
55
56/*
57 * Given the decoded CSD structure, decode the raw CID to our CID structure.
58 */
59static void mmc_decode_cid(struct mmc_card *card)
60{
61 u32 *resp = card->raw_cid;
62
63 /*
64 * The selection of the format here is based upon published
65 * specs from sandisk and from what people have reported.
66 */
67 switch (card->csd.mmca_vsn) {
68 case 0: /* MMC v1.0 - v1.2 */
69 case 1: /* MMC v1.4 */
70 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
71 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
72 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
73 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
74 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
75 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
76 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
77 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
78 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
79 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
80 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
81 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
82 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
83 break;
84
85 case 2: /* MMC v2.0 - v2.2 */
86 case 3: /* MMC v3.1 - v3.3 */
87 case 4: /* MMC v4 */
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.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
96 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
97 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
98 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
99 break;
100
101 default:
102 printk("%s: card has unknown MMCA version %d\n",
103 mmc_hostname(card->host), card->csd.mmca_vsn);
104 mmc_card_set_bad(card);
105 break;
106 }
107}
108
109/*
110 * Given a 128-bit response, decode to our card CSD structure.
111 */
112static void mmc_decode_csd(struct mmc_card *card)
113{
114 struct mmc_csd *csd = &card->csd;
115 unsigned int e, m, csd_struct;
116 u32 *resp = card->raw_csd;
117
118 /*
119 * We only understand CSD structure v1.1 and v1.2.
120 * v1.2 has extra information in bits 15, 11 and 10.
121 */
122 csd_struct = UNSTUFF_BITS(resp, 126, 2);
123 if (csd_struct != 1 && csd_struct != 2) {
124 printk("%s: unrecognised CSD structure version %d\n",
125 mmc_hostname(card->host), csd_struct);
126 mmc_card_set_bad(card);
127 return;
128 }
129
130 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
131 m = UNSTUFF_BITS(resp, 115, 4);
132 e = UNSTUFF_BITS(resp, 112, 3);
133 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
134 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
135
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);
140
141 e = UNSTUFF_BITS(resp, 47, 3);
142 m = UNSTUFF_BITS(resp, 62, 12);
143 csd->capacity = (1 + m) << (e + 2);
144
145 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
146 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
147 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
148 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
149 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
150 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
151 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
152}
153
154/*
155 * Read and decode extended CSD. Switch to high-speed and wide bus
156 * if supported.
157 */
158static int mmc_process_ext_csd(struct mmc_card *card)
159{
160 int err;
161 u8 *ext_csd;
162
163 BUG_ON(!card);
164
165 err = MMC_ERR_FAILED;
166
167 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
168 return MMC_ERR_NONE;
169
170 /*
171 * As the ext_csd is so large and mostly unused, we don't store the
172 * raw block in mmc_card.
173 */
174 ext_csd = kmalloc(512, GFP_KERNEL);
175 if (!ext_csd) {
176 printk(KERN_ERR "%s: could not allocate a buffer to "
177 "receive the ext_csd. mmc v4 cards will be "
178 "treated as v3.\n", mmc_hostname(card->host));
179 return MMC_ERR_FAILED;
180 }
181
182 err = mmc_send_ext_csd(card, ext_csd);
183 if (err != MMC_ERR_NONE) {
184 /*
185 * High capacity cards should have this "magic" size
186 * stored in their CSD.
187 */
188 if (card->csd.capacity == (4096 * 512)) {
189 printk(KERN_ERR "%s: unable to read EXT_CSD "
190 "on a possible high capacity card. "
191 "Card will be ignored.\n",
192 mmc_hostname(card->host));
193 } else {
194 printk(KERN_WARNING "%s: unable to read "
195 "EXT_CSD, performance might "
196 "suffer.\n",
197 mmc_hostname(card->host));
198 err = MMC_ERR_NONE;
199 }
200 goto out;
201 }
202
203 card->ext_csd.sectors =
204 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
205 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
206 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
207 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
208 if (card->ext_csd.sectors)
209 mmc_card_set_blockaddr(card);
210
211 switch (ext_csd[EXT_CSD_CARD_TYPE]) {
212 case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
213 card->ext_csd.hs_max_dtr = 52000000;
214 break;
215 case EXT_CSD_CARD_TYPE_26:
216 card->ext_csd.hs_max_dtr = 26000000;
217 break;
218 default:
219 /* MMC v4 spec says this cannot happen */
220 printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
221 "support any high-speed modes.\n",
222 mmc_hostname(card->host));
223 goto out;
224 }
225
226 if (card->host->caps & MMC_CAP_MMC_HIGHSPEED) {
227 /* Activate highspeed support. */
228 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
229 EXT_CSD_HS_TIMING, 1);
230 if (err != MMC_ERR_NONE) {
231 printk(KERN_WARNING "%s: failed to switch "
232 "card to mmc v4 high-speed mode.\n",
233 mmc_hostname(card->host));
234 err = MMC_ERR_NONE;
235 goto out;
236 }
237
238 mmc_card_set_highspeed(card);
239
240 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
241 }
242
243 /* Check for host support for wide-bus modes. */
244 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
245 /* Activate 4-bit support. */
246 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
247 EXT_CSD_BUS_WIDTH, EXT_CSD_BUS_WIDTH_4);
248 if (err != MMC_ERR_NONE) {
249 printk(KERN_WARNING "%s: failed to switch "
250 "card to mmc v4 4-bit bus mode.\n",
251 mmc_hostname(card->host));
252 err = MMC_ERR_NONE;
253 goto out;
254 }
255
256 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
257 }
258
259out:
260 kfree(ext_csd);
261
262 return err;
263}
264
265/*
266 * Host is being removed. Free up the current card.
267 */
268static void mmc_remove(struct mmc_host *host)
269{
270 BUG_ON(!host);
271 BUG_ON(!host->card);
272
273 mmc_remove_card(host->card);
274 host->card = NULL;
275}
276
277/*
278 * Card detection callback from host.
279 */
280static void mmc_detect(struct mmc_host *host)
281{
282 int err;
283
284 BUG_ON(!host);
285 BUG_ON(!host->card);
286
287 mmc_claim_host(host);
288
289 /*
290 * Just check if our card has been removed.
291 */
292 err = mmc_send_status(host->card, NULL);
293
294 mmc_release_host(host);
295
296 if (err != MMC_ERR_NONE) {
297 mmc_remove_card(host->card);
298 host->card = NULL;
299
300 mmc_claim_host(host);
301 mmc_detach_bus(host);
302 mmc_release_host(host);
303 }
304}
305
306static const struct mmc_bus_ops mmc_ops = {
307 .remove = mmc_remove,
308 .detect = mmc_detect,
309};
310
311/*
312 * Starting point for MMC card init.
313 */
314int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
315{
316 struct mmc_card *card;
317 int err;
318 u32 cid[4];
319 unsigned int max_dtr;
320
321 BUG_ON(!host);
322 BUG_ON(!host->claimed);
323
324 mmc_attach_bus(host, &mmc_ops);
325
326 host->ocr = mmc_select_voltage(host, ocr);
327
328 /*
329 * Can we support the voltage of the card?
330 */
331 if (!host->ocr)
332 goto err;
333
334 /*
335 * Since we're changing the OCR value, we seem to
336 * need to tell some cards to go back to the idle
337 * state. We wait 1ms to give cards time to
338 * respond.
339 */
340 mmc_go_idle(host);
341
342 /* The extra bit indicates that we support high capacity */
343 mmc_send_op_cond(host, host->ocr | (1 << 30), NULL);
344
345 /*
346 * Fetch CID from card.
347 */
348 err = mmc_all_send_cid(host, cid);
349 if (err != MMC_ERR_NONE)
350 goto err;
351
352 /*
353 * Allocate card structure.
354 */
355 card = mmc_alloc_card(host);
356 if (IS_ERR(card))
357 goto err;
358
359 card->type = MMC_TYPE_MMC;
360 card->rca = 1;
361 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
362
363 /*
364 * Set card RCA.
365 */
366 err = mmc_set_relative_addr(card);
367 if (err != MMC_ERR_NONE)
368 goto free_card;
369
370 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
371
372 /*
373 * Fetch CSD from card.
374 */
375 err = mmc_send_csd(card, card->raw_csd);
376 if (err != MMC_ERR_NONE)
377 goto free_card;
378
379 mmc_decode_csd(card);
380 mmc_decode_cid(card);
381
382 /*
383 * Fetch and process extened CSD.
384 * This will switch into high-speed and wide bus modes,
385 * as available.
386 */
387 err = mmc_select_card(card);
388 if (err != MMC_ERR_NONE)
389 goto free_card;
390
391 err = mmc_process_ext_csd(card);
392 if (err != MMC_ERR_NONE)
393 goto free_card;
394
395 /*
396 * Compute bus speed.
397 */
398 max_dtr = (unsigned int)-1;
399
400 if (mmc_card_highspeed(card)) {
401 if (max_dtr > card->ext_csd.hs_max_dtr)
402 max_dtr = card->ext_csd.hs_max_dtr;
403 } else if (max_dtr > card->csd.max_dtr) {
404 max_dtr = card->csd.max_dtr;
405 }
406
407 mmc_set_clock(host, max_dtr);
408
409 host->card = card;
410
411 mmc_release_host(host);
412
413 err = mmc_register_card(card);
414 if (err)
415 goto reclaim_host;
416
417 return 0;
418
419reclaim_host:
420 mmc_claim_host(host);
421free_card:
422 mmc_remove_card(card);
423 host->card = NULL;
424err:
425 mmc_detach_bus(host);
426 mmc_release_host(host);
427
428 return 0;
429}
430
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
new file mode 100644
index 000000000000..6c6beb48f3a8
--- /dev/null
+++ b/drivers/mmc/core/sd.c
@@ -0,0 +1,431 @@
1/*
2 * linux/drivers/mmc/sd.c
3 *
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.
7 *
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.
11 */
12
13#include <linux/err.h>
14
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
17#include <linux/mmc/mmc.h>
18
19#include "core.h"
20#include "sysfs.h"
21#include "mmc_ops.h"
22#include "sd_ops.h"
23
24#include "core.h"
25
26static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
28 0, 0, 0, 0
29};
30
31static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
34};
35
36static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
38};
39
40static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
43};
44
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; \
52 \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
56 __res & __mask; \
57 })
58
59/*
60 * Given the decoded CSD structure, decode the raw CID to our CID structure.
61 */
62static void mmc_decode_cid(struct mmc_card *card)
63{
64 u32 *resp = card->raw_cid;
65
66 memset(&card->cid, 0, sizeof(struct mmc_cid));
67
68 /*
69 * SD doesn't currently have a version field so we will
70 * have to assume we can parse this.
71 */
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);
84
85 card->cid.year += 2000; /* SD cards year offset */
86}
87
88/*
89 * Given a 128-bit response, decode to our card CSD structure.
90 */
91static void mmc_decode_csd(struct mmc_card *card)
92{
93 struct mmc_csd *csd = &card->csd;
94 unsigned int e, m, csd_struct;
95 u32 *resp = card->raw_csd;
96
97 csd_struct = UNSTUFF_BITS(resp, 126, 2);
98
99 switch (csd_struct) {
100 case 0:
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;
105
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);
110
111 e = UNSTUFF_BITS(resp, 47, 3);
112 m = UNSTUFF_BITS(resp, 62, 12);
113 csd->capacity = (1 + m) << (e + 2);
114
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);
122 break;
123 case 1:
124 /*
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.
129 */
130 mmc_card_set_blockaddr(card);
131
132 csd->tacc_ns = 0; /* Unused */
133 csd->tacc_clks = 0; /* Unused */
134
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);
139
140 m = UNSTUFF_BITS(resp, 48, 22);
141 csd->capacity = (1 + m) << 10;
142
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;
150 break;
151 default:
152 printk("%s: unrecognised CSD structure version %d\n",
153 mmc_hostname(card->host), csd_struct);
154 mmc_card_set_bad(card);
155 return;
156 }
157}
158
159/*
160 * Given a 64-bit response, decode to our card SCR structure.
161 */
162static void mmc_decode_scr(struct mmc_card *card)
163{
164 struct sd_scr *scr = &card->scr;
165 unsigned int scr_struct;
166 u32 resp[4];
167
168 BUG_ON(!mmc_card_sd(card));
169
170 resp[3] = card->raw_scr[1];
171 resp[2] = card->raw_scr[0];
172
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);
178 return;
179 }
180
181 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
182 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
183}
184
185/*
186 * Test if the card supports high-speed mode and, if so, switch to it.
187 */
188static int mmc_switch_hs(struct mmc_card *card)
189{
190 int err;
191 u8 *status;
192
193 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
194 return MMC_ERR_NONE;
195
196 err = MMC_ERR_FAILED;
197
198 status = kmalloc(64, GFP_KERNEL);
199 if (!status) {
200 printk("%s: could not allocate a buffer for switch "
201 "capabilities.\n",
202 mmc_hostname(card->host));
203 return err;
204 }
205
206 err = mmc_sd_switch(card, 0, 0, 1, status);
207 if (err != MMC_ERR_NONE) {
208 /*
209 * Card not supporting high-speed will ignore the
210 * command.
211 */
212 if (err == MMC_ERR_TIMEOUT)
213 err = MMC_ERR_NONE;
214 goto out;
215 }
216
217 if (status[13] & 0x02)
218 card->sw_caps.hs_max_dtr = 50000000;
219
220 err = mmc_sd_switch(card, 1, 0, 1, status);
221 if (err != MMC_ERR_NONE)
222 goto out;
223
224 if ((status[16] & 0xF) != 1) {
225 printk(KERN_WARNING "%s: Problem switching card "
226 "into high-speed mode!\n",
227 mmc_hostname(card->host));
228 } else {
229 mmc_card_set_highspeed(card);
230 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
231 }
232
233out:
234 kfree(status);
235
236 return err;
237}
238
239/*
240 * Host is being removed. Free up the current card.
241 */
242static void mmc_sd_remove(struct mmc_host *host)
243{
244 BUG_ON(!host);
245 BUG_ON(!host->card);
246
247 mmc_remove_card(host->card);
248 host->card = NULL;
249}
250
251/*
252 * Card detection callback from host.
253 */
254static void mmc_sd_detect(struct mmc_host *host)
255{
256 int err;
257
258 BUG_ON(!host);
259 BUG_ON(!host->card);
260
261 mmc_claim_host(host);
262
263 /*
264 * Just check if our card has been removed.
265 */
266 err = mmc_send_status(host->card, NULL);
267
268 mmc_release_host(host);
269
270 if (err != MMC_ERR_NONE) {
271 mmc_remove_card(host->card);
272 host->card = NULL;
273
274 mmc_claim_host(host);
275 mmc_detach_bus(host);
276 mmc_release_host(host);
277 }
278}
279
280static const struct mmc_bus_ops mmc_sd_ops = {
281 .remove = mmc_sd_remove,
282 .detect = mmc_sd_detect,
283};
284
285/*
286 * Starting point for SD card init.
287 */
288int mmc_attach_sd(struct mmc_host *host, u32 ocr)
289{
290 struct mmc_card *card;
291 int err;
292 u32 cid[4];
293 unsigned int max_dtr;
294
295 BUG_ON(!host);
296 BUG_ON(!host->claimed);
297
298 mmc_attach_bus(host, &mmc_sd_ops);
299
300 host->ocr = mmc_select_voltage(host, ocr);
301
302 /*
303 * Can we support the voltage(s) of the card(s)?
304 */
305 if (!host->ocr)
306 goto err;
307
308 /*
309 * Since we're changing the OCR value, we seem to
310 * need to tell some cards to go back to the idle
311 * state. We wait 1ms to give cards time to
312 * respond.
313 */
314 mmc_go_idle(host);
315
316 /*
317 * If SD_SEND_IF_COND indicates an SD 2.0
318 * compliant card and we should set bit 30
319 * of the ocr to indicate that we can handle
320 * block-addressed SDHC cards.
321 */
322 err = mmc_send_if_cond(host, host->ocr);
323 if (err == MMC_ERR_NONE)
324 ocr = host->ocr | (1 << 30);
325
326 mmc_send_app_op_cond(host, ocr, NULL);
327
328 /*
329 * Fetch CID from card.
330 */
331 err = mmc_all_send_cid(host, cid);
332 if (err != MMC_ERR_NONE)
333 goto err;
334
335 /*
336 * Allocate card structure.
337 */
338 card = mmc_alloc_card(host);
339 if (IS_ERR(card))
340 goto err;
341
342 card->type = MMC_TYPE_SD;
343 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
344
345 /*
346 * Set card RCA.
347 */
348 err = mmc_send_relative_addr(host, &card->rca);
349 if (err != MMC_ERR_NONE)
350 goto free_card;
351
352 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
353
354 /*
355 * Fetch CSD from card.
356 */
357 err = mmc_send_csd(card, card->raw_csd);
358 if (err != MMC_ERR_NONE)
359 goto free_card;
360
361 mmc_decode_csd(card);
362 mmc_decode_cid(card);
363
364 /*
365 * Fetch SCR from card.
366 */
367 err = mmc_select_card(card);
368 if (err != MMC_ERR_NONE)
369 goto free_card;
370
371 err = mmc_app_send_scr(card, card->raw_scr);
372 if (err != MMC_ERR_NONE)
373 goto free_card;
374
375 mmc_decode_scr(card);
376
377 /*
378 * Check if card can be switched into high-speed mode.
379 */
380 err = mmc_switch_hs(card);
381 if (err != MMC_ERR_NONE)
382 goto free_card;
383
384 /*
385 * Compute bus speed.
386 */
387 max_dtr = (unsigned int)-1;
388
389 if (mmc_card_highspeed(card)) {
390 if (max_dtr > card->sw_caps.hs_max_dtr)
391 max_dtr = card->sw_caps.hs_max_dtr;
392 } else if (max_dtr > card->csd.max_dtr) {
393 max_dtr = card->csd.max_dtr;
394 }
395
396 mmc_set_clock(host, max_dtr);
397
398 /*
399 * Switch to wider bus (if supported).
400 */
401 if ((host->caps && MMC_CAP_4_BIT_DATA) &&
402 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
403 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
404 if (err != MMC_ERR_NONE)
405 goto free_card;
406
407 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
408 }
409
410 host->card = card;
411
412 mmc_release_host(host);
413
414 err = mmc_register_card(card);
415 if (err)
416 goto reclaim_host;
417
418 return 0;
419
420reclaim_host:
421 mmc_claim_host(host);
422free_card:
423 mmc_remove_card(card);
424 host->card = NULL;
425err:
426 mmc_detach_bus(host);
427 mmc_release_host(host);
428
429 return 0;
430}
431