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