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);
327 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
328 if (card->ext_csd.rev > 7) {
329 pr_err("%s: unrecognised EXT_CSD revision %d\n",
330 mmc_hostname(card->host), card->ext_csd.rev);
335 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
336 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
337 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
338 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
339 if (card->ext_csd.rev >= 2) {
340 card->ext_csd.sectors =
341 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
342 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
343 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
344 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
346 /* Cards with density > 2GiB are sector addressed */
347 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
348 mmc_card_set_blockaddr(card);
351 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
352 mmc_select_card_type(card);
354 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
355 card->ext_csd.raw_erase_timeout_mult =
356 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
357 card->ext_csd.raw_hc_erase_grp_size =
358 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
359 if (card->ext_csd.rev >= 3) {
360 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
361 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
363 /* EXT_CSD value is in units of 10ms, but we store in ms */
364 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
366 /* Sleep / awake timeout in 100ns units */
367 if (sa_shift > 0 && sa_shift <= 0x17)
368 card->ext_csd.sa_timeout =
369 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
370 card->ext_csd.erase_group_def =
371 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
372 card->ext_csd.hc_erase_timeout = 300 *
373 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
374 card->ext_csd.hc_erase_size =
375 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
377 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
380 * There are two boot regions of equal size, defined in
383 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
384 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
385 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
386 mmc_part_add(card, part_size,
387 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
389 MMC_BLK_DATA_AREA_BOOT);
394 card->ext_csd.raw_hc_erase_gap_size =
395 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
396 card->ext_csd.raw_sec_trim_mult =
397 ext_csd[EXT_CSD_SEC_TRIM_MULT];
398 card->ext_csd.raw_sec_erase_mult =
399 ext_csd[EXT_CSD_SEC_ERASE_MULT];
400 card->ext_csd.raw_sec_feature_support =
401 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
402 card->ext_csd.raw_trim_mult =
403 ext_csd[EXT_CSD_TRIM_MULT];
404 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
405 if (card->ext_csd.rev >= 4) {
407 * Enhanced area feature support -- check whether the eMMC
408 * card has the Enhanced area enabled. If so, export enhanced
409 * area offset and size to user by adding sysfs interface.
411 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
412 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
414 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
416 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
418 card->ext_csd.enhanced_area_en = 1;
420 * calculate the enhanced data area offset, in bytes
422 card->ext_csd.enhanced_area_offset =
423 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
424 (ext_csd[137] << 8) + ext_csd[136];
425 if (mmc_card_blockaddr(card))
426 card->ext_csd.enhanced_area_offset <<= 9;
428 * calculate the enhanced data area size, in kilobytes
430 card->ext_csd.enhanced_area_size =
431 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
433 card->ext_csd.enhanced_area_size *=
434 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
435 card->ext_csd.enhanced_area_size <<= 9;
438 * If the enhanced area is not enabled, disable these
441 card->ext_csd.enhanced_area_offset = -EINVAL;
442 card->ext_csd.enhanced_area_size = -EINVAL;
446 * General purpose partition feature support --
447 * If ext_csd has the size of general purpose partitions,
448 * set size, part_cfg, partition name in mmc_part.
450 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
451 EXT_CSD_PART_SUPPORT_PART_EN) {
452 if (card->ext_csd.enhanced_area_en != 1) {
454 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
456 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
458 card->ext_csd.enhanced_area_en = 1;
461 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
462 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
463 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
464 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
467 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
469 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
471 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
472 part_size *= (size_t)(hc_erase_grp_sz *
474 mmc_part_add(card, part_size << 19,
475 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
477 MMC_BLK_DATA_AREA_GP);
480 card->ext_csd.sec_trim_mult =
481 ext_csd[EXT_CSD_SEC_TRIM_MULT];
482 card->ext_csd.sec_erase_mult =
483 ext_csd[EXT_CSD_SEC_ERASE_MULT];
484 card->ext_csd.sec_feature_support =
485 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
486 card->ext_csd.trim_timeout = 300 *
487 ext_csd[EXT_CSD_TRIM_MULT];
490 * Note that the call to mmc_part_add above defaults to read
491 * only. If this default assumption is changed, the call must
492 * take into account the value of boot_locked below.
494 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
495 card->ext_csd.boot_ro_lockable = true;
497 /* Save power class values */
498 card->ext_csd.raw_pwr_cl_52_195 =
499 ext_csd[EXT_CSD_PWR_CL_52_195];
500 card->ext_csd.raw_pwr_cl_26_195 =
501 ext_csd[EXT_CSD_PWR_CL_26_195];
502 card->ext_csd.raw_pwr_cl_52_360 =
503 ext_csd[EXT_CSD_PWR_CL_52_360];
504 card->ext_csd.raw_pwr_cl_26_360 =
505 ext_csd[EXT_CSD_PWR_CL_26_360];
506 card->ext_csd.raw_pwr_cl_200_195 =
507 ext_csd[EXT_CSD_PWR_CL_200_195];
508 card->ext_csd.raw_pwr_cl_200_360 =
509 ext_csd[EXT_CSD_PWR_CL_200_360];
510 card->ext_csd.raw_pwr_cl_ddr_52_195 =
511 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
512 card->ext_csd.raw_pwr_cl_ddr_52_360 =
513 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
514 card->ext_csd.raw_pwr_cl_ddr_200_360 =
515 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
518 if (card->ext_csd.rev >= 5) {
519 /* Adjust production date as per JEDEC JESD84-B451 */
520 if (card->cid.year < 2010)
521 card->cid.year += 16;
523 /* check whether the eMMC card supports BKOPS */
524 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
525 card->ext_csd.bkops = 1;
526 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
527 card->ext_csd.raw_bkops_status =
528 ext_csd[EXT_CSD_BKOPS_STATUS];
529 if (!card->ext_csd.bkops_en)
530 pr_info("%s: BKOPS_EN bit is not set\n",
531 mmc_hostname(card->host));
534 /* check whether the eMMC card supports HPI */
535 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
536 card->ext_csd.hpi = 1;
537 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
538 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
540 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
542 * Indicate the maximum timeout to close
543 * a command interrupted by HPI
545 card->ext_csd.out_of_int_time =
546 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
549 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
550 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
553 * RPMB regions are defined in multiples of 128K.
555 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
556 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
557 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
558 EXT_CSD_PART_CONFIG_ACC_RPMB,
560 MMC_BLK_DATA_AREA_RPMB);
564 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
565 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
566 card->erased_byte = 0xFF;
568 card->erased_byte = 0x0;
570 /* eMMC v4.5 or later */
571 if (card->ext_csd.rev >= 6) {
572 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
574 card->ext_csd.generic_cmd6_time = 10 *
575 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
576 card->ext_csd.power_off_longtime = 10 *
577 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
579 card->ext_csd.cache_size =
580 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
581 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
582 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
583 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
585 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
586 card->ext_csd.data_sector_size = 4096;
588 card->ext_csd.data_sector_size = 512;
590 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
591 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
592 card->ext_csd.data_tag_unit_size =
593 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
594 (card->ext_csd.data_sector_size);
596 card->ext_csd.data_tag_unit_size = 0;
599 card->ext_csd.max_packed_writes =
600 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
601 card->ext_csd.max_packed_reads =
602 ext_csd[EXT_CSD_MAX_PACKED_READS];
604 card->ext_csd.data_sector_size = 512;
611 static inline void mmc_free_ext_csd(u8 *ext_csd)
617 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
622 if (bus_width == MMC_BUS_WIDTH_1)
625 err = mmc_get_ext_csd(card, &bw_ext_csd);
627 if (err || bw_ext_csd == NULL) {
632 /* only compare read only fields */
633 err = !((card->ext_csd.raw_partition_support ==
634 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
635 (card->ext_csd.raw_erased_mem_count ==
636 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
637 (card->ext_csd.rev ==
638 bw_ext_csd[EXT_CSD_REV]) &&
639 (card->ext_csd.raw_ext_csd_structure ==
640 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
641 (card->ext_csd.raw_card_type ==
642 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
643 (card->ext_csd.raw_s_a_timeout ==
644 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
645 (card->ext_csd.raw_hc_erase_gap_size ==
646 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
647 (card->ext_csd.raw_erase_timeout_mult ==
648 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
649 (card->ext_csd.raw_hc_erase_grp_size ==
650 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
651 (card->ext_csd.raw_sec_trim_mult ==
652 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
653 (card->ext_csd.raw_sec_erase_mult ==
654 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
655 (card->ext_csd.raw_sec_feature_support ==
656 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
657 (card->ext_csd.raw_trim_mult ==
658 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
659 (card->ext_csd.raw_sectors[0] ==
660 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
661 (card->ext_csd.raw_sectors[1] ==
662 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
663 (card->ext_csd.raw_sectors[2] ==
664 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
665 (card->ext_csd.raw_sectors[3] ==
666 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
667 (card->ext_csd.raw_pwr_cl_52_195 ==
668 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
669 (card->ext_csd.raw_pwr_cl_26_195 ==
670 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
671 (card->ext_csd.raw_pwr_cl_52_360 ==
672 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
673 (card->ext_csd.raw_pwr_cl_26_360 ==
674 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
675 (card->ext_csd.raw_pwr_cl_200_195 ==
676 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
677 (card->ext_csd.raw_pwr_cl_200_360 ==
678 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
679 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
680 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
681 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
682 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
683 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
684 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
690 mmc_free_ext_csd(bw_ext_csd);
694 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
695 card->raw_cid[2], card->raw_cid[3]);
696 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
697 card->raw_csd[2], card->raw_csd[3]);
698 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
699 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
700 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
701 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
702 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
703 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
704 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
705 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
706 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
707 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
708 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
709 card->ext_csd.enhanced_area_offset);
710 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
711 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
712 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
714 static struct attribute *mmc_std_attrs[] = {
718 &dev_attr_erase_size.attr,
719 &dev_attr_preferred_erase_size.attr,
720 &dev_attr_fwrev.attr,
721 &dev_attr_hwrev.attr,
722 &dev_attr_manfid.attr,
724 &dev_attr_oemid.attr,
726 &dev_attr_serial.attr,
727 &dev_attr_enhanced_area_offset.attr,
728 &dev_attr_enhanced_area_size.attr,
729 &dev_attr_raw_rpmb_size_mult.attr,
730 &dev_attr_rel_sectors.attr,
733 ATTRIBUTE_GROUPS(mmc_std);
735 static struct device_type mmc_type = {
736 .groups = mmc_std_groups,
740 * Select the PowerClass for the current bus width
741 * If power class is defined for 4/8 bit bus in the
742 * extended CSD register, select it by executing the
743 * mmc_switch command.
745 static int __mmc_select_powerclass(struct mmc_card *card,
746 unsigned int bus_width)
748 struct mmc_host *host = card->host;
749 struct mmc_ext_csd *ext_csd = &card->ext_csd;
750 unsigned int pwrclass_val = 0;
753 /* Power class selection is supported for versions >= 4.0 */
754 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
757 /* Power class values are defined only for 4/8 bit bus */
758 if (bus_width == EXT_CSD_BUS_WIDTH_1)
761 switch (1 << host->ios.vdd) {
762 case MMC_VDD_165_195:
763 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
764 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
765 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
766 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
767 ext_csd->raw_pwr_cl_52_195 :
768 ext_csd->raw_pwr_cl_ddr_52_195;
769 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
770 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
781 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
782 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
783 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
784 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
785 ext_csd->raw_pwr_cl_52_360 :
786 ext_csd->raw_pwr_cl_ddr_52_360;
787 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
788 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
789 ext_csd->raw_pwr_cl_ddr_200_360 :
790 ext_csd->raw_pwr_cl_200_360;
793 pr_warning("%s: Voltage range not supported "
794 "for power class.\n", mmc_hostname(host));
798 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
799 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
800 EXT_CSD_PWR_CL_8BIT_SHIFT;
802 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
803 EXT_CSD_PWR_CL_4BIT_SHIFT;
805 /* If the power class is different from the default value */
806 if (pwrclass_val > 0) {
807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
810 card->ext_csd.generic_cmd6_time);
816 static int mmc_select_powerclass(struct mmc_card *card)
818 struct mmc_host *host = card->host;
819 u32 bus_width, ext_csd_bits;
822 /* Power class selection is supported for versions >= 4.0 */
823 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
826 bus_width = host->ios.bus_width;
827 /* Power class values are defined only for 4/8 bit bus */
828 if (bus_width == MMC_BUS_WIDTH_1)
831 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
833 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
834 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
836 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
837 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
839 err = __mmc_select_powerclass(card, ext_csd_bits);
841 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
842 mmc_hostname(host), 1 << bus_width, ddr);
848 * Set the bus speed for the selected speed mode.
850 static void mmc_set_bus_speed(struct mmc_card *card)
852 unsigned int max_dtr = (unsigned int)-1;
854 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
855 max_dtr > card->ext_csd.hs200_max_dtr)
856 max_dtr = card->ext_csd.hs200_max_dtr;
857 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
858 max_dtr = card->ext_csd.hs_max_dtr;
859 else if (max_dtr > card->csd.max_dtr)
860 max_dtr = card->csd.max_dtr;
862 mmc_set_clock(card->host, max_dtr);
866 * Select the bus width amoung 4-bit and 8-bit(SDR).
867 * If the bus width is changed successfully, return the selected width value.
868 * Zero is returned instead of error value if the wide width is not supported.
870 static int mmc_select_bus_width(struct mmc_card *card)
872 static unsigned ext_csd_bits[] = {
876 static unsigned bus_widths[] = {
880 struct mmc_host *host = card->host;
881 unsigned idx, bus_width = 0;
884 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
885 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
888 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
891 * Unlike SD, MMC cards dont have a configuration register to notify
892 * supported bus width. So bus test command should be run to identify
893 * the supported bus width or compare the ext csd values of current
894 * bus width and ext csd values of 1 bit mode read earlier.
896 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
898 * Host is capable of 8bit transfer, then switch
899 * the device to work in 8bit transfer mode. If the
900 * mmc switch command returns error then switch to
901 * 4bit transfer mode. On success set the corresponding
902 * bus width on the host.
904 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
907 card->ext_csd.generic_cmd6_time);
911 bus_width = bus_widths[idx];
912 mmc_set_bus_width(host, bus_width);
915 * If controller can't handle bus width test,
916 * compare ext_csd previously read in 1 bit mode
917 * against ext_csd at new bus width
919 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
920 err = mmc_compare_ext_csds(card, bus_width);
922 err = mmc_bus_test(card, bus_width);
928 pr_warn("%s: switch to bus width %d failed\n",
929 mmc_hostname(host), ext_csd_bits[idx]);
937 * Switch to the high-speed mode
939 static int mmc_select_hs(struct mmc_card *card)
943 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
944 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
945 card->ext_csd.generic_cmd6_time,
948 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
954 * Activate wide bus and DDR if supported.
956 static int mmc_select_hs_ddr(struct mmc_card *card)
958 struct mmc_host *host = card->host;
959 u32 bus_width, ext_csd_bits;
962 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
965 bus_width = host->ios.bus_width;
966 if (bus_width == MMC_BUS_WIDTH_1)
969 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
970 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
972 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
975 card->ext_csd.generic_cmd6_time);
977 pr_warn("%s: switch to bus width %d ddr failed\n",
978 mmc_hostname(host), 1 << bus_width);
983 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
986 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
988 * 1.8V vccq at 3.3V core voltage (vcc) is not required
989 * in the JEDEC spec for DDR.
991 * Do not force change in vccq since we are obviously
992 * working and no change to vccq is needed.
994 * WARNING: eMMC rules are NOT the same as SD DDR
996 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
997 err = __mmc_set_signal_voltage(host,
998 MMC_SIGNAL_VOLTAGE_120);
1003 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1008 static int mmc_select_hs400(struct mmc_card *card)
1010 struct mmc_host *host = card->host;
1014 * HS400 mode requires 8-bit bus width
1016 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1017 host->ios.bus_width == MMC_BUS_WIDTH_8))
1021 * Before switching to dual data rate operation for HS400,
1022 * it is required to convert from HS200 mode to HS mode.
1024 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1025 mmc_set_bus_speed(card);
1027 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1028 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1029 card->ext_csd.generic_cmd6_time,
1032 pr_warn("%s: switch to high-speed from hs200 failed, err:%d\n",
1033 mmc_hostname(host), err);
1037 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1039 EXT_CSD_DDR_BUS_WIDTH_8,
1040 card->ext_csd.generic_cmd6_time);
1042 pr_warn("%s: switch to bus width for hs400 failed, err:%d\n",
1043 mmc_hostname(host), err);
1047 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1048 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS400,
1049 card->ext_csd.generic_cmd6_time,
1052 pr_warn("%s: switch to hs400 failed, err:%d\n",
1053 mmc_hostname(host), err);
1057 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1058 mmc_set_bus_speed(card);
1064 * For device supporting HS200 mode, the following sequence
1065 * should be done before executing the tuning process.
1066 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1067 * 2. switch to HS200 mode
1068 * 3. set the clock to > 52Mhz and <=200MHz
1070 static int mmc_select_hs200(struct mmc_card *card)
1072 struct mmc_host *host = card->host;
1075 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1076 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1078 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1079 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1081 /* If fails try again during next card power cycle */
1086 * Set the bus width(4 or 8) with host's support and
1087 * switch to HS200 mode if bus width is set successfully.
1089 err = mmc_select_bus_width(card);
1090 if (!IS_ERR_VALUE(err)) {
1091 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1092 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1093 card->ext_csd.generic_cmd6_time,
1096 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1103 * Activate High Speed or HS200 mode if supported.
1105 static int mmc_select_timing(struct mmc_card *card)
1109 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
1110 card->ext_csd.hs_max_dtr == 0))
1113 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1114 err = mmc_select_hs200(card);
1115 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1116 err = mmc_select_hs(card);
1118 if (err && err != -EBADMSG)
1122 pr_warn("%s: switch to %s failed\n",
1123 mmc_card_hs(card) ? "high-speed" :
1124 (mmc_card_hs200(card) ? "hs200" : ""),
1125 mmc_hostname(card->host));
1131 * Set the bus speed to the selected bus timing.
1132 * If timing is not selected, backward compatible is the default.
1134 mmc_set_bus_speed(card);
1139 * Execute tuning sequence to seek the proper bus operating
1140 * conditions for HS200 and HS400, which sends CMD21 to the device.
1142 static int mmc_hs200_tuning(struct mmc_card *card)
1144 struct mmc_host *host = card->host;
1148 * Timing should be adjusted to the HS400 target
1149 * operation frequency for tuning process
1151 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1152 host->ios.bus_width == MMC_BUS_WIDTH_8)
1153 if (host->ops->prepare_hs400_tuning)
1154 host->ops->prepare_hs400_tuning(host, &host->ios);
1156 if (host->ops->execute_tuning) {
1157 mmc_host_clk_hold(host);
1158 err = host->ops->execute_tuning(host,
1159 MMC_SEND_TUNING_BLOCK_HS200);
1160 mmc_host_clk_release(host);
1163 pr_warn("%s: tuning execution failed\n",
1164 mmc_hostname(host));
1171 * Handle the detection and initialisation of a card.
1173 * In the case of a resume, "oldcard" will contain the card
1174 * we're trying to reinitialise.
1176 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1177 struct mmc_card *oldcard)
1179 struct mmc_card *card;
1186 WARN_ON(!host->claimed);
1188 /* Set correct bus mode for MMC before attempting init */
1189 if (!mmc_host_is_spi(host))
1190 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1193 * Since we're changing the OCR value, we seem to
1194 * need to tell some cards to go back to the idle
1195 * state. We wait 1ms to give cards time to
1197 * mmc_go_idle is needed for eMMC that are asleep
1201 /* The extra bit indicates that we support high capacity */
1202 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1207 * For SPI, enable CRC as appropriate.
1209 if (mmc_host_is_spi(host)) {
1210 err = mmc_spi_set_crc(host, use_spi_crc);
1216 * Fetch CID from card.
1218 if (mmc_host_is_spi(host))
1219 err = mmc_send_cid(host, cid);
1221 err = mmc_all_send_cid(host, cid);
1226 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1234 * Allocate card structure.
1236 card = mmc_alloc_card(host, &mmc_type);
1238 err = PTR_ERR(card);
1243 card->type = MMC_TYPE_MMC;
1245 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1249 * For native busses: set card RCA and quit open drain mode.
1251 if (!mmc_host_is_spi(host)) {
1252 err = mmc_set_relative_addr(card);
1256 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1261 * Fetch CSD from card.
1263 err = mmc_send_csd(card, card->raw_csd);
1267 err = mmc_decode_csd(card);
1270 err = mmc_decode_cid(card);
1276 * Select card, as all following commands rely on that.
1278 if (!mmc_host_is_spi(host)) {
1279 err = mmc_select_card(card);
1286 * Fetch and process extended CSD.
1289 err = mmc_get_ext_csd(card, &ext_csd);
1292 err = mmc_read_ext_csd(card, ext_csd);
1296 /* If doing byte addressing, check if required to do sector
1297 * addressing. Handle the case of <2GB cards needing sector
1298 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1299 * ocr register has bit 30 set for sector addressing.
1301 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1302 mmc_card_set_blockaddr(card);
1304 /* Erase size depends on CSD and Extended CSD */
1305 mmc_set_erase_size(card);
1309 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1310 * bit. This bit will be lost every time after a reset or power off.
1312 if (card->ext_csd.enhanced_area_en ||
1313 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1314 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1315 EXT_CSD_ERASE_GROUP_DEF, 1,
1316 card->ext_csd.generic_cmd6_time);
1318 if (err && err != -EBADMSG)
1324 * Just disable enhanced area off & sz
1325 * will try to enable ERASE_GROUP_DEF
1326 * during next time reinit
1328 card->ext_csd.enhanced_area_offset = -EINVAL;
1329 card->ext_csd.enhanced_area_size = -EINVAL;
1331 card->ext_csd.erase_group_def = 1;
1333 * enable ERASE_GRP_DEF successfully.
1334 * This will affect the erase size, so
1335 * here need to reset erase size
1337 mmc_set_erase_size(card);
1342 * Ensure eMMC user default partition is enabled
1344 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1345 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1346 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1347 card->ext_csd.part_config,
1348 card->ext_csd.part_time);
1349 if (err && err != -EBADMSG)
1354 * Enable power_off_notification byte in the ext_csd register
1356 if (card->ext_csd.rev >= 6) {
1357 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1358 EXT_CSD_POWER_OFF_NOTIFICATION,
1360 card->ext_csd.generic_cmd6_time);
1361 if (err && err != -EBADMSG)
1365 * The err can be -EBADMSG or 0,
1366 * so check for success and update the flag
1369 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1373 * Select timing interface
1375 err = mmc_select_timing(card);
1379 if (mmc_card_hs200(card)) {
1380 err = mmc_hs200_tuning(card);
1384 err = mmc_select_hs400(card);
1387 } else if (mmc_card_hs(card)) {
1388 /* Select the desired bus width optionally */
1389 err = mmc_select_bus_width(card);
1390 if (!IS_ERR_VALUE(err)) {
1391 err = mmc_select_hs_ddr(card);
1398 * Choose the power class with selected bus interface
1400 mmc_select_powerclass(card);
1403 * Enable HPI feature (if supported)
1405 if (card->ext_csd.hpi) {
1406 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1407 EXT_CSD_HPI_MGMT, 1,
1408 card->ext_csd.generic_cmd6_time);
1409 if (err && err != -EBADMSG)
1412 pr_warning("%s: Enabling HPI failed\n",
1413 mmc_hostname(card->host));
1416 card->ext_csd.hpi_en = 1;
1420 * If cache size is higher than 0, this indicates
1421 * the existence of cache and it can be turned on.
1423 if (card->ext_csd.cache_size > 0) {
1424 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1425 EXT_CSD_CACHE_CTRL, 1,
1426 card->ext_csd.generic_cmd6_time);
1427 if (err && err != -EBADMSG)
1431 * Only if no error, cache is turned on successfully.
1434 pr_warning("%s: Cache is supported, "
1435 "but failed to turn on (%d)\n",
1436 mmc_hostname(card->host), err);
1437 card->ext_csd.cache_ctrl = 0;
1440 card->ext_csd.cache_ctrl = 1;
1445 * The mandatory minimum values are defined for packed command.
1448 if (card->ext_csd.max_packed_writes >= 3 &&
1449 card->ext_csd.max_packed_reads >= 5 &&
1450 host->caps2 & MMC_CAP2_PACKED_CMD) {
1451 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1452 EXT_CSD_EXP_EVENTS_CTRL,
1453 EXT_CSD_PACKED_EVENT_EN,
1454 card->ext_csd.generic_cmd6_time);
1455 if (err && err != -EBADMSG)
1458 pr_warn("%s: Enabling packed event failed\n",
1459 mmc_hostname(card->host));
1460 card->ext_csd.packed_event_en = 0;
1463 card->ext_csd.packed_event_en = 1;
1470 mmc_free_ext_csd(ext_csd);
1475 mmc_remove_card(card);
1477 mmc_free_ext_csd(ext_csd);
1482 static int mmc_can_sleep(struct mmc_card *card)
1484 return (card && card->ext_csd.rev >= 3);
1487 static int mmc_sleep(struct mmc_host *host)
1489 struct mmc_command cmd = {0};
1490 struct mmc_card *card = host->card;
1491 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1494 err = mmc_deselect_cards(host);
1498 cmd.opcode = MMC_SLEEP_AWAKE;
1499 cmd.arg = card->rca << 16;
1503 * If the max_busy_timeout of the host is specified, validate it against
1504 * the sleep cmd timeout. A failure means we need to prevent the host
1505 * from doing hw busy detection, which is done by converting to a R1
1506 * response instead of a R1B.
1508 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1509 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1511 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1512 cmd.busy_timeout = timeout_ms;
1515 err = mmc_wait_for_cmd(host, &cmd, 0);
1520 * If the host does not wait while the card signals busy, then we will
1521 * will have to wait the sleep/awake timeout. Note, we cannot use the
1522 * SEND_STATUS command to poll the status because that command (and most
1523 * others) is invalid while the card sleeps.
1525 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1526 mmc_delay(timeout_ms);
1531 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1534 mmc_card_mmc(card) &&
1535 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1538 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1540 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1543 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1544 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1545 timeout = card->ext_csd.power_off_longtime;
1547 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1548 EXT_CSD_POWER_OFF_NOTIFICATION,
1549 notify_type, timeout, true, false, false);
1551 pr_err("%s: Power Off Notification timed out, %u\n",
1552 mmc_hostname(card->host), timeout);
1554 /* Disable the power off notification after the switch operation. */
1555 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1561 * Host is being removed. Free up the current card.
1563 static void mmc_remove(struct mmc_host *host)
1566 BUG_ON(!host->card);
1568 mmc_remove_card(host->card);
1573 * Card detection - card is alive.
1575 static int mmc_alive(struct mmc_host *host)
1577 return mmc_send_status(host->card, NULL);
1581 * Card detection callback from host.
1583 static void mmc_detect(struct mmc_host *host)
1588 BUG_ON(!host->card);
1590 mmc_get_card(host->card);
1593 * Just check if our card has been removed.
1595 err = _mmc_detect_card_removed(host);
1597 mmc_put_card(host->card);
1602 mmc_claim_host(host);
1603 mmc_detach_bus(host);
1604 mmc_power_off(host);
1605 mmc_release_host(host);
1609 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1612 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1613 EXT_CSD_POWER_OFF_LONG;
1616 BUG_ON(!host->card);
1618 mmc_claim_host(host);
1620 if (mmc_card_suspended(host->card))
1623 if (mmc_card_doing_bkops(host->card)) {
1624 err = mmc_stop_bkops(host->card);
1629 err = mmc_flush_cache(host->card);
1633 if (mmc_can_poweroff_notify(host->card) &&
1634 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1635 err = mmc_poweroff_notify(host->card, notify_type);
1636 else if (mmc_can_sleep(host->card))
1637 err = mmc_sleep(host);
1638 else if (!mmc_host_is_spi(host))
1639 err = mmc_deselect_cards(host);
1642 mmc_power_off(host);
1643 mmc_card_set_suspended(host->card);
1646 mmc_release_host(host);
1653 static int mmc_suspend(struct mmc_host *host)
1657 err = _mmc_suspend(host, true);
1659 pm_runtime_disable(&host->card->dev);
1660 pm_runtime_set_suspended(&host->card->dev);
1667 * This function tries to determine if the same card is still present
1668 * and, if so, restore all state to it.
1670 static int _mmc_resume(struct mmc_host *host)
1675 BUG_ON(!host->card);
1677 mmc_claim_host(host);
1679 if (!mmc_card_suspended(host->card))
1682 mmc_power_up(host, host->card->ocr);
1683 err = mmc_init_card(host, host->card->ocr, host->card);
1684 mmc_card_clr_suspended(host->card);
1687 mmc_release_host(host);
1694 static int mmc_shutdown(struct mmc_host *host)
1699 * In a specific case for poweroff notify, we need to resume the card
1700 * before we can shutdown it properly.
1702 if (mmc_can_poweroff_notify(host->card) &&
1703 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1704 err = _mmc_resume(host);
1707 err = _mmc_suspend(host, false);
1713 * Callback for resume.
1715 static int mmc_resume(struct mmc_host *host)
1719 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1720 err = _mmc_resume(host);
1721 pm_runtime_set_active(&host->card->dev);
1722 pm_runtime_mark_last_busy(&host->card->dev);
1724 pm_runtime_enable(&host->card->dev);
1730 * Callback for runtime_suspend.
1732 static int mmc_runtime_suspend(struct mmc_host *host)
1736 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1739 err = _mmc_suspend(host, true);
1741 pr_err("%s: error %d doing aggessive suspend\n",
1742 mmc_hostname(host), err);
1748 * Callback for runtime_resume.
1750 static int mmc_runtime_resume(struct mmc_host *host)
1754 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1757 err = _mmc_resume(host);
1759 pr_err("%s: error %d doing aggessive resume\n",
1760 mmc_hostname(host), err);
1765 static int mmc_power_restore(struct mmc_host *host)
1769 mmc_claim_host(host);
1770 ret = mmc_init_card(host, host->card->ocr, host->card);
1771 mmc_release_host(host);
1776 static const struct mmc_bus_ops mmc_ops = {
1777 .remove = mmc_remove,
1778 .detect = mmc_detect,
1779 .suspend = mmc_suspend,
1780 .resume = mmc_resume,
1781 .runtime_suspend = mmc_runtime_suspend,
1782 .runtime_resume = mmc_runtime_resume,
1783 .power_restore = mmc_power_restore,
1785 .shutdown = mmc_shutdown,
1789 * Starting point for MMC card init.
1791 int mmc_attach_mmc(struct mmc_host *host)
1797 WARN_ON(!host->claimed);
1799 /* Set correct bus mode for MMC before attempting attach */
1800 if (!mmc_host_is_spi(host))
1801 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1803 err = mmc_send_op_cond(host, 0, &ocr);
1807 mmc_attach_bus(host, &mmc_ops);
1808 if (host->ocr_avail_mmc)
1809 host->ocr_avail = host->ocr_avail_mmc;
1812 * We need to get OCR a different way for SPI.
1814 if (mmc_host_is_spi(host)) {
1815 err = mmc_spi_read_ocr(host, 1, &ocr);
1820 rocr = mmc_select_voltage(host, ocr);
1823 * Can we support the voltage of the card?
1831 * Detect and init the card.
1833 err = mmc_init_card(host, rocr, NULL);
1837 mmc_release_host(host);
1838 err = mmc_add_card(host->card);
1839 mmc_claim_host(host);
1846 mmc_release_host(host);
1847 mmc_remove_card(host->card);
1848 mmc_claim_host(host);
1851 mmc_detach_bus(host);
1853 pr_err("%s: error %d whilst initialising MMC card\n",
1854 mmc_hostname(host), err);