14 #include <linux/slab.h>
15 #include <linux/stat.h>
26 static const unsigned int tran_exp[] = {
27 10000, 100000, 1000000, 10000000,
31 static const unsigned char tran_mant[] = {
32 0, 10, 12, 13, 15, 20, 25, 30,
33 35, 40, 45, 50, 55, 60, 70, 80,
36 static const unsigned int tacc_exp[] = {
37 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
40 static const unsigned int tacc_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
45 #define UNSTUFF_BITS(resp,start,size) \
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; \
53 __res = resp[__off] >> __shft; \
54 if (__size + __shft > 32) \
55 __res |= resp[__off-1] << ((32 - __shft) % 32); \
70 switch (card->
csd.mmca_vsn) {
105 pr_err(
"%s: card has unknown MMCA version %d\n",
115 if (card->
ext_csd.erase_group_def & 1)
126 static int mmc_decode_csd(
struct mmc_card *card)
129 unsigned int e,
m,
a,
b;
139 pr_err(
"%s: unrecognised CSD structure version %d\n",
147 csd->
tacc_ns = (tacc_exp[
e] * tacc_mant[
m] + 9) / 10;
152 csd->
max_dtr = tran_exp[
e] * tran_mant[
m];
180 static int mmc_get_ext_csd(
struct mmc_card *card,
u8 **new_ext_csd)
199 pr_err(
"%s: could not allocate a buffer to "
220 if (card->
csd.capacity == (4096 * 512)) {
221 pr_err(
"%s: unable to read EXT_CSD "
222 "on a possible high capacity card. "
223 "Card will be ignored.\n",
227 "EXT_CSD, performance might "
233 *new_ext_csd = ext_csd;
238 static void mmc_select_card_type(
struct mmc_card *card)
243 unsigned int hs_max_dtr = 0;
264 card->
ext_csd.hs_max_dtr = hs_max_dtr;
271 static int mmc_read_ext_csd(
struct mmc_card *card,
u8 *ext_csd)
274 unsigned int part_size;
275 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
284 if (card->
csd.structure == 3) {
285 if (card->
ext_csd.raw_ext_csd_structure > 2) {
286 pr_err(
"%s: unrecognised EXT_CSD structure "
288 card->
ext_csd.raw_ext_csd_structure);
296 pr_err(
"%s: unrecognised EXT_CSD revision %d\n",
314 if (card->
ext_csd.sectors > (2
u * 1024 * 1024 * 1024) / 512)
319 mmc_select_card_type(card);
322 card->
ext_csd.raw_erase_timeout_mult =
324 card->
ext_csd.raw_hc_erase_grp_size =
334 if (sa_shift > 0 && sa_shift <= 0x17)
337 card->
ext_csd.erase_group_def =
339 card->
ext_csd.hc_erase_timeout = 300 *
353 mmc_part_add(card, part_size,
361 card->
ext_csd.raw_hc_erase_gap_size =
363 card->
ext_csd.raw_sec_trim_mult =
365 card->
ext_csd.raw_sec_erase_mult =
367 card->
ext_csd.raw_sec_feature_support =
385 card->
ext_csd.enhanced_area_en = 1;
389 card->
ext_csd.enhanced_area_offset =
390 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
391 (ext_csd[137] << 8) + ext_csd[136];
393 card->
ext_csd.enhanced_area_offset <<= 9;
397 card->
ext_csd.enhanced_area_size =
398 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
400 card->
ext_csd.enhanced_area_size *=
401 (
size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
402 card->
ext_csd.enhanced_area_size <<= 9;
419 if (card->
ext_csd.enhanced_area_en != 1) {
425 card->
ext_csd.enhanced_area_en = 1;
439 part_size *= (
size_t)(hc_erase_grp_sz *
441 mmc_part_add(card, part_size << 19,
451 card->
ext_csd.sec_feature_support =
453 card->
ext_csd.trim_timeout = 300 *
462 card->
ext_csd.boot_ro_lockable =
true;
470 card->
ext_csd.raw_bkops_status =
473 pr_info(
"%s: BKOPS_EN bit is not set\n",
488 card->
ext_csd.out_of_int_time =
506 card->
ext_csd.generic_cmd6_time = 10 *
508 card->
ext_csd.power_off_longtime = 10 *
518 card->
ext_csd.data_sector_size = 4096;
520 card->
ext_csd.data_sector_size = 512;
524 card->
ext_csd.data_tag_unit_size =
526 (card->
ext_csd.data_sector_size);
528 card->
ext_csd.data_tag_unit_size = 0;
531 card->
ext_csd.data_sector_size = 512;
538 static inline void mmc_free_ext_csd(
u8 *ext_csd)
552 err = mmc_get_ext_csd(card, &bw_ext_csd);
554 if (err || bw_ext_csd ==
NULL) {
560 err = !((card->
ext_csd.raw_partition_support ==
562 (card->
ext_csd.raw_erased_mem_count ==
563 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
566 (card->
ext_csd.raw_ext_csd_structure ==
568 (card->
ext_csd.raw_card_type ==
570 (card->
ext_csd.raw_s_a_timeout ==
572 (card->
ext_csd.raw_hc_erase_gap_size ==
574 (card->
ext_csd.raw_erase_timeout_mult ==
576 (card->
ext_csd.raw_hc_erase_grp_size ==
578 (card->
ext_csd.raw_sec_trim_mult ==
580 (card->
ext_csd.raw_sec_erase_mult ==
582 (card->
ext_csd.raw_sec_feature_support ==
584 (card->
ext_csd.raw_trim_mult ==
586 (card->
ext_csd.raw_sectors[0] ==
588 (card->
ext_csd.raw_sectors[1] ==
590 (card->
ext_csd.raw_sectors[2] ==
592 (card->
ext_csd.raw_sectors[3] ==
598 mmc_free_ext_csd(bw_ext_csd);
616 card->
ext_csd.enhanced_area_offset);
619 static struct attribute *mmc_std_attrs[] = {
623 &dev_attr_erase_size.attr,
624 &dev_attr_preferred_erase_size.attr,
625 &dev_attr_fwrev.attr,
626 &dev_attr_hwrev.attr,
627 &dev_attr_manfid.attr,
629 &dev_attr_oemid.attr,
630 &dev_attr_serial.attr,
631 &dev_attr_enhanced_area_offset.attr,
632 &dev_attr_enhanced_area_size.attr,
637 .attrs = mmc_std_attrs,
646 .groups = mmc_attr_groups,
655 static int mmc_select_powerclass(
struct mmc_card *card,
656 unsigned int bus_width,
u8 *ext_csd)
659 unsigned int pwrclass_val;
660 unsigned int index = 0;
679 switch (1 << host->
ios.vdd) {
681 if (host->
ios.clock <= 26000000)
683 else if (host->
ios.clock <= 52000000)
687 else if (host->
ios.clock <= 200000000)
699 if (host->
ios.clock <= 26000000)
701 else if (host->
ios.clock <= 52000000)
705 else if (host->
ios.clock <= 200000000)
714 pwrclass_val = ext_csd[
index];
724 if (pwrclass_val > 0) {
728 card->
ext_csd.generic_cmd6_time);
738 static int mmc_select_hs200(
struct mmc_card *card)
742 static unsigned ext_csd_bits[] = {
746 static unsigned bus_widths[] = {
759 if (err && card->
ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
775 for (; idx >= 0; idx--) {
787 card->
ext_csd.generic_cmd6_time);
794 err = mmc_compare_ext_csds(card, bus_widths[idx]);
821 unsigned int max_dtr;
906 err = mmc_decode_csd(card);
909 err = mmc_decode_cid(card);
928 err = mmc_get_ext_csd(card, &ext_csd);
931 err = mmc_read_ext_csd(card, ext_csd);
944 mmc_set_erase_size(card);
951 if (card->
ext_csd.enhanced_area_en ||
955 card->
ext_csd.generic_cmd6_time);
970 card->
ext_csd.erase_group_def = 1;
976 mmc_set_erase_size(card);
1001 card->
ext_csd.generic_cmd6_time);
1016 if (card->
ext_csd.hs_max_dtr != 0) {
1018 if (card->
ext_csd.hs_max_dtr > 52000000 &&
1020 err = mmc_select_hs200(card);
1024 card->
ext_csd.generic_cmd6_time);
1030 pr_warning(
"%s: switch to highspeed failed\n",
1034 if (card->
ext_csd.hs_max_dtr > 52000000 &&
1049 max_dtr = (
unsigned int)-1;
1052 if (max_dtr > card->
ext_csd.hs_max_dtr)
1053 max_dtr = card->
ext_csd.hs_max_dtr;
1054 }
else if (max_dtr > card->
csd.max_dtr) {
1055 max_dtr = card->
csd.max_dtr;
1064 if ((card->
ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
1069 else if ((card->
ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1081 u32 bus_width = card->
host->ios.bus_width;
1095 card->
host->ops->execute_tuning) {
1096 mmc_host_clk_hold(card->
host);
1097 err = card->
host->ops->execute_tuning(card->
host,
1099 mmc_host_clk_release(card->
host);
1109 err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
1111 pr_warning(
"%s: power class selection to bus width %d"
1122 static unsigned ext_csd_bits[][2] = {
1127 static unsigned bus_widths[] = {
1132 unsigned idx, bus_width = 0;
1138 for (; idx <
ARRAY_SIZE(bus_widths); idx++) {
1139 bus_width = bus_widths[
idx];
1142 err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1146 "bus width %d failed\n",
1152 ext_csd_bits[idx][0],
1153 card->
ext_csd.generic_cmd6_time);
1163 err = mmc_compare_ext_csds(card,
1173 err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1177 "bus width %d ddr %d failed\n",
1179 1 << bus_width, ddr);
1183 ext_csd_bits[idx][1],
1184 card->
ext_csd.generic_cmd6_time);
1187 pr_warning(
"%s: switch to bus width %d ddr %d "
1189 1 << bus_width, ddr);
1224 card->
ext_csd.generic_cmd6_time);
1240 card->
ext_csd.cache_size > 0) {
1243 card->
ext_csd.generic_cmd6_time);
1252 "but failed to turn on (%d)\n",
1264 mmc_free_ext_csd(ext_csd);
1271 mmc_free_ext_csd(ext_csd);
1276 static int mmc_can_poweroff_notify(
const struct mmc_card *card)
1290 timeout = card->
ext_csd.power_off_longtime;
1294 notify_type, timeout);
1296 pr_err(
"%s: Power Off Notification timed out, %u\n",
1308 static void mmc_remove(
struct mmc_host *host)
1320 static int mmc_alive(
struct mmc_host *host)
1328 static void mmc_detect(
struct mmc_host *host)
1335 mmc_claim_host(host);
1347 mmc_claim_host(host);
1357 static int mmc_suspend(
struct mmc_host *host)
1364 mmc_claim_host(host);
1365 if (mmc_can_poweroff_notify(host->
card))
1383 static int mmc_resume(
struct mmc_host *host)
1390 mmc_claim_host(host);
1391 err = mmc_init_card(host, host->
ocr, host->
card);
1397 static int mmc_power_restore(
struct mmc_host *host)
1402 mmc_claim_host(host);
1403 ret = mmc_init_card(host, host->
ocr, host->
card);
1409 static int mmc_sleep(
struct mmc_host *host)
1414 if (card && card->
ext_csd.rev >= 3) {
1417 pr_debug(
"%s: Error %d while putting card into sleep",
1424 static int mmc_awake(
struct mmc_host *host)
1429 if (card && card->
ext_csd.rev >= 3) {
1432 pr_debug(
"%s: Error %d while awaking sleeping card",
1442 .remove = mmc_remove,
1443 .detect = mmc_detect,
1446 .power_restore = mmc_power_restore,
1450 static const struct mmc_bus_ops mmc_ops_unsafe = {
1453 .remove = mmc_remove,
1454 .detect = mmc_detect,
1455 .suspend = mmc_suspend,
1456 .resume = mmc_resume,
1457 .power_restore = mmc_power_restore,
1461 static void mmc_attach_bus_ops(
struct mmc_host *host)
1465 if (!mmc_card_is_removable(host))
1466 bus_ops = &mmc_ops_unsafe;
1491 mmc_attach_bus_ops(host);
1509 pr_warning(
"%s: card claims to support voltages "
1510 "below the defined range. These will be ignored.\n",
1528 err = mmc_init_card(host, host->
ocr,
NULL);
1534 mmc_claim_host(host);
1543 mmc_claim_host(host);
1548 pr_err(
"%s: error %d whilst initialising MMC card\n",