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