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>
32 #define DEFAULT_CMD6_TIMEOUT_MS 500
33 #define MIN_CACHE_EN_TIMEOUT_MS 1600
35 static const unsigned int tran_exp[] = {
36 10000, 100000, 1000000, 10000000,
40 static const unsigned char tran_mant[] = {
41 0, 10, 12, 13, 15, 20, 25, 30,
42 35, 40, 45, 50, 55, 60, 70, 80,
45 static const unsigned int taac_exp[] = {
46 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
49 static const unsigned int taac_mant[] = {
50 0, 10, 12, 13, 15, 20, 25, 30,
51 35, 40, 45, 50, 55, 60, 70, 80,
54 #define UNSTUFF_BITS(resp,start,size) \
56 const int __size = size; \
57 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
58 const int __off = 3 - ((start) / 32); \
59 const int __shft = (start) & 31; \
62 __res = resp[__off] >> __shft; \
63 if (__size + __shft > 32) \
64 __res |= resp[__off-1] << ((32 - __shft) % 32); \
69 * Given the decoded CSD structure, decode the raw CID to our CID structure.
71 static int mmc_decode_cid(struct mmc_card *card)
73 u32 *resp = card->raw_cid;
76 * The selection of the format here is based upon published
77 * specs from sandisk and from what people have reported.
79 switch (card->csd.mmca_vsn) {
80 case 0: /* MMC v1.0 - v1.2 */
81 case 1: /* MMC v1.4 */
82 card->cid.manfid = UNSTUFF_BITS(resp, 104, 24);
83 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
84 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
85 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
86 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
87 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
88 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
89 card->cid.prod_name[6] = UNSTUFF_BITS(resp, 48, 8);
90 card->cid.hwrev = UNSTUFF_BITS(resp, 44, 4);
91 card->cid.fwrev = UNSTUFF_BITS(resp, 40, 4);
92 card->cid.serial = UNSTUFF_BITS(resp, 16, 24);
93 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
94 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
97 case 2: /* MMC v2.0 - v2.2 */
98 case 3: /* MMC v3.1 - v3.3 */
100 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
101 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
102 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
103 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
104 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
105 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
106 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
107 card->cid.prod_name[5] = UNSTUFF_BITS(resp, 56, 8);
108 card->cid.prv = UNSTUFF_BITS(resp, 48, 8);
109 card->cid.serial = UNSTUFF_BITS(resp, 16, 32);
110 card->cid.month = UNSTUFF_BITS(resp, 12, 4);
111 card->cid.year = UNSTUFF_BITS(resp, 8, 4) + 1997;
115 pr_err("%s: card has unknown MMCA version %d\n",
116 mmc_hostname(card->host), card->csd.mmca_vsn);
123 static void mmc_set_erase_size(struct mmc_card *card)
125 if (card->ext_csd.erase_group_def & 1)
126 card->erase_size = card->ext_csd.hc_erase_size;
128 card->erase_size = card->csd.erase_size;
130 mmc_init_erase(card);
134 * Given a 128-bit response, decode to our card CSD structure.
136 static int mmc_decode_csd(struct mmc_card *card)
138 struct mmc_csd *csd = &card->csd;
139 unsigned int e, m, a, b;
140 u32 *resp = card->raw_csd;
143 * We only understand CSD structure v1.1 and v1.2.
144 * v1.2 has extra information in bits 15, 11 and 10.
145 * We also support eMMC v4.4 & v4.41.
147 csd->structure = UNSTUFF_BITS(resp, 126, 2);
148 if (csd->structure == 0) {
149 pr_err("%s: unrecognised CSD structure version %d\n",
150 mmc_hostname(card->host), csd->structure);
154 csd->mmca_vsn = UNSTUFF_BITS(resp, 122, 4);
155 m = UNSTUFF_BITS(resp, 115, 4);
156 e = UNSTUFF_BITS(resp, 112, 3);
157 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
158 csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
160 m = UNSTUFF_BITS(resp, 99, 4);
161 e = UNSTUFF_BITS(resp, 96, 3);
162 csd->max_dtr = tran_exp[e] * tran_mant[m];
163 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
165 e = UNSTUFF_BITS(resp, 47, 3);
166 m = UNSTUFF_BITS(resp, 62, 12);
167 csd->capacity = (1 + m) << (e + 2);
169 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
170 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
171 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
172 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
173 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
174 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
175 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
176 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
178 if (csd->write_blkbits >= 9) {
179 a = UNSTUFF_BITS(resp, 42, 5);
180 b = UNSTUFF_BITS(resp, 37, 5);
181 csd->erase_size = (a + 1) * (b + 1);
182 csd->erase_size <<= csd->write_blkbits - 9;
188 static void mmc_select_card_type(struct mmc_card *card)
190 struct mmc_host *host = card->host;
191 u8 card_type = card->ext_csd.raw_card_type;
192 u32 caps = host->caps, caps2 = host->caps2;
193 unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
194 unsigned int avail_type = 0;
196 if (caps & MMC_CAP_MMC_HIGHSPEED &&
197 card_type & EXT_CSD_CARD_TYPE_HS_26) {
198 hs_max_dtr = MMC_HIGH_26_MAX_DTR;
199 avail_type |= EXT_CSD_CARD_TYPE_HS_26;
202 if (caps & MMC_CAP_MMC_HIGHSPEED &&
203 card_type & EXT_CSD_CARD_TYPE_HS_52) {
204 hs_max_dtr = MMC_HIGH_52_MAX_DTR;
205 avail_type |= EXT_CSD_CARD_TYPE_HS_52;
208 if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
209 card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
210 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
211 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
214 if (caps & MMC_CAP_1_2V_DDR &&
215 card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
216 hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
217 avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
220 if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
221 card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
222 hs200_max_dtr = MMC_HS200_MAX_DTR;
223 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
226 if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
227 card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
228 hs200_max_dtr = MMC_HS200_MAX_DTR;
229 avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
232 if (caps2 & MMC_CAP2_HS400_1_8V &&
233 card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
234 hs200_max_dtr = MMC_HS200_MAX_DTR;
235 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
238 if (caps2 & MMC_CAP2_HS400_1_2V &&
239 card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
240 hs200_max_dtr = MMC_HS200_MAX_DTR;
241 avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
244 if ((caps2 & MMC_CAP2_HS400_ES) &&
245 card->ext_csd.strobe_support &&
246 (avail_type & EXT_CSD_CARD_TYPE_HS400))
247 avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
249 card->ext_csd.hs_max_dtr = hs_max_dtr;
250 card->ext_csd.hs200_max_dtr = hs200_max_dtr;
251 card->mmc_avail_type = avail_type;
254 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
256 u8 hc_erase_grp_sz, hc_wp_grp_sz;
259 * Disable these attributes by default
261 card->ext_csd.enhanced_area_offset = -EINVAL;
262 card->ext_csd.enhanced_area_size = -EINVAL;
265 * Enhanced area feature support -- check whether the eMMC
266 * card has the Enhanced area enabled. If so, export enhanced
267 * area offset and size to user by adding sysfs interface.
269 if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
270 (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
271 if (card->ext_csd.partition_setting_completed) {
273 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
275 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
278 * calculate the enhanced data area offset, in bytes
280 card->ext_csd.enhanced_area_offset =
281 (((unsigned long long)ext_csd[139]) << 24) +
282 (((unsigned long long)ext_csd[138]) << 16) +
283 (((unsigned long long)ext_csd[137]) << 8) +
284 (((unsigned long long)ext_csd[136]));
285 if (mmc_card_blockaddr(card))
286 card->ext_csd.enhanced_area_offset <<= 9;
288 * calculate the enhanced data area size, in kilobytes
290 card->ext_csd.enhanced_area_size =
291 (ext_csd[142] << 16) + (ext_csd[141] << 8) +
293 card->ext_csd.enhanced_area_size *=
294 (size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
295 card->ext_csd.enhanced_area_size <<= 9;
297 pr_warn("%s: defines enhanced area without partition setting complete\n",
298 mmc_hostname(card->host));
303 static void mmc_part_add(struct mmc_card *card, u64 size,
304 unsigned int part_cfg, char *name, int idx, bool ro,
307 card->part[card->nr_parts].size = size;
308 card->part[card->nr_parts].part_cfg = part_cfg;
309 sprintf(card->part[card->nr_parts].name, name, idx);
310 card->part[card->nr_parts].force_ro = ro;
311 card->part[card->nr_parts].area_type = area_type;
315 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
318 u8 hc_erase_grp_sz, hc_wp_grp_sz;
322 * General purpose partition feature support --
323 * If ext_csd has the size of general purpose partitions,
324 * set size, part_cfg, partition name in mmc_part.
326 if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
327 EXT_CSD_PART_SUPPORT_PART_EN) {
329 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
331 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
333 for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
334 if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
335 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
336 !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
338 if (card->ext_csd.partition_setting_completed == 0) {
339 pr_warn("%s: has partition size defined without partition complete\n",
340 mmc_hostname(card->host));
344 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
346 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
348 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
349 part_size *= (hc_erase_grp_sz * hc_wp_grp_sz);
350 mmc_part_add(card, part_size << 19,
351 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
353 MMC_BLK_DATA_AREA_GP);
358 /* Minimum partition switch timeout in milliseconds */
359 #define MMC_MIN_PART_SWITCH_TIME 300
362 * Decode extended CSD.
364 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
368 struct device_node *np;
369 bool broken_hpi = false;
371 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
372 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
373 if (card->csd.structure == 3) {
374 if (card->ext_csd.raw_ext_csd_structure > 2) {
375 pr_err("%s: unrecognised EXT_CSD structure "
376 "version %d\n", mmc_hostname(card->host),
377 card->ext_csd.raw_ext_csd_structure);
383 np = mmc_of_find_child_device(card->host, 0);
384 if (np && of_device_is_compatible(np, "mmc-card"))
385 broken_hpi = of_property_read_bool(np, "broken-hpi");
389 * The EXT_CSD format is meant to be forward compatible. As long
390 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
391 * are authorized, see JEDEC JESD84-B50 section B.8.
393 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
395 /* fixup device after ext_csd revision field is updated */
396 mmc_fixup_device(card, mmc_ext_csd_fixups);
398 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
399 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
400 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
401 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
402 if (card->ext_csd.rev >= 2) {
403 card->ext_csd.sectors =
404 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
405 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
406 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
407 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
409 /* Cards with density > 2GiB are sector addressed */
410 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
411 mmc_card_set_blockaddr(card);
414 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
415 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
416 mmc_select_card_type(card);
418 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
419 card->ext_csd.raw_erase_timeout_mult =
420 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
421 card->ext_csd.raw_hc_erase_grp_size =
422 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
423 if (card->ext_csd.rev >= 3) {
424 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
425 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
427 /* EXT_CSD value is in units of 10ms, but we store in ms */
428 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
430 /* Sleep / awake timeout in 100ns units */
431 if (sa_shift > 0 && sa_shift <= 0x17)
432 card->ext_csd.sa_timeout =
433 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
434 card->ext_csd.erase_group_def =
435 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
436 card->ext_csd.hc_erase_timeout = 300 *
437 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
438 card->ext_csd.hc_erase_size =
439 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
441 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
444 * There are two boot regions of equal size, defined in
447 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
448 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
449 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
450 mmc_part_add(card, part_size,
451 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
453 MMC_BLK_DATA_AREA_BOOT);
458 card->ext_csd.raw_hc_erase_gap_size =
459 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
460 card->ext_csd.raw_sec_trim_mult =
461 ext_csd[EXT_CSD_SEC_TRIM_MULT];
462 card->ext_csd.raw_sec_erase_mult =
463 ext_csd[EXT_CSD_SEC_ERASE_MULT];
464 card->ext_csd.raw_sec_feature_support =
465 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
466 card->ext_csd.raw_trim_mult =
467 ext_csd[EXT_CSD_TRIM_MULT];
468 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
469 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
470 if (card->ext_csd.rev >= 4) {
471 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
472 EXT_CSD_PART_SETTING_COMPLETED)
473 card->ext_csd.partition_setting_completed = 1;
475 card->ext_csd.partition_setting_completed = 0;
477 mmc_manage_enhanced_area(card, ext_csd);
479 mmc_manage_gp_partitions(card, ext_csd);
481 card->ext_csd.sec_trim_mult =
482 ext_csd[EXT_CSD_SEC_TRIM_MULT];
483 card->ext_csd.sec_erase_mult =
484 ext_csd[EXT_CSD_SEC_ERASE_MULT];
485 card->ext_csd.sec_feature_support =
486 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
487 card->ext_csd.trim_timeout = 300 *
488 ext_csd[EXT_CSD_TRIM_MULT];
491 * Note that the call to mmc_part_add above defaults to read
492 * only. If this default assumption is changed, the call must
493 * take into account the value of boot_locked below.
495 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
496 card->ext_csd.boot_ro_lockable = true;
498 /* Save power class values */
499 card->ext_csd.raw_pwr_cl_52_195 =
500 ext_csd[EXT_CSD_PWR_CL_52_195];
501 card->ext_csd.raw_pwr_cl_26_195 =
502 ext_csd[EXT_CSD_PWR_CL_26_195];
503 card->ext_csd.raw_pwr_cl_52_360 =
504 ext_csd[EXT_CSD_PWR_CL_52_360];
505 card->ext_csd.raw_pwr_cl_26_360 =
506 ext_csd[EXT_CSD_PWR_CL_26_360];
507 card->ext_csd.raw_pwr_cl_200_195 =
508 ext_csd[EXT_CSD_PWR_CL_200_195];
509 card->ext_csd.raw_pwr_cl_200_360 =
510 ext_csd[EXT_CSD_PWR_CL_200_360];
511 card->ext_csd.raw_pwr_cl_ddr_52_195 =
512 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
513 card->ext_csd.raw_pwr_cl_ddr_52_360 =
514 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
515 card->ext_csd.raw_pwr_cl_ddr_200_360 =
516 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
519 if (card->ext_csd.rev >= 5) {
520 /* Adjust production date as per JEDEC JESD84-B451 */
521 if (card->cid.year < 2010)
522 card->cid.year += 16;
524 /* check whether the eMMC card supports BKOPS */
525 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
526 card->ext_csd.bkops = 1;
527 card->ext_csd.man_bkops_en =
528 (ext_csd[EXT_CSD_BKOPS_EN] &
529 EXT_CSD_MANUAL_BKOPS_MASK);
530 card->ext_csd.raw_bkops_status =
531 ext_csd[EXT_CSD_BKOPS_STATUS];
532 if (card->ext_csd.man_bkops_en)
533 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
534 mmc_hostname(card->host));
535 card->ext_csd.auto_bkops_en =
536 (ext_csd[EXT_CSD_BKOPS_EN] &
537 EXT_CSD_AUTO_BKOPS_MASK);
538 if (card->ext_csd.auto_bkops_en)
539 pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
540 mmc_hostname(card->host));
543 /* check whether the eMMC card supports HPI */
544 if (!mmc_card_broken_hpi(card) &&
545 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
546 card->ext_csd.hpi = 1;
547 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
548 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
550 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
552 * Indicate the maximum timeout to close
553 * a command interrupted by HPI
555 card->ext_csd.out_of_int_time =
556 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
559 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
560 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
563 * RPMB regions are defined in multiples of 128K.
565 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
566 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
567 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
568 EXT_CSD_PART_CONFIG_ACC_RPMB,
570 MMC_BLK_DATA_AREA_RPMB);
574 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
575 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
576 card->erased_byte = 0xFF;
578 card->erased_byte = 0x0;
580 /* eMMC v4.5 or later */
581 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
582 if (card->ext_csd.rev >= 6) {
583 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
585 card->ext_csd.generic_cmd6_time = 10 *
586 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
587 card->ext_csd.power_off_longtime = 10 *
588 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
590 card->ext_csd.cache_size =
591 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
592 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
593 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
594 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
596 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
597 card->ext_csd.data_sector_size = 4096;
599 card->ext_csd.data_sector_size = 512;
601 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
602 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
603 card->ext_csd.data_tag_unit_size =
604 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
605 (card->ext_csd.data_sector_size);
607 card->ext_csd.data_tag_unit_size = 0;
610 card->ext_csd.max_packed_writes =
611 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
612 card->ext_csd.max_packed_reads =
613 ext_csd[EXT_CSD_MAX_PACKED_READS];
615 card->ext_csd.data_sector_size = 512;
619 * GENERIC_CMD6_TIME is to be used "unless a specific timeout is defined
620 * when accessing a specific field", so use it here if there is no
621 * PARTITION_SWITCH_TIME.
623 if (!card->ext_csd.part_time)
624 card->ext_csd.part_time = card->ext_csd.generic_cmd6_time;
625 /* Some eMMC set the value too low so set a minimum */
626 if (card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
627 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
629 /* eMMC v5 or later */
630 if (card->ext_csd.rev >= 7) {
631 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
633 card->ext_csd.ffu_capable =
634 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
635 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
637 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
638 card->ext_csd.device_life_time_est_typ_a =
639 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
640 card->ext_csd.device_life_time_est_typ_b =
641 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
644 /* eMMC v5.1 or later */
645 if (card->ext_csd.rev >= 8) {
646 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
647 EXT_CSD_CMDQ_SUPPORTED;
648 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
649 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
650 /* Exclude inefficiently small queue depths */
651 if (card->ext_csd.cmdq_depth <= 2) {
652 card->ext_csd.cmdq_support = false;
653 card->ext_csd.cmdq_depth = 0;
655 if (card->ext_csd.cmdq_support) {
656 pr_debug("%s: Command Queue supported depth %u\n",
657 mmc_hostname(card->host),
658 card->ext_csd.cmdq_depth);
665 static int mmc_read_ext_csd(struct mmc_card *card)
670 if (!mmc_can_ext_csd(card))
673 err = mmc_get_ext_csd(card, &ext_csd);
675 /* If the host or the card can't do the switch,
676 * fail more gracefully. */
683 * High capacity cards should have this "magic" size
684 * stored in their CSD.
686 if (card->csd.capacity == (4096 * 512)) {
687 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
688 mmc_hostname(card->host));
690 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
691 mmc_hostname(card->host));
698 err = mmc_decode_ext_csd(card, ext_csd);
703 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
708 if (bus_width == MMC_BUS_WIDTH_1)
711 err = mmc_get_ext_csd(card, &bw_ext_csd);
715 /* only compare read only fields */
716 err = !((card->ext_csd.raw_partition_support ==
717 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
718 (card->ext_csd.raw_erased_mem_count ==
719 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
720 (card->ext_csd.rev ==
721 bw_ext_csd[EXT_CSD_REV]) &&
722 (card->ext_csd.raw_ext_csd_structure ==
723 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
724 (card->ext_csd.raw_card_type ==
725 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
726 (card->ext_csd.raw_s_a_timeout ==
727 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
728 (card->ext_csd.raw_hc_erase_gap_size ==
729 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
730 (card->ext_csd.raw_erase_timeout_mult ==
731 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
732 (card->ext_csd.raw_hc_erase_grp_size ==
733 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
734 (card->ext_csd.raw_sec_trim_mult ==
735 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
736 (card->ext_csd.raw_sec_erase_mult ==
737 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
738 (card->ext_csd.raw_sec_feature_support ==
739 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
740 (card->ext_csd.raw_trim_mult ==
741 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
742 (card->ext_csd.raw_sectors[0] ==
743 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
744 (card->ext_csd.raw_sectors[1] ==
745 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
746 (card->ext_csd.raw_sectors[2] ==
747 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
748 (card->ext_csd.raw_sectors[3] ==
749 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
750 (card->ext_csd.raw_pwr_cl_52_195 ==
751 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
752 (card->ext_csd.raw_pwr_cl_26_195 ==
753 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
754 (card->ext_csd.raw_pwr_cl_52_360 ==
755 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
756 (card->ext_csd.raw_pwr_cl_26_360 ==
757 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
758 (card->ext_csd.raw_pwr_cl_200_195 ==
759 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
760 (card->ext_csd.raw_pwr_cl_200_360 ==
761 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
762 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
763 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
764 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
765 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
766 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
767 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
776 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
777 card->raw_cid[2], card->raw_cid[3]);
778 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
779 card->raw_csd[2], card->raw_csd[3]);
780 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
781 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
782 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
783 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
784 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
785 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
786 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
787 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
788 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
789 MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
790 MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
791 card->ext_csd.device_life_time_est_typ_a,
792 card->ext_csd.device_life_time_est_typ_b);
793 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
794 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
795 card->ext_csd.enhanced_area_offset);
796 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
797 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
798 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
799 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
800 MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
802 static ssize_t mmc_fwrev_show(struct device *dev,
803 struct device_attribute *attr,
806 struct mmc_card *card = mmc_dev_to_card(dev);
808 if (card->ext_csd.rev < 7) {
809 return sprintf(buf, "0x%x\n", card->cid.fwrev);
811 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
812 card->ext_csd.fwrev);
816 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
818 static ssize_t mmc_dsr_show(struct device *dev,
819 struct device_attribute *attr,
822 struct mmc_card *card = mmc_dev_to_card(dev);
823 struct mmc_host *host = card->host;
825 if (card->csd.dsr_imp && host->dsr_req)
826 return sprintf(buf, "0x%x\n", host->dsr);
828 /* return default DSR value */
829 return sprintf(buf, "0x%x\n", 0x404);
832 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
834 static struct attribute *mmc_std_attrs[] = {
838 &dev_attr_erase_size.attr,
839 &dev_attr_preferred_erase_size.attr,
840 &dev_attr_fwrev.attr,
841 &dev_attr_ffu_capable.attr,
842 &dev_attr_hwrev.attr,
843 &dev_attr_manfid.attr,
845 &dev_attr_oemid.attr,
847 &dev_attr_pre_eol_info.attr,
848 &dev_attr_life_time.attr,
849 &dev_attr_serial.attr,
850 &dev_attr_enhanced_area_offset.attr,
851 &dev_attr_enhanced_area_size.attr,
852 &dev_attr_raw_rpmb_size_mult.attr,
853 &dev_attr_rel_sectors.attr,
856 &dev_attr_cmdq_en.attr,
859 ATTRIBUTE_GROUPS(mmc_std);
861 static struct device_type mmc_type = {
862 .groups = mmc_std_groups,
866 * Select the PowerClass for the current bus width
867 * If power class is defined for 4/8 bit bus in the
868 * extended CSD register, select it by executing the
869 * mmc_switch command.
871 static int __mmc_select_powerclass(struct mmc_card *card,
872 unsigned int bus_width)
874 struct mmc_host *host = card->host;
875 struct mmc_ext_csd *ext_csd = &card->ext_csd;
876 unsigned int pwrclass_val = 0;
879 switch (1 << host->ios.vdd) {
880 case MMC_VDD_165_195:
881 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
882 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
883 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
884 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
885 ext_csd->raw_pwr_cl_52_195 :
886 ext_csd->raw_pwr_cl_ddr_52_195;
887 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
888 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
899 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
900 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
901 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
902 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
903 ext_csd->raw_pwr_cl_52_360 :
904 ext_csd->raw_pwr_cl_ddr_52_360;
905 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
906 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
907 ext_csd->raw_pwr_cl_ddr_200_360 :
908 ext_csd->raw_pwr_cl_200_360;
911 pr_warn("%s: Voltage range not supported for power class\n",
916 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
917 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
918 EXT_CSD_PWR_CL_8BIT_SHIFT;
920 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
921 EXT_CSD_PWR_CL_4BIT_SHIFT;
923 /* If the power class is different from the default value */
924 if (pwrclass_val > 0) {
925 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
928 card->ext_csd.generic_cmd6_time);
934 static int mmc_select_powerclass(struct mmc_card *card)
936 struct mmc_host *host = card->host;
937 u32 bus_width, ext_csd_bits;
940 /* Power class selection is supported for versions >= 4.0 */
941 if (!mmc_can_ext_csd(card))
944 bus_width = host->ios.bus_width;
945 /* Power class values are defined only for 4/8 bit bus */
946 if (bus_width == MMC_BUS_WIDTH_1)
949 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
951 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
952 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
954 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
955 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
957 err = __mmc_select_powerclass(card, ext_csd_bits);
959 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
960 mmc_hostname(host), 1 << bus_width, ddr);
966 * Set the bus speed for the selected speed mode.
968 static void mmc_set_bus_speed(struct mmc_card *card)
970 unsigned int max_dtr = (unsigned int)-1;
972 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
973 max_dtr > card->ext_csd.hs200_max_dtr)
974 max_dtr = card->ext_csd.hs200_max_dtr;
975 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
976 max_dtr = card->ext_csd.hs_max_dtr;
977 else if (max_dtr > card->csd.max_dtr)
978 max_dtr = card->csd.max_dtr;
980 mmc_set_clock(card->host, max_dtr);
984 * Select the bus width amoung 4-bit and 8-bit(SDR).
985 * If the bus width is changed successfully, return the selected width value.
986 * Zero is returned instead of error value if the wide width is not supported.
988 static int mmc_select_bus_width(struct mmc_card *card)
990 static unsigned ext_csd_bits[] = {
994 static unsigned bus_widths[] = {
998 struct mmc_host *host = card->host;
999 unsigned idx, bus_width = 0;
1002 if (!mmc_can_ext_csd(card) ||
1003 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1006 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
1009 * Unlike SD, MMC cards dont have a configuration register to notify
1010 * supported bus width. So bus test command should be run to identify
1011 * the supported bus width or compare the ext csd values of current
1012 * bus width and ext csd values of 1 bit mode read earlier.
1014 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1016 * Host is capable of 8bit transfer, then switch
1017 * the device to work in 8bit transfer mode. If the
1018 * mmc switch command returns error then switch to
1019 * 4bit transfer mode. On success set the corresponding
1020 * bus width on the host.
1022 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1025 card->ext_csd.generic_cmd6_time);
1029 bus_width = bus_widths[idx];
1030 mmc_set_bus_width(host, bus_width);
1033 * If controller can't handle bus width test,
1034 * compare ext_csd previously read in 1 bit mode
1035 * against ext_csd at new bus width
1037 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1038 err = mmc_compare_ext_csds(card, bus_width);
1040 err = mmc_bus_test(card, bus_width);
1046 pr_warn("%s: switch to bus width %d failed\n",
1047 mmc_hostname(host), 1 << bus_width);
1055 * Switch to the high-speed mode
1057 static int mmc_select_hs(struct mmc_card *card)
1061 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1062 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1063 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1066 pr_warn("%s: switch to high-speed failed, err:%d\n",
1067 mmc_hostname(card->host), err);
1073 * Activate wide bus and DDR if supported.
1075 static int mmc_select_hs_ddr(struct mmc_card *card)
1077 struct mmc_host *host = card->host;
1078 u32 bus_width, ext_csd_bits;
1081 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1084 bus_width = host->ios.bus_width;
1085 if (bus_width == MMC_BUS_WIDTH_1)
1088 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1089 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1091 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1094 card->ext_csd.generic_cmd6_time,
1095 MMC_TIMING_MMC_DDR52,
1098 pr_err("%s: switch to bus width %d ddr failed\n",
1099 mmc_hostname(host), 1 << bus_width);
1104 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1107 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1109 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1110 * in the JEDEC spec for DDR.
1112 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1113 * host controller can support this, like some of the SDHCI
1114 * controller which connect to an eMMC device. Some of these
1115 * host controller still needs to use 1.8v vccq for supporting
1118 * So the sequence will be:
1119 * if (host and device can both support 1.2v IO)
1121 * else if (host and device can both support 1.8v IO)
1123 * so if host and device can only support 3.3v IO, this is the
1126 * WARNING: eMMC rules are NOT the same as SD DDR
1128 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
1129 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1134 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1135 host->caps & MMC_CAP_1_8V_DDR)
1136 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1138 /* make sure vccq is 3.3v after switching disaster */
1140 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1145 static int mmc_select_hs400(struct mmc_card *card)
1147 struct mmc_host *host = card->host;
1148 unsigned int max_dtr;
1153 * HS400 mode requires 8-bit bus width
1155 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1156 host->ios.bus_width == MMC_BUS_WIDTH_8))
1159 /* Switch card to HS mode */
1160 val = EXT_CSD_TIMING_HS;
1161 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1162 EXT_CSD_HS_TIMING, val,
1163 card->ext_csd.generic_cmd6_time, 0,
1166 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1167 mmc_hostname(host), err);
1171 /* Set host controller to HS timing */
1172 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1174 /* Reduce frequency to HS frequency */
1175 max_dtr = card->ext_csd.hs_max_dtr;
1176 mmc_set_clock(host, max_dtr);
1178 err = mmc_switch_status(card);
1182 /* Switch card to DDR */
1183 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1185 EXT_CSD_DDR_BUS_WIDTH_8,
1186 card->ext_csd.generic_cmd6_time);
1188 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1189 mmc_hostname(host), err);
1193 /* Switch card to HS400 */
1194 val = EXT_CSD_TIMING_HS400 |
1195 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1196 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1197 EXT_CSD_HS_TIMING, val,
1198 card->ext_csd.generic_cmd6_time, 0,
1201 pr_err("%s: switch to hs400 failed, err:%d\n",
1202 mmc_hostname(host), err);
1206 /* Set host controller to HS400 timing and frequency */
1207 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1208 mmc_set_bus_speed(card);
1210 err = mmc_switch_status(card);
1217 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1222 int mmc_hs200_to_hs400(struct mmc_card *card)
1224 return mmc_select_hs400(card);
1227 int mmc_hs400_to_hs200(struct mmc_card *card)
1229 struct mmc_host *host = card->host;
1230 unsigned int max_dtr;
1234 /* Reduce frequency to HS */
1235 max_dtr = card->ext_csd.hs_max_dtr;
1236 mmc_set_clock(host, max_dtr);
1238 /* Switch HS400 to HS DDR */
1239 val = EXT_CSD_TIMING_HS;
1240 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1241 val, card->ext_csd.generic_cmd6_time, 0,
1246 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1248 err = mmc_switch_status(card);
1252 /* Switch HS DDR to HS */
1253 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1254 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1255 0, true, false, true);
1259 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1261 err = mmc_switch_status(card);
1265 /* Switch HS to HS200 */
1266 val = EXT_CSD_TIMING_HS200 |
1267 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1268 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1269 val, card->ext_csd.generic_cmd6_time, 0,
1274 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1277 * For HS200, CRC errors are not a reliable way to know the switch
1278 * failed. If there really is a problem, we would expect tuning will
1279 * fail and the result ends up the same.
1281 err = __mmc_switch_status(card, false);
1285 mmc_set_bus_speed(card);
1290 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1295 static void mmc_select_driver_type(struct mmc_card *card)
1297 int card_drv_type, drive_strength, drv_type;
1299 card_drv_type = card->ext_csd.raw_driver_strength |
1300 mmc_driver_type_mask(0);
1302 drive_strength = mmc_select_drive_strength(card,
1303 card->ext_csd.hs200_max_dtr,
1304 card_drv_type, &drv_type);
1306 card->drive_strength = drive_strength;
1309 mmc_set_driver_type(card->host, drv_type);
1312 static int mmc_select_hs400es(struct mmc_card *card)
1314 struct mmc_host *host = card->host;
1318 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1323 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1324 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1326 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1327 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1329 /* If fails try again during next card power cycle */
1333 err = mmc_select_bus_width(card);
1337 /* Switch card to HS mode */
1338 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1339 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1340 card->ext_csd.generic_cmd6_time, 0,
1343 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1344 mmc_hostname(host), err);
1348 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1349 err = mmc_switch_status(card);
1353 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1355 /* Switch card to DDR with strobe bit */
1356 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1357 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1360 card->ext_csd.generic_cmd6_time);
1362 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1363 mmc_hostname(host), err);
1367 mmc_select_driver_type(card);
1369 /* Switch card to HS400 */
1370 val = EXT_CSD_TIMING_HS400 |
1371 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1372 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1373 EXT_CSD_HS_TIMING, val,
1374 card->ext_csd.generic_cmd6_time, 0,
1377 pr_err("%s: switch to hs400es failed, err:%d\n",
1378 mmc_hostname(host), err);
1382 /* Set host controller to HS400 timing and frequency */
1383 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1385 /* Controller enable enhanced strobe function */
1386 host->ios.enhanced_strobe = true;
1387 if (host->ops->hs400_enhanced_strobe)
1388 host->ops->hs400_enhanced_strobe(host, &host->ios);
1390 err = mmc_switch_status(card);
1397 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1403 * For device supporting HS200 mode, the following sequence
1404 * should be done before executing the tuning process.
1405 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1406 * 2. switch to HS200 mode
1407 * 3. set the clock to > 52Mhz and <=200MHz
1409 static int mmc_select_hs200(struct mmc_card *card)
1411 struct mmc_host *host = card->host;
1412 unsigned int old_timing, old_signal_voltage;
1416 old_signal_voltage = host->ios.signal_voltage;
1417 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1418 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1420 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1421 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1423 /* If fails try again during next card power cycle */
1427 mmc_select_driver_type(card);
1430 * Set the bus width(4 or 8) with host's support and
1431 * switch to HS200 mode if bus width is set successfully.
1433 err = mmc_select_bus_width(card);
1435 val = EXT_CSD_TIMING_HS200 |
1436 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1437 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1438 EXT_CSD_HS_TIMING, val,
1439 card->ext_csd.generic_cmd6_time, 0,
1443 old_timing = host->ios.timing;
1444 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1447 * For HS200, CRC errors are not a reliable way to know the
1448 * switch failed. If there really is a problem, we would expect
1449 * tuning will fail and the result ends up the same.
1451 err = __mmc_switch_status(card, false);
1454 * mmc_select_timing() assumes timing has not changed if
1455 * it is a switch error.
1457 if (err == -EBADMSG)
1458 mmc_set_timing(host, old_timing);
1462 /* fall back to the old signal voltage, if fails report error */
1463 if (mmc_set_signal_voltage(host, old_signal_voltage))
1466 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1473 * Activate High Speed, HS200 or HS400ES mode if supported.
1475 static int mmc_select_timing(struct mmc_card *card)
1479 if (!mmc_can_ext_csd(card))
1482 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1483 err = mmc_select_hs400es(card);
1484 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1485 err = mmc_select_hs200(card);
1486 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1487 err = mmc_select_hs(card);
1489 if (err && err != -EBADMSG)
1494 * Set the bus speed to the selected bus timing.
1495 * If timing is not selected, backward compatible is the default.
1497 mmc_set_bus_speed(card);
1502 * Execute tuning sequence to seek the proper bus operating
1503 * conditions for HS200 and HS400, which sends CMD21 to the device.
1505 static int mmc_hs200_tuning(struct mmc_card *card)
1507 struct mmc_host *host = card->host;
1510 * Timing should be adjusted to the HS400 target
1511 * operation frequency for tuning process
1513 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1514 host->ios.bus_width == MMC_BUS_WIDTH_8)
1515 if (host->ops->prepare_hs400_tuning)
1516 host->ops->prepare_hs400_tuning(host, &host->ios);
1518 return mmc_execute_tuning(card);
1522 * Handle the detection and initialisation of a card.
1524 * In the case of a resume, "oldcard" will contain the card
1525 * we're trying to reinitialise.
1527 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1528 struct mmc_card *oldcard)
1530 struct mmc_card *card;
1535 WARN_ON(!host->claimed);
1537 /* Set correct bus mode for MMC before attempting init */
1538 if (!mmc_host_is_spi(host))
1539 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1542 * Since we're changing the OCR value, we seem to
1543 * need to tell some cards to go back to the idle
1544 * state. We wait 1ms to give cards time to
1546 * mmc_go_idle is needed for eMMC that are asleep
1550 /* The extra bit indicates that we support high capacity */
1551 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1556 * For SPI, enable CRC as appropriate.
1558 if (mmc_host_is_spi(host)) {
1559 err = mmc_spi_set_crc(host, use_spi_crc);
1565 * Fetch CID from card.
1567 err = mmc_send_cid(host, cid);
1572 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1580 * Allocate card structure.
1582 card = mmc_alloc_card(host, &mmc_type);
1584 err = PTR_ERR(card);
1589 card->type = MMC_TYPE_MMC;
1591 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1595 * Call the optional HC's init_card function to handle quirks.
1597 if (host->ops->init_card)
1598 host->ops->init_card(host, card);
1601 * For native busses: set card RCA and quit open drain mode.
1603 if (!mmc_host_is_spi(host)) {
1604 err = mmc_set_relative_addr(card);
1608 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1613 * Fetch CSD from card.
1615 err = mmc_send_csd(card, card->raw_csd);
1619 err = mmc_decode_csd(card);
1622 err = mmc_decode_cid(card);
1628 * handling only for cards supporting DSR and hosts requesting
1631 if (card->csd.dsr_imp && host->dsr_req)
1635 * Select card, as all following commands rely on that.
1637 if (!mmc_host_is_spi(host)) {
1638 err = mmc_select_card(card);
1644 /* Read extended CSD. */
1645 err = mmc_read_ext_csd(card);
1650 * If doing byte addressing, check if required to do sector
1651 * addressing. Handle the case of <2GB cards needing sector
1652 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1653 * ocr register has bit 30 set for sector addressing.
1656 mmc_card_set_blockaddr(card);
1658 /* Erase size depends on CSD and Extended CSD */
1659 mmc_set_erase_size(card);
1662 /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
1663 if (card->ext_csd.rev >= 3) {
1664 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1665 EXT_CSD_ERASE_GROUP_DEF, 1,
1666 card->ext_csd.generic_cmd6_time);
1668 if (err && err != -EBADMSG)
1674 * Just disable enhanced area off & sz
1675 * will try to enable ERASE_GROUP_DEF
1676 * during next time reinit
1678 card->ext_csd.enhanced_area_offset = -EINVAL;
1679 card->ext_csd.enhanced_area_size = -EINVAL;
1681 card->ext_csd.erase_group_def = 1;
1683 * enable ERASE_GRP_DEF successfully.
1684 * This will affect the erase size, so
1685 * here need to reset erase size
1687 mmc_set_erase_size(card);
1692 * Ensure eMMC user default partition is enabled
1694 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1695 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1696 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1697 card->ext_csd.part_config,
1698 card->ext_csd.part_time);
1699 if (err && err != -EBADMSG)
1704 * Enable power_off_notification byte in the ext_csd register
1706 if (card->ext_csd.rev >= 6) {
1707 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1708 EXT_CSD_POWER_OFF_NOTIFICATION,
1710 card->ext_csd.generic_cmd6_time);
1711 if (err && err != -EBADMSG)
1715 * The err can be -EBADMSG or 0,
1716 * so check for success and update the flag
1719 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1723 * Select timing interface
1725 err = mmc_select_timing(card);
1729 if (mmc_card_hs200(card)) {
1730 err = mmc_hs200_tuning(card);
1734 err = mmc_select_hs400(card);
1737 } else if (!mmc_card_hs400es(card)) {
1738 /* Select the desired bus width optionally */
1739 err = mmc_select_bus_width(card);
1740 if (err > 0 && mmc_card_hs(card)) {
1741 err = mmc_select_hs_ddr(card);
1748 * Choose the power class with selected bus interface
1750 mmc_select_powerclass(card);
1753 * Enable HPI feature (if supported)
1755 if (card->ext_csd.hpi) {
1756 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1757 EXT_CSD_HPI_MGMT, 1,
1758 card->ext_csd.generic_cmd6_time);
1759 if (err && err != -EBADMSG)
1762 pr_warn("%s: Enabling HPI failed\n",
1763 mmc_hostname(card->host));
1764 card->ext_csd.hpi_en = 0;
1767 card->ext_csd.hpi_en = 1;
1772 * If cache size is higher than 0, this indicates the existence of cache
1773 * and it can be turned on. Note that some eMMCs from Micron has been
1774 * reported to need ~800 ms timeout, while enabling the cache after
1775 * sudden power failure tests. Let's extend the timeout to a minimum of
1776 * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
1778 if (card->ext_csd.cache_size > 0) {
1779 unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
1781 timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
1782 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1783 EXT_CSD_CACHE_CTRL, 1, timeout_ms);
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 * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1802 * disabled for a time, so a flag is needed to indicate to re-enable the
1805 card->reenable_cmdq = card->ext_csd.cmdq_en;
1814 mmc_remove_card(card);
1819 static int mmc_can_sleep(struct mmc_card *card)
1821 return (card && card->ext_csd.rev >= 3);
1824 static int mmc_sleep(struct mmc_host *host)
1826 struct mmc_command cmd = {};
1827 struct mmc_card *card = host->card;
1828 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1831 /* Re-tuning can't be done once the card is deselected */
1832 mmc_retune_hold(host);
1834 err = mmc_deselect_cards(host);
1838 cmd.opcode = MMC_SLEEP_AWAKE;
1839 cmd.arg = card->rca << 16;
1843 * If the max_busy_timeout of the host is specified, validate it against
1844 * the sleep cmd timeout. A failure means we need to prevent the host
1845 * from doing hw busy detection, which is done by converting to a R1
1846 * response instead of a R1B.
1848 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1849 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1851 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1852 cmd.busy_timeout = timeout_ms;
1855 err = mmc_wait_for_cmd(host, &cmd, 0);
1860 * If the host does not wait while the card signals busy, then we will
1861 * will have to wait the sleep/awake timeout. Note, we cannot use the
1862 * SEND_STATUS command to poll the status because that command (and most
1863 * others) is invalid while the card sleeps.
1865 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1866 mmc_delay(timeout_ms);
1869 mmc_retune_release(host);
1873 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1876 mmc_card_mmc(card) &&
1877 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1880 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1882 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1885 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1886 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1887 timeout = card->ext_csd.power_off_longtime;
1889 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1890 EXT_CSD_POWER_OFF_NOTIFICATION,
1891 notify_type, timeout, 0, true, false, false);
1893 pr_err("%s: Power Off Notification timed out, %u\n",
1894 mmc_hostname(card->host), timeout);
1896 /* Disable the power off notification after the switch operation. */
1897 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1903 * Host is being removed. Free up the current card.
1905 static void mmc_remove(struct mmc_host *host)
1907 mmc_remove_card(host->card);
1912 * Card detection - card is alive.
1914 static int mmc_alive(struct mmc_host *host)
1916 return mmc_send_status(host->card, NULL);
1920 * Card detection callback from host.
1922 static void mmc_detect(struct mmc_host *host)
1926 mmc_get_card(host->card);
1929 * Just check if our card has been removed.
1931 err = _mmc_detect_card_removed(host);
1933 mmc_put_card(host->card);
1938 mmc_claim_host(host);
1939 mmc_detach_bus(host);
1940 mmc_power_off(host);
1941 mmc_release_host(host);
1945 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
1948 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
1949 EXT_CSD_POWER_OFF_LONG;
1951 mmc_claim_host(host);
1953 if (mmc_card_suspended(host->card))
1956 if (mmc_card_doing_bkops(host->card)) {
1957 err = mmc_stop_bkops(host->card);
1962 err = mmc_flush_cache(host->card);
1966 if (mmc_can_poweroff_notify(host->card) &&
1967 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
1968 err = mmc_poweroff_notify(host->card, notify_type);
1969 else if (mmc_can_sleep(host->card))
1970 err = mmc_sleep(host);
1971 else if (!mmc_host_is_spi(host))
1972 err = mmc_deselect_cards(host);
1975 mmc_power_off(host);
1976 mmc_card_set_suspended(host->card);
1979 mmc_release_host(host);
1986 static int mmc_suspend(struct mmc_host *host)
1990 err = _mmc_suspend(host, true);
1992 pm_runtime_disable(&host->card->dev);
1993 pm_runtime_set_suspended(&host->card->dev);
2000 * This function tries to determine if the same card is still present
2001 * and, if so, restore all state to it.
2003 static int _mmc_resume(struct mmc_host *host)
2007 mmc_claim_host(host);
2009 if (!mmc_card_suspended(host->card))
2012 mmc_power_up(host, host->card->ocr);
2013 err = mmc_init_card(host, host->card->ocr, host->card);
2014 mmc_card_clr_suspended(host->card);
2017 mmc_release_host(host);
2024 static int mmc_shutdown(struct mmc_host *host)
2029 * In a specific case for poweroff notify, we need to resume the card
2030 * before we can shutdown it properly.
2032 if (mmc_can_poweroff_notify(host->card) &&
2033 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
2034 err = _mmc_resume(host);
2037 err = _mmc_suspend(host, false);
2043 * Callback for resume.
2045 static int mmc_resume(struct mmc_host *host)
2047 pm_runtime_enable(&host->card->dev);
2052 * Callback for runtime_suspend.
2054 static int mmc_runtime_suspend(struct mmc_host *host)
2058 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2061 err = _mmc_suspend(host, true);
2063 pr_err("%s: error %d doing aggressive suspend\n",
2064 mmc_hostname(host), err);
2070 * Callback for runtime_resume.
2072 static int mmc_runtime_resume(struct mmc_host *host)
2076 err = _mmc_resume(host);
2077 if (err && err != -ENOMEDIUM)
2078 pr_err("%s: error %d doing runtime resume\n",
2079 mmc_hostname(host), err);
2084 static int mmc_can_reset(struct mmc_card *card)
2088 rst_n_function = card->ext_csd.rst_n_function;
2089 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2094 static int mmc_reset(struct mmc_host *host)
2096 struct mmc_card *card = host->card;
2099 * In the case of recovery, we can't expect flushing the cache to work
2100 * always, but we have a go and ignore errors.
2102 mmc_flush_cache(host->card);
2104 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2105 mmc_can_reset(card)) {
2106 /* If the card accept RST_n signal, send it. */
2107 mmc_set_clock(host, host->f_init);
2108 host->ops->hw_reset(host);
2109 /* Set initial state and call mmc_set_ios */
2110 mmc_set_initial_state(host);
2112 /* Do a brute force power cycle */
2113 mmc_power_cycle(host, card->ocr);
2114 mmc_pwrseq_reset(host);
2116 return mmc_init_card(host, card->ocr, card);
2119 static const struct mmc_bus_ops mmc_ops = {
2120 .remove = mmc_remove,
2121 .detect = mmc_detect,
2122 .suspend = mmc_suspend,
2123 .resume = mmc_resume,
2124 .runtime_suspend = mmc_runtime_suspend,
2125 .runtime_resume = mmc_runtime_resume,
2127 .shutdown = mmc_shutdown,
2132 * Starting point for MMC card init.
2134 int mmc_attach_mmc(struct mmc_host *host)
2139 WARN_ON(!host->claimed);
2141 /* Set correct bus mode for MMC before attempting attach */
2142 if (!mmc_host_is_spi(host))
2143 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2145 err = mmc_send_op_cond(host, 0, &ocr);
2149 mmc_attach_bus(host, &mmc_ops);
2150 if (host->ocr_avail_mmc)
2151 host->ocr_avail = host->ocr_avail_mmc;
2154 * We need to get OCR a different way for SPI.
2156 if (mmc_host_is_spi(host)) {
2157 err = mmc_spi_read_ocr(host, 1, &ocr);
2162 rocr = mmc_select_voltage(host, ocr);
2165 * Can we support the voltage of the card?
2173 * Detect and init the card.
2175 err = mmc_init_card(host, rocr, NULL);
2179 mmc_release_host(host);
2180 err = mmc_add_card(host->card);
2184 mmc_claim_host(host);
2188 mmc_remove_card(host->card);
2189 mmc_claim_host(host);
2192 mmc_detach_bus(host);
2194 pr_err("%s: error %d whilst initialising MMC card\n",
2195 mmc_hostname(host), err);