xref: /linux/drivers/mmc/core/mmc.c (revision f884ab15afdc5514e88105c92a4e2e1e6539869a)
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/slab.h>
15 #include <linux/stat.h>
16 
17 #include <linux/mmc/host.h>
18 #include <linux/mmc/card.h>
19 #include <linux/mmc/mmc.h>
20 
21 #include "core.h"
22 #include "bus.h"
23 #include "mmc_ops.h"
24 #include "sd_ops.h"
25 
26 static const unsigned int tran_exp[] = {
27 	10000,		100000,		1000000,	10000000,
28 	0,		0,		0,		0
29 };
30 
31 static const unsigned char tran_mant[] = {
32 	0,	10,	12,	13,	15,	20,	25,	30,
33 	35,	40,	45,	50,	55,	60,	70,	80,
34 };
35 
36 static const unsigned int tacc_exp[] = {
37 	1,	10,	100,	1000,	10000,	100000,	1000000, 10000000,
38 };
39 
40 static const unsigned int tacc_mant[] = {
41 	0,	10,	12,	13,	15,	20,	25,	30,
42 	35,	40,	45,	50,	55,	60,	70,	80,
43 };
44 
45 #define UNSTUFF_BITS(resp,start,size)					\
46 	({								\
47 		const int __size = size;				\
48 		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
49 		const int __off = 3 - ((start) / 32);			\
50 		const int __shft = (start) & 31;			\
51 		u32 __res;						\
52 									\
53 		__res = resp[__off] >> __shft;				\
54 		if (__size + __shft > 32)				\
55 			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
56 		__res & __mask;						\
57 	})
58 
59 /*
60  * Given the decoded CSD structure, decode the raw CID to our CID structure.
61  */
62 static int mmc_decode_cid(struct mmc_card *card)
63 {
64 	u32 *resp = card->raw_cid;
65 
66 	/*
67 	 * The selection of the format here is based upon published
68 	 * specs from sandisk and from what people have reported.
69 	 */
70 	switch (card->csd.mmca_vsn) {
71 	case 0: /* MMC v1.0 - v1.2 */
72 	case 1: /* MMC v1.4 */
73 		card->cid.manfid	= UNSTUFF_BITS(resp, 104, 24);
74 		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
75 		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
76 		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
77 		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
78 		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
79 		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
80 		card->cid.prod_name[6]	= UNSTUFF_BITS(resp, 48, 8);
81 		card->cid.hwrev		= UNSTUFF_BITS(resp, 44, 4);
82 		card->cid.fwrev		= UNSTUFF_BITS(resp, 40, 4);
83 		card->cid.serial	= UNSTUFF_BITS(resp, 16, 24);
84 		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
85 		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
86 		break;
87 
88 	case 2: /* MMC v2.0 - v2.2 */
89 	case 3: /* MMC v3.1 - v3.3 */
90 	case 4: /* MMC v4 */
91 		card->cid.manfid	= UNSTUFF_BITS(resp, 120, 8);
92 		card->cid.oemid		= UNSTUFF_BITS(resp, 104, 16);
93 		card->cid.prod_name[0]	= UNSTUFF_BITS(resp, 96, 8);
94 		card->cid.prod_name[1]	= UNSTUFF_BITS(resp, 88, 8);
95 		card->cid.prod_name[2]	= UNSTUFF_BITS(resp, 80, 8);
96 		card->cid.prod_name[3]	= UNSTUFF_BITS(resp, 72, 8);
97 		card->cid.prod_name[4]	= UNSTUFF_BITS(resp, 64, 8);
98 		card->cid.prod_name[5]	= UNSTUFF_BITS(resp, 56, 8);
99 		card->cid.prv		= UNSTUFF_BITS(resp, 48, 8);
100 		card->cid.serial	= UNSTUFF_BITS(resp, 16, 32);
101 		card->cid.month		= UNSTUFF_BITS(resp, 12, 4);
102 		card->cid.year		= UNSTUFF_BITS(resp, 8, 4) + 1997;
103 		break;
104 
105 	default:
106 		pr_err("%s: card has unknown MMCA version %d\n",
107 			mmc_hostname(card->host), card->csd.mmca_vsn);
108 		return -EINVAL;
109 	}
110 
111 	return 0;
112 }
113 
114 static void mmc_set_erase_size(struct mmc_card *card)
115 {
116 	if (card->ext_csd.erase_group_def & 1)
117 		card->erase_size = card->ext_csd.hc_erase_size;
118 	else
119 		card->erase_size = card->csd.erase_size;
120 
121 	mmc_init_erase(card);
122 }
123 
124 /*
125  * Given a 128-bit response, decode to our card CSD structure.
126  */
127 static int mmc_decode_csd(struct mmc_card *card)
128 {
129 	struct mmc_csd *csd = &card->csd;
130 	unsigned int e, m, a, b;
131 	u32 *resp = card->raw_csd;
132 
133 	/*
134 	 * We only understand CSD structure v1.1 and v1.2.
135 	 * v1.2 has extra information in bits 15, 11 and 10.
136 	 * We also support eMMC v4.4 & v4.41.
137 	 */
138 	csd->structure = UNSTUFF_BITS(resp, 126, 2);
139 	if (csd->structure == 0) {
140 		pr_err("%s: unrecognised CSD structure version %d\n",
141 			mmc_hostname(card->host), csd->structure);
142 		return -EINVAL;
143 	}
144 
145 	csd->mmca_vsn	 = UNSTUFF_BITS(resp, 122, 4);
146 	m = UNSTUFF_BITS(resp, 115, 4);
147 	e = UNSTUFF_BITS(resp, 112, 3);
148 	csd->tacc_ns	 = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
149 	csd->tacc_clks	 = UNSTUFF_BITS(resp, 104, 8) * 100;
150 
151 	m = UNSTUFF_BITS(resp, 99, 4);
152 	e = UNSTUFF_BITS(resp, 96, 3);
153 	csd->max_dtr	  = tran_exp[e] * tran_mant[m];
154 	csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
155 
156 	e = UNSTUFF_BITS(resp, 47, 3);
157 	m = UNSTUFF_BITS(resp, 62, 12);
158 	csd->capacity	  = (1 + m) << (e + 2);
159 
160 	csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
161 	csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
162 	csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
163 	csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
164 	csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
165 	csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
166 	csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
167 
168 	if (csd->write_blkbits >= 9) {
169 		a = UNSTUFF_BITS(resp, 42, 5);
170 		b = UNSTUFF_BITS(resp, 37, 5);
171 		csd->erase_size = (a + 1) * (b + 1);
172 		csd->erase_size <<= csd->write_blkbits - 9;
173 	}
174 
175 	return 0;
176 }
177 
178 /*
179  * Read extended CSD.
180  */
181 static int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd)
182 {
183 	int err;
184 	u8 *ext_csd;
185 
186 	BUG_ON(!card);
187 	BUG_ON(!new_ext_csd);
188 
189 	*new_ext_csd = NULL;
190 
191 	if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
192 		return 0;
193 
194 	/*
195 	 * As the ext_csd is so large and mostly unused, we don't store the
196 	 * raw block in mmc_card.
197 	 */
198 	ext_csd = kmalloc(512, GFP_KERNEL);
199 	if (!ext_csd) {
200 		pr_err("%s: could not allocate a buffer to "
201 			"receive the ext_csd.\n", mmc_hostname(card->host));
202 		return -ENOMEM;
203 	}
204 
205 	err = mmc_send_ext_csd(card, ext_csd);
206 	if (err) {
207 		kfree(ext_csd);
208 		*new_ext_csd = NULL;
209 
210 		/* If the host or the card can't do the switch,
211 		 * fail more gracefully. */
212 		if ((err != -EINVAL)
213 		 && (err != -ENOSYS)
214 		 && (err != -EFAULT))
215 			return err;
216 
217 		/*
218 		 * High capacity cards should have this "magic" size
219 		 * stored in their CSD.
220 		 */
221 		if (card->csd.capacity == (4096 * 512)) {
222 			pr_err("%s: unable to read EXT_CSD "
223 				"on a possible high capacity card. "
224 				"Card will be ignored.\n",
225 				mmc_hostname(card->host));
226 		} else {
227 			pr_warning("%s: unable to read "
228 				"EXT_CSD, performance might "
229 				"suffer.\n",
230 				mmc_hostname(card->host));
231 			err = 0;
232 		}
233 	} else
234 		*new_ext_csd = ext_csd;
235 
236 	return err;
237 }
238 
239 static void mmc_select_card_type(struct mmc_card *card)
240 {
241 	struct mmc_host *host = card->host;
242 	u8 card_type = card->ext_csd.raw_card_type & EXT_CSD_CARD_TYPE_MASK;
243 	u32 caps = host->caps, caps2 = host->caps2;
244 	unsigned int hs_max_dtr = 0;
245 
246 	if (card_type & EXT_CSD_CARD_TYPE_26)
247 		hs_max_dtr = MMC_HIGH_26_MAX_DTR;
248 
249 	if (caps & MMC_CAP_MMC_HIGHSPEED &&
250 			card_type & EXT_CSD_CARD_TYPE_52)
251 		hs_max_dtr = MMC_HIGH_52_MAX_DTR;
252 
253 	if ((caps & MMC_CAP_1_8V_DDR &&
254 			card_type & EXT_CSD_CARD_TYPE_DDR_1_8V) ||
255 	    (caps & MMC_CAP_1_2V_DDR &&
256 			card_type & EXT_CSD_CARD_TYPE_DDR_1_2V))
257 		hs_max_dtr = MMC_HIGH_DDR_MAX_DTR;
258 
259 	if ((caps2 & MMC_CAP2_HS200_1_8V_SDR &&
260 			card_type & EXT_CSD_CARD_TYPE_SDR_1_8V) ||
261 	    (caps2 & MMC_CAP2_HS200_1_2V_SDR &&
262 			card_type & EXT_CSD_CARD_TYPE_SDR_1_2V))
263 		hs_max_dtr = MMC_HS200_MAX_DTR;
264 
265 	card->ext_csd.hs_max_dtr = hs_max_dtr;
266 	card->ext_csd.card_type = card_type;
267 }
268 
269 /*
270  * Decode extended CSD.
271  */
272 static int mmc_read_ext_csd(struct mmc_card *card, u8 *ext_csd)
273 {
274 	int err = 0, idx;
275 	unsigned int part_size;
276 	u8 hc_erase_grp_sz = 0, hc_wp_grp_sz = 0;
277 
278 	BUG_ON(!card);
279 
280 	if (!ext_csd)
281 		return 0;
282 
283 	/* Version is coded in the CSD_STRUCTURE byte in the EXT_CSD register */
284 	card->ext_csd.raw_ext_csd_structure = ext_csd[EXT_CSD_STRUCTURE];
285 	if (card->csd.structure == 3) {
286 		if (card->ext_csd.raw_ext_csd_structure > 2) {
287 			pr_err("%s: unrecognised EXT_CSD structure "
288 				"version %d\n", mmc_hostname(card->host),
289 					card->ext_csd.raw_ext_csd_structure);
290 			err = -EINVAL;
291 			goto out;
292 		}
293 	}
294 
295 	card->ext_csd.rev = ext_csd[EXT_CSD_REV];
296 	if (card->ext_csd.rev > 6) {
297 		pr_err("%s: unrecognised EXT_CSD revision %d\n",
298 			mmc_hostname(card->host), card->ext_csd.rev);
299 		err = -EINVAL;
300 		goto out;
301 	}
302 
303 	card->ext_csd.raw_sectors[0] = ext_csd[EXT_CSD_SEC_CNT + 0];
304 	card->ext_csd.raw_sectors[1] = ext_csd[EXT_CSD_SEC_CNT + 1];
305 	card->ext_csd.raw_sectors[2] = ext_csd[EXT_CSD_SEC_CNT + 2];
306 	card->ext_csd.raw_sectors[3] = ext_csd[EXT_CSD_SEC_CNT + 3];
307 	if (card->ext_csd.rev >= 2) {
308 		card->ext_csd.sectors =
309 			ext_csd[EXT_CSD_SEC_CNT + 0] << 0 |
310 			ext_csd[EXT_CSD_SEC_CNT + 1] << 8 |
311 			ext_csd[EXT_CSD_SEC_CNT + 2] << 16 |
312 			ext_csd[EXT_CSD_SEC_CNT + 3] << 24;
313 
314 		/* Cards with density > 2GiB are sector addressed */
315 		if (card->ext_csd.sectors > (2u * 1024 * 1024 * 1024) / 512)
316 			mmc_card_set_blockaddr(card);
317 	}
318 
319 	card->ext_csd.raw_card_type = ext_csd[EXT_CSD_CARD_TYPE];
320 	mmc_select_card_type(card);
321 
322 	card->ext_csd.raw_s_a_timeout = ext_csd[EXT_CSD_S_A_TIMEOUT];
323 	card->ext_csd.raw_erase_timeout_mult =
324 		ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
325 	card->ext_csd.raw_hc_erase_grp_size =
326 		ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
327 	if (card->ext_csd.rev >= 3) {
328 		u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
329 		card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];
330 
331 		/* EXT_CSD value is in units of 10ms, but we store in ms */
332 		card->ext_csd.part_time = 10 * ext_csd[EXT_CSD_PART_SWITCH_TIME];
333 
334 		/* Sleep / awake timeout in 100ns units */
335 		if (sa_shift > 0 && sa_shift <= 0x17)
336 			card->ext_csd.sa_timeout =
337 					1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
338 		card->ext_csd.erase_group_def =
339 			ext_csd[EXT_CSD_ERASE_GROUP_DEF];
340 		card->ext_csd.hc_erase_timeout = 300 *
341 			ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
342 		card->ext_csd.hc_erase_size =
343 			ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
344 
345 		card->ext_csd.rel_sectors = ext_csd[EXT_CSD_REL_WR_SEC_C];
346 
347 		/*
348 		 * There are two boot regions of equal size, defined in
349 		 * multiples of 128K.
350 		 */
351 		if (ext_csd[EXT_CSD_BOOT_MULT] && mmc_boot_partition_access(card->host)) {
352 			for (idx = 0; idx < MMC_NUM_BOOT_PARTITION; idx++) {
353 				part_size = ext_csd[EXT_CSD_BOOT_MULT] << 17;
354 				mmc_part_add(card, part_size,
355 					EXT_CSD_PART_CONFIG_ACC_BOOT0 + idx,
356 					"boot%d", idx, true,
357 					MMC_BLK_DATA_AREA_BOOT);
358 			}
359 		}
360 	}
361 
362 	card->ext_csd.raw_hc_erase_gap_size =
363 		ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
364 	card->ext_csd.raw_sec_trim_mult =
365 		ext_csd[EXT_CSD_SEC_TRIM_MULT];
366 	card->ext_csd.raw_sec_erase_mult =
367 		ext_csd[EXT_CSD_SEC_ERASE_MULT];
368 	card->ext_csd.raw_sec_feature_support =
369 		ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
370 	card->ext_csd.raw_trim_mult =
371 		ext_csd[EXT_CSD_TRIM_MULT];
372 	card->ext_csd.raw_partition_support = ext_csd[EXT_CSD_PARTITION_SUPPORT];
373 	if (card->ext_csd.rev >= 4) {
374 		/*
375 		 * Enhanced area feature support -- check whether the eMMC
376 		 * card has the Enhanced area enabled.  If so, export enhanced
377 		 * area offset and size to user by adding sysfs interface.
378 		 */
379 		if ((ext_csd[EXT_CSD_PARTITION_SUPPORT] & 0x2) &&
380 		    (ext_csd[EXT_CSD_PARTITION_ATTRIBUTE] & 0x1)) {
381 			hc_erase_grp_sz =
382 				ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
383 			hc_wp_grp_sz =
384 				ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
385 
386 			card->ext_csd.enhanced_area_en = 1;
387 			/*
388 			 * calculate the enhanced data area offset, in bytes
389 			 */
390 			card->ext_csd.enhanced_area_offset =
391 				(ext_csd[139] << 24) + (ext_csd[138] << 16) +
392 				(ext_csd[137] << 8) + ext_csd[136];
393 			if (mmc_card_blockaddr(card))
394 				card->ext_csd.enhanced_area_offset <<= 9;
395 			/*
396 			 * calculate the enhanced data area size, in kilobytes
397 			 */
398 			card->ext_csd.enhanced_area_size =
399 				(ext_csd[142] << 16) + (ext_csd[141] << 8) +
400 				ext_csd[140];
401 			card->ext_csd.enhanced_area_size *=
402 				(size_t)(hc_erase_grp_sz * hc_wp_grp_sz);
403 			card->ext_csd.enhanced_area_size <<= 9;
404 		} else {
405 			/*
406 			 * If the enhanced area is not enabled, disable these
407 			 * device attributes.
408 			 */
409 			card->ext_csd.enhanced_area_offset = -EINVAL;
410 			card->ext_csd.enhanced_area_size = -EINVAL;
411 		}
412 
413 		/*
414 		 * General purpose partition feature support --
415 		 * If ext_csd has the size of general purpose partitions,
416 		 * set size, part_cfg, partition name in mmc_part.
417 		 */
418 		if (ext_csd[EXT_CSD_PARTITION_SUPPORT] &
419 			EXT_CSD_PART_SUPPORT_PART_EN) {
420 			if (card->ext_csd.enhanced_area_en != 1) {
421 				hc_erase_grp_sz =
422 					ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
423 				hc_wp_grp_sz =
424 					ext_csd[EXT_CSD_HC_WP_GRP_SIZE];
425 
426 				card->ext_csd.enhanced_area_en = 1;
427 			}
428 
429 			for (idx = 0; idx < MMC_NUM_GP_PARTITION; idx++) {
430 				if (!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3] &&
431 				!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1] &&
432 				!ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2])
433 					continue;
434 				part_size =
435 				(ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 2]
436 					<< 16) +
437 				(ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3 + 1]
438 					<< 8) +
439 				ext_csd[EXT_CSD_GP_SIZE_MULT + idx * 3];
440 				part_size *= (size_t)(hc_erase_grp_sz *
441 					hc_wp_grp_sz);
442 				mmc_part_add(card, part_size << 19,
443 					EXT_CSD_PART_CONFIG_ACC_GP0 + idx,
444 					"gp%d", idx, false,
445 					MMC_BLK_DATA_AREA_GP);
446 			}
447 		}
448 		card->ext_csd.sec_trim_mult =
449 			ext_csd[EXT_CSD_SEC_TRIM_MULT];
450 		card->ext_csd.sec_erase_mult =
451 			ext_csd[EXT_CSD_SEC_ERASE_MULT];
452 		card->ext_csd.sec_feature_support =
453 			ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
454 		card->ext_csd.trim_timeout = 300 *
455 			ext_csd[EXT_CSD_TRIM_MULT];
456 
457 		/*
458 		 * Note that the call to mmc_part_add above defaults to read
459 		 * only. If this default assumption is changed, the call must
460 		 * take into account the value of boot_locked below.
461 		 */
462 		card->ext_csd.boot_ro_lock = ext_csd[EXT_CSD_BOOT_WP];
463 		card->ext_csd.boot_ro_lockable = true;
464 	}
465 
466 	if (card->ext_csd.rev >= 5) {
467 		/* check whether the eMMC card supports BKOPS */
468 		if (ext_csd[EXT_CSD_BKOPS_SUPPORT] & 0x1) {
469 			card->ext_csd.bkops = 1;
470 			card->ext_csd.bkops_en = ext_csd[EXT_CSD_BKOPS_EN];
471 			card->ext_csd.raw_bkops_status =
472 				ext_csd[EXT_CSD_BKOPS_STATUS];
473 			if (!card->ext_csd.bkops_en)
474 				pr_info("%s: BKOPS_EN bit is not set\n",
475 					mmc_hostname(card->host));
476 		}
477 
478 		/* check whether the eMMC card supports HPI */
479 		if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x1) {
480 			card->ext_csd.hpi = 1;
481 			if (ext_csd[EXT_CSD_HPI_FEATURES] & 0x2)
482 				card->ext_csd.hpi_cmd =	MMC_STOP_TRANSMISSION;
483 			else
484 				card->ext_csd.hpi_cmd = MMC_SEND_STATUS;
485 			/*
486 			 * Indicate the maximum timeout to close
487 			 * a command interrupted by HPI
488 			 */
489 			card->ext_csd.out_of_int_time =
490 				ext_csd[EXT_CSD_OUT_OF_INTERRUPT_TIME] * 10;
491 		}
492 
493 		card->ext_csd.rel_param = ext_csd[EXT_CSD_WR_REL_PARAM];
494 		card->ext_csd.rst_n_function = ext_csd[EXT_CSD_RST_N_FUNCTION];
495 
496 		/*
497 		 * RPMB regions are defined in multiples of 128K.
498 		 */
499 		card->ext_csd.raw_rpmb_size_mult = ext_csd[EXT_CSD_RPMB_MULT];
500 		if (ext_csd[EXT_CSD_RPMB_MULT] && mmc_host_cmd23(card->host)) {
501 			mmc_part_add(card, ext_csd[EXT_CSD_RPMB_MULT] << 17,
502 				EXT_CSD_PART_CONFIG_ACC_RPMB,
503 				"rpmb", 0, false,
504 				MMC_BLK_DATA_AREA_RPMB);
505 		}
506 	}
507 
508 	card->ext_csd.raw_erased_mem_count = ext_csd[EXT_CSD_ERASED_MEM_CONT];
509 	if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
510 		card->erased_byte = 0xFF;
511 	else
512 		card->erased_byte = 0x0;
513 
514 	/* eMMC v4.5 or later */
515 	if (card->ext_csd.rev >= 6) {
516 		card->ext_csd.feature_support |= MMC_DISCARD_FEATURE;
517 
518 		card->ext_csd.generic_cmd6_time = 10 *
519 			ext_csd[EXT_CSD_GENERIC_CMD6_TIME];
520 		card->ext_csd.power_off_longtime = 10 *
521 			ext_csd[EXT_CSD_POWER_OFF_LONG_TIME];
522 
523 		card->ext_csd.cache_size =
524 			ext_csd[EXT_CSD_CACHE_SIZE + 0] << 0 |
525 			ext_csd[EXT_CSD_CACHE_SIZE + 1] << 8 |
526 			ext_csd[EXT_CSD_CACHE_SIZE + 2] << 16 |
527 			ext_csd[EXT_CSD_CACHE_SIZE + 3] << 24;
528 
529 		if (ext_csd[EXT_CSD_DATA_SECTOR_SIZE] == 1)
530 			card->ext_csd.data_sector_size = 4096;
531 		else
532 			card->ext_csd.data_sector_size = 512;
533 
534 		if ((ext_csd[EXT_CSD_DATA_TAG_SUPPORT] & 1) &&
535 		    (ext_csd[EXT_CSD_TAG_UNIT_SIZE] <= 8)) {
536 			card->ext_csd.data_tag_unit_size =
537 			((unsigned int) 1 << ext_csd[EXT_CSD_TAG_UNIT_SIZE]) *
538 			(card->ext_csd.data_sector_size);
539 		} else {
540 			card->ext_csd.data_tag_unit_size = 0;
541 		}
542 
543 		card->ext_csd.max_packed_writes =
544 			ext_csd[EXT_CSD_MAX_PACKED_WRITES];
545 		card->ext_csd.max_packed_reads =
546 			ext_csd[EXT_CSD_MAX_PACKED_READS];
547 	} else {
548 		card->ext_csd.data_sector_size = 512;
549 	}
550 
551 out:
552 	return err;
553 }
554 
555 static inline void mmc_free_ext_csd(u8 *ext_csd)
556 {
557 	kfree(ext_csd);
558 }
559 
560 
561 static int mmc_compare_ext_csds(struct mmc_card *card, unsigned bus_width)
562 {
563 	u8 *bw_ext_csd;
564 	int err;
565 
566 	if (bus_width == MMC_BUS_WIDTH_1)
567 		return 0;
568 
569 	err = mmc_get_ext_csd(card, &bw_ext_csd);
570 
571 	if (err || bw_ext_csd == NULL) {
572 		err = -EINVAL;
573 		goto out;
574 	}
575 
576 	/* only compare read only fields */
577 	err = !((card->ext_csd.raw_partition_support ==
578 			bw_ext_csd[EXT_CSD_PARTITION_SUPPORT]) &&
579 		(card->ext_csd.raw_erased_mem_count ==
580 			bw_ext_csd[EXT_CSD_ERASED_MEM_CONT]) &&
581 		(card->ext_csd.rev ==
582 			bw_ext_csd[EXT_CSD_REV]) &&
583 		(card->ext_csd.raw_ext_csd_structure ==
584 			bw_ext_csd[EXT_CSD_STRUCTURE]) &&
585 		(card->ext_csd.raw_card_type ==
586 			bw_ext_csd[EXT_CSD_CARD_TYPE]) &&
587 		(card->ext_csd.raw_s_a_timeout ==
588 			bw_ext_csd[EXT_CSD_S_A_TIMEOUT]) &&
589 		(card->ext_csd.raw_hc_erase_gap_size ==
590 			bw_ext_csd[EXT_CSD_HC_WP_GRP_SIZE]) &&
591 		(card->ext_csd.raw_erase_timeout_mult ==
592 			bw_ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]) &&
593 		(card->ext_csd.raw_hc_erase_grp_size ==
594 			bw_ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE]) &&
595 		(card->ext_csd.raw_sec_trim_mult ==
596 			bw_ext_csd[EXT_CSD_SEC_TRIM_MULT]) &&
597 		(card->ext_csd.raw_sec_erase_mult ==
598 			bw_ext_csd[EXT_CSD_SEC_ERASE_MULT]) &&
599 		(card->ext_csd.raw_sec_feature_support ==
600 			bw_ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]) &&
601 		(card->ext_csd.raw_trim_mult ==
602 			bw_ext_csd[EXT_CSD_TRIM_MULT]) &&
603 		(card->ext_csd.raw_sectors[0] ==
604 			bw_ext_csd[EXT_CSD_SEC_CNT + 0]) &&
605 		(card->ext_csd.raw_sectors[1] ==
606 			bw_ext_csd[EXT_CSD_SEC_CNT + 1]) &&
607 		(card->ext_csd.raw_sectors[2] ==
608 			bw_ext_csd[EXT_CSD_SEC_CNT + 2]) &&
609 		(card->ext_csd.raw_sectors[3] ==
610 			bw_ext_csd[EXT_CSD_SEC_CNT + 3]));
611 	if (err)
612 		err = -EINVAL;
613 
614 out:
615 	mmc_free_ext_csd(bw_ext_csd);
616 	return err;
617 }
618 
619 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
620 	card->raw_cid[2], card->raw_cid[3]);
621 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
622 	card->raw_csd[2], card->raw_csd[3]);
623 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
624 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
625 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
626 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
627 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
628 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
629 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
630 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
631 MMC_DEV_ATTR(prv, "0x%x\n", card->cid.prv);
632 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
633 MMC_DEV_ATTR(enhanced_area_offset, "%llu\n",
634 		card->ext_csd.enhanced_area_offset);
635 MMC_DEV_ATTR(enhanced_area_size, "%u\n", card->ext_csd.enhanced_area_size);
636 MMC_DEV_ATTR(raw_rpmb_size_mult, "%#x\n", card->ext_csd.raw_rpmb_size_mult);
637 MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
638 
639 static struct attribute *mmc_std_attrs[] = {
640 	&dev_attr_cid.attr,
641 	&dev_attr_csd.attr,
642 	&dev_attr_date.attr,
643 	&dev_attr_erase_size.attr,
644 	&dev_attr_preferred_erase_size.attr,
645 	&dev_attr_fwrev.attr,
646 	&dev_attr_hwrev.attr,
647 	&dev_attr_manfid.attr,
648 	&dev_attr_name.attr,
649 	&dev_attr_oemid.attr,
650 	&dev_attr_prv.attr,
651 	&dev_attr_serial.attr,
652 	&dev_attr_enhanced_area_offset.attr,
653 	&dev_attr_enhanced_area_size.attr,
654 	&dev_attr_raw_rpmb_size_mult.attr,
655 	&dev_attr_rel_sectors.attr,
656 	NULL,
657 };
658 
659 static struct attribute_group mmc_std_attr_group = {
660 	.attrs = mmc_std_attrs,
661 };
662 
663 static const struct attribute_group *mmc_attr_groups[] = {
664 	&mmc_std_attr_group,
665 	NULL,
666 };
667 
668 static struct device_type mmc_type = {
669 	.groups = mmc_attr_groups,
670 };
671 
672 /*
673  * Select the PowerClass for the current bus width
674  * If power class is defined for 4/8 bit bus in the
675  * extended CSD register, select it by executing the
676  * mmc_switch command.
677  */
678 static int mmc_select_powerclass(struct mmc_card *card,
679 		unsigned int bus_width, u8 *ext_csd)
680 {
681 	int err = 0;
682 	unsigned int pwrclass_val;
683 	unsigned int index = 0;
684 	struct mmc_host *host;
685 
686 	BUG_ON(!card);
687 
688 	host = card->host;
689 	BUG_ON(!host);
690 
691 	if (ext_csd == NULL)
692 		return 0;
693 
694 	/* Power class selection is supported for versions >= 4.0 */
695 	if (card->csd.mmca_vsn < CSD_SPEC_VER_4)
696 		return 0;
697 
698 	/* Power class values are defined only for 4/8 bit bus */
699 	if (bus_width == EXT_CSD_BUS_WIDTH_1)
700 		return 0;
701 
702 	switch (1 << host->ios.vdd) {
703 	case MMC_VDD_165_195:
704 		if (host->ios.clock <= 26000000)
705 			index = EXT_CSD_PWR_CL_26_195;
706 		else if	(host->ios.clock <= 52000000)
707 			index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
708 				EXT_CSD_PWR_CL_52_195 :
709 				EXT_CSD_PWR_CL_DDR_52_195;
710 		else if (host->ios.clock <= 200000000)
711 			index = EXT_CSD_PWR_CL_200_195;
712 		break;
713 	case MMC_VDD_27_28:
714 	case MMC_VDD_28_29:
715 	case MMC_VDD_29_30:
716 	case MMC_VDD_30_31:
717 	case MMC_VDD_31_32:
718 	case MMC_VDD_32_33:
719 	case MMC_VDD_33_34:
720 	case MMC_VDD_34_35:
721 	case MMC_VDD_35_36:
722 		if (host->ios.clock <= 26000000)
723 			index = EXT_CSD_PWR_CL_26_360;
724 		else if	(host->ios.clock <= 52000000)
725 			index = (bus_width <= EXT_CSD_BUS_WIDTH_8) ?
726 				EXT_CSD_PWR_CL_52_360 :
727 				EXT_CSD_PWR_CL_DDR_52_360;
728 		else if (host->ios.clock <= 200000000)
729 			index = EXT_CSD_PWR_CL_200_360;
730 		break;
731 	default:
732 		pr_warning("%s: Voltage range not supported "
733 			   "for power class.\n", mmc_hostname(host));
734 		return -EINVAL;
735 	}
736 
737 	pwrclass_val = ext_csd[index];
738 
739 	if (bus_width & (EXT_CSD_BUS_WIDTH_8 | EXT_CSD_DDR_BUS_WIDTH_8))
740 		pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_8BIT_MASK) >>
741 				EXT_CSD_PWR_CL_8BIT_SHIFT;
742 	else
743 		pwrclass_val = (pwrclass_val & EXT_CSD_PWR_CL_4BIT_MASK) >>
744 				EXT_CSD_PWR_CL_4BIT_SHIFT;
745 
746 	/* If the power class is different from the default value */
747 	if (pwrclass_val > 0) {
748 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
749 				 EXT_CSD_POWER_CLASS,
750 				 pwrclass_val,
751 				 card->ext_csd.generic_cmd6_time);
752 	}
753 
754 	return err;
755 }
756 
757 /*
758  * Selects the desired buswidth and switch to the HS200 mode
759  * if bus width set without error
760  */
761 static int mmc_select_hs200(struct mmc_card *card)
762 {
763 	int idx, err = -EINVAL;
764 	struct mmc_host *host;
765 	static unsigned ext_csd_bits[] = {
766 		EXT_CSD_BUS_WIDTH_4,
767 		EXT_CSD_BUS_WIDTH_8,
768 	};
769 	static unsigned bus_widths[] = {
770 		MMC_BUS_WIDTH_4,
771 		MMC_BUS_WIDTH_8,
772 	};
773 
774 	BUG_ON(!card);
775 
776 	host = card->host;
777 
778 	if (card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_2V &&
779 			host->caps2 & MMC_CAP2_HS200_1_2V_SDR)
780 		err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120);
781 
782 	if (err && card->ext_csd.card_type & EXT_CSD_CARD_TYPE_SDR_1_8V &&
783 			host->caps2 & MMC_CAP2_HS200_1_8V_SDR)
784 		err = __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
785 
786 	/* If fails try again during next card power cycle */
787 	if (err)
788 		goto err;
789 
790 	idx = (host->caps & MMC_CAP_8_BIT_DATA) ? 1 : 0;
791 
792 	/*
793 	 * Unlike SD, MMC cards dont have a configuration register to notify
794 	 * supported bus width. So bus test command should be run to identify
795 	 * the supported bus width or compare the ext csd values of current
796 	 * bus width and ext csd values of 1 bit mode read earlier.
797 	 */
798 	for (; idx >= 0; idx--) {
799 
800 		/*
801 		 * Host is capable of 8bit transfer, then switch
802 		 * the device to work in 8bit transfer mode. If the
803 		 * mmc switch command returns error then switch to
804 		 * 4bit transfer mode. On success set the corresponding
805 		 * bus width on the host.
806 		 */
807 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
808 				 EXT_CSD_BUS_WIDTH,
809 				 ext_csd_bits[idx],
810 				 card->ext_csd.generic_cmd6_time);
811 		if (err)
812 			continue;
813 
814 		mmc_set_bus_width(card->host, bus_widths[idx]);
815 
816 		if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
817 			err = mmc_compare_ext_csds(card, bus_widths[idx]);
818 		else
819 			err = mmc_bus_test(card, bus_widths[idx]);
820 		if (!err)
821 			break;
822 	}
823 
824 	/* switch to HS200 mode if bus width set successfully */
825 	if (!err)
826 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
827 				 EXT_CSD_HS_TIMING, 2, 0);
828 err:
829 	return err;
830 }
831 
832 /*
833  * Handle the detection and initialisation of a card.
834  *
835  * In the case of a resume, "oldcard" will contain the card
836  * we're trying to reinitialise.
837  */
838 static int mmc_init_card(struct mmc_host *host, u32 ocr,
839 	struct mmc_card *oldcard)
840 {
841 	struct mmc_card *card;
842 	int err, ddr = 0;
843 	u32 cid[4];
844 	unsigned int max_dtr;
845 	u32 rocr;
846 	u8 *ext_csd = NULL;
847 
848 	BUG_ON(!host);
849 	WARN_ON(!host->claimed);
850 
851 	/* Set correct bus mode for MMC before attempting init */
852 	if (!mmc_host_is_spi(host))
853 		mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
854 
855 	/*
856 	 * Since we're changing the OCR value, we seem to
857 	 * need to tell some cards to go back to the idle
858 	 * state.  We wait 1ms to give cards time to
859 	 * respond.
860 	 * mmc_go_idle is needed for eMMC that are asleep
861 	 */
862 	mmc_go_idle(host);
863 
864 	/* The extra bit indicates that we support high capacity */
865 	err = mmc_send_op_cond(host, ocr | (1 << 30), &rocr);
866 	if (err)
867 		goto err;
868 
869 	/*
870 	 * For SPI, enable CRC as appropriate.
871 	 */
872 	if (mmc_host_is_spi(host)) {
873 		err = mmc_spi_set_crc(host, use_spi_crc);
874 		if (err)
875 			goto err;
876 	}
877 
878 	/*
879 	 * Fetch CID from card.
880 	 */
881 	if (mmc_host_is_spi(host))
882 		err = mmc_send_cid(host, cid);
883 	else
884 		err = mmc_all_send_cid(host, cid);
885 	if (err)
886 		goto err;
887 
888 	if (oldcard) {
889 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
890 			err = -ENOENT;
891 			goto err;
892 		}
893 
894 		card = oldcard;
895 	} else {
896 		/*
897 		 * Allocate card structure.
898 		 */
899 		card = mmc_alloc_card(host, &mmc_type);
900 		if (IS_ERR(card)) {
901 			err = PTR_ERR(card);
902 			goto err;
903 		}
904 
905 		card->type = MMC_TYPE_MMC;
906 		card->rca = 1;
907 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
908 	}
909 
910 	/*
911 	 * For native busses:  set card RCA and quit open drain mode.
912 	 */
913 	if (!mmc_host_is_spi(host)) {
914 		err = mmc_set_relative_addr(card);
915 		if (err)
916 			goto free_card;
917 
918 		mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
919 	}
920 
921 	if (!oldcard) {
922 		/*
923 		 * Fetch CSD from card.
924 		 */
925 		err = mmc_send_csd(card, card->raw_csd);
926 		if (err)
927 			goto free_card;
928 
929 		err = mmc_decode_csd(card);
930 		if (err)
931 			goto free_card;
932 		err = mmc_decode_cid(card);
933 		if (err)
934 			goto free_card;
935 	}
936 
937 	/*
938 	 * Select card, as all following commands rely on that.
939 	 */
940 	if (!mmc_host_is_spi(host)) {
941 		err = mmc_select_card(card);
942 		if (err)
943 			goto free_card;
944 	}
945 
946 	if (!oldcard) {
947 		/*
948 		 * Fetch and process extended CSD.
949 		 */
950 
951 		err = mmc_get_ext_csd(card, &ext_csd);
952 		if (err)
953 			goto free_card;
954 		err = mmc_read_ext_csd(card, ext_csd);
955 		if (err)
956 			goto free_card;
957 
958 		/* If doing byte addressing, check if required to do sector
959 		 * addressing.  Handle the case of <2GB cards needing sector
960 		 * addressing.  See section 8.1 JEDEC Standard JED84-A441;
961 		 * ocr register has bit 30 set for sector addressing.
962 		 */
963 		if (!(mmc_card_blockaddr(card)) && (rocr & (1<<30)))
964 			mmc_card_set_blockaddr(card);
965 
966 		/* Erase size depends on CSD and Extended CSD */
967 		mmc_set_erase_size(card);
968 	}
969 
970 	/*
971 	 * If enhanced_area_en is TRUE, host needs to enable ERASE_GRP_DEF
972 	 * bit.  This bit will be lost every time after a reset or power off.
973 	 */
974 	if (card->ext_csd.enhanced_area_en ||
975 	    (card->ext_csd.rev >= 3 && (host->caps2 & MMC_CAP2_HC_ERASE_SZ))) {
976 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
977 				 EXT_CSD_ERASE_GROUP_DEF, 1,
978 				 card->ext_csd.generic_cmd6_time);
979 
980 		if (err && err != -EBADMSG)
981 			goto free_card;
982 
983 		if (err) {
984 			err = 0;
985 			/*
986 			 * Just disable enhanced area off & sz
987 			 * will try to enable ERASE_GROUP_DEF
988 			 * during next time reinit
989 			 */
990 			card->ext_csd.enhanced_area_offset = -EINVAL;
991 			card->ext_csd.enhanced_area_size = -EINVAL;
992 		} else {
993 			card->ext_csd.erase_group_def = 1;
994 			/*
995 			 * enable ERASE_GRP_DEF successfully.
996 			 * This will affect the erase size, so
997 			 * here need to reset erase size
998 			 */
999 			mmc_set_erase_size(card);
1000 		}
1001 	}
1002 
1003 	/*
1004 	 * Ensure eMMC user default partition is enabled
1005 	 */
1006 	if (card->ext_csd.part_config & EXT_CSD_PART_CONFIG_ACC_MASK) {
1007 		card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
1008 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_PART_CONFIG,
1009 				 card->ext_csd.part_config,
1010 				 card->ext_csd.part_time);
1011 		if (err && err != -EBADMSG)
1012 			goto free_card;
1013 	}
1014 
1015 	/*
1016 	 * If the host supports the power_off_notify capability then
1017 	 * set the notification byte in the ext_csd register of device
1018 	 */
1019 	if ((host->caps2 & MMC_CAP2_POWEROFF_NOTIFY) &&
1020 	    (card->ext_csd.rev >= 6)) {
1021 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022 				 EXT_CSD_POWER_OFF_NOTIFICATION,
1023 				 EXT_CSD_POWER_ON,
1024 				 card->ext_csd.generic_cmd6_time);
1025 		if (err && err != -EBADMSG)
1026 			goto free_card;
1027 
1028 		/*
1029 		 * The err can be -EBADMSG or 0,
1030 		 * so check for success and update the flag
1031 		 */
1032 		if (!err)
1033 			card->ext_csd.power_off_notification = EXT_CSD_POWER_ON;
1034 	}
1035 
1036 	/*
1037 	 * Activate high speed (if supported)
1038 	 */
1039 	if (card->ext_csd.hs_max_dtr != 0) {
1040 		err = 0;
1041 		if (card->ext_csd.hs_max_dtr > 52000000 &&
1042 		    host->caps2 & MMC_CAP2_HS200)
1043 			err = mmc_select_hs200(card);
1044 		else if	(host->caps & MMC_CAP_MMC_HIGHSPEED)
1045 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1046 					 EXT_CSD_HS_TIMING, 1,
1047 					 card->ext_csd.generic_cmd6_time);
1048 
1049 		if (err && err != -EBADMSG)
1050 			goto free_card;
1051 
1052 		if (err) {
1053 			pr_warning("%s: switch to highspeed failed\n",
1054 			       mmc_hostname(card->host));
1055 			err = 0;
1056 		} else {
1057 			if (card->ext_csd.hs_max_dtr > 52000000 &&
1058 			    host->caps2 & MMC_CAP2_HS200) {
1059 				mmc_card_set_hs200(card);
1060 				mmc_set_timing(card->host,
1061 					       MMC_TIMING_MMC_HS200);
1062 			} else {
1063 				mmc_card_set_highspeed(card);
1064 				mmc_set_timing(card->host, MMC_TIMING_MMC_HS);
1065 			}
1066 		}
1067 	}
1068 
1069 	/*
1070 	 * Compute bus speed.
1071 	 */
1072 	max_dtr = (unsigned int)-1;
1073 
1074 	if (mmc_card_highspeed(card) || mmc_card_hs200(card)) {
1075 		if (max_dtr > card->ext_csd.hs_max_dtr)
1076 			max_dtr = card->ext_csd.hs_max_dtr;
1077 		if (mmc_card_highspeed(card) && (max_dtr > 52000000))
1078 			max_dtr = 52000000;
1079 	} else if (max_dtr > card->csd.max_dtr) {
1080 		max_dtr = card->csd.max_dtr;
1081 	}
1082 
1083 	mmc_set_clock(host, max_dtr);
1084 
1085 	/*
1086 	 * Indicate DDR mode (if supported).
1087 	 */
1088 	if (mmc_card_highspeed(card)) {
1089 		if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_8V)
1090 			&& ((host->caps & (MMC_CAP_1_8V_DDR |
1091 			     MMC_CAP_UHS_DDR50))
1092 				== (MMC_CAP_1_8V_DDR | MMC_CAP_UHS_DDR50)))
1093 				ddr = MMC_1_8V_DDR_MODE;
1094 		else if ((card->ext_csd.card_type & EXT_CSD_CARD_TYPE_DDR_1_2V)
1095 			&& ((host->caps & (MMC_CAP_1_2V_DDR |
1096 			     MMC_CAP_UHS_DDR50))
1097 				== (MMC_CAP_1_2V_DDR | MMC_CAP_UHS_DDR50)))
1098 				ddr = MMC_1_2V_DDR_MODE;
1099 	}
1100 
1101 	/*
1102 	 * Indicate HS200 SDR mode (if supported).
1103 	 */
1104 	if (mmc_card_hs200(card)) {
1105 		u32 ext_csd_bits;
1106 		u32 bus_width = card->host->ios.bus_width;
1107 
1108 		/*
1109 		 * For devices supporting HS200 mode, the bus width has
1110 		 * to be set before executing the tuning function. If
1111 		 * set before tuning, then device will respond with CRC
1112 		 * errors for responses on CMD line. So for HS200 the
1113 		 * sequence will be
1114 		 * 1. set bus width 4bit / 8 bit (1 bit not supported)
1115 		 * 2. switch to HS200 mode
1116 		 * 3. set the clock to > 52Mhz <=200MHz and
1117 		 * 4. execute tuning for HS200
1118 		 */
1119 		if ((host->caps2 & MMC_CAP2_HS200) &&
1120 		    card->host->ops->execute_tuning) {
1121 			mmc_host_clk_hold(card->host);
1122 			err = card->host->ops->execute_tuning(card->host,
1123 				MMC_SEND_TUNING_BLOCK_HS200);
1124 			mmc_host_clk_release(card->host);
1125 		}
1126 		if (err) {
1127 			pr_warning("%s: tuning execution failed\n",
1128 				   mmc_hostname(card->host));
1129 			goto err;
1130 		}
1131 
1132 		ext_csd_bits = (bus_width == MMC_BUS_WIDTH_8) ?
1133 				EXT_CSD_BUS_WIDTH_8 : EXT_CSD_BUS_WIDTH_4;
1134 		err = mmc_select_powerclass(card, ext_csd_bits, ext_csd);
1135 		if (err)
1136 			pr_warning("%s: power class selection to bus width %d"
1137 				   " failed\n", mmc_hostname(card->host),
1138 				   1 << bus_width);
1139 	}
1140 
1141 	/*
1142 	 * Activate wide bus and DDR (if supported).
1143 	 */
1144 	if (!mmc_card_hs200(card) &&
1145 	    (card->csd.mmca_vsn >= CSD_SPEC_VER_4) &&
1146 	    (host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA))) {
1147 		static unsigned ext_csd_bits[][2] = {
1148 			{ EXT_CSD_BUS_WIDTH_8, EXT_CSD_DDR_BUS_WIDTH_8 },
1149 			{ EXT_CSD_BUS_WIDTH_4, EXT_CSD_DDR_BUS_WIDTH_4 },
1150 			{ EXT_CSD_BUS_WIDTH_1, EXT_CSD_BUS_WIDTH_1 },
1151 		};
1152 		static unsigned bus_widths[] = {
1153 			MMC_BUS_WIDTH_8,
1154 			MMC_BUS_WIDTH_4,
1155 			MMC_BUS_WIDTH_1
1156 		};
1157 		unsigned idx, bus_width = 0;
1158 
1159 		if (host->caps & MMC_CAP_8_BIT_DATA)
1160 			idx = 0;
1161 		else
1162 			idx = 1;
1163 		for (; idx < ARRAY_SIZE(bus_widths); idx++) {
1164 			bus_width = bus_widths[idx];
1165 			if (bus_width == MMC_BUS_WIDTH_1)
1166 				ddr = 0; /* no DDR for 1-bit width */
1167 			err = mmc_select_powerclass(card, ext_csd_bits[idx][0],
1168 						    ext_csd);
1169 			if (err)
1170 				pr_warning("%s: power class selection to "
1171 					   "bus width %d failed\n",
1172 					   mmc_hostname(card->host),
1173 					   1 << bus_width);
1174 
1175 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1176 					 EXT_CSD_BUS_WIDTH,
1177 					 ext_csd_bits[idx][0],
1178 					 card->ext_csd.generic_cmd6_time);
1179 			if (!err) {
1180 				mmc_set_bus_width(card->host, bus_width);
1181 
1182 				/*
1183 				 * If controller can't handle bus width test,
1184 				 * compare ext_csd previously read in 1 bit mode
1185 				 * against ext_csd at new bus width
1186 				 */
1187 				if (!(host->caps & MMC_CAP_BUS_WIDTH_TEST))
1188 					err = mmc_compare_ext_csds(card,
1189 						bus_width);
1190 				else
1191 					err = mmc_bus_test(card, bus_width);
1192 				if (!err)
1193 					break;
1194 			}
1195 		}
1196 
1197 		if (!err && ddr) {
1198 			err = mmc_select_powerclass(card, ext_csd_bits[idx][1],
1199 						    ext_csd);
1200 			if (err)
1201 				pr_warning("%s: power class selection to "
1202 					   "bus width %d ddr %d failed\n",
1203 					   mmc_hostname(card->host),
1204 					   1 << bus_width, ddr);
1205 
1206 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1207 					 EXT_CSD_BUS_WIDTH,
1208 					 ext_csd_bits[idx][1],
1209 					 card->ext_csd.generic_cmd6_time);
1210 		}
1211 		if (err) {
1212 			pr_warning("%s: switch to bus width %d ddr %d "
1213 				"failed\n", mmc_hostname(card->host),
1214 				1 << bus_width, ddr);
1215 			goto free_card;
1216 		} else if (ddr) {
1217 			/*
1218 			 * eMMC cards can support 3.3V to 1.2V i/o (vccq)
1219 			 * signaling.
1220 			 *
1221 			 * EXT_CSD_CARD_TYPE_DDR_1_8V means 3.3V or 1.8V vccq.
1222 			 *
1223 			 * 1.8V vccq at 3.3V core voltage (vcc) is not required
1224 			 * in the JEDEC spec for DDR.
1225 			 *
1226 			 * Do not force change in vccq since we are obviously
1227 			 * working and no change to vccq is needed.
1228 			 *
1229 			 * WARNING: eMMC rules are NOT the same as SD DDR
1230 			 */
1231 			if (ddr == MMC_1_2V_DDR_MODE) {
1232 				err = __mmc_set_signal_voltage(host,
1233 					MMC_SIGNAL_VOLTAGE_120);
1234 				if (err)
1235 					goto err;
1236 			}
1237 			mmc_card_set_ddr_mode(card);
1238 			mmc_set_timing(card->host, MMC_TIMING_UHS_DDR50);
1239 			mmc_set_bus_width(card->host, bus_width);
1240 		}
1241 	}
1242 
1243 	/*
1244 	 * Enable HPI feature (if supported)
1245 	 */
1246 	if (card->ext_csd.hpi) {
1247 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1248 				EXT_CSD_HPI_MGMT, 1,
1249 				card->ext_csd.generic_cmd6_time);
1250 		if (err && err != -EBADMSG)
1251 			goto free_card;
1252 		if (err) {
1253 			pr_warning("%s: Enabling HPI failed\n",
1254 				   mmc_hostname(card->host));
1255 			err = 0;
1256 		} else
1257 			card->ext_csd.hpi_en = 1;
1258 	}
1259 
1260 	/*
1261 	 * If cache size is higher than 0, this indicates
1262 	 * the existence of cache and it can be turned on.
1263 	 */
1264 	if ((host->caps2 & MMC_CAP2_CACHE_CTRL) &&
1265 			card->ext_csd.cache_size > 0) {
1266 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1267 				EXT_CSD_CACHE_CTRL, 1,
1268 				card->ext_csd.generic_cmd6_time);
1269 		if (err && err != -EBADMSG)
1270 			goto free_card;
1271 
1272 		/*
1273 		 * Only if no error, cache is turned on successfully.
1274 		 */
1275 		if (err) {
1276 			pr_warning("%s: Cache is supported, "
1277 					"but failed to turn on (%d)\n",
1278 					mmc_hostname(card->host), err);
1279 			card->ext_csd.cache_ctrl = 0;
1280 			err = 0;
1281 		} else {
1282 			card->ext_csd.cache_ctrl = 1;
1283 		}
1284 	}
1285 
1286 	/*
1287 	 * The mandatory minimum values are defined for packed command.
1288 	 * read: 5, write: 3
1289 	 */
1290 	if (card->ext_csd.max_packed_writes >= 3 &&
1291 	    card->ext_csd.max_packed_reads >= 5 &&
1292 	    host->caps2 & MMC_CAP2_PACKED_CMD) {
1293 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1294 				EXT_CSD_EXP_EVENTS_CTRL,
1295 				EXT_CSD_PACKED_EVENT_EN,
1296 				card->ext_csd.generic_cmd6_time);
1297 		if (err && err != -EBADMSG)
1298 			goto free_card;
1299 		if (err) {
1300 			pr_warn("%s: Enabling packed event failed\n",
1301 				mmc_hostname(card->host));
1302 			card->ext_csd.packed_event_en = 0;
1303 			err = 0;
1304 		} else {
1305 			card->ext_csd.packed_event_en = 1;
1306 		}
1307 	}
1308 
1309 	if (!oldcard)
1310 		host->card = card;
1311 
1312 	mmc_free_ext_csd(ext_csd);
1313 	return 0;
1314 
1315 free_card:
1316 	if (!oldcard)
1317 		mmc_remove_card(card);
1318 err:
1319 	mmc_free_ext_csd(ext_csd);
1320 
1321 	return err;
1322 }
1323 
1324 static int mmc_can_poweroff_notify(const struct mmc_card *card)
1325 {
1326 	return card &&
1327 		mmc_card_mmc(card) &&
1328 		(card->ext_csd.power_off_notification == EXT_CSD_POWER_ON);
1329 }
1330 
1331 static int mmc_poweroff_notify(struct mmc_card *card, unsigned int notify_type)
1332 {
1333 	unsigned int timeout = card->ext_csd.generic_cmd6_time;
1334 	int err;
1335 
1336 	/* Use EXT_CSD_POWER_OFF_SHORT as default notification type. */
1337 	if (notify_type == EXT_CSD_POWER_OFF_LONG)
1338 		timeout = card->ext_csd.power_off_longtime;
1339 
1340 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1341 			 EXT_CSD_POWER_OFF_NOTIFICATION,
1342 			 notify_type, timeout);
1343 	if (err)
1344 		pr_err("%s: Power Off Notification timed out, %u\n",
1345 		       mmc_hostname(card->host), timeout);
1346 
1347 	/* Disable the power off notification after the switch operation. */
1348 	card->ext_csd.power_off_notification = EXT_CSD_NO_POWER_NOTIFICATION;
1349 
1350 	return err;
1351 }
1352 
1353 /*
1354  * Host is being removed. Free up the current card.
1355  */
1356 static void mmc_remove(struct mmc_host *host)
1357 {
1358 	BUG_ON(!host);
1359 	BUG_ON(!host->card);
1360 
1361 	mmc_remove_card(host->card);
1362 	host->card = NULL;
1363 }
1364 
1365 /*
1366  * Card detection - card is alive.
1367  */
1368 static int mmc_alive(struct mmc_host *host)
1369 {
1370 	return mmc_send_status(host->card, NULL);
1371 }
1372 
1373 /*
1374  * Card detection callback from host.
1375  */
1376 static void mmc_detect(struct mmc_host *host)
1377 {
1378 	int err;
1379 
1380 	BUG_ON(!host);
1381 	BUG_ON(!host->card);
1382 
1383 	mmc_claim_host(host);
1384 
1385 	/*
1386 	 * Just check if our card has been removed.
1387 	 */
1388 	err = _mmc_detect_card_removed(host);
1389 
1390 	mmc_release_host(host);
1391 
1392 	if (err) {
1393 		mmc_remove(host);
1394 
1395 		mmc_claim_host(host);
1396 		mmc_detach_bus(host);
1397 		mmc_power_off(host);
1398 		mmc_release_host(host);
1399 	}
1400 }
1401 
1402 /*
1403  * Suspend callback from host.
1404  */
1405 static int mmc_suspend(struct mmc_host *host)
1406 {
1407 	int err = 0;
1408 
1409 	BUG_ON(!host);
1410 	BUG_ON(!host->card);
1411 
1412 	mmc_claim_host(host);
1413 
1414 	err = mmc_cache_ctrl(host, 0);
1415 	if (err)
1416 		goto out;
1417 
1418 	if (mmc_can_poweroff_notify(host->card))
1419 		err = mmc_poweroff_notify(host->card, EXT_CSD_POWER_OFF_SHORT);
1420 	else if (mmc_card_can_sleep(host))
1421 		err = mmc_card_sleep(host);
1422 	else if (!mmc_host_is_spi(host))
1423 		err = mmc_deselect_cards(host);
1424 	host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1425 
1426 out:
1427 	mmc_release_host(host);
1428 	return err;
1429 }
1430 
1431 /*
1432  * Resume callback from host.
1433  *
1434  * This function tries to determine if the same card is still present
1435  * and, if so, restore all state to it.
1436  */
1437 static int mmc_resume(struct mmc_host *host)
1438 {
1439 	int err;
1440 
1441 	BUG_ON(!host);
1442 	BUG_ON(!host->card);
1443 
1444 	mmc_claim_host(host);
1445 	err = mmc_init_card(host, host->ocr, host->card);
1446 	mmc_release_host(host);
1447 
1448 	return err;
1449 }
1450 
1451 static int mmc_power_restore(struct mmc_host *host)
1452 {
1453 	int ret;
1454 
1455 	host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_200);
1456 	mmc_claim_host(host);
1457 	ret = mmc_init_card(host, host->ocr, host->card);
1458 	mmc_release_host(host);
1459 
1460 	return ret;
1461 }
1462 
1463 static int mmc_sleep(struct mmc_host *host)
1464 {
1465 	struct mmc_card *card = host->card;
1466 	int err = -ENOSYS;
1467 
1468 	if (card && card->ext_csd.rev >= 3) {
1469 		err = mmc_card_sleepawake(host, 1);
1470 		if (err < 0)
1471 			pr_debug("%s: Error %d while putting card into sleep",
1472 				 mmc_hostname(host), err);
1473 	}
1474 
1475 	return err;
1476 }
1477 
1478 static int mmc_awake(struct mmc_host *host)
1479 {
1480 	struct mmc_card *card = host->card;
1481 	int err = -ENOSYS;
1482 
1483 	if (card && card->ext_csd.rev >= 3) {
1484 		err = mmc_card_sleepawake(host, 0);
1485 		if (err < 0)
1486 			pr_debug("%s: Error %d while awaking sleeping card",
1487 				 mmc_hostname(host), err);
1488 	}
1489 
1490 	return err;
1491 }
1492 
1493 static const struct mmc_bus_ops mmc_ops = {
1494 	.awake = mmc_awake,
1495 	.sleep = mmc_sleep,
1496 	.remove = mmc_remove,
1497 	.detect = mmc_detect,
1498 	.suspend = NULL,
1499 	.resume = NULL,
1500 	.power_restore = mmc_power_restore,
1501 	.alive = mmc_alive,
1502 };
1503 
1504 static const struct mmc_bus_ops mmc_ops_unsafe = {
1505 	.awake = mmc_awake,
1506 	.sleep = mmc_sleep,
1507 	.remove = mmc_remove,
1508 	.detect = mmc_detect,
1509 	.suspend = mmc_suspend,
1510 	.resume = mmc_resume,
1511 	.power_restore = mmc_power_restore,
1512 	.alive = mmc_alive,
1513 };
1514 
1515 static void mmc_attach_bus_ops(struct mmc_host *host)
1516 {
1517 	const struct mmc_bus_ops *bus_ops;
1518 
1519 	if (!mmc_card_is_removable(host))
1520 		bus_ops = &mmc_ops_unsafe;
1521 	else
1522 		bus_ops = &mmc_ops;
1523 	mmc_attach_bus(host, bus_ops);
1524 }
1525 
1526 /*
1527  * Starting point for MMC card init.
1528  */
1529 int mmc_attach_mmc(struct mmc_host *host)
1530 {
1531 	int err;
1532 	u32 ocr;
1533 
1534 	BUG_ON(!host);
1535 	WARN_ON(!host->claimed);
1536 
1537 	/* Set correct bus mode for MMC before attempting attach */
1538 	if (!mmc_host_is_spi(host))
1539 		mmc_set_bus_mode(host, MMC_BUSMODE_OPENDRAIN);
1540 
1541 	err = mmc_send_op_cond(host, 0, &ocr);
1542 	if (err)
1543 		return err;
1544 
1545 	mmc_attach_bus_ops(host);
1546 	if (host->ocr_avail_mmc)
1547 		host->ocr_avail = host->ocr_avail_mmc;
1548 
1549 	/*
1550 	 * We need to get OCR a different way for SPI.
1551 	 */
1552 	if (mmc_host_is_spi(host)) {
1553 		err = mmc_spi_read_ocr(host, 1, &ocr);
1554 		if (err)
1555 			goto err;
1556 	}
1557 
1558 	/*
1559 	 * Sanity check the voltages that the card claims to
1560 	 * support.
1561 	 */
1562 	if (ocr & 0x7F) {
1563 		pr_warning("%s: card claims to support voltages "
1564 		       "below the defined range. These will be ignored.\n",
1565 		       mmc_hostname(host));
1566 		ocr &= ~0x7F;
1567 	}
1568 
1569 	host->ocr = mmc_select_voltage(host, ocr);
1570 
1571 	/*
1572 	 * Can we support the voltage of the card?
1573 	 */
1574 	if (!host->ocr) {
1575 		err = -EINVAL;
1576 		goto err;
1577 	}
1578 
1579 	/*
1580 	 * Detect and init the card.
1581 	 */
1582 	err = mmc_init_card(host, host->ocr, NULL);
1583 	if (err)
1584 		goto err;
1585 
1586 	mmc_release_host(host);
1587 	err = mmc_add_card(host->card);
1588 	mmc_claim_host(host);
1589 	if (err)
1590 		goto remove_card;
1591 
1592 	return 0;
1593 
1594 remove_card:
1595 	mmc_release_host(host);
1596 	mmc_remove_card(host->card);
1597 	mmc_claim_host(host);
1598 	host->card = NULL;
1599 err:
1600 	mmc_detach_bus(host);
1601 
1602 	pr_err("%s: error %d whilst initialising MMC card\n",
1603 		mmc_hostname(host), err);
1604 
1605 	return err;
1606 }
1607