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>
15 #include <linux/slab.h>
16 #include <linux/stat.h>
17 #include <linux/pm_runtime.h>
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
30 #define DEFAULT_CMD6_TIMEOUT_MS 500
32 static const unsigned int tran_exp[] = {
33 10000, 100000, 1000000, 10000000,
37 static const unsigned char tran_mant[] = {
38 0, 10, 12, 13, 15, 20, 25, 30,
39 35, 40, 45, 50, 55, 60, 70, 80,
42 static const unsigned int tacc_exp[] = {
43 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
46 static const unsigned int tacc_mant[] = {
47 0, 10, 12, 13, 15, 20, 25, 30,
48 35, 40, 45, 50, 55, 60, 70, 80,
51 static const struct mmc_fixup mmc_ext_csd_fixups[] = {
53 * Certain Hynix eMMC 4.41 cards might get broken when HPI feature
54 * is used so disable the HPI feature for such buggy cards.
56 MMC_FIXUP_EXT_CSD_REV(CID_NAME_ANY, CID_MANFID_HYNIX,
57 0x014a, add_quirk, MMC_QUIRK_BROKEN_HPI, 5),
62 #define UNSTUFF_BITS(resp,start,size) \
64 const int __size = size; \
65 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
66 const int __off = 3 - ((start) / 32); \
67 const int __shft = (start) & 31; \
70 __res = resp[__off] >> __shft; \
71 if (__size + __shft > 32) \
72 __res |= resp[__off-1] << ((32 - __shft) % 32); \
77 * Given the decoded CSD structure, decode the raw CID to our CID structure.
79 static int mmc_decode_cid(struct mmc_card *card)
81 u32 *resp = card->raw_cid;
84 * The selection of the format here is based upon published
85 * specs from sandisk and from what people have reported.
87 switch (card->csd.mmca_vsn) {
88 case 0: /* MMC v1.0 - v1.2 */
89 case 1: /* MMC v1.4 */
90 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
91 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
92 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
93 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
94 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
95 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
96 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
97 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
98 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
99 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
100 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
101 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
102 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
105 case 2: /* MMC v2.0 - v2.2 */
106 case 3: /* MMC v3.1 - v3.3 */
108 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
109 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
110 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
111 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
112 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
113 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
114 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
115 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
116 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
117 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
118 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
119 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
123 pr_err("%s: card has unknown MMCA version %d\n",
124 mmc_hostname(card->host), card->csd.mmca_vsn);
131 static void mmc_set_erase_size(struct mmc_card *card)
133 if (card->ext_csd.erase_group_def & 1)
134 card->erase_size = card->ext_csd.hc_erase_size;
136 card->erase_size = card->csd.erase_size;
138 mmc_init_erase(card);
142 * Given a 128-bit response, decode to our card CSD structure.
144 static int mmc_decode_csd(struct mmc_card *card)
146 struct mmc_csd *csd = &card->csd;
147 unsigned int e, m, a, b;
148 u32 *resp = card->raw_csd;
151 * We only understand CSD structure v1.1 and v1.2.
152 * v1.2 has extra information in bits 15, 11 and 10.
153 * We also support eMMC v4.4 & v4.41.
155 csd->structure = UNSTUFF_BITS(resp, 126, 2);
156 if (csd->structure == 0) {
157 pr_err("%s: unrecognised CSD structure version %d\n",
158 mmc_hostname(card->host), csd->structure);
162 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
163 m = UNSTUFF_BITS(resp, 115, 4);
164 e = UNSTUFF_BITS(resp, 112, 3);
165 csd->tacc_ns = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
166 csd->tacc_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
168 m = UNSTUFF_BITS(resp, 99, 4);
169 e = UNSTUFF_BITS(resp, 96, 3);
170 csd->max_dtr = tran_exp[e] * tran_mant[m];
171 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
173 e = UNSTUFF_BITS(resp, 47, 3);
174 m = UNSTUFF_BITS(resp, 62, 12);
175 csd->capacity = (1 + m) << (e + 2);
177 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
178 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
179 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
180 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
181 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
182 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
183 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
184 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
186 if (csd->write_blkbits >= 9) {
187 a = UNSTUFF_BITS(resp, 42, 5);
188 b = UNSTUFF_BITS(resp, 37, 5);
189 csd->erase_size = (a + 1) * (b + 1);
190 csd->erase_size <<= csd->write_blkbits - 9;
196 static void mmc_select_card_type(struct mmc_card *card)
198 struct mmc_host *host = card->host;
199 u8 card_type = card->ext_csd.raw_card_type;
200 u32 caps = host->caps, caps2 = host->caps2;
201 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
202 unsigned int avail_type = 0;
204 if (caps & MMC_CAP_MMC_HIGHSPEED &&
205 card_type & EXT_CSD_CARD_TYPE_HS_26) {
206 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
207 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
210 if (caps & MMC_CAP_MMC_HIGHSPEED &&
211 card_type & EXT_CSD_CARD_TYPE_HS_52) {
212 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
213 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
216 if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
217 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
218 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
219 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
222 if (caps & MMC_CAP_1_2V_DDR &&
223 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
224 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
225 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
228 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
229 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
230 hs200_max_dtr = MMC_HS200_MAX_DTR;
231 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
234 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
235 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
236 hs200_max_dtr = MMC_HS200_MAX_DTR;
237 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
240 if (caps2 & MMC_CAP2_HS400_1_8V &&
241 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
242 hs200_max_dtr = MMC_HS200_MAX_DTR;
243 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
246 if (caps2 & MMC_CAP2_HS400_1_2V &&
247 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
248 hs200_max_dtr = MMC_HS200_MAX_DTR;
249 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
252 if ((caps2 & MMC_CAP2_HS400_ES) &&
253 card->ext_csd.strobe_support &&
254 (avail_type & EXT_CSD_CARD_TYPE_HS400))
255 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
257 card->ext_csd.hs_max_dtr = hs_max_dtr;
258 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
259 card->mmc_avail_type = avail_type;
262 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
264 u8 hc_erase_grp_sz, hc_wp_grp_sz;
267 * Disable these attributes by default
269 card->ext_csd.enhanced_area_offset = -EINVAL;
270 card->ext_csd.enhanced_area_size = -EINVAL;
273 * Enhanced area feature support -- check whether the eMMC
274 * card has the Enhanced area enabled. If so, export enhanced
275 * area offset and size to user by adding sysfs interface.
277 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
278 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
279 if (card->ext_csd.partition_setting_completed) {
281 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
283 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
286 * calculate the enhanced data area offset, in bytes
288 card->ext_csd.enhanced_area_offset =
289 (((unsigned long long)ext_csd[139]) << 24) +
290 (((unsigned long long)ext_csd[138]) << 16) +
291 (((unsigned long long)ext_csd[137]) << 8) +
292 (((unsigned long long)ext_csd[136]));
293 if (mmc_card_blockaddr(card))
294 card->ext_csd.enhanced_area_offset <<= 9;
296 * calculate the enhanced data area size, in kilobytes
298 card->ext_csd.enhanced_area_size =
299 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
301 card->ext_csd.enhanced_area_size *=
302 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
303 card->ext_csd.enhanced_area_size <<= 9;
305 pr_warn("%s: defines enhanced area without partition setting complete\n",
306 mmc_hostname(card->host));
311 static void mmc_part_add(struct mmc_card *card, unsigned int size,
312 unsigned int part_cfg, char *name, int idx, bool ro,
315 card->part[card->nr_parts].size = size;
316 card->part[card->nr_parts].part_cfg = part_cfg;
317 sprintf(card->part[card->nr_parts].name, name, idx);
318 card->part[card->nr_parts].force_ro = ro;
319 card->part[card->nr_parts].area_type = area_type;
323 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
326 u8 hc_erase_grp_sz, hc_wp_grp_sz;
327 unsigned int part_size;
330 * General purpose partition feature support --
331 * If ext_csd has the size of general purpose partitions,
332 * set size, part_cfg, partition name in mmc_part.
334 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
335 EXT_CSD_PART_SUPPORT_PART_EN) {
337 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
339 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
341 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
342 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
343 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
344 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
346 if (card->ext_csd.partition_setting_completed == 0) {
347 pr_warn("%s: has partition size defined without partition complete\n",
348 mmc_hostname(card->host));
352 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
354 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
356 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
357 part_size *= (size_t)(hc_erase_grp_sz *
359 mmc_part_add(card, part_size << 19,
360 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
362 MMC_BLK_DATA_AREA_GP);
367 /* Minimum partition switch timeout in milliseconds */
368 #define MMC_MIN_PART_SWITCH_TIME 300
371 * Decode extended CSD.
373 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
376 unsigned int part_size;
377 struct device_node *np;
378 bool broken_hpi = false;
380 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
381 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
382 if (card->csd.structure == 3) {
383 if (card->ext_csd.raw_ext_csd_structure > 2) {
384 pr_err("%s: unrecognised EXT_CSD structure "
385 "version %d\n", mmc_hostname(card->host),
386 card->ext_csd.raw_ext_csd_structure);
392 np = mmc_of_find_child_device(card->host, 0);
393 if (np && of_device_is_compatible(np, "mmc-card"))
394 broken_hpi = of_property_read_bool(np, "broken-hpi");
398 * The EXT_CSD format is meant to be forward compatible. As long
399 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
400 * are authorized, see JEDEC JESD84-B50 section B.8.
402 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
404 /* fixup device after ext_csd revision field is updated */
405 mmc_fixup_device(card, mmc_ext_csd_fixups);
407 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
408 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
409 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
410 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
411 if (card->ext_csd.rev >= 2) {
412 card->ext_csd.sectors =
413 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
414 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
415 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
416 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
418 /* Cards with density > 2GiB are sector addressed */
419 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
420 mmc_card_set_blockaddr(card);
423 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
424 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
425 mmc_select_card_type(card);
427 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
428 card->ext_csd.raw_erase_timeout_mult =
429 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
430 card->ext_csd.raw_hc_erase_grp_size =
431 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
432 if (card->ext_csd.rev >= 3) {
433 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
434 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
436 /* EXT_CSD value is in units of 10ms, but we store in ms */
437 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
438 /* Some eMMC set the value too low so set a minimum */
439 if (card->ext_csd.part_time &&
440 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
441 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
443 /* Sleep / awake timeout in 100ns units */
444 if (sa_shift > 0 && sa_shift <= 0x17)
445 card->ext_csd.sa_timeout =
446 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
447 card->ext_csd.erase_group_def =
448 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
449 card->ext_csd.hc_erase_timeout = 300 *
450 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
451 card->ext_csd.hc_erase_size =
452 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
454 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
457 * There are two boot regions of equal size, defined in
460 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
461 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
462 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
463 mmc_part_add(card, part_size,
464 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
466 MMC_BLK_DATA_AREA_BOOT);
471 card->ext_csd.raw_hc_erase_gap_size =
472 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
473 card->ext_csd.raw_sec_trim_mult =
474 ext_csd[EXT_CSD_SEC_TRIM_MULT];
475 card->ext_csd.raw_sec_erase_mult =
476 ext_csd[EXT_CSD_SEC_ERASE_MULT];
477 card->ext_csd.raw_sec_feature_support =
478 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
479 card->ext_csd.raw_trim_mult =
480 ext_csd[EXT_CSD_TRIM_MULT];
481 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
482 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
483 if (card->ext_csd.rev >= 4) {
484 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
485 EXT_CSD_PART_SETTING_COMPLETED)
486 card->ext_csd.partition_setting_completed = 1;
488 card->ext_csd.partition_setting_completed = 0;
490 mmc_manage_enhanced_area(card, ext_csd);
492 mmc_manage_gp_partitions(card, ext_csd);
494 card->ext_csd.sec_trim_mult =
495 ext_csd[EXT_CSD_SEC_TRIM_MULT];
496 card->ext_csd.sec_erase_mult =
497 ext_csd[EXT_CSD_SEC_ERASE_MULT];
498 card->ext_csd.sec_feature_support =
499 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
500 card->ext_csd.trim_timeout = 300 *
501 ext_csd[EXT_CSD_TRIM_MULT];
504 * Note that the call to mmc_part_add above defaults to read
505 * only. If this default assumption is changed, the call must
506 * take into account the value of boot_locked below.
508 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
509 card->ext_csd.boot_ro_lockable = true;
511 /* Save power class values */
512 card->ext_csd.raw_pwr_cl_52_195 =
513 ext_csd[EXT_CSD_PWR_CL_52_195];
514 card->ext_csd.raw_pwr_cl_26_195 =
515 ext_csd[EXT_CSD_PWR_CL_26_195];
516 card->ext_csd.raw_pwr_cl_52_360 =
517 ext_csd[EXT_CSD_PWR_CL_52_360];
518 card->ext_csd.raw_pwr_cl_26_360 =
519 ext_csd[EXT_CSD_PWR_CL_26_360];
520 card->ext_csd.raw_pwr_cl_200_195 =
521 ext_csd[EXT_CSD_PWR_CL_200_195];
522 card->ext_csd.raw_pwr_cl_200_360 =
523 ext_csd[EXT_CSD_PWR_CL_200_360];
524 card->ext_csd.raw_pwr_cl_ddr_52_195 =
525 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
526 card->ext_csd.raw_pwr_cl_ddr_52_360 =
527 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
528 card->ext_csd.raw_pwr_cl_ddr_200_360 =
529 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
532 if (card->ext_csd.rev >= 5) {
533 /* Adjust production date as per JEDEC JESD84-B451 */
534 if (card->cid.year < 2010)
535 card->cid.year += 16;
537 /* check whether the eMMC card supports BKOPS */
538 if (!mmc_card_broken_hpi(card) &&
539 ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
540 card->ext_csd.bkops = 1;
541 card->ext_csd.man_bkops_en =
542 (ext_csd[EXT_CSD_BKOPS_EN] &
543 EXT_CSD_MANUAL_BKOPS_MASK);
544 card->ext_csd.raw_bkops_status =
545 ext_csd[EXT_CSD_BKOPS_STATUS];
546 if (card->ext_csd.man_bkops_en)
547 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
548 mmc_hostname(card->host));
549 card->ext_csd.auto_bkops_en =
550 (ext_csd[EXT_CSD_BKOPS_EN] &
551 EXT_CSD_AUTO_BKOPS_MASK);
552 if (card->ext_csd.auto_bkops_en)
553 pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
554 mmc_hostname(card->host));
557 /* check whether the eMMC card supports HPI */
558 if (!mmc_card_broken_hpi(card) &&
559 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
560 card->ext_csd.hpi = 1;
561 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
562 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
564 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
566 * Indicate the maximum timeout to close
567 * a command interrupted by HPI
569 card->ext_csd.out_of_int_time =
570 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
573 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
574 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
577 * RPMB regions are defined in multiples of 128K.
579 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
580 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
581 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
582 EXT_CSD_PART_CONFIG_ACC_RPMB,
584 MMC_BLK_DATA_AREA_RPMB);
588 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
589 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
590 card->erased_byte = 0xFF;
592 card->erased_byte = 0x0;
594 /* eMMC v4.5 or later */
595 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
596 if (card->ext_csd.rev >= 6) {
597 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
599 card->ext_csd.generic_cmd6_time = 10 *
600 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
601 card->ext_csd.power_off_longtime = 10 *
602 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
604 card->ext_csd.cache_size =
605 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
606 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
607 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
608 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
610 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
611 card->ext_csd.data_sector_size = 4096;
613 card->ext_csd.data_sector_size = 512;
615 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
616 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
617 card->ext_csd.data_tag_unit_size =
618 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
619 (card->ext_csd.data_sector_size);
621 card->ext_csd.data_tag_unit_size = 0;
624 card->ext_csd.max_packed_writes =
625 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
626 card->ext_csd.max_packed_reads =
627 ext_csd[EXT_CSD_MAX_PACKED_READS];
629 card->ext_csd.data_sector_size = 512;
632 /* eMMC v5 or later */
633 if (card->ext_csd.rev >= 7) {
634 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
636 card->ext_csd.ffu_capable =
637 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
638 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
640 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
641 card->ext_csd.device_life_time_est_typ_a =
642 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
643 card->ext_csd.device_life_time_est_typ_b =
644 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
647 /* eMMC v5.1 or later */
648 if (card->ext_csd.rev >= 8) {
649 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
650 EXT_CSD_CMDQ_SUPPORTED;
651 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
652 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
653 /* Exclude inefficiently small queue depths */
654 if (card->ext_csd.cmdq_depth <= 2) {
655 card->ext_csd.cmdq_support = false;
656 card->ext_csd.cmdq_depth = 0;
658 if (card->ext_csd.cmdq_support) {
659 pr_debug("%s: Command Queue supported depth %u\n",
660 mmc_hostname(card->host),
661 card->ext_csd.cmdq_depth);
668 static int mmc_read_ext_csd(struct mmc_card *card)
673 if (!mmc_can_ext_csd(card))
676 err = mmc_get_ext_csd(card, &ext_csd);
678 /* If the host or the card can't do the switch,
679 * fail more gracefully. */
686 * High capacity cards should have this "magic" size
687 * stored in their CSD.
689 if (card->csd.capacity == (4096 * 512)) {
690 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
691 mmc_hostname(card->host));
693 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
694 mmc_hostname(card->host));
701 err = mmc_decode_ext_csd(card, ext_csd);
706 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
711 if (bus_width == MMC_BUS_WIDTH_1)
714 err = mmc_get_ext_csd(card, &bw_ext_csd);
718 /* only compare read only fields */
719 err = !((card->ext_csd.raw_partition_support ==
720 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
721 (card->ext_csd.raw_erased_mem_count ==
722 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
723 (card->ext_csd.rev ==
724 bw_ext_csd[EXT_CSD_REV]) &&
725 (card->ext_csd.raw_ext_csd_structure ==
726 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
727 (card->ext_csd.raw_card_type ==
728 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
729 (card->ext_csd.raw_s_a_timeout ==
730 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
731 (card->ext_csd.raw_hc_erase_gap_size ==
732 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
733 (card->ext_csd.raw_erase_timeout_mult ==
734 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
735 (card->ext_csd.raw_hc_erase_grp_size ==
736 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
737 (card->ext_csd.raw_sec_trim_mult ==
738 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
739 (card->ext_csd.raw_sec_erase_mult ==
740 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
741 (card->ext_csd.raw_sec_feature_support ==
742 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
743 (card->ext_csd.raw_trim_mult ==
744 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
745 (card->ext_csd.raw_sectors[0] ==
746 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
747 (card->ext_csd.raw_sectors[1] ==
748 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
749 (card->ext_csd.raw_sectors[2] ==
750 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
751 (card->ext_csd.raw_sectors[3] ==
752 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
753 (card->ext_csd.raw_pwr_cl_52_195 ==
754 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
755 (card->ext_csd.raw_pwr_cl_26_195 ==
756 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
757 (card->ext_csd.raw_pwr_cl_52_360 ==
758 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
759 (card->ext_csd.raw_pwr_cl_26_360 ==
760 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
761 (card->ext_csd.raw_pwr_cl_200_195 ==
762 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
763 (card->ext_csd.raw_pwr_cl_200_360 ==
764 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
765 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
766 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
767 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
768 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
769 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
770 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
779 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
780 card->raw_cid[2], card->raw_cid[3]);
781 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
782 card->raw_csd[2], card->raw_csd[3]);
783 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
784 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
785 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
786 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
787 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
788 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
789 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
790 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
791 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
792 MMC_DEV_ATTR(pre_eol_info, "%02x\n", card->ext_csd.pre_eol_info);
793 MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
794 card->ext_csd.device_life_time_est_typ_a,
795 card->ext_csd.device_life_time_est_typ_b);
796 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
797 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
798 card->ext_csd.enhanced_area_offset);
799 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
800 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
801 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
802 MMC_DEV_ATTR(ocr, "%08x\n", card->ocr);
804 static ssize_t mmc_fwrev_show(struct device *dev,
805 struct device_attribute *attr,
808 struct mmc_card *card = mmc_dev_to_card(dev);
810 if (card->ext_csd.rev < 7) {
811 return sprintf(buf, "0x%x\n", card->cid.fwrev);
813 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
814 card->ext_csd.fwrev);
818 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
820 static ssize_t mmc_dsr_show(struct device *dev,
821 struct device_attribute *attr,
824 struct mmc_card *card = mmc_dev_to_card(dev);
825 struct mmc_host *host = card->host;
827 if (card->csd.dsr_imp && host->dsr_req)
828 return sprintf(buf, "0x%x\n", host->dsr);
830 /* return default DSR value */
831 return sprintf(buf, "0x%x\n", 0x404);
834 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
836 static struct attribute *mmc_std_attrs[] = {
840 &dev_attr_erase_size.attr,
841 &dev_attr_preferred_erase_size.attr,
842 &dev_attr_fwrev.attr,
843 &dev_attr_ffu_capable.attr,
844 &dev_attr_hwrev.attr,
845 &dev_attr_manfid.attr,
847 &dev_attr_oemid.attr,
849 &dev_attr_pre_eol_info.attr,
850 &dev_attr_life_time.attr,
851 &dev_attr_serial.attr,
852 &dev_attr_enhanced_area_offset.attr,
853 &dev_attr_enhanced_area_size.attr,
854 &dev_attr_raw_rpmb_size_mult.attr,
855 &dev_attr_rel_sectors.attr,
860 ATTRIBUTE_GROUPS(mmc_std);
862 static struct device_type mmc_type = {
863 .groups = mmc_std_groups,
867 * Select the PowerClass for the current bus width
868 * If power class is defined for 4/8 bit bus in the
869 * extended CSD register, select it by executing the
870 * mmc_switch command.
872 static int __mmc_select_powerclass(struct mmc_card *card,
873 unsigned int bus_width)
875 struct mmc_host *host = card->host;
876 struct mmc_ext_csd *ext_csd = &card->ext_csd;
877 unsigned int pwrclass_val = 0;
880 switch (1 << host->ios.vdd) {
881 case MMC_VDD_165_195:
882 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
883 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
884 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
885 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
886 ext_csd->raw_pwr_cl_52_195 :
887 ext_csd->raw_pwr_cl_ddr_52_195;
888 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
889 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
900 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
901 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
902 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
903 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
904 ext_csd->raw_pwr_cl_52_360 :
905 ext_csd->raw_pwr_cl_ddr_52_360;
906 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
907 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
908 ext_csd->raw_pwr_cl_ddr_200_360 :
909 ext_csd->raw_pwr_cl_200_360;
912 pr_warn("%s: Voltage range not supported for power class\n",
917 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
918 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
919 EXT_CSD_PWR_CL_8BIT_SHIFT;
921 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
922 EXT_CSD_PWR_CL_4BIT_SHIFT;
924 /* If the power class is different from the default value */
925 if (pwrclass_val > 0) {
926 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
929 card->ext_csd.generic_cmd6_time);
935 static int mmc_select_powerclass(struct mmc_card *card)
937 struct mmc_host *host = card->host;
938 u32 bus_width, ext_csd_bits;
941 /* Power class selection is supported for versions >= 4.0 */
942 if (!mmc_can_ext_csd(card))
945 bus_width = host->ios.bus_width;
946 /* Power class values are defined only for 4/8 bit bus */
947 if (bus_width == MMC_BUS_WIDTH_1)
950 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
952 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
953 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
955 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
956 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
958 err = __mmc_select_powerclass(card, ext_csd_bits);
960 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
961 mmc_hostname(host), 1 << bus_width, ddr);
967 * Set the bus speed for the selected speed mode.
969 static void mmc_set_bus_speed(struct mmc_card *card)
971 unsigned int max_dtr = (unsigned int)-1;
973 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
974 max_dtr > card->ext_csd.hs200_max_dtr)
975 max_dtr = card->ext_csd.hs200_max_dtr;
976 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
977 max_dtr = card->ext_csd.hs_max_dtr;
978 else if (max_dtr > card->csd.max_dtr)
979 max_dtr = card->csd.max_dtr;
981 mmc_set_clock(card->host, max_dtr);
985 * Select the bus width amoung 4-bit and 8-bit(SDR).
986 * If the bus width is changed successfully, return the selected width value.
987 * Zero is returned instead of error value if the wide width is not supported.
989 static int mmc_select_bus_width(struct mmc_card *card)
991 static unsigned ext_csd_bits[] = {
995 static unsigned bus_widths[] = {
999 struct mmc_host *host = card->host;
1000 unsigned idx, bus_width = 0;
1003 if (!mmc_can_ext_csd(card) ||
1004 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1007 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
1010 * Unlike SD, MMC cards dont have a configuration register to notify
1011 * supported bus width. So bus test command should be run to identify
1012 * the supported bus width or compare the ext csd values of current
1013 * bus width and ext csd values of 1 bit mode read earlier.
1015 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1017 * Host is capable of 8bit transfer, then switch
1018 * the device to work in 8bit transfer mode. If the
1019 * mmc switch command returns error then switch to
1020 * 4bit transfer mode. On success set the corresponding
1021 * bus width on the host.
1023 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1026 card->ext_csd.generic_cmd6_time);
1030 bus_width = bus_widths[idx];
1031 mmc_set_bus_width(host, bus_width);
1034 * If controller can't handle bus width test,
1035 * compare ext_csd previously read in 1 bit mode
1036 * against ext_csd at new bus width
1038 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1039 err = mmc_compare_ext_csds(card, bus_width);
1041 err = mmc_bus_test(card, bus_width);
1047 pr_warn("%s: switch to bus width %d failed\n",
1048 mmc_hostname(host), 1 << bus_width);
1056 * Switch to the high-speed mode
1058 static int mmc_select_hs(struct mmc_card *card)
1062 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1063 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1064 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1067 pr_warn("%s: switch to high-speed failed, err:%d\n",
1068 mmc_hostname(card->host), err);
1074 * Activate wide bus and DDR if supported.
1076 static int mmc_select_hs_ddr(struct mmc_card *card)
1078 struct mmc_host *host = card->host;
1079 u32 bus_width, ext_csd_bits;
1082 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1085 bus_width = host->ios.bus_width;
1086 if (bus_width == MMC_BUS_WIDTH_1)
1089 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1090 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1092 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1095 card->ext_csd.generic_cmd6_time,
1096 MMC_TIMING_MMC_DDR52,
1099 pr_err("%s: switch to bus width %d ddr failed\n",
1100 mmc_hostname(host), 1 << bus_width);
1105 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1108 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1110 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1111 * in the JEDEC spec for DDR.
1113 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1114 * host controller can support this, like some of the SDHCI
1115 * controller which connect to an eMMC device. Some of these
1116 * host controller still needs to use 1.8v vccq for supporting
1119 * So the sequence will be:
1120 * if (host and device can both support 1.2v IO)
1122 * else if (host and device can both support 1.8v IO)
1124 * so if host and device can only support 3.3v IO, this is the
1127 * WARNING: eMMC rules are NOT the same as SD DDR
1129 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
1130 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1135 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1136 host->caps & MMC_CAP_1_8V_DDR)
1137 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1139 /* make sure vccq is 3.3v after switching disaster */
1141 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1146 static int mmc_select_hs400(struct mmc_card *card)
1148 struct mmc_host *host = card->host;
1149 unsigned int max_dtr;
1154 * HS400 mode requires 8-bit bus width
1156 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1157 host->ios.bus_width == MMC_BUS_WIDTH_8))
1160 /* Switch card to HS mode */
1161 val = EXT_CSD_TIMING_HS;
1162 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1163 EXT_CSD_HS_TIMING, val,
1164 card->ext_csd.generic_cmd6_time, 0,
1167 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1168 mmc_hostname(host), err);
1172 /* Set host controller to HS timing */
1173 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1175 /* Reduce frequency to HS frequency */
1176 max_dtr = card->ext_csd.hs_max_dtr;
1177 mmc_set_clock(host, max_dtr);
1179 err = mmc_switch_status(card);
1183 /* Switch card to DDR */
1184 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1186 EXT_CSD_DDR_BUS_WIDTH_8,
1187 card->ext_csd.generic_cmd6_time);
1189 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1190 mmc_hostname(host), err);
1194 /* Switch card to HS400 */
1195 val = EXT_CSD_TIMING_HS400 |
1196 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1197 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1198 EXT_CSD_HS_TIMING, val,
1199 card->ext_csd.generic_cmd6_time, 0,
1202 pr_err("%s: switch to hs400 failed, err:%d\n",
1203 mmc_hostname(host), err);
1207 /* Set host controller to HS400 timing and frequency */
1208 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1209 mmc_set_bus_speed(card);
1211 err = mmc_switch_status(card);
1218 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1223 int mmc_hs200_to_hs400(struct mmc_card *card)
1225 return mmc_select_hs400(card);
1228 int mmc_hs400_to_hs200(struct mmc_card *card)
1230 struct mmc_host *host = card->host;
1231 unsigned int max_dtr;
1235 /* Reduce frequency to HS */
1236 max_dtr = card->ext_csd.hs_max_dtr;
1237 mmc_set_clock(host, max_dtr);
1239 /* Switch HS400 to HS DDR */
1240 val = EXT_CSD_TIMING_HS;
1241 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1242 val, card->ext_csd.generic_cmd6_time, 0,
1247 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1249 err = mmc_switch_status(card);
1253 /* Switch HS DDR to HS */
1254 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1255 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1256 0, true, false, true);
1260 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1262 err = mmc_switch_status(card);
1266 /* Switch HS to HS200 */
1267 val = EXT_CSD_TIMING_HS200 |
1268 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1269 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1270 val, card->ext_csd.generic_cmd6_time, 0,
1275 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1278 * For HS200, CRC errors are not a reliable way to know the switch
1279 * failed. If there really is a problem, we would expect tuning will
1280 * fail and the result ends up the same.
1282 err = __mmc_switch_status(card, false);
1286 mmc_set_bus_speed(card);
1291 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1296 static int mmc_select_hs400es(struct mmc_card *card)
1298 struct mmc_host *host = card->host;
1302 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1307 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1308 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1310 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1311 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1313 /* If fails try again during next card power cycle */
1317 err = mmc_select_bus_width(card);
1321 /* Switch card to HS mode */
1322 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1323 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1324 card->ext_csd.generic_cmd6_time, 0,
1327 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1328 mmc_hostname(host), err);
1332 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1333 err = mmc_switch_status(card);
1337 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1339 /* Switch card to DDR with strobe bit */
1340 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1341 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1344 card->ext_csd.generic_cmd6_time);
1346 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1347 mmc_hostname(host), err);
1351 /* Switch card to HS400 */
1352 val = EXT_CSD_TIMING_HS400 |
1353 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1354 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1355 EXT_CSD_HS_TIMING, val,
1356 card->ext_csd.generic_cmd6_time, 0,
1359 pr_err("%s: switch to hs400es failed, err:%d\n",
1360 mmc_hostname(host), err);
1364 /* Set host controller to HS400 timing and frequency */
1365 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1367 /* Controller enable enhanced strobe function */
1368 host->ios.enhanced_strobe = true;
1369 if (host->ops->hs400_enhanced_strobe)
1370 host->ops->hs400_enhanced_strobe(host, &host->ios);
1372 err = mmc_switch_status(card);
1379 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1384 static void mmc_select_driver_type(struct mmc_card *card)
1386 int card_drv_type, drive_strength, drv_type;
1388 card_drv_type = card->ext_csd.raw_driver_strength |
1389 mmc_driver_type_mask(0);
1391 drive_strength = mmc_select_drive_strength(card,
1392 card->ext_csd.hs200_max_dtr,
1393 card_drv_type, &drv_type);
1395 card->drive_strength = drive_strength;
1398 mmc_set_driver_type(card->host, drv_type);
1402 * For device supporting HS200 mode, the following sequence
1403 * should be done before executing the tuning process.
1404 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1405 * 2. switch to HS200 mode
1406 * 3. set the clock to > 52Mhz and <=200MHz
1408 static int mmc_select_hs200(struct mmc_card *card)
1410 struct mmc_host *host = card->host;
1411 unsigned int old_timing, old_signal_voltage;
1415 old_signal_voltage = host->ios.signal_voltage;
1416 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1417 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1419 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1420 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1422 /* If fails try again during next card power cycle */
1426 mmc_select_driver_type(card);
1429 * Set the bus width(4 or 8) with host's support and
1430 * switch to HS200 mode if bus width is set successfully.
1432 err = mmc_select_bus_width(card);
1434 val = EXT_CSD_TIMING_HS200 |
1435 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1436 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1437 EXT_CSD_HS_TIMING, val,
1438 card->ext_csd.generic_cmd6_time, 0,
1442 old_timing = host->ios.timing;
1443 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1446 * For HS200, CRC errors are not a reliable way to know the
1447 * switch failed. If there really is a problem, we would expect
1448 * tuning will fail and the result ends up the same.
1450 err = __mmc_switch_status(card, false);
1453 * mmc_select_timing() assumes timing has not changed if
1454 * it is a switch error.
1456 if (err == -EBADMSG)
1457 mmc_set_timing(host, old_timing);
1461 /* fall back to the old signal voltage, if fails report error */
1462 if (mmc_set_signal_voltage(host, old_signal_voltage))
1465 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1472 * Activate High Speed, HS200 or HS400ES mode if supported.
1474 static int mmc_select_timing(struct mmc_card *card)
1478 if (!mmc_can_ext_csd(card))
1481 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1482 err = mmc_select_hs400es(card);
1483 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1484 err = mmc_select_hs200(card);
1485 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1486 err = mmc_select_hs(card);
1488 if (err && err != -EBADMSG)
1493 * Set the bus speed to the selected bus timing.
1494 * If timing is not selected, backward compatible is the default.
1496 mmc_set_bus_speed(card);
1501 * Execute tuning sequence to seek the proper bus operating
1502 * conditions for HS200 and HS400, which sends CMD21 to the device.
1504 static int mmc_hs200_tuning(struct mmc_card *card)
1506 struct mmc_host *host = card->host;
1509 * Timing should be adjusted to the HS400 target
1510 * operation frequency for tuning process
1512 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1513 host->ios.bus_width == MMC_BUS_WIDTH_8)
1514 if (host->ops->prepare_hs400_tuning)
1515 host->ops->prepare_hs400_tuning(host, &host->ios);
1517 return mmc_execute_tuning(card);
1521 * Handle the detection and initialisation of a card.
1523 * In the case of a resume, "oldcard" will contain the card
1524 * we're trying to reinitialise.
1526 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1527 struct mmc_card *oldcard)
1529 struct mmc_card *card;
1534 WARN_ON(!host->claimed);
1536 /* Set correct bus mode for MMC before attempting init */
1537 if (!mmc_host_is_spi(host))
1538 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1541 * Since we're changing the OCR value, we seem to
1542 * need to tell some cards to go back to the idle
1543 * state. We wait 1ms to give cards time to
1545 * mmc_go_idle is needed for eMMC that are asleep
1549 /* The extra bit indicates that we support high capacity */
1550 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1555 * For SPI, enable CRC as appropriate.
1557 if (mmc_host_is_spi(host)) {
1558 err = mmc_spi_set_crc(host, use_spi_crc);
1564 * Fetch CID from card.
1566 if (mmc_host_is_spi(host))
1567 err = mmc_send_cid(host, cid);
1569 err = mmc_all_send_cid(host, cid);
1574 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1582 * Allocate card structure.
1584 card = mmc_alloc_card(host, &mmc_type);
1586 err = PTR_ERR(card);
1591 card->type = MMC_TYPE_MMC;
1593 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1597 * Call the optional HC's init_card function to handle quirks.
1599 if (host->ops->init_card)
1600 host->ops->init_card(host, card);
1603 * For native busses: set card RCA and quit open drain mode.
1605 if (!mmc_host_is_spi(host)) {
1606 err = mmc_set_relative_addr(card);
1610 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1615 * Fetch CSD from card.
1617 err = mmc_send_csd(card, card->raw_csd);
1621 err = mmc_decode_csd(card);
1624 err = mmc_decode_cid(card);
1630 * handling only for cards supporting DSR and hosts requesting
1633 if (card->csd.dsr_imp && host->dsr_req)
1637 * Select card, as all following commands rely on that.
1639 if (!mmc_host_is_spi(host)) {
1640 err = mmc_select_card(card);
1646 /* Read extended CSD. */
1647 err = mmc_read_ext_csd(card);
1652 * If doing byte addressing, check if required to do sector
1653 * addressing. Handle the case of <2GB cards needing sector
1654 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1655 * ocr register has bit 30 set for sector addressing.
1658 mmc_card_set_blockaddr(card);
1660 /* Erase size depends on CSD and Extended CSD */
1661 mmc_set_erase_size(card);
1665 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
1666 * bit. This bit will be lost every time after a reset or power off.
1668 if (card->ext_csd.partition_setting_completed ||
1669 (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
1670 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1671 EXT_CSD_ERASE_GROUP_DEF, 1,
1672 card->ext_csd.generic_cmd6_time);
1674 if (err && err != -EBADMSG)
1680 * Just disable enhanced area off & sz
1681 * will try to enable ERASE_GROUP_DEF
1682 * during next time reinit
1684 card->ext_csd.enhanced_area_offset = -EINVAL;
1685 card->ext_csd.enhanced_area_size = -EINVAL;
1687 card->ext_csd.erase_group_def = 1;
1689 * enable ERASE_GRP_DEF successfully.
1690 * This will affect the erase size, so
1691 * here need to reset erase size
1693 mmc_set_erase_size(card);
1698 * Ensure eMMC user default partition is enabled
1700 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1701 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1702 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1703 card->ext_csd.part_config,
1704 card->ext_csd.part_time);
1705 if (err && err != -EBADMSG)
1710 * Enable power_off_notification byte in the ext_csd register
1712 if (card->ext_csd.rev >= 6) {
1713 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1714 EXT_CSD_POWER_OFF_NOTIFICATION,
1716 card->ext_csd.generic_cmd6_time);
1717 if (err && err != -EBADMSG)
1721 * The err can be -EBADMSG or 0,
1722 * so check for success and update the flag
1725 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1729 * Select timing interface
1731 err = mmc_select_timing(card);
1735 if (mmc_card_hs200(card)) {
1736 err = mmc_hs200_tuning(card);
1740 err = mmc_select_hs400(card);
1744 /* Select the desired bus width optionally */
1745 err = mmc_select_bus_width(card);
1746 if (err > 0 && mmc_card_hs(card)) {
1747 err = mmc_select_hs_ddr(card);
1754 * Choose the power class with selected bus interface
1756 mmc_select_powerclass(card);
1759 * Enable HPI feature (if supported)
1761 if (card->ext_csd.hpi) {
1762 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1763 EXT_CSD_HPI_MGMT, 1,
1764 card->ext_csd.generic_cmd6_time);
1765 if (err && err != -EBADMSG)
1768 pr_warn("%s: Enabling HPI failed\n",
1769 mmc_hostname(card->host));
1772 card->ext_csd.hpi_en = 1;
1776 * If cache size is higher than 0, this indicates
1777 * the existence of cache and it can be turned on.
1779 if (!mmc_card_broken_hpi(card) &&
1780 card->ext_csd.cache_size > 0) {
1781 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1782 EXT_CSD_CACHE_CTRL, 1,
1783 card->ext_csd.generic_cmd6_time);
1784 if (err && err != -EBADMSG)
1788 * Only if no error, cache is turned on successfully.
1791 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1792 mmc_hostname(card->host), err);
1793 card->ext_csd.cache_ctrl = 0;
1796 card->ext_csd.cache_ctrl = 1;
1801 * The mandatory minimum values are defined for packed command.
1804 if (card->ext_csd.max_packed_writes >= 3 &&
1805 card->ext_csd.max_packed_reads >= 5 &&
1806 host->caps2 & MMC_CAP2_PACKED_CMD) {
1807 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1808 EXT_CSD_EXP_EVENTS_CTRL,
1809 EXT_CSD_PACKED_EVENT_EN,
1810 card->ext_csd.generic_cmd6_time);
1811 if (err && err != -EBADMSG)
1814 pr_warn("%s: Enabling packed event failed\n",
1815 mmc_hostname(card->host));
1816 card->ext_csd.packed_event_en = 0;
1819 card->ext_csd.packed_event_en = 1;
1830 mmc_remove_card(card);
1835 static int mmc_can_sleep(struct mmc_card *card)
1837 return (card && card->ext_csd.rev >= 3);
1840 static int mmc_sleep(struct mmc_host *host)
1842 struct mmc_command cmd = {};
1843 struct mmc_card *card = host->card;
1844 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1847 /* Re-tuning can't be done once the card is deselected */
1848 mmc_retune_hold(host);
1850 err = mmc_deselect_cards(host);
1854 cmd.opcode = MMC_SLEEP_AWAKE;
1855 cmd.arg = card->rca << 16;
1859 * If the max_busy_timeout of the host is specified, validate it against
1860 * the sleep cmd timeout. A failure means we need to prevent the host
1861 * from doing hw busy detection, which is done by converting to a R1
1862 * response instead of a R1B.
1864 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1865 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1867 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1868 cmd.busy_timeout = timeout_ms;
1871 err = mmc_wait_for_cmd(host, &cmd, 0);
1876 * If the host does not wait while the card signals busy, then we will
1877 * will have to wait the sleep/awake timeout. Note, we cannot use the
1878 * SEND_STATUS command to poll the status because that command (and most
1879 * others) is invalid while the card sleeps.
1881 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1882 mmc_delay(timeout_ms);
1885 mmc_retune_release(host);
1889 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1892 mmc_card_mmc(card) &&
1893 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1896 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1898 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1901 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1902 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1903 timeout = card->ext_csd.power_off_longtime;
1905 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1906 EXT_CSD_POWER_OFF_NOTIFICATION,
1907 notify_type, timeout, 0, true, false, false);
1909 pr_err("%s: Power Off Notification timed out, %u\n",
1910 mmc_hostname(card->host), timeout);
1912 /* Disable the power off notification after the switch operation. */
1913 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1919 * Host is being removed. Free up the current card.
1921 static void mmc_remove(struct mmc_host *host)
1923 mmc_remove_card(host->card);
1928 * Card detection - card is alive.
1930 static int mmc_alive(struct mmc_host *host)
1932 return mmc_send_status(host->card, NULL);
1936 * Card detection callback from host.
1938 static void mmc_detect(struct mmc_host *host)
1942 mmc_get_card(host->card);
1945 * Just check if our card has been removed.
1947 err = _mmc_detect_card_removed(host);
1949 mmc_put_card(host->card);
1954 mmc_claim_host(host);
1955 mmc_detach_bus(host);
1956 mmc_power_off(host);
1957 mmc_release_host(host);
1961 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1964 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1965 EXT_CSD_POWER_OFF_LONG;
1967 mmc_claim_host(host);
1969 if (mmc_card_suspended(host->card))
1972 if (mmc_card_doing_bkops(host->card)) {
1973 err = mmc_stop_bkops(host->card);
1978 err = mmc_flush_cache(host->card);
1982 if (mmc_can_poweroff_notify(host->card) &&
1983 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1984 err = mmc_poweroff_notify(host->card, notify_type);
1985 else if (mmc_can_sleep(host->card))
1986 err = mmc_sleep(host);
1987 else if (!mmc_host_is_spi(host))
1988 err = mmc_deselect_cards(host);
1991 mmc_power_off(host);
1992 mmc_card_set_suspended(host->card);
1995 mmc_release_host(host);
2002 static int mmc_suspend(struct mmc_host *host)
2006 err = _mmc_suspend(host, true);
2008 pm_runtime_disable(&host->card->dev);
2009 pm_runtime_set_suspended(&host->card->dev);
2016 * This function tries to determine if the same card is still present
2017 * and, if so, restore all state to it.
2019 static int _mmc_resume(struct mmc_host *host)
2023 mmc_claim_host(host);
2025 if (!mmc_card_suspended(host->card))
2028 mmc_power_up(host, host->card->ocr);
2029 err = mmc_init_card(host, host->card->ocr, host->card);
2030 mmc_card_clr_suspended(host->card);
2033 mmc_release_host(host);
2040 static int mmc_shutdown(struct mmc_host *host)
2045 * In a specific case for poweroff notify, we need to resume the card
2046 * before we can shutdown it properly.
2048 if (mmc_can_poweroff_notify(host->card) &&
2049 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
2050 err = _mmc_resume(host);
2053 err = _mmc_suspend(host, false);
2059 * Callback for resume.
2061 static int mmc_resume(struct mmc_host *host)
2063 pm_runtime_enable(&host->card->dev);
2068 * Callback for runtime_suspend.
2070 static int mmc_runtime_suspend(struct mmc_host *host)
2074 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2077 err = _mmc_suspend(host, true);
2079 pr_err("%s: error %d doing aggressive suspend\n",
2080 mmc_hostname(host), err);
2086 * Callback for runtime_resume.
2088 static int mmc_runtime_resume(struct mmc_host *host)
2092 err = _mmc_resume(host);
2093 if (err && err != -ENOMEDIUM)
2094 pr_err("%s: error %d doing runtime resume\n",
2095 mmc_hostname(host), err);
2100 int mmc_can_reset(struct mmc_card *card)
2104 rst_n_function = card->ext_csd.rst_n_function;
2105 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2109 EXPORT_SYMBOL(mmc_can_reset);
2111 static int mmc_reset(struct mmc_host *host)
2113 struct mmc_card *card = host->card;
2116 * In the case of recovery, we can't expect flushing the cache to work
2117 * always, but we have a go and ignore errors.
2119 mmc_flush_cache(host->card);
2121 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2122 mmc_can_reset(card)) {
2123 /* If the card accept RST_n signal, send it. */
2124 mmc_set_clock(host, host->f_init);
2125 host->ops->hw_reset(host);
2126 /* Set initial state and call mmc_set_ios */
2127 mmc_set_initial_state(host);
2129 /* Do a brute force power cycle */
2130 mmc_power_cycle(host, card->ocr);
2132 return mmc_init_card(host, card->ocr, card);
2135 static const struct mmc_bus_ops mmc_ops = {
2136 .remove = mmc_remove,
2137 .detect = mmc_detect,
2138 .suspend = mmc_suspend,
2139 .resume = mmc_resume,
2140 .runtime_suspend = mmc_runtime_suspend,
2141 .runtime_resume = mmc_runtime_resume,
2143 .shutdown = mmc_shutdown,
2148 * Starting point for MMC card init.
2150 int mmc_attach_mmc(struct mmc_host *host)
2155 WARN_ON(!host->claimed);
2157 /* Set correct bus mode for MMC before attempting attach */
2158 if (!mmc_host_is_spi(host))
2159 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2161 err = mmc_send_op_cond(host, 0, &ocr);
2165 mmc_attach_bus(host, &mmc_ops);
2166 if (host->ocr_avail_mmc)
2167 host->ocr_avail = host->ocr_avail_mmc;
2170 * We need to get OCR a different way for SPI.
2172 if (mmc_host_is_spi(host)) {
2173 err = mmc_spi_read_ocr(host, 1, &ocr);
2178 rocr = mmc_select_voltage(host, ocr);
2181 * Can we support the voltage of the card?
2189 * Detect and init the card.
2191 err = mmc_init_card(host, rocr, NULL);
2195 mmc_release_host(host);
2196 err = mmc_add_card(host->card);
2200 mmc_claim_host(host);
2204 mmc_remove_card(host->card);
2205 mmc_claim_host(host);
2208 mmc_detach_bus(host);
2210 pr_err("%s: error %d whilst initialising MMC card\n",
2211 mmc_hostname(host), err);