~ubuntu-branches/ubuntu/precise/linux-ti-omap4/precise

« back to all changes in this revision

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

  • Committer: Bazaar Package Importer
  • Author(s): Paolo Pisati
  • Date: 2011-06-29 15:23:51 UTC
  • mfrom: (26.1.1 natty-proposed)
  • Revision ID: james.westby@ubuntu.com-20110629152351-xs96tm303d95rpbk
Tags: 3.0.0-1200.2
* Rebased against 3.0.0-6.7
* BSP from TI based on 3.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
#include "core.h"
21
21
#include "bus.h"
22
22
#include "mmc_ops.h"
 
23
#include "sd_ops.h"
23
24
 
24
25
static const unsigned int tran_exp[] = {
25
26
        10000,          100000,         1000000,        10000000,
173
174
}
174
175
 
175
176
/*
176
 
 * Read and decode extended CSD.
 
177
 * Read extended CSD.
177
178
 */
178
 
static int mmc_read_ext_csd(struct mmc_card *card)
 
179
static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
179
180
{
180
181
        int err;
181
182
        u8 *ext_csd;
182
183
 
183
184
        BUG_ON(!card);
 
185
        BUG_ON(!new_ext_csd);
 
186
 
 
187
        *new_ext_csd = NULL;
184
188
 
185
189
        if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
186
190
                return 0;
198
202
 
199
203
        err = mmc_send_ext_csd(card, ext_csd);
200
204
        if (err) {
 
205
                kfree(ext_csd);
 
206
                *new_ext_csd = NULL;
 
207
 
201
208
                /* If the host or the card can't do the switch,
202
209
                 * fail more gracefully. */
203
210
                if ((err != -EINVAL)
204
211
                 && (err != -ENOSYS)
205
212
                 && (err != -EFAULT))
206
 
                        goto out;
 
213
                        return err;
207
214
 
208
215
                /*
209
216
                 * High capacity cards should have this "magic" size
221
228
                                mmc_hostname(card->host));
222
229
                        err = 0;
223
230
                }
224
 
 
225
 
                goto out;
226
 
        }
 
231
        } else
 
232
                *new_ext_csd = ext_csd;
 
233
 
 
234
        return err;
 
235
}
 
236
 
 
237
/*
 
238
 * Decode extended CSD.
 
239
 */
 
240
static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
 
241
{
 
242
        int err = 0;
 
243
 
 
244
        BUG_ON(!card);
 
245
 
 
246
        if (!ext_csd)
 
247
                return 0;
227
248
 
228
249
        /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
 
250
        card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
229
251
        if (card->csd.structure == 3) {
230
 
                int ext_csd_struct = ext_csd[EXT_CSD_STRUCTURE];
231
 
                if (ext_csd_struct > 2) {
 
252
                if (card->ext_csd.raw_ext_csd_structure > 2) {
232
253
                        printk(KERN_ERR "%s: unrecognised EXT_CSD structure "
233
254
                                "version %d\n", mmc_hostname(card->host),
234
 
                                        ext_csd_struct);
 
255
                                        card->ext_csd.raw_ext_csd_structure);
235
256
                        err = -EINVAL;
236
257
                        goto out;
237
258
                }
245
266
                goto out;
246
267
        }
247
268
 
 
269
        card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
 
270
        card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
 
271
        card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
 
272
        card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
248
273
        if (card->ext_csd.rev >= 2) {
249
274
                card->ext_csd.sectors =
250
275
                        ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
256
281
                if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
257
282
                        mmc_card_set_blockaddr(card);
258
283
        }
259
 
 
 
284
        card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
260
285
        switch (ext_csd[EXT_CSD_CARD_TYPE] & EXT_CSD_CARD_TYPE_MASK) {
261
286
        case EXT_CSD_CARD_TYPE_DDR_52 | EXT_CSD_CARD_TYPE_52 |
262
287
             EXT_CSD_CARD_TYPE_26:
286
311
                        mmc_hostname(card->host));
287
312
        }
288
313
 
 
314
        card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
 
315
        card->ext_csd.raw_erase_timeout_mult =
 
316
                ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
 
317
        card->ext_csd.raw_hc_erase_grp_size =
 
318
                ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
289
319
        if (card->ext_csd.rev >= 3) {
290
320
                u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
 
321
                card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
 
322
 
 
323
                /* EXT_CSD value is in units of 10ms, but we store in ms */
 
324
                card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
291
325
 
292
326
                /* Sleep / awake timeout in 100ns units */
293
327
                if (sa_shift > 0 && sa_shift <= 0x17)
299
333
                        ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
300
334
                card->ext_csd.hc_erase_size =
301
335
                        ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
 
336
 
 
337
                card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
 
338
 
 
339
                /*
 
340
                 * There are two boot regions of equal size, defined in
 
341
                 * multiples of 128K.
 
342
                 */
 
343
                card->ext_csd.boot_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
302
344
        }
303
345
 
 
346
        card->ext_csd.raw_hc_erase_gap_size =
 
347
                ext_csd[EXT_CSD_PARTITION_ATTRIBUTE];
 
348
        card->ext_csd.raw_sec_trim_mult =
 
349
                ext_csd[EXT_CSD_SEC_TRIM_MULT];
 
350
        card->ext_csd.raw_sec_erase_mult =
 
351
                ext_csd[EXT_CSD_SEC_ERASE_MULT];
 
352
        card->ext_csd.raw_sec_feature_support =
 
353
                ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
 
354
        card->ext_csd.raw_trim_mult =
 
355
                ext_csd[EXT_CSD_TRIM_MULT];
304
356
        if (card->ext_csd.rev >= 4) {
305
357
                /*
306
358
                 * Enhanced area feature support -- check whether the eMMC
308
360
                 * area offset and size to user by adding sysfs interface.
309
361
                 */
310
362
                if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
311
 
                                (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
 
363
                    (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
312
364
                        u8 hc_erase_grp_sz =
313
365
                                ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
314
366
                        u8 hc_wp_grp_sz =
350
402
                        ext_csd[EXT_CSD_TRIM_MULT];
351
403
        }
352
404
 
 
405
        if (card->ext_csd.rev >= 5)
 
406
                card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
 
407
 
353
408
        if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
354
409
                card->erased_byte = 0xFF;
355
410
        else
356
411
                card->erased_byte = 0x0;
357
412
 
358
413
out:
 
414
        return err;
 
415
}
 
416
 
 
417
static inline void mmc_free_ext_csd(u8 *ext_csd)
 
418
{
359
419
        kfree(ext_csd);
360
 
 
 
420
}
 
421
 
 
422
 
 
423
static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
 
424
{
 
425
        u8 *bw_ext_csd;
 
426
        int err;
 
427
 
 
428
        if (bus_width == MMC_BUS_WIDTH_1)
 
429
                return 0;
 
430
 
 
431
        err = mmc_get_ext_csd(card, &bw_ext_csd);
 
432
 
 
433
        if (err || bw_ext_csd == NULL) {
 
434
                if (bus_width != MMC_BUS_WIDTH_1)
 
435
                        err = -EINVAL;
 
436
                goto out;
 
437
        }
 
438
 
 
439
        if (bus_width == MMC_BUS_WIDTH_1)
 
440
                goto out;
 
441
 
 
442
        /* only compare read only fields */
 
443
        err = (!(card->ext_csd.raw_partition_support ==
 
444
                        bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
 
445
                (card->ext_csd.raw_erased_mem_count ==
 
446
                        bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
 
447
                (card->ext_csd.rev ==
 
448
                        bw_ext_csd[EXT_CSD_REV]) &&
 
449
                (card->ext_csd.raw_ext_csd_structure ==
 
450
                        bw_ext_csd[EXT_CSD_STRUCTURE]) &&
 
451
                (card->ext_csd.raw_card_type ==
 
452
                        bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
 
453
                (card->ext_csd.raw_s_a_timeout ==
 
454
                        bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
 
455
                (card->ext_csd.raw_hc_erase_gap_size ==
 
456
                        bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
 
457
                (card->ext_csd.raw_erase_timeout_mult ==
 
458
                        bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
 
459
                (card->ext_csd.raw_hc_erase_grp_size ==
 
460
                        bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
 
461
                (card->ext_csd.raw_sec_trim_mult ==
 
462
                        bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
 
463
                (card->ext_csd.raw_sec_erase_mult ==
 
464
                        bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
 
465
                (card->ext_csd.raw_sec_feature_support ==
 
466
                        bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
 
467
                (card->ext_csd.raw_trim_mult ==
 
468
                        bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
 
469
                (card->ext_csd.raw_sectors[0] ==
 
470
                        bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
 
471
                (card->ext_csd.raw_sectors[1] ==
 
472
                        bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
 
473
                (card->ext_csd.raw_sectors[2] ==
 
474
                        bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
 
475
                (card->ext_csd.raw_sectors[3] ==
 
476
                        bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
 
477
        if (err)
 
478
                err = -EINVAL;
 
479
 
 
480
out:
 
481
        mmc_free_ext_csd(bw_ext_csd);
361
482
        return err;
362
483
}
363
484
 
422
543
        u32 cid[4];
423
544
        unsigned int max_dtr;
424
545
        u32 rocr;
 
546
        u8 *ext_csd = NULL;
425
547
 
426
548
        BUG_ON(!host);
427
549
        WARN_ON(!host->claimed);
520
642
                /*
521
643
                 * Fetch and process extended CSD.
522
644
                 */
523
 
                err = mmc_read_ext_csd(card);
 
645
 
 
646
                err = mmc_get_ext_csd(card, &ext_csd);
 
647
                if (err)
 
648
                        goto free_card;
 
649
                err = mmc_read_ext_csd(card, ext_csd);
524
650
                if (err)
525
651
                        goto free_card;
526
652
 
538
664
 
539
665
        /*
540
666
         * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
541
 
         * bit.  This bit will be lost everytime after a reset or power off.
 
667
         * bit.  This bit will be lost every time after a reset or power off.
542
668
         */
543
669
        if (card->ext_csd.enhanced_area_en) {
544
670
                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
545
 
                                EXT_CSD_ERASE_GROUP_DEF, 1);
 
671
                                 EXT_CSD_ERASE_GROUP_DEF, 1, 0);
546
672
 
547
673
                if (err && err != -EBADMSG)
548
674
                        goto free_card;
568
694
        }
569
695
 
570
696
        /*
 
697
         * Ensure eMMC user default partition is enabled
 
698
         */
 
699
        if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
 
700
                card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
 
701
                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
 
702
                                 card->ext_csd.part_config,
 
703
                                 card->ext_csd.part_time);
 
704
                if (err && err != -EBADMSG)
 
705
                        goto free_card;
 
706
        }
 
707
 
 
708
        /*
571
709
         * Activate high speed (if supported)
572
710
         */
573
711
        if ((card->ext_csd.hs_max_dtr != 0) &&
574
712
                (host->caps & MMC_CAP_MMC_HIGHSPEED)) {
575
713
                err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
576
 
                        EXT_CSD_HS_TIMING, 1);
 
714
                                 EXT_CSD_HS_TIMING, 1, 0);
577
715
                if (err && err != -EBADMSG)
578
716
                        goto free_card;
579
717
 
606
744
         */
607
745
        if (mmc_card_highspeed(card)) {
608
746
                if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
609
 
                        && (host->caps & (MMC_CAP_1_8V_DDR)))
 
747
                        && ((host->caps & (MMC_CAP_1_8V_DDR |
 
748
                             MMC_CAP_UHS_DDR50))
 
749
                                == (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
610
750
                                ddr = MMC_1_8V_DDR_MODE;
611
751
                else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
612
 
                        && (host->caps & (MMC_CAP_1_2V_DDR)))
 
752
                        && ((host->caps & (MMC_CAP_1_2V_DDR |
 
753
                             MMC_CAP_UHS_DDR50))
 
754
                                == (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
613
755
                                ddr = MMC_1_2V_DDR_MODE;
614
756
        }
615
757
 
640
782
                                ddr = 0; /* no DDR for 1-bit width */
641
783
                        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
642
784
                                         EXT_CSD_BUS_WIDTH,
643
 
                                         ext_csd_bits[idx][0]);
 
785
                                         ext_csd_bits[idx][0],
 
786
                                         0);
644
787
                        if (!err) {
645
 
                                mmc_set_bus_width_ddr(card->host,
646
 
                                                      bus_width, MMC_SDR_MODE);
 
788
                                mmc_set_bus_width(card->host, bus_width);
 
789
 
647
790
                                /*
648
791
                                 * If controller can't handle bus width test,
649
 
                                 * use the highest bus width to maintain
650
 
                                 * compatibility with previous MMC behavior.
 
792
                                 * compare ext_csd previously read in 1 bit mode
 
793
                                 * against ext_csd at new bus width
651
794
                                 */
652
795
                                if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
653
 
                                        break;
654
 
                                err = mmc_bus_test(card, bus_width);
 
796
                                        err = mmc_compare_ext_csds(card,
 
797
                                                bus_width);
 
798
                                else
 
799
                                        err = mmc_bus_test(card, bus_width);
655
800
                                if (!err)
656
801
                                        break;
657
802
                        }
659
804
 
660
805
                if (!err && ddr) {
661
806
                        err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
662
 
                                        EXT_CSD_BUS_WIDTH,
663
 
                                        ext_csd_bits[idx][1]);
 
807
                                         EXT_CSD_BUS_WIDTH,
 
808
                                         ext_csd_bits[idx][1],
 
809
                                         0);
664
810
                }
665
811
                if (err) {
666
812
                        printk(KERN_WARNING "%s: switch to bus width %d ddr %d "
668
814
                                1 << bus_width, ddr);
669
815
                        goto free_card;
670
816
                } else if (ddr) {
 
817
                        /*
 
818
                         * eMMC cards can support 3.3V to 1.2V i/o (vccq)
 
819
                         * signaling.
 
820
                         *
 
821
                         * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
 
822
                         *
 
823
                         * 1.8V vccq at 3.3V core voltage (vcc) is not required
 
824
                         * in the JEDEC spec for DDR.
 
825
                         *
 
826
                         * Do not force change in vccq since we are obviously
 
827
                         * working and no change to vccq is needed.
 
828
                         *
 
829
                         * WARNING: eMMC rules are NOT the same as SD DDR
 
830
                         */
 
831
                        if (ddr == EXT_CSD_CARD_TYPE_DDR_1_2V) {
 
832
                                err = mmc_set_signal_voltage(host,
 
833
                                        MMC_SIGNAL_VOLTAGE_120, 0);
 
834
                                if (err)
 
835
                                        goto err;
 
836
                        }
671
837
                        mmc_card_set_ddr_mode(card);
672
 
                        mmc_set_bus_width_ddr(card->host, bus_width, ddr);
 
838
                        mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
 
839
                        mmc_set_bus_width(card->host, bus_width);
673
840
                }
674
841
        }
675
842
 
676
843
        if (!oldcard)
677
844
                host->card = card;
678
845
 
 
846
        mmc_free_ext_csd(ext_csd);
679
847
        return 0;
680
848
 
681
849
free_card:
682
850
        if (!oldcard)
683
851
                mmc_remove_card(card);
684
852
err:
 
853
        mmc_free_ext_csd(ext_csd);
685
854
 
686
855
        return err;
687
856
}