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;
247 if (card_type & EXT_CSD_CARD_TYPE_26)
248 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
250 if (caps & MMC_CAP_MMC_HIGHSPEED &&
251 card_type & EXT_CSD_CARD_TYPE_52)
252 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
254 if ((caps & MMC_CAP_1_8V_DDR &&
255 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
256 (caps & MMC_CAP_1_2V_DDR &&
257 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
258 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
260 if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
261 card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
262 (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
263 card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
264 hs_max_dtr = MMC_HS200_MAX_DTR;
266 card->ext_csd.hs_max_dtr = hs_max_dtr;
267 card->ext_csd.card_type = card_type;
271 * Decode extended CSD.
273 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
276 unsigned int part_size;
277 u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
284 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
285 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
286 if (card->csd.structure == 3) {
287 if (card->ext_csd.raw_ext_csd_structure > 2) {
288 pr_err("%s: unrecognised EXT_CSD structure "
289 "version %d\n", mmc_hostname(card->host),
290 card->ext_csd.raw_ext_csd_structure);
296 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
297 if (card->ext_csd.rev > 7) {
298 pr_err("%s: unrecognised EXT_CSD revision %d\n",
299 mmc_hostname(card->host), card->ext_csd.rev);
304 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
305 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
306 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
307 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
308 if (card->ext_csd.rev >= 2) {
309 card->ext_csd.sectors =
310 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
311 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
312 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
313 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
315 /* Cards with density > 2GiB are sector addressed */
316 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
317 mmc_card_set_blockaddr(card);
320 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
321 mmc_select_card_type(card);
323 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
324 card->ext_csd.raw_erase_timeout_mult =
325 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
326 card->ext_csd.raw_hc_erase_grp_size =
327 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
328 if (card->ext_csd.rev >= 3) {
329 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
330 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
332 /* EXT_CSD value is in units of 10ms, but we store in ms */
333 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
335 /* Sleep / awake timeout in 100ns units */
336 if (sa_shift > 0 && sa_shift <= 0x17)
337 card->ext_csd.sa_timeout =
338 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
339 card->ext_csd.erase_group_def =
340 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
341 card->ext_csd.hc_erase_timeout = 300 *
342 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
343 card->ext_csd.hc_erase_size =
344 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
346 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
349 * There are two boot regions of equal size, defined in
353 //emmc: We do NOT alloc boot partition now. noted by xbw, at 2014-03-09
354 card->ext_csd.boot_size = 0;
356 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
357 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
358 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
359 mmc_part_add(card, part_size,
360 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
362 MMC_BLK_DATA_AREA_BOOT);
368 card->ext_csd.raw_hc_erase_gap_size =
369 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
370 card->ext_csd.raw_sec_trim_mult =
371 ext_csd[EXT_CSD_SEC_TRIM_MULT];
372 card->ext_csd.raw_sec_erase_mult =
373 ext_csd[EXT_CSD_SEC_ERASE_MULT];
374 card->ext_csd.raw_sec_feature_support =
375 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
376 card->ext_csd.raw_trim_mult =
377 ext_csd[EXT_CSD_TRIM_MULT];
378 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
379 if (card->ext_csd.rev >= 4) {
381 * Enhanced area feature support -- check whether the eMMC
382 * card has the Enhanced area enabled. If so, export enhanced
383 * area offset and size to user by adding sysfs interface.
385 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
386 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
388 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
390 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
392 card->ext_csd.enhanced_area_en = 1;
394 * calculate the enhanced data area offset, in bytes
396 card->ext_csd.enhanced_area_offset =
397 (ext_csd[139] << 24) + (ext_csd[138] << 16) +
398 (ext_csd[137] << 8) + ext_csd[136];
399 if (mmc_card_blockaddr(card))
400 card->ext_csd.enhanced_area_offset <<= 9;
402 * calculate the enhanced data area size, in kilobytes
404 card->ext_csd.enhanced_area_size =
405 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
407 card->ext_csd.enhanced_area_size *=
408 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
409 card->ext_csd.enhanced_area_size <<= 9;
412 * If the enhanced area is not enabled, disable these
415 card->ext_csd.enhanced_area_offset = -EINVAL;
416 card->ext_csd.enhanced_area_size = -EINVAL;
420 * General purpose partition feature support --
421 * If ext_csd has the size of general purpose partitions,
422 * set size, part_cfg, partition name in mmc_part.
424 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
425 EXT_CSD_PART_SUPPORT_PART_EN) {
426 if (card->ext_csd.enhanced_area_en != 1) {
428 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
430 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
432 card->ext_csd.enhanced_area_en = 1;
435 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
436 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
437 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
438 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
441 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
443 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
445 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
446 part_size *= (size_t)(hc_erase_grp_sz *
448 mmc_part_add(card, part_size << 19,
449 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
451 MMC_BLK_DATA_AREA_GP);
454 card->ext_csd.sec_trim_mult =
455 ext_csd[EXT_CSD_SEC_TRIM_MULT];
456 card->ext_csd.sec_erase_mult =
457 ext_csd[EXT_CSD_SEC_ERASE_MULT];
458 card->ext_csd.sec_feature_support =
459 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
460 card->ext_csd.trim_timeout = 300 *
461 ext_csd[EXT_CSD_TRIM_MULT];
464 * Note that the call to mmc_part_add above defaults to read
465 * only. If this default assumption is changed, the call must
466 * take into account the value of boot_locked below.
468 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
469 card->ext_csd.boot_ro_lockable = true;
472 if (card->ext_csd.rev >= 5) {
473 /* Adjust production date as per JEDEC JESD84-B451 */
474 if (card->cid.year < 2010)
475 card->cid.year += 16;
477 /* check whether the eMMC card supports BKOPS */
478 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
479 card->ext_csd.bkops = 1;
480 card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
481 card->ext_csd.raw_bkops_status =
482 ext_csd[EXT_CSD_BKOPS_STATUS];
483 if (!card->ext_csd.bkops_en)
484 pr_info("%s: BKOPS_EN bit is not set\n",
485 mmc_hostname(card->host));
488 /* check whether the eMMC card supports HPI */
489 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
490 card->ext_csd.hpi = 1;
491 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
492 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
494 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
496 * Indicate the maximum timeout to close
497 * a command interrupted by HPI
499 card->ext_csd.out_of_int_time =
500 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
503 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
504 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
507 * RPMB regions are defined in multiples of 128K.
509 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
510 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
511 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
512 EXT_CSD_PART_CONFIG_ACC_RPMB,
514 MMC_BLK_DATA_AREA_RPMB);
518 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
519 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
520 card->erased_byte = 0xFF;
522 card->erased_byte = 0x0;
524 /* eMMC v4.5 or later */
525 if (card->ext_csd.rev >= 6) {
526 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
528 card->ext_csd.generic_cmd6_time = 10 *
529 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
530 card->ext_csd.power_off_longtime = 10 *
531 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
533 card->ext_csd.cache_size =
534 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
535 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
536 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
537 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
539 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
540 card->ext_csd.data_sector_size = 4096;
542 card->ext_csd.data_sector_size = 512;
544 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
545 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
546 card->ext_csd.data_tag_unit_size =
547 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
548 (card->ext_csd.data_sector_size);
550 card->ext_csd.data_tag_unit_size = 0;
553 card->ext_csd.max_packed_writes =
554 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
555 card->ext_csd.max_packed_reads =
556 ext_csd[EXT_CSD_MAX_PACKED_READS];
558 card->ext_csd.data_sector_size = 512;
565 static inline void mmc_free_ext_csd(u8 *ext_csd)
571 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
576 if (bus_width == MMC_BUS_WIDTH_1)
579 err = mmc_get_ext_csd(card, &bw_ext_csd);
581 if (err || bw_ext_csd == NULL) {
586 /* only compare read only fields */
587 err = !((card->ext_csd.raw_partition_support ==
588 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
589 (card->ext_csd.raw_erased_mem_count ==
590 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
591 (card->ext_csd.rev ==
592 bw_ext_csd[EXT_CSD_REV]) &&
593 (card->ext_csd.raw_ext_csd_structure ==
594 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
595 (card->ext_csd.raw_card_type ==
596 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
597 (card->ext_csd.raw_s_a_timeout ==
598 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
599 (card->ext_csd.raw_hc_erase_gap_size ==
600 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
601 (card->ext_csd.raw_erase_timeout_mult ==
602 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
603 (card->ext_csd.raw_hc_erase_grp_size ==
604 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
605 (card->ext_csd.raw_sec_trim_mult ==
606 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
607 (card->ext_csd.raw_sec_erase_mult ==
608 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
609 (card->ext_csd.raw_sec_feature_support ==
610 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
611 (card->ext_csd.raw_trim_mult ==
612 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
613 (card->ext_csd.raw_sectors[0] ==
614 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
615 (card->ext_csd.raw_sectors[1] ==
616 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
617 (card->ext_csd.raw_sectors[2] ==
618 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
619 (card->ext_csd.raw_sectors[3] ==
620 bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
625 mmc_free_ext_csd(bw_ext_csd);
629 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
630 card->raw_cid[2], card->raw_cid[3]);
631 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
632 card->raw_csd[2], card->raw_csd[3]);
633 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
634 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
635 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
636 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
637 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
638 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
639 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
640 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
641 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
642 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
643 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
644 card->ext_csd.enhanced_area_offset);
645 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
646 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
647 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
649 static struct attribute *mmc_std_attrs[] = {
653 &dev_attr_erase_size.attr,
654 &dev_attr_preferred_erase_size.attr,
655 &dev_attr_fwrev.attr,
656 &dev_attr_hwrev.attr,
657 &dev_attr_manfid.attr,
659 &dev_attr_oemid.attr,
661 &dev_attr_serial.attr,
662 &dev_attr_enhanced_area_offset.attr,
663 &dev_attr_enhanced_area_size.attr,
664 &dev_attr_raw_rpmb_size_mult.attr,
665 &dev_attr_rel_sectors.attr,
669 static struct attribute_group mmc_std_attr_group = {
670 .attrs = mmc_std_attrs,
673 static const struct attribute_group *mmc_attr_groups[] = {
678 static struct device_type mmc_type = {
679 .groups = mmc_attr_groups,
683 * Select the PowerClass for the current bus width
684 * If power class is defined for 4/8 bit bus in the
685 * extended CSD register, select it by executing the
686 * mmc_switch command.
688 static int mmc_select_powerclass(struct mmc_card *card,
689 unsigned int bus_width)
692 unsigned int pwrclass_val = 0;
693 struct mmc_host *host;
700 /* Power class selection is supported for versions >= 4.0 */
701 if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
704 /* Power class values are defined only for 4/8 bit bus */
705 if (bus_width == EXT_CSD_BUS_WIDTH_1)
708 switch (1 << host->ios.vdd) {
709 case MMC_VDD_165_195:
710 if (host->ios.clock <= 26000000)
711 pwrclass_val = card->ext_csd.raw_pwr_cl_26_195;
712 else if (host->ios.clock <= 52000000)
713 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
714 card->ext_csd.raw_pwr_cl_52_195 :
715 card->ext_csd.raw_pwr_cl_ddr_52_195;
716 else if (host->ios.clock <= 200000000)
717 pwrclass_val = card->ext_csd.raw_pwr_cl_200_195;
728 if (host->ios.clock <= 26000000)
729 pwrclass_val = card->ext_csd.raw_pwr_cl_26_360;
730 else if (host->ios.clock <= 52000000)
731 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
732 card->ext_csd.raw_pwr_cl_52_360 :
733 card->ext_csd.raw_pwr_cl_ddr_52_360;
734 else if (host->ios.clock <= 200000000)
735 pwrclass_val = card->ext_csd.raw_pwr_cl_200_360;
738 pr_warning("%s: Voltage range not supported "
739 "for power class.\n", mmc_hostname(host));
743 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
744 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
745 EXT_CSD_PWR_CL_8BIT_SHIFT;
747 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
748 EXT_CSD_PWR_CL_4BIT_SHIFT;
750 /* If the power class is different from the default value */
751 if (pwrclass_val > 0) {
752 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
755 card->ext_csd.generic_cmd6_time);
762 * Selects the desired buswidth and switch to the HS200 mode
763 * if bus width set without error
765 static int mmc_select_hs200(struct mmc_card *card)
767 int idx, err = -EINVAL;
768 struct mmc_host *host;
769 static unsigned ext_csd_bits[] = {
773 static unsigned bus_widths[] = {
782 if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
783 host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
784 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
786 if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
787 host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
788 err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
790 /* If fails try again during next card power cycle */
794 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
797 * Unlike SD, MMC cards dont have a configuration register to notify
798 * supported bus width. So bus test command should be run to identify
799 * the supported bus width or compare the ext csd values of current
800 * bus width and ext csd values of 1 bit mode read earlier.
802 for (; idx >= 0; idx--) {
805 * Host is capable of 8bit transfer, then switch
806 * the device to work in 8bit transfer mode. If the
807 * mmc switch command returns error then switch to
808 * 4bit transfer mode. On success set the corresponding
809 * bus width on the host.
811 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
814 card->ext_csd.generic_cmd6_time);
818 mmc_set_bus_width(card->host, bus_widths[idx]);
820 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
821 err = mmc_compare_ext_csds(card, bus_widths[idx]);
823 err = mmc_bus_test(card, bus_widths[idx]);
828 /* switch to HS200 mode if bus width set successfully */
830 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
831 EXT_CSD_HS_TIMING, 2, 0);
837 * Handle the detection and initialisation of a card.
839 * In the case of a resume, "oldcard" will contain the card
840 * we're trying to reinitialise.
842 static int mmc_init_card(struct mmc_host *host, u32 ocr,
843 struct mmc_card *oldcard)
845 struct mmc_card *card;
848 unsigned int max_dtr;
853 WARN_ON(!host->claimed);
855 /* Set correct bus mode for MMC before attempting init */
856 if (!mmc_host_is_spi(host))
857 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
860 * Since we're changing the OCR value, we seem to
861 * need to tell some cards to go back to the idle
862 * state. We wait 1ms to give cards time to
864 * mmc_go_idle is needed for eMMC that are asleep
868 /* The extra bit indicates that we support high capacity */
869 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
874 * For SPI, enable CRC as appropriate.
876 if (mmc_host_is_spi(host)) {
877 err = mmc_spi_set_crc(host, use_spi_crc);
883 * Fetch CID from card.
885 if (mmc_host_is_spi(host))
886 err = mmc_send_cid(host, cid);
888 err = mmc_all_send_cid(host, cid);
893 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
901 * Allocate card structure.
903 card = mmc_alloc_card(host, &mmc_type);
910 card->type = MMC_TYPE_MMC;
912 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
916 * For native busses: set card RCA and quit open drain mode.
918 if (!mmc_host_is_spi(host)) {
919 err = mmc_set_relative_addr(card);
923 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
928 * Fetch CSD from card.
930 err = mmc_send_csd(card, card->raw_csd);
934 err = mmc_decode_csd(card);
937 err = mmc_decode_cid(card);
943 * Select card, as all following commands rely on that.
945 if (!mmc_host_is_spi(host)) {
946 err = mmc_select_card(card);
953 * Fetch and process extended CSD.
956 err = mmc_get_ext_csd(card, &ext_csd);
959 err = mmc_read_ext_csd(card, ext_csd);
963 /* If doing byte addressing, check if required to do sector
964 * addressing. Handle the case of <2GB cards needing sector
965 * addressing. See section 8.1 JEDEC Standard JED84-A441;
966 * ocr register has bit 30 set for sector addressing.
968 if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
969 mmc_card_set_blockaddr(card);
971 /* Erase size depends on CSD and Extended CSD */
972 mmc_set_erase_size(card);
976 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
977 * bit. This bit will be lost every time after a reset or power off.
979 if (card->ext_csd.enhanced_area_en ||
980 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
981 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
982 EXT_CSD_ERASE_GROUP_DEF, 1,
983 card->ext_csd.generic_cmd6_time);
985 if (err && err != -EBADMSG)
991 * Just disable enhanced area off & sz
992 * will try to enable ERASE_GROUP_DEF
993 * during next time reinit
995 card->ext_csd.enhanced_area_offset = -EINVAL;
996 card->ext_csd.enhanced_area_size = -EINVAL;
998 card->ext_csd.erase_group_def = 1;
1000 * enable ERASE_GRP_DEF successfully.
1001 * This will affect the erase size, so
1002 * here need to reset erase size
1004 mmc_set_erase_size(card);
1009 * Ensure eMMC user default partition is enabled
1011 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1012 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1013 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1014 card->ext_csd.part_config,
1015 card->ext_csd.part_time);
1016 if (err && err != -EBADMSG)
1021 * Enable power_off_notification byte in the ext_csd register
1023 if (card->ext_csd.rev >= 6) {
1024 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1025 EXT_CSD_POWER_OFF_NOTIFICATION,
1027 card->ext_csd.generic_cmd6_time);
1028 if (err && err != -EBADMSG)
1032 * The err can be -EBADMSG or 0,
1033 * so check for success and update the flag
1036 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1040 * Activate high speed (if supported)
1042 if (card->ext_csd.hs_max_dtr != 0) {
1044 if (card->ext_csd.hs_max_dtr > 52000000 &&
1045 host->caps2 & MMC_CAP2_HS200)
1046 err = mmc_select_hs200(card);
1047 else if (host->caps & MMC_CAP_MMC_HIGHSPEED)
1048 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1049 EXT_CSD_HS_TIMING, 1,
1050 card->ext_csd.generic_cmd6_time);
1052 if (err && err != -EBADMSG)
1056 pr_warning("%s: switch to highspeed failed\n",
1057 mmc_hostname(card->host));
1060 if (card->ext_csd.hs_max_dtr > 52000000 &&
1061 host->caps2 & MMC_CAP2_HS200) {
1062 mmc_card_set_hs200(card);
1063 mmc_set_timing(card->host,
1064 MMC_TIMING_MMC_HS200);
1066 mmc_card_set_highspeed(card);
1067 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1073 * Compute bus speed.
1075 max_dtr = (unsigned int)-1;
1077 if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
1078 if (max_dtr > card->ext_csd.hs_max_dtr)
1079 max_dtr = card->ext_csd.hs_max_dtr;
1080 if (mmc_card_highspeed(card) && (max_dtr > 52000000))
1082 } else if (max_dtr > card->csd.max_dtr) {
1083 max_dtr = card->csd.max_dtr;
1086 mmc_set_clock(host, max_dtr);
1089 * Indicate DDR mode (if supported).
1091 if (mmc_card_highspeed(card)) {
1092 if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
1093 && (host->caps & MMC_CAP_1_8V_DDR))
1094 ddr = MMC_1_8V_DDR_MODE;
1095 else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1096 && (host->caps & MMC_CAP_1_2V_DDR))
1097 ddr = MMC_1_2V_DDR_MODE;
1101 * Indicate HS200 SDR mode (if supported).
1103 if (mmc_card_hs200(card)) {
1105 u32 bus_width = card->host->ios.bus_width;
1108 * For devices supporting HS200 mode, the bus width has
1109 * to be set before executing the tuning function. If
1110 * set before tuning, then device will respond with CRC
1111 * errors for responses on CMD line. So for HS200 the
1113 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1114 * 2. switch to HS200 mode
1115 * 3. set the clock to > 52Mhz <=200MHz and
1116 * 4. execute tuning for HS200
1118 if ((host->caps2 & MMC_CAP2_HS200) &&
1119 card->host->ops->execute_tuning) {
1120 mmc_host_clk_hold(card->host);
1121 err = card->host->ops->execute_tuning(card->host,
1122 MMC_SEND_TUNING_BLOCK_HS200);
1123 mmc_host_clk_release(card->host);
1126 pr_warning("%s: tuning execution failed\n",
1127 mmc_hostname(card->host));
1131 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1132 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1133 err = mmc_select_powerclass(card, ext_csd_bits);
1135 pr_warning("%s: power class selection to bus width %d"
1136 " failed\n", mmc_hostname(card->host),
1141 * Activate wide bus and DDR (if supported).
1143 if (!mmc_card_hs200(card) &&
1144 (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
1145 (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
1146 static unsigned ext_csd_bits[][2] = {
1147 { EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1148 { EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1149 { EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1151 static unsigned bus_widths[] = {
1156 unsigned idx, bus_width = 0;
1158 if (host->caps & MMC_CAP_8_BIT_DATA)
1162 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1163 bus_width = bus_widths[idx];
1164 if (bus_width == MMC_BUS_WIDTH_1)
1165 ddr = 0; /* no DDR for 1-bit width */
1166 err = mmc_select_powerclass(card, ext_csd_bits[idx][0]);
1168 pr_warning("%s: power class selection to "
1169 "bus width %d failed\n",
1170 mmc_hostname(card->host),
1173 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1175 ext_csd_bits[idx][0],
1176 card->ext_csd.generic_cmd6_time);
1178 mmc_set_bus_width(card->host, bus_width);
1181 * If controller can't handle bus width test,
1182 * compare ext_csd previously read in 1 bit mode
1183 * against ext_csd at new bus width
1185 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1186 err = mmc_compare_ext_csds(card,
1189 err = mmc_bus_test(card, bus_width);
1196 err = mmc_select_powerclass(card, ext_csd_bits[idx][1]);
1198 pr_warning("%s: power class selection to "
1199 "bus width %d ddr %d failed\n",
1200 mmc_hostname(card->host),
1201 1 << bus_width, ddr);
1203 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1205 ext_csd_bits[idx][1],
1206 card->ext_csd.generic_cmd6_time);
1209 pr_warning("%s: switch to bus width %d ddr %d "
1210 "failed\n", mmc_hostname(card->host),
1211 1 << bus_width, ddr);
1215 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1218 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1220 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1221 * in the JEDEC spec for DDR.
1223 * Do not force change in vccq since we are obviously
1224 * working and no change to vccq is needed.
1226 * WARNING: eMMC rules are NOT the same as SD DDR
1228 if (ddr == MMC_1_2V_DDR_MODE) {
1229 err = __mmc_set_signal_voltage(host,
1230 MMC_SIGNAL_VOLTAGE_120);
1234 mmc_card_set_ddr_mode(card);
1235 mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1236 mmc_set_bus_width(card->host, bus_width);
1241 * Enable HPI feature (if supported)
1243 if (card->ext_csd.hpi) {
1244 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1245 EXT_CSD_HPI_MGMT, 1,
1246 card->ext_csd.generic_cmd6_time);
1247 if (err && err != -EBADMSG)
1250 pr_warning("%s: Enabling HPI failed\n",
1251 mmc_hostname(card->host));
1254 card->ext_csd.hpi_en = 1;
1258 * If cache size is higher than 0, this indicates
1259 * the existence of cache and it can be turned on.
1261 if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1262 card->ext_csd.cache_size > 0) {
1263 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1264 EXT_CSD_CACHE_CTRL, 1,
1265 card->ext_csd.generic_cmd6_time);
1266 if (err && err != -EBADMSG)
1270 * Only if no error, cache is turned on successfully.
1273 pr_warning("%s: Cache is supported, "
1274 "but failed to turn on (%d)\n",
1275 mmc_hostname(card->host), err);
1276 card->ext_csd.cache_ctrl = 0;
1279 card->ext_csd.cache_ctrl = 1;
1284 * The mandatory minimum values are defined for packed command.
1287 if (card->ext_csd.max_packed_writes >= 3 &&
1288 card->ext_csd.max_packed_reads >= 5 &&
1289 host->caps2 & MMC_CAP2_PACKED_CMD) {
1290 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1291 EXT_CSD_EXP_EVENTS_CTRL,
1292 EXT_CSD_PACKED_EVENT_EN,
1293 card->ext_csd.generic_cmd6_time);
1294 if (err && err != -EBADMSG)
1297 pr_warn("%s: Enabling packed event failed\n",
1298 mmc_hostname(card->host));
1299 card->ext_csd.packed_event_en = 0;
1302 card->ext_csd.packed_event_en = 1;
1309 mmc_free_ext_csd(ext_csd);
1314 mmc_remove_card(card);
1316 mmc_free_ext_csd(ext_csd);
1321 static int mmc_can_sleep(struct mmc_card *card)
1323 return (card && card->ext_csd.rev >= 3);
1326 static int mmc_sleep(struct mmc_host *host)
1328 struct mmc_command cmd = {0};
1329 struct mmc_card *card = host->card;
1332 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
1335 err = mmc_deselect_cards(host);
1339 cmd.opcode = MMC_SLEEP_AWAKE;
1340 cmd.arg = card->rca << 16;
1343 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1344 err = mmc_wait_for_cmd(host, &cmd, 0);
1349 * If the host does not wait while the card signals busy, then we will
1350 * will have to wait the sleep/awake timeout. Note, we cannot use the
1351 * SEND_STATUS command to poll the status because that command (and most
1352 * others) is invalid while the card sleeps.
1354 if (!(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1355 mmc_delay(DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000));
1360 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1363 mmc_card_mmc(card) &&
1364 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1367 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1369 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1372 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1373 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1374 timeout = card->ext_csd.power_off_longtime;
1376 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1377 EXT_CSD_POWER_OFF_NOTIFICATION,
1378 notify_type, timeout, true, false);
1380 pr_err("%s: Power Off Notification timed out, %u\n",
1381 mmc_hostname(card->host), timeout);
1383 /* Disable the power off notification after the switch operation. */
1384 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1390 * Host is being removed. Free up the current card.
1392 static void mmc_remove(struct mmc_host *host)
1395 BUG_ON(!host->card);
1397 mmc_remove_card(host->card);
1402 * Card detection - card is alive.
1404 static int mmc_alive(struct mmc_host *host)
1406 return mmc_send_status(host->card, NULL);
1410 * Card detection callback from host.
1412 static void mmc_detect(struct mmc_host *host)
1417 BUG_ON(!host->card);
1419 mmc_get_card(host->card);
1422 * Just check if our card has been removed.
1424 err = _mmc_detect_card_removed(host);
1426 mmc_put_card(host->card);
1431 mmc_claim_host(host);
1432 mmc_detach_bus(host);
1433 mmc_power_off(host);
1434 mmc_release_host(host);
1438 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1441 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1442 EXT_CSD_POWER_OFF_LONG;
1445 BUG_ON(!host->card);
1447 mmc_claim_host(host);
1449 if (mmc_card_suspended(host->card))
1452 if (mmc_card_doing_bkops(host->card)) {
1453 err = mmc_stop_bkops(host->card);
1458 err = mmc_cache_ctrl(host, 0);
1462 if (mmc_can_poweroff_notify(host->card) &&
1463 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1464 err = mmc_poweroff_notify(host->card, notify_type);
1465 else if (mmc_can_sleep(host->card))
1466 err = mmc_sleep(host);
1467 else if (!mmc_host_is_spi(host))
1468 err = mmc_deselect_cards(host);
1469 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1472 mmc_power_off(host);
1473 mmc_card_set_suspended(host->card);
1476 mmc_release_host(host);
1483 static int mmc_suspend(struct mmc_host *host)
1485 return _mmc_suspend(host, true);
1489 * Resume callback from host.
1491 * This function tries to determine if the same card is still present
1492 * and, if so, restore all state to it.
1494 static int _mmc_resume(struct mmc_host *host)
1499 BUG_ON(!host->card);
1501 mmc_claim_host(host);
1503 if (!mmc_card_suspended(host->card))
1506 mmc_power_up(host, host->card->ocr);
1507 err = mmc_init_card(host, host->card->ocr, host->card);
1508 mmc_card_clr_suspended(host->card);
1511 mmc_release_host(host);
1517 * Callback for resume.
1519 static int mmc_resume(struct mmc_host *host)
1523 if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1524 err = _mmc_resume(host);
1525 pm_runtime_set_active(&host->card->dev);
1526 pm_runtime_mark_last_busy(&host->card->dev);
1528 pm_runtime_enable(&host->card->dev);
1533 static int mmc_shutdown(struct mmc_host *host)
1538 * In a specific case for poweroff notify, we need to resume the card
1539 * before we can shutdown it properly.
1541 if (mmc_can_poweroff_notify(host->card) &&
1542 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
1543 err = mmc_resume(host);
1546 err = _mmc_suspend(host, false);
1552 * Callback for runtime_suspend.
1554 static int mmc_runtime_suspend(struct mmc_host *host)
1558 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1561 err = _mmc_suspend(host, true);
1563 pr_err("%s: error %d doing aggessive suspend\n",
1564 mmc_hostname(host), err);
1570 * Callback for runtime_resume.
1572 static int mmc_runtime_resume(struct mmc_host *host)
1576 if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1579 err = _mmc_resume(host);
1581 pr_err("%s: error %d doing aggessive resume\n",
1582 mmc_hostname(host), err);
1587 static int mmc_power_restore(struct mmc_host *host)
1591 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1592 mmc_claim_host(host);
1593 ret = mmc_init_card(host, host->card->ocr, host->card);
1594 mmc_release_host(host);
1599 static const struct mmc_bus_ops mmc_ops = {
1600 .remove = mmc_remove,
1601 .detect = mmc_detect,
1604 .power_restore = mmc_power_restore,
1606 .shutdown = mmc_shutdown,
1609 static const struct mmc_bus_ops mmc_ops_unsafe = {
1610 .remove = mmc_remove,
1611 .detect = mmc_detect,
1612 .suspend = mmc_suspend,
1613 .resume = mmc_resume,
1614 .runtime_suspend = mmc_runtime_suspend,
1615 .runtime_resume = mmc_runtime_resume,
1616 .power_restore = mmc_power_restore,
1618 .shutdown = mmc_shutdown,
1621 static void mmc_attach_bus_ops(struct mmc_host *host)
1623 const struct mmc_bus_ops *bus_ops;
1625 if (!mmc_card_is_removable(host))
1626 bus_ops = &mmc_ops_unsafe;
1629 mmc_attach_bus(host, bus_ops);
1633 * Starting point for MMC card init.
1635 int mmc_attach_mmc(struct mmc_host *host)
1641 WARN_ON(!host->claimed);
1643 /* Set correct bus mode for MMC before attempting attach */
1644 if (!mmc_host_is_spi(host))
1645 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1647 err = mmc_send_op_cond(host, 0, &ocr);
1651 mmc_attach_bus_ops(host);
1652 if (host->ocr_avail_mmc)
1653 host->ocr_avail = host->ocr_avail_mmc;
1656 * We need to get OCR a different way for SPI.
1658 if (mmc_host_is_spi(host)) {
1659 err = mmc_spi_read_ocr(host, 1, &ocr);
1664 rocr = mmc_select_voltage(host, ocr);
1667 * Can we support the voltage of the card?
1675 * Detect and init the card.
1677 err = mmc_init_card(host, rocr, NULL);
1681 mmc_release_host(host);
1682 err = mmc_add_card(host->card);
1683 mmc_claim_host(host);
1690 mmc_release_host(host);
1691 mmc_remove_card(host->card);
1692 mmc_claim_host(host);
1695 mmc_detach_bus(host);
1697 pr_err("%s: error %d whilst initialising MMC card\n",
1698 mmc_hostname(host), err);