~ubuntu-branches/debian/wheezy/linux-2.6/wheezy

« back to all changes in this revision

Viewing changes to drivers/mmc/core/mmc.c

  • Committer: Bazaar Package Importer
  • Author(s): Ben Hutchings, Ben Hutchings, Aurelien Jarno, Martin Michlmayr
  • Date: 2011-04-06 13:53:30 UTC
  • mfrom: (43.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110406135330-wjufxhd0tvn3zx4z
Tags: 2.6.38-3
[ Ben Hutchings ]
* [ppc64] Add to linux-tools package architectures (Closes: #620124)
* [amd64] Save cr4 to mmu_cr4_features at boot time (Closes: #620284)
* appletalk: Fix bugs introduced when removing use of BKL
* ALSA: Fix yet another race in disconnection
* cciss: Fix lost command issue
* ath9k: Fix kernel panic in AR2427
* ses: Avoid kernel panic when lun 0 is not mapped
* PCI/ACPI: Report ASPM support to BIOS if not disabled from command line

[ Aurelien Jarno ]
* rtlwifi: fix build when PCI is not enabled.

[ Martin Michlmayr ]
* rtlwifi: Eliminate udelay calls with too large values (Closes: #620204)

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
 */
12
12
 
13
13
#include <linux/err.h>
 
14
#include <linux/slab.h>
14
15
 
15
16
#include <linux/mmc/host.h>
16
17
#include <linux/mmc/card.h>
107
108
        return 0;
108
109
}
109
110
 
 
111
static void mmc_set_erase_size(struct mmc_card *card)
 
112
{
 
113
        if (card->ext_csd.erase_group_def & 1)
 
114
                card->erase_size = card->ext_csd.hc_erase_size;
 
115
        else
 
116
                card->erase_size = card->csd.erase_size;
 
117
 
 
118
        mmc_init_erase(card);
 
119
}
 
120
 
110
121
/*
111
122
 * Given a 128-bit response, decode to our card CSD structure.
112
123
 */
113
124
static int mmc_decode_csd(struct mmc_card *card)
114
125
{
115
126
        struct mmc_csd *csd = &card->csd;
116
 
        unsigned int e, m, csd_struct;
 
127
        unsigned int e, m, a, b;
117
128
        u32 *resp = card->raw_csd;
118
129
 
119
130
        /*
120
131
         * We only understand CSD structure v1.1 and v1.2.
121
132
         * v1.2 has extra information in bits 15, 11 and 10.
 
133
         * We also support eMMC v4.4 & v4.41.
122
134
         */
123
 
        csd_struct = UNSTUFF_BITS(resp, 126, 2);
124
 
        if (csd_struct != 1 && csd_struct != 2) {
 
135
        csd->structure = UNSTUFF_BITS(resp, 126, 2);
 
136
        if (csd->structure == 0) {
125
137
                printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
126
 
                        mmc_hostname(card->host), csd_struct);
 
138
                        mmc_hostname(card->host), csd->structure);
127
139
                return -EINVAL;
128
140
        }
129
141
 
150
162
        csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
151
163
        csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
152
164
 
 
165
        if (csd->write_blkbits >= 9) {
 
166
                a = UNSTUFF_BITS(resp, 42, 5);
 
167
                b = UNSTUFF_BITS(resp, 37, 5);
 
168
                csd->erase_size = (a + 1) * (b + 1);
 
169
                csd->erase_size <<= csd->write_blkbits - 9;
 
170
        }
 
171
 
153
172
        return 0;
154
173
}
155
174
 
206
225
                goto out;
207
226
        }
208
227
 
 
228
        /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
 
229
        if (card->csd.structure == 3) {
 
230
                int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE];
 
231
                if (ext_csd_struct > 2) {
 
232
                        printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
 
233
                                "version %d\n", mmc_hostname(card->host),
 
234
                                        ext_csd_struct);
 
235
                        err = -EINVAL;
 
236
                        goto out;
 
237
                }
 
238
        }
 
239
 
209
240
        card->ext_csd.rev = ext_csd[EXT_CSD_REV];
