xref: /openbmc/linux/drivers/mmc/core/mmc.c (revision 2984f26a)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/mmc/core/mmc.c
4  *
5  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
6  *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7  *  MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
8  */
9 
10 #include <linux/err.h>
11 #include <linux/of.h>
12 #include <linux/slab.h>
13 #include <linux/stat.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/random.h>
16 #include <linux/sysfs.h>
17 
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21 
22 #include "core.h"
23 #include "card.h"
24 #include "host.h"
25 #include "bus.h"
26 #include "mmc_ops.h"
27 #include "quirks.h"
28 #include "sd_ops.h"
29 #include "pwrseq.h"
30 
31 #define DEFAULT_CMD6_TIMEOUT_MS	500
32 #define MIN_CACHE_EN_TIMEOUT_MS 1600
33 #define CACHE_FLUSH_TIMEOUT_MS 30000 /* 30s */
34 
35 static const unsigned int tran_exp[] = {
36 	10000,		100000,		1000000,	10000000,
37 	0,		0,		0,		0
38 };
39 
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,
43 };
44 
45 static const unsigned int taac_exp[] = {
46 	1,	10,	100,	1000,	10000,	100000,	1000000, 10000000,
47 };
48 
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,
52 };
53 
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;						\
61 									\
62 		__res = resp[__off] >> __shft;				\
63 		if (__size + __shft > 32)				\
64 			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
65 		__res & __mask;						\
66 	})
67 
68 /*
69  * Given the decoded CSD structure, decode the raw CID to our CID structure.
70  */
71 static int mmc_decode_cid(struct mmc_card *card)
72 {
73 	u32 *resp = card->raw_cid;
74 
75 	/*
76 	 * Add the raw card ID (cid) data to the entropy pool. It doesn't
77 	 * matter that not all of it is unique, it's just bonus entropy.
78 	 */
79 	add_device_randomness(&card->raw_cid, sizeof(card->raw_cid));
80 
81 	/*
82 	 * The selection of the format here is based upon published
83 	 * specs from sandisk and from what people have reported.
84 	 */
85 	switch (card->csd.mmca_vsn) {
86 	case 0: /* MMC v1.0 - v1.2 */
87 	case 1: /* MMC v1.4 */
88 		card->cid.manfid	= UNSTUFF_BITS(resp, 104, 24);
89 		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
90 		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
91 		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
92 		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
93 		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
94 		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
95 		card->cid.prod_name[6]	= UNSTUFF_BITS(resp, 48, 8);
96 		card->cid.hwrev		= UNSTUFF_BITS(resp, 44, 4);
97 		card->cid.fwrev		= UNSTUFF_BITS(resp, 40, 4);
98 		card->cid.serial	= UNSTUFF_BITS(resp, 16, 24);
99 		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
100 		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
101 		break;
102 
103 	case 2: /* MMC v2.0 - v2.2 */
104 	case 3: /* MMC v3.1 - v3.3 */
105 	case 4: /* MMC v4 */
106 		card->cid.manfid	= UNSTUFF_BITS(resp, 120, 8);
107 		card->cid.oemid		= UNSTUFF_BITS(resp, 104, 16);
108 		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
109 		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
110 		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
111 		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
112 		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
113 		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
114 		card->cid.prv		= UNSTUFF_BITS(resp, 48, 8);
115 		card->cid.serial	= UNSTUFF_BITS(resp, 16, 32);
116 		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
117 		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
118 		break;
119 
120 	default:
121 		pr_err("%s: card has unknown MMCA version %d\n",
122 			mmc_hostname(card->host), card->csd.mmca_vsn);
123 		return -EINVAL;
124 	}
125 
126 	return 0;
127 }
128 
129 static void mmc_set_erase_size(struct mmc_card *card)
130 {
131 	if (card->ext_csd.erase_group_def & 1)
132 		card->erase_size = card->ext_csd.hc_erase_size;
133 	else
134 		card->erase_size = card->csd.erase_size;
135 
136 	mmc_init_erase(card);
137 }
138 
139 /*
140  * Given a 128-bit response, decode to our card CSD structure.
141  */
142 static int mmc_decode_csd(struct mmc_card *card)
143 {
144 	struct mmc_csd *csd = &card->csd;
145 	unsigned int e, m, a, b;
146 	u32 *resp = card->raw_csd;
147 
148 	/*
149 	 * We only understand CSD structure v1.1 and v1.2.
150 	 * v1.2 has extra information in bits 15, 11 and 10.
151 	 * We also support eMMC v4.4 & v4.41.
152 	 */
153 	csd->structure = UNSTUFF_BITS(resp, 126, 2);
154 	if (csd->structure == 0) {
155 		pr_err("%s: unrecognised CSD structure version %d\n",
156 			mmc_hostname(card->host), csd->structure);
157 		return -EINVAL;
158 	}
159 
160 	csd->mmca_vsn	 = UNSTUFF_BITS(resp, 122, 4);
161 	m = UNSTUFF_BITS(resp, 115, 4);
162 	e = UNSTUFF_BITS(resp, 112, 3);
163 	csd->taac_ns	 = (taac_exp[e] * taac_mant[m] + 9) / 10;
164 	csd->taac_clks	 = UNSTUFF_BITS(resp, 104, 8) * 100;
165 
166 	m = UNSTUFF_BITS(resp, 99, 4);
167 	e = UNSTUFF_BITS(resp, 96, 3);
168 	csd->max_dtr	  = tran_exp[e] * tran_mant[m];
169 	csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
170 
171 	e = UNSTUFF_BITS(resp, 47, 3);
172 	m = UNSTUFF_BITS(resp, 62, 12);
173 	csd->capacity	  = (1 + m) << (e + 2);
174 
175 	csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
176 	csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
177 	csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
178 	csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
179 	csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
180 	csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
181 	csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
182 	csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
183 
184 	if (csd->write_blkbits >= 9) {
185 		a = UNSTUFF_BITS(resp, 42, 5);
186 		b = UNSTUFF_BITS(resp, 37, 5);
187 		csd->erase_size = (a + 1) * (b + 1);
188 		csd->erase_size <<= csd->write_blkbits - 9;
189 	}
190 
191 	return 0;
192 }
193 
194 static void mmc_select_card_type(struct mmc_card *card)
195 {
196 	struct mmc_host *host = card->host;
197 	u8 card_type = card->ext_csd.raw_card_type;
198 	u32 caps = host->caps, caps2 = host->caps2;
199 	unsigned int hs_max_dtr = 0, hs200_max_dtr = 0;
200 	unsigned int avail_type = 0;
201 
202 	if (caps & MMC_CAP_MMC_HIGHSPEED &&
203 	    card_type & EXT_CSD_CARD_TYPE_HS_26) {
204 		hs_max_dtr = MMC_HIGH_26_MAX_DTR;
205 		avail_type |= EXT_CSD_CARD_TYPE_HS_26;
206 	}
207 
208 	if (caps & MMC_CAP_MMC_HIGHSPEED &&
209 	    card_type & EXT_CSD_CARD_TYPE_HS_52) {
210 		hs_max_dtr = MMC_HIGH_52_MAX_DTR;
211 		avail_type |= EXT_CSD_CARD_TYPE_HS_52;
212 	}
213 
214 	if (caps & (MMC_CAP_1_8V_DDR | MMC_CAP_3_3V_DDR) &&
215 	    card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) {
216 		hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
217 		avail_type |= EXT_CSD_CARD_TYPE_DDR_1_8V;
218 	}
219 
220 	if (caps & MMC_CAP_1_2V_DDR &&
221 	    card_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
222 		hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
223 		avail_type |= EXT_CSD_CARD_TYPE_DDR_1_2V;
224 	}
225 
226 	if (caps2 & MMC_CAP2_HS200_1_8V_SDR &&
227 	    card_type & EXT_CSD_CARD_TYPE_HS200_1_8V) {
228 		hs200_max_dtr = MMC_HS200_MAX_DTR;
229 		avail_type |= EXT_CSD_CARD_TYPE_HS200_1_8V;
230 	}
231 
232 	if (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
233 	    card_type & EXT_CSD_CARD_TYPE_HS200_1_2V) {
234 		hs200_max_dtr = MMC_HS200_MAX_DTR;
235 		avail_type |= EXT_CSD_CARD_TYPE_HS200_1_2V;
236 	}
237 
238 	if (caps2 & MMC_CAP2_HS400_1_8V &&
239 	    card_type & EXT_CSD_CARD_TYPE_HS400_1_8V) {
240 		hs200_max_dtr = MMC_HS200_MAX_DTR;
241 		avail_type |= EXT_CSD_CARD_TYPE_HS400_1_8V;
242 	}
243 
244 	if (caps2 & MMC_CAP2_HS400_1_2V &&
245 	    card_type & EXT_CSD_CARD_TYPE_HS400_1_2V) {
246 		hs200_max_dtr = MMC_HS200_MAX_DTR;
247 		avail_type |= EXT_CSD_CARD_TYPE_HS400_1_2V;
248 	}
249 
250 	if ((caps2 & MMC_CAP2_HS400_ES) &&
251 	    card->ext_csd.strobe_support &&
252 	    (avail_type & EXT_CSD_CARD_TYPE_HS400))
253 		avail_type |= EXT_CSD_CARD_TYPE_HS400ES;
254 
255 	card->ext_csd.hs_max_dtr = hs_max_dtr;
256 	card->ext_csd.hs200_max_dtr = hs200_max_dtr;
257 	card->mmc_avail_type = avail_type;
258 }
259 
260 static void mmc_manage_enhanced_area(struct mmc_card *card, u8 *ext_csd)
261 {
262 	u8 hc_erase_grp_sz, hc_wp_grp_sz;
263 
264 	/*
265 	 * Disable these attributes by default
266 	 */
267 	card->ext_csd.enhanced_area_offset = -EINVAL;
268 	card->ext_csd.enhanced_area_size = -EINVAL;
269 
270 	/*
271 	 * Enhanced area feature support -- check whether the eMMC
272 	 * card has the Enhanced area enabled.  If so, export enhanced
273 	 * area offset and size to user by adding sysfs interface.
274 	 */
275 	if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
276 	    (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
277 		if (card->ext_csd.partition_setting_completed) {
278 			hc_erase_grp_sz =
279 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
280 			hc_wp_grp_sz =
281 				ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
282 
283 			/*
284 			 * calculate the enhanced data area offset, in bytes
285 			 */
286 			card->ext_csd.enhanced_area_offset =
287 				(((unsigned long long)ext_csd[139]) << 24) +
288 				(((unsigned long long)ext_csd[138]) << 16) +
289 				(((unsigned long long)ext_csd[137]) << 8) +
290 				(((unsigned long long)ext_csd[136]));
291 			if (mmc_card_blockaddr(card))
292 				card->ext_csd.enhanced_area_offset <<= 9;
293 			/*
294 			 * calculate the enhanced data area size, in kilobytes
295 			 */
296 			card->ext_csd.enhanced_area_size =
297 				(ext_csd[142] << 16) + (ext_csd[141] << 8) +
298 				ext_csd[140];
299 			card->ext_csd.enhanced_area_size *=
300 				(size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
301 			card->ext_csd.enhanced_area_size <<= 9;
302 		} else {
303 			pr_warn("%s: defines enhanced area without partition setting complete\n",
304 				mmc_hostname(card->host));
305 		}
306 	}
307 }
308 
309 static void mmc_part_add(struct mmc_card *card, u64 size,
310 			 unsigned int part_cfg, char *name, int idx, bool ro,
311 			 int area_type)
312 {
313 	card->part[card->nr_parts].size = size;
314 	card->part[card->nr_parts].part_cfg = part_cfg;
315 	sprintf(card->part[card->nr_parts].name, name, idx);
316 	card->part[card->nr_parts].force_ro = ro;
317 	card->part[card->nr_parts].area_type = area_type;
318 	card->nr_parts++;
319 }
320 
321 static void mmc_manage_gp_partitions(struct mmc_card *card, u8 *ext_csd)
322 {
323 	int idx;
324 	u8 hc_erase_grp_sz, hc_wp_grp_sz;
325 	u64 part_size;
326 
327 	/*
328 	 * General purpose partition feature support --
329 	 * If ext_csd has the size of general purpose partitions,
330 	 * set size, part_cfg, partition name in mmc_part.
331 	 */
332 	if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
333 	    EXT_CSD_PART_SUPPORT_PART_EN) {
334 		hc_erase_grp_sz =
335 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
336 		hc_wp_grp_sz =
337 			ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
338 
339 		for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
340 			if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
341 			    !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
342 			    !ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
343 				continue;
344 			if (card->ext_csd.partition_setting_completed == 0) {
345 				pr_warn("%s: has partition size defined without partition complete\n",
346 					mmc_hostname(card->host));
347 				break;
348 			}
349 			part_size =
350 				(ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
351 				<< 16) +
352 				(ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
353 				<< 8) +
354 				ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
355 			part_size *= (hc_erase_grp_sz * hc_wp_grp_sz);
356 			mmc_part_add(card, part_size << 19,
357 				EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
358 				"gp%d", idx, false,
359 				MMC_BLK_DATA_AREA_GP);
360 		}
361 	}
362 }
363 
364 /* Minimum partition switch timeout in milliseconds */
365 #define MMC_MIN_PART_SWITCH_TIME	300
366 
367 /*
368  * Decode extended CSD.
369  */
370 static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
371 {
372 	int err = 0, idx;
373 	u64 part_size;
374 	struct device_node *np;
375 	bool broken_hpi = false;
376 
377 	/* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
378 	card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
379 	if (card->csd.structure == 3) {
380 		if (card->ext_csd.raw_ext_csd_structure > 2) {
381 			pr_err("%s: unrecognised EXT_CSD structure "
382 				"version %d\n", mmc_hostname(card->host),
383 					card->ext_csd.raw_ext_csd_structure);
384 			err = -EINVAL;
385 			goto out;
386 		}
387 	}
388 
389 	np = mmc_of_find_child_device(card->host, 0);
390 	if (np && of_device_is_compatible(np, "mmc-card"))
391 		broken_hpi = of_property_read_bool(np, "broken-hpi");
392 	of_node_put(np);
393 
394 	/*
395 	 * The EXT_CSD format is meant to be forward compatible. As long
396 	 * as CSD_STRUCTURE does not change, all values for EXT_CSD_REV
397 	 * are authorized, see JEDEC JESD84-B50 section B.8.
398 	 */
399 	card->ext_csd.rev = ext_csd[EXT_CSD_REV];
400 
401 	/* fixup device after ext_csd revision field is updated */
402 	mmc_fixup_device(card, mmc_ext_csd_fixups);
403 
404 	card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
405 	card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
406 	card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
407 	card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
408 	if (card->ext_csd.rev >= 2) {
409 		card->ext_csd.sectors =
410 			ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
411 			ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
412 			ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
413 			ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
414 
415 		/* Cards with density > 2GiB are sector addressed */
416 		if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
417 			mmc_card_set_blockaddr(card);
418 	}
419 
420 	card->ext_csd.strobe_support = ext_csd[EXT_CSD_STROBE_SUPPORT];
421 	card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
422 	mmc_select_card_type(card);
423 
424 	card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
425 	card->ext_csd.raw_erase_timeout_mult =
426 		ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
427 	card->ext_csd.raw_hc_erase_grp_size =
428 		ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
429 	card->ext_csd.raw_boot_mult =
430 		ext_csd[EXT_CSD_BOOT_MULT];
431 	if (card->ext_csd.rev >= 3) {
432 		u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
433 		card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
434 
435 		/* EXT_CSD value is in units of 10ms, but we store in ms */
436 		card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
437 
438 		/* Sleep / awake timeout in 100ns units */
439 		if (sa_shift > 0 && sa_shift <= 0x17)
440 			card->ext_csd.sa_timeout =
441 					1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
442 		card->ext_csd.erase_group_def =
443 			ext_csd[EXT_CSD_ERASE_GROUP_DEF];
444 		card->ext_csd.hc_erase_timeout = 300 *
445 			ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
446 		card->ext_csd.hc_erase_size =
447 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
448 
449 		card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
450 
451 		/*
452 		 * There are two boot regions of equal size, defined in
453 		 * multiples of 128K.
454 		 */
455 		if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
456 			for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
457 				part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
458 				mmc_part_add(card, part_size,
459 					EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
460 					"boot%d", idx, true,
461 					MMC_BLK_DATA_AREA_BOOT);
462 			}
463 		}
464 	}
465 
466 	card->ext_csd.raw_hc_erase_gap_size =
467 		ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
468 	card->ext_csd.raw_sec_trim_mult =
469 		ext_csd[EXT_CSD_SEC_TRIM_MULT];
470 	card->ext_csd.raw_sec_erase_mult =
471 		ext_csd[EXT_CSD_SEC_ERASE_MULT];
472 	card->ext_csd.raw_sec_feature_support =
473 		ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
474 	card->ext_csd.raw_trim_mult =
475 		ext_csd[EXT_CSD_TRIM_MULT];
476 	card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
477 	card->ext_csd.raw_driver_strength = ext_csd[EXT_CSD_DRIVER_STRENGTH];
478 	if (card->ext_csd.rev >= 4) {
479 		if (ext_csd[EXT_CSD_PARTITION_SETTING_COMPLETED] &
480 		    EXT_CSD_PART_SETTING_COMPLETED)
481 			card->ext_csd.partition_setting_completed = 1;
482 		else
483 			card->ext_csd.partition_setting_completed = 0;
484 
485 		mmc_manage_enhanced_area(card, ext_csd);
486 
487 		mmc_manage_gp_partitions(card, ext_csd);
488 
489 		card->ext_csd.sec_trim_mult =
490 			ext_csd[EXT_CSD_SEC_TRIM_MULT];
491 		card->ext_csd.sec_erase_mult =
492 			ext_csd[EXT_CSD_SEC_ERASE_MULT];
493 		card->ext_csd.sec_feature_support =
494 			ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
495 		card->ext_csd.trim_timeout = 300 *
496 			ext_csd[EXT_CSD_TRIM_MULT];
497 
498 		/*
499 		 * Note that the call to mmc_part_add above defaults to read
500 		 * only. If this default assumption is changed, the call must
501 		 * take into account the value of boot_locked below.
502 		 */
503 		card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
504 		card->ext_csd.boot_ro_lockable = true;
505 
506 		/* Save power class values */
507 		card->ext_csd.raw_pwr_cl_52_195 =
508 			ext_csd[EXT_CSD_PWR_CL_52_195];
509 		card->ext_csd.raw_pwr_cl_26_195 =
510 			ext_csd[EXT_CSD_PWR_CL_26_195];
511 		card->ext_csd.raw_pwr_cl_52_360 =
512 			ext_csd[EXT_CSD_PWR_CL_52_360];
513 		card->ext_csd.raw_pwr_cl_26_360 =
514 			ext_csd[EXT_CSD_PWR_CL_26_360];
515 		card->ext_csd.raw_pwr_cl_200_195 =
516 			ext_csd[EXT_CSD_PWR_CL_200_195];
517 		card->ext_csd.raw_pwr_cl_200_360 =
518 			ext_csd[EXT_CSD_PWR_CL_200_360];
519 		card->ext_csd.raw_pwr_cl_ddr_52_195 =
520 			ext_csd[EXT_CSD_PWR_CL_DDR_52_195];
521 		card->ext_csd.raw_pwr_cl_ddr_52_360 =
522 			ext_csd[EXT_CSD_PWR_CL_DDR_52_360];
523 		card->ext_csd.raw_pwr_cl_ddr_200_360 =
524 			ext_csd[EXT_CSD_PWR_CL_DDR_200_360];
525 	}
526 
527 	if (card->ext_csd.rev >= 5) {
528 		/* Adjust production date as per JEDEC JESD84-B451 */
529 		if (card->cid.year < 2010)
530 			card->cid.year += 16;
531 
532 		/* check whether the eMMC card supports BKOPS */
533 		if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
534 			card->ext_csd.bkops = 1;
535 			card->ext_csd.man_bkops_en =
536 					(ext_csd[EXT_CSD_BKOPS_EN] &
537 						EXT_CSD_MANUAL_BKOPS_MASK);
538 			card->ext_csd.raw_bkops_status =
539 				ext_csd[EXT_CSD_BKOPS_STATUS];
540 			if (card->ext_csd.man_bkops_en)
541 				pr_debug("%s: MAN_BKOPS_EN bit is set\n",
542 					mmc_hostname(card->host));
543 			card->ext_csd.auto_bkops_en =
544 					(ext_csd[EXT_CSD_BKOPS_EN] &
545 						EXT_CSD_AUTO_BKOPS_MASK);
546 			if (card->ext_csd.auto_bkops_en)
547 				pr_debug("%s: AUTO_BKOPS_EN bit is set\n",
548 					mmc_hostname(card->host));
549 		}
550 
551 		/* check whether the eMMC card supports HPI */
552 		if (!mmc_card_broken_hpi(card) &&
553 		    !broken_hpi && (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1)) {
554 			card->ext_csd.hpi = 1;
555 			if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
556 				card->ext_csd.hpi_cmd =	MMC_STOP_TRANSMISSION;
557 			else
558 				card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
559 			/*
560 			 * Indicate the maximum timeout to close
561 			 * a command interrupted by HPI
562 			 */
563 			card->ext_csd.out_of_int_time =
564 				ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
565 		}
566 
567 		card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
568 		card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
569 
570 		/*
571 		 * RPMB regions are defined in multiples of 128K.
572 		 */
573 		card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
574 		if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
575 			mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
576 				EXT_CSD_PART_CONFIG_ACC_RPMB,
577 				"rpmb", 0, false,
578 				MMC_BLK_DATA_AREA_RPMB);
579 		}
580 	}
581 
582 	card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
583 	if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
584 		card->erased_byte = 0xFF;
585 	else
586 		card->erased_byte = 0x0;
587 
588 	/* eMMC v4.5 or later */
589 	card->ext_csd.generic_cmd6_time = DEFAULT_CMD6_TIMEOUT_MS;
590 	if (card->ext_csd.rev >= 6) {
591 		card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
592 
593 		card->ext_csd.generic_cmd6_time = 10 *
594 			ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
595 		card->ext_csd.power_off_longtime = 10 *
596 			ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
597 
598 		card->ext_csd.cache_size =
599 			ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
600 			ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
601 			ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
602 			ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
603 
604 		if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
605 			card->ext_csd.data_sector_size = 4096;
606 		else
607 			card->ext_csd.data_sector_size = 512;
608 
609 		if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
610 		    (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
611 			card->ext_csd.data_tag_unit_size =
612 			((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
613 			(card->ext_csd.data_sector_size);
614 		} else {
615 			card->ext_csd.data_tag_unit_size = 0;
616 		}
617 
618 		card->ext_csd.max_packed_writes =
619 			ext_csd[EXT_CSD_MAX_PACKED_WRITES];
620 		card->ext_csd.max_packed_reads =
621 			ext_csd[EXT_CSD_MAX_PACKED_READS];
622 	} else {
623 		card->ext_csd.data_sector_size = 512;
624 	}
625 
626 	/*
627 	 * GENERIC_CMD6_TIME is to be used "unless a specific timeout is defined
628 	 * when accessing a specific field", so use it here if there is no
629 	 * PARTITION_SWITCH_TIME.
630 	 */
631 	if (!card->ext_csd.part_time)
632 		card->ext_csd.part_time = card->ext_csd.generic_cmd6_time;
633 	/* Some eMMC set the value too low so set a minimum */
634 	if (card->ext_csd.part_time < MMC_MIN_PART_SWITCH_TIME)
635 		card->ext_csd.part_time = MMC_MIN_PART_SWITCH_TIME;
636 
637 	/* eMMC v5 or later */
638 	if (card->ext_csd.rev >= 7) {
639 		memcpy(card->ext_csd.fwrev, &ext_csd[EXT_CSD_FIRMWARE_VERSION],
640 		       MMC_FIRMWARE_LEN);
641 		card->ext_csd.ffu_capable =
642 			(ext_csd[EXT_CSD_SUPPORTED_MODE] & 0x1) &&
643 			!(ext_csd[EXT_CSD_FW_CONFIG] & 0x1);
644 
645 		card->ext_csd.pre_eol_info = ext_csd[EXT_CSD_PRE_EOL_INFO];
646 		card->ext_csd.device_life_time_est_typ_a =
647 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_A];
648 		card->ext_csd.device_life_time_est_typ_b =
649 			ext_csd[EXT_CSD_DEVICE_LIFE_TIME_EST_TYP_B];
650 	}
651 
652 	/* eMMC v5.1 or later */
653 	if (card->ext_csd.rev >= 8) {
654 		card->ext_csd.cmdq_support = ext_csd[EXT_CSD_CMDQ_SUPPORT] &
655 					     EXT_CSD_CMDQ_SUPPORTED;
656 		card->ext_csd.cmdq_depth = (ext_csd[EXT_CSD_CMDQ_DEPTH] &
657 					    EXT_CSD_CMDQ_DEPTH_MASK) + 1;
658 		/* Exclude inefficiently small queue depths */
659 		if (card->ext_csd.cmdq_depth <= 2) {
660 			card->ext_csd.cmdq_support = false;
661 			card->ext_csd.cmdq_depth = 0;
662 		}
663 		if (card->ext_csd.cmdq_support) {
664 			pr_debug("%s: Command Queue supported depth %u\n",
665 				 mmc_hostname(card->host),
666 				 card->ext_csd.cmdq_depth);
667 		}
668 		card->ext_csd.enhanced_rpmb_supported =
669 					(card->ext_csd.rel_param &
670 					 EXT_CSD_WR_REL_PARAM_EN_RPMB_REL_WR);
671 	}
672 out:
673 	return err;
674 }
675 
676 static int mmc_read_ext_csd(struct mmc_card *card)
677 {
678 	u8 *ext_csd;
679 	int err;
680 
681 	if (!mmc_can_ext_csd(card))
682 		return 0;
683 
684 	err = mmc_get_ext_csd(card, &ext_csd);
685 	if (err) {
686 		/* If the host or the card can't do the switch,
687 		 * fail more gracefully. */
688 		if ((err != -EINVAL)
689 		 && (err != -ENOSYS)
690 		 && (err != -EFAULT))
691 			return err;
692 
693 		/*
694 		 * High capacity cards should have this "magic" size
695 		 * stored in their CSD.
696 		 */
697 		if (card->csd.capacity == (4096 * 512)) {
698 			pr_err("%s: unable to read EXT_CSD on a possible high capacity card. Card will be ignored.\n",
699 				mmc_hostname(card->host));
700 		} else {
701 			pr_warn("%s: unable to read EXT_CSD, performance might suffer\n",
702 				mmc_hostname(card->host));
703 			err = 0;
704 		}
705 
706 		return err;
707 	}
708 
709 	err = mmc_decode_ext_csd(card, ext_csd);
710 	kfree(ext_csd);
711 	return err;
712 }
713 
714 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
715 {
716 	u8 *bw_ext_csd;
717 	int err;
718 
719 	if (bus_width == MMC_BUS_WIDTH_1)
720 		return 0;
721 
722 	err = mmc_get_ext_csd(card, &bw_ext_csd);
723 	if (err)
724 		return err;
725 
726 	/* only compare read only fields */
727 	err = !((card->ext_csd.raw_partition_support ==
728 			bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
729 		(card->ext_csd.raw_erased_mem_count ==
730 			bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
731 		(card->ext_csd.rev ==
732 			bw_ext_csd[EXT_CSD_REV]) &&
733 		(card->ext_csd.raw_ext_csd_structure ==
734 			bw_ext_csd[EXT_CSD_STRUCTURE]) &&
735 		(card->ext_csd.raw_card_type ==
736 			bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
737 		(card->ext_csd.raw_s_a_timeout ==
738 			bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
739 		(card->ext_csd.raw_hc_erase_gap_size ==
740 			bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
741 		(card->ext_csd.raw_erase_timeout_mult ==
742 			bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
743 		(card->ext_csd.raw_hc_erase_grp_size ==
744 			bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
745 		(card->ext_csd.raw_sec_trim_mult ==
746 			bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
747 		(card->ext_csd.raw_sec_erase_mult ==
748 			bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
749 		(card->ext_csd.raw_sec_feature_support ==
750 			bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
751 		(card->ext_csd.raw_trim_mult ==
752 			bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
753 		(card->ext_csd.raw_sectors[0] ==
754 			bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
755 		(card->ext_csd.raw_sectors[1] ==
756 			bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
757 		(card->ext_csd.raw_sectors[2] ==
758 			bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
759 		(card->ext_csd.raw_sectors[3] ==
760 			bw_ext_csd[EXT_CSD_SEC_CNT + 3]) &&
761 		(card->ext_csd.raw_pwr_cl_52_195 ==
762 			bw_ext_csd[EXT_CSD_PWR_CL_52_195]) &&
763 		(card->ext_csd.raw_pwr_cl_26_195 ==
764 			bw_ext_csd[EXT_CSD_PWR_CL_26_195]) &&
765 		(card->ext_csd.raw_pwr_cl_52_360 ==
766 			bw_ext_csd[EXT_CSD_PWR_CL_52_360]) &&
767 		(card->ext_csd.raw_pwr_cl_26_360 ==
768 			bw_ext_csd[EXT_CSD_PWR_CL_26_360]) &&
769 		(card->ext_csd.raw_pwr_cl_200_195 ==
770 			bw_ext_csd[EXT_CSD_PWR_CL_200_195]) &&
771 		(card->ext_csd.raw_pwr_cl_200_360 ==
772 			bw_ext_csd[EXT_CSD_PWR_CL_200_360]) &&
773 		(card->ext_csd.raw_pwr_cl_ddr_52_195 ==
774 			bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_195]) &&
775 		(card->ext_csd.raw_pwr_cl_ddr_52_360 ==
776 			bw_ext_csd[EXT_CSD_PWR_CL_DDR_52_360]) &&
777 		(card->ext_csd.raw_pwr_cl_ddr_200_360 ==
778 			bw_ext_csd[EXT_CSD_PWR_CL_DDR_200_360]));
779 
780 	if (err)
781 		err = -EINVAL;
782 
783 	kfree(bw_ext_csd);
784 	return err;
785 }
786 
787 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
788 	card->raw_cid[2], card->raw_cid[3]);
789 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
790 	card->raw_csd[2], card->raw_csd[3]);
791 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
792 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
793 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
794 MMC_DEV_ATTR(ffu_capable, "%d\n", card->ext_csd.ffu_capable);
795 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
796 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
797 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
798 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
799 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
800 MMC_DEV_ATTR(rev, "0x%x\n", card->ext_csd.rev);
801 MMC_DEV_ATTR(pre_eol_info, "0x%02x\n", card->ext_csd.pre_eol_info);
802 MMC_DEV_ATTR(life_time, "0x%02x 0x%02x\n",
803 	card->ext_csd.device_life_time_est_typ_a,
804 	card->ext_csd.device_life_time_est_typ_b);
805 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
806 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
807 		card->ext_csd.enhanced_area_offset);
808 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
809 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
810 MMC_DEV_ATTR(enhanced_rpmb_supported, "%#x\n",
811 	card->ext_csd.enhanced_rpmb_supported);
812 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
813 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
814 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
815 MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
816 
817 static ssize_t mmc_fwrev_show(struct device *dev,
818 			      struct device_attribute *attr,
819 			      char *buf)
820 {
821 	struct mmc_card *card = mmc_dev_to_card(dev);
822 
823 	if (card->ext_csd.rev < 7)
824 		return sysfs_emit(buf, "0x%x\n", card->cid.fwrev);
825 	else
826 		return sysfs_emit(buf, "0x%*phN\n", MMC_FIRMWARE_LEN,
827 				  card->ext_csd.fwrev);
828 }
829 
830 static DEVICE_ATTR(fwrev, S_IRUGO, mmc_fwrev_show, NULL);
831 
832 static ssize_t mmc_dsr_show(struct device *dev,
833 			    struct device_attribute *attr,
834 			    char *buf)
835 {
836 	struct mmc_card *card = mmc_dev_to_card(dev);
837 	struct mmc_host *host = card->host;
838 
839 	if (card->csd.dsr_imp && host->dsr_req)
840 		return sysfs_emit(buf, "0x%x\n", host->dsr);
841 	else
842 		/* return default DSR value */
843 		return sysfs_emit(buf, "0x%x\n", 0x404);
844 }
845 
846 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
847 
848 static struct attribute *mmc_std_attrs[] = {
849 	&dev_attr_cid.attr,
850 	&dev_attr_csd.attr,
851 	&dev_attr_date.attr,
852 	&dev_attr_erase_size.attr,
853 	&dev_attr_preferred_erase_size.attr,
854 	&dev_attr_fwrev.attr,
855 	&dev_attr_ffu_capable.attr,
856 	&dev_attr_hwrev.attr,
857 	&dev_attr_manfid.attr,
858 	&dev_attr_name.attr,
859 	&dev_attr_oemid.attr,
860 	&dev_attr_prv.attr,
861 	&dev_attr_rev.attr,
862 	&dev_attr_pre_eol_info.attr,
863 	&dev_attr_life_time.attr,
864 	&dev_attr_serial.attr,
865 	&dev_attr_enhanced_area_offset.attr,
866 	&dev_attr_enhanced_area_size.attr,
867 	&dev_attr_raw_rpmb_size_mult.attr,
868 	&dev_attr_enhanced_rpmb_supported.attr,
869 	&dev_attr_rel_sectors.attr,
870 	&dev_attr_ocr.attr,
871 	&dev_attr_rca.attr,
872 	&dev_attr_dsr.attr,
873 	&dev_attr_cmdq_en.attr,
874 	NULL,
875 };
876 ATTRIBUTE_GROUPS(mmc_std);
877 
878 static struct device_type mmc_type = {
879 	.groups = mmc_std_groups,
880 };
881 
882 /*
883  * Select the PowerClass for the current bus width
884  * If power class is defined for 4/8 bit bus in the
885  * extended CSD register, select it by executing the
886  * mmc_switch command.
887  */
888 static int __mmc_select_powerclass(struct mmc_card *card,
889 				   unsigned int bus_width)
890 {
891 	struct mmc_host *host = card->host;
892 	struct mmc_ext_csd *ext_csd = &card->ext_csd;
893 	unsigned int pwrclass_val = 0;
894 	int err = 0;
895 
896 	switch (1 << host->ios.vdd) {
897 	case MMC_VDD_165_195:
898 		if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
899 			pwrclass_val = ext_csd->raw_pwr_cl_26_195;
900 		else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
901 			pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
902 				ext_csd->raw_pwr_cl_52_195 :
903 				ext_csd->raw_pwr_cl_ddr_52_195;
904 		else if (host->ios.clock <= MMC_HS200_MAX_DTR)
905 			pwrclass_val = ext_csd->raw_pwr_cl_200_195;
906 		break;
907 	case MMC_VDD_27_28:
908 	case MMC_VDD_28_29:
909 	case MMC_VDD_29_30:
910 	case MMC_VDD_30_31:
911 	case MMC_VDD_31_32:
912 	case MMC_VDD_32_33:
913 	case MMC_VDD_33_34:
914 	case MMC_VDD_34_35:
915 	case MMC_VDD_35_36:
916 		if (host->ios.clock <= MMC_HIGH_26_MAX_DTR)
917 			pwrclass_val = ext_csd->raw_pwr_cl_26_360;
918 		else if (host->ios.clock <= MMC_HIGH_52_MAX_DTR)
919 			pwrclass_val = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
920 				ext_csd->raw_pwr_cl_52_360 :
921 				ext_csd->raw_pwr_cl_ddr_52_360;
922 		else if (host->ios.clock <= MMC_HS200_MAX_DTR)
923 			pwrclass_val = (bus_width == EXT_CSD_DDR_BUS_WIDTH_8) ?
924 				ext_csd->raw_pwr_cl_ddr_200_360 :
925 				ext_csd->raw_pwr_cl_200_360;
926 		break;
927 	default:
928 		pr_warn("%s: Voltage range not supported for power class\n",
929 			mmc_hostname(host));
930 		return -EINVAL;
931 	}
932 
933 	if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
934 		pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
935 				EXT_CSD_PWR_CL_8BIT_SHIFT;
936 	else
937 		pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
938 				EXT_CSD_PWR_CL_4BIT_SHIFT;
939 
940 	/* If the power class is different from the default value */
941 	if (pwrclass_val > 0) {
942 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
943 				 EXT_CSD_POWER_CLASS,
944 				 pwrclass_val,
945 				 card->ext_csd.generic_cmd6_time);
946 	}
947 
948 	return err;
949 }
950 
951 static int mmc_select_powerclass(struct mmc_card *card)
952 {
953 	struct mmc_host *host = card->host;
954 	u32 bus_width, ext_csd_bits;
955 	int err, ddr;
956 
957 	/* Power class selection is supported for versions >= 4.0 */
958 	if (!mmc_can_ext_csd(card))
959 		return 0;
960 
961 	bus_width = host->ios.bus_width;
962 	/* Power class values are defined only for 4/8 bit bus */
963 	if (bus_width == MMC_BUS_WIDTH_1)
964 		return 0;
965 
966 	ddr = card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52;
967 	if (ddr)
968 		ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
969 			EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
970 	else
971 		ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
972 			EXT_CSD_BUS_WIDTH_8 :  EXT_CSD_BUS_WIDTH_4;
973 
974 	err = __mmc_select_powerclass(card, ext_csd_bits);
975 	if (err)
976 		pr_warn("%s: power class selection to bus width %d ddr %d failed\n",
977 			mmc_hostname(host), 1 << bus_width, ddr);
978 
979 	return err;
980 }
981 
982 /*
983  * Set the bus speed for the selected speed mode.
984  */
985 static void mmc_set_bus_speed(struct mmc_card *card)
986 {
987 	unsigned int max_dtr = (unsigned int)-1;
988 
989 	if ((mmc_card_hs200(card) || mmc_card_hs400(card)) &&
990 	     max_dtr > card->ext_csd.hs200_max_dtr)
991 		max_dtr = card->ext_csd.hs200_max_dtr;
992 	else if (mmc_card_hs(card) && max_dtr > card->ext_csd.hs_max_dtr)
993 		max_dtr = card->ext_csd.hs_max_dtr;
994 	else if (max_dtr > card->csd.max_dtr)
995 		max_dtr = card->csd.max_dtr;
996 
997 	mmc_set_clock(card->host, max_dtr);
998 }
999 
1000 /*
1001  * Select the bus width amoung 4-bit and 8-bit(SDR).
1002  * If the bus width is changed successfully, return the selected width value.
1003  * Zero is returned instead of error value if the wide width is not supported.
1004  */
1005 static int mmc_select_bus_width(struct mmc_card *card)
1006 {
1007 	static unsigned ext_csd_bits[] = {
1008 		EXT_CSD_BUS_WIDTH_8,
1009 		EXT_CSD_BUS_WIDTH_4,
1010 		EXT_CSD_BUS_WIDTH_1,
1011 	};
1012 	static unsigned bus_widths[] = {
1013 		MMC_BUS_WIDTH_8,
1014 		MMC_BUS_WIDTH_4,
1015 		MMC_BUS_WIDTH_1,
1016 	};
1017 	struct mmc_host *host = card->host;
1018 	unsigned idx, bus_width = 0;
1019 	int err = 0;
1020 
1021 	if (!mmc_can_ext_csd(card) ||
1022 	    !(host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
1023 		return 0;
1024 
1025 	idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 0 : 1;
1026 
1027 	/*
1028 	 * Unlike SD, MMC cards dont have a configuration register to notify
1029 	 * supported bus width. So bus test command should be run to identify
1030 	 * the supported bus width or compare the ext csd values of current
1031 	 * bus width and ext csd values of 1 bit mode read earlier.
1032 	 */
1033 	for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1034 		/*
1035 		 * Host is capable of 8bit transfer, then switch
1036 		 * the device to work in 8bit transfer mode. If the
1037 		 * mmc switch command returns error then switch to
1038 		 * 4bit transfer mode. On success set the corresponding
1039 		 * bus width on the host.
1040 		 */
1041 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1042 				 EXT_CSD_BUS_WIDTH,
1043 				 ext_csd_bits[idx],
1044 				 card->ext_csd.generic_cmd6_time);
1045 		if (err)
1046 			continue;
1047 
1048 		bus_width = bus_widths[idx];
1049 		mmc_set_bus_width(host, bus_width);
1050 
1051 		/*
1052 		 * If controller can't handle bus width test,
1053 		 * compare ext_csd previously read in 1 bit mode
1054 		 * against ext_csd at new bus width
1055 		 */
1056 		if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1057 			err = mmc_compare_ext_csds(card, bus_width);
1058 		else
1059 			err = mmc_bus_test(card, bus_width);
1060 
1061 		if (!err) {
1062 			err = bus_width;
1063 			break;
1064 		} else {
1065 			pr_warn("%s: switch to bus width %d failed\n",
1066 				mmc_hostname(host), 1 << bus_width);
1067 		}
1068 	}
1069 
1070 	return err;
1071 }
1072 
1073 /*
1074  * Switch to the high-speed mode
1075  */
1076 static int mmc_select_hs(struct mmc_card *card)
1077 {
1078 	int err;
1079 
1080 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1081 			   EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1082 			   card->ext_csd.generic_cmd6_time, MMC_TIMING_MMC_HS,
1083 			   true, true, MMC_CMD_RETRIES);
1084 	if (err)
1085 		pr_warn("%s: switch to high-speed failed, err:%d\n",
1086 			mmc_hostname(card->host), err);
1087 
1088 	return err;
1089 }
1090 
1091 /*
1092  * Activate wide bus and DDR if supported.
1093  */
1094 static int mmc_select_hs_ddr(struct mmc_card *card)
1095 {
1096 	struct mmc_host *host = card->host;
1097 	u32 bus_width, ext_csd_bits;
1098 	int err = 0;
1099 
1100 	if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_52))
1101 		return 0;
1102 
1103 	bus_width = host->ios.bus_width;
1104 	if (bus_width == MMC_BUS_WIDTH_1)
1105 		return 0;
1106 
1107 	ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1108 		EXT_CSD_DDR_BUS_WIDTH_8 : EXT_CSD_DDR_BUS_WIDTH_4;
1109 
1110 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1111 			   EXT_CSD_BUS_WIDTH,
1112 			   ext_csd_bits,
1113 			   card->ext_csd.generic_cmd6_time,
1114 			   MMC_TIMING_MMC_DDR52,
1115 			   true, true, MMC_CMD_RETRIES);
1116 	if (err) {
1117 		pr_err("%s: switch to bus width %d ddr failed\n",
1118 			mmc_hostname(host), 1 << bus_width);
1119 		return err;
1120 	}
1121 
1122 	/*
1123 	 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1124 	 * signaling.
1125 	 *
1126 	 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1127 	 *
1128 	 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1129 	 * in the JEDEC spec for DDR.
1130 	 *
1131 	 * Even (e)MMC card can support 3.3v to 1.2v vccq, but not all
1132 	 * host controller can support this, like some of the SDHCI
1133 	 * controller which connect to an eMMC device. Some of these
1134 	 * host controller still needs to use 1.8v vccq for supporting
1135 	 * DDR mode.
1136 	 *
1137 	 * So the sequence will be:
1138 	 * if (host and device can both support 1.2v IO)
1139 	 *	use 1.2v IO;
1140 	 * else if (host and device can both support 1.8v IO)
1141 	 *	use 1.8v IO;
1142 	 * so if host and device can only support 3.3v IO, this is the
1143 	 * last choice.
1144 	 *
1145 	 * WARNING: eMMC rules are NOT the same as SD DDR
1146 	 */
1147 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_2V) {
1148 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1149 		if (!err)
1150 			return 0;
1151 	}
1152 
1153 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_DDR_1_8V &&
1154 	    host->caps & MMC_CAP_1_8V_DDR)
1155 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1156 
1157 	/* make sure vccq is 3.3v after switching disaster */
1158 	if (err)
1159 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
1160 
1161 	return err;
1162 }
1163 
1164 static int mmc_select_hs400(struct mmc_card *card)
1165 {
1166 	struct mmc_host *host = card->host;
1167 	unsigned int max_dtr;
1168 	int err = 0;
1169 	u8 val;
1170 
1171 	/*
1172 	 * HS400 mode requires 8-bit bus width
1173 	 */
1174 	if (!(card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1175 	      host->ios.bus_width == MMC_BUS_WIDTH_8))
1176 		return 0;
1177 
1178 	/* Switch card to HS mode */
1179 	val = EXT_CSD_TIMING_HS;
1180 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1181 			   EXT_CSD_HS_TIMING, val,
1182 			   card->ext_csd.generic_cmd6_time, 0,
1183 			   false, true, MMC_CMD_RETRIES);
1184 	if (err) {
1185 		pr_err("%s: switch to high-speed from hs200 failed, err:%d\n",
1186 			mmc_hostname(host), err);
1187 		return err;
1188 	}
1189 
1190 	/* Prepare host to downgrade to HS timing */
1191 	if (host->ops->hs400_downgrade)
1192 		host->ops->hs400_downgrade(host);
1193 
1194 	/* Set host controller to HS timing */
1195 	mmc_set_timing(host, MMC_TIMING_MMC_HS);
1196 
1197 	/* Reduce frequency to HS frequency */
1198 	max_dtr = card->ext_csd.hs_max_dtr;
1199 	mmc_set_clock(host, max_dtr);
1200 
1201 	err = mmc_switch_status(card, true);
1202 	if (err)
1203 		goto out_err;
1204 
1205 	if (host->ops->hs400_prepare_ddr)
1206 		host->ops->hs400_prepare_ddr(host);
1207 
1208 	/* Switch card to DDR */
1209 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1210 			 EXT_CSD_BUS_WIDTH,
1211 			 EXT_CSD_DDR_BUS_WIDTH_8,
1212 			 card->ext_csd.generic_cmd6_time);
1213 	if (err) {
1214 		pr_err("%s: switch to bus width for hs400 failed, err:%d\n",
1215 			mmc_hostname(host), err);
1216 		return err;
1217 	}
1218 
1219 	/* Switch card to HS400 */
1220 	val = EXT_CSD_TIMING_HS400 |
1221 	      card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1222 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1223 			   EXT_CSD_HS_TIMING, val,
1224 			   card->ext_csd.generic_cmd6_time, 0,
1225 			   false, true, MMC_CMD_RETRIES);
1226 	if (err) {
1227 		pr_err("%s: switch to hs400 failed, err:%d\n",
1228 			 mmc_hostname(host), err);
1229 		return err;
1230 	}
1231 
1232 	/* Set host controller to HS400 timing and frequency */
1233 	mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1234 	mmc_set_bus_speed(card);
1235 
1236 	if (host->ops->execute_hs400_tuning) {
1237 		mmc_retune_disable(host);
1238 		err = host->ops->execute_hs400_tuning(host, card);
1239 		mmc_retune_enable(host);
1240 		if (err)
1241 			goto out_err;
1242 	}
1243 
1244 	if (host->ops->hs400_complete)
1245 		host->ops->hs400_complete(host);
1246 
1247 	err = mmc_switch_status(card, true);
1248 	if (err)
1249 		goto out_err;
1250 
1251 	return 0;
1252 
1253 out_err:
1254 	pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1255 	       __func__, err);
1256 	return err;
1257 }
1258 
1259 int mmc_hs200_to_hs400(struct mmc_card *card)
1260 {
1261 	return mmc_select_hs400(card);
1262 }
1263 
1264 int mmc_hs400_to_hs200(struct mmc_card *card)
1265 {
1266 	struct mmc_host *host = card->host;
1267 	unsigned int max_dtr;
1268 	int err;
1269 	u8 val;
1270 
1271 	/* Reduce frequency to HS */
1272 	max_dtr = card->ext_csd.hs_max_dtr;
1273 	mmc_set_clock(host, max_dtr);
1274 
1275 	/* Switch HS400 to HS DDR */
1276 	val = EXT_CSD_TIMING_HS;
1277 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1278 			   val, card->ext_csd.generic_cmd6_time, 0,
1279 			   false, true, MMC_CMD_RETRIES);
1280 	if (err)
1281 		goto out_err;
1282 
1283 	if (host->ops->hs400_downgrade)
1284 		host->ops->hs400_downgrade(host);
1285 
1286 	mmc_set_timing(host, MMC_TIMING_MMC_DDR52);
1287 
1288 	err = mmc_switch_status(card, true);
1289 	if (err)
1290 		goto out_err;
1291 
1292 	/* Switch HS DDR to HS */
1293 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BUS_WIDTH,
1294 			   EXT_CSD_BUS_WIDTH_8, card->ext_csd.generic_cmd6_time,
1295 			   0, false, true, MMC_CMD_RETRIES);
1296 	if (err)
1297 		goto out_err;
1298 
1299 	mmc_set_timing(host, MMC_TIMING_MMC_HS);
1300 
1301 	err = mmc_switch_status(card, true);
1302 	if (err)
1303 		goto out_err;
1304 
1305 	/* Switch HS to HS200 */
1306 	val = EXT_CSD_TIMING_HS200 |
1307 	      card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1308 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_HS_TIMING,
1309 			   val, card->ext_csd.generic_cmd6_time, 0,
1310 			   false, true, MMC_CMD_RETRIES);
1311 	if (err)
1312 		goto out_err;
1313 
1314 	mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1315 
1316 	/*
1317 	 * For HS200, CRC errors are not a reliable way to know the switch
1318 	 * failed. If there really is a problem, we would expect tuning will
1319 	 * fail and the result ends up the same.
1320 	 */
1321 	err = mmc_switch_status(card, false);
1322 	if (err)
1323 		goto out_err;
1324 
1325 	mmc_set_bus_speed(card);
1326 
1327 	/* Prepare tuning for HS400 mode. */
1328 	if (host->ops->prepare_hs400_tuning)
1329 		host->ops->prepare_hs400_tuning(host, &host->ios);
1330 
1331 	return 0;
1332 
1333 out_err:
1334 	pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1335 	       __func__, err);
1336 	return err;
1337 }
1338 
1339 static void mmc_select_driver_type(struct mmc_card *card)
1340 {
1341 	int card_drv_type, drive_strength, drv_type = 0;
1342 	int fixed_drv_type = card->host->fixed_drv_type;
1343 
1344 	card_drv_type = card->ext_csd.raw_driver_strength |
1345 			mmc_driver_type_mask(0);
1346 
1347 	if (fixed_drv_type >= 0)
1348 		drive_strength = card_drv_type & mmc_driver_type_mask(fixed_drv_type)
1349 				 ? fixed_drv_type : 0;
1350 	else
1351 		drive_strength = mmc_select_drive_strength(card,
1352 							   card->ext_csd.hs200_max_dtr,
1353 							   card_drv_type, &drv_type);
1354 
1355 	card->drive_strength = drive_strength;
1356 
1357 	if (drv_type)
1358 		mmc_set_driver_type(card->host, drv_type);
1359 }
1360 
1361 static int mmc_select_hs400es(struct mmc_card *card)
1362 {
1363 	struct mmc_host *host = card->host;
1364 	int err = -EINVAL;
1365 	u8 val;
1366 
1367 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_2V)
1368 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1369 
1370 	if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400_1_8V)
1371 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1372 
1373 	/* If fails try again during next card power cycle */
1374 	if (err)
1375 		goto out_err;
1376 
1377 	err = mmc_select_bus_width(card);
1378 	if (err != MMC_BUS_WIDTH_8) {
1379 		pr_err("%s: switch to 8bit bus width failed, err:%d\n",
1380 			mmc_hostname(host), err);
1381 		err = err < 0 ? err : -ENOTSUPP;
1382 		goto out_err;
1383 	}
1384 
1385 	/* Switch card to HS mode */
1386 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1387 			   EXT_CSD_HS_TIMING, EXT_CSD_TIMING_HS,
1388 			   card->ext_csd.generic_cmd6_time, 0,
1389 			   false, true, MMC_CMD_RETRIES);
1390 	if (err) {
1391 		pr_err("%s: switch to hs for hs400es failed, err:%d\n",
1392 			mmc_hostname(host), err);
1393 		goto out_err;
1394 	}
1395 
1396 	/*
1397 	 * Bump to HS timing and frequency. Some cards don't handle
1398 	 * SEND_STATUS reliably at the initial frequency.
1399 	 */
1400 	mmc_set_timing(host, MMC_TIMING_MMC_HS);
1401 	mmc_set_bus_speed(card);
1402 
1403 	err = mmc_switch_status(card, true);
1404 	if (err)
1405 		goto out_err;
1406 
1407 	/* Switch card to DDR with strobe bit */
1408 	val = EXT_CSD_DDR_BUS_WIDTH_8 | EXT_CSD_BUS_WIDTH_STROBE;
1409 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1410 			 EXT_CSD_BUS_WIDTH,
1411 			 val,
1412 			 card->ext_csd.generic_cmd6_time);
1413 	if (err) {
1414 		pr_err("%s: switch to bus width for hs400es failed, err:%d\n",
1415 			mmc_hostname(host), err);
1416 		goto out_err;
1417 	}
1418 
1419 	mmc_select_driver_type(card);
1420 
1421 	/* Switch card to HS400 */
1422 	val = EXT_CSD_TIMING_HS400 |
1423 	      card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1424 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1425 			   EXT_CSD_HS_TIMING, val,
1426 			   card->ext_csd.generic_cmd6_time, 0,
1427 			   false, true, MMC_CMD_RETRIES);
1428 	if (err) {
1429 		pr_err("%s: switch to hs400es failed, err:%d\n",
1430 			mmc_hostname(host), err);
1431 		goto out_err;
1432 	}
1433 
1434 	/* Set host controller to HS400 timing and frequency */
1435 	mmc_set_timing(host, MMC_TIMING_MMC_HS400);
1436 
1437 	/* Controller enable enhanced strobe function */
1438 	host->ios.enhanced_strobe = true;
1439 	if (host->ops->hs400_enhanced_strobe)
1440 		host->ops->hs400_enhanced_strobe(host, &host->ios);
1441 
1442 	err = mmc_switch_status(card, true);
1443 	if (err)
1444 		goto out_err;
1445 
1446 	return 0;
1447 
1448 out_err:
1449 	pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1450 	       __func__, err);
1451 	return err;
1452 }
1453 
1454 /*
1455  * For device supporting HS200 mode, the following sequence
1456  * should be done before executing the tuning process.
1457  * 1. set the desired bus width(4-bit or 8-bit, 1-bit is not supported)
1458  * 2. switch to HS200 mode
1459  * 3. set the clock to > 52Mhz and <=200MHz
1460  */
1461 static int mmc_select_hs200(struct mmc_card *card)
1462 {
1463 	struct mmc_host *host = card->host;
1464 	unsigned int old_timing, old_signal_voltage, old_clock;
1465 	int err = -EINVAL;
1466 	u8 val;
1467 
1468 	old_signal_voltage = host->ios.signal_voltage;
1469 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_2V)
1470 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
1471 
1472 	if (err && card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200_1_8V)
1473 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
1474 
1475 	/* If fails try again during next card power cycle */
1476 	if (err)
1477 		return err;
1478 
1479 	mmc_select_driver_type(card);
1480 
1481 	/*
1482 	 * Set the bus width(4 or 8) with host's support and
1483 	 * switch to HS200 mode if bus width is set successfully.
1484 	 */
1485 	err = mmc_select_bus_width(card);
1486 	if (err > 0) {
1487 		val = EXT_CSD_TIMING_HS200 |
1488 		      card->drive_strength << EXT_CSD_DRV_STR_SHIFT;
1489 		err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1490 				   EXT_CSD_HS_TIMING, val,
1491 				   card->ext_csd.generic_cmd6_time, 0,
1492 				   false, true, MMC_CMD_RETRIES);
1493 		if (err)
1494 			goto err;
1495 
1496 		/*
1497 		 * Bump to HS timing and frequency. Some cards don't handle
1498 		 * SEND_STATUS reliably at the initial frequency.
1499 		 * NB: We can't move to full (HS200) speeds until after we've
1500 		 * successfully switched over.
1501 		 */
1502 		old_timing = host->ios.timing;
1503 		old_clock = host->ios.clock;
1504 		mmc_set_timing(host, MMC_TIMING_MMC_HS200);
1505 		mmc_set_clock(card->host, card->ext_csd.hs_max_dtr);
1506 
1507 		/*
1508 		 * For HS200, CRC errors are not a reliable way to know the
1509 		 * switch failed. If there really is a problem, we would expect
1510 		 * tuning will fail and the result ends up the same.
1511 		 */
1512 		err = mmc_switch_status(card, false);
1513 
1514 		/*
1515 		 * mmc_select_timing() assumes timing has not changed if
1516 		 * it is a switch error.
1517 		 */
1518 		if (err == -EBADMSG) {
1519 			mmc_set_clock(host, old_clock);
1520 			mmc_set_timing(host, old_timing);
1521 		}
1522 	}
1523 err:
1524 	if (err) {
1525 		/* fall back to the old signal voltage, if fails report error */
1526 		if (mmc_set_signal_voltage(host, old_signal_voltage))
1527 			err = -EIO;
1528 
1529 		pr_err("%s: %s failed, error %d\n", mmc_hostname(card->host),
1530 		       __func__, err);
1531 	}
1532 	return err;
1533 }
1534 
1535 /*
1536  * Activate High Speed, HS200 or HS400ES mode if supported.
1537  */
1538 static int mmc_select_timing(struct mmc_card *card)
1539 {
1540 	int err = 0;
1541 
1542 	if (!mmc_can_ext_csd(card))
1543 		goto bus_speed;
1544 
1545 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400ES) {
1546 		err = mmc_select_hs400es(card);
1547 		goto out;
1548 	}
1549 
1550 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS200) {
1551 		err = mmc_select_hs200(card);
1552 		if (err == -EBADMSG)
1553 			card->mmc_avail_type &= ~EXT_CSD_CARD_TYPE_HS200;
1554 		else
1555 			goto out;
1556 	}
1557 
1558 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS)
1559 		err = mmc_select_hs(card);
1560 
1561 out:
1562 	if (err && err != -EBADMSG)
1563 		return err;
1564 
1565 bus_speed:
1566 	/*
1567 	 * Set the bus speed to the selected bus timing.
1568 	 * If timing is not selected, backward compatible is the default.
1569 	 */
1570 	mmc_set_bus_speed(card);
1571 	return 0;
1572 }
1573 
1574 /*
1575  * Execute tuning sequence to seek the proper bus operating
1576  * conditions for HS200 and HS400, which sends CMD21 to the device.
1577  */
1578 static int mmc_hs200_tuning(struct mmc_card *card)
1579 {
1580 	struct mmc_host *host = card->host;
1581 
1582 	/*
1583 	 * Timing should be adjusted to the HS400 target
1584 	 * operation frequency for tuning process
1585 	 */
1586 	if (card->mmc_avail_type & EXT_CSD_CARD_TYPE_HS400 &&
1587 	    host->ios.bus_width == MMC_BUS_WIDTH_8)
1588 		if (host->ops->prepare_hs400_tuning)
1589 			host->ops->prepare_hs400_tuning(host, &host->ios);
1590 
1591 	return mmc_execute_tuning(card);
1592 }
1593 
1594 /*
1595  * Handle the detection and initialisation of a card.
1596  *
1597  * In the case of a resume, "oldcard" will contain the card
1598  * we're trying to reinitialise.
1599  */
1600 static int mmc_init_card(struct mmc_host *host, u32 ocr,
1601 	struct mmc_card *oldcard)
1602 {
1603 	struct mmc_card *card;
1604 	int err;
1605 	u32 cid[4];
1606 	u32 rocr;
1607 
1608 	WARN_ON(!host->claimed);
1609 
1610 	/* Set correct bus mode for MMC before attempting init */
1611 	if (!mmc_host_is_spi(host))
1612 		mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1613 
1614 	/*
1615 	 * Since we're changing the OCR value, we seem to
1616 	 * need to tell some cards to go back to the idle
1617 	 * state.  We wait 1ms to give cards time to
1618 	 * respond.
1619 	 * mmc_go_idle is needed for eMMC that are asleep
1620 	 */
1621 	mmc_go_idle(host);
1622 
1623 	/* The extra bit indicates that we support high capacity */
1624 	err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
1625 	if (err)
1626 		goto err;
1627 
1628 	/*
1629 	 * For SPI, enable CRC as appropriate.
1630 	 */
1631 	if (mmc_host_is_spi(host)) {
1632 		err = mmc_spi_set_crc(host, use_spi_crc);
1633 		if (err)
1634 			goto err;
1635 	}
1636 
1637 	/*
1638 	 * Fetch CID from card.
1639 	 */
1640 	err = mmc_send_cid(host, cid);
1641 	if (err)
1642 		goto err;
1643 
1644 	if (oldcard) {
1645 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1646 			pr_debug("%s: Perhaps the card was replaced\n",
1647 				mmc_hostname(host));
1648 			err = -ENOENT;
1649 			goto err;
1650 		}
1651 
1652 		card = oldcard;
1653 	} else {
1654 		/*
1655 		 * Allocate card structure.
1656 		 */
1657 		card = mmc_alloc_card(host, &mmc_type);
1658 		if (IS_ERR(card)) {
1659 			err = PTR_ERR(card);
1660 			goto err;
1661 		}
1662 
1663 		card->ocr = ocr;
1664 		card->type = MMC_TYPE_MMC;
1665 		card->rca = 1;
1666 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1667 	}
1668 
1669 	/*
1670 	 * Call the optional HC's init_card function to handle quirks.
1671 	 */
1672 	if (host->ops->init_card)
1673 		host->ops->init_card(host, card);
1674 
1675 	/*
1676 	 * For native busses:  set card RCA and quit open drain mode.
1677 	 */
1678 	if (!mmc_host_is_spi(host)) {
1679 		err = mmc_set_relative_addr(card);
1680 		if (err)
1681 			goto free_card;
1682 
1683 		mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1684 	}
1685 
1686 	if (!oldcard) {
1687 		/*
1688 		 * Fetch CSD from card.
1689 		 */
1690 		err = mmc_send_csd(card, card->raw_csd);
1691 		if (err)
1692 			goto free_card;
1693 
1694 		err = mmc_decode_csd(card);
1695 		if (err)
1696 			goto free_card;
1697 		err = mmc_decode_cid(card);
1698 		if (err)
1699 			goto free_card;
1700 	}
1701 
1702 	/*
1703 	 * handling only for cards supporting DSR and hosts requesting
1704 	 * DSR configuration
1705 	 */
1706 	if (card->csd.dsr_imp && host->dsr_req)
1707 		mmc_set_dsr(host);
1708 
1709 	/*
1710 	 * Select card, as all following commands rely on that.
1711 	 */
1712 	if (!mmc_host_is_spi(host)) {
1713 		err = mmc_select_card(card);
1714 		if (err)
1715 			goto free_card;
1716 	}
1717 
1718 	if (!oldcard) {
1719 		/* Read extended CSD. */
1720 		err = mmc_read_ext_csd(card);
1721 		if (err)
1722 			goto free_card;
1723 
1724 		/*
1725 		 * If doing byte addressing, check if required to do sector
1726 		 * addressing.  Handle the case of <2GB cards needing sector
1727 		 * addressing.  See section 8.1 JEDEC Standard JED84-A441;
1728 		 * ocr register has bit 30 set for sector addressing.
1729 		 */
1730 		if (rocr & BIT(30))
1731 			mmc_card_set_blockaddr(card);
1732 
1733 		/* Erase size depends on CSD and Extended CSD */
1734 		mmc_set_erase_size(card);
1735 	}
1736 
1737 	/* Enable ERASE_GRP_DEF. This bit is lost after a reset or power off. */
1738 	if (card->ext_csd.rev >= 3) {
1739 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1740 				 EXT_CSD_ERASE_GROUP_DEF, 1,
1741 				 card->ext_csd.generic_cmd6_time);
1742 
1743 		if (err && err != -EBADMSG)
1744 			goto free_card;
1745 
1746 		if (err) {
1747 			/*
1748 			 * Just disable enhanced area off & sz
1749 			 * will try to enable ERASE_GROUP_DEF
1750 			 * during next time reinit
1751 			 */
1752 			card->ext_csd.enhanced_area_offset = -EINVAL;
1753 			card->ext_csd.enhanced_area_size = -EINVAL;
1754 		} else {
1755 			card->ext_csd.erase_group_def = 1;
1756 			/*
1757 			 * enable ERASE_GRP_DEF successfully.
1758 			 * This will affect the erase size, so
1759 			 * here need to reset erase size
1760 			 */
1761 			mmc_set_erase_size(card);
1762 		}
1763 	}
1764 
1765 	/*
1766 	 * Ensure eMMC user default partition is enabled
1767 	 */
1768 	if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1769 		card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1770 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1771 				 card->ext_csd.part_config,
1772 				 card->ext_csd.part_time);
1773 		if (err && err != -EBADMSG)
1774 			goto free_card;
1775 	}
1776 
1777 	/*
1778 	 * Enable power_off_notification byte in the ext_csd register
1779 	 */
1780 	if (card->ext_csd.rev >= 6) {
1781 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1782 				 EXT_CSD_POWER_OFF_NOTIFICATION,
1783 				 EXT_CSD_POWER_ON,
1784 				 card->ext_csd.generic_cmd6_time);
1785 		if (err && err != -EBADMSG)
1786 			goto free_card;
1787 
1788 		/*
1789 		 * The err can be -EBADMSG or 0,
1790 		 * so check for success and update the flag
1791 		 */
1792 		if (!err)
1793 			card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1794 	}
1795 
1796 	/* set erase_arg */
1797 	if (mmc_can_discard(card))
1798 		card->erase_arg = MMC_DISCARD_ARG;
1799 	else if (mmc_can_trim(card))
1800 		card->erase_arg = MMC_TRIM_ARG;
1801 	else
1802 		card->erase_arg = MMC_ERASE_ARG;
1803 
1804 	/*
1805 	 * Select timing interface
1806 	 */
1807 	err = mmc_select_timing(card);
1808 	if (err)
1809 		goto free_card;
1810 
1811 	if (mmc_card_hs200(card)) {
1812 		host->doing_init_tune = 1;
1813 
1814 		err = mmc_hs200_tuning(card);
1815 		if (!err)
1816 			err = mmc_select_hs400(card);
1817 
1818 		host->doing_init_tune = 0;
1819 
1820 		if (err)
1821 			goto free_card;
1822 
1823 	} else if (!mmc_card_hs400es(card)) {
1824 		/* Select the desired bus width optionally */
1825 		err = mmc_select_bus_width(card);
1826 		if (err > 0 && mmc_card_hs(card)) {
1827 			err = mmc_select_hs_ddr(card);
1828 			if (err)
1829 				goto free_card;
1830 		}
1831 	}
1832 
1833 	/*
1834 	 * Choose the power class with selected bus interface
1835 	 */
1836 	mmc_select_powerclass(card);
1837 
1838 	/*
1839 	 * Enable HPI feature (if supported)
1840 	 */
1841 	if (card->ext_csd.hpi) {
1842 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1843 				EXT_CSD_HPI_MGMT, 1,
1844 				card->ext_csd.generic_cmd6_time);
1845 		if (err && err != -EBADMSG)
1846 			goto free_card;
1847 		if (err) {
1848 			pr_warn("%s: Enabling HPI failed\n",
1849 				mmc_hostname(card->host));
1850 			card->ext_csd.hpi_en = 0;
1851 		} else {
1852 			card->ext_csd.hpi_en = 1;
1853 		}
1854 	}
1855 
1856 	/*
1857 	 * If cache size is higher than 0, this indicates the existence of cache
1858 	 * and it can be turned on. Note that some eMMCs from Micron has been
1859 	 * reported to need ~800 ms timeout, while enabling the cache after
1860 	 * sudden power failure tests. Let's extend the timeout to a minimum of
1861 	 * DEFAULT_CACHE_EN_TIMEOUT_MS and do it for all cards.
1862 	 */
1863 	if (card->ext_csd.cache_size > 0) {
1864 		unsigned int timeout_ms = MIN_CACHE_EN_TIMEOUT_MS;
1865 
1866 		timeout_ms = max(card->ext_csd.generic_cmd6_time, timeout_ms);
1867 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1868 				EXT_CSD_CACHE_CTRL, 1, timeout_ms);
1869 		if (err && err != -EBADMSG)
1870 			goto free_card;
1871 
1872 		/*
1873 		 * Only if no error, cache is turned on successfully.
1874 		 */
1875 		if (err) {
1876 			pr_warn("%s: Cache is supported, but failed to turn on (%d)\n",
1877 				mmc_hostname(card->host), err);
1878 			card->ext_csd.cache_ctrl = 0;
1879 		} else {
1880 			card->ext_csd.cache_ctrl = 1;
1881 		}
1882 	}
1883 
1884 	/*
1885 	 * Enable Command Queue if supported. Note that Packed Commands cannot
1886 	 * be used with Command Queue.
1887 	 */
1888 	card->ext_csd.cmdq_en = false;
1889 	if (card->ext_csd.cmdq_support && host->caps2 & MMC_CAP2_CQE) {
1890 		err = mmc_cmdq_enable(card);
1891 		if (err && err != -EBADMSG)
1892 			goto free_card;
1893 		if (err) {
1894 			pr_warn("%s: Enabling CMDQ failed\n",
1895 				mmc_hostname(card->host));
1896 			card->ext_csd.cmdq_support = false;
1897 			card->ext_csd.cmdq_depth = 0;
1898 		}
1899 	}
1900 	/*
1901 	 * In some cases (e.g. RPMB or mmc_test), the Command Queue must be
1902 	 * disabled for a time, so a flag is needed to indicate to re-enable the
1903 	 * Command Queue.
1904 	 */
1905 	card->reenable_cmdq = card->ext_csd.cmdq_en;
1906 
1907 	if (host->cqe_ops && !host->cqe_enabled) {
1908 		err = host->cqe_ops->cqe_enable(host, card);
1909 		if (!err) {
1910 			host->cqe_enabled = true;
1911 
1912 			if (card->ext_csd.cmdq_en) {
1913 				pr_info("%s: Command Queue Engine enabled\n",
1914 					mmc_hostname(host));
1915 			} else {
1916 				host->hsq_enabled = true;
1917 				pr_info("%s: Host Software Queue enabled\n",
1918 					mmc_hostname(host));
1919 			}
1920 		}
1921 	}
1922 
1923 	if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1924 	    host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1925 		pr_err("%s: Host failed to negotiate down from 3.3V\n",
1926 			mmc_hostname(host));
1927 		err = -EINVAL;
1928 		goto free_card;
1929 	}
1930 
1931 	if (!oldcard)
1932 		host->card = card;
1933 
1934 	return 0;
1935 
1936 free_card:
1937 	if (!oldcard)
1938 		mmc_remove_card(card);
1939 err:
1940 	return err;
1941 }
1942 
1943 static int mmc_can_sleep(struct mmc_card *card)
1944 {
1945 	return card->ext_csd.rev >= 3;
1946 }
1947 
1948 static int mmc_sleep_busy_cb(void *cb_data, bool *busy)
1949 {
1950 	struct mmc_host *host = cb_data;
1951 
1952 	*busy = host->ops->card_busy(host);
1953 	return 0;
1954 }
1955 
1956 static int mmc_sleep(struct mmc_host *host)
1957 {
1958 	struct mmc_command cmd = {};
1959 	struct mmc_card *card = host->card;
1960 	unsigned int timeout_ms = DIV_ROUND_UP(card->ext_csd.sa_timeout, 10000);
1961 	bool use_r1b_resp;
1962 	int err;
1963 
1964 	/* Re-tuning can't be done once the card is deselected */
1965 	mmc_retune_hold(host);
1966 
1967 	err = mmc_deselect_cards(host);
1968 	if (err)
1969 		goto out_release;
1970 
1971 	cmd.opcode = MMC_SLEEP_AWAKE;
1972 	cmd.arg = card->rca << 16;
1973 	cmd.arg |= 1 << 15;
1974 	use_r1b_resp = mmc_prepare_busy_cmd(host, &cmd, timeout_ms);
1975 
1976 	err = mmc_wait_for_cmd(host, &cmd, 0);
1977 	if (err)
1978 		goto out_release;
1979 
1980 	/*
1981 	 * If the host does not wait while the card signals busy, then we can
1982 	 * try to poll, but only if the host supports HW polling, as the
1983 	 * SEND_STATUS cmd is not allowed. If we can't poll, then we simply need
1984 	 * to wait the sleep/awake timeout.
1985 	 */
1986 	if (host->caps & MMC_CAP_WAIT_WHILE_BUSY && use_r1b_resp)
1987 		goto out_release;
1988 
1989 	if (!host->ops->card_busy) {
1990 		mmc_delay(timeout_ms);
1991 		goto out_release;
1992 	}
1993 
1994 	err = __mmc_poll_for_busy(host, 0, timeout_ms, &mmc_sleep_busy_cb, host);
1995 
1996 out_release:
1997 	mmc_retune_release(host);
1998 	return err;
1999 }
2000 
2001 static int mmc_can_poweroff_notify(const struct mmc_card *card)
2002 {
2003 	return card &&
2004 		mmc_card_mmc(card) &&
2005 		(card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
2006 }
2007 
2008 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
2009 {
2010 	unsigned int timeout = card->ext_csd.generic_cmd6_time;
2011 	int err;
2012 
2013 	/* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
2014 	if (notify_type == EXT_CSD_POWER_OFF_LONG)
2015 		timeout = card->ext_csd.power_off_longtime;
2016 
2017 	err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2018 			EXT_CSD_POWER_OFF_NOTIFICATION,
2019 			notify_type, timeout, 0, false, false, MMC_CMD_RETRIES);
2020 	if (err)
2021 		pr_err("%s: Power Off Notification timed out, %u\n",
2022 		       mmc_hostname(card->host), timeout);
2023 
2024 	/* Disable the power off notification after the switch operation. */
2025 	card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
2026 
2027 	return err;
2028 }
2029 
2030 /*
2031  * Host is being removed. Free up the current card.
2032  */
2033 static void mmc_remove(struct mmc_host *host)
2034 {
2035 	mmc_remove_card(host->card);
2036 	host->card = NULL;
2037 }
2038 
2039 /*
2040  * Card detection - card is alive.
2041  */
2042 static int mmc_alive(struct mmc_host *host)
2043 {
2044 	return mmc_send_status(host->card, NULL);
2045 }
2046 
2047 /*
2048  * Card detection callback from host.
2049  */
2050 static void mmc_detect(struct mmc_host *host)
2051 {
2052 	int err;
2053 
2054 	mmc_get_card(host->card, NULL);
2055 
2056 	/*
2057 	 * Just check if our card has been removed.
2058 	 */
2059 	err = _mmc_detect_card_removed(host);
2060 
2061 	mmc_put_card(host->card, NULL);
2062 
2063 	if (err) {
2064 		mmc_remove(host);
2065 
2066 		mmc_claim_host(host);
2067 		mmc_detach_bus(host);
2068 		mmc_power_off(host);
2069 		mmc_release_host(host);
2070 	}
2071 }
2072 
2073 static bool _mmc_cache_enabled(struct mmc_host *host)
2074 {
2075 	return host->card->ext_csd.cache_size > 0 &&
2076 	       host->card->ext_csd.cache_ctrl & 1;
2077 }
2078 
2079 /*
2080  * Flush the internal cache of the eMMC to non-volatile storage.
2081  */
2082 static int _mmc_flush_cache(struct mmc_host *host)
2083 {
2084 	int err = 0;
2085 
2086 	if (mmc_card_broken_cache_flush(host->card) && !host->card->written_flag)
2087 		return 0;
2088 
2089 	if (_mmc_cache_enabled(host)) {
2090 		err = mmc_switch(host->card, EXT_CSD_CMD_SET_NORMAL,
2091 				 EXT_CSD_FLUSH_CACHE, 1,
2092 				 CACHE_FLUSH_TIMEOUT_MS);
2093 		if (err)
2094 			pr_err("%s: cache flush error %d\n", mmc_hostname(host), err);
2095 		else
2096 			host->card->written_flag = false;
2097 	}
2098 
2099 	return err;
2100 }
2101 
2102 static int _mmc_suspend(struct mmc_host *host, bool is_suspend)
2103 {
2104 	int err = 0;
2105 	unsigned int notify_type = is_suspend ? EXT_CSD_POWER_OFF_SHORT :
2106 					EXT_CSD_POWER_OFF_LONG;
2107 
2108 	mmc_claim_host(host);
2109 
2110 	if (mmc_card_suspended(host->card))
2111 		goto out;
2112 
2113 	err = _mmc_flush_cache(host);
2114 	if (err)
2115 		goto out;
2116 
2117 	if (mmc_can_poweroff_notify(host->card) &&
2118 	    ((host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) || !is_suspend ||
2119 	     (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE_IN_SUSPEND)))
2120 		err = mmc_poweroff_notify(host->card, notify_type);
2121 	else if (mmc_can_sleep(host->card))
2122 		err = mmc_sleep(host);
2123 	else if (!mmc_host_is_spi(host))
2124 		err = mmc_deselect_cards(host);
2125 
2126 	if (!err) {
2127 		mmc_power_off(host);
2128 		mmc_card_set_suspended(host->card);
2129 	}
2130 out:
2131 	mmc_release_host(host);
2132 	return err;
2133 }
2134 
2135 /*
2136  * Suspend callback
2137  */
2138 static int mmc_suspend(struct mmc_host *host)
2139 {
2140 	int err;
2141 
2142 	err = _mmc_suspend(host, true);
2143 	if (!err) {
2144 		pm_runtime_disable(&host->card->dev);
2145 		pm_runtime_set_suspended(&host->card->dev);
2146 	}
2147 
2148 	return err;
2149 }
2150 
2151 /*
2152  * This function tries to determine if the same card is still present
2153  * and, if so, restore all state to it.
2154  */
2155 static int _mmc_resume(struct mmc_host *host)
2156 {
2157 	int err = 0;
2158 
2159 	mmc_claim_host(host);
2160 
2161 	if (!mmc_card_suspended(host->card))
2162 		goto out;
2163 
2164 	mmc_power_up(host, host->card->ocr);
2165 	err = mmc_init_card(host, host->card->ocr, host->card);
2166 	mmc_card_clr_suspended(host->card);
2167 
2168 out:
2169 	mmc_release_host(host);
2170 	return err;
2171 }
2172 
2173 /*
2174  * Shutdown callback
2175  */
2176 static int mmc_shutdown(struct mmc_host *host)
2177 {
2178 	int err = 0;
2179 
2180 	/*
2181 	 * In a specific case for poweroff notify, we need to resume the card
2182 	 * before we can shutdown it properly.
2183 	 */
2184 	if (mmc_can_poweroff_notify(host->card) &&
2185 		!(host->caps2 & MMC_CAP2_FULL_PWR_CYCLE))
2186 		err = _mmc_resume(host);
2187 
2188 	if (!err)
2189 		err = _mmc_suspend(host, false);
2190 
2191 	return err;
2192 }
2193 
2194 /*
2195  * Callback for resume.
2196  */
2197 static int mmc_resume(struct mmc_host *host)
2198 {
2199 	pm_runtime_enable(&host->card->dev);
2200 	return 0;
2201 }
2202 
2203 /*
2204  * Callback for runtime_suspend.
2205  */
2206 static int mmc_runtime_suspend(struct mmc_host *host)
2207 {
2208 	int err;
2209 
2210 	if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
2211 		return 0;
2212 
2213 	err = _mmc_suspend(host, true);
2214 	if (err)
2215 		pr_err("%s: error %d doing aggressive suspend\n",
2216 			mmc_hostname(host), err);
2217 
2218 	return err;
2219 }
2220 
2221 /*
2222  * Callback for runtime_resume.
2223  */
2224 static int mmc_runtime_resume(struct mmc_host *host)
2225 {
2226 	int err;
2227 
2228 	err = _mmc_resume(host);
2229 	if (err && err != -ENOMEDIUM)
2230 		pr_err("%s: error %d doing runtime resume\n",
2231 			mmc_hostname(host), err);
2232 
2233 	return 0;
2234 }
2235 
2236 static int mmc_can_reset(struct mmc_card *card)
2237 {
2238 	u8 rst_n_function;
2239 
2240 	rst_n_function = card->ext_csd.rst_n_function;
2241 	if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2242 		return 0;
2243 	return 1;
2244 }
2245 
2246 static int _mmc_hw_reset(struct mmc_host *host)
2247 {
2248 	struct mmc_card *card = host->card;
2249 
2250 	/*
2251 	 * In the case of recovery, we can't expect flushing the cache to work
2252 	 * always, but we have a go and ignore errors.
2253 	 */
2254 	_mmc_flush_cache(host);
2255 
2256 	if ((host->caps & MMC_CAP_HW_RESET) && host->ops->card_hw_reset &&
2257 	     mmc_can_reset(card)) {
2258 		/* If the card accept RST_n signal, send it. */
2259 		mmc_set_clock(host, host->f_init);
2260 		host->ops->card_hw_reset(host);
2261 		/* Set initial state and call mmc_set_ios */
2262 		mmc_set_initial_state(host);
2263 	} else {
2264 		/* Do a brute force power cycle */
2265 		mmc_power_cycle(host, card->ocr);
2266 		mmc_pwrseq_reset(host);
2267 	}
2268 	return mmc_init_card(host, card->ocr, card);
2269 }
2270 
2271 static const struct mmc_bus_ops mmc_ops = {
2272 	.remove = mmc_remove,
2273 	.detect = mmc_detect,
2274 	.suspend = mmc_suspend,
2275 	.resume = mmc_resume,
2276 	.runtime_suspend = mmc_runtime_suspend,
2277 	.runtime_resume = mmc_runtime_resume,
2278 	.alive = mmc_alive,
2279 	.shutdown = mmc_shutdown,
2280 	.hw_reset = _mmc_hw_reset,
2281 	.cache_enabled = _mmc_cache_enabled,
2282 	.flush_cache = _mmc_flush_cache,
2283 };
2284 
2285 /*
2286  * Starting point for MMC card init.
2287  */
2288 int mmc_attach_mmc(struct mmc_host *host)
2289 {
2290 	int err;
2291 	u32 ocr, rocr;
2292 
2293 	WARN_ON(!host->claimed);
2294 
2295 	/* Set correct bus mode for MMC before attempting attach */
2296 	if (!mmc_host_is_spi(host))
2297 		mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
2298 
2299 	err = mmc_send_op_cond(host, 0, &ocr);
2300 	if (err)
2301 		return err;
2302 
2303 	mmc_attach_bus(host, &mmc_ops);
2304 	if (host->ocr_avail_mmc)
2305 		host->ocr_avail = host->ocr_avail_mmc;
2306 
2307 	/*
2308 	 * We need to get OCR a different way for SPI.
2309 	 */
2310 	if (mmc_host_is_spi(host)) {
2311 		err = mmc_spi_read_ocr(host, 1, &ocr);
2312 		if (err)
2313 			goto err;
2314 	}
2315 
2316 	rocr = mmc_select_voltage(host, ocr);
2317 
2318 	/*
2319 	 * Can we support the voltage of the card?
2320 	 */
2321 	if (!rocr) {
2322 		err = -EINVAL;
2323 		goto err;
2324 	}
2325 
2326 	/*
2327 	 * Detect and init the card.
2328 	 */
2329 	err = mmc_init_card(host, rocr, NULL);
2330 	if (err)
2331 		goto err;
2332 
2333 	mmc_release_host(host);
2334 	err = mmc_add_card(host->card);
2335 	if (err)
2336 		goto remove_card;
2337 
2338 	mmc_claim_host(host);
2339 	return 0;
2340 
2341 remove_card:
2342 	mmc_remove_card(host->card);
2343 	mmc_claim_host(host);
2344 	host->card = NULL;
2345 err:
2346 	mmc_detach_bus(host);
2347 
2348 	pr_err("%s: error %d whilst initialising MMC card\n",
2349 		mmc_hostname(host), err);
2350 
2351 	return err;
2352 }
2353