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 & EXT_CSD_CARD_TYPE_MASK;
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 card->ext_csd.hs_max_dtr = hs_max_dtr;
285 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
286 card->mmc_avail_type = avail_type;
290 * Decode extended CSD.
292 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
295 unsigned int part_size;
296 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
303 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
304 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
305 if (card->csd.structure == 3) {
306 if (card->ext_csd.raw_ext_csd_structure > 2) {
307 pr_err("%s: unrecognised EXT_CSD structure "
308 "version %d\n", mmc_hostname(card->host),
309 card->ext_csd.raw_ext_csd_structure);
315 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
316 if (card->ext_csd.rev > 7) {
317 pr_err("%s: unrecognised EXT_CSD revision %d\n",
318 mmc_hostname(card->host), card->ext_csd.rev);
323 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
324 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
325 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
326 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
327 if (card->ext_csd.rev >= 2) {
328 card->ext_csd.sectors =
329 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
330 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
331 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
332 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
334 /* Cards with density > 2GiB are sector addressed */
335 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
336 mmc_card_set_blockaddr(card);
339 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
340 mmc_select_card_type(card);
342 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
343 card->ext_csd.raw_erase_timeout_mult =
344 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
345 card->ext_csd.raw_hc_erase_grp_size =
346 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
347 if (card->ext_csd.rev >= 3) {
348 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
349 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
351 /* EXT_CSD value is in units of 10ms, but we store in ms */
352 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
354 /* Sleep / awake timeout in 100ns units */
355 if (sa_shift > 0 && sa_shift <= 0x17)
356 card->ext_csd.sa_timeout =
357 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
358 card->ext_csd.erase_group_def =
359 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
360 card->ext_csd.hc_erase_timeout = 300 *
361 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
362 card->ext_csd.hc_erase_size =
363 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
365 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
368 * There are two boot regions of equal size, defined in
371 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
372 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
373 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
374 mmc_part_add(card, part_size,
375 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
377 MMC_BLK_DATA_AREA_BOOT);
382 card->ext_csd.raw_hc_erase_gap_size =
383 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
384 card->ext_csd.raw_sec_trim_mult =
385 ext_csd[EXT_CSD_SEC_TRIM_MULT];
386 card->ext_csd.raw_sec_erase_mult =
387 ext_csd[EXT_CSD_SEC_ERASE_MULT];
388 card->ext_csd.raw_sec_feature_support =
389 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
390 card->ext_csd.raw_trim_mult =
391 ext_csd[EXT_CSD_TRIM_MULT];
392 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
393 if (card->ext_csd.rev >= 4) {
395 * Enhanced area feature support -- check whether the eMMC
396 * card has the Enhanced area enabled. If so, export enhanced
397 * area offset and size to user by adding sysfs interface.
399 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
400 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
402 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
404 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
406 card->ext_csd.enhanced_area_en = 1;
408 * calculate the enhanced data area offset, in bytes
410 card->ext_csd.enhanced_area_offset =
411 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
412 (ext_csd[137] << 8) + ext_csd[136];
413 if (mmc_card_blockaddr(card))
414 card->ext_csd.enhanced_area_offset <<= 9;
416 * calculate the enhanced data area size, in kilobytes
418 card->ext_csd.enhanced_area_size =
419 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
421 card->ext_csd.enhanced_area_size *=
422 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
423 card->ext_csd.enhanced_area_size <<= 9;
426 * If the enhanced area is not enabled, disable these
429 card->ext_csd.enhanced_area_offset = -EINVAL;
430 card->ext_csd.enhanced_area_size = -EINVAL;
434 * General purpose partition feature support --
435 * If ext_csd has the size of general purpose partitions,
436 * set size, part_cfg, partition name in mmc_part.
438 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
439 EXT_CSD_PART_SUPPORT_PART_EN) {
440 if (card->ext_csd.enhanced_area_en != 1) {
442 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
444 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
446 card->ext_csd.enhanced_area_en = 1;
449 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
450 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
451 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
452 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
455 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
457 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
459 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
460 part_size *= (size_t)(hc_erase_grp_sz *
462 mmc_part_add(card, part_size << 19,
463 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
465 MMC_BLK_DATA_AREA_GP);
468 card->ext_csd.sec_trim_mult =
469 ext_csd[EXT_CSD_SEC_TRIM_MULT];
470 card->ext_csd.sec_erase_mult =
471 ext_csd[EXT_CSD_SEC_ERASE_MULT];
472 card->ext_csd.sec_feature_support =
473 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
474 card->ext_csd.trim_timeout = 300 *
475 ext_csd[EXT_CSD_TRIM_MULT];
478 * Note that the call to mmc_part_add above defaults to read
479 * only. If this default assumption is changed, the call must
480 * take into account the value of boot_locked below.
482 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
483 card->ext_csd.boot_ro_lockable = true;
485 /* Save power class values */
486 card->ext_csd.raw_pwr_cl_52_195 =
487 ext_csd[EXT_CSD_PWR_CL_52_195];
488 card->ext_csd.raw_pwr_cl_26_195 =
489 ext_csd[EXT_CSD_PWR_CL_26_195];
490 card->ext_csd.raw_pwr_cl_52_360 =
491 ext_csd[EXT_CSD_PWR_CL_52_360];
492 card->ext_csd.raw_pwr_cl_26_360 =
493 ext_csd[EXT_CSD_PWR_CL_26_360];
494 card->ext_csd.raw_pwr_cl_200_195 =
495 ext_csd[EXT_CSD_PWR_CL_200_195];
496 card->ext_csd.raw_pwr_cl_200_360 =
497 ext_csd[EXT_CSD_PWR_CL_200_360];
498 card->ext_csd.raw_pwr_cl_ddr_52_195 =
499 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
500 card->ext_csd.raw_pwr_cl_ddr_52_360 =
501 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
504 if (card->ext_csd.rev >= 5) {
505 /* Adjust production date as per JEDEC JESD84-B451 */
506 if (card->cid.year < 2010)
507 card->cid.year += 16;
509 /* check whether the eMMC card supports BKOPS */
510 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
511 card->ext_csd.bkops = 1;
512 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
513 card->ext_csd.raw_bkops_status =
514 ext_csd[EXT_CSD_BKOPS_STATUS];
515 if (!card->ext_csd.bkops_en)
516 pr_info("%s: BKOPS_EN bit is not set\n",
517 mmc_hostname(card->host));
520 /* check whether the eMMC card supports HPI */
521 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
522 card->ext_csd.hpi = 1;
523 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
524 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
526 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
528 * Indicate the maximum timeout to close
529 * a command interrupted by HPI
531 card->ext_csd.out_of_int_time =
532 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
535 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
536 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
539 * RPMB regions are defined in multiples of 128K.
541 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
542 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
543 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
544 EXT_CSD_PART_CONFIG_ACC_RPMB,
546 MMC_BLK_DATA_AREA_RPMB);
550 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
551 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
552 card->erased_byte = 0xFF;
554 card->erased_byte = 0x0;
556 /* eMMC v4.5 or later */
557 if (card->ext_csd.rev >= 6) {
558 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
560 card->ext_csd.generic_cmd6_time = 10 *
561 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
562 card->ext_csd.power_off_longtime = 10 *
563 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
565 card->ext_csd.cache_size =
566 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
567 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
568 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
569 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
571 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
572 card->ext_csd.data_sector_size = 4096;
574 card->ext_csd.data_sector_size = 512;
576 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
577 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
578 card->ext_csd.data_tag_unit_size =
579 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
580 (card->ext_csd.data_sector_size);
582 card->ext_csd.data_tag_unit_size = 0;
585 card->ext_csd.max_packed_writes =
586 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
587 card->ext_csd.max_packed_reads =
588 ext_csd[EXT_CSD_MAX_PACKED_READS];
590 card->ext_csd.data_sector_size = 512;
597 static inline void mmc_free_ext_csd(u8 *ext_csd)
603 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
608 if (bus_width == MMC_BUS_WIDTH_1)
611 err = mmc_get_ext_csd(card, &bw_ext_csd);
613 if (err || bw_ext_csd == NULL) {
618 /* only compare read only fields */
619 err = !((card->ext_csd.raw_partition_support ==
620 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
621 (card->ext_csd.raw_erased_mem_count ==
622 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
623 (card->ext_csd.rev ==
624 bw_ext_csd[EXT_CSD_REV]) &&
625 (card->ext_csd.raw_ext_csd_structure ==
626 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
627 (card->ext_csd.raw_card_type ==
628 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
629 (card->ext_csd.raw_s_a_timeout ==
630 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
631 (card->ext_csd.raw_hc_erase_gap_size ==
632 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
633 (card->ext_csd.raw_erase_timeout_mult ==
634 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
635 (card->ext_csd.raw_hc_erase_grp_size ==
636 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
637 (card->ext_csd.raw_sec_trim_mult ==
638 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
639 (card->ext_csd.raw_sec_erase_mult ==
640 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
641 (card->ext_csd.raw_sec_feature_support ==
642 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
643 (card->ext_csd.raw_trim_mult ==
644 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
645 (card->ext_csd.raw_sectors[0] ==
646 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
647 (card->ext_csd.raw_sectors[1] ==
648 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
649 (card->ext_csd.raw_sectors[2] ==
650 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
651 (card->ext_csd.raw_sectors[3] ==
652 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
653 (card->ext_csd.raw_pwr_cl_52_195 ==
654 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
655 (card->ext_csd.raw_pwr_cl_26_195 ==
656 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
657 (card->ext_csd.raw_pwr_cl_52_360 ==
658 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
659 (card->ext_csd.raw_pwr_cl_26_360 ==
660 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
661 (card->ext_csd.raw_pwr_cl_200_195 ==
662 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
663 (card->ext_csd.raw_pwr_cl_200_360 ==
664 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
665 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
666 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
667 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
668 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]));
673 mmc_free_ext_csd(bw_ext_csd);
677 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
678 card->raw_cid[2], card->raw_cid[3]);
679 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
680 card->raw_csd[2], card->raw_csd[3]);
681 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
682 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
683 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
684 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
685 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
686 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
687 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
688 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
689 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
690 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
691 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
692 card->ext_csd.enhanced_area_offset);
693 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
694 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
695 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
697 static struct attribute *mmc_std_attrs[] = {
701 &dev_attr_erase_size.attr,
702 &dev_attr_preferred_erase_size.attr,
703 &dev_attr_fwrev.attr,
704 &dev_attr_hwrev.attr,
705 &dev_attr_manfid.attr,
707 &dev_attr_oemid.attr,
709 &dev_attr_serial.attr,
710 &dev_attr_enhanced_area_offset.attr,
711 &dev_attr_enhanced_area_size.attr,
712 &dev_attr_raw_rpmb_size_mult.attr,
713 &dev_attr_rel_sectors.attr,
716 ATTRIBUTE_GROUPS(mmc_std);
718 static struct device_type mmc_type = {
719 .groups = mmc_std_groups,
723 * Select the PowerClass for the current bus width
724 * If power class is defined for 4/8 bit bus in the
725 * extended CSD register, select it by executing the
726 * mmc_switch command.
728 static int __mmc_select_powerclass(struct mmc_card *card,
729 unsigned int bus_width)
731 struct mmc_host *host = card->host;
732 struct mmc_ext_csd *ext_csd = &card->ext_csd;
733 unsigned int pwrclass_val = 0;
736 /* Power class selection is supported for versions >= 4.0 */
737 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
740 /* Power class values are defined only for 4/8 bit bus */
741 if (bus_width == EXT_CSD_BUS_WIDTH_1)
744 switch (1 << host->ios.vdd) {
745 case MMC_VDD_165_195:
746 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
747 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
748 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
749 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
750 ext_csd->raw_pwr_cl_52_195 :
751 ext_csd->raw_pwr_cl_ddr_52_195;
752 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
753 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
764 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
765 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
766 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
767 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
768 ext_csd->raw_pwr_cl_52_360 :
769 ext_csd->raw_pwr_cl_ddr_52_360;
770 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
771 pwrclass_val = ext_csd->raw_pwr_cl_200_360;
774 pr_warning("%s: Voltage range not supported "
775 "for power class.\n", mmc_hostname(host));
779 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
780 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
781 EXT_CSD_PWR_CL_8BIT_SHIFT;
783 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
784 EXT_CSD_PWR_CL_4BIT_SHIFT;
786 /* If the power class is different from the default value */
787 if (pwrclass_val > 0) {
788 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
791 card->ext_csd.generic_cmd6_time);
797 static int mmc_select_powerclass(struct mmc_card *card)
799 struct mmc_host *host = card->host;
800 u32 bus_width, ext_csd_bits;
803 /* Power class selection is supported for versions >= 4.0 */
804 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
807 bus_width = host->ios.bus_width;
808 /* Power class values are defined only for 4/8 bit bus */
809 if (bus_width == MMC_BUS_WIDTH_1)
812 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
814 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
815 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
817 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
818 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
820 err = __mmc_select_powerclass(card, ext_csd_bits);
822 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
823 mmc_hostname(host), 1 << bus_width, ddr);
829 * Set the bus speed for the selected speed mode.
831 static void mmc_set_bus_speed(struct mmc_card *card)
833 unsigned int max_dtr = (unsigned int)-1;
835 if (mmc_card_hs200(card) && max_dtr > card->ext_csd.hs200_max_dtr)
836 max_dtr = card->ext_csd.hs200_max_dtr;
837 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
838 max_dtr = card->ext_csd.hs_max_dtr;
839 else if (max_dtr > card->csd.max_dtr)
840 max_dtr = card->csd.max_dtr;
842 mmc_set_clock(card->host, max_dtr);
846 * Select the bus width amoung 4-bit and 8-bit(SDR).
847 * If the bus width is changed successfully, return the selected width value.
848 * Zero is returned instead of error value if the wide width is not supported.
850 static int mmc_select_bus_width(struct mmc_card *card)
852 static unsigned ext_csd_bits[] = {
856 static unsigned bus_widths[] = {
860 struct mmc_host *host = card->host;
861 unsigned idx, bus_width = 0;
864 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4) &&
865 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
868 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
871 * Unlike SD, MMC cards dont have a configuration register to notify
872 * supported bus width. So bus test command should be run to identify
873 * the supported bus width or compare the ext csd values of current
874 * bus width and ext csd values of 1 bit mode read earlier.
876 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
878 * Host is capable of 8bit transfer, then switch
879 * the device to work in 8bit transfer mode. If the
880 * mmc switch command returns error then switch to
881 * 4bit transfer mode. On success set the corresponding
882 * bus width on the host.
884 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
887 card->ext_csd.generic_cmd6_time);
891 bus_width = bus_widths[idx];
892 mmc_set_bus_width(host, bus_width);
895 * If controller can't handle bus width test,
896 * compare ext_csd previously read in 1 bit mode
897 * against ext_csd at new bus width
899 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
900 err = mmc_compare_ext_csds(card, bus_width);
902 err = mmc_bus_test(card, bus_width);
908 pr_warn("%s: switch to bus width %d failed\n",
909 mmc_hostname(host), ext_csd_bits[idx]);
917 * Switch to the high-speed mode
919 static int mmc_select_hs(struct mmc_card *card)
923 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
924 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
925 card->ext_csd.generic_cmd6_time,
928 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
934 * Activate wide bus and DDR if supported.
936 static int mmc_select_hs_ddr(struct mmc_card *card)
938 struct mmc_host *host = card->host;
939 u32 bus_width, ext_csd_bits;
942 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
945 bus_width = host->ios.bus_width;
946 if (bus_width == MMC_BUS_WIDTH_1)
949 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
950 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
952 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
955 card->ext_csd.generic_cmd6_time);
957 pr_warn("%s: switch to bus width %d ddr failed\n",
958 mmc_hostname(host), 1 << bus_width);
963 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
966 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
968 * 1.8V vccq at 3.3V core voltage (vcc) is not required
969 * in the JEDEC spec for DDR.
971 * Do not force change in vccq since we are obviously
972 * working and no change to vccq is needed.
974 * WARNING: eMMC rules are NOT the same as SD DDR
976 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
977 err = __mmc_set_signal_voltage(host,
978 MMC_SIGNAL_VOLTAGE_120);
983 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
989 * For device supporting HS200 mode, the following sequence
990 * should be done before executing the tuning process.
991 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
992 * 2. switch to HS200 mode
993 * 3. set the clock to > 52Mhz and <=200MHz
995 static int mmc_select_hs200(struct mmc_card *card)
997 struct mmc_host *host = card->host;
1000 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1001 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1003 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1004 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1006 /* If fails try again during next card power cycle */
1011 * Set the bus width(4 or 8) with host's support and
1012 * switch to HS200 mode if bus width is set successfully.
1014 err = mmc_select_bus_width(card);
1015 if (!IS_ERR_VALUE(err)) {
1016 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1017 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS200,
1018 card->ext_csd.generic_cmd6_time,
1021 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1028 * Activate High Speed or HS200 mode if supported.
1030 static int mmc_select_timing(struct mmc_card *card)
1034 if ((card->csd.mmca_vsn < CSD_SPEC_VER_4 &&
1035 card->ext_csd.hs_max_dtr == 0))
1038 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1039 err = mmc_select_hs200(card);
1040 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1041 err = mmc_select_hs(card);
1043 if (err && err != -EBADMSG)
1047 pr_warn("%s: switch to %s failed\n",
1048 mmc_card_hs(card) ? "high-speed" :
1049 (mmc_card_hs200(card) ? "hs200" : ""),
1050 mmc_hostname(card->host));
1056 * Set the bus speed to the selected bus timing.
1057 * If timing is not selected, backward compatible is the default.
1059 mmc_set_bus_speed(card);
1064 * Execute tuning sequence to seek the proper bus operating
1065 * conditions for HS200, which sends CMD21 to the device.
1067 static int mmc_hs200_tuning(struct mmc_card *card)
1069 struct mmc_host *host = card->host;
1072 if (host->ops->execute_tuning) {
1073 mmc_host_clk_hold(host);
1074 err = host->ops->execute_tuning(host,
1075 MMC_SEND_TUNING_BLOCK_HS200);
1076 mmc_host_clk_release(host);
1079 pr_warn("%s: tuning execution failed\n",
1080 mmc_hostname(host));
1087 * Handle the detection and initialisation of a card.
1089 * In the case of a resume, "oldcard" will contain the card
1090 * we're trying to reinitialise.
1092 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1093 struct mmc_card *oldcard)
1095 struct mmc_card *card;
1102 WARN_ON(!host->claimed);
1104 /* Set correct bus mode for MMC before attempting init */
1105 if (!mmc_host_is_spi(host))
1106 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1109 * Since we're changing the OCR value, we seem to
1110 * need to tell some cards to go back to the idle
1111 * state. We wait 1ms to give cards time to
1113 * mmc_go_idle is needed for eMMC that are asleep
1117 /* The extra bit indicates that we support high capacity */
1118 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1123 * For SPI, enable CRC as appropriate.
1125 if (mmc_host_is_spi(host)) {
1126 err = mmc_spi_set_crc(host, use_spi_crc);
1132 * Fetch CID from card.
1134 if (mmc_host_is_spi(host))
1135 err = mmc_send_cid(host, cid);
1137 err = mmc_all_send_cid(host, cid);
1142 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1150 * Allocate card structure.
1152 card = mmc_alloc_card(host, &mmc_type);
1154 err = PTR_ERR(card);
1159 card->type = MMC_TYPE_MMC;
1161 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1165 * For native busses: set card RCA and quit open drain mode.
1167 if (!mmc_host_is_spi(host)) {
1168 err = mmc_set_relative_addr(card);
1172 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1177 * Fetch CSD from card.
1179 err = mmc_send_csd(card, card->raw_csd);
1183 err = mmc_decode_csd(card);
1186 err = mmc_decode_cid(card);
1192 * Select card, as all following commands rely on that.
1194 if (!mmc_host_is_spi(host)) {
1195 err = mmc_select_card(card);
1202 * Fetch and process extended CSD.
1205 err = mmc_get_ext_csd(card, &ext_csd);
1208 err = mmc_read_ext_csd(card, ext_csd);
1212 /* If doing byte addressing, check if required to do sector
1213 * addressing. Handle the case of <2GB cards needing sector
1214 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1215 * ocr register has bit 30 set for sector addressing.
1217 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
1218 mmc_card_set_blockaddr(card);
1220 /* Erase size depends on CSD and Extended CSD */
1221 mmc_set_erase_size(card);
1225 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1226 * bit. This bit will be lost every time after a reset or power off.
1228 if (card->ext_csd.enhanced_area_en ||
1229 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1230 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1231 EXT_CSD_ERASE_GROUP_DEF, 1,
1232 card->ext_csd.generic_cmd6_time);
1234 if (err && err != -EBADMSG)
1240 * Just disable enhanced area off & sz
1241 * will try to enable ERASE_GROUP_DEF
1242 * during next time reinit
1244 card->ext_csd.enhanced_area_offset = -EINVAL;
1245 card->ext_csd.enhanced_area_size = -EINVAL;
1247 card->ext_csd.erase_group_def = 1;
1249 * enable ERASE_GRP_DEF successfully.
1250 * This will affect the erase size, so
1251 * here need to reset erase size
1253 mmc_set_erase_size(card);
1258 * Ensure eMMC user default partition is enabled
1260 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1261 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1262 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1263 card->ext_csd.part_config,
1264 card->ext_csd.part_time);
1265 if (err && err != -EBADMSG)
1270 * Enable power_off_notification byte in the ext_csd register
1272 if (card->ext_csd.rev >= 6) {
1273 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1274 EXT_CSD_POWER_OFF_NOTIFICATION,
1276 card->ext_csd.generic_cmd6_time);
1277 if (err && err != -EBADMSG)
1281 * The err can be -EBADMSG or 0,
1282 * so check for success and update the flag
1285 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1289 * Select timing interface
1291 err = mmc_select_timing(card);
1295 if (mmc_card_hs200(card)) {
1296 err = mmc_hs200_tuning(card);
1299 } else if (mmc_card_hs(card)) {
1300 /* Select the desired bus width optionally */
1301 err = mmc_select_bus_width(card);
1302 if (!IS_ERR_VALUE(err)) {
1303 err = mmc_select_hs_ddr(card);
1310 * Choose the power class with selected bus interface
1312 mmc_select_powerclass(card);
1315 * Enable HPI feature (if supported)
1317 if (card->ext_csd.hpi) {
1318 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1319 EXT_CSD_HPI_MGMT, 1,
1320 card->ext_csd.generic_cmd6_time);
1321 if (err && err != -EBADMSG)
1324 pr_warning("%s: Enabling HPI failed\n",
1325 mmc_hostname(card->host));
1328 card->ext_csd.hpi_en = 1;
1332 * If cache size is higher than 0, this indicates
1333 * the existence of cache and it can be turned on.
1335 if (card->ext_csd.cache_size > 0) {
1336 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1337 EXT_CSD_CACHE_CTRL, 1,
1338 card->ext_csd.generic_cmd6_time);
1339 if (err && err != -EBADMSG)
1343 * Only if no error, cache is turned on successfully.
1346 pr_warning("%s: Cache is supported, "
1347 "but failed to turn on (%d)\n",
1348 mmc_hostname(card->host), err);
1349 card->ext_csd.cache_ctrl = 0;
1352 card->ext_csd.cache_ctrl = 1;
1357 * The mandatory minimum values are defined for packed command.
1360 if (card->ext_csd.max_packed_writes >= 3 &&
1361 card->ext_csd.max_packed_reads >= 5 &&
1362 host->caps2 & MMC_CAP2_PACKED_CMD) {
1363 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1364 EXT_CSD_EXP_EVENTS_CTRL,
1365 EXT_CSD_PACKED_EVENT_EN,
1366 card->ext_csd.generic_cmd6_time);
1367 if (err && err != -EBADMSG)
1370 pr_warn("%s: Enabling packed event failed\n",
1371 mmc_hostname(card->host));
1372 card->ext_csd.packed_event_en = 0;
1375 card->ext_csd.packed_event_en = 1;
1382 mmc_free_ext_csd(ext_csd);
1387 mmc_remove_card(card);
1389 mmc_free_ext_csd(ext_csd);
1394 static int mmc_can_sleep(struct mmc_card *card)
1396 return (card && card->ext_csd.rev >= 3);
1399 static int mmc_sleep(struct mmc_host *host)
1401 struct mmc_command cmd = {0};
1402 struct mmc_card *card = host->card;
1403 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1406 err = mmc_deselect_cards(host);
1410 cmd.opcode = MMC_SLEEP_AWAKE;
1411 cmd.arg = card->rca << 16;
1415 * If the max_busy_timeout of the host is specified, validate it against
1416 * the sleep cmd timeout. A failure means we need to prevent the host
1417 * from doing hw busy detection, which is done by converting to a R1
1418 * response instead of a R1B.
1420 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1421 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1423 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1424 cmd.busy_timeout = timeout_ms;
1427 err = mmc_wait_for_cmd(host, &cmd, 0);
1432 * If the host does not wait while the card signals busy, then we will
1433 * will have to wait the sleep/awake timeout. Note, we cannot use the
1434 * SEND_STATUS command to poll the status because that command (and most
1435 * others) is invalid while the card sleeps.
1437 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1438 mmc_delay(timeout_ms);
1443 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1446 mmc_card_mmc(card) &&
1447 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1450 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1452 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1455 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1456 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1457 timeout = card->ext_csd.power_off_longtime;
1459 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1460 EXT_CSD_POWER_OFF_NOTIFICATION,
1461 notify_type, timeout, true, false, false);
1463 pr_err("%s: Power Off Notification timed out, %u\n",
1464 mmc_hostname(card->host), timeout);
1466 /* Disable the power off notification after the switch operation. */
1467 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1473 * Host is being removed. Free up the current card.
1475 static void mmc_remove(struct mmc_host *host)
1478 BUG_ON(!host->card);
1480 mmc_remove_card(host->card);
1485 * Card detection - card is alive.
1487 static int mmc_alive(struct mmc_host *host)
1489 return mmc_send_status(host->card, NULL);
1493 * Card detection callback from host.
1495 static void mmc_detect(struct mmc_host *host)
1500 BUG_ON(!host->card);
1502 mmc_get_card(host->card);
1505 * Just check if our card has been removed.
1507 err = _mmc_detect_card_removed(host);
1509 mmc_put_card(host->card);
1514 mmc_claim_host(host);
1515 mmc_detach_bus(host);
1516 mmc_power_off(host);
1517 mmc_release_host(host);
1521 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1524 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1525 EXT_CSD_POWER_OFF_LONG;
1528 BUG_ON(!host->card);
1530 mmc_claim_host(host);
1532 if (mmc_card_suspended(host->card))
1535 if (mmc_card_doing_bkops(host->card)) {
1536 err = mmc_stop_bkops(host->card);
1541 err = mmc_flush_cache(host->card);
1545 if (mmc_can_poweroff_notify(host->card) &&
1546 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1547 err = mmc_poweroff_notify(host->card, notify_type);
1548 else if (mmc_can_sleep(host->card))
1549 err = mmc_sleep(host);
1550 else if (!mmc_host_is_spi(host))
1551 err = mmc_deselect_cards(host);
1554 mmc_power_off(host);
1555 mmc_card_set_suspended(host->card);
1558 mmc_release_host(host);
1565 static int mmc_suspend(struct mmc_host *host)
1569 err = _mmc_suspend(host, true);
1571 pm_runtime_disable(&host->card->dev);
1572 pm_runtime_set_suspended(&host->card->dev);
1579 * This function tries to determine if the same card is still present
1580 * and, if so, restore all state to it.
1582 static int _mmc_resume(struct mmc_host *host)
1587 BUG_ON(!host->card);
1589 mmc_claim_host(host);
1591 if (!mmc_card_suspended(host->card))
1594 mmc_power_up(host, host->card->ocr);
1595 err = mmc_init_card(host, host->card->ocr, host->card);
1596 mmc_card_clr_suspended(host->card);
1599 mmc_release_host(host);
1606 static int mmc_shutdown(struct mmc_host *host)
1611 * In a specific case for poweroff notify, we need to resume the card
1612 * before we can shutdown it properly.
1614 if (mmc_can_poweroff_notify(host->card) &&
1615 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1616 err = _mmc_resume(host);
1619 err = _mmc_suspend(host, false);
1625 * Callback for resume.
1627 static int mmc_resume(struct mmc_host *host)
1631 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1632 err = _mmc_resume(host);
1633 pm_runtime_set_active(&host->card->dev);
1634 pm_runtime_mark_last_busy(&host->card->dev);
1636 pm_runtime_enable(&host->card->dev);
1642 * Callback for runtime_suspend.
1644 static int mmc_runtime_suspend(struct mmc_host *host)
1648 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1651 err = _mmc_suspend(host, true);
1653 pr_err("%s: error %d doing aggessive suspend\n",
1654 mmc_hostname(host), err);
1660 * Callback for runtime_resume.
1662 static int mmc_runtime_resume(struct mmc_host *host)
1666 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1669 err = _mmc_resume(host);
1671 pr_err("%s: error %d doing aggessive resume\n",
1672 mmc_hostname(host), err);
1677 static int mmc_power_restore(struct mmc_host *host)
1681 mmc_claim_host(host);
1682 ret = mmc_init_card(host, host->card->ocr, host->card);
1683 mmc_release_host(host);
1688 static const struct mmc_bus_ops mmc_ops = {
1689 .remove = mmc_remove,
1690 .detect = mmc_detect,
1691 .suspend = mmc_suspend,
1692 .resume = mmc_resume,
1693 .runtime_suspend = mmc_runtime_suspend,
1694 .runtime_resume = mmc_runtime_resume,
1695 .power_restore = mmc_power_restore,
1697 .shutdown = mmc_shutdown,
1701 * Starting point for MMC card init.
1703 int mmc_attach_mmc(struct mmc_host *host)
1709 WARN_ON(!host->claimed);
1711 /* Set correct bus mode for MMC before attempting attach */
1712 if (!mmc_host_is_spi(host))
1713 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1715 err = mmc_send_op_cond(host, 0, &ocr);
1719 mmc_attach_bus(host, &mmc_ops);
1720 if (host->ocr_avail_mmc)
1721 host->ocr_avail = host->ocr_avail_mmc;
1724 * We need to get OCR a different way for SPI.
1726 if (mmc_host_is_spi(host)) {
1727 err = mmc_spi_read_ocr(host, 1, &ocr);
1732 rocr = mmc_select_voltage(host, ocr);
1735 * Can we support the voltage of the card?
1743 * Detect and init the card.
1745 err = mmc_init_card(host, rocr, NULL);
1749 mmc_release_host(host);
1750 err = mmc_add_card(host->card);
1751 mmc_claim_host(host);
1758 mmc_release_host(host);
1759 mmc_remove_card(host->card);
1760 mmc_claim_host(host);
1763 mmc_detach_bus(host);
1765 pr_err("%s: error %d whilst initialising MMC card\n",
1766 mmc_hostname(host), err);