210
 
        if (card->ext_csd.rev > 3) {
211
 
                printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
212
 
                        "version %d\n", mmc_hostname(card->host),
213
 
                        card->ext_csd.rev);
 
241
        if (card->ext_csd.rev > 5) {
 
242
                printk(KERN_ERR "%s: unrecognised EXT_CSD revision %d\n",
 
243
                        mmc_hostname(card->host), card->ext_csd.rev);
214
244
                err = -EINVAL;
215
245
                goto out;
216
246
        }
221
251
                        ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
222
252
                        ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
223
253
                        ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
224
 
                if (card->ext_csd.sectors)
 
254
 
 
255
                /* Cards with density > 2GiB are sector addressed */
 
256
                if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
225
257
                        mmc_card_set_blockaddr(card);
226
258
        }
227
259
 
228
 
        switch (ext_csd[EXT_CSD_CARD_TYPE]) {
 
260
        switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
 
261
        case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
 
262
             EXT_CSD_CARD_TYPE_26:
 
263
                card->ext_csd.hs_max_dtr = 52000000;
 
264
                card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_52;
 
265
                break;
 
266
        case EXT_CSD_CARD_TYPE_DDR_1_2V | EXT_CSD_CARD_TYPE_52 |
 
267
             EXT_CSD_CARD_TYPE_26:
 
268
                card->ext_csd.hs_max_dtr = 52000000;
 
269
                card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_2V;
 
270
                break;
 
271
        case EXT_CSD_CARD_TYPE_DDR_1_8V | EXT_CSD_CARD_TYPE_52 |
 
272
             EXT_CSD_CARD_TYPE_26:
 
273
                card->ext_csd.hs_max_dtr = 52000000;
 
274
                card->ext_csd.card_type = EXT_CSD_CARD_TYPE_DDR_1_8V;
 
275
                break;
229
276
        case EXT_CSD_CARD_TYPE_52 | EXT_CSD_CARD_TYPE_26:
230
277
                card->ext_csd.hs_max_dtr = 52000000;
231
278
                break;
237
284
                printk(KERN_WARNING "%s: card is mmc v4 but doesn't "
238
285
                        "support any high-speed modes.\n",
239
286
                        mmc_hostname(card->host));
240
 
                goto out;
241
287
        }
242
288
 
243
289
        if (card->ext_csd.rev >= 3) {
247
293
                if (sa_shift > 0 && sa_shift <= 0x17)
248
294
                        card->ext_csd.sa_timeout =
249
295
                                        1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
250
 
        }
 
296
                card->ext_csd.erase_group_def =
 
297
                        ext_csd[EXT_CSD_ERASE_GROUP_DEF];
 
298
                card->ext_csd.hc_erase_timeout = 300 *
 
299
                        ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
 
300
                card->ext_csd.hc_erase_size =
 
301
                        ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
 
302
        }
 
303
 
 
304
        if (card->ext_csd.rev >= 4) {
 
305
                card->ext_csd.sec_trim_mult =
 
306
                        ext_csd[EXT_CSD_SEC_TRIM_MULT];
 
307
                card->ext_csd.sec_erase_mult =
 
308
                        ext_csd[EXT_CSD_SEC_ERASE_MULT];
 
309
                card->ext_csd.sec_feature_support =
 
310
                        ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
 
311
                card->ext_csd.trim_timeout = 300 *
 
312
                        ext_csd[EXT_CSD_TRIM_MULT];
 
313
        }
 
314
 
 
315
        if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
 
316
                card->erased_byte = 0xFF;
 
317
        else
 
318
                card->erased_byte = 0x0;
251
319
 
252
320
out:
253
321
        kfree(ext_csd);
260
328
MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
261
329
        card->raw_csd[2], card->raw_csd[3]);
262
330
MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
 
331
MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
 
332
MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
263
333
MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
264
334
MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
265
335
MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
271
341
        &dev_attr_cid.attr,
272
342
        &dev_attr_csd.attr,
273
343
        &dev_attr_date.attr,
 
344
        &dev_attr_erase_size.attr,
 
345
        &dev_attr_preferred_erase_size.attr,
274
346
        &dev_attr_fwrev.attr,
275
347
        &dev_attr_hwrev.attr,
276
348
        &dev_attr_manfid.attr,
