staging: rtl8188eu: remove unused code
[linux/fpc-iii.git] / drivers / mmc / core / mmc.c
blob3e786ba204c3fd63a4f6b10b9f7acefb41276a88
1 /*
2 * linux/drivers/mmc/core/mmc.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
6 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
13 #include <linux/err.h>
14 #include <linux/of.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>
23 #include "core.h"
24 #include "card.h"
25 #include "host.h"
26 #include "bus.h"
27 #include "mmc_ops.h"
28 #include "quirks.h"
29 #include "sd_ops.h"
30 #include "pwrseq.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,
37 0, 0, 0, 0
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) \
55 ({ \
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; \
60 u32 __res; \
62 __res = resp[__off] >> __shft; \
63 if (__size + __shft > 32) \
64 __res |= resp[__off-1] << ((32 - __shft) % 32); \
65 __res & __mask; \
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;
95 break;
97 case 2: /* MMC v2.0 - v2.2 */
98 case 3: /* MMC v3.1 - v3.3 */
99 case 4: /* MMC v4 */
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;
112 break;
114 default:
115 pr_err("%s: card has unknown MMCA version %d\n",
116 mmc_hostname(card->host), card->csd.mmca_vsn);
117 return -EINVAL;
120 return 0;
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;
127 else
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);
151 return -EINVAL;
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;
185 return 0;
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) {
272 hc_erase_grp_sz =
273 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
274 hc_wp_grp_sz =
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) +
292 ext_csd[140];
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;
296 } else {
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, unsigned int size,
304 unsigned int part_cfg, char *name, int idx, bool ro,
305 int area_type)
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;
312 card->nr_parts++;
315 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
317 int idx;
318 u8 hc_erase_grp_sz, hc_wp_grp_sz;
319 unsigned int part_size;
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) {
328 hc_erase_grp_sz =
329 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
330 hc_wp_grp_sz =
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])
337 continue;
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));
341 break;
343 part_size =
344 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
345 << 16) +
346 (ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
347 << 8) +
348 ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
349 part_size *= (size_t)(hc_erase_grp_sz *
350 hc_wp_grp_sz);
351 mmc_part_add(card, part_size << 19,
352 EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
353 "gp%d", idx, false,
354 MMC_BLK_DATA_AREA_GP);
359 /* Minimum partition switch timeout in milliseconds */
360 #define MMC_MIN_PART_SWITCH_TIME 300
363 * Decode extended CSD.
365 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
367 int err = 0, idx;
368 unsigned int part_size;
369 struct device_node *np;
370 bool broken_hpi = false;
372 /* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
373 card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
374 if (card->csd.structure == 3) {
375 if (card->ext_csd.raw_ext_csd_structure > 2) {
376 pr_err("%s: unrecognised EXT_CSD structure "
377 "version %d\n", mmc_hostname(card->host),
378 card->ext_csd.raw_ext_csd_structure);
379 err = -EINVAL;
380 goto out;
384 np = mmc_of_find_child_device(card->host, 0);
385 if (np && of_device_is_compatible(np, "mmc-card"))
386 broken_hpi = of_property_read_bool(np, "broken-hpi");
387 of_node_put(np);
390 * The EXT_CSD format is meant to be forward compatible. As long
391 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
392 * are authorized, see JEDEC JESD84-B50 section B.8.
394 card->ext_csd.rev = ext_csd[EXT_CSD_REV];
396 /* fixup device after ext_csd revision field is updated */
397 mmc_fixup_device(card, mmc_ext_csd_fixups);
399 card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
400 card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
401 card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
402 card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
403 if (card->ext_csd.rev >= 2) {
404 card->ext_csd.sectors =
405 ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
406 ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
407 ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
408 ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
410 /* Cards with density > 2GiB are sector addressed */
411 if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
412 mmc_card_set_blockaddr(card);
415 card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
416 card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
417 mmc_select_card_type(card);
419 card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
420 card->ext_csd.raw_erase_timeout_mult =
421 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
422 card->ext_csd.raw_hc_erase_grp_size =
423 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
424 if (card->ext_csd.rev >= 3) {
425 u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
426 card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
428 /* EXT_CSD value is in units of 10ms, but we store in ms */
429 card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
430 /* Some eMMC set the value too low so set a minimum */
431 if (card->ext_csd.part_time &&
432 card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
433 card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
435 /* Sleep / awake timeout in 100ns units */
436 if (sa_shift > 0 && sa_shift <= 0x17)
437 card->ext_csd.sa_timeout =
438 1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
439 card->ext_csd.erase_group_def =
440 ext_csd[EXT_CSD_ERASE_GROUP_DEF];
441 card->ext_csd.hc_erase_timeout = 300 *
442 ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
443 card->ext_csd.hc_erase_size =
444 ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
446 card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
449 * There are two boot regions of equal size, defined in
450 * multiples of 128K.
452 if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
453 for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
454 part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
455 mmc_part_add(card, part_size,
456 EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
457 "boot%d", idx, true,
458 MMC_BLK_DATA_AREA_BOOT);
463 card->ext_csd.raw_hc_erase_gap_size =
464 ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
465 card->ext_csd.raw_sec_trim_mult =
466 ext_csd[EXT_CSD_SEC_TRIM_MULT];
467 card->ext_csd.raw_sec_erase_mult =
468 ext_csd[EXT_CSD_SEC_ERASE_MULT];
469 card->ext_csd.raw_sec_feature_support =
470 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
471 card->ext_csd.raw_trim_mult =
472 ext_csd[EXT_CSD_TRIM_MULT];
473 card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
474 card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
475 if (card->ext_csd.rev >= 4) {
476 if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
477 EXT_CSD_PART_SETTING_COMPLETED)
478 card->ext_csd.partition_setting_completed = 1;
479 else
480 card->ext_csd.partition_setting_completed = 0;
482 mmc_manage_enhanced_area(card, ext_csd);
484 mmc_manage_gp_partitions(card, ext_csd);
486 card->ext_csd.sec_trim_mult =
487 ext_csd[EXT_CSD_SEC_TRIM_MULT];
488 card->ext_csd.sec_erase_mult =
489 ext_csd[EXT_CSD_SEC_ERASE_MULT];
490 card->ext_csd.sec_feature_support =
491 ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
492 card->ext_csd.trim_timeout = 300 *
493 ext_csd[EXT_CSD_TRIM_MULT];
496 * Note that the call to mmc_part_add above defaults to read
497 * only. If this default assumption is changed, the call must
498 * take into account the value of boot_locked below.
500 card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
501 card->ext_csd.boot_ro_lockable = true;
503 /* Save power class values */
504 card->ext_csd.raw_pwr_cl_52_195 =
505 ext_csd[EXT_CSD_PWR_CL_52_195];
506 card->ext_csd.raw_pwr_cl_26_195 =
507 ext_csd[EXT_CSD_PWR_CL_26_195];
508 card->ext_csd.raw_pwr_cl_52_360 =
509 ext_csd[EXT_CSD_PWR_CL_52_360];
510 card->ext_csd.raw_pwr_cl_26_360 =
511 ext_csd[EXT_CSD_PWR_CL_26_360];
512 card->ext_csd.raw_pwr_cl_200_195 =
513 ext_csd[EXT_CSD_PWR_CL_200_195];
514 card->ext_csd.raw_pwr_cl_200_360 =
515 ext_csd[EXT_CSD_PWR_CL_200_360];
516 card->ext_csd.raw_pwr_cl_ddr_52_195 =
517 ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
518 card->ext_csd.raw_pwr_cl_ddr_52_360 =
519 ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
520 card->ext_csd.raw_pwr_cl_ddr_200_360 =
521 ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
524 if (card->ext_csd.rev >= 5) {
525 /* Adjust production date as per JEDEC JESD84-B451 */
526 if (card->cid.year < 2010)
527 card->cid.year += 16;
529 /* check whether the eMMC card supports BKOPS */
530 if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
531 card->ext_csd.bkops = 1;
532 card->ext_csd.man_bkops_en =
533 (ext_csd[EXT_CSD_BKOPS_EN] &
534 EXT_CSD_MANUAL_BKOPS_MASK);
535 card->ext_csd.raw_bkops_status =
536 ext_csd[EXT_CSD_BKOPS_STATUS];
537 if (card->ext_csd.man_bkops_en)
538 pr_debug("%s: MAN_BKOPS_EN bit is set\n",
539 mmc_hostname(card->host));
540 card->ext_csd.auto_bkops_en =
541 (ext_csd[EXT_CSD_BKOPS_EN] &
542 EXT_CSD_AUTO_BKOPS_MASK);
543 if (card->ext_csd.auto_bkops_en)
544 pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
545 mmc_hostname(card->host));
548 /* check whether the eMMC card supports HPI */
549 if (!mmc_card_broken_hpi(card) &&
550 !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
551 card->ext_csd.hpi = 1;
552 if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
553 card->ext_csd.hpi_cmd = MMC_STOP_TRANSMISSION;
554 else
555 card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
557 * Indicate the maximum timeout to close
558 * a command interrupted by HPI
560 card->ext_csd.out_of_int_time =
561 ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
564 card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
565 card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
568 * RPMB regions are defined in multiples of 128K.
570 card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
571 if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
572 mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
573 EXT_CSD_PART_CONFIG_ACC_RPMB,
574 "rpmb", 0, false,
575 MMC_BLK_DATA_AREA_RPMB);
579 card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
580 if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
581 card->erased_byte = 0xFF;
582 else
583 card->erased_byte = 0x0;
585 /* eMMC v4.5 or later */
586 card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
587 if (card->ext_csd.rev >= 6) {
588 card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
590 card->ext_csd.generic_cmd6_time = 10 *
591 ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
592 card->ext_csd.power_off_longtime = 10 *
593 ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
595 card->ext_csd.cache_size =
596 ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
597 ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
598 ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
599 ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
601 if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
602 card->ext_csd.data_sector_size = 4096;
603 else
604 card->ext_csd.data_sector_size = 512;
606 if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
607 (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
608 card->ext_csd.data_tag_unit_size =
609 ((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
610 (card->ext_csd.data_sector_size);
611 } else {
612 card->ext_csd.data_tag_unit_size = 0;
615 card->ext_csd.max_packed_writes =
616 ext_csd[EXT_CSD_MAX_PACKED_WRITES];
617 card->ext_csd.max_packed_reads =
618 ext_csd[EXT_CSD_MAX_PACKED_READS];
619 } else {
620 card->ext_csd.data_sector_size = 512;
623 /* eMMC v5 or later */
624 if (card->ext_csd.rev >= 7) {
625 memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
626 MMC_FIRMWARE_LEN);
627 card->ext_csd.ffu_capable =
628 (ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
629 !(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
631 card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
632 card->ext_csd.device_life_time_est_typ_a =
633 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
634 card->ext_csd.device_life_time_est_typ_b =
635 ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
638 /* eMMC v5.1 or later */
639 if (card->ext_csd.rev >= 8) {
640 card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
641 EXT_CSD_CMDQ_SUPPORTED;
642 card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
643 EXT_CSD_CMDQ_DEPTH_MASK) + 1;
644 /* Exclude inefficiently small queue depths */
645 if (card->ext_csd.cmdq_depth <= 2) {
646 card->ext_csd.cmdq_support = false;
647 card->ext_csd.cmdq_depth = 0;
649 if (card->ext_csd.cmdq_support) {
650 pr_debug("%s: Command Queue supported depth %u\n",
651 mmc_hostname(card->host),
652 card->ext_csd.cmdq_depth);
655 out:
656 return err;
659 static int mmc_read_ext_csd(struct mmc_card *card)
661 u8 *ext_csd;
662 int err;
664 if (!mmc_can_ext_csd(card))
665 return 0;
667 err = mmc_get_ext_csd(card, &ext_csd);
668 if (err) {
669 /* If the host or the card can't do the switch,
670 * fail more gracefully. */
671 if ((err != -EINVAL)
672 && (err != -ENOSYS)
673 && (err != -EFAULT))
674 return err;
677 * High capacity cards should have this "magic" size
678 * stored in their CSD.
680 if (card->csd.capacity == (4096 * 512)) {
681 pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
682 mmc_hostname(card->host));
683 } else {
684 pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
685 mmc_hostname(card->host));
686 err = 0;
689 return err;
692 err = mmc_decode_ext_csd(card, ext_csd);
693 kfree(ext_csd);
694 return err;
697 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
699 u8 *bw_ext_csd;
700 int err;
702 if (bus_width == MMC_BUS_WIDTH_1)
703 return 0;
705 err = mmc_get_ext_csd(card, &bw_ext_csd);
706 if (err)
707 return err;
709 /* only compare read only fields */
710 err = !((card->ext_csd.raw_partition_support ==
711 bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
712 (card->ext_csd.raw_erased_mem_count ==
713 bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
714 (card->ext_csd.rev ==
715 bw_ext_csd[EXT_CSD_REV]) &&
716 (card->ext_csd.raw_ext_csd_structure ==
717 bw_ext_csd[EXT_CSD_STRUCTURE]) &&
718 (card->ext_csd.raw_card_type ==
719 bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
720 (card->ext_csd.raw_s_a_timeout ==
721 bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
722 (card->ext_csd.raw_hc_erase_gap_size ==
723 bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
724 (card->ext_csd.raw_erase_timeout_mult ==
725 bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
726 (card->ext_csd.raw_hc_erase_grp_size ==
727 bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
728 (card->ext_csd.raw_sec_trim_mult ==
729 bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
730 (card->ext_csd.raw_sec_erase_mult ==
731 bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
732 (card->ext_csd.raw_sec_feature_support ==
733 bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
734 (card->ext_csd.raw_trim_mult ==
735 bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
736 (card->ext_csd.raw_sectors[0] ==
737 bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
738 (card->ext_csd.raw_sectors[1] ==
739 bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
740 (card->ext_csd.raw_sectors[2] ==
741 bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
742 (card->ext_csd.raw_sectors[3] ==
743 bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
744 (card->ext_csd.raw_pwr_cl_52_195 ==
745 bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
746 (card->ext_csd.raw_pwr_cl_26_195 ==
747 bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
748 (card->ext_csd.raw_pwr_cl_52_360 ==
749 bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
750 (card->ext_csd.raw_pwr_cl_26_360 ==
751 bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
752 (card->ext_csd.raw_pwr_cl_200_195 ==
753 bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
754 (card->ext_csd.raw_pwr_cl_200_360 ==
755 bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
756 (card->ext_csd.raw_pwr_cl_ddr_52_195 ==
757 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
758 (card->ext_csd.raw_pwr_cl_ddr_52_360 ==
759 bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
760 (card->ext_csd.raw_pwr_cl_ddr_200_360 ==
761 bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
763 if (err)
764 err = -EINVAL;
766 kfree(bw_ext_csd);
767 return err;
770 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
771 card->raw_cid[2], card->raw_cid[3]);
772 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
773 card->raw_csd[2], card->raw_csd[3]);
774 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
775 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
776 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
777 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
778 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
779 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
780 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
781 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
782 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
783 MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev);
784 MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
785 MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
786 card->ext_csd.device_life_time_est_typ_a,
787 card->ext_csd.device_life_time_est_typ_b);
788 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
789 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
790 card->ext_csd.enhanced_area_offset);
791 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
792 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
793 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
794 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
795 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
796 MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
798 static ssize_t mmc_fwrev_show(struct device *dev,
799 struct device_attribute *attr,
800 char *buf)
802 struct mmc_card *card = mmc_dev_to_card(dev);
804 if (card->ext_csd.rev < 7) {
805 return sprintf(buf, "0x%x\n", card->cid.fwrev);
806 } else {
807 return sprintf(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
808 card->ext_csd.fwrev);
812 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
814 static ssize_t mmc_dsr_show(struct device *dev,
815 struct device_attribute *attr,
816 char *buf)
818 struct mmc_card *card = mmc_dev_to_card(dev);
819 struct mmc_host *host = card->host;
821 if (card->csd.dsr_imp && host->dsr_req)
822 return sprintf(buf, "0x%x\n", host->dsr);
823 else
824 /* return default DSR value */
825 return sprintf(buf, "0x%x\n", 0x404);
828 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
830 static struct attribute *mmc_std_attrs[] = {
831 &dev_attr_cid.attr,
832 &dev_attr_csd.attr,
833 &dev_attr_date.attr,
834 &dev_attr_erase_size.attr,
835 &dev_attr_preferred_erase_size.attr,
836 &dev_attr_fwrev.attr,
837 &dev_attr_ffu_capable.attr,
838 &dev_attr_hwrev.attr,
839 &dev_attr_manfid.attr,
840 &dev_attr_name.attr,
841 &dev_attr_oemid.attr,
842 &dev_attr_prv.attr,
843 &dev_attr_rev.attr,
844 &dev_attr_pre_eol_info.attr,
845 &dev_attr_life_time.attr,
846 &dev_attr_serial.attr,
847 &dev_attr_enhanced_area_offset.attr,
848 &dev_attr_enhanced_area_size.attr,
849 &dev_attr_raw_rpmb_size_mult.attr,
850 &dev_attr_rel_sectors.attr,
851 &dev_attr_ocr.attr,
852 &dev_attr_rca.attr,
853 &dev_attr_dsr.attr,
854 &dev_attr_cmdq_en.attr,
855 NULL,
857 ATTRIBUTE_GROUPS(mmc_std);
859 static struct device_type mmc_type = {
860 .groups = mmc_std_groups,
864 * Select the PowerClass for the current bus width
865 * If power class is defined for 4/8 bit bus in the
866 * extended CSD register, select it by executing the
867 * mmc_switch command.
869 static int __mmc_select_powerclass(struct mmc_card *card,
870 unsigned int bus_width)
872 struct mmc_host *host = card->host;
873 struct mmc_ext_csd *ext_csd = &card->ext_csd;
874 unsigned int pwrclass_val = 0;
875 int err = 0;
877 switch (1 << host->ios.vdd) {
878 case MMC_VDD_165_195:
879 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
880 pwrclass_val = ext_csd->raw_pwr_cl_26_195;
881 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
882 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
883 ext_csd->raw_pwr_cl_52_195 :
884 ext_csd->raw_pwr_cl_ddr_52_195;
885 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
886 pwrclass_val = ext_csd->raw_pwr_cl_200_195;
887 break;
888 case MMC_VDD_27_28:
889 case MMC_VDD_28_29:
890 case MMC_VDD_29_30:
891 case MMC_VDD_30_31:
892 case MMC_VDD_31_32:
893 case MMC_VDD_32_33:
894 case MMC_VDD_33_34:
895 case MMC_VDD_34_35:
896 case MMC_VDD_35_36:
897 if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
898 pwrclass_val = ext_csd->raw_pwr_cl_26_360;
899 else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
900 pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
901 ext_csd->raw_pwr_cl_52_360 :
902 ext_csd->raw_pwr_cl_ddr_52_360;
903 else if (host->ios.clock <= MMC_HS200_MAX_DTR)
904 pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
905 ext_csd->raw_pwr_cl_ddr_200_360 :
906 ext_csd->raw_pwr_cl_200_360;
907 break;
908 default:
909 pr_warn("%s: Voltage range not supported for power class\n",
910 mmc_hostname(host));
911 return -EINVAL;
914 if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
915 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
916 EXT_CSD_PWR_CL_8BIT_SHIFT;
917 else
918 pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
919 EXT_CSD_PWR_CL_4BIT_SHIFT;
921 /* If the power class is different from the default value */
922 if (pwrclass_val > 0) {
923 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
924 EXT_CSD_POWER_CLASS,
925 pwrclass_val,
926 card->ext_csd.generic_cmd6_time);
929 return err;
932 static int mmc_select_powerclass(struct mmc_card *card)
934 struct mmc_host *host = card->host;
935 u32 bus_width, ext_csd_bits;
936 int err, ddr;
938 /* Power class selection is supported for versions >= 4.0 */
939 if (!mmc_can_ext_csd(card))
940 return 0;
942 bus_width = host->ios.bus_width;
943 /* Power class values are defined only for 4/8 bit bus */
944 if (bus_width == MMC_BUS_WIDTH_1)
945 return 0;
947 ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
948 if (ddr)
949 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
950 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
951 else
952 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
953 EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
955 err = __mmc_select_powerclass(card, ext_csd_bits);
956 if (err)
957 pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
958 mmc_hostname(host), 1 << bus_width, ddr);
960 return err;
964 * Set the bus speed for the selected speed mode.
966 static void mmc_set_bus_speed(struct mmc_card *card)
968 unsigned int max_dtr = (unsigned int)-1;
970 if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
971 max_dtr > card->ext_csd.hs200_max_dtr)
972 max_dtr = card->ext_csd.hs200_max_dtr;
973 else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
974 max_dtr = card->ext_csd.hs_max_dtr;
975 else if (max_dtr > card->csd.max_dtr)
976 max_dtr = card->csd.max_dtr;
978 mmc_set_clock(card->host, max_dtr);
982 * Select the bus width amoung 4-bit and 8-bit(SDR).
983 * If the bus width is changed successfully, return the selected width value.
984 * Zero is returned instead of error value if the wide width is not supported.
986 static int mmc_select_bus_width(struct mmc_card *card)
988 static unsigned ext_csd_bits[] = {
989 EXT_CSD_BUS_WIDTH_8,
990 EXT_CSD_BUS_WIDTH_4,
992 static unsigned bus_widths[] = {
993 MMC_BUS_WIDTH_8,
994 MMC_BUS_WIDTH_4,
996 struct mmc_host *host = card->host;
997 unsigned idx, bus_width = 0;
998 int err = 0;
1000 if (!mmc_can_ext_csd(card) ||
1001 !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1002 return 0;
1004 idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
1007 * Unlike SD, MMC cards dont have a configuration register to notify
1008 * supported bus width. So bus test command should be run to identify
1009 * the supported bus width or compare the ext csd values of current
1010 * bus width and ext csd values of 1 bit mode read earlier.
1012 for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1014 * Host is capable of 8bit transfer, then switch
1015 * the device to work in 8bit transfer mode. If the
1016 * mmc switch command returns error then switch to
1017 * 4bit transfer mode. On success set the corresponding
1018 * bus width on the host.
1020 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1021 EXT_CSD_BUS_WIDTH,
1022 ext_csd_bits[idx],
1023 card->ext_csd.generic_cmd6_time);
1024 if (err)
1025 continue;
1027 bus_width = bus_widths[idx];
1028 mmc_set_bus_width(host, bus_width);
1031 * If controller can't handle bus width test,
1032 * compare ext_csd previously read in 1 bit mode
1033 * against ext_csd at new bus width
1035 if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1036 err = mmc_compare_ext_csds(card, bus_width);
1037 else
1038 err = mmc_bus_test(card, bus_width);
1040 if (!err) {
1041 err = bus_width;
1042 break;
1043 } else {
1044 pr_warn("%s: switch to bus width %d failed\n",
1045 mmc_hostname(host), 1 << bus_width);
1049 return err;
1053 * Switch to the high-speed mode
1055 static int mmc_select_hs(struct mmc_card *card)
1057 int err;
1059 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1060 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1061 card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1062 true, true, true);
1063 if (err)
1064 pr_warn("%s: switch to high-speed failed, err:%d\n",
1065 mmc_hostname(card->host), err);
1067 return err;
1071 * Activate wide bus and DDR if supported.
1073 static int mmc_select_hs_ddr(struct mmc_card *card)
1075 struct mmc_host *host = card->host;
1076 u32 bus_width, ext_csd_bits;
1077 int err = 0;
1079 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1080 return 0;
1082 bus_width = host->ios.bus_width;
1083 if (bus_width == MMC_BUS_WIDTH_1)
1084 return 0;
1086 ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1087 EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1089 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1090 EXT_CSD_BUS_WIDTH,
1091 ext_csd_bits,
1092 card->ext_csd.generic_cmd6_time,
1093 MMC_TIMING_MMC_DDR52,
1094 true, true, true);
1095 if (err) {
1096 pr_err("%s: switch to bus width %d ddr failed\n",
1097 mmc_hostname(host), 1 << bus_width);
1098 return err;
1102 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1103 * signaling.
1105 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1107 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1108 * in the JEDEC spec for DDR.
1110 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1111 * host controller can support this, like some of the SDHCI
1112 * controller which connect to an eMMC device. Some of these
1113 * host controller still needs to use 1.8v vccq for supporting
1114 * DDR mode.
1116 * So the sequence will be:
1117 * if (host and device can both support 1.2v IO)
1118 * use 1.2v IO;
1119 * else if (host and device can both support 1.8v IO)
1120 * use 1.8v IO;
1121 * so if host and device can only support 3.3v IO, this is the
1122 * last choice.
1124 * WARNING: eMMC rules are NOT the same as SD DDR
1126 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
1127 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1128 if (!err)
1129 return 0;
1132 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1133 host->caps & MMC_CAP_1_8V_DDR)
1134 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1136 /* make sure vccq is 3.3v after switching disaster */
1137 if (err)
1138 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1140 return err;
1143 static int mmc_select_hs400(struct mmc_card *card)
1145 struct mmc_host *host = card->host;
1146 unsigned int max_dtr;
1147 int err = 0;
1148 u8 val;
1151 * HS400 mode requires 8-bit bus width
1153 if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1154 host->ios.bus_width == MMC_BUS_WIDTH_8))
1155 return 0;
1157 /* Switch card to HS mode */
1158 val = EXT_CSD_TIMING_HS;
1159 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1160 EXT_CSD_HS_TIMING, val,
1161 card->ext_csd.generic_cmd6_time, 0,
1162 true, false, true);
1163 if (err) {
1164 pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1165 mmc_hostname(host), err);
1166 return err;
1169 /* Set host controller to HS timing */
1170 mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1172 /* Prepare host to downgrade to HS timing */
1173 if (host->ops->hs400_downgrade)
1174 host->ops->hs400_downgrade(host);
1176 /* Reduce frequency to HS frequency */
1177 max_dtr = card->ext_csd.hs_max_dtr;
1178 mmc_set_clock(host, max_dtr);
1180 err = mmc_switch_status(card);
1181 if (err)
1182 goto out_err;
1184 if (host->ops->hs400_prepare_ddr)
1185 host->ops->hs400_prepare_ddr(host);
1187 /* Switch card to DDR */
1188 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1189 EXT_CSD_BUS_WIDTH,
1190 EXT_CSD_DDR_BUS_WIDTH_8,
1191 card->ext_csd.generic_cmd6_time);
1192 if (err) {
1193 pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1194 mmc_hostname(host), err);
1195 return err;
1198 /* Switch card to HS400 */
1199 val = EXT_CSD_TIMING_HS400 |
1200 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1201 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1202 EXT_CSD_HS_TIMING, val,
1203 card->ext_csd.generic_cmd6_time, 0,
1204 true, false, true);
1205 if (err) {
1206 pr_err("%s: switch to hs400 failed, err:%d\n",
1207 mmc_hostname(host), err);
1208 return err;
1211 /* Set host controller to HS400 timing and frequency */
1212 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1213 mmc_set_bus_speed(card);
1215 err = mmc_switch_status(card);
1216 if (err)
1217 goto out_err;
1219 if (host->ops->hs400_complete)
1220 host->ops->hs400_complete(host);
1222 return 0;
1224 out_err:
1225 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1226 __func__, err);
1227 return err;
1230 int mmc_hs200_to_hs400(struct mmc_card *card)
1232 return mmc_select_hs400(card);
1235 int mmc_hs400_to_hs200(struct mmc_card *card)
1237 struct mmc_host *host = card->host;
1238 unsigned int max_dtr;
1239 int err;
1240 u8 val;
1242 /* Reduce frequency to HS */
1243 max_dtr = card->ext_csd.hs_max_dtr;
1244 mmc_set_clock(host, max_dtr);
1246 /* Switch HS400 to HS DDR */
1247 val = EXT_CSD_TIMING_HS;
1248 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1249 val, card->ext_csd.generic_cmd6_time, 0,
1250 true, false, true);
1251 if (err)
1252 goto out_err;
1254 mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1256 err = mmc_switch_status(card);
1257 if (err)
1258 goto out_err;
1260 /* Switch HS DDR to HS */
1261 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1262 EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1263 0, true, false, true);
1264 if (err)
1265 goto out_err;
1267 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1269 if (host->ops->hs400_downgrade)
1270 host->ops->hs400_downgrade(host);
1272 err = mmc_switch_status(card);
1273 if (err)
1274 goto out_err;
1276 /* Switch HS to HS200 */
1277 val = EXT_CSD_TIMING_HS200 |
1278 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1279 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1280 val, card->ext_csd.generic_cmd6_time, 0,
1281 true, false, true);
1282 if (err)
1283 goto out_err;
1285 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1288 * For HS200, CRC errors are not a reliable way to know the switch
1289 * failed. If there really is a problem, we would expect tuning will
1290 * fail and the result ends up the same.
1292 err = __mmc_switch_status(card, false);
1293 if (err)
1294 goto out_err;
1296 mmc_set_bus_speed(card);
1298 /* Prepare tuning for HS400 mode. */
1299 if (host->ops->prepare_hs400_tuning)
1300 host->ops->prepare_hs400_tuning(host, &host->ios);
1302 return 0;
1304 out_err:
1305 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1306 __func__, err);
1307 return err;
1310 static void mmc_select_driver_type(struct mmc_card *card)
1312 int card_drv_type, drive_strength, drv_type = 0;
1313 int fixed_drv_type = card->host->fixed_drv_type;
1315 card_drv_type = card->ext_csd.raw_driver_strength |
1316 mmc_driver_type_mask(0);
1318 if (fixed_drv_type >= 0)
1319 drive_strength = card_drv_type & mmc_driver_type_mask(fixed_drv_type)
1320 ? fixed_drv_type : 0;
1321 else
1322 drive_strength = mmc_select_drive_strength(card,
1323 card->ext_csd.hs200_max_dtr,
1324 card_drv_type, &drv_type);
1326 card->drive_strength = drive_strength;
1328 if (drv_type)
1329 mmc_set_driver_type(card->host, drv_type);
1332 static int mmc_select_hs400es(struct mmc_card *card)
1334 struct mmc_host *host = card->host;
1335 int err = -EINVAL;
1336 u8 val;
1338 if (!(host->caps & MMC_CAP_8_BIT_DATA)) {
1339 err = -ENOTSUPP;
1340 goto out_err;
1343 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1344 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1346 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1347 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1349 /* If fails try again during next card power cycle */
1350 if (err)
1351 goto out_err;
1353 err = mmc_select_bus_width(card);
1354 if (err != MMC_BUS_WIDTH_8) {
1355 pr_err("%s: switch to 8bit bus width failed, err:%d\n",
1356 mmc_hostname(host), err);
1357 err = err < 0 ? err : -ENOTSUPP;
1358 goto out_err;
1361 /* Switch card to HS mode */
1362 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1363 EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1364 card->ext_csd.generic_cmd6_time, 0,
1365 true, false, true);
1366 if (err) {
1367 pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1368 mmc_hostname(host), err);
1369 goto out_err;
1372 mmc_set_timing(host, MMC_TIMING_MMC_HS);
1373 err = mmc_switch_status(card);
1374 if (err)
1375 goto out_err;
1377 mmc_set_clock(host, card->ext_csd.hs_max_dtr);
1379 /* Switch card to DDR with strobe bit */
1380 val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1381 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1382 EXT_CSD_BUS_WIDTH,
1383 val,
1384 card->ext_csd.generic_cmd6_time);
1385 if (err) {
1386 pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1387 mmc_hostname(host), err);
1388 goto out_err;
1391 mmc_select_driver_type(card);
1393 /* Switch card to HS400 */
1394 val = EXT_CSD_TIMING_HS400 |
1395 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1396 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1397 EXT_CSD_HS_TIMING, val,
1398 card->ext_csd.generic_cmd6_time, 0,
1399 true, false, true);
1400 if (err) {
1401 pr_err("%s: switch to hs400es failed, err:%d\n",
1402 mmc_hostname(host), err);
1403 goto out_err;
1406 /* Set host controller to HS400 timing and frequency */
1407 mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1409 /* Controller enable enhanced strobe function */
1410 host->ios.enhanced_strobe = true;
1411 if (host->ops->hs400_enhanced_strobe)
1412 host->ops->hs400_enhanced_strobe(host, &host->ios);
1414 err = mmc_switch_status(card);
1415 if (err)
1416 goto out_err;
1418 return 0;
1420 out_err:
1421 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1422 __func__, err);
1423 return err;
1427 * For device supporting HS200 mode, the following sequence
1428 * should be done before executing the tuning process.
1429 * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1430 * 2. switch to HS200 mode
1431 * 3. set the clock to > 52Mhz and <=200MHz
1433 static int mmc_select_hs200(struct mmc_card *card)
1435 struct mmc_host *host = card->host;
1436 unsigned int old_timing, old_signal_voltage;
1437 int err = -EINVAL;
1438 u8 val;
1440 old_signal_voltage = host->ios.signal_voltage;
1441 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1442 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1444 if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1445 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1447 /* If fails try again during next card power cycle */
1448 if (err)
1449 return err;
1451 mmc_select_driver_type(card);
1454 * Set the bus width(4 or 8) with host's support and
1455 * switch to HS200 mode if bus width is set successfully.
1457 err = mmc_select_bus_width(card);
1458 if (err > 0) {
1459 val = EXT_CSD_TIMING_HS200 |
1460 card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1461 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1462 EXT_CSD_HS_TIMING, val,
1463 card->ext_csd.generic_cmd6_time, 0,
1464 true, false, true);
1465 if (err)
1466 goto err;
1467 old_timing = host->ios.timing;
1468 mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1471 * For HS200, CRC errors are not a reliable way to know the
1472 * switch failed. If there really is a problem, we would expect
1473 * tuning will fail and the result ends up the same.
1475 err = __mmc_switch_status(card, false);
1478 * mmc_select_timing() assumes timing has not changed if
1479 * it is a switch error.
1481 if (err == -EBADMSG)
1482 mmc_set_timing(host, old_timing);
1484 err:
1485 if (err) {
1486 /* fall back to the old signal voltage, if fails report error */
1487 if (mmc_set_signal_voltage(host, old_signal_voltage))
1488 err = -EIO;
1490 pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1491 __func__, err);
1493 return err;
1497 * Activate High Speed, HS200 or HS400ES mode if supported.
1499 static int mmc_select_timing(struct mmc_card *card)
1501 int err = 0;
1503 if (!mmc_can_ext_csd(card))
1504 goto bus_speed;
1506 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES)
1507 err = mmc_select_hs400es(card);
1508 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200)
1509 err = mmc_select_hs200(card);
1510 else if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1511 err = mmc_select_hs(card);
1513 if (err && err != -EBADMSG)
1514 return err;
1516 bus_speed:
1518 * Set the bus speed to the selected bus timing.
1519 * If timing is not selected, backward compatible is the default.
1521 mmc_set_bus_speed(card);
1522 return 0;
1526 * Execute tuning sequence to seek the proper bus operating
1527 * conditions for HS200 and HS400, which sends CMD21 to the device.
1529 static int mmc_hs200_tuning(struct mmc_card *card)
1531 struct mmc_host *host = card->host;
1534 * Timing should be adjusted to the HS400 target
1535 * operation frequency for tuning process
1537 if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1538 host->ios.bus_width == MMC_BUS_WIDTH_8)
1539 if (host->ops->prepare_hs400_tuning)
1540 host->ops->prepare_hs400_tuning(host, &host->ios);
1542 return mmc_execute_tuning(card);
1546 * Handle the detection and initialisation of a card.
1548 * In the case of a resume, "oldcard" will contain the card
1549 * we're trying to reinitialise.
1551 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1552 struct mmc_card *oldcard)
1554 struct mmc_card *card;
1555 int err;
1556 u32 cid[4];
1557 u32 rocr;
1559 WARN_ON(!host->claimed);
1561 /* Set correct bus mode for MMC before attempting init */
1562 if (!mmc_host_is_spi(host))
1563 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1566 * Since we're changing the OCR value, we seem to
1567 * need to tell some cards to go back to the idle
1568 * state. We wait 1ms to give cards time to
1569 * respond.
1570 * mmc_go_idle is needed for eMMC that are asleep
1572 mmc_go_idle(host);
1574 /* The extra bit indicates that we support high capacity */
1575 err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1576 if (err)
1577 goto err;
1580 * For SPI, enable CRC as appropriate.
1582 if (mmc_host_is_spi(host)) {
1583 err = mmc_spi_set_crc(host, use_spi_crc);
1584 if (err)
1585 goto err;
1589 * Fetch CID from card.
1591 err = mmc_send_cid(host, cid);
1592 if (err)
1593 goto err;
1595 if (oldcard) {
1596 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1597 pr_debug("%s: Perhaps the card was replaced\n",
1598 mmc_hostname(host));
1599 err = -ENOENT;
1600 goto err;
1603 card = oldcard;
1604 } else {
1606 * Allocate card structure.
1608 card = mmc_alloc_card(host, &mmc_type);
1609 if (IS_ERR(card)) {
1610 err = PTR_ERR(card);
1611 goto err;
1614 card->ocr = ocr;
1615 card->type = MMC_TYPE_MMC;
1616 card->rca = 1;
1617 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1621 * Call the optional HC's init_card function to handle quirks.
1623 if (host->ops->init_card)
1624 host->ops->init_card(host, card);
1627 * For native busses: set card RCA and quit open drain mode.
1629 if (!mmc_host_is_spi(host)) {
1630 err = mmc_set_relative_addr(card);
1631 if (err)
1632 goto free_card;
1634 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1637 if (!oldcard) {
1639 * Fetch CSD from card.
1641 err = mmc_send_csd(card, card->raw_csd);
1642 if (err)
1643 goto free_card;
1645 err = mmc_decode_csd(card);
1646 if (err)
1647 goto free_card;
1648 err = mmc_decode_cid(card);
1649 if (err)
1650 goto free_card;
1654 * handling only for cards supporting DSR and hosts requesting
1655 * DSR configuration
1657 if (card->csd.dsr_imp && host->dsr_req)
1658 mmc_set_dsr(host);
1661 * Select card, as all following commands rely on that.
1663 if (!mmc_host_is_spi(host)) {
1664 err = mmc_select_card(card);
1665 if (err)
1666 goto free_card;
1669 if (!oldcard) {
1670 /* Read extended CSD. */
1671 err = mmc_read_ext_csd(card);
1672 if (err)
1673 goto free_card;
1676 * If doing byte addressing, check if required to do sector
1677 * addressing. Handle the case of <2GB cards needing sector
1678 * addressing. See section 8.1 JEDEC Standard JED84-A441;
1679 * ocr register has bit 30 set for sector addressing.
1681 if (rocr & BIT(30))
1682 mmc_card_set_blockaddr(card);
1684 /* Erase size depends on CSD and Extended CSD */
1685 mmc_set_erase_size(card);
1688 /* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
1689 if (card->ext_csd.rev >= 3) {
1690 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1691 EXT_CSD_ERASE_GROUP_DEF, 1,
1692 card->ext_csd.generic_cmd6_time);
1694 if (err && err != -EBADMSG)
1695 goto free_card;
1697 if (err) {
1698 err = 0;
1700 * Just disable enhanced area off & sz
1701 * will try to enable ERASE_GROUP_DEF
1702 * during next time reinit
1704 card->ext_csd.enhanced_area_offset = -EINVAL;
1705 card->ext_csd.enhanced_area_size = -EINVAL;
1706 } else {
1707 card->ext_csd.erase_group_def = 1;
1709 * enable ERASE_GRP_DEF successfully.
1710 * This will affect the erase size, so
1711 * here need to reset erase size
1713 mmc_set_erase_size(card);
1718 * Ensure eMMC user default partition is enabled
1720 if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1721 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1722 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1723 card->ext_csd.part_config,
1724 card->ext_csd.part_time);
1725 if (err && err != -EBADMSG)
1726 goto free_card;
1730 * Enable power_off_notification byte in the ext_csd register
1732 if (card->ext_csd.rev >= 6) {
1733 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1734 EXT_CSD_POWER_OFF_NOTIFICATION,
1735 EXT_CSD_POWER_ON,
1736 card->ext_csd.generic_cmd6_time);
1737 if (err && err != -EBADMSG)
1738 goto free_card;
1741 * The err can be -EBADMSG or 0,
1742 * so check for success and update the flag
1744 if (!err)
1745 card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1748 /* set erase_arg */
1749 if (mmc_can_discard(card))
1750 card->erase_arg = MMC_DISCARD_ARG;
1751 else if (mmc_can_trim(card))
1752 card->erase_arg = MMC_TRIM_ARG;
1753 else
1754 card->erase_arg = MMC_ERASE_ARG;
1757 * Select timing interface
1759 err = mmc_select_timing(card);
1760 if (err)
1761 goto free_card;
1763 if (mmc_card_hs200(card)) {
1764 err = mmc_hs200_tuning(card);
1765 if (err)
1766 goto free_card;
1768 err = mmc_select_hs400(card);
1769 if (err)
1770 goto free_card;
1771 } else if (!mmc_card_hs400es(card)) {
1772 /* Select the desired bus width optionally */
1773 err = mmc_select_bus_width(card);
1774 if (err > 0 && mmc_card_hs(card)) {
1775 err = mmc_select_hs_ddr(card);
1776 if (err)
1777 goto free_card;
1782 * Choose the power class with selected bus interface
1784 mmc_select_powerclass(card);
1787 * Enable HPI feature (if supported)
1789 if (card->ext_csd.hpi) {
1790 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1791 EXT_CSD_HPI_MGMT, 1,
1792 card->ext_csd.generic_cmd6_time);
1793 if (err && err != -EBADMSG)
1794 goto free_card;
1795 if (err) {
1796 pr_warn("%s: Enabling HPI failed\n",
1797 mmc_hostname(card->host));
1798 card->ext_csd.hpi_en = 0;
1799 err = 0;
1800 } else {
1801 card->ext_csd.hpi_en = 1;
1806 * If cache size is higher than 0, this indicates the existence of cache
1807 * and it can be turned on. Note that some eMMCs from Micron has been
1808 * reported to need ~800 ms timeout, while enabling the cache after
1809 * sudden power failure tests. Let's extend the timeout to a minimum of
1810 * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
1812 if (card->ext_csd.cache_size > 0) {
1813 unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
1815 timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
1816 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1817 EXT_CSD_CACHE_CTRL, 1, timeout_ms);
1818 if (err && err != -EBADMSG)
1819 goto free_card;
1822 * Only if no error, cache is turned on successfully.
1824 if (err) {
1825 pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1826 mmc_hostname(card->host), err);
1827 card->ext_csd.cache_ctrl = 0;
1828 err = 0;
1829 } else {
1830 card->ext_csd.cache_ctrl = 1;
1835 * Enable Command Queue if supported. Note that Packed Commands cannot
1836 * be used with Command Queue.
1838 card->ext_csd.cmdq_en = false;
1839 if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) {
1840 err = mmc_cmdq_enable(card);
1841 if (err && err != -EBADMSG)
1842 goto free_card;
1843 if (err) {
1844 pr_warn("%s: Enabling CMDQ failed\n",
1845 mmc_hostname(card->host));
1846 card->ext_csd.cmdq_support = false;
1847 card->ext_csd.cmdq_depth = 0;
1848 err = 0;
1852 * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1853 * disabled for a time, so a flag is needed to indicate to re-enable the
1854 * Command Queue.
1856 card->reenable_cmdq = card->ext_csd.cmdq_en;
1858 if (card->ext_csd.cmdq_en && !host->cqe_enabled) {
1859 err = host->cqe_ops->cqe_enable(host, card);
1860 if (err) {
1861 pr_err("%s: Failed to enable CQE, error %d\n",
1862 mmc_hostname(host), err);
1863 } else {
1864 host->cqe_enabled = true;
1865 pr_info("%s: Command Queue Engine enabled\n",
1866 mmc_hostname(host));
1870 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1871 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1872 pr_err("%s: Host failed to negotiate down from 3.3V\n",
1873 mmc_hostname(host));
1874 err = -EINVAL;
1875 goto free_card;
1878 if (!oldcard)
1879 host->card = card;
1881 return 0;
1883 free_card:
1884 if (!oldcard)
1885 mmc_remove_card(card);
1886 err:
1887 return err;
1890 static int mmc_can_sleep(struct mmc_card *card)
1892 return (card && card->ext_csd.rev >= 3);
1895 static int mmc_sleep(struct mmc_host *host)
1897 struct mmc_command cmd = {};
1898 struct mmc_card *card = host->card;
1899 unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1900 int err;
1902 /* Re-tuning can't be done once the card is deselected */
1903 mmc_retune_hold(host);
1905 err = mmc_deselect_cards(host);
1906 if (err)
1907 goto out_release;
1909 cmd.opcode = MMC_SLEEP_AWAKE;
1910 cmd.arg = card->rca << 16;
1911 cmd.arg |= 1 << 15;
1914 * If the max_busy_timeout of the host is specified, validate it against
1915 * the sleep cmd timeout. A failure means we need to prevent the host
1916 * from doing hw busy detection, which is done by converting to a R1
1917 * response instead of a R1B.
1919 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) {
1920 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1921 } else {
1922 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
1923 cmd.busy_timeout = timeout_ms;
1926 err = mmc_wait_for_cmd(host, &cmd, 0);
1927 if (err)
1928 goto out_release;
1931 * If the host does not wait while the card signals busy, then we will
1932 * will have to wait the sleep/awake timeout. Note, we cannot use the
1933 * SEND_STATUS command to poll the status because that command (and most
1934 * others) is invalid while the card sleeps.
1936 if (!cmd.busy_timeout || !(host->caps & MMC_CAP_WAIT_WHILE_BUSY))
1937 mmc_delay(timeout_ms);
1939 out_release:
1940 mmc_retune_release(host);
1941 return err;
1944 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1946 return card &&
1947 mmc_card_mmc(card) &&
1948 (card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1951 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1953 unsigned int timeout = card->ext_csd.generic_cmd6_time;
1954 int err;
1956 /* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1957 if (notify_type == EXT_CSD_POWER_OFF_LONG)
1958 timeout = card->ext_csd.power_off_longtime;
1960 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1961 EXT_CSD_POWER_OFF_NOTIFICATION,
1962 notify_type, timeout, 0, true, false, false);
1963 if (err)
1964 pr_err("%s: Power Off Notification timed out, %u\n",
1965 mmc_hostname(card->host), timeout);
1967 /* Disable the power off notification after the switch operation. */
1968 card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1970 return err;
1974 * Host is being removed. Free up the current card.
1976 static void mmc_remove(struct mmc_host *host)
1978 mmc_remove_card(host->card);
1979 host->card = NULL;
1983 * Card detection - card is alive.
1985 static int mmc_alive(struct mmc_host *host)
1987 return mmc_send_status(host->card, NULL);
1991 * Card detection callback from host.
1993 static void mmc_detect(struct mmc_host *host)
1995 int err;
1997 mmc_get_card(host->card, NULL);
2000 * Just check if our card has been removed.
2002 err = _mmc_detect_card_removed(host);
2004 mmc_put_card(host->card, NULL);
2006 if (err) {
2007 mmc_remove(host);
2009 mmc_claim_host(host);
2010 mmc_detach_bus(host);
2011 mmc_power_off(host);
2012 mmc_release_host(host);
2016 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
2018 int err = 0;
2019 unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
2020 EXT_CSD_POWER_OFF_LONG;
2022 mmc_claim_host(host);
2024 if (mmc_card_suspended(host->card))
2025 goto out;
2027 err = mmc_flush_cache(host->card);
2028 if (err)
2029 goto out;
2031 if (mmc_can_poweroff_notify(host->card) &&
2032 ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend))
2033 err = mmc_poweroff_notify(host->card, notify_type);
2034 else if (mmc_can_sleep(host->card))
2035 err = mmc_sleep(host);
2036 else if (!mmc_host_is_spi(host))
2037 err = mmc_deselect_cards(host);
2039 if (!err) {
2040 mmc_power_off(host);
2041 mmc_card_set_suspended(host->card);
2043 out:
2044 mmc_release_host(host);
2045 return err;
2049 * Suspend callback
2051 static int mmc_suspend(struct mmc_host *host)
2053 int err;
2055 err = _mmc_suspend(host, true);
2056 if (!err) {
2057 pm_runtime_disable(&host->card->dev);
2058 pm_runtime_set_suspended(&host->card->dev);
2061 return err;
2065 * This function tries to determine if the same card is still present
2066 * and, if so, restore all state to it.
2068 static int _mmc_resume(struct mmc_host *host)
2070 int err = 0;
2072 mmc_claim_host(host);
2074 if (!mmc_card_suspended(host->card))
2075 goto out;
2077 mmc_power_up(host, host->card->ocr);
2078 err = mmc_init_card(host, host->card->ocr, host->card);
2079 mmc_card_clr_suspended(host->card);
2081 out:
2082 mmc_release_host(host);
2083 return err;
2087 * Shutdown callback
2089 static int mmc_shutdown(struct mmc_host *host)
2091 int err = 0;
2094 * In a specific case for poweroff notify, we need to resume the card
2095 * before we can shutdown it properly.
2097 if (mmc_can_poweroff_notify(host->card) &&
2098 !(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
2099 err = _mmc_resume(host);
2101 if (!err)
2102 err = _mmc_suspend(host, false);
2104 return err;
2108 * Callback for resume.
2110 static int mmc_resume(struct mmc_host *host)
2112 pm_runtime_enable(&host->card->dev);
2113 return 0;
2117 * Callback for runtime_suspend.
2119 static int mmc_runtime_suspend(struct mmc_host *host)
2121 int err;
2123 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2124 return 0;
2126 err = _mmc_suspend(host, true);
2127 if (err)
2128 pr_err("%s: error %d doing aggressive suspend\n",
2129 mmc_hostname(host), err);
2131 return err;
2135 * Callback for runtime_resume.
2137 static int mmc_runtime_resume(struct mmc_host *host)
2139 int err;
2141 err = _mmc_resume(host);
2142 if (err && err != -ENOMEDIUM)
2143 pr_err("%s: error %d doing runtime resume\n",
2144 mmc_hostname(host), err);
2146 return 0;
2149 static int mmc_can_reset(struct mmc_card *card)
2151 u8 rst_n_function;
2153 rst_n_function = card->ext_csd.rst_n_function;
2154 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2155 return 0;
2156 return 1;
2159 static int _mmc_hw_reset(struct mmc_host *host)
2161 struct mmc_card *card = host->card;
2164 * In the case of recovery, we can't expect flushing the cache to work
2165 * always, but we have a go and ignore errors.
2167 mmc_flush_cache(host->card);
2169 if ((host->caps & MMC_CAP_HW_RESET) && host->ops->hw_reset &&
2170 mmc_can_reset(card)) {
2171 /* If the card accept RST_n signal, send it. */
2172 mmc_set_clock(host, host->f_init);
2173 host->ops->hw_reset(host);
2174 /* Set initial state and call mmc_set_ios */
2175 mmc_set_initial_state(host);
2176 } else {
2177 /* Do a brute force power cycle */
2178 mmc_power_cycle(host, card->ocr);
2179 mmc_pwrseq_reset(host);
2181 return mmc_init_card(host, card->ocr, card);
2184 static const struct mmc_bus_ops mmc_ops = {
2185 .remove = mmc_remove,
2186 .detect = mmc_detect,
2187 .suspend = mmc_suspend,
2188 .resume = mmc_resume,
2189 .runtime_suspend = mmc_runtime_suspend,
2190 .runtime_resume = mmc_runtime_resume,
2191 .alive = mmc_alive,
2192 .shutdown = mmc_shutdown,
2193 .hw_reset = _mmc_hw_reset,
2197 * Starting point for MMC card init.
2199 int mmc_attach_mmc(struct mmc_host *host)
2201 int err;
2202 u32 ocr, rocr;
2204 WARN_ON(!host->claimed);
2206 /* Set correct bus mode for MMC before attempting attach */
2207 if (!mmc_host_is_spi(host))
2208 mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2210 err = mmc_send_op_cond(host, 0, &ocr);
2211 if (err)
2212 return err;
2214 mmc_attach_bus(host, &mmc_ops);
2215 if (host->ocr_avail_mmc)
2216 host->ocr_avail = host->ocr_avail_mmc;
2219 * We need to get OCR a different way for SPI.
2221 if (mmc_host_is_spi(host)) {
2222 err = mmc_spi_read_ocr(host, 1, &ocr);
2223 if (err)
2224 goto err;
2227 rocr = mmc_select_voltage(host, ocr);
2230 * Can we support the voltage of the card?
2232 if (!rocr) {
2233 err = -EINVAL;
2234 goto err;
2238 * Detect and init the card.
2240 err = mmc_init_card(host, rocr, NULL);
2241 if (err)
2242 goto err;
2244 mmc_release_host(host);
2245 err = mmc_add_card(host->card);
2246 if (err)
2247 goto remove_card;
2249 mmc_claim_host(host);
2250 return 0;
2252 remove_card:
2253 mmc_remove_card(host->card);
2254 mmc_claim_host(host);
2255 host->card = NULL;
2256 err:
2257 mmc_detach_bus(host);
2259 pr_err("%s: error %d whilst initialising MMC card\n",
2260 mmc_hostname(host), err);
2262 return err;