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->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
166 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
167 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
169 if (csd->write_blkbits >= 9) {
170 a = UNSTUFF_BITS(resp, 42, 5);
171 b = UNSTUFF_BITS(resp, 37, 5);
172 csd->erase_size = (a + 1) * (b + 1);
173 csd->erase_size <<= csd->write_blkbits - 9;
182 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
188 BUG_ON(!new_ext_csd);
192 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
196 * As the ext_csd is so large and mostly unused, we don't store the
197 * raw block in mmc_card.
199 ext_csd = kmalloc(512, GFP_KERNEL);
201 pr_err("%s: could not allocate a buffer to "
202 "receive the ext_csd.\n", mmc_hostname(card->host));
206 err = mmc_send_ext_csd(card, ext_csd);
211 /* If the host or the card can't do the switch,
212 * fail more gracefully. */
219 * High capacity cards should have this "magic" size
220 * stored in their CSD.
222 if (card->csd.capacity == (4096 * 512)) {
223 pr_err("%s: unable to read EXT_CSD "
224 "on a possible high capacity card. "
225 "Card will be ignored.\n",
226 mmc_hostname(card->host));
228 pr_warning("%s: unable to read "
229 "EXT_CSD, performance might "
231 mmc_hostname(card->host));
235 *new_ext_csd = ext_csd;
240 static void mmc_select_card_type(struct mmc_card *card)
242 struct mmc_host *host = card->host;
243 u8 card_type = card->ext_csd.raw_card_type;
244 u32 caps = host->caps, caps2 = host->caps2;
245 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
246 unsigned int avail_type = 0;
248 if (caps & MMC_CAP_MMC_HIGHSPEED &&
249 card_type & EXT_CSD_CARD_TYPE_HS_26) {
250 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
251 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
254 if (caps & MMC_CAP_MMC_HIGHSPEED &&
255 card_type & EXT_CSD_CARD_TYPE_HS_52) {
256 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
257 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
260 if (caps & MMC_CAP_1_8V_DDR &&
261 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
262 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
263 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
266 if (caps & MMC_CAP_1_2V_DDR &&
267 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
268 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
269 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
272 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
273 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
274 hs200_max_dtr = MMC_HS200_MAX_DTR;
275 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
278 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
279 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
280 hs200_max_dtr = MMC_HS200_MAX_DTR;
281 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
284 if (caps2 & MMC_CAP2_HS400_1_8V &&
285 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
286 hs200_max_dtr = MMC_HS200_MAX_DTR;
287 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
290 if (caps2 & MMC_CAP2_HS400_1_2V &&
291 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
292 hs200_max_dtr = MMC_HS200_MAX_DTR;
293 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
296 card->ext_csd.hs_max_dtr = hs_max_dtr;
297 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
298 card->mmc_avail_type = avail_type;
302 * Decode extended CSD.
304 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
307 unsigned int part_size;
308 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
315 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
316 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
317 if (card->csd.structure == 3) {
318 if (card->ext_csd.raw_ext_csd_structure > 2) {
319 pr_err("%s: unrecognised EXT_CSD structure "
320 "version %d\n", mmc_hostname(card->host),
321 card->ext_csd.raw_ext_csd_structure);
328 * The EXT_CSD format is meant to be forward compatible. As long
329 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
330 * are authorized, see JEDEC JESD84-B50 section B.8.
332 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
334 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
335 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
336 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
337 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
338 if (card->ext_csd.rev >= 2) {
339 card->ext_csd.sectors =
340 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
341 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
342 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
343 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
345 /* Cards with density > 2GiB are sector addressed */
346 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
347 mmc_card_set_blockaddr(card);
350 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
351 mmc_select_card_type(card);
353 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
354 card->ext_csd.raw_erase_timeout_mult =
355 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
356 card->ext_csd.raw_hc_erase_grp_size =
357 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
358 if (card->ext_csd.rev >= 3) {
359 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
360 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
362 /* EXT_CSD value is in units of 10ms, but we store in ms */
363 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
365 /* Sleep / awake timeout in 100ns units */
366 if (sa_shift > 0 && sa_shift <= 0x17)
367 card->ext_csd.sa_timeout =
368 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
369 card->ext_csd.erase_group_def =
370 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
371 card->ext_csd.hc_erase_timeout = 300 *
372 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
373 card->ext_csd.hc_erase_size =
374 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
376 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
379 * There are two boot regions of equal size, defined in
382 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
383 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
384 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
385 mmc_part_add(card, part_size,
386 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
388 MMC_BLK_DATA_AREA_BOOT);
393 card->ext_csd.raw_hc_erase_gap_size =
394 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
395 card->ext_csd.raw_sec_trim_mult =
396 ext_csd[EXT_CSD_SEC_TRIM_MULT];
397 card->ext_csd.raw_sec_erase_mult =
398 ext_csd[EXT_CSD_SEC_ERASE_MULT];
399 card->ext_csd.raw_sec_feature_support =
400 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
401 card->ext_csd.raw_trim_mult =
402 ext_csd[EXT_CSD_TRIM_MULT];
403 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
404 if (card->ext_csd.rev >= 4) {
406 * Enhanced area feature support -- check whether the eMMC
407 * card has the Enhanced area enabled. If so, export enhanced
408 * area offset and size to user by adding sysfs interface.
410 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
411 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
413 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
415 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
417 card->ext_csd.enhanced_area_en = 1;
419 * calculate the enhanced data area offset, in bytes
421 card->ext_csd.enhanced_area_offset =
422 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
423 (ext_csd[137] << 8) + ext_csd[136];
424 if (mmc_card_blockaddr(card))
425 card->ext_csd.enhanced_area_offset <<= 9;
427 * calculate the enhanced data area size, in kilobytes
429 card->ext_csd.enhanced_area_size =
430 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
432 card->ext_csd.enhanced_area_size *=
433 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
434 card->ext_csd.enhanced_area_size <<= 9;
437 * If the enhanced area is not enabled, disable these
440 card->ext_csd.enhanced_area_offset = -EINVAL;
441 card->ext_csd.enhanced_area_size = -EINVAL;
445 * General purpose partition feature support --
446 * If ext_csd has the size of general purpose partitions,
447 * set size, part_cfg, partition name in mmc_part.
449 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
450 EXT_CSD_PART_SUPPORT_PART_EN) {
451 if (card->ext_csd.enhanced_area_en != 1) {
453 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
455 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
457 card->ext_csd.enhanced_area_en = 1;
460 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
461 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
462 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
463 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
466 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
468 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
470 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
471 part_size *= (size_t)(hc_erase_grp_sz *
473 mmc_part_add(card, part_size << 19,
474 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
476 MMC_BLK_DATA_AREA_GP);
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 (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
524 card->ext_csd.bkops = 1;
525 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
526 card->ext_csd.raw_bkops_status =
527 ext_csd[EXT_CSD_BKOPS_STATUS];
528 if (!card->ext_csd.bkops_en)
529 pr_info("%s: BKOPS_EN bit is not set\n",
530 mmc_hostname(card->host));
533 /* check whether the eMMC card supports HPI */
534 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
535 card->ext_csd.hpi = 1;
536 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
537 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
539 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
541 * Indicate the maximum timeout to close
542 * a command interrupted by HPI
544 card->ext_csd.out_of_int_time =
545 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
548 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
549 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
552 * RPMB regions are defined in multiples of 128K.
554 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
555 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
556 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
557 EXT_CSD_PART_CONFIG_ACC_RPMB,
559 MMC_BLK_DATA_AREA_RPMB);
563 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
564 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
565 card->erased_byte = 0xFF;
567 card->erased_byte = 0x0;
569 /* eMMC v4.5 or later */
570 if (card->ext_csd.rev >= 6) {
571 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
573 card->ext_csd.generic_cmd6_time = 10 *
574 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
575 card->ext_csd.power_off_longtime = 10 *
576 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
578 card->ext_csd.cache_size =
579 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
580 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
581 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
582 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
584 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
585 card->ext_csd.data_sector_size = 4096;
587 card->ext_csd.data_sector_size = 512;
589 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
590 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
591 card->ext_csd.data_tag_unit_size =
592 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
593 (card->ext_csd.data_sector_size);
595 card->ext_csd.data_tag_unit_size = 0;
598 card->ext_csd.max_packed_writes =
599 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
600 card->ext_csd.max_packed_reads =
601 ext_csd[EXT_CSD_MAX_PACKED_READS];
603 card->ext_csd.data_sector_size = 512;
610 static inline void mmc_free_ext_csd(u8 *ext_csd)
616 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
621 if (bus_width == MMC_BUS_WIDTH_1)
624 err = mmc_get_ext_csd(card, &bw_ext_csd);
626 if (err || bw_ext_csd == NULL) {
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]));
689 mmc_free_ext_csd(bw_ext_csd);
693 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
694 card->raw_cid[2], card->raw_cid[3]);
695 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
696 card->raw_csd[2], card->raw_csd[3]);
697 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
698 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
699 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
700 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
701 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
702 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
703 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
704 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
705 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
706 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
707 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
708 card->ext_csd.enhanced_area_offset);
709 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
710 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
711 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
713 static struct attribute *mmc_std_attrs[] = {
717 &dev_attr_erase_size.attr,
718 &dev_attr_preferred_erase_size.attr,
719 &dev_attr_fwrev.attr,
720 &dev_attr_hwrev.attr,
721 &dev_attr_manfid.attr,
723 &dev_attr_oemid.attr,
725 &dev_attr_serial.attr,
726 &dev_attr_enhanced_area_offset.attr,
727 &dev_attr_enhanced_area_size.attr,
728 &dev_attr_raw_rpmb_size_mult.attr,
729 &dev_attr_rel_sectors.attr,
732 ATTRIBUTE_GROUPS(mmc_std);
734 static struct device_type mmc_type = {
735 .groups = mmc_std_groups,
739 * Select the PowerClass for the current bus width
740 * If power class is defined for 4/8 bit bus in the
741 * extended CSD register, select it by executing the
742 * mmc_switch command.
744 static int __mmc_select_powerclass(struct mmc_card *card,
745 unsigned int bus_width)
747 struct mmc_host *host = card->host;
748 struct mmc_ext_csd *ext_csd = &card->ext_csd;
749 unsigned int pwrclass_val = 0;
752 /* Power class selection is supported for versions >= 4.0 */
753 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
756 /* Power class values are defined only for 4/8 bit bus */
757 if (bus_width == EXT_CSD_BUS_WIDTH_1)
760 switch (1 << host->ios.vdd) {
761 case MMC_VDD_165_195:
762 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
763 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
764 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
765 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
766 ext_csd->raw_pwr_cl_52_195 :
767 ext_csd->raw_pwr_cl_ddr_52_195;
768 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
769 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
780 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
781 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
782 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
783 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
784 ext_csd->raw_pwr_cl_52_360 :
785 ext_csd->raw_pwr_cl_ddr_52_360;
786 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
787 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
788 ext_csd->raw_pwr_cl_ddr_200_360 :
789 ext_csd->raw_pwr_cl_200_360;
792 pr_warning("%s: Voltage range not supported "
793 "for power class.\n", mmc_hostname(host));
797 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
798 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
799 EXT_CSD_PWR_CL_8BIT_SHIFT;
801 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
802 EXT_CSD_PWR_CL_4BIT_SHIFT;
804 /* If the power class is different from the default value */
805 if (pwrclass_val > 0) {
806 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
809 card->ext_csd.generic_cmd6_time);
815 static int mmc_select_powerclass(struct mmc_card *card)
817 struct mmc_host *host = card->host;
818 u32 bus_width, ext_csd_bits;
821 /* Power class selection is supported for versions >= 4.0 */
822 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
825 bus_width = host->ios.bus_width;
826 /* Power class values are defined only for 4/8 bit bus */
827 if (bus_width == MMC_BUS_WIDTH_1)
830 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
832 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
833 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
835 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
836 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
838 err = __mmc_select_powerclass(card, ext_csd_bits);
840 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
841 mmc_hostname(host), 1 << bus_width, ddr);
847 * Set the bus speed for the selected speed mode.
849 static void mmc_set_bus_speed(struct mmc_card *card)
851 unsigned int max_dtr = (unsigned int)-1;
853 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
854 max_dtr > card->ext_csd.hs200_max_dtr)
855 max_dtr = card->ext_csd.hs200_max_dtr;
856 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
857 max_dtr = card->ext_csd.hs_max_dtr;
858 else if (max_dtr > card->csd.max_dtr)
859 max_dtr = card->csd.max_dtr;
861 mmc_set_clock(card->host, max_dtr);
865 * Select the bus width amoung 4-bit and 8-bit(SDR).
866 * If the bus width is changed successfully, return the selected width value.
867 * Zero is returned instead of error value if the wide width is not supported.
869 static int mmc_select_bus_width(struct mmc_card *card)
871 static unsigned ext_csd_bits[] = {
875 static unsigned bus_widths[] = {
879 struct mmc_host *host = card->host;
880 unsigned idx, bus_width = 0;
883 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
884 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
887 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
890 * Unlike SD, MMC cards dont have a configuration register to notify
891 * supported bus width. So bus test command should be run to identify
892 * the supported bus width or compare the ext csd values of current
893 * bus width and ext csd values of 1 bit mode read earlier.
895 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
897 * Host is capable of 8bit transfer, then switch
898 * the device to work in 8bit transfer mode. If the
899 * mmc switch command returns error then switch to
900 * 4bit transfer mode. On success set the corresponding
901 * bus width on the host.
903 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
906 card->ext_csd.generic_cmd6_time);
910 bus_width = bus_widths[idx];
911 mmc_set_bus_width(host, bus_width);
914 * If controller can't handle bus width test,
915 * compare ext_csd previously read in 1 bit mode
916 * against ext_csd at new bus width
918 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
919 err = mmc_compare_ext_csds(card, bus_width);
921 err = mmc_bus_test(card, bus_width);
927 pr_warn("%s: switch to bus width %d failed\n",
928 mmc_hostname(host), ext_csd_bits[idx]);
936 * Switch to the high-speed mode
938 static int mmc_select_hs(struct mmc_card *card)
942 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
943 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
944 card->ext_csd.generic_cmd6_time,
947 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
953 * Activate wide bus and DDR if supported.
955 static int mmc_select_hs_ddr(struct mmc_card *card)
957 struct mmc_host *host = card->host;
958 u32 bus_width, ext_csd_bits;
961 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
964 bus_width = host->ios.bus_width;
965 if (bus_width == MMC_BUS_WIDTH_1)
968 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
969 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
971 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
974 card->ext_csd.generic_cmd6_time);
976 pr_warn("%s: switch to bus width %d ddr failed\n",
977 mmc_hostname(host), 1 << bus_width);
982 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
985 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
987 * 1.8V vccq at 3.3V core voltage (vcc) is not required
988 * in the JEDEC spec for DDR.
990 * Do not force change in vccq since we are obviously
991 * working and no change to vccq is needed.
993 * WARNING: eMMC rules are NOT the same as SD DDR
995 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
996 err = __mmc_set_signal_voltage(host,
997 MMC_SIGNAL_VOLTAGE_120);
1002 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1007 static int mmc_select_hs400(struct mmc_card *card)
1009 struct mmc_host *host = card->host;
1013 * HS400 mode requires 8-bit bus width
1015 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1016 host->ios.bus_width == MMC_BUS_WIDTH_8))
1020 * Before switching to dual data rate operation for HS400,
1021 * it is required to convert from HS200 mode to HS mode.
1023 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1024 mmc_set_bus_speed(card);
1026 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1027 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1028 card->ext_csd.generic_cmd6_time,
1031 pr_warn("%s: switch to high-speed from hs200 failed, err:%d\n",
1032 mmc_hostname(host), err);
1036 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1038 EXT_CSD_DDR_BUS_WIDTH_8,
1039 card->ext_csd.generic_cmd6_time);
1041 pr_warn("%s: switch to bus width for hs400 failed, err:%d\n",
1042 mmc_hostname(host), err);
1046 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1047 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400,
1048 card->ext_csd.generic_cmd6_time,
1051 pr_warn("%s: switch to hs400 failed, err:%d\n",
1052 mmc_hostname(host), err);
1056 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1057 mmc_set_bus_speed(card);
1063 * For device supporting HS200 mode, the following sequence
1064 * should be done before executing the tuning process.
1065 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1066 * 2. switch to HS200 mode
1067 * 3. set the clock to > 52Mhz and <=200MHz
1069 static int mmc_select_hs200(struct mmc_card *card)
1071 struct mmc_host *host = card->host;
1074 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1075 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1077 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1078 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1080 /* If fails try again during next card power cycle */
1085 * Set the bus width(4 or 8) with host's support and
1086 * switch to HS200 mode if bus width is set successfully.
1088 err = mmc_select_bus_width(card);
1089 if (!IS_ERR_VALUE(err)) {
1090 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1091 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1092 card->ext_csd.generic_cmd6_time,
1095 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1102 * Activate High Speed or HS200 mode if supported.
1104 static int mmc_select_timing(struct mmc_card *card)
1108 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
1109 card->ext_csd.hs_max_dtr == 0))
1112 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1113 err = mmc_select_hs200(card);
1114 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1115 err = mmc_select_hs(card);
1117 if (err && err != -EBADMSG)
1121 pr_warn("%s: switch to %s failed\n",
1122 mmc_card_hs(card) ? "high-speed" :
1123 (mmc_card_hs200(card) ? "hs200" : ""),
1124 mmc_hostname(card->host));
1130 * Set the bus speed to the selected bus timing.
1131 * If timing is not selected, backward compatible is the default.
1133 mmc_set_bus_speed(card);
1138 * Execute tuning sequence to seek the proper bus operating
1139 * conditions for HS200 and HS400, which sends CMD21 to the device.
1141 static int mmc_hs200_tuning(struct mmc_card *card)
1143 struct mmc_host *host = card->host;
1147 * Timing should be adjusted to the HS400 target
1148 * operation frequency for tuning process
1150 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1151 host->ios.bus_width == MMC_BUS_WIDTH_8)
1152 if (host->ops->prepare_hs400_tuning)
1153 host->ops->prepare_hs400_tuning(host, &host->ios);
1155 if (host->ops->execute_tuning) {
1156 mmc_host_clk_hold(host);
1157 err = host->ops->execute_tuning(host,
1158 MMC_SEND_TUNING_BLOCK_HS200);
1159 mmc_host_clk_release(host);
1162 pr_warn("%s: tuning execution failed\n",
1163 mmc_hostname(host));
1170 * Handle the detection and initialisation of a card.
1172 * In the case of a resume, "oldcard" will contain the card
1173 * we're trying to reinitialise.
1175 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1176 struct mmc_card *oldcard)
1178 struct mmc_card *card;
1185 WARN_ON(!host->claimed);
1187 /* Set correct bus mode for MMC before attempting init */
1188 if (!mmc_host_is_spi(host))
1189 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1192 * Since we're changing the OCR value, we seem to
1193 * need to tell some cards to go back to the idle
1194 * state. We wait 1ms to give cards time to
1196 * mmc_go_idle is needed for eMMC that are asleep
1200 /* The extra bit indicates that we support high capacity */
1201 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1206 * For SPI, enable CRC as appropriate.
1208 if (mmc_host_is_spi(host)) {
1209 err = mmc_spi_set_crc(host, use_spi_crc);
1215 * Fetch CID from card.
1217 if (mmc_host_is_spi(host))
1218 err = mmc_send_cid(host, cid);
1220 err = mmc_all_send_cid(host, cid);
1225 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1233 * Allocate card structure.
1235 card = mmc_alloc_card(host, &mmc_type);
1237 err = PTR_ERR(card);
1242 card->type = MMC_TYPE_MMC;
1244 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1248 * For native busses: set card RCA and quit open drain mode.
1250 if (!mmc_host_is_spi(host)) {
1251 err = mmc_set_relative_addr(card);
1255 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1260 * Fetch CSD from card.
1262 err = mmc_send_csd(card, card->raw_csd);
1266 err = mmc_decode_csd(card);
1269 err = mmc_decode_cid(card);
1275 * Select card, as all following commands rely on that.
1277 if (!mmc_host_is_spi(host)) {
1278 err = mmc_select_card(card);
1285 * Fetch and process extended CSD.
1288 err = mmc_get_ext_csd(card, &ext_csd);
1291 err = mmc_read_ext_csd(card, ext_csd);
1295 /* If doing byte addressing, check if required to do sector
1296 * addressing. Handle the case of <2GB cards needing sector
1297 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1298 * ocr register has bit 30 set for sector addressing.
1300 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1301 mmc_card_set_blockaddr(card);
1303 /* Erase size depends on CSD and Extended CSD */
1304 mmc_set_erase_size(card);
1308 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1309 * bit. This bit will be lost every time after a reset or power off.
1311 if (card->ext_csd.enhanced_area_en ||
1312 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1313 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1314 EXT_CSD_ERASE_GROUP_DEF, 1,
1315 card->ext_csd.generic_cmd6_time);
1317 if (err && err != -EBADMSG)
1323 * Just disable enhanced area off & sz
1324 * will try to enable ERASE_GROUP_DEF
1325 * during next time reinit
1327 card->ext_csd.enhanced_area_offset = -EINVAL;
1328 card->ext_csd.enhanced_area_size = -EINVAL;
1330 card->ext_csd.erase_group_def = 1;
1332 * enable ERASE_GRP_DEF successfully.
1333 * This will affect the erase size, so
1334 * here need to reset erase size
1336 mmc_set_erase_size(card);
1341 * Ensure eMMC user default partition is enabled
1343 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1344 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1345 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1346 card->ext_csd.part_config,
1347 card->ext_csd.part_time);
1348 if (err && err != -EBADMSG)
1353 * Enable power_off_notification byte in the ext_csd register
1355 if (card->ext_csd.rev >= 6) {
1356 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1357 EXT_CSD_POWER_OFF_NOTIFICATION,
1359 card->ext_csd.generic_cmd6_time);
1360 if (err && err != -EBADMSG)
1364 * The err can be -EBADMSG or 0,
1365 * so check for success and update the flag
1368 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1372 * Select timing interface
1374 err = mmc_select_timing(card);
1378 if (mmc_card_hs200(card)) {
1379 err = mmc_hs200_tuning(card);
1383 err = mmc_select_hs400(card);
1386 } else if (mmc_card_hs(card)) {
1387 /* Select the desired bus width optionally */
1388 err = mmc_select_bus_width(card);
1389 if (!IS_ERR_VALUE(err)) {
1390 err = mmc_select_hs_ddr(card);
1397 * Choose the power class with selected bus interface
1399 mmc_select_powerclass(card);
1402 * Enable HPI feature (if supported)
1404 if (card->ext_csd.hpi) {
1405 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1406 EXT_CSD_HPI_MGMT, 1,
1407 card->ext_csd.generic_cmd6_time);
1408 if (err && err != -EBADMSG)
1411 pr_warning("%s: Enabling HPI failed\n",
1412 mmc_hostname(card->host));
1415 card->ext_csd.hpi_en = 1;
1419 * If cache size is higher than 0, this indicates
1420 * the existence of cache and it can be turned on.
1422 if (card->ext_csd.cache_size > 0) {
1423 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1424 EXT_CSD_CACHE_CTRL, 1,
1425 card->ext_csd.generic_cmd6_time);
1426 if (err && err != -EBADMSG)
1430 * Only if no error, cache is turned on successfully.
1433 pr_warning("%s: Cache is supported, "
1434 "but failed to turn on (%d)\n",
1435 mmc_hostname(card->host), err);
1436 card->ext_csd.cache_ctrl = 0;
1439 card->ext_csd.cache_ctrl = 1;
1444 * The mandatory minimum values are defined for packed command.
1447 if (card->ext_csd.max_packed_writes >= 3 &&
1448 card->ext_csd.max_packed_reads >= 5 &&
1449 host->caps2 & MMC_CAP2_PACKED_CMD) {
1450 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1451 EXT_CSD_EXP_EVENTS_CTRL,
1452 EXT_CSD_PACKED_EVENT_EN,
1453 card->ext_csd.generic_cmd6_time);
1454 if (err && err != -EBADMSG)
1457 pr_warn("%s: Enabling packed event failed\n",
1458 mmc_hostname(card->host));
1459 card->ext_csd.packed_event_en = 0;
1462 card->ext_csd.packed_event_en = 1;
1469 mmc_free_ext_csd(ext_csd);
1474 mmc_remove_card(card);
1476 mmc_free_ext_csd(ext_csd);
1481 static int mmc_can_sleep(struct mmc_card *card)
1483 return (card && card->ext_csd.rev >= 3);
1486 static int mmc_sleep(struct mmc_host *host)
1488 struct mmc_command cmd = {0};
1489 struct mmc_card *card = host->card;
1490 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1493 err = mmc_deselect_cards(host);
1497 cmd.opcode = MMC_SLEEP_AWAKE;
1498 cmd.arg = card->rca << 16;
1502 * If the max_busy_timeout of the host is specified, validate it against
1503 * the sleep cmd timeout. A failure means we need to prevent the host
1504 * from doing hw busy detection, which is done by converting to a R1
1505 * response instead of a R1B.
1507 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1508 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1510 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1511 cmd.busy_timeout = timeout_ms;
1514 err = mmc_wait_for_cmd(host, &cmd, 0);
1519 * If the host does not wait while the card signals busy, then we will
1520 * will have to wait the sleep/awake timeout. Note, we cannot use the
1521 * SEND_STATUS command to poll the status because that command (and most
1522 * others) is invalid while the card sleeps.
1524 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1525 mmc_delay(timeout_ms);
1530 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1533 mmc_card_mmc(card) &&
1534 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1537 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1539 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1542 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1543 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1544 timeout = card->ext_csd.power_off_longtime;
1546 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1547 EXT_CSD_POWER_OFF_NOTIFICATION,
1548 notify_type, timeout, true, false, false);
1550 pr_err("%s: Power Off Notification timed out, %u\n",
1551 mmc_hostname(card->host), timeout);
1553 /* Disable the power off notification after the switch operation. */
1554 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1560 * Host is being removed. Free up the current card.
1562 static void mmc_remove(struct mmc_host *host)
1565 BUG_ON(!host->card);
1567 mmc_remove_card(host->card);
1572 * Card detection - card is alive.
1574 static int mmc_alive(struct mmc_host *host)
1576 return mmc_send_status(host->card, NULL);
1580 * Card detection callback from host.
1582 static void mmc_detect(struct mmc_host *host)
1587 BUG_ON(!host->card);
1589 mmc_get_card(host->card);
1592 * Just check if our card has been removed.
1594 err = _mmc_detect_card_removed(host);
1596 mmc_put_card(host->card);
1601 mmc_claim_host(host);
1602 mmc_detach_bus(host);
1603 mmc_power_off(host);
1604 mmc_release_host(host);
1608 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1611 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1612 EXT_CSD_POWER_OFF_LONG;
1615 BUG_ON(!host->card);
1617 mmc_claim_host(host);
1619 if (mmc_card_suspended(host->card))
1622 if (mmc_card_doing_bkops(host->card)) {
1623 err = mmc_stop_bkops(host->card);
1628 err = mmc_flush_cache(host->card);
1632 if (mmc_can_poweroff_notify(host->card) &&
1633 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1634 err = mmc_poweroff_notify(host->card, notify_type);
1635 else if (mmc_can_sleep(host->card))
1636 err = mmc_sleep(host);
1637 else if (!mmc_host_is_spi(host))
1638 err = mmc_deselect_cards(host);
1641 mmc_power_off(host);
1642 mmc_card_set_suspended(host->card);
1645 mmc_release_host(host);
1652 static int mmc_suspend(struct mmc_host *host)
1656 err = _mmc_suspend(host, true);
1658 pm_runtime_disable(&host->card->dev);
1659 pm_runtime_set_suspended(&host->card->dev);
1666 * This function tries to determine if the same card is still present
1667 * and, if so, restore all state to it.
1669 static int _mmc_resume(struct mmc_host *host)
1674 BUG_ON(!host->card);
1676 mmc_claim_host(host);
1678 if (!mmc_card_suspended(host->card))
1681 mmc_power_up(host, host->card->ocr);
1682 err = mmc_init_card(host, host->card->ocr, host->card);
1683 mmc_card_clr_suspended(host->card);
1686 mmc_release_host(host);
1693 static int mmc_shutdown(struct mmc_host *host)
1698 * In a specific case for poweroff notify, we need to resume the card
1699 * before we can shutdown it properly.
1701 if (mmc_can_poweroff_notify(host->card) &&
1702 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1703 err = _mmc_resume(host);
1706 err = _mmc_suspend(host, false);
1712 * Callback for resume.
1714 static int mmc_resume(struct mmc_host *host)
1718 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1719 err = _mmc_resume(host);
1720 pm_runtime_set_active(&host->card->dev);
1721 pm_runtime_mark_last_busy(&host->card->dev);
1723 pm_runtime_enable(&host->card->dev);
1729 * Callback for runtime_suspend.
1731 static int mmc_runtime_suspend(struct mmc_host *host)
1735 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1738 err = _mmc_suspend(host, true);
1740 pr_err("%s: error %d doing aggessive suspend\n",
1741 mmc_hostname(host), err);
1747 * Callback for runtime_resume.
1749 static int mmc_runtime_resume(struct mmc_host *host)
1753 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1756 err = _mmc_resume(host);
1758 pr_err("%s: error %d doing aggessive resume\n",
1759 mmc_hostname(host), err);
1764 static int mmc_power_restore(struct mmc_host *host)
1768 mmc_claim_host(host);
1769 ret = mmc_init_card(host, host->card->ocr, host->card);
1770 mmc_release_host(host);
1775 static const struct mmc_bus_ops mmc_ops = {
1776 .remove = mmc_remove,
1777 .detect = mmc_detect,
1778 .suspend = mmc_suspend,
1779 .resume = mmc_resume,
1780 .runtime_suspend = mmc_runtime_suspend,
1781 .runtime_resume = mmc_runtime_resume,
1782 .power_restore = mmc_power_restore,
1784 .shutdown = mmc_shutdown,
1788 * Starting point for MMC card init.
1790 int mmc_attach_mmc(struct mmc_host *host)
1796 WARN_ON(!host->claimed);
1798 /* Set correct bus mode for MMC before attempting attach */
1799 if (!mmc_host_is_spi(host))
1800 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1802 err = mmc_send_op_cond(host, 0, &ocr);
1806 mmc_attach_bus(host, &mmc_ops);
1807 if (host->ocr_avail_mmc)
1808 host->ocr_avail = host->ocr_avail_mmc;
1811 * We need to get OCR a different way for SPI.
1813 if (mmc_host_is_spi(host)) {
1814 err = mmc_spi_read_ocr(host, 1, &ocr);
1819 rocr = mmc_select_voltage(host, ocr);
1822 * Can we support the voltage of the card?
1830 * Detect and init the card.
1832 err = mmc_init_card(host, rocr, NULL);
1836 mmc_release_host(host);
1837 err = mmc_add_card(host->card);
1838 mmc_claim_host(host);
1845 mmc_release_host(host);
1846 mmc_remove_card(host->card);
1847 mmc_claim_host(host);
1850 mmc_detach_bus(host);
1852 pr_err("%s: error %d whilst initialising MMC card\n",
1853 mmc_hostname(host), err);