303
375
        struct mmc_card *oldcard)
304
376
{
305
377
        struct mmc_card *card;
306
 
        int err;
 
378
        int err, ddr = 0;
307
379
        u32 cid[4];
308
380
        unsigned int max_dtr;
309
381
 
407
479
                err = mmc_read_ext_csd(card);
408
480
                if (err)
409
481
                        goto free_card;
 
482
                /* Erase size depends on CSD and Extended CSD */
 
483
                mmc_set_erase_size(card);
410
484
        }
411
485
 
412
486
        /*
444
518
        mmc_set_clock(host, max_dtr);
445
519
 
446
520
        /*
447
 
         * Activate wide bus (if supported).
 
521
         * Indicate DDR mode (if supported).
 
522
         */
 
523
        if (mmc_card_highspeed(card)) {
 
524
                if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
 
525
                        && (host->caps & (MMC_CAP_1_8V_DDR)))
 
526
                                ddr = MMC_1_8V_DDR_MODE;
 
527
                else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
 
528
                        && (host->caps & (MMC_CAP_1_2V_DDR)))
 
529
                                ddr = MMC_1_2V_DDR_MODE;
 
530
        }
 
531
 
 
532
        /*
 
533
         * Activate wide bus and DDR (if supported).
448
534
         */
449
535
        if ((card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
450
536
            (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
451
 
                unsigned ext_csd_bit, bus_width;
452
 
 
453
 
                if (host->caps & MMC_CAP_8_BIT_DATA) {
454
 
                        ext_csd_bit = EXT_CSD_BUS_WIDTH_8;
455
 
                        bus_width = MMC_BUS_WIDTH_8;
456
 
                } else {
457
 
                        ext_csd_bit = EXT_CSD_BUS_WIDTH_4;
458
 
                        bus_width = MMC_BUS_WIDTH_4;
459
 
                }
460
 
 
461
 
                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
462
 
                                 EXT_CSD_BUS_WIDTH, ext_csd_bit);
463
 
 
464
 
                if (err && err != -EBADMSG)
 
537
                static unsigned ext_csd_bits[][2] = {
 
538
                        { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
 
539
                        { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
 
540
                        { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
 
541
                };
 
542
                static unsigned bus_widths[] = {
 
543
                        MMC_BUS_WIDTH_8,
 
544
                        MMC_BUS_WIDTH_4,
 
545
                        MMC_BUS_WIDTH_1
 
546
                };
 
547
                unsigned idx, bus_width = 0;
 
548
 
 
549
                if (host->caps & MMC_CAP_8_BIT_DATA)
 
550
                        idx = 0;
 
551
                else
 
552
                        idx = 1;
 
553
                for (; idx < ARRAY_SIZE(bus_widths); idx++) {
 
554
                        bus_width = bus_widths[idx];
 
555
                        if (bus_width == MMC_BUS_WIDTH_1)
 
556
                                ddr = 0; /* no DDR for 1-bit width */
 
557
                        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
 
558
                                         EXT_CSD_BUS_WIDTH,
 
559
                                         ext_csd_bits[idx][0]);
 
560
                        if (!err) {
 
561
                                mmc_set_bus_width_ddr(card->host,
 
562
                                                      bus_width, MMC_SDR_MODE);
 
563
                                /*
 
564
                                 * If controller can't handle bus width test,
 
565
                                 * use the highest bus width to maintain
 
566
                                 * compatibility with previous MMC behavior.
 
567
                                 */
 
568
                                if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
 
569
                                        break;
 
570
                                err = mmc_bus_test(card, bus_width);
 
571
                                if (!err)
 
572
                                        break;
 
573
                        }
 
574
                }
 
575
 
 
576
                if (!err && ddr) {
 
577
                        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
 
578
                                        EXT_CSD_BUS_WIDTH,
 
579
                                        ext_csd_bits[idx][1]);
 
580
                }
 
581
                if (err) {
 
582
                        printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
 
583
                                "failed\n", mmc_hostname(card->host),
 
584
                                1 << bus_width, ddr);
465
585
                        goto free_card;
466
 
 
467
 
                if (err) {
468
 
                        printk(KERN_WARNING "%s: switch to bus width %d "
469
 
                               "failed\n", mmc_hostname(card->host),
470
 
                               1 << bus_width);
471
 
                        err = 0;
472
 
                } else {
473
 
                        mmc_set_bus_width(card->host, bus_width);
 
586
                } else if (ddr) {
 
587
                        mmc_card_set_ddr_mode(card);
 
588
                        mmc_set_bus_width_ddr(card->host, bus_width, ddr);
474
589
                }
475
590
        }
476
591
 
564
679
        return err;
565
680
}
566
681
 
567
 
static void mmc_power_restore(struct mmc_host *host)
 
682
static int mmc_power_restore(struct mmc_host *host)
568
683
{
 
684
        int ret;
 
685
 
569
686
        host->card->state &= ~MMC_STATE_HIGHSPEED;
570
687
        mmc_claim_host(host);
571
 
        mmc_init_card(host, host->ocr, host->card);
 
688
        ret = mmc_init_card(host, host->ocr, host->card);
572
689
        mmc_release_host(host);
 
690
 
 
691
        return ret;
573
692
}
574
693
 
575
694
static int mmc_sleep(struct mmc_host *host)
602
721
        return err;
603
722
}
604
723
 
