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>
14 #include <linux/slab.h>
15 #include <linux/stat.h>
16 #include <linux/pm_runtime.h>
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
27 static const unsigned int tran_exp[] = {
28 10000, 100000, 1000000, 10000000,
32 static const unsigned char tran_mant[] = {
33 0, 10, 12, 13, 15, 20, 25, 30,
34 35, 40, 45, 50, 55, 60, 70, 80,
37 static const unsigned int tacc_exp[] = {
38 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
41 static const unsigned int tacc_mant[] = {
42 0, 10, 12, 13, 15, 20, 25, 30,
43 35, 40, 45, 50, 55, 60, 70, 80,
46 #define UNSTUFF_BITS(resp,start,size) \
48 const int __size = size; \
49 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
50 const int __off = 3 - ((start) / 32); \
51 const int __shft = (start) & 31; \
54 __res = resp[__off] >> __shft; \
55 if (__size + __shft > 32) \
56 __res |= resp[__off-1] << ((32 - __shft) % 32); \
61 * Given the decoded CSD structure, decode the raw CID to our CID structure.
63 static int mmc_decode_cid(struct mmc_card *card)
65 u32 *resp = card->raw_cid;
68 * The selection of the format here is based upon published
69 * specs from sandisk and from what people have reported.
71 switch (card->csd.mmca_vsn) {
72 case 0: /* MMC v1.0 - v1.2 */
73 case 1: /* MMC v1.4 */
74 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
75 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
76 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
77 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
78 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
79 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
80 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
81 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
82 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
83 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
84 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
85 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
86 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
89 case 2: /* MMC v2.0 - v2.2 */
90 case 3: /* MMC v3.1 - v3.3 */
92 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
93 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
94 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
95 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
96 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
97 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
98 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
99 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
100 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
101 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
102 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
103 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
107 pr_err("%s: card has unknown MMCA version %d\n",
108 mmc_hostname(card->host), card->csd.mmca_vsn);
115 static void mmc_set_erase_size(struct mmc_card *card)
117 if (card->ext_csd.erase_group_def & 1)
118 card->erase_size = card->ext_csd.hc_erase_size;
120 card->erase_size = card->csd.erase_size;
122 mmc_init_erase(card);
126 * Given a 128-bit response, decode to our card CSD structure.
128 static int mmc_decode_csd(struct mmc_card *card)
130 struct mmc_csd *csd = &card->csd;
131 unsigned int e, m, a, b;
132 u32 *resp = card->raw_csd;
135 * We only understand CSD structure v1.1 and v1.2.
136 * v1.2 has extra information in bits 15, 11 and 10.
137 * We also support eMMC v4.4 & v4.41.
139 csd->structure = UNSTUFF_BITS(resp, 126, 2);
140 if (csd->structure == 0) {
141 pr_err("%s: unrecognised CSD structure version %d\n",
142 mmc_hostname(card->host), csd->structure);
146 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
147 m = UNSTUFF_BITS(resp, 115, 4);
148 e = UNSTUFF_BITS(resp, 112, 3);
149 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
150 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
152 m = UNSTUFF_BITS(resp, 99, 4);
153 e = UNSTUFF_BITS(resp, 96, 3);
154 csd->max_dtr = tran_exp[e] * tran_mant[m];
155 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
157 e = UNSTUFF_BITS(resp, 47, 3);
158 m = UNSTUFF_BITS(resp, 62, 12);
159 csd->capacity = (1 + m) << (e + 2);
161 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
162 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
163 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
164 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
165 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
166 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
167 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
168 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
170 if (csd->write_blkbits >= 9) {
171 a = UNSTUFF_BITS(resp, 42, 5);
172 b = UNSTUFF_BITS(resp, 37, 5);
173 csd->erase_size = (a + 1) * (b + 1);
174 csd->erase_size <<= csd->write_blkbits - 9;
180 static void mmc_select_card_type(struct mmc_card *card)
182 struct mmc_host *host = card->host;
183 u8 card_type = card->ext_csd.raw_card_type;
184 u32 caps = host->caps, caps2 = host->caps2;
185 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
186 unsigned int avail_type = 0;
188 if (caps & MMC_CAP_MMC_HIGHSPEED &&
189 card_type & EXT_CSD_CARD_TYPE_HS_26) {
190 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
191 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
194 if (caps & MMC_CAP_MMC_HIGHSPEED &&
195 card_type & EXT_CSD_CARD_TYPE_HS_52) {
196 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
197 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
200 if (caps & MMC_CAP_1_8V_DDR &&
201 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
202 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
203 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
206 if (caps & MMC_CAP_1_2V_DDR &&
207 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
208 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
209 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
212 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
213 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
214 hs200_max_dtr = MMC_HS200_MAX_DTR;
215 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
218 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
219 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
220 hs200_max_dtr = MMC_HS200_MAX_DTR;
221 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
224 if (caps2 & MMC_CAP2_HS400_1_8V &&
225 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
226 hs200_max_dtr = MMC_HS200_MAX_DTR;
227 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
230 if (caps2 & MMC_CAP2_HS400_1_2V &&
231 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
232 hs200_max_dtr = MMC_HS200_MAX_DTR;
233 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
236 card->ext_csd.hs_max_dtr = hs_max_dtr;
237 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
238 card->mmc_avail_type = avail_type;
241 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
243 u8 hc_erase_grp_sz, hc_wp_grp_sz;
246 * Disable these attributes by default
248 card->ext_csd.enhanced_area_offset = -EINVAL;
249 card->ext_csd.enhanced_area_size = -EINVAL;
252 * Enhanced area feature support -- check whether the eMMC
253 * card has the Enhanced area enabled. If so, export enhanced
254 * area offset and size to user by adding sysfs interface.
256 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
257 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
258 if (card->ext_csd.partition_setting_completed) {
260 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
262 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
265 * calculate the enhanced data area offset, in bytes
267 card->ext_csd.enhanced_area_offset =
268 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
269 (ext_csd[137] << 8) + ext_csd[136];
270 if (mmc_card_blockaddr(card))
271 card->ext_csd.enhanced_area_offset <<= 9;
273 * calculate the enhanced data area size, in kilobytes
275 card->ext_csd.enhanced_area_size =
276 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
278 card->ext_csd.enhanced_area_size *=
279 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
280 card->ext_csd.enhanced_area_size <<= 9;
282 pr_warn("%s: defines enhanced area without partition setting complete\n",
283 mmc_hostname(card->host));
288 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
291 u8 hc_erase_grp_sz, hc_wp_grp_sz;
292 unsigned int part_size;
295 * General purpose partition feature support --
296 * If ext_csd has the size of general purpose partitions,
297 * set size, part_cfg, partition name in mmc_part.
299 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
300 EXT_CSD_PART_SUPPORT_PART_EN) {
302 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
304 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
306 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
307 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
308 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
309 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
311 if (card->ext_csd.partition_setting_completed == 0) {
312 pr_warn("%s: has partition size defined without partition complete\n",
313 mmc_hostname(card->host));
317 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
319 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
321 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
322 part_size *= (size_t)(hc_erase_grp_sz *
324 mmc_part_add(card, part_size << 19,
325 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
327 MMC_BLK_DATA_AREA_GP);
333 * Decode extended CSD.
335 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
338 unsigned int part_size;
340 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
341 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
342 if (card->csd.structure == 3) {
343 if (card->ext_csd.raw_ext_csd_structure > 2) {
344 pr_err("%s: unrecognised EXT_CSD structure "
345 "version %d\n", mmc_hostname(card->host),
346 card->ext_csd.raw_ext_csd_structure);
353 * The EXT_CSD format is meant to be forward compatible. As long
354 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
355 * are authorized, see JEDEC JESD84-B50 section B.8.
357 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
359 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
360 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
361 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
362 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
363 if (card->ext_csd.rev >= 2) {
364 card->ext_csd.sectors =
365 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
366 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
367 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
368 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
370 /* Cards with density > 2GiB are sector addressed */
371 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
372 mmc_card_set_blockaddr(card);
375 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
376 mmc_select_card_type(card);
378 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
379 card->ext_csd.raw_erase_timeout_mult =
380 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
381 card->ext_csd.raw_hc_erase_grp_size =
382 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
383 if (card->ext_csd.rev >= 3) {
384 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
385 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
387 /* EXT_CSD value is in units of 10ms, but we store in ms */
388 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
390 /* Sleep / awake timeout in 100ns units */
391 if (sa_shift > 0 && sa_shift <= 0x17)
392 card->ext_csd.sa_timeout =
393 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
394 card->ext_csd.erase_group_def =
395 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
396 card->ext_csd.hc_erase_timeout = 300 *
397 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
398 card->ext_csd.hc_erase_size =
399 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
401 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
404 * There are two boot regions of equal size, defined in
407 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
408 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
409 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
410 mmc_part_add(card, part_size,
411 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
413 MMC_BLK_DATA_AREA_BOOT);
418 card->ext_csd.raw_hc_erase_gap_size =
419 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
420 card->ext_csd.raw_sec_trim_mult =
421 ext_csd[EXT_CSD_SEC_TRIM_MULT];
422 card->ext_csd.raw_sec_erase_mult =
423 ext_csd[EXT_CSD_SEC_ERASE_MULT];
424 card->ext_csd.raw_sec_feature_support =
425 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
426 card->ext_csd.raw_trim_mult =
427 ext_csd[EXT_CSD_TRIM_MULT];
428 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
429 if (card->ext_csd.rev >= 4) {
430 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
431 EXT_CSD_PART_SETTING_COMPLETED)
432 card->ext_csd.partition_setting_completed = 1;
434 card->ext_csd.partition_setting_completed = 0;
436 mmc_manage_enhanced_area(card, ext_csd);
438 mmc_manage_gp_partitions(card, ext_csd);
440 card->ext_csd.sec_trim_mult =
441 ext_csd[EXT_CSD_SEC_TRIM_MULT];
442 card->ext_csd.sec_erase_mult =
443 ext_csd[EXT_CSD_SEC_ERASE_MULT];
444 card->ext_csd.sec_feature_support =
445 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
446 card->ext_csd.trim_timeout = 300 *
447 ext_csd[EXT_CSD_TRIM_MULT];
450 * Note that the call to mmc_part_add above defaults to read
451 * only. If this default assumption is changed, the call must
452 * take into account the value of boot_locked below.
454 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
455 card->ext_csd.boot_ro_lockable = true;
457 /* Save power class values */
458 card->ext_csd.raw_pwr_cl_52_195 =
459 ext_csd[EXT_CSD_PWR_CL_52_195];
460 card->ext_csd.raw_pwr_cl_26_195 =
461 ext_csd[EXT_CSD_PWR_CL_26_195];
462 card->ext_csd.raw_pwr_cl_52_360 =
463 ext_csd[EXT_CSD_PWR_CL_52_360];
464 card->ext_csd.raw_pwr_cl_26_360 =
465 ext_csd[EXT_CSD_PWR_CL_26_360];
466 card->ext_csd.raw_pwr_cl_200_195 =
467 ext_csd[EXT_CSD_PWR_CL_200_195];
468 card->ext_csd.raw_pwr_cl_200_360 =
469 ext_csd[EXT_CSD_PWR_CL_200_360];
470 card->ext_csd.raw_pwr_cl_ddr_52_195 =
471 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
472 card->ext_csd.raw_pwr_cl_ddr_52_360 =
473 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
474 card->ext_csd.raw_pwr_cl_ddr_200_360 =
475 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
478 if (card->ext_csd.rev >= 5) {
479 /* Adjust production date as per JEDEC JESD84-B451 */
480 if (card->cid.year < 2010)
481 card->cid.year += 16;
483 /* check whether the eMMC card supports BKOPS */
484 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
485 card->ext_csd.bkops = 1;
486 card->ext_csd.man_bkops_en =
487 (ext_csd[EXT_CSD_BKOPS_EN] &
488 EXT_CSD_MANUAL_BKOPS_MASK);
489 card->ext_csd.raw_bkops_status =
490 ext_csd[EXT_CSD_BKOPS_STATUS];
491 if (!card->ext_csd.man_bkops_en)
492 pr_info("%s: MAN_BKOPS_EN bit is not set\n",
493 mmc_hostname(card->host));
496 /* check whether the eMMC card supports HPI */
497 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
498 card->ext_csd.hpi = 1;
499 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
500 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
502 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
504 * Indicate the maximum timeout to close
505 * a command interrupted by HPI
507 card->ext_csd.out_of_int_time =
508 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
511 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
512 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
515 * RPMB regions are defined in multiples of 128K.
517 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
518 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
519 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
520 EXT_CSD_PART_CONFIG_ACC_RPMB,
522 MMC_BLK_DATA_AREA_RPMB);
526 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
527 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
528 card->erased_byte = 0xFF;
530 card->erased_byte = 0x0;
532 /* eMMC v4.5 or later */
533 if (card->ext_csd.rev >= 6) {
534 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
536 card->ext_csd.generic_cmd6_time = 10 *
537 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
538 card->ext_csd.power_off_longtime = 10 *
539 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
541 card->ext_csd.cache_size =
542 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
543 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
544 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
545 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
547 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
548 card->ext_csd.data_sector_size = 4096;
550 card->ext_csd.data_sector_size = 512;
552 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
553 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
554 card->ext_csd.data_tag_unit_size =
555 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
556 (card->ext_csd.data_sector_size);
558 card->ext_csd.data_tag_unit_size = 0;
561 card->ext_csd.max_packed_writes =
562 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
563 card->ext_csd.max_packed_reads =
564 ext_csd[EXT_CSD_MAX_PACKED_READS];
566 card->ext_csd.data_sector_size = 512;
569 /* eMMC v5 or later */
570 if (card->ext_csd.rev >= 7) {
571 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
573 card->ext_csd.ffu_capable =
574 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
575 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
581 static int mmc_read_ext_csd(struct mmc_card *card)
586 if (!mmc_can_ext_csd(card))
589 err = mmc_get_ext_csd(card, &ext_csd);
591 /* If the host or the card can't do the switch,
592 * fail more gracefully. */
599 * High capacity cards should have this "magic" size
600 * stored in their CSD.
602 if (card->csd.capacity == (4096 * 512)) {
603 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
604 mmc_hostname(card->host));
606 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
607 mmc_hostname(card->host));
614 err = mmc_decode_ext_csd(card, ext_csd);
619 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
624 if (bus_width == MMC_BUS_WIDTH_1)
627 err = mmc_get_ext_csd(card, &bw_ext_csd);
631 /* only compare read only fields */
632 err = !((card->ext_csd.raw_partition_support ==
633 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
634 (card->ext_csd.raw_erased_mem_count ==
635 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
636 (card->ext_csd.rev ==
637 bw_ext_csd[EXT_CSD_REV]) &&
638 (card->ext_csd.raw_ext_csd_structure ==
639 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
640 (card->ext_csd.raw_card_type ==
641 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
642 (card->ext_csd.raw_s_a_timeout ==
643 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
644 (card->ext_csd.raw_hc_erase_gap_size ==
645 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
646 (card->ext_csd.raw_erase_timeout_mult ==
647 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
648 (card->ext_csd.raw_hc_erase_grp_size ==
649 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
650 (card->ext_csd.raw_sec_trim_mult ==
651 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
652 (card->ext_csd.raw_sec_erase_mult ==
653 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
654 (card->ext_csd.raw_sec_feature_support ==
655 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
656 (card->ext_csd.raw_trim_mult ==
657 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
658 (card->ext_csd.raw_sectors[0] ==
659 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
660 (card->ext_csd.raw_sectors[1] ==
661 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
662 (card->ext_csd.raw_sectors[2] ==
663 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
664 (card->ext_csd.raw_sectors[3] ==
665 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
666 (card->ext_csd.raw_pwr_cl_52_195 ==
667 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
668 (card->ext_csd.raw_pwr_cl_26_195 ==
669 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
670 (card->ext_csd.raw_pwr_cl_52_360 ==
671 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
672 (card->ext_csd.raw_pwr_cl_26_360 ==
673 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
674 (card->ext_csd.raw_pwr_cl_200_195 ==
675 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
676 (card->ext_csd.raw_pwr_cl_200_360 ==
677 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
678 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
679 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
680 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
681 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
682 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
683 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
692 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
693 card->raw_cid[2], card->raw_cid[3]);
694 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
695 card->raw_csd[2], card->raw_csd[3]);
696 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
697 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
698 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
699 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
700 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
701 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
702 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
703 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
704 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
705 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
706 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
707 card->ext_csd.enhanced_area_offset);
708 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
709 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
710 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
712 static ssize_t mmc_fwrev_show(struct device *dev,
713 struct device_attribute *attr,
716 struct mmc_card *card = mmc_dev_to_card(dev);
718 if (card->ext_csd.rev < 7) {
719 return sprintf(buf, "0x%x\n", card->cid.fwrev);
721 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
722 card->ext_csd.fwrev);
726 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
728 static struct attribute *mmc_std_attrs[] = {
732 &dev_attr_erase_size.attr,
733 &dev_attr_preferred_erase_size.attr,
734 &dev_attr_fwrev.attr,
735 &dev_attr_ffu_capable.attr,
736 &dev_attr_hwrev.attr,
737 &dev_attr_manfid.attr,
739 &dev_attr_oemid.attr,
741 &dev_attr_serial.attr,
742 &dev_attr_enhanced_area_offset.attr,
743 &dev_attr_enhanced_area_size.attr,
744 &dev_attr_raw_rpmb_size_mult.attr,
745 &dev_attr_rel_sectors.attr,
748 ATTRIBUTE_GROUPS(mmc_std);
750 static struct device_type mmc_type = {
751 .groups = mmc_std_groups,
755 * Select the PowerClass for the current bus width
756 * If power class is defined for 4/8 bit bus in the
757 * extended CSD register, select it by executing the
758 * mmc_switch command.
760 static int __mmc_select_powerclass(struct mmc_card *card,
761 unsigned int bus_width)
763 struct mmc_host *host = card->host;
764 struct mmc_ext_csd *ext_csd = &card->ext_csd;
765 unsigned int pwrclass_val = 0;
768 switch (1 << host->ios.vdd) {
769 case MMC_VDD_165_195:
770 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
771 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
772 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
773 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
774 ext_csd->raw_pwr_cl_52_195 :
775 ext_csd->raw_pwr_cl_ddr_52_195;
776 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
777 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
788 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
789 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
790 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
791 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
792 ext_csd->raw_pwr_cl_52_360 :
793 ext_csd->raw_pwr_cl_ddr_52_360;
794 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
795 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
796 ext_csd->raw_pwr_cl_ddr_200_360 :
797 ext_csd->raw_pwr_cl_200_360;
800 pr_warn("%s: Voltage range not supported for power class\n",
805 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
806 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
807 EXT_CSD_PWR_CL_8BIT_SHIFT;
809 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
810 EXT_CSD_PWR_CL_4BIT_SHIFT;
812 /* If the power class is different from the default value */
813 if (pwrclass_val > 0) {
814 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
817 card->ext_csd.generic_cmd6_time);
823 static int mmc_select_powerclass(struct mmc_card *card)
825 struct mmc_host *host = card->host;
826 u32 bus_width, ext_csd_bits;
829 /* Power class selection is supported for versions >= 4.0 */
830 if (!mmc_can_ext_csd(card))
833 bus_width = host->ios.bus_width;
834 /* Power class values are defined only for 4/8 bit bus */
835 if (bus_width == MMC_BUS_WIDTH_1)
838 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
840 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
841 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
843 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
844 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
846 err = __mmc_select_powerclass(card, ext_csd_bits);
848 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
849 mmc_hostname(host), 1 << bus_width, ddr);
855 * Set the bus speed for the selected speed mode.
857 static void mmc_set_bus_speed(struct mmc_card *card)
859 unsigned int max_dtr = (unsigned int)-1;
861 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
862 max_dtr > card->ext_csd.hs200_max_dtr)
863 max_dtr = card->ext_csd.hs200_max_dtr;
864 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
865 max_dtr = card->ext_csd.hs_max_dtr;
866 else if (max_dtr > card->csd.max_dtr)
867 max_dtr = card->csd.max_dtr;
869 mmc_set_clock(card->host, max_dtr);
873 * Select the bus width amoung 4-bit and 8-bit(SDR).
874 * If the bus width is changed successfully, return the selected width value.
875 * Zero is returned instead of error value if the wide width is not supported.
877 static int mmc_select_bus_width(struct mmc_card *card)
879 static unsigned ext_csd_bits[] = {
883 static unsigned bus_widths[] = {
887 struct mmc_host *host = card->host;
888 unsigned idx, bus_width = 0;
891 if (!mmc_can_ext_csd(card) ||
892 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
895 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
898 * Unlike SD, MMC cards dont have a configuration register to notify
899 * supported bus width. So bus test command should be run to identify
900 * the supported bus width or compare the ext csd values of current
901 * bus width and ext csd values of 1 bit mode read earlier.
903 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
905 * Host is capable of 8bit transfer, then switch
906 * the device to work in 8bit transfer mode. If the
907 * mmc switch command returns error then switch to
908 * 4bit transfer mode. On success set the corresponding
909 * bus width on the host.
911 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
914 card->ext_csd.generic_cmd6_time);
918 bus_width = bus_widths[idx];
919 mmc_set_bus_width(host, bus_width);
922 * If controller can't handle bus width test,
923 * compare ext_csd previously read in 1 bit mode
924 * against ext_csd at new bus width
926 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
927 err = mmc_compare_ext_csds(card, bus_width);
929 err = mmc_bus_test(card, bus_width);
935 pr_warn("%s: switch to bus width %d failed\n",
936 mmc_hostname(host), ext_csd_bits[idx]);
944 * Switch to the high-speed mode
946 static int mmc_select_hs(struct mmc_card *card)
950 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
951 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
952 card->ext_csd.generic_cmd6_time,
955 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
961 * Activate wide bus and DDR if supported.
963 static int mmc_select_hs_ddr(struct mmc_card *card)
965 struct mmc_host *host = card->host;
966 u32 bus_width, ext_csd_bits;
969 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
972 bus_width = host->ios.bus_width;
973 if (bus_width == MMC_BUS_WIDTH_1)
976 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
977 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
979 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
982 card->ext_csd.generic_cmd6_time);
984 pr_err("%s: switch to bus width %d ddr failed\n",
985 mmc_hostname(host), 1 << bus_width);
990 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
993 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
995 * 1.8V vccq at 3.3V core voltage (vcc) is not required
996 * in the JEDEC spec for DDR.
998 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
999 * host controller can support this, like some of the SDHCI
1000 * controller which connect to an eMMC device. Some of these
1001 * host controller still needs to use 1.8v vccq for supporting
1004 * So the sequence will be:
1005 * if (host and device can both support 1.2v IO)
1007 * else if (host and device can both support 1.8v IO)
1009 * so if host and device can only support 3.3v IO, this is the
1012 * WARNING: eMMC rules are NOT the same as SD DDR
1015 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1016 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1018 if (err && (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V))
1019 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1021 /* make sure vccq is 3.3v after switching disaster */
1023 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1026 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1031 static int mmc_select_hs400(struct mmc_card *card)
1033 struct mmc_host *host = card->host;
1037 * HS400 mode requires 8-bit bus width
1039 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1040 host->ios.bus_width == MMC_BUS_WIDTH_8))
1044 * Before switching to dual data rate operation for HS400,
1045 * it is required to convert from HS200 mode to HS mode.
1047 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1048 mmc_set_bus_speed(card);
1050 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1051 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1052 card->ext_csd.generic_cmd6_time,
1055 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1056 mmc_hostname(host), err);
1060 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1062 EXT_CSD_DDR_BUS_WIDTH_8,
1063 card->ext_csd.generic_cmd6_time);
1065 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1066 mmc_hostname(host), err);
1070 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1071 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400,
1072 card->ext_csd.generic_cmd6_time,
1075 pr_err("%s: switch to hs400 failed, err:%d\n",
1076 mmc_hostname(host), err);
1080 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1081 mmc_set_bus_speed(card);
1087 * For device supporting HS200 mode, the following sequence
1088 * should be done before executing the tuning process.
1089 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1090 * 2. switch to HS200 mode
1091 * 3. set the clock to > 52Mhz and <=200MHz
1093 static int mmc_select_hs200(struct mmc_card *card)
1095 struct mmc_host *host = card->host;
1098 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1099 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1101 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1102 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1104 /* If fails try again during next card power cycle */
1109 * Set the bus width(4 or 8) with host's support and
1110 * switch to HS200 mode if bus width is set successfully.
1112 err = mmc_select_bus_width(card);
1113 if (!IS_ERR_VALUE(err)) {
1114 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1115 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1116 card->ext_csd.generic_cmd6_time,
1119 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1126 * Activate High Speed or HS200 mode if supported.
1128 static int mmc_select_timing(struct mmc_card *card)
1132 if (!mmc_can_ext_csd(card))
1135 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1136 err = mmc_select_hs200(card);
1137 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1138 err = mmc_select_hs(card);
1140 if (err && err != -EBADMSG)
1144 pr_warn("%s: switch to %s failed\n",
1145 mmc_card_hs(card) ? "high-speed" :
1146 (mmc_card_hs200(card) ? "hs200" : ""),
1147 mmc_hostname(card->host));
1153 * Set the bus speed to the selected bus timing.
1154 * If timing is not selected, backward compatible is the default.
1156 mmc_set_bus_speed(card);
1161 * Execute tuning sequence to seek the proper bus operating
1162 * conditions for HS200 and HS400, which sends CMD21 to the device.
1164 static int mmc_hs200_tuning(struct mmc_card *card)
1166 struct mmc_host *host = card->host;
1169 * Timing should be adjusted to the HS400 target
1170 * operation frequency for tuning process
1172 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1173 host->ios.bus_width == MMC_BUS_WIDTH_8)
1174 if (host->ops->prepare_hs400_tuning)
1175 host->ops->prepare_hs400_tuning(host, &host->ios);
1177 return mmc_execute_tuning(card);
1181 * Handle the detection and initialisation of a card.
1183 * In the case of a resume, "oldcard" will contain the card
1184 * we're trying to reinitialise.
1186 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1187 struct mmc_card *oldcard)
1189 struct mmc_card *card;
1195 WARN_ON(!host->claimed);
1197 /* Set correct bus mode for MMC before attempting init */
1198 if (!mmc_host_is_spi(host))
1199 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1202 * Since we're changing the OCR value, we seem to
1203 * need to tell some cards to go back to the idle
1204 * state. We wait 1ms to give cards time to
1206 * mmc_go_idle is needed for eMMC that are asleep
1210 /* The extra bit indicates that we support high capacity */
1211 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1216 * For SPI, enable CRC as appropriate.
1218 if (mmc_host_is_spi(host)) {
1219 err = mmc_spi_set_crc(host, use_spi_crc);
1225 * Fetch CID from card.
1227 if (mmc_host_is_spi(host))
1228 err = mmc_send_cid(host, cid);
1230 err = mmc_all_send_cid(host, cid);
1235 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1243 * Allocate card structure.
1245 card = mmc_alloc_card(host, &mmc_type);
1247 err = PTR_ERR(card);
1252 card->type = MMC_TYPE_MMC;
1254 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1258 * Call the optional HC's init_card function to handle quirks.
1260 if (host->ops->init_card)
1261 host->ops->init_card(host, card);
1264 * For native busses: set card RCA and quit open drain mode.
1266 if (!mmc_host_is_spi(host)) {
1267 err = mmc_set_relative_addr(card);
1271 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1276 * Fetch CSD from card.
1278 err = mmc_send_csd(card, card->raw_csd);
1282 err = mmc_decode_csd(card);
1285 err = mmc_decode_cid(card);
1291 * handling only for cards supporting DSR and hosts requesting
1294 if (card->csd.dsr_imp && host->dsr_req)
1298 * Select card, as all following commands rely on that.
1300 if (!mmc_host_is_spi(host)) {
1301 err = mmc_select_card(card);
1307 /* Read extended CSD. */
1308 err = mmc_read_ext_csd(card);
1312 /* If doing byte addressing, check if required to do sector
1313 * addressing. Handle the case of <2GB cards needing sector
1314 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1315 * ocr register has bit 30 set for sector addressing.
1317 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1318 mmc_card_set_blockaddr(card);
1320 /* Erase size depends on CSD and Extended CSD */
1321 mmc_set_erase_size(card);
1325 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1326 * bit. This bit will be lost every time after a reset or power off.
1328 if (card->ext_csd.partition_setting_completed ||
1329 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1330 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1331 EXT_CSD_ERASE_GROUP_DEF, 1,
1332 card->ext_csd.generic_cmd6_time);
1334 if (err && err != -EBADMSG)
1340 * Just disable enhanced area off & sz
1341 * will try to enable ERASE_GROUP_DEF
1342 * during next time reinit
1344 card->ext_csd.enhanced_area_offset = -EINVAL;
1345 card->ext_csd.enhanced_area_size = -EINVAL;
1347 card->ext_csd.erase_group_def = 1;
1349 * enable ERASE_GRP_DEF successfully.
1350 * This will affect the erase size, so
1351 * here need to reset erase size
1353 mmc_set_erase_size(card);
1358 * Ensure eMMC user default partition is enabled
1360 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1361 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1362 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1363 card->ext_csd.part_config,
1364 card->ext_csd.part_time);
1365 if (err && err != -EBADMSG)
1370 * Enable power_off_notification byte in the ext_csd register
1372 if (card->ext_csd.rev >= 6) {
1373 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1374 EXT_CSD_POWER_OFF_NOTIFICATION,
1376 card->ext_csd.generic_cmd6_time);
1377 if (err && err != -EBADMSG)
1381 * The err can be -EBADMSG or 0,
1382 * so check for success and update the flag
1385 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1389 * Select timing interface
1391 err = mmc_select_timing(card);
1395 if (mmc_card_hs200(card)) {
1396 err = mmc_hs200_tuning(card);
1400 err = mmc_select_hs400(card);
1403 } else if (mmc_card_hs(card)) {
1404 /* Select the desired bus width optionally */
1405 err = mmc_select_bus_width(card);
1406 if (!IS_ERR_VALUE(err)) {
1407 err = mmc_select_hs_ddr(card);
1414 * Choose the power class with selected bus interface
1416 mmc_select_powerclass(card);
1419 * Enable HPI feature (if supported)
1421 if (card->ext_csd.hpi) {
1422 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1423 EXT_CSD_HPI_MGMT, 1,
1424 card->ext_csd.generic_cmd6_time);
1425 if (err && err != -EBADMSG)
1428 pr_warn("%s: Enabling HPI failed\n",
1429 mmc_hostname(card->host));
1432 card->ext_csd.hpi_en = 1;
1436 * If cache size is higher than 0, this indicates
1437 * the existence of cache and it can be turned on.
1439 if (card->ext_csd.cache_size > 0) {
1440 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1441 EXT_CSD_CACHE_CTRL, 1,
1442 card->ext_csd.generic_cmd6_time);
1443 if (err && err != -EBADMSG)
1447 * Only if no error, cache is turned on successfully.
1450 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1451 mmc_hostname(card->host), err);
1452 card->ext_csd.cache_ctrl = 0;
1455 card->ext_csd.cache_ctrl = 1;
1460 * The mandatory minimum values are defined for packed command.
1463 if (card->ext_csd.max_packed_writes >= 3 &&
1464 card->ext_csd.max_packed_reads >= 5 &&
1465 host->caps2 & MMC_CAP2_PACKED_CMD) {
1466 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1467 EXT_CSD_EXP_EVENTS_CTRL,
1468 EXT_CSD_PACKED_EVENT_EN,
1469 card->ext_csd.generic_cmd6_time);
1470 if (err && err != -EBADMSG)
1473 pr_warn("%s: Enabling packed event failed\n",
1474 mmc_hostname(card->host));
1475 card->ext_csd.packed_event_en = 0;
1478 card->ext_csd.packed_event_en = 1;
1489 mmc_remove_card(card);
1494 static int mmc_can_sleep(struct mmc_card *card)
1496 return (card && card->ext_csd.rev >= 3);
1499 static int mmc_sleep(struct mmc_host *host)
1501 struct mmc_command cmd = {0};
1502 struct mmc_card *card = host->card;
1503 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1506 err = mmc_deselect_cards(host);
1510 cmd.opcode = MMC_SLEEP_AWAKE;
1511 cmd.arg = card->rca << 16;
1515 * If the max_busy_timeout of the host is specified, validate it against
1516 * the sleep cmd timeout. A failure means we need to prevent the host
1517 * from doing hw busy detection, which is done by converting to a R1
1518 * response instead of a R1B.
1520 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1521 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1523 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1524 cmd.busy_timeout = timeout_ms;
1527 err = mmc_wait_for_cmd(host, &cmd, 0);
1532 * If the host does not wait while the card signals busy, then we will
1533 * will have to wait the sleep/awake timeout. Note, we cannot use the
1534 * SEND_STATUS command to poll the status because that command (and most
1535 * others) is invalid while the card sleeps.
1537 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1538 mmc_delay(timeout_ms);
1543 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1546 mmc_card_mmc(card) &&
1547 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1550 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1552 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1555 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1556 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1557 timeout = card->ext_csd.power_off_longtime;
1559 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1560 EXT_CSD_POWER_OFF_NOTIFICATION,
1561 notify_type, timeout, true, false, false);
1563 pr_err("%s: Power Off Notification timed out, %u\n",
1564 mmc_hostname(card->host), timeout);
1566 /* Disable the power off notification after the switch operation. */
1567 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1573 * Host is being removed. Free up the current card.
1575 static void mmc_remove(struct mmc_host *host)
1578 BUG_ON(!host->card);
1580 mmc_remove_card(host->card);
1585 * Card detection - card is alive.
1587 static int mmc_alive(struct mmc_host *host)
1589 return mmc_send_status(host->card, NULL);
1593 * Card detection callback from host.
1595 static void mmc_detect(struct mmc_host *host)
1600 BUG_ON(!host->card);
1602 mmc_get_card(host->card);
1605 * Just check if our card has been removed.
1607 err = _mmc_detect_card_removed(host);
1609 mmc_put_card(host->card);
1614 mmc_claim_host(host);
1615 mmc_detach_bus(host);
1616 mmc_power_off(host);
1617 mmc_release_host(host);
1621 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1624 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1625 EXT_CSD_POWER_OFF_LONG;
1628 BUG_ON(!host->card);
1630 mmc_claim_host(host);
1632 if (mmc_card_suspended(host->card))
1635 if (mmc_card_doing_bkops(host->card)) {
1636 err = mmc_stop_bkops(host->card);
1641 err = mmc_flush_cache(host->card);
1645 if (mmc_can_poweroff_notify(host->card) &&
1646 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1647 err = mmc_poweroff_notify(host->card, notify_type);
1648 else if (mmc_can_sleep(host->card))
1649 err = mmc_sleep(host);
1650 else if (!mmc_host_is_spi(host))
1651 err = mmc_deselect_cards(host);
1654 mmc_power_off(host);
1655 mmc_card_set_suspended(host->card);
1658 mmc_release_host(host);
1665 static int mmc_suspend(struct mmc_host *host)
1669 err = _mmc_suspend(host, true);
1671 pm_runtime_disable(&host->card->dev);
1672 pm_runtime_set_suspended(&host->card->dev);
1679 * This function tries to determine if the same card is still present
1680 * and, if so, restore all state to it.
1682 static int _mmc_resume(struct mmc_host *host)
1687 BUG_ON(!host->card);
1689 mmc_claim_host(host);
1691 if (!mmc_card_suspended(host->card))
1694 mmc_power_up(host, host->card->ocr);
1695 err = mmc_init_card(host, host->card->ocr, host->card);
1696 mmc_card_clr_suspended(host->card);
1699 mmc_release_host(host);
1706 static int mmc_shutdown(struct mmc_host *host)
1711 * In a specific case for poweroff notify, we need to resume the card
1712 * before we can shutdown it properly.
1714 if (mmc_can_poweroff_notify(host->card) &&
1715 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1716 err = _mmc_resume(host);
1719 err = _mmc_suspend(host, false);
1725 * Callback for resume.
1727 static int mmc_resume(struct mmc_host *host)
1731 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1732 err = _mmc_resume(host);
1733 pm_runtime_set_active(&host->card->dev);
1734 pm_runtime_mark_last_busy(&host->card->dev);
1736 pm_runtime_enable(&host->card->dev);
1742 * Callback for runtime_suspend.
1744 static int mmc_runtime_suspend(struct mmc_host *host)
1748 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1751 err = _mmc_suspend(host, true);
1753 pr_err("%s: error %d doing aggessive suspend\n",
1754 mmc_hostname(host), err);
1760 * Callback for runtime_resume.
1762 static int mmc_runtime_resume(struct mmc_host *host)
1766 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1769 err = _mmc_resume(host);
1771 pr_err("%s: error %d doing aggessive resume\n",
1772 mmc_hostname(host), err);
1777 static int mmc_power_restore(struct mmc_host *host)
1781 mmc_claim_host(host);
1782 ret = mmc_init_card(host, host->card->ocr, host->card);
1783 mmc_release_host(host);
1788 int mmc_can_reset(struct mmc_card *card)
1792 rst_n_function = card->ext_csd.rst_n_function;
1793 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1797 EXPORT_SYMBOL(mmc_can_reset);
1799 static int mmc_reset(struct mmc_host *host)
1801 struct mmc_card *card = host->card;
1804 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1807 if (!mmc_can_reset(card))
1810 mmc_host_clk_hold(host);
1811 mmc_set_clock(host, host->f_init);
1813 host->ops->hw_reset(host);
1815 /* If the reset has happened, then a status command will fail */
1816 if (!mmc_send_status(card, &status)) {
1817 mmc_host_clk_release(host);
1821 /* Set initial state and call mmc_set_ios */
1822 mmc_set_initial_state(host);
1823 mmc_host_clk_release(host);
1825 return mmc_power_restore(host);
1828 static const struct mmc_bus_ops mmc_ops = {
1829 .remove = mmc_remove,
1830 .detect = mmc_detect,
1831 .suspend = mmc_suspend,
1832 .resume = mmc_resume,
1833 .runtime_suspend = mmc_runtime_suspend,
1834 .runtime_resume = mmc_runtime_resume,
1835 .power_restore = mmc_power_restore,
1837 .shutdown = mmc_shutdown,
1842 * Starting point for MMC card init.
1844 int mmc_attach_mmc(struct mmc_host *host)
1850 WARN_ON(!host->claimed);
1852 /* Set correct bus mode for MMC before attempting attach */
1853 if (!mmc_host_is_spi(host))
1854 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1856 err = mmc_send_op_cond(host, 0, &ocr);
1860 mmc_attach_bus(host, &mmc_ops);
1861 if (host->ocr_avail_mmc)
1862 host->ocr_avail = host->ocr_avail_mmc;
1865 * We need to get OCR a different way for SPI.
1867 if (mmc_host_is_spi(host)) {
1868 err = mmc_spi_read_ocr(host, 1, &ocr);
1873 rocr = mmc_select_voltage(host, ocr);
1876 * Can we support the voltage of the card?
1884 * Detect and init the card.
1886 err = mmc_init_card(host, rocr, NULL);
1890 mmc_release_host(host);
1891 err = mmc_add_card(host->card);
1892 mmc_claim_host(host);
1899 mmc_release_host(host);
1900 mmc_remove_card(host->card);
1901 mmc_claim_host(host);
1904 mmc_detach_bus(host);
1906 pr_err("%s: error %d whilst initialising MMC card\n",
1907 mmc_hostname(host), err);