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>
29 static const unsigned int tran_exp[] = {
30 10000, 100000, 1000000, 10000000,
34 static const unsigned char tran_mant[] = {
35 0, 10, 12, 13, 15, 20, 25, 30,
36 35, 40, 45, 50, 55, 60, 70, 80,
39 static const unsigned int tacc_exp[] = {
40 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
43 static const unsigned int tacc_mant[] = {
44 0, 10, 12, 13, 15, 20, 25, 30,
45 35, 40, 45, 50, 55, 60, 70, 80,
48 static const struct mmc_fixup mmc_ext_csd_fixups[] = {
50 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
51 * is used so disable the HPI feature for such buggy cards.
53 MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
54 0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
59 #define UNSTUFF_BITS(resp,start,size) \
61 const int __size = size; \
62 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
63 const int __off = 3 - ((start) / 32); \
64 const int __shft = (start) & 31; \
67 __res = resp[__off] >> __shft; \
68 if (__size + __shft > 32) \
69 __res |= resp[__off-1] << ((32 - __shft) % 32); \
74 * Given the decoded CSD structure, decode the raw CID to our CID structure.
76 static int mmc_decode_cid(struct mmc_card *card)
78 u32 *resp = card->raw_cid;
81 * The selection of the format here is based upon published
82 * specs from sandisk and from what people have reported.
84 switch (card->csd.mmca_vsn) {
85 case 0: /* MMC v1.0 - v1.2 */
86 case 1: /* MMC v1.4 */
87 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
88 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
89 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
90 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
91 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
92 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
93 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
94 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
95 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
96 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
97 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
98 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
99 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
102 case 2: /* MMC v2.0 - v2.2 */
103 case 3: /* MMC v3.1 - v3.3 */
105 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
106 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
107 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
108 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
109 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
110 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
111 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
112 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
113 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
114 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
115 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
116 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
120 pr_err("%s: card has unknown MMCA version %d\n",
121 mmc_hostname(card->host), card->csd.mmca_vsn);
128 static void mmc_set_erase_size(struct mmc_card *card)
130 if (card->ext_csd.erase_group_def & 1)
131 card->erase_size = card->ext_csd.hc_erase_size;
133 card->erase_size = card->csd.erase_size;
135 mmc_init_erase(card);
139 * Given a 128-bit response, decode to our card CSD structure.
141 static int mmc_decode_csd(struct mmc_card *card)
143 struct mmc_csd *csd = &card->csd;
144 unsigned int e, m, a, b;
145 u32 *resp = card->raw_csd;
148 * We only understand CSD structure v1.1 and v1.2.
149 * v1.2 has extra information in bits 15, 11 and 10.
150 * We also support eMMC v4.4 & v4.41.
152 csd->structure = UNSTUFF_BITS(resp, 126, 2);
153 if (csd->structure == 0) {
154 pr_err("%s: unrecognised CSD structure version %d\n",
155 mmc_hostname(card->host), csd->structure);
159 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
160 m = UNSTUFF_BITS(resp, 115, 4);
161 e = UNSTUFF_BITS(resp, 112, 3);
162 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
163 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
165 m = UNSTUFF_BITS(resp, 99, 4);
166 e = UNSTUFF_BITS(resp, 96, 3);
167 csd->max_dtr = tran_exp[e] * tran_mant[m];
168 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
170 e = UNSTUFF_BITS(resp, 47, 3);
171 m = UNSTUFF_BITS(resp, 62, 12);
172 csd->capacity = (1 + m) << (e + 2);
174 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
175 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
176 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
177 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
178 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
179 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
180 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
181 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
183 if (csd->write_blkbits >= 9) {
184 a = UNSTUFF_BITS(resp, 42, 5);
185 b = UNSTUFF_BITS(resp, 37, 5);
186 csd->erase_size = (a + 1) * (b + 1);
187 csd->erase_size <<= csd->write_blkbits - 9;
193 static void mmc_select_card_type(struct mmc_card *card)
195 struct mmc_host *host = card->host;
196 u8 card_type = card->ext_csd.raw_card_type;
197 u32 caps = host->caps, caps2 = host->caps2;
198 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
199 unsigned int avail_type = 0;
201 if (caps & MMC_CAP_MMC_HIGHSPEED &&
202 card_type & EXT_CSD_CARD_TYPE_HS_26) {
203 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
204 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
207 if (caps & MMC_CAP_MMC_HIGHSPEED &&
208 card_type & EXT_CSD_CARD_TYPE_HS_52) {
209 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
210 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
213 if (caps & MMC_CAP_1_8V_DDR &&
214 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
215 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
216 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
219 if (caps & MMC_CAP_1_2V_DDR &&
220 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
221 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
222 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
225 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
226 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
227 hs200_max_dtr = MMC_HS200_MAX_DTR;
228 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
231 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
232 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
233 hs200_max_dtr = MMC_HS200_MAX_DTR;
234 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
237 if (caps2 & MMC_CAP2_HS400_1_8V &&
238 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
239 hs200_max_dtr = MMC_HS200_MAX_DTR;
240 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
243 if (caps2 & MMC_CAP2_HS400_1_2V &&
244 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
245 hs200_max_dtr = MMC_HS200_MAX_DTR;
246 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
249 if ((caps2 & MMC_CAP2_HS400_ES) &&
250 card->ext_csd.strobe_support &&
251 (avail_type & EXT_CSD_CARD_TYPE_HS400))
252 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
254 card->ext_csd.hs_max_dtr = hs_max_dtr;
255 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
256 card->mmc_avail_type = avail_type;
259 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
261 u8 hc_erase_grp_sz, hc_wp_grp_sz;
264 * Disable these attributes by default
266 card->ext_csd.enhanced_area_offset = -EINVAL;
267 card->ext_csd.enhanced_area_size = -EINVAL;
270 * Enhanced area feature support -- check whether the eMMC
271 * card has the Enhanced area enabled. If so, export enhanced
272 * area offset and size to user by adding sysfs interface.
274 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
275 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
276 if (card->ext_csd.partition_setting_completed) {
278 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
280 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
283 * calculate the enhanced data area offset, in bytes
285 card->ext_csd.enhanced_area_offset =
286 (((unsigned long long)ext_csd[139]) << 24) +
287 (((unsigned long long)ext_csd[138]) << 16) +
288 (((unsigned long long)ext_csd[137]) << 8) +
289 (((unsigned long long)ext_csd[136]));
290 if (mmc_card_blockaddr(card))
291 card->ext_csd.enhanced_area_offset <<= 9;
293 * calculate the enhanced data area size, in kilobytes
295 card->ext_csd.enhanced_area_size =
296 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
298 card->ext_csd.enhanced_area_size *=
299 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
300 card->ext_csd.enhanced_area_size <<= 9;
302 pr_warn("%s: defines enhanced area without partition setting complete\n",
303 mmc_hostname(card->host));
308 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
311 u8 hc_erase_grp_sz, hc_wp_grp_sz;
312 unsigned int part_size;
315 * General purpose partition feature support --
316 * If ext_csd has the size of general purpose partitions,
317 * set size, part_cfg, partition name in mmc_part.
319 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
320 EXT_CSD_PART_SUPPORT_PART_EN) {
322 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
324 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
326 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
327 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
328 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
329 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
331 if (card->ext_csd.partition_setting_completed == 0) {
332 pr_warn("%s: has partition size defined without partition complete\n",
333 mmc_hostname(card->host));
337 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
339 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
341 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
342 part_size *= (size_t)(hc_erase_grp_sz *
344 mmc_part_add(card, part_size << 19,
345 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
347 MMC_BLK_DATA_AREA_GP);
352 /* Minimum partition switch timeout in milliseconds */
353 #define MMC_MIN_PART_SWITCH_TIME 300
356 * Decode extended CSD.
358 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
361 unsigned int part_size;
362 struct device_node *np;
363 bool broken_hpi = false;
365 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
366 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
367 if (card->csd.structure == 3) {
368 if (card->ext_csd.raw_ext_csd_structure > 2) {
369 pr_err("%s: unrecognised EXT_CSD structure "
370 "version %d\n", mmc_hostname(card->host),
371 card->ext_csd.raw_ext_csd_structure);
377 np = mmc_of_find_child_device(card->host, 0);
378 if (np && of_device_is_compatible(np, "mmc-card"))
379 broken_hpi = of_property_read_bool(np, "broken-hpi");
383 * The EXT_CSD format is meant to be forward compatible. As long
384 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
385 * are authorized, see JEDEC JESD84-B50 section B.8.
387 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
389 /* fixup device after ext_csd revision field is updated */
390 mmc_fixup_device(card, mmc_ext_csd_fixups);
392 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
393 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
394 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
395 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
396 if (card->ext_csd.rev >= 2) {
397 card->ext_csd.sectors =
398 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
399 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
400 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
401 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
403 /* Cards with density > 2GiB are sector addressed */
404 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
405 mmc_card_set_blockaddr(card);
408 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
409 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
410 mmc_select_card_type(card);
412 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
413 card->ext_csd.raw_erase_timeout_mult =
414 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
415 card->ext_csd.raw_hc_erase_grp_size =
416 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
417 if (card->ext_csd.rev >= 3) {
418 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
419 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
421 /* EXT_CSD value is in units of 10ms, but we store in ms */
422 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
423 /* Some eMMC set the value too low so set a minimum */
424 if (card->ext_csd.part_time &&
425 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
426 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
428 /* Sleep / awake timeout in 100ns units */
429 if (sa_shift > 0 && sa_shift <= 0x17)
430 card->ext_csd.sa_timeout =
431 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
432 card->ext_csd.erase_group_def =
433 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
434 card->ext_csd.hc_erase_timeout = 300 *
435 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
436 card->ext_csd.hc_erase_size =
437 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
439 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
442 * There are two boot regions of equal size, defined in
445 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
446 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
447 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
448 mmc_part_add(card, part_size,
449 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
451 MMC_BLK_DATA_AREA_BOOT);
456 card->ext_csd.raw_hc_erase_gap_size =
457 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
458 card->ext_csd.raw_sec_trim_mult =
459 ext_csd[EXT_CSD_SEC_TRIM_MULT];
460 card->ext_csd.raw_sec_erase_mult =
461 ext_csd[EXT_CSD_SEC_ERASE_MULT];
462 card->ext_csd.raw_sec_feature_support =
463 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
464 card->ext_csd.raw_trim_mult =
465 ext_csd[EXT_CSD_TRIM_MULT];
466 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
467 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
468 if (card->ext_csd.rev >= 4) {
469 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
470 EXT_CSD_PART_SETTING_COMPLETED)
471 card->ext_csd.partition_setting_completed = 1;
473 card->ext_csd.partition_setting_completed = 0;
475 mmc_manage_enhanced_area(card, ext_csd);
477 mmc_manage_gp_partitions(card, ext_csd);
479 card->ext_csd.sec_trim_mult =
480 ext_csd[EXT_CSD_SEC_TRIM_MULT];
481 card->ext_csd.sec_erase_mult =
482 ext_csd[EXT_CSD_SEC_ERASE_MULT];
483 card->ext_csd.sec_feature_support =
484 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
485 card->ext_csd.trim_timeout = 300 *
486 ext_csd[EXT_CSD_TRIM_MULT];
489 * Note that the call to mmc_part_add above defaults to read
490 * only. If this default assumption is changed, the call must
491 * take into account the value of boot_locked below.
493 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
494 card->ext_csd.boot_ro_lockable = true;
496 /* Save power class values */
497 card->ext_csd.raw_pwr_cl_52_195 =
498 ext_csd[EXT_CSD_PWR_CL_52_195];
499 card->ext_csd.raw_pwr_cl_26_195 =
500 ext_csd[EXT_CSD_PWR_CL_26_195];
501 card->ext_csd.raw_pwr_cl_52_360 =
502 ext_csd[EXT_CSD_PWR_CL_52_360];
503 card->ext_csd.raw_pwr_cl_26_360 =
504 ext_csd[EXT_CSD_PWR_CL_26_360];
505 card->ext_csd.raw_pwr_cl_200_195 =
506 ext_csd[EXT_CSD_PWR_CL_200_195];
507 card->ext_csd.raw_pwr_cl_200_360 =
508 ext_csd[EXT_CSD_PWR_CL_200_360];
509 card->ext_csd.raw_pwr_cl_ddr_52_195 =
510 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
511 card->ext_csd.raw_pwr_cl_ddr_52_360 =
512 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
513 card->ext_csd.raw_pwr_cl_ddr_200_360 =
514 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
517 if (card->ext_csd.rev >= 5) {
518 /* Adjust production date as per JEDEC JESD84-B451 */
519 if (card->cid.year < 2010)
520 card->cid.year += 16;
522 /* check whether the eMMC card supports BKOPS */
523 if (!mmc_card_broken_hpi(card) &&
524 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
525 card->ext_csd.bkops = 1;
526 card->ext_csd.man_bkops_en =
527 (ext_csd[EXT_CSD_BKOPS_EN] &
528 EXT_CSD_MANUAL_BKOPS_MASK);
529 card->ext_csd.raw_bkops_status =
530 ext_csd[EXT_CSD_BKOPS_STATUS];
531 if (!card->ext_csd.man_bkops_en)
532 pr_debug("%s: MAN_BKOPS_EN bit is not set\n",
533 mmc_hostname(card->host));
536 /* check whether the eMMC card supports HPI */
537 if (!mmc_card_broken_hpi(card) &&
538 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
539 card->ext_csd.hpi = 1;
540 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
541 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
543 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
545 * Indicate the maximum timeout to close
546 * a command interrupted by HPI
548 card->ext_csd.out_of_int_time =
549 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
552 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
553 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
556 * RPMB regions are defined in multiples of 128K.
558 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
559 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
560 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
561 EXT_CSD_PART_CONFIG_ACC_RPMB,
563 MMC_BLK_DATA_AREA_RPMB);
567 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
568 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
569 card->erased_byte = 0xFF;
571 card->erased_byte = 0x0;
573 /* eMMC v4.5 or later */
574 if (card->ext_csd.rev >= 6) {
575 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
577 card->ext_csd.generic_cmd6_time = 10 *
578 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
579 card->ext_csd.power_off_longtime = 10 *
580 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
582 card->ext_csd.cache_size =
583 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
584 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
585 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
586 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
588 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
589 card->ext_csd.data_sector_size = 4096;
591 card->ext_csd.data_sector_size = 512;
593 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
594 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
595 card->ext_csd.data_tag_unit_size =
596 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
597 (card->ext_csd.data_sector_size);
599 card->ext_csd.data_tag_unit_size = 0;
602 card->ext_csd.max_packed_writes =
603 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
604 card->ext_csd.max_packed_reads =
605 ext_csd[EXT_CSD_MAX_PACKED_READS];
607 card->ext_csd.data_sector_size = 512;
610 /* eMMC v5 or later */
611 if (card->ext_csd.rev >= 7) {
612 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
614 card->ext_csd.ffu_capable =
615 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
616 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
622 static int mmc_read_ext_csd(struct mmc_card *card)
627 if (!mmc_can_ext_csd(card))
630 err = mmc_get_ext_csd(card, &ext_csd);
632 /* If the host or the card can't do the switch,
633 * fail more gracefully. */
640 * High capacity cards should have this "magic" size
641 * stored in their CSD.
643 if (card->csd.capacity == (4096 * 512)) {
644 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
645 mmc_hostname(card->host));
647 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
648 mmc_hostname(card->host));
655 err = mmc_decode_ext_csd(card, ext_csd);
660 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
665 if (bus_width == MMC_BUS_WIDTH_1)
668 err = mmc_get_ext_csd(card, &bw_ext_csd);
672 /* only compare read only fields */
673 err = !((card->ext_csd.raw_partition_support ==
674 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
675 (card->ext_csd.raw_erased_mem_count ==
676 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
677 (card->ext_csd.rev ==
678 bw_ext_csd[EXT_CSD_REV]) &&
679 (card->ext_csd.raw_ext_csd_structure ==
680 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
681 (card->ext_csd.raw_card_type ==
682 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
683 (card->ext_csd.raw_s_a_timeout ==
684 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
685 (card->ext_csd.raw_hc_erase_gap_size ==
686 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
687 (card->ext_csd.raw_erase_timeout_mult ==
688 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
689 (card->ext_csd.raw_hc_erase_grp_size ==
690 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
691 (card->ext_csd.raw_sec_trim_mult ==
692 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
693 (card->ext_csd.raw_sec_erase_mult ==
694 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
695 (card->ext_csd.raw_sec_feature_support ==
696 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
697 (card->ext_csd.raw_trim_mult ==
698 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
699 (card->ext_csd.raw_sectors[0] ==
700 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
701 (card->ext_csd.raw_sectors[1] ==
702 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
703 (card->ext_csd.raw_sectors[2] ==
704 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
705 (card->ext_csd.raw_sectors[3] ==
706 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
707 (card->ext_csd.raw_pwr_cl_52_195 ==
708 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
709 (card->ext_csd.raw_pwr_cl_26_195 ==
710 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
711 (card->ext_csd.raw_pwr_cl_52_360 ==
712 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
713 (card->ext_csd.raw_pwr_cl_26_360 ==
714 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
715 (card->ext_csd.raw_pwr_cl_200_195 ==
716 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
717 (card->ext_csd.raw_pwr_cl_200_360 ==
718 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
719 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
720 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
721 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
722 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
723 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
724 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
733 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
734 card->raw_cid[2], card->raw_cid[3]);
735 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
736 card->raw_csd[2], card->raw_csd[3]);
737 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
738 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
739 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
740 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
741 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
742 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
743 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
744 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
745 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
746 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
747 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
748 card->ext_csd.enhanced_area_offset);
749 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
750 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
751 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
752 MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
754 static ssize_t mmc_fwrev_show(struct device *dev,
755 struct device_attribute *attr,
758 struct mmc_card *card = mmc_dev_to_card(dev);
760 if (card->ext_csd.rev < 7) {
761 return sprintf(buf, "0x%x\n", card->cid.fwrev);
763 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
764 card->ext_csd.fwrev);
768 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
770 static ssize_t mmc_dsr_show(struct device *dev,
771 struct device_attribute *attr,
774 struct mmc_card *card = mmc_dev_to_card(dev);
775 struct mmc_host *host = card->host;
777 if (card->csd.dsr_imp && host->dsr_req)
778 return sprintf(buf, "0x%x\n", host->dsr);
780 /* return default DSR value */
781 return sprintf(buf, "0x%x\n", 0x404);
784 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
786 static struct attribute *mmc_std_attrs[] = {
790 &dev_attr_erase_size.attr,
791 &dev_attr_preferred_erase_size.attr,
792 &dev_attr_fwrev.attr,
793 &dev_attr_ffu_capable.attr,
794 &dev_attr_hwrev.attr,
795 &dev_attr_manfid.attr,
797 &dev_attr_oemid.attr,
799 &dev_attr_serial.attr,
800 &dev_attr_enhanced_area_offset.attr,
801 &dev_attr_enhanced_area_size.attr,
802 &dev_attr_raw_rpmb_size_mult.attr,
803 &dev_attr_rel_sectors.attr,
808 ATTRIBUTE_GROUPS(mmc_std);
810 static struct device_type mmc_type = {
811 .groups = mmc_std_groups,
815 * Select the PowerClass for the current bus width
816 * If power class is defined for 4/8 bit bus in the
817 * extended CSD register, select it by executing the
818 * mmc_switch command.
820 static int __mmc_select_powerclass(struct mmc_card *card,
821 unsigned int bus_width)
823 struct mmc_host *host = card->host;
824 struct mmc_ext_csd *ext_csd = &card->ext_csd;
825 unsigned int pwrclass_val = 0;
828 switch (1 << host->ios.vdd) {
829 case MMC_VDD_165_195:
830 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
831 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
832 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
833 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
834 ext_csd->raw_pwr_cl_52_195 :
835 ext_csd->raw_pwr_cl_ddr_52_195;
836 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
837 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
848 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
849 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
850 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
851 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
852 ext_csd->raw_pwr_cl_52_360 :
853 ext_csd->raw_pwr_cl_ddr_52_360;
854 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
855 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
856 ext_csd->raw_pwr_cl_ddr_200_360 :
857 ext_csd->raw_pwr_cl_200_360;
860 pr_warn("%s: Voltage range not supported for power class\n",
865 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
866 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
867 EXT_CSD_PWR_CL_8BIT_SHIFT;
869 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
870 EXT_CSD_PWR_CL_4BIT_SHIFT;
872 /* If the power class is different from the default value */
873 if (pwrclass_val > 0) {
874 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
877 card->ext_csd.generic_cmd6_time);
883 static int mmc_select_powerclass(struct mmc_card *card)
885 struct mmc_host *host = card->host;
886 u32 bus_width, ext_csd_bits;
889 /* Power class selection is supported for versions >= 4.0 */
890 if (!mmc_can_ext_csd(card))
893 bus_width = host->ios.bus_width;
894 /* Power class values are defined only for 4/8 bit bus */
895 if (bus_width == MMC_BUS_WIDTH_1)
898 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
900 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
901 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
903 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
904 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
906 err = __mmc_select_powerclass(card, ext_csd_bits);
908 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
909 mmc_hostname(host), 1 << bus_width, ddr);
915 * Set the bus speed for the selected speed mode.
917 static void mmc_set_bus_speed(struct mmc_card *card)
919 unsigned int max_dtr = (unsigned int)-1;
921 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
922 max_dtr > card->ext_csd.hs200_max_dtr)
923 max_dtr = card->ext_csd.hs200_max_dtr;
924 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
925 max_dtr = card->ext_csd.hs_max_dtr;
926 else if (max_dtr > card->csd.max_dtr)
927 max_dtr = card->csd.max_dtr;
929 mmc_set_clock(card->host, max_dtr);
933 * Select the bus width amoung 4-bit and 8-bit(SDR).
934 * If the bus width is changed successfully, return the selected width value.
935 * Zero is returned instead of error value if the wide width is not supported.
937 static int mmc_select_bus_width(struct mmc_card *card)
939 static unsigned ext_csd_bits[] = {
943 static unsigned bus_widths[] = {
947 struct mmc_host *host = card->host;
948 unsigned idx, bus_width = 0;
951 if (!mmc_can_ext_csd(card) ||
952 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
955 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
958 * Unlike SD, MMC cards dont have a configuration register to notify
959 * supported bus width. So bus test command should be run to identify
960 * the supported bus width or compare the ext csd values of current
961 * bus width and ext csd values of 1 bit mode read earlier.
963 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
965 * Host is capable of 8bit transfer, then switch
966 * the device to work in 8bit transfer mode. If the
967 * mmc switch command returns error then switch to
968 * 4bit transfer mode. On success set the corresponding
969 * bus width on the host.
971 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
974 card->ext_csd.generic_cmd6_time);
978 bus_width = bus_widths[idx];
979 mmc_set_bus_width(host, bus_width);
982 * If controller can't handle bus width test,
983 * compare ext_csd previously read in 1 bit mode
984 * against ext_csd at new bus width
986 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
987 err = mmc_compare_ext_csds(card, bus_width);
989 err = mmc_bus_test(card, bus_width);
995 pr_warn("%s: switch to bus width %d failed\n",
996 mmc_hostname(host), 1 << bus_width);
1003 /* Caller must hold re-tuning */
1004 static int mmc_switch_status(struct mmc_card *card)
1009 err = mmc_send_status(card, &status);
1013 return mmc_switch_status_error(card->host, status);
1017 * Switch to the high-speed mode
1019 static int mmc_select_hs(struct mmc_card *card)
1023 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1024 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1025 card->ext_csd.generic_cmd6_time,
1028 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1029 err = mmc_switch_status(card);
1033 pr_warn("%s: switch to high-speed failed, err:%d\n",
1034 mmc_hostname(card->host), err);
1040 * Activate wide bus and DDR if supported.
1042 static int mmc_select_hs_ddr(struct mmc_card *card)
1044 struct mmc_host *host = card->host;
1045 u32 bus_width, ext_csd_bits;
1048 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1051 bus_width = host->ios.bus_width;
1052 if (bus_width == MMC_BUS_WIDTH_1)
1055 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1056 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1058 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1061 card->ext_csd.generic_cmd6_time);
1063 pr_err("%s: switch to bus width %d ddr failed\n",
1064 mmc_hostname(host), 1 << bus_width);
1069 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1072 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1074 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1075 * in the JEDEC spec for DDR.
1077 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1078 * host controller can support this, like some of the SDHCI
1079 * controller which connect to an eMMC device. Some of these
1080 * host controller still needs to use 1.8v vccq for supporting
1083 * So the sequence will be:
1084 * if (host and device can both support 1.2v IO)
1086 * else if (host and device can both support 1.8v IO)
1088 * so if host and device can only support 3.3v IO, this is the
1091 * WARNING: eMMC rules are NOT the same as SD DDR
1094 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1095 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1097 if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1098 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1100 /* make sure vccq is 3.3v after switching disaster */
1102 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1105 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1110 static int mmc_select_hs400(struct mmc_card *card)
1112 struct mmc_host *host = card->host;
1113 unsigned int max_dtr;
1118 * HS400 mode requires 8-bit bus width
1120 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1121 host->ios.bus_width == MMC_BUS_WIDTH_8))
1124 /* Switch card to HS mode */
1125 val = EXT_CSD_TIMING_HS;
1126 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1127 EXT_CSD_HS_TIMING, val,
1128 card->ext_csd.generic_cmd6_time,
1131 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1132 mmc_hostname(host), err);
1136 /* Set host controller to HS timing */
1137 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1139 /* Reduce frequency to HS frequency */
1140 max_dtr = card->ext_csd.hs_max_dtr;
1141 mmc_set_clock(host, max_dtr);
1143 err = mmc_switch_status(card);
1147 /* Switch card to DDR */
1148 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1150 EXT_CSD_DDR_BUS_WIDTH_8,
1151 card->ext_csd.generic_cmd6_time);
1153 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1154 mmc_hostname(host), err);
1158 /* Switch card to HS400 */
1159 val = EXT_CSD_TIMING_HS400 |
1160 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1161 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1162 EXT_CSD_HS_TIMING, val,
1163 card->ext_csd.generic_cmd6_time,
1166 pr_err("%s: switch to hs400 failed, err:%d\n",
1167 mmc_hostname(host), err);
1171 /* Set host controller to HS400 timing and frequency */
1172 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1173 mmc_set_bus_speed(card);
1175 err = mmc_switch_status(card);
1182 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1187 int mmc_hs200_to_hs400(struct mmc_card *card)
1189 return mmc_select_hs400(card);
1192 int mmc_hs400_to_hs200(struct mmc_card *card)
1194 struct mmc_host *host = card->host;
1195 unsigned int max_dtr;
1199 /* Reduce frequency to HS */
1200 max_dtr = card->ext_csd.hs_max_dtr;
1201 mmc_set_clock(host, max_dtr);
1203 /* Switch HS400 to HS DDR */
1204 val = EXT_CSD_TIMING_HS;
1205 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1206 val, card->ext_csd.generic_cmd6_time,
1211 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1213 err = mmc_switch_status(card);
1217 /* Switch HS DDR to HS */
1218 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1219 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1224 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1226 err = mmc_switch_status(card);
1230 /* Switch HS to HS200 */
1231 val = EXT_CSD_TIMING_HS200 |
1232 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1233 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1234 val, card->ext_csd.generic_cmd6_time,
1239 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1241 err = mmc_switch_status(card);
1245 mmc_set_bus_speed(card);
1250 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1255 static int mmc_select_hs400es(struct mmc_card *card)
1257 struct mmc_host *host = card->host;
1261 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1266 err = mmc_select_bus_width(card);
1270 /* Switch card to HS mode */
1271 err = mmc_select_hs(card);
1275 err = mmc_switch_status(card);
1279 /* Switch card to DDR with strobe bit */
1280 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1281 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1284 card->ext_csd.generic_cmd6_time);
1286 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1287 mmc_hostname(host), err);
1291 /* Switch card to HS400 */
1292 val = EXT_CSD_TIMING_HS400 |
1293 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1294 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1295 EXT_CSD_HS_TIMING, val,
1296 card->ext_csd.generic_cmd6_time,
1299 pr_err("%s: switch to hs400es failed, err:%d\n",
1300 mmc_hostname(host), err);
1304 /* Set host controller to HS400 timing and frequency */
1305 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1307 /* Controller enable enhanced strobe function */
1308 host->ios.enhanced_strobe = true;
1309 if (host->ops->hs400_enhanced_strobe)
1310 host->ops->hs400_enhanced_strobe(host, &host->ios);
1312 err = mmc_switch_status(card);
1319 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1324 static void mmc_select_driver_type(struct mmc_card *card)
1326 int card_drv_type, drive_strength, drv_type;
1328 card_drv_type = card->ext_csd.raw_driver_strength |
1329 mmc_driver_type_mask(0);
1331 drive_strength = mmc_select_drive_strength(card,
1332 card->ext_csd.hs200_max_dtr,
1333 card_drv_type, &drv_type);
1335 card->drive_strength = drive_strength;
1338 mmc_set_driver_type(card->host, drv_type);
1342 * For device supporting HS200 mode, the following sequence
1343 * should be done before executing the tuning process.
1344 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1345 * 2. switch to HS200 mode
1346 * 3. set the clock to > 52Mhz and <=200MHz
1348 static int mmc_select_hs200(struct mmc_card *card)
1350 struct mmc_host *host = card->host;
1351 unsigned int old_timing, old_signal_voltage;
1355 old_signal_voltage = host->ios.signal_voltage;
1356 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1357 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1359 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1360 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1362 /* If fails try again during next card power cycle */
1366 mmc_select_driver_type(card);
1369 * Set the bus width(4 or 8) with host's support and
1370 * switch to HS200 mode if bus width is set successfully.
1372 err = mmc_select_bus_width(card);
1374 val = EXT_CSD_TIMING_HS200 |
1375 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1376 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1377 EXT_CSD_HS_TIMING, val,
1378 card->ext_csd.generic_cmd6_time,
1382 old_timing = host->ios.timing;
1383 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1385 err = mmc_switch_status(card);
1387 * mmc_select_timing() assumes timing has not changed if
1388 * it is a switch error.
1390 if (err == -EBADMSG)
1391 mmc_set_timing(host, old_timing);
1395 /* fall back to the old signal voltage, if fails report error */
1396 if (__mmc_set_signal_voltage(host, old_signal_voltage))
1399 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1406 * Activate High Speed, HS200 or HS400ES mode if supported.
1408 static int mmc_select_timing(struct mmc_card *card)
1412 if (!mmc_can_ext_csd(card))
1415 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1416 err = mmc_select_hs400es(card);
1417 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1418 err = mmc_select_hs200(card);
1419 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1420 err = mmc_select_hs(card);
1422 if (err && err != -EBADMSG)
1427 * Set the bus speed to the selected bus timing.
1428 * If timing is not selected, backward compatible is the default.
1430 mmc_set_bus_speed(card);
1435 * Execute tuning sequence to seek the proper bus operating
1436 * conditions for HS200 and HS400, which sends CMD21 to the device.
1438 static int mmc_hs200_tuning(struct mmc_card *card)
1440 struct mmc_host *host = card->host;
1443 * Timing should be adjusted to the HS400 target
1444 * operation frequency for tuning process
1446 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1447 host->ios.bus_width == MMC_BUS_WIDTH_8)
1448 if (host->ops->prepare_hs400_tuning)
1449 host->ops->prepare_hs400_tuning(host, &host->ios);
1451 return mmc_execute_tuning(card);
1455 * Handle the detection and initialisation of a card.
1457 * In the case of a resume, "oldcard" will contain the card
1458 * we're trying to reinitialise.
1460 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1461 struct mmc_card *oldcard)
1463 struct mmc_card *card;
1469 WARN_ON(!host->claimed);
1471 /* Set correct bus mode for MMC before attempting init */
1472 if (!mmc_host_is_spi(host))
1473 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1476 * Since we're changing the OCR value, we seem to
1477 * need to tell some cards to go back to the idle
1478 * state. We wait 1ms to give cards time to
1480 * mmc_go_idle is needed for eMMC that are asleep
1484 /* The extra bit indicates that we support high capacity */
1485 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1490 * For SPI, enable CRC as appropriate.
1492 if (mmc_host_is_spi(host)) {
1493 err = mmc_spi_set_crc(host, use_spi_crc);
1499 * Fetch CID from card.
1501 if (mmc_host_is_spi(host))
1502 err = mmc_send_cid(host, cid);
1504 err = mmc_all_send_cid(host, cid);
1509 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1517 * Allocate card structure.
1519 card = mmc_alloc_card(host, &mmc_type);
1521 err = PTR_ERR(card);
1526 card->type = MMC_TYPE_MMC;
1528 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1532 * Call the optional HC's init_card function to handle quirks.
1534 if (host->ops->init_card)
1535 host->ops->init_card(host, card);
1538 * For native busses: set card RCA and quit open drain mode.
1540 if (!mmc_host_is_spi(host)) {
1541 err = mmc_set_relative_addr(card);
1545 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1550 * Fetch CSD from card.
1552 err = mmc_send_csd(card, card->raw_csd);
1556 err = mmc_decode_csd(card);
1559 err = mmc_decode_cid(card);
1565 * handling only for cards supporting DSR and hosts requesting
1568 if (card->csd.dsr_imp && host->dsr_req)
1572 * Select card, as all following commands rely on that.
1574 if (!mmc_host_is_spi(host)) {
1575 err = mmc_select_card(card);
1581 /* Read extended CSD. */
1582 err = mmc_read_ext_csd(card);
1587 * If doing byte addressing, check if required to do sector
1588 * addressing. Handle the case of <2GB cards needing sector
1589 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1590 * ocr register has bit 30 set for sector addressing.
1593 mmc_card_set_blockaddr(card);
1595 /* Erase size depends on CSD and Extended CSD */
1596 mmc_set_erase_size(card);
1600 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1601 * bit. This bit will be lost every time after a reset or power off.
1603 if (card->ext_csd.partition_setting_completed ||
1604 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1605 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1606 EXT_CSD_ERASE_GROUP_DEF, 1,
1607 card->ext_csd.generic_cmd6_time);
1609 if (err && err != -EBADMSG)
1615 * Just disable enhanced area off & sz
1616 * will try to enable ERASE_GROUP_DEF
1617 * during next time reinit
1619 card->ext_csd.enhanced_area_offset = -EINVAL;
1620 card->ext_csd.enhanced_area_size = -EINVAL;
1622 card->ext_csd.erase_group_def = 1;
1624 * enable ERASE_GRP_DEF successfully.
1625 * This will affect the erase size, so
1626 * here need to reset erase size
1628 mmc_set_erase_size(card);
1633 * Ensure eMMC user default partition is enabled
1635 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1636 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1637 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1638 card->ext_csd.part_config,
1639 card->ext_csd.part_time);
1640 if (err && err != -EBADMSG)
1645 * Enable power_off_notification byte in the ext_csd register
1647 if (card->ext_csd.rev >= 6) {
1648 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1649 EXT_CSD_POWER_OFF_NOTIFICATION,
1651 card->ext_csd.generic_cmd6_time);
1652 if (err && err != -EBADMSG)
1656 * The err can be -EBADMSG or 0,
1657 * so check for success and update the flag
1660 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1664 * Select timing interface
1666 err = mmc_select_timing(card);
1670 if (mmc_card_hs200(card)) {
1671 err = mmc_hs200_tuning(card);
1675 err = mmc_select_hs400(card);
1678 } else if (mmc_card_hs(card)) {
1679 /* Select the desired bus width optionally */
1680 err = mmc_select_bus_width(card);
1682 err = mmc_select_hs_ddr(card);
1689 * Choose the power class with selected bus interface
1691 mmc_select_powerclass(card);
1694 * Enable HPI feature (if supported)
1696 if (card->ext_csd.hpi) {
1697 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1698 EXT_CSD_HPI_MGMT, 1,
1699 card->ext_csd.generic_cmd6_time);
1700 if (err && err != -EBADMSG)
1703 pr_warn("%s: Enabling HPI failed\n",
1704 mmc_hostname(card->host));
1707 card->ext_csd.hpi_en = 1;
1711 * If cache size is higher than 0, this indicates
1712 * the existence of cache and it can be turned on.
1714 if (!mmc_card_broken_hpi(card) &&
1715 card->ext_csd.cache_size > 0) {
1716 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1717 EXT_CSD_CACHE_CTRL, 1,
1718 card->ext_csd.generic_cmd6_time);
1719 if (err && err != -EBADMSG)
1723 * Only if no error, cache is turned on successfully.
1726 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1727 mmc_hostname(card->host), err);
1728 card->ext_csd.cache_ctrl = 0;
1731 card->ext_csd.cache_ctrl = 1;
1736 * The mandatory minimum values are defined for packed command.
1739 if (card->ext_csd.max_packed_writes >= 3 &&
1740 card->ext_csd.max_packed_reads >= 5 &&
1741 host->caps2 & MMC_CAP2_PACKED_CMD) {
1742 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1743 EXT_CSD_EXP_EVENTS_CTRL,
1744 EXT_CSD_PACKED_EVENT_EN,
1745 card->ext_csd.generic_cmd6_time);
1746 if (err && err != -EBADMSG)
1749 pr_warn("%s: Enabling packed event failed\n",
1750 mmc_hostname(card->host));
1751 card->ext_csd.packed_event_en = 0;
1754 card->ext_csd.packed_event_en = 1;
1765 mmc_remove_card(card);
1770 static int mmc_can_sleep(struct mmc_card *card)
1772 return (card && card->ext_csd.rev >= 3);
1775 static int mmc_sleep(struct mmc_host *host)
1777 struct mmc_command cmd = {0};
1778 struct mmc_card *card = host->card;
1779 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1782 /* Re-tuning can't be done once the card is deselected */
1783 mmc_retune_hold(host);
1785 err = mmc_deselect_cards(host);
1789 cmd.opcode = MMC_SLEEP_AWAKE;
1790 cmd.arg = card->rca << 16;
1794 * If the max_busy_timeout of the host is specified, validate it against
1795 * the sleep cmd timeout. A failure means we need to prevent the host
1796 * from doing hw busy detection, which is done by converting to a R1
1797 * response instead of a R1B.
1799 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1800 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1802 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1803 cmd.busy_timeout = timeout_ms;
1806 err = mmc_wait_for_cmd(host, &cmd, 0);
1811 * If the host does not wait while the card signals busy, then we will
1812 * will have to wait the sleep/awake timeout. Note, we cannot use the
1813 * SEND_STATUS command to poll the status because that command (and most
1814 * others) is invalid while the card sleeps.
1816 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1817 mmc_delay(timeout_ms);
1820 mmc_retune_release(host);
1824 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1827 mmc_card_mmc(card) &&
1828 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1831 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1833 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1836 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1837 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1838 timeout = card->ext_csd.power_off_longtime;
1840 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1841 EXT_CSD_POWER_OFF_NOTIFICATION,
1842 notify_type, timeout, true, false, false);
1844 pr_err("%s: Power Off Notification timed out, %u\n",
1845 mmc_hostname(card->host), timeout);
1847 /* Disable the power off notification after the switch operation. */
1848 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1854 * Host is being removed. Free up the current card.
1856 static void mmc_remove(struct mmc_host *host)
1859 BUG_ON(!host->card);
1861 mmc_remove_card(host->card);
1866 * Card detection - card is alive.
1868 static int mmc_alive(struct mmc_host *host)
1870 return mmc_send_status(host->card, NULL);
1874 * Card detection callback from host.
1876 static void mmc_detect(struct mmc_host *host)
1881 BUG_ON(!host->card);
1883 mmc_get_card(host->card);
1886 * Just check if our card has been removed.
1888 err = _mmc_detect_card_removed(host);
1890 mmc_put_card(host->card);
1895 mmc_claim_host(host);
1896 mmc_detach_bus(host);
1897 mmc_power_off(host);
1898 mmc_release_host(host);
1902 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1905 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1906 EXT_CSD_POWER_OFF_LONG;
1909 BUG_ON(!host->card);
1911 mmc_claim_host(host);
1913 if (mmc_card_suspended(host->card))
1916 if (mmc_card_doing_bkops(host->card)) {
1917 err = mmc_stop_bkops(host->card);
1922 err = mmc_flush_cache(host->card);
1926 if (mmc_can_poweroff_notify(host->card) &&
1927 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1928 err = mmc_poweroff_notify(host->card, notify_type);
1929 else if (mmc_can_sleep(host->card))
1930 err = mmc_sleep(host);
1931 else if (!mmc_host_is_spi(host))
1932 err = mmc_deselect_cards(host);
1935 mmc_power_off(host);
1936 mmc_card_set_suspended(host->card);
1939 mmc_release_host(host);
1946 static int mmc_suspend(struct mmc_host *host)
1950 err = _mmc_suspend(host, true);
1952 pm_runtime_disable(&host->card->dev);
1953 pm_runtime_set_suspended(&host->card->dev);
1960 * This function tries to determine if the same card is still present
1961 * and, if so, restore all state to it.
1963 static int _mmc_resume(struct mmc_host *host)
1968 BUG_ON(!host->card);
1970 mmc_claim_host(host);
1972 if (!mmc_card_suspended(host->card))
1975 mmc_power_up(host, host->card->ocr);
1976 err = mmc_init_card(host, host->card->ocr, host->card);
1977 mmc_card_clr_suspended(host->card);
1980 mmc_release_host(host);
1987 static int mmc_shutdown(struct mmc_host *host)
1992 * In a specific case for poweroff notify, we need to resume the card
1993 * before we can shutdown it properly.
1995 if (mmc_can_poweroff_notify(host->card) &&
1996 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1997 err = _mmc_resume(host);
2000 err = _mmc_suspend(host, false);
2006 * Callback for resume.
2008 static int mmc_resume(struct mmc_host *host)
2010 pm_runtime_enable(&host->card->dev);
2015 * Callback for runtime_suspend.
2017 static int mmc_runtime_suspend(struct mmc_host *host)
2021 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2024 err = _mmc_suspend(host, true);
2026 pr_err("%s: error %d doing aggressive suspend\n",
2027 mmc_hostname(host), err);
2033 * Callback for runtime_resume.
2035 static int mmc_runtime_resume(struct mmc_host *host)
2039 err = _mmc_resume(host);
2040 if (err && err != -ENOMEDIUM)
2041 pr_err("%s: error %d doing runtime resume\n",
2042 mmc_hostname(host), err);
2047 int mmc_can_reset(struct mmc_card *card)
2051 rst_n_function = card->ext_csd.rst_n_function;
2052 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2056 EXPORT_SYMBOL(mmc_can_reset);
2058 static int mmc_reset(struct mmc_host *host)
2060 struct mmc_card *card = host->card;
2063 * In the case of recovery, we can't expect flushing the cache to work
2064 * always, but we have a go and ignore errors.
2066 mmc_flush_cache(host->card);
2068 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2069 mmc_can_reset(card)) {
2070 /* If the card accept RST_n signal, send it. */
2071 mmc_set_clock(host, host->f_init);
2072 host->ops->hw_reset(host);
2073 /* Set initial state and call mmc_set_ios */
2074 mmc_set_initial_state(host);
2076 /* Do a brute force power cycle */
2077 mmc_power_cycle(host, card->ocr);
2079 return mmc_init_card(host, card->ocr, card);
2082 static const struct mmc_bus_ops mmc_ops = {
2083 .remove = mmc_remove,
2084 .detect = mmc_detect,
2085 .suspend = mmc_suspend,
2086 .resume = mmc_resume,
2087 .runtime_suspend = mmc_runtime_suspend,
2088 .runtime_resume = mmc_runtime_resume,
2090 .shutdown = mmc_shutdown,
2095 * Starting point for MMC card init.
2097 int mmc_attach_mmc(struct mmc_host *host)
2103 WARN_ON(!host->claimed);
2105 /* Set correct bus mode for MMC before attempting attach */
2106 if (!mmc_host_is_spi(host))
2107 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2109 err = mmc_send_op_cond(host, 0, &ocr);
2113 mmc_attach_bus(host, &mmc_ops);
2114 if (host->ocr_avail_mmc)
2115 host->ocr_avail = host->ocr_avail_mmc;
2118 * We need to get OCR a different way for SPI.
2120 if (mmc_host_is_spi(host)) {
2121 err = mmc_spi_read_ocr(host, 1, &ocr);
2126 rocr = mmc_select_voltage(host, ocr);
2129 * Can we support the voltage of the card?
2137 * Detect and init the card.
2139 err = mmc_init_card(host, rocr, NULL);
2143 mmc_release_host(host);
2144 err = mmc_add_card(host->card);
2148 mmc_claim_host(host);
2152 mmc_remove_card(host->card);
2153 mmc_claim_host(host);
2156 mmc_detach_bus(host);
2158 pr_err("%s: error %d whilst initialising MMC card\n",
2159 mmc_hostname(host), err);