605
 
#ifdef CONFIG_MMC_UNSAFE_RESUME
606
 
 
607
 
static const struct mmc_bus_ops mmc_ops = {
608
 
        .awake = mmc_awake,
609
 
        .sleep = mmc_sleep,
610
 
        .remove = mmc_remove,
611
 
        .detect = mmc_detect,
612
 
        .suspend = mmc_suspend,
613
 
        .resume = mmc_resume,
614
 
        .power_restore = mmc_power_restore,
615
 
};
616
 
 
617
 
static void mmc_attach_bus_ops(struct mmc_host *host)
618
 
{
619
 
        mmc_attach_bus(host, &mmc_ops);
620
 
}
621
 
 
622
 
#else
623
 
 
624
724
static const struct mmc_bus_ops mmc_ops = {
625
725
        .awake = mmc_awake,
626
726
        .sleep = mmc_sleep,
645
745
{
646
746
        const struct mmc_bus_ops *bus_ops;
647
747
 
648
 
        if (host->caps & MMC_CAP_NONREMOVABLE)
 
748
        if (!mmc_card_is_removable(host))
649
749
                bus_ops = &mmc_ops_unsafe;
650
750
        else
651
751
                bus_ops = &mmc_ops;
652
752
        mmc_attach_bus(host, bus_ops);
653
753
}
654
754
 
655
 
#endif
656
 
 
657
755
/*
658
756
 * Starting point for MMC card init.
659
757
 */
660
 
int mmc_attach_mmc(struct mmc_host *host, u32 ocr)
 
758
int mmc_attach_mmc(struct mmc_host *host)
661
759
{
662
760
        int err;
 
761
        u32 ocr;
663
762
 
664
763
        BUG_ON(!host);
665
764
        WARN_ON(!host->claimed);
666
765
 
 
766
        err = mmc_send_op_cond(host, 0, &ocr);
 
767
        if (err)
 
768
                return err;
 
769
 
667
770
        mmc_attach_bus_ops(host);
 
771
        if (host->ocr_avail_mmc)
 
772
                host->ocr_avail = host->ocr_avail_mmc;
668
773
 
669
774
        /*
670
775
         * We need to get OCR a different way for SPI.
704
809
                goto err;
705
810
 
706
811
        mmc_release_host(host);
707
 
 
708
812
        err = mmc_add_card(host->card);
 
813
        mmc_claim_host(host);
709
814
        if (err)
710
815
                goto remove_card;
711
816
 
712
817
        return 0;
713
818
 
714
819
remove_card:
 
820
        mmc_release_host(host);
715
821
        mmc_remove_card(host->card);
 
822
        mmc_claim_host(host);
716
823
        host->card = NULL;
717
 
        mmc_claim_host(host);
718
824
err:
719
825
        mmc_detach_bus(host);
720
 
        mmc_release_host(host);
721
826
 
722
827
        printk(KERN_ERR "%s: error %d whilst initialising MMC card\n",
723
828
                mmc_hostname(host), err);