2 * linux/drivers/mmc/core/mmc.c
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.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
15 #include <linux/slab.h>
16 #include <linux/stat.h>
17 #include <linux/pm_runtime.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
28 static const unsigned int tran_exp[] = {
29 10000, 100000, 1000000, 10000000,
33 static const unsigned char tran_mant[] = {
34 0, 10, 12, 13, 15, 20, 25, 30,
35 35, 40, 45, 50, 55, 60, 70, 80,
38 static const unsigned int tacc_exp[] = {
39 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
42 static const unsigned int tacc_mant[] = {
43 0, 10, 12, 13, 15, 20, 25, 30,
44 35, 40, 45, 50, 55, 60, 70, 80,
47 #define UNSTUFF_BITS(resp,start,size) \
49 const int __size = size; \
50 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
51 const int __off = 3 - ((start) / 32); \
52 const int __shft = (start) & 31; \
55 __res = resp[__off] >> __shft; \
56 if (__size + __shft > 32) \
57 __res |= resp[__off-1] << ((32 - __shft) % 32); \
62 * Given the decoded CSD structure, decode the raw CID to our CID structure.
64 static int mmc_decode_cid(struct mmc_card *card)
66 u32 *resp = card->raw_cid;
69 * The selection of the format here is based upon published
70 * specs from sandisk and from what people have reported.
72 switch (card->csd.mmca_vsn) {
73 case 0: /* MMC v1.0 - v1.2 */
74 case 1: /* MMC v1.4 */
75 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
76 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
77 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
78 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
79 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
80 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
81 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
82 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
83 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
84 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
85 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
86 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
87 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
90 case 2: /* MMC v2.0 - v2.2 */
91 case 3: /* MMC v3.1 - v3.3 */
93 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
94 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
95 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
96 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
97 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
98 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
99 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
100 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
101 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
102 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
103 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
104 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
108 pr_err("%s: card has unknown MMCA version %d\n",
109 mmc_hostname(card->host), card->csd.mmca_vsn);
116 static void mmc_set_erase_size(struct mmc_card *card)
118 if (card->ext_csd.erase_group_def & 1)
119 card->erase_size = card->ext_csd.hc_erase_size;
121 card->erase_size = card->csd.erase_size;
123 mmc_init_erase(card);
127 * Given a 128-bit response, decode to our card CSD structure.
129 static int mmc_decode_csd(struct mmc_card *card)
131 struct mmc_csd *csd = &card->csd;
132 unsigned int e, m, a, b;
133 u32 *resp = card->raw_csd;
136 * We only understand CSD structure v1.1 and v1.2.
137 * v1.2 has extra information in bits 15, 11 and 10.
138 * We also support eMMC v4.4 & v4.41.
140 csd->structure = UNSTUFF_BITS(resp, 126, 2);
141 if (csd->structure == 0) {
142 pr_err("%s: unrecognised CSD structure version %d\n",
143 mmc_hostname(card->host), csd->structure);
147 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
148 m = UNSTUFF_BITS(resp, 115, 4);
149 e = UNSTUFF_BITS(resp, 112, 3);
150 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
151 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
153 m = UNSTUFF_BITS(resp, 99, 4);
154 e = UNSTUFF_BITS(resp, 96, 3);
155 csd->max_dtr = tran_exp[e] * tran_mant[m];
156 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
158 e = UNSTUFF_BITS(resp, 47, 3);
159 m = UNSTUFF_BITS(resp, 62, 12);
160 csd->capacity = (1 + m) << (e + 2);
162 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
163 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
164 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
165 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
166 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
167 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
168 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
169 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
171 if (csd->write_blkbits >= 9) {
172 a = UNSTUFF_BITS(resp, 42, 5);
173 b = UNSTUFF_BITS(resp, 37, 5);
174 csd->erase_size = (a + 1) * (b + 1);
175 csd->erase_size <<= csd->write_blkbits - 9;
181 static void mmc_select_card_type(struct mmc_card *card)
183 struct mmc_host *host = card->host;
184 u8 card_type = card->ext_csd.raw_card_type;
185 u32 caps = host->caps, caps2 = host->caps2;
186 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
187 unsigned int avail_type = 0;
189 if (caps & MMC_CAP_MMC_HIGHSPEED &&
190 card_type & EXT_CSD_CARD_TYPE_HS_26) {
191 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
192 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
195 if (caps & MMC_CAP_MMC_HIGHSPEED &&
196 card_type & EXT_CSD_CARD_TYPE_HS_52) {
197 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
198 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
201 if (caps & MMC_CAP_1_8V_DDR &&
202 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
203 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
204 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
207 if (caps & MMC_CAP_1_2V_DDR &&
208 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
209 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
210 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
213 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
214 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
215 hs200_max_dtr = MMC_HS200_MAX_DTR;
216 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
219 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
220 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
221 hs200_max_dtr = MMC_HS200_MAX_DTR;
222 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
225 if (caps2 & MMC_CAP2_HS400_1_8V &&
226 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
227 hs200_max_dtr = MMC_HS200_MAX_DTR;
228 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
231 if (caps2 & MMC_CAP2_HS400_1_2V &&
232 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
233 hs200_max_dtr = MMC_HS200_MAX_DTR;
234 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
237 card->ext_csd.hs_max_dtr = hs_max_dtr;
238 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
239 card->mmc_avail_type = avail_type;
242 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
244 u8 hc_erase_grp_sz, hc_wp_grp_sz;
247 * Disable these attributes by default
249 card->ext_csd.enhanced_area_offset = -EINVAL;
250 card->ext_csd.enhanced_area_size = -EINVAL;
253 * Enhanced area feature support -- check whether the eMMC
254 * card has the Enhanced area enabled. If so, export enhanced
255 * area offset and size to user by adding sysfs interface.
257 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
258 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
259 if (card->ext_csd.partition_setting_completed) {
261 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
263 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
266 * calculate the enhanced data area offset, in bytes
268 card->ext_csd.enhanced_area_offset =
269 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
270 (ext_csd[137] << 8) + ext_csd[136];
271 if (mmc_card_blockaddr(card))
272 card->ext_csd.enhanced_area_offset <<= 9;
274 * calculate the enhanced data area size, in kilobytes
276 card->ext_csd.enhanced_area_size =
277 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
279 card->ext_csd.enhanced_area_size *=
280 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
281 card->ext_csd.enhanced_area_size <<= 9;
283 pr_warn("%s: defines enhanced area without partition setting complete\n",
284 mmc_hostname(card->host));
289 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
292 u8 hc_erase_grp_sz, hc_wp_grp_sz;
293 unsigned int part_size;
296 * General purpose partition feature support --
297 * If ext_csd has the size of general purpose partitions,
298 * set size, part_cfg, partition name in mmc_part.
300 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
301 EXT_CSD_PART_SUPPORT_PART_EN) {
303 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
305 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
307 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
308 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
309 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
310 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
312 if (card->ext_csd.partition_setting_completed == 0) {
313 pr_warn("%s: has partition size defined without partition complete\n",
314 mmc_hostname(card->host));
318 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
320 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
322 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
323 part_size *= (size_t)(hc_erase_grp_sz *
325 mmc_part_add(card, part_size << 19,
326 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
328 MMC_BLK_DATA_AREA_GP);
334 * Decode extended CSD.
336 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
339 unsigned int part_size;
340 struct device_node *np;
341 bool broken_hpi = false;
343 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
344 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
345 if (card->csd.structure == 3) {
346 if (card->ext_csd.raw_ext_csd_structure > 2) {
347 pr_err("%s: unrecognised EXT_CSD structure "
348 "version %d\n", mmc_hostname(card->host),
349 card->ext_csd.raw_ext_csd_structure);
355 np = mmc_of_find_child_device(card->host, 0);
356 if (np && of_device_is_compatible(np, "mmc-card"))
357 broken_hpi = of_property_read_bool(np, "broken-hpi");
361 * The EXT_CSD format is meant to be forward compatible. As long
362 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
363 * are authorized, see JEDEC JESD84-B50 section B.8.
365 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
367 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
368 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
369 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
370 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
371 if (card->ext_csd.rev >= 2) {
372 card->ext_csd.sectors =
373 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
374 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
375 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
376 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
378 /* Cards with density > 2GiB are sector addressed */
379 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
380 mmc_card_set_blockaddr(card);
383 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
384 mmc_select_card_type(card);
386 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
387 card->ext_csd.raw_erase_timeout_mult =
388 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
389 card->ext_csd.raw_hc_erase_grp_size =
390 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
391 if (card->ext_csd.rev >= 3) {
392 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
393 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
395 /* EXT_CSD value is in units of 10ms, but we store in ms */
396 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
398 /* Sleep / awake timeout in 100ns units */
399 if (sa_shift > 0 && sa_shift <= 0x17)
400 card->ext_csd.sa_timeout =
401 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
402 card->ext_csd.erase_group_def =
403 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
404 card->ext_csd.hc_erase_timeout = 300 *
405 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
406 card->ext_csd.hc_erase_size =
407 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
409 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
412 * There are two boot regions of equal size, defined in
415 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
416 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
417 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
418 mmc_part_add(card, part_size,
419 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
421 MMC_BLK_DATA_AREA_BOOT);
426 card->ext_csd.raw_hc_erase_gap_size =
427 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
428 card->ext_csd.raw_sec_trim_mult =
429 ext_csd[EXT_CSD_SEC_TRIM_MULT];
430 card->ext_csd.raw_sec_erase_mult =
431 ext_csd[EXT_CSD_SEC_ERASE_MULT];
432 card->ext_csd.raw_sec_feature_support =
433 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
434 card->ext_csd.raw_trim_mult =
435 ext_csd[EXT_CSD_TRIM_MULT];
436 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
437 if (card->ext_csd.rev >= 4) {
438 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
439 EXT_CSD_PART_SETTING_COMPLETED)
440 card->ext_csd.partition_setting_completed = 1;
442 card->ext_csd.partition_setting_completed = 0;
444 mmc_manage_enhanced_area(card, ext_csd);
446 mmc_manage_gp_partitions(card, ext_csd);
448 card->ext_csd.sec_trim_mult =
449 ext_csd[EXT_CSD_SEC_TRIM_MULT];
450 card->ext_csd.sec_erase_mult =
451 ext_csd[EXT_CSD_SEC_ERASE_MULT];
452 card->ext_csd.sec_feature_support =
453 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
454 card->ext_csd.trim_timeout = 300 *
455 ext_csd[EXT_CSD_TRIM_MULT];
458 * Note that the call to mmc_part_add above defaults to read
459 * only. If this default assumption is changed, the call must
460 * take into account the value of boot_locked below.
462 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
463 card->ext_csd.boot_ro_lockable = true;
465 /* Save power class values */
466 card->ext_csd.raw_pwr_cl_52_195 =
467 ext_csd[EXT_CSD_PWR_CL_52_195];
468 card->ext_csd.raw_pwr_cl_26_195 =
469 ext_csd[EXT_CSD_PWR_CL_26_195];
470 card->ext_csd.raw_pwr_cl_52_360 =
471 ext_csd[EXT_CSD_PWR_CL_52_360];
472 card->ext_csd.raw_pwr_cl_26_360 =
473 ext_csd[EXT_CSD_PWR_CL_26_360];
474 card->ext_csd.raw_pwr_cl_200_195 =
475 ext_csd[EXT_CSD_PWR_CL_200_195];
476 card->ext_csd.raw_pwr_cl_200_360 =
477 ext_csd[EXT_CSD_PWR_CL_200_360];
478 card->ext_csd.raw_pwr_cl_ddr_52_195 =
479 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
480 card->ext_csd.raw_pwr_cl_ddr_52_360 =
481 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
482 card->ext_csd.raw_pwr_cl_ddr_200_360 =
483 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
486 if (card->ext_csd.rev >= 5) {
487 /* Adjust production date as per JEDEC JESD84-B451 */
488 if (card->cid.year < 2010)
489 card->cid.year += 16;
491 /* check whether the eMMC card supports BKOPS */
492 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
493 card->ext_csd.bkops = 1;
494 card->ext_csd.man_bkops_en =
495 (ext_csd[EXT_CSD_BKOPS_EN] &
496 EXT_CSD_MANUAL_BKOPS_MASK);
497 card->ext_csd.raw_bkops_status =
498 ext_csd[EXT_CSD_BKOPS_STATUS];
499 if (!card->ext_csd.man_bkops_en)
500 pr_info("%s: MAN_BKOPS_EN bit is not set\n",
501 mmc_hostname(card->host));
504 /* check whether the eMMC card supports HPI */
505 if (!broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
506 card->ext_csd.hpi = 1;
507 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
508 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
510 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
512 * Indicate the maximum timeout to close
513 * a command interrupted by HPI
515 card->ext_csd.out_of_int_time =
516 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
519 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
520 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
523 * RPMB regions are defined in multiples of 128K.
525 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
526 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
527 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
528 EXT_CSD_PART_CONFIG_ACC_RPMB,
530 MMC_BLK_DATA_AREA_RPMB);
534 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
535 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
536 card->erased_byte = 0xFF;
538 card->erased_byte = 0x0;
540 /* eMMC v4.5 or later */
541 if (card->ext_csd.rev >= 6) {
542 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
544 card->ext_csd.generic_cmd6_time = 10 *
545 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
546 card->ext_csd.power_off_longtime = 10 *
547 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
549 card->ext_csd.cache_size =
550 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
551 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
552 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
553 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
555 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
556 card->ext_csd.data_sector_size = 4096;
558 card->ext_csd.data_sector_size = 512;
560 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
561 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
562 card->ext_csd.data_tag_unit_size =
563 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
564 (card->ext_csd.data_sector_size);
566 card->ext_csd.data_tag_unit_size = 0;
569 card->ext_csd.max_packed_writes =
570 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
571 card->ext_csd.max_packed_reads =
572 ext_csd[EXT_CSD_MAX_PACKED_READS];
574 card->ext_csd.data_sector_size = 512;
577 /* eMMC v5 or later */
578 if (card->ext_csd.rev >= 7) {
579 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
581 card->ext_csd.ffu_capable =
582 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
583 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
589 static int mmc_read_ext_csd(struct mmc_card *card)
594 if (!mmc_can_ext_csd(card))
597 err = mmc_get_ext_csd(card, &ext_csd);
599 /* If the host or the card can't do the switch,
600 * fail more gracefully. */
607 * High capacity cards should have this "magic" size
608 * stored in their CSD.
610 if (card->csd.capacity == (4096 * 512)) {
611 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
612 mmc_hostname(card->host));
614 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
615 mmc_hostname(card->host));
622 err = mmc_decode_ext_csd(card, ext_csd);
627 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
632 if (bus_width == MMC_BUS_WIDTH_1)
635 err = mmc_get_ext_csd(card, &bw_ext_csd);
639 /* only compare read only fields */
640 err = !((card->ext_csd.raw_partition_support ==
641 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
642 (card->ext_csd.raw_erased_mem_count ==
643 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
644 (card->ext_csd.rev ==
645 bw_ext_csd[EXT_CSD_REV]) &&
646 (card->ext_csd.raw_ext_csd_structure ==
647 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
648 (card->ext_csd.raw_card_type ==
649 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
650 (card->ext_csd.raw_s_a_timeout ==
651 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
652 (card->ext_csd.raw_hc_erase_gap_size ==
653 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
654 (card->ext_csd.raw_erase_timeout_mult ==
655 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
656 (card->ext_csd.raw_hc_erase_grp_size ==
657 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
658 (card->ext_csd.raw_sec_trim_mult ==
659 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
660 (card->ext_csd.raw_sec_erase_mult ==
661 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
662 (card->ext_csd.raw_sec_feature_support ==
663 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
664 (card->ext_csd.raw_trim_mult ==
665 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
666 (card->ext_csd.raw_sectors[0] ==
667 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
668 (card->ext_csd.raw_sectors[1] ==
669 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
670 (card->ext_csd.raw_sectors[2] ==
671 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
672 (card->ext_csd.raw_sectors[3] ==
673 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
674 (card->ext_csd.raw_pwr_cl_52_195 ==
675 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
676 (card->ext_csd.raw_pwr_cl_26_195 ==
677 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
678 (card->ext_csd.raw_pwr_cl_52_360 ==
679 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
680 (card->ext_csd.raw_pwr_cl_26_360 ==
681 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
682 (card->ext_csd.raw_pwr_cl_200_195 ==
683 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
684 (card->ext_csd.raw_pwr_cl_200_360 ==
685 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
686 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
687 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
688 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
689 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
690 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
691 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
700 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
701 card->raw_cid[2], card->raw_cid[3]);
702 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
703 card->raw_csd[2], card->raw_csd[3]);
704 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
705 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
706 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
707 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
708 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
709 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
710 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
711 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
712 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
713 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
714 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
715 card->ext_csd.enhanced_area_offset);
716 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
717 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
718 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
720 static ssize_t mmc_fwrev_show(struct device *dev,
721 struct device_attribute *attr,
724 struct mmc_card *card = mmc_dev_to_card(dev);
726 if (card->ext_csd.rev < 7) {
727 return sprintf(buf, "0x%x\n", card->cid.fwrev);
729 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
730 card->ext_csd.fwrev);
734 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
736 static struct attribute *mmc_std_attrs[] = {
740 &dev_attr_erase_size.attr,
741 &dev_attr_preferred_erase_size.attr,
742 &dev_attr_fwrev.attr,
743 &dev_attr_ffu_capable.attr,
744 &dev_attr_hwrev.attr,
745 &dev_attr_manfid.attr,
747 &dev_attr_oemid.attr,
749 &dev_attr_serial.attr,
750 &dev_attr_enhanced_area_offset.attr,
751 &dev_attr_enhanced_area_size.attr,
752 &dev_attr_raw_rpmb_size_mult.attr,
753 &dev_attr_rel_sectors.attr,
756 ATTRIBUTE_GROUPS(mmc_std);
758 static struct device_type mmc_type = {
759 .groups = mmc_std_groups,
763 * Select the PowerClass for the current bus width
764 * If power class is defined for 4/8 bit bus in the
765 * extended CSD register, select it by executing the
766 * mmc_switch command.
768 static int __mmc_select_powerclass(struct mmc_card *card,
769 unsigned int bus_width)
771 struct mmc_host *host = card->host;
772 struct mmc_ext_csd *ext_csd = &card->ext_csd;
773 unsigned int pwrclass_val = 0;
776 switch (1 << host->ios.vdd) {
777 case MMC_VDD_165_195:
778 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
779 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
780 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
781 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
782 ext_csd->raw_pwr_cl_52_195 :
783 ext_csd->raw_pwr_cl_ddr_52_195;
784 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
785 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
796 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
797 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
798 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
799 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
800 ext_csd->raw_pwr_cl_52_360 :
801 ext_csd->raw_pwr_cl_ddr_52_360;
802 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
803 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
804 ext_csd->raw_pwr_cl_ddr_200_360 :
805 ext_csd->raw_pwr_cl_200_360;
808 pr_warn("%s: Voltage range not supported for power class\n",
813 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
814 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
815 EXT_CSD_PWR_CL_8BIT_SHIFT;
817 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
818 EXT_CSD_PWR_CL_4BIT_SHIFT;
820 /* If the power class is different from the default value */
821 if (pwrclass_val > 0) {
822 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
825 card->ext_csd.generic_cmd6_time);
831 static int mmc_select_powerclass(struct mmc_card *card)
833 struct mmc_host *host = card->host;
834 u32 bus_width, ext_csd_bits;
837 /* Power class selection is supported for versions >= 4.0 */
838 if (!mmc_can_ext_csd(card))
841 bus_width = host->ios.bus_width;
842 /* Power class values are defined only for 4/8 bit bus */
843 if (bus_width == MMC_BUS_WIDTH_1)
846 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
848 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
849 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
851 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
852 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
854 err = __mmc_select_powerclass(card, ext_csd_bits);
856 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
857 mmc_hostname(host), 1 << bus_width, ddr);
863 * Set the bus speed for the selected speed mode.
865 static void mmc_set_bus_speed(struct mmc_card *card)
867 unsigned int max_dtr = (unsigned int)-1;
869 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
870 max_dtr > card->ext_csd.hs200_max_dtr)
871 max_dtr = card->ext_csd.hs200_max_dtr;
872 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
873 max_dtr = card->ext_csd.hs_max_dtr;
874 else if (max_dtr > card->csd.max_dtr)
875 max_dtr = card->csd.max_dtr;
877 mmc_set_clock(card->host, max_dtr);
881 * Select the bus width amoung 4-bit and 8-bit(SDR).
882 * If the bus width is changed successfully, return the selected width value.
883 * Zero is returned instead of error value if the wide width is not supported.
885 static int mmc_select_bus_width(struct mmc_card *card)
887 static unsigned ext_csd_bits[] = {
891 static unsigned bus_widths[] = {
895 struct mmc_host *host = card->host;
896 unsigned idx, bus_width = 0;
899 if (!mmc_can_ext_csd(card) ||
900 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
903 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
906 * Unlike SD, MMC cards dont have a configuration register to notify
907 * supported bus width. So bus test command should be run to identify
908 * the supported bus width or compare the ext csd values of current
909 * bus width and ext csd values of 1 bit mode read earlier.
911 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
913 * Host is capable of 8bit transfer, then switch
914 * the device to work in 8bit transfer mode. If the
915 * mmc switch command returns error then switch to
916 * 4bit transfer mode. On success set the corresponding
917 * bus width on the host.
919 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
922 card->ext_csd.generic_cmd6_time);
926 bus_width = bus_widths[idx];
927 mmc_set_bus_width(host, bus_width);
930 * If controller can't handle bus width test,
931 * compare ext_csd previously read in 1 bit mode
932 * against ext_csd at new bus width
934 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
935 err = mmc_compare_ext_csds(card, bus_width);
937 err = mmc_bus_test(card, bus_width);
943 pr_warn("%s: switch to bus width %d failed\n",
944 mmc_hostname(host), ext_csd_bits[idx]);
952 * Switch to the high-speed mode
954 static int mmc_select_hs(struct mmc_card *card)
958 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
959 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
960 card->ext_csd.generic_cmd6_time,
963 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
969 * Activate wide bus and DDR if supported.
971 static int mmc_select_hs_ddr(struct mmc_card *card)
973 struct mmc_host *host = card->host;
974 u32 bus_width, ext_csd_bits;
977 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
980 bus_width = host->ios.bus_width;
981 if (bus_width == MMC_BUS_WIDTH_1)
984 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
985 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
987 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
990 card->ext_csd.generic_cmd6_time);
992 pr_err("%s: switch to bus width %d ddr failed\n",
993 mmc_hostname(host), 1 << bus_width);
998 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1001 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1003 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1004 * in the JEDEC spec for DDR.
1006 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1007 * host controller can support this, like some of the SDHCI
1008 * controller which connect to an eMMC device. Some of these
1009 * host controller still needs to use 1.8v vccq for supporting
1012 * So the sequence will be:
1013 * if (host and device can both support 1.2v IO)
1015 * else if (host and device can both support 1.8v IO)
1017 * so if host and device can only support 3.3v IO, this is the
1020 * WARNING: eMMC rules are NOT the same as SD DDR
1023 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1024 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1026 if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1027 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1029 /* make sure vccq is 3.3v after switching disaster */
1031 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1034 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1039 static int mmc_select_hs400(struct mmc_card *card)
1041 struct mmc_host *host = card->host;
1045 * HS400 mode requires 8-bit bus width
1047 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1048 host->ios.bus_width == MMC_BUS_WIDTH_8))
1052 * Before switching to dual data rate operation for HS400,
1053 * it is required to convert from HS200 mode to HS mode.
1055 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1056 mmc_set_bus_speed(card);
1058 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1059 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1060 card->ext_csd.generic_cmd6_time,
1063 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1064 mmc_hostname(host), err);
1068 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1070 EXT_CSD_DDR_BUS_WIDTH_8,
1071 card->ext_csd.generic_cmd6_time);
1073 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1074 mmc_hostname(host), err);
1078 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1079 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400,
1080 card->ext_csd.generic_cmd6_time,
1083 pr_err("%s: switch to hs400 failed, err:%d\n",
1084 mmc_hostname(host), err);
1088 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1089 mmc_set_bus_speed(card);
1095 * For device supporting HS200 mode, the following sequence
1096 * should be done before executing the tuning process.
1097 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1098 * 2. switch to HS200 mode
1099 * 3. set the clock to > 52Mhz and <=200MHz
1101 static int mmc_select_hs200(struct mmc_card *card)
1103 struct mmc_host *host = card->host;
1106 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1107 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1109 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1110 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1112 /* If fails try again during next card power cycle */
1117 * Set the bus width(4 or 8) with host's support and
1118 * switch to HS200 mode if bus width is set successfully.
1120 err = mmc_select_bus_width(card);
1121 if (!IS_ERR_VALUE(err)) {
1122 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1123 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1124 card->ext_csd.generic_cmd6_time,
1127 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1134 * Activate High Speed or HS200 mode if supported.
1136 static int mmc_select_timing(struct mmc_card *card)
1140 if (!mmc_can_ext_csd(card))
1143 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1144 err = mmc_select_hs200(card);
1145 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1146 err = mmc_select_hs(card);
1148 if (err && err != -EBADMSG)
1152 pr_warn("%s: switch to %s failed\n",
1153 mmc_card_hs(card) ? "high-speed" :
1154 (mmc_card_hs200(card) ? "hs200" : ""),
1155 mmc_hostname(card->host));
1161 * Set the bus speed to the selected bus timing.
1162 * If timing is not selected, backward compatible is the default.
1164 mmc_set_bus_speed(card);
1169 * Execute tuning sequence to seek the proper bus operating
1170 * conditions for HS200 and HS400, which sends CMD21 to the device.
1172 static int mmc_hs200_tuning(struct mmc_card *card)
1174 struct mmc_host *host = card->host;
1177 * Timing should be adjusted to the HS400 target
1178 * operation frequency for tuning process
1180 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1181 host->ios.bus_width == MMC_BUS_WIDTH_8)
1182 if (host->ops->prepare_hs400_tuning)
1183 host->ops->prepare_hs400_tuning(host, &host->ios);
1185 return mmc_execute_tuning(card);
1189 * Handle the detection and initialisation of a card.
1191 * In the case of a resume, "oldcard" will contain the card
1192 * we're trying to reinitialise.
1194 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1195 struct mmc_card *oldcard)
1197 struct mmc_card *card;
1203 WARN_ON(!host->claimed);
1205 /* Set correct bus mode for MMC before attempting init */
1206 if (!mmc_host_is_spi(host))
1207 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1210 * Since we're changing the OCR value, we seem to
1211 * need to tell some cards to go back to the idle
1212 * state. We wait 1ms to give cards time to
1214 * mmc_go_idle is needed for eMMC that are asleep
1218 /* The extra bit indicates that we support high capacity */
1219 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1224 * For SPI, enable CRC as appropriate.
1226 if (mmc_host_is_spi(host)) {
1227 err = mmc_spi_set_crc(host, use_spi_crc);
1233 * Fetch CID from card.
1235 if (mmc_host_is_spi(host))
1236 err = mmc_send_cid(host, cid);
1238 err = mmc_all_send_cid(host, cid);
1243 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1251 * Allocate card structure.
1253 card = mmc_alloc_card(host, &mmc_type);
1255 err = PTR_ERR(card);
1260 card->type = MMC_TYPE_MMC;
1262 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1266 * Call the optional HC's init_card function to handle quirks.
1268 if (host->ops->init_card)
1269 host->ops->init_card(host, card);
1272 * For native busses: set card RCA and quit open drain mode.
1274 if (!mmc_host_is_spi(host)) {
1275 err = mmc_set_relative_addr(card);
1279 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1284 * Fetch CSD from card.
1286 err = mmc_send_csd(card, card->raw_csd);
1290 err = mmc_decode_csd(card);
1293 err = mmc_decode_cid(card);
1299 * handling only for cards supporting DSR and hosts requesting
1302 if (card->csd.dsr_imp && host->dsr_req)
1306 * Select card, as all following commands rely on that.
1308 if (!mmc_host_is_spi(host)) {
1309 err = mmc_select_card(card);
1315 /* Read extended CSD. */
1316 err = mmc_read_ext_csd(card);
1320 /* If doing byte addressing, check if required to do sector
1321 * addressing. Handle the case of <2GB cards needing sector
1322 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1323 * ocr register has bit 30 set for sector addressing.
1325 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1326 mmc_card_set_blockaddr(card);
1328 /* Erase size depends on CSD and Extended CSD */
1329 mmc_set_erase_size(card);
1333 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1334 * bit. This bit will be lost every time after a reset or power off.
1336 if (card->ext_csd.partition_setting_completed ||
1337 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1338 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1339 EXT_CSD_ERASE_GROUP_DEF, 1,
1340 card->ext_csd.generic_cmd6_time);
1342 if (err && err != -EBADMSG)
1348 * Just disable enhanced area off & sz
1349 * will try to enable ERASE_GROUP_DEF
1350 * during next time reinit
1352 card->ext_csd.enhanced_area_offset = -EINVAL;
1353 card->ext_csd.enhanced_area_size = -EINVAL;
1355 card->ext_csd.erase_group_def = 1;
1357 * enable ERASE_GRP_DEF successfully.
1358 * This will affect the erase size, so
1359 * here need to reset erase size
1361 mmc_set_erase_size(card);
1366 * Ensure eMMC user default partition is enabled
1368 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1369 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1370 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1371 card->ext_csd.part_config,
1372 card->ext_csd.part_time);
1373 if (err && err != -EBADMSG)
1378 * Enable power_off_notification byte in the ext_csd register
1380 if (card->ext_csd.rev >= 6) {
1381 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1382 EXT_CSD_POWER_OFF_NOTIFICATION,
1384 card->ext_csd.generic_cmd6_time);
1385 if (err && err != -EBADMSG)
1389 * The err can be -EBADMSG or 0,
1390 * so check for success and update the flag
1393 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1397 * Select timing interface
1399 err = mmc_select_timing(card);
1403 if (mmc_card_hs200(card)) {
1404 err = mmc_hs200_tuning(card);
1408 err = mmc_select_hs400(card);
1411 } else if (mmc_card_hs(card)) {
1412 /* Select the desired bus width optionally */
1413 err = mmc_select_bus_width(card);
1414 if (!IS_ERR_VALUE(err)) {
1415 err = mmc_select_hs_ddr(card);
1422 * Choose the power class with selected bus interface
1424 mmc_select_powerclass(card);
1427 * Enable HPI feature (if supported)
1429 if (card->ext_csd.hpi) {
1430 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1431 EXT_CSD_HPI_MGMT, 1,
1432 card->ext_csd.generic_cmd6_time);
1433 if (err && err != -EBADMSG)
1436 pr_warn("%s: Enabling HPI failed\n",
1437 mmc_hostname(card->host));
1440 card->ext_csd.hpi_en = 1;
1444 * If cache size is higher than 0, this indicates
1445 * the existence of cache and it can be turned on.
1447 if (card->ext_csd.cache_size > 0) {
1448 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1449 EXT_CSD_CACHE_CTRL, 1,
1450 card->ext_csd.generic_cmd6_time);
1451 if (err && err != -EBADMSG)
1455 * Only if no error, cache is turned on successfully.
1458 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1459 mmc_hostname(card->host), err);
1460 card->ext_csd.cache_ctrl = 0;
1463 card->ext_csd.cache_ctrl = 1;
1468 * The mandatory minimum values are defined for packed command.
1471 if (card->ext_csd.max_packed_writes >= 3 &&
1472 card->ext_csd.max_packed_reads >= 5 &&
1473 host->caps2 & MMC_CAP2_PACKED_CMD) {
1474 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1475 EXT_CSD_EXP_EVENTS_CTRL,
1476 EXT_CSD_PACKED_EVENT_EN,
1477 card->ext_csd.generic_cmd6_time);
1478 if (err && err != -EBADMSG)
1481 pr_warn("%s: Enabling packed event failed\n",
1482 mmc_hostname(card->host));
1483 card->ext_csd.packed_event_en = 0;
1486 card->ext_csd.packed_event_en = 1;
1497 mmc_remove_card(card);
1502 static int mmc_can_sleep(struct mmc_card *card)
1504 return (card && card->ext_csd.rev >= 3);
1507 static int mmc_sleep(struct mmc_host *host)
1509 struct mmc_command cmd = {0};
1510 struct mmc_card *card = host->card;
1511 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1514 err = mmc_deselect_cards(host);
1518 cmd.opcode = MMC_SLEEP_AWAKE;
1519 cmd.arg = card->rca << 16;
1523 * If the max_busy_timeout of the host is specified, validate it against
1524 * the sleep cmd timeout. A failure means we need to prevent the host
1525 * from doing hw busy detection, which is done by converting to a R1
1526 * response instead of a R1B.
1528 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1529 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1531 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1532 cmd.busy_timeout = timeout_ms;
1535 err = mmc_wait_for_cmd(host, &cmd, 0);
1540 * If the host does not wait while the card signals busy, then we will
1541 * will have to wait the sleep/awake timeout. Note, we cannot use the
1542 * SEND_STATUS command to poll the status because that command (and most
1543 * others) is invalid while the card sleeps.
1545 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1546 mmc_delay(timeout_ms);
1551 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1554 mmc_card_mmc(card) &&
1555 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1558 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1560 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1563 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1564 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1565 timeout = card->ext_csd.power_off_longtime;
1567 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1568 EXT_CSD_POWER_OFF_NOTIFICATION,
1569 notify_type, timeout, true, false, false);
1571 pr_err("%s: Power Off Notification timed out, %u\n",
1572 mmc_hostname(card->host), timeout);
1574 /* Disable the power off notification after the switch operation. */
1575 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1581 * Host is being removed. Free up the current card.
1583 static void mmc_remove(struct mmc_host *host)
1586 BUG_ON(!host->card);
1588 mmc_remove_card(host->card);
1593 * Card detection - card is alive.
1595 static int mmc_alive(struct mmc_host *host)
1597 return mmc_send_status(host->card, NULL);
1601 * Card detection callback from host.
1603 static void mmc_detect(struct mmc_host *host)
1608 BUG_ON(!host->card);
1610 mmc_get_card(host->card);
1613 * Just check if our card has been removed.
1615 err = _mmc_detect_card_removed(host);
1617 mmc_put_card(host->card);
1622 mmc_claim_host(host);
1623 mmc_detach_bus(host);
1624 mmc_power_off(host);
1625 mmc_release_host(host);
1629 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1632 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1633 EXT_CSD_POWER_OFF_LONG;
1636 BUG_ON(!host->card);
1638 mmc_claim_host(host);
1640 if (mmc_card_suspended(host->card))
1643 if (mmc_card_doing_bkops(host->card)) {
1644 err = mmc_stop_bkops(host->card);
1649 err = mmc_flush_cache(host->card);
1653 if (mmc_can_poweroff_notify(host->card) &&
1654 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1655 err = mmc_poweroff_notify(host->card, notify_type);
1656 else if (mmc_can_sleep(host->card))
1657 err = mmc_sleep(host);
1658 else if (!mmc_host_is_spi(host))
1659 err = mmc_deselect_cards(host);
1662 mmc_power_off(host);
1663 mmc_card_set_suspended(host->card);
1666 mmc_release_host(host);
1673 static int mmc_suspend(struct mmc_host *host)
1677 err = _mmc_suspend(host, true);
1679 pm_runtime_disable(&host->card->dev);
1680 pm_runtime_set_suspended(&host->card->dev);
1687 * This function tries to determine if the same card is still present
1688 * and, if so, restore all state to it.
1690 static int _mmc_resume(struct mmc_host *host)
1695 BUG_ON(!host->card);
1697 mmc_claim_host(host);
1699 if (!mmc_card_suspended(host->card))
1702 mmc_power_up(host, host->card->ocr);
1703 err = mmc_init_card(host, host->card->ocr, host->card);
1704 mmc_card_clr_suspended(host->card);
1707 mmc_release_host(host);
1714 static int mmc_shutdown(struct mmc_host *host)
1719 * In a specific case for poweroff notify, we need to resume the card
1720 * before we can shutdown it properly.
1722 if (mmc_can_poweroff_notify(host->card) &&
1723 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1724 err = _mmc_resume(host);
1727 err = _mmc_suspend(host, false);
1733 * Callback for resume.
1735 static int mmc_resume(struct mmc_host *host)
1739 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1740 err = _mmc_resume(host);
1741 pm_runtime_set_active(&host->card->dev);
1742 pm_runtime_mark_last_busy(&host->card->dev);
1744 pm_runtime_enable(&host->card->dev);
1750 * Callback for runtime_suspend.
1752 static int mmc_runtime_suspend(struct mmc_host *host)
1756 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1759 err = _mmc_suspend(host, true);
1761 pr_err("%s: error %d doing aggressive suspend\n",
1762 mmc_hostname(host), err);
1768 * Callback for runtime_resume.
1770 static int mmc_runtime_resume(struct mmc_host *host)
1774 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1777 err = _mmc_resume(host);
1779 pr_err("%s: error %d doing aggressive resume\n",
1780 mmc_hostname(host), err);
1785 static int mmc_power_restore(struct mmc_host *host)
1789 mmc_claim_host(host);
1790 ret = mmc_init_card(host, host->card->ocr, host->card);
1791 mmc_release_host(host);
1796 int mmc_can_reset(struct mmc_card *card)
1800 rst_n_function = card->ext_csd.rst_n_function;
1801 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1805 EXPORT_SYMBOL(mmc_can_reset);
1807 static int mmc_reset(struct mmc_host *host)
1809 struct mmc_card *card = host->card;
1812 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1815 if (!mmc_can_reset(card))
1818 mmc_host_clk_hold(host);
1819 mmc_set_clock(host, host->f_init);
1821 host->ops->hw_reset(host);
1823 /* If the reset has happened, then a status command will fail */
1824 if (!mmc_send_status(card, &status)) {
1825 mmc_host_clk_release(host);
1829 /* Set initial state and call mmc_set_ios */
1830 mmc_set_initial_state(host);
1831 mmc_host_clk_release(host);
1833 return mmc_power_restore(host);
1836 static const struct mmc_bus_ops mmc_ops = {
1837 .remove = mmc_remove,
1838 .detect = mmc_detect,
1839 .suspend = mmc_suspend,
1840 .resume = mmc_resume,
1841 .runtime_suspend = mmc_runtime_suspend,
1842 .runtime_resume = mmc_runtime_resume,
1843 .power_restore = mmc_power_restore,
1845 .shutdown = mmc_shutdown,
1850 * Starting point for MMC card init.
1852 int mmc_attach_mmc(struct mmc_host *host)
1858 WARN_ON(!host->claimed);
1860 /* Set correct bus mode for MMC before attempting attach */
1861 if (!mmc_host_is_spi(host))
1862 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1864 err = mmc_send_op_cond(host, 0, &ocr);
1868 mmc_attach_bus(host, &mmc_ops);
1869 if (host->ocr_avail_mmc)
1870 host->ocr_avail = host->ocr_avail_mmc;
1873 * We need to get OCR a different way for SPI.
1875 if (mmc_host_is_spi(host)) {
1876 err = mmc_spi_read_ocr(host, 1, &ocr);
1881 rocr = mmc_select_voltage(host, ocr);
1884 * Can we support the voltage of the card?
1892 * Detect and init the card.
1894 err = mmc_init_card(host, rocr, NULL);
1898 mmc_release_host(host);
1899 err = mmc_add_card(host->card);
1900 mmc_claim_host(host);
1907 mmc_release_host(host);
1908 mmc_remove_card(host->card);
1909 mmc_claim_host(host);
1912 mmc_detach_bus(host);
1914 pr_err("%s: error %d whilst initialising MMC card\n",
1915 mmc_hostname(host), err);