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