xref: /openbmc/linux/drivers/mmc/core/sd.c (revision 0e6774ec)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  linux/drivers/mmc/core/sd.c
4  *
5  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
6  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
7  *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
8  */
9 
10 #include <linux/err.h>
11 #include <linux/sizes.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/scatterlist.h>
17 #include <linux/sysfs.h>
18 
19 #include <linux/mmc/host.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/mmc.h>
22 #include <linux/mmc/sd.h>
23 
24 #include "core.h"
25 #include "card.h"
26 #include "host.h"
27 #include "bus.h"
28 #include "mmc_ops.h"
29 #include "quirks.h"
30 #include "sd.h"
31 #include "sd_ops.h"
32 
33 static const unsigned int tran_exp[] = {
34 	10000,		100000,		1000000,	10000000,
35 	0,		0,		0,		0
36 };
37 
38 static const unsigned char tran_mant[] = {
39 	0,	10,	12,	13,	15,	20,	25,	30,
40 	35,	40,	45,	50,	55,	60,	70,	80,
41 };
42 
43 static const unsigned int taac_exp[] = {
44 	1,	10,	100,	1000,	10000,	100000,	1000000, 10000000,
45 };
46 
47 static const unsigned int taac_mant[] = {
48 	0,	10,	12,	13,	15,	20,	25,	30,
49 	35,	40,	45,	50,	55,	60,	70,	80,
50 };
51 
52 static const unsigned int sd_au_size[] = {
53 	0,		SZ_16K / 512,		SZ_32K / 512,	SZ_64K / 512,
54 	SZ_128K / 512,	SZ_256K / 512,		SZ_512K / 512,	SZ_1M / 512,
55 	SZ_2M / 512,	SZ_4M / 512,		SZ_8M / 512,	(SZ_8M + SZ_4M) / 512,
56 	SZ_16M / 512,	(SZ_16M + SZ_8M) / 512,	SZ_32M / 512,	SZ_64M / 512,
57 };
58 
59 #define UNSTUFF_BITS(resp,start,size)					\
60 	({								\
61 		const int __size = size;				\
62 		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
63 		const int __off = 3 - ((start) / 32);			\
64 		const int __shft = (start) & 31;			\
65 		u32 __res;						\
66 									\
67 		__res = resp[__off] >> __shft;				\
68 		if (__size + __shft > 32)				\
69 			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
70 		__res & __mask;						\
71 	})
72 
73 #define SD_POWEROFF_NOTIFY_TIMEOUT_MS 1000
74 #define SD_WRITE_EXTR_SINGLE_TIMEOUT_MS 1000
75 
76 struct sd_busy_data {
77 	struct mmc_card *card;
78 	u8 *reg_buf;
79 };
80 
81 /*
82  * Given the decoded CSD structure, decode the raw CID to our CID structure.
83  */
84 void mmc_decode_cid(struct mmc_card *card)
85 {
86 	u32 *resp = card->raw_cid;
87 
88 	/*
89 	 * Add the raw card ID (cid) data to the entropy pool. It doesn't
90 	 * matter that not all of it is unique, it's just bonus entropy.
91 	 */
92 	add_device_randomness(&card->raw_cid, sizeof(card->raw_cid));
93 
94 	/*
95 	 * SD doesn't currently have a version field so we will
96 	 * have to assume we can parse this.
97 	 */
98 	card->cid.manfid		= UNSTUFF_BITS(resp, 120, 8);
99 	card->cid.oemid			= UNSTUFF_BITS(resp, 104, 16);
100 	card->cid.prod_name[0]		= UNSTUFF_BITS(resp, 96, 8);
101 	card->cid.prod_name[1]		= UNSTUFF_BITS(resp, 88, 8);
102 	card->cid.prod_name[2]		= UNSTUFF_BITS(resp, 80, 8);
103 	card->cid.prod_name[3]		= UNSTUFF_BITS(resp, 72, 8);
104 	card->cid.prod_name[4]		= UNSTUFF_BITS(resp, 64, 8);
105 	card->cid.hwrev			= UNSTUFF_BITS(resp, 60, 4);
106 	card->cid.fwrev			= UNSTUFF_BITS(resp, 56, 4);
107 	card->cid.serial		= UNSTUFF_BITS(resp, 24, 32);
108 	card->cid.year			= UNSTUFF_BITS(resp, 12, 8);
109 	card->cid.month			= UNSTUFF_BITS(resp, 8, 4);
110 
111 	card->cid.year += 2000; /* SD cards year offset */
112 }
113 
114 /*
115  * Given a 128-bit response, decode to our card CSD structure.
116  */
117 static int mmc_decode_csd(struct mmc_card *card)
118 {
119 	struct mmc_csd *csd = &card->csd;
120 	unsigned int e, m, csd_struct;
121 	u32 *resp = card->raw_csd;
122 
123 	csd_struct = UNSTUFF_BITS(resp, 126, 2);
124 
125 	switch (csd_struct) {
126 	case 0:
127 		m = UNSTUFF_BITS(resp, 115, 4);
128 		e = UNSTUFF_BITS(resp, 112, 3);
129 		csd->taac_ns	 = (taac_exp[e] * taac_mant[m] + 9) / 10;
130 		csd->taac_clks	 = UNSTUFF_BITS(resp, 104, 8) * 100;
131 
132 		m = UNSTUFF_BITS(resp, 99, 4);
133 		e = UNSTUFF_BITS(resp, 96, 3);
134 		csd->max_dtr	  = tran_exp[e] * tran_mant[m];
135 		csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
136 
137 		e = UNSTUFF_BITS(resp, 47, 3);
138 		m = UNSTUFF_BITS(resp, 62, 12);
139 		csd->capacity	  = (1 + m) << (e + 2);
140 
141 		csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
142 		csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
143 		csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
144 		csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
145 		csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
146 		csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
147 		csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
148 		csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
149 
150 		if (UNSTUFF_BITS(resp, 46, 1)) {
151 			csd->erase_size = 1;
152 		} else if (csd->write_blkbits >= 9) {
153 			csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
154 			csd->erase_size <<= csd->write_blkbits - 9;
155 		}
156 
157 		if (UNSTUFF_BITS(resp, 13, 1))
158 			mmc_card_set_readonly(card);
159 		break;
160 	case 1:
161 		/*
162 		 * This is a block-addressed SDHC or SDXC card. Most
163 		 * interesting fields are unused and have fixed
164 		 * values. To avoid getting tripped by buggy cards,
165 		 * we assume those fixed values ourselves.
166 		 */
167 		mmc_card_set_blockaddr(card);
168 
169 		csd->taac_ns	 = 0; /* Unused */
170 		csd->taac_clks	 = 0; /* Unused */
171 
172 		m = UNSTUFF_BITS(resp, 99, 4);
173 		e = UNSTUFF_BITS(resp, 96, 3);
174 		csd->max_dtr	  = tran_exp[e] * tran_mant[m];
175 		csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
176 		csd->c_size	  = UNSTUFF_BITS(resp, 48, 22);
177 
178 		/* SDXC cards have a minimum C_SIZE of 0x00FFFF */
179 		if (csd->c_size >= 0xFFFF)
180 			mmc_card_set_ext_capacity(card);
181 
182 		m = UNSTUFF_BITS(resp, 48, 22);
183 		csd->capacity     = (1 + m) << 10;
184 
185 		csd->read_blkbits = 9;
186 		csd->read_partial = 0;
187 		csd->write_misalign = 0;
188 		csd->read_misalign = 0;
189 		csd->r2w_factor = 4; /* Unused */
190 		csd->write_blkbits = 9;
191 		csd->write_partial = 0;
192 		csd->erase_size = 1;
193 
194 		if (UNSTUFF_BITS(resp, 13, 1))
195 			mmc_card_set_readonly(card);
196 		break;
197 	default:
198 		pr_err("%s: unrecognised CSD structure version %d\n",
199 			mmc_hostname(card->host), csd_struct);
200 		return -EINVAL;
201 	}
202 
203 	card->erase_size = csd->erase_size;
204 
205 	return 0;
206 }
207 
208 /*
209  * Given a 64-bit response, decode to our card SCR structure.
210  */
211 static int mmc_decode_scr(struct mmc_card *card)
212 {
213 	struct sd_scr *scr = &card->scr;
214 	unsigned int scr_struct;
215 	u32 resp[4];
216 
217 	resp[3] = card->raw_scr[1];
218 	resp[2] = card->raw_scr[0];
219 
220 	scr_struct = UNSTUFF_BITS(resp, 60, 4);
221 	if (scr_struct != 0) {
222 		pr_err("%s: unrecognised SCR structure version %d\n",
223 			mmc_hostname(card->host), scr_struct);
224 		return -EINVAL;
225 	}
226 
227 	scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
228 	scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
229 	if (scr->sda_vsn == SCR_SPEC_VER_2)
230 		/* Check if Physical Layer Spec v3.0 is supported */
231 		scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
232 
233 	if (scr->sda_spec3) {
234 		scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
235 		scr->sda_specx = UNSTUFF_BITS(resp, 38, 4);
236 	}
237 
238 	if (UNSTUFF_BITS(resp, 55, 1))
239 		card->erased_byte = 0xFF;
240 	else
241 		card->erased_byte = 0x0;
242 
243 	if (scr->sda_spec4)
244 		scr->cmds = UNSTUFF_BITS(resp, 32, 4);
245 	else if (scr->sda_spec3)
246 		scr->cmds = UNSTUFF_BITS(resp, 32, 2);
247 
248 	/* SD Spec says: any SD Card shall set at least bits 0 and 2 */
249 	if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) ||
250 	    !(scr->bus_widths & SD_SCR_BUS_WIDTH_4)) {
251 		pr_err("%s: invalid bus width\n", mmc_hostname(card->host));
252 		return -EINVAL;
253 	}
254 
255 	return 0;
256 }
257 
258 /*
259  * Fetch and process SD Status register.
260  */
261 static int mmc_read_ssr(struct mmc_card *card)
262 {
263 	unsigned int au, es, et, eo;
264 	__be32 *raw_ssr;
265 	u32 resp[4] = {};
266 	u8 discard_support;
267 	int i;
268 
269 	if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
270 		pr_warn("%s: card lacks mandatory SD Status function\n",
271 			mmc_hostname(card->host));
272 		return 0;
273 	}
274 
275 	raw_ssr = kmalloc(sizeof(card->raw_ssr), GFP_KERNEL);
276 	if (!raw_ssr)
277 		return -ENOMEM;
278 
279 	if (mmc_app_sd_status(card, raw_ssr)) {
280 		pr_warn("%s: problem reading SD Status register\n",
281 			mmc_hostname(card->host));
282 		kfree(raw_ssr);
283 		return 0;
284 	}
285 
286 	for (i = 0; i < 16; i++)
287 		card->raw_ssr[i] = be32_to_cpu(raw_ssr[i]);
288 
289 	kfree(raw_ssr);
290 
291 	/*
292 	 * UNSTUFF_BITS only works with four u32s so we have to offset the
293 	 * bitfield positions accordingly.
294 	 */
295 	au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4);
296 	if (au) {
297 		if (au <= 9 || card->scr.sda_spec3) {
298 			card->ssr.au = sd_au_size[au];
299 			es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16);
300 			et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6);
301 			if (es && et) {
302 				eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2);
303 				card->ssr.erase_timeout = (et * 1000) / es;
304 				card->ssr.erase_offset = eo * 1000;
305 			}
306 		} else {
307 			pr_warn("%s: SD Status: Invalid Allocation Unit size\n",
308 				mmc_hostname(card->host));
309 		}
310 	}
311 
312 	/*
313 	 * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
314 	 */
315 	resp[3] = card->raw_ssr[6];
316 	discard_support = UNSTUFF_BITS(resp, 313 - 288, 1);
317 	card->erase_arg = (card->scr.sda_specx && discard_support) ?
318 			    SD_DISCARD_ARG : SD_ERASE_ARG;
319 
320 	return 0;
321 }
322 
323 /*
324  * Fetches and decodes switch information
325  */
326 static int mmc_read_switch(struct mmc_card *card)
327 {
328 	int err;
329 	u8 *status;
330 
331 	if (card->scr.sda_vsn < SCR_SPEC_VER_1)
332 		return 0;
333 
334 	if (!(card->csd.cmdclass & CCC_SWITCH)) {
335 		pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
336 			mmc_hostname(card->host));
337 		return 0;
338 	}
339 
340 	status = kmalloc(64, GFP_KERNEL);
341 	if (!status)
342 		return -ENOMEM;
343 
344 	/*
345 	 * Find out the card's support bits with a mode 0 operation.
346 	 * The argument does not matter, as the support bits do not
347 	 * change with the arguments.
348 	 */
349 	err = mmc_sd_switch(card, 0, 0, 0, status);
350 	if (err) {
351 		/*
352 		 * If the host or the card can't do the switch,
353 		 * fail more gracefully.
354 		 */
355 		if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
356 			goto out;
357 
358 		pr_warn("%s: problem reading Bus Speed modes\n",
359 			mmc_hostname(card->host));
360 		err = 0;
361 
362 		goto out;
363 	}
364 
365 	if (status[13] & SD_MODE_HIGH_SPEED)
366 		card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
367 
368 	if (card->scr.sda_spec3) {
369 		card->sw_caps.sd3_bus_mode = status[13];
370 		/* Driver Strengths supported by the card */
371 		card->sw_caps.sd3_drv_type = status[9];
372 		card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
373 	}
374 
375 out:
376 	kfree(status);
377 
378 	return err;
379 }
380 
381 /*
382  * Test if the card supports high-speed mode and, if so, switch to it.
383  */
384 int mmc_sd_switch_hs(struct mmc_card *card)
385 {
386 	int err;
387 	u8 *status;
388 
389 	if (card->scr.sda_vsn < SCR_SPEC_VER_1)
390 		return 0;
391 
392 	if (!(card->csd.cmdclass & CCC_SWITCH))
393 		return 0;
394 
395 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
396 		return 0;
397 
398 	if (card->sw_caps.hs_max_dtr == 0)
399 		return 0;
400 
401 	status = kmalloc(64, GFP_KERNEL);
402 	if (!status)
403 		return -ENOMEM;
404 
405 	err = mmc_sd_switch(card, 1, 0, HIGH_SPEED_BUS_SPEED, status);
406 	if (err)
407 		goto out;
408 
409 	if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) {
410 		pr_warn("%s: Problem switching card into high-speed mode!\n",
411 			mmc_hostname(card->host));
412 		err = 0;
413 	} else {
414 		err = 1;
415 	}
416 
417 out:
418 	kfree(status);
419 
420 	return err;
421 }
422 
423 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
424 {
425 	int card_drv_type, drive_strength, drv_type;
426 	int err;
427 
428 	card->drive_strength = 0;
429 
430 	card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
431 
432 	drive_strength = mmc_select_drive_strength(card,
433 						   card->sw_caps.uhs_max_dtr,
434 						   card_drv_type, &drv_type);
435 
436 	if (drive_strength) {
437 		err = mmc_sd_switch(card, 1, 2, drive_strength, status);
438 		if (err)
439 			return err;
440 		if ((status[15] & 0xF) != drive_strength) {
441 			pr_warn("%s: Problem setting drive strength!\n",
442 				mmc_hostname(card->host));
443 			return 0;
444 		}
445 		card->drive_strength = drive_strength;
446 	}
447 
448 	if (drv_type)
449 		mmc_set_driver_type(card->host, drv_type);
450 
451 	return 0;
452 }
453 
454 static void sd_update_bus_speed_mode(struct mmc_card *card)
455 {
456 	/*
457 	 * If the host doesn't support any of the UHS-I modes, fallback on
458 	 * default speed.
459 	 */
460 	if (!mmc_host_uhs(card->host)) {
461 		card->sd_bus_speed = 0;
462 		return;
463 	}
464 
465 	if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
466 	    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
467 			card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
468 	} else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
469 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
470 			card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
471 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
472 		    MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
473 		    SD_MODE_UHS_SDR50)) {
474 			card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
475 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
476 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
477 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
478 			card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
479 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
480 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
481 		    MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
482 		    SD_MODE_UHS_SDR12)) {
483 			card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
484 	}
485 }
486 
487 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
488 {
489 	int err;
490 	unsigned int timing = 0;
491 
492 	switch (card->sd_bus_speed) {
493 	case UHS_SDR104_BUS_SPEED:
494 		timing = MMC_TIMING_UHS_SDR104;
495 		card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
496 		break;
497 	case UHS_DDR50_BUS_SPEED:
498 		timing = MMC_TIMING_UHS_DDR50;
499 		card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
500 		break;
501 	case UHS_SDR50_BUS_SPEED:
502 		timing = MMC_TIMING_UHS_SDR50;
503 		card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
504 		break;
505 	case UHS_SDR25_BUS_SPEED:
506 		timing = MMC_TIMING_UHS_SDR25;
507 		card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
508 		break;
509 	case UHS_SDR12_BUS_SPEED:
510 		timing = MMC_TIMING_UHS_SDR12;
511 		card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
512 		break;
513 	default:
514 		return 0;
515 	}
516 
517 	err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
518 	if (err)
519 		return err;
520 
521 	if ((status[16] & 0xF) != card->sd_bus_speed)
522 		pr_warn("%s: Problem setting bus speed mode!\n",
523 			mmc_hostname(card->host));
524 	else {
525 		mmc_set_timing(card->host, timing);
526 		mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
527 	}
528 
529 	return 0;
530 }
531 
532 /* Get host's max current setting at its current voltage */
533 static u32 sd_get_host_max_current(struct mmc_host *host)
534 {
535 	u32 voltage, max_current;
536 
537 	voltage = 1 << host->ios.vdd;
538 	switch (voltage) {
539 	case MMC_VDD_165_195:
540 		max_current = host->max_current_180;
541 		break;
542 	case MMC_VDD_29_30:
543 	case MMC_VDD_30_31:
544 		max_current = host->max_current_300;
545 		break;
546 	case MMC_VDD_32_33:
547 	case MMC_VDD_33_34:
548 		max_current = host->max_current_330;
549 		break;
550 	default:
551 		max_current = 0;
552 	}
553 
554 	return max_current;
555 }
556 
557 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
558 {
559 	int current_limit = SD_SET_CURRENT_NO_CHANGE;
560 	int err;
561 	u32 max_current;
562 
563 	/*
564 	 * Current limit switch is only defined for SDR50, SDR104, and DDR50
565 	 * bus speed modes. For other bus speed modes, we do not change the
566 	 * current limit.
567 	 */
568 	if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
569 	    (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
570 	    (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
571 		return 0;
572 
573 	/*
574 	 * Host has different current capabilities when operating at
575 	 * different voltages, so find out its max current first.
576 	 */
577 	max_current = sd_get_host_max_current(card->host);
578 
579 	/*
580 	 * We only check host's capability here, if we set a limit that is
581 	 * higher than the card's maximum current, the card will be using its
582 	 * maximum current, e.g. if the card's maximum current is 300ma, and
583 	 * when we set current limit to 200ma, the card will draw 200ma, and
584 	 * when we set current limit to 400/600/800ma, the card will draw its
585 	 * maximum 300ma from the host.
586 	 *
587 	 * The above is incorrect: if we try to set a current limit that is
588 	 * not supported by the card, the card can rightfully error out the
589 	 * attempt, and remain at the default current limit.  This results
590 	 * in a 300mA card being limited to 200mA even though the host
591 	 * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
592 	 * an iMX6 host. --rmk
593 	 */
594 	if (max_current >= 800 &&
595 	    card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
596 		current_limit = SD_SET_CURRENT_LIMIT_800;
597 	else if (max_current >= 600 &&
598 		 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
599 		current_limit = SD_SET_CURRENT_LIMIT_600;
600 	else if (max_current >= 400 &&
601 		 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
602 		current_limit = SD_SET_CURRENT_LIMIT_400;
603 	else if (max_current >= 200 &&
604 		 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
605 		current_limit = SD_SET_CURRENT_LIMIT_200;
606 
607 	if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
608 		err = mmc_sd_switch(card, 1, 3, current_limit, status);
609 		if (err)
610 			return err;
611 
612 		if (((status[15] >> 4) & 0x0F) != current_limit)
613 			pr_warn("%s: Problem setting current limit!\n",
614 				mmc_hostname(card->host));
615 
616 	}
617 
618 	return 0;
619 }
620 
621 /*
622  * UHS-I specific initialization procedure
623  */
624 static int mmc_sd_init_uhs_card(struct mmc_card *card)
625 {
626 	int err;
627 	u8 *status;
628 
629 	if (!(card->csd.cmdclass & CCC_SWITCH))
630 		return 0;
631 
632 	status = kmalloc(64, GFP_KERNEL);
633 	if (!status)
634 		return -ENOMEM;
635 
636 	/* Set 4-bit bus width */
637 	err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
638 	if (err)
639 		goto out;
640 
641 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
642 
643 	/*
644 	 * Select the bus speed mode depending on host
645 	 * and card capability.
646 	 */
647 	sd_update_bus_speed_mode(card);
648 
649 	/* Set the driver strength for the card */
650 	err = sd_select_driver_type(card, status);
651 	if (err)
652 		goto out;
653 
654 	/* Set current limit for the card */
655 	err = sd_set_current_limit(card, status);
656 	if (err)
657 		goto out;
658 
659 	/* Set bus speed mode of the card */
660 	err = sd_set_bus_speed_mode(card, status);
661 	if (err)
662 		goto out;
663 
664 	/*
665 	 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
666 	 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
667 	 */
668 	if (!mmc_host_is_spi(card->host) &&
669 		(card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
670 		 card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
671 		 card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
672 		err = mmc_execute_tuning(card);
673 
674 		/*
675 		 * As SD Specifications Part1 Physical Layer Specification
676 		 * Version 3.01 says, CMD19 tuning is available for unlocked
677 		 * cards in transfer state of 1.8V signaling mode. The small
678 		 * difference between v3.00 and 3.01 spec means that CMD19
679 		 * tuning is also available for DDR50 mode.
680 		 */
681 		if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
682 			pr_warn("%s: ddr50 tuning failed\n",
683 				mmc_hostname(card->host));
684 			err = 0;
685 		}
686 	}
687 
688 out:
689 	kfree(status);
690 
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(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
699 MMC_DEV_ATTR(ssr,
700 	"%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
701 		card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
702 		card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
703 		card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
704 		card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
705 		card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
706 		card->raw_ssr[15]);
707 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
708 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
709 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
710 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
711 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
712 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
713 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
714 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
715 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
716 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
717 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
718 
719 
720 static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
721 			    char *buf)
722 {
723 	struct mmc_card *card = mmc_dev_to_card(dev);
724 	struct mmc_host *host = card->host;
725 
726 	if (card->csd.dsr_imp && host->dsr_req)
727 		return sysfs_emit(buf, "0x%x\n", host->dsr);
728 	/* return default DSR value */
729 	return sysfs_emit(buf, "0x%x\n", 0x404);
730 }
731 
732 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
733 
734 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
735 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
736 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
737 
738 #define sdio_info_attr(num)									\
739 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf)	\
740 {												\
741 	struct mmc_card *card = mmc_dev_to_card(dev);						\
742 												\
743 	if (num > card->num_info)								\
744 		return -ENODATA;								\
745 	if (!card->info[num - 1][0])								\
746 		return 0;									\
747 	return sysfs_emit(buf, "%s\n", card->info[num - 1]);					\
748 }												\
749 static DEVICE_ATTR_RO(info##num)
750 
751 sdio_info_attr(1);
752 sdio_info_attr(2);
753 sdio_info_attr(3);
754 sdio_info_attr(4);
755 
756 static struct attribute *sd_std_attrs[] = {
757 	&dev_attr_vendor.attr,
758 	&dev_attr_device.attr,
759 	&dev_attr_revision.attr,
760 	&dev_attr_info1.attr,
761 	&dev_attr_info2.attr,
762 	&dev_attr_info3.attr,
763 	&dev_attr_info4.attr,
764 	&dev_attr_cid.attr,
765 	&dev_attr_csd.attr,
766 	&dev_attr_scr.attr,
767 	&dev_attr_ssr.attr,
768 	&dev_attr_date.attr,
769 	&dev_attr_erase_size.attr,
770 	&dev_attr_preferred_erase_size.attr,
771 	&dev_attr_fwrev.attr,
772 	&dev_attr_hwrev.attr,
773 	&dev_attr_manfid.attr,
774 	&dev_attr_name.attr,
775 	&dev_attr_oemid.attr,
776 	&dev_attr_serial.attr,
777 	&dev_attr_ocr.attr,
778 	&dev_attr_rca.attr,
779 	&dev_attr_dsr.attr,
780 	NULL,
781 };
782 
783 static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
784 				 int index)
785 {
786 	struct device *dev = kobj_to_dev(kobj);
787 	struct mmc_card *card = mmc_dev_to_card(dev);
788 
789 	/* CIS vendor and device ids, revision and info string are available only for Combo cards */
790 	if ((attr == &dev_attr_vendor.attr ||
791 	     attr == &dev_attr_device.attr ||
792 	     attr == &dev_attr_revision.attr ||
793 	     attr == &dev_attr_info1.attr ||
794 	     attr == &dev_attr_info2.attr ||
795 	     attr == &dev_attr_info3.attr ||
796 	     attr == &dev_attr_info4.attr
797 	    ) &&!mmc_card_sd_combo(card))
798 		return 0;
799 
800 	return attr->mode;
801 }
802 
803 static const struct attribute_group sd_std_group = {
804 	.attrs = sd_std_attrs,
805 	.is_visible = sd_std_is_visible,
806 };
807 __ATTRIBUTE_GROUPS(sd_std);
808 
809 struct device_type sd_type = {
810 	.groups = sd_std_groups,
811 };
812 
813 /*
814  * Fetch CID from card.
815  */
816 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
817 {
818 	int err;
819 	u32 max_current;
820 	int retries = 10;
821 	u32 pocr = ocr;
822 
823 try_again:
824 	if (!retries) {
825 		ocr &= ~SD_OCR_S18R;
826 		pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
827 	}
828 
829 	/*
830 	 * Since we're changing the OCR value, we seem to
831 	 * need to tell some cards to go back to the idle
832 	 * state.  We wait 1ms to give cards time to
833 	 * respond.
834 	 */
835 	mmc_go_idle(host);
836 
837 	/*
838 	 * If SD_SEND_IF_COND indicates an SD 2.0
839 	 * compliant card and we should set bit 30
840 	 * of the ocr to indicate that we can handle
841 	 * block-addressed SDHC cards.
842 	 */
843 	err = mmc_send_if_cond(host, ocr);
844 	if (!err)
845 		ocr |= SD_OCR_CCS;
846 
847 	/*
848 	 * If the host supports one of UHS-I modes, request the card
849 	 * to switch to 1.8V signaling level. If the card has failed
850 	 * repeatedly to switch however, skip this.
851 	 */
852 	if (retries && mmc_host_uhs(host))
853 		ocr |= SD_OCR_S18R;
854 
855 	/*
856 	 * If the host can supply more than 150mA at current voltage,
857 	 * XPC should be set to 1.
858 	 */
859 	max_current = sd_get_host_max_current(host);
860 	if (max_current > 150)
861 		ocr |= SD_OCR_XPC;
862 
863 	err = mmc_send_app_op_cond(host, ocr, rocr);
864 	if (err)
865 		return err;
866 
867 	/*
868 	 * In case the S18A bit is set in the response, let's start the signal
869 	 * voltage switch procedure. SPI mode doesn't support CMD11.
870 	 * Note that, according to the spec, the S18A bit is not valid unless
871 	 * the CCS bit is set as well. We deliberately deviate from the spec in
872 	 * regards to this, which allows UHS-I to be supported for SDSC cards.
873 	 */
874 	if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
875 	    rocr && (*rocr & SD_ROCR_S18A)) {
876 		err = mmc_set_uhs_voltage(host, pocr);
877 		if (err == -EAGAIN) {
878 			retries--;
879 			goto try_again;
880 		} else if (err) {
881 			retries = 0;
882 			goto try_again;
883 		}
884 	}
885 
886 	err = mmc_send_cid(host, cid);
887 	return err;
888 }
889 
890 int mmc_sd_get_csd(struct mmc_card *card)
891 {
892 	int err;
893 
894 	/*
895 	 * Fetch CSD from card.
896 	 */
897 	err = mmc_send_csd(card, card->raw_csd);
898 	if (err)
899 		return err;
900 
901 	err = mmc_decode_csd(card);
902 	if (err)
903 		return err;
904 
905 	return 0;
906 }
907 
908 static int mmc_sd_get_ro(struct mmc_host *host)
909 {
910 	int ro;
911 
912 	/*
913 	 * Some systems don't feature a write-protect pin and don't need one.
914 	 * E.g. because they only have micro-SD card slot. For those systems
915 	 * assume that the SD card is always read-write.
916 	 */
917 	if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
918 		return 0;
919 
920 	if (!host->ops->get_ro)
921 		return -1;
922 
923 	ro = host->ops->get_ro(host);
924 
925 	return ro;
926 }
927 
928 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
929 	bool reinit)
930 {
931 	int err;
932 
933 	if (!reinit) {
934 		/*
935 		 * Fetch SCR from card.
936 		 */
937 		err = mmc_app_send_scr(card);
938 		if (err)
939 			return err;
940 
941 		err = mmc_decode_scr(card);
942 		if (err)
943 			return err;
944 
945 		/*
946 		 * Fetch and process SD Status register.
947 		 */
948 		err = mmc_read_ssr(card);
949 		if (err)
950 			return err;
951 
952 		/* Erase init depends on CSD and SSR */
953 		mmc_init_erase(card);
954 	}
955 
956 	/*
957 	 * Fetch switch information from card. Note, sd3_bus_mode can change if
958 	 * voltage switch outcome changes, so do this always.
959 	 */
960 	err = mmc_read_switch(card);
961 	if (err)
962 		return err;
963 
964 	/*
965 	 * For SPI, enable CRC as appropriate.
966 	 * This CRC enable is located AFTER the reading of the
967 	 * card registers because some SDHC cards are not able
968 	 * to provide valid CRCs for non-512-byte blocks.
969 	 */
970 	if (mmc_host_is_spi(host)) {
971 		err = mmc_spi_set_crc(host, use_spi_crc);
972 		if (err)
973 			return err;
974 	}
975 
976 	/*
977 	 * Check if read-only switch is active.
978 	 */
979 	if (!reinit) {
980 		int ro = mmc_sd_get_ro(host);
981 
982 		if (ro < 0) {
983 			pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
984 				mmc_hostname(host));
985 		} else if (ro > 0) {
986 			mmc_card_set_readonly(card);
987 		}
988 	}
989 
990 	return 0;
991 }
992 
993 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
994 {
995 	unsigned max_dtr = (unsigned int)-1;
996 
997 	if (mmc_card_hs(card)) {
998 		if (max_dtr > card->sw_caps.hs_max_dtr)
999 			max_dtr = card->sw_caps.hs_max_dtr;
1000 	} else if (max_dtr > card->csd.max_dtr) {
1001 		max_dtr = card->csd.max_dtr;
1002 	}
1003 
1004 	return max_dtr;
1005 }
1006 
1007 static bool mmc_sd_card_using_v18(struct mmc_card *card)
1008 {
1009 	/*
1010 	 * According to the SD spec., the Bus Speed Mode (function group 1) bits
1011 	 * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus
1012 	 * they can be used to determine if the card has already switched to
1013 	 * 1.8V signaling.
1014 	 */
1015 	return card->sw_caps.sd3_bus_mode &
1016 	       (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
1017 }
1018 
1019 static int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
1020 			    u8 reg_data)
1021 {
1022 	struct mmc_host *host = card->host;
1023 	struct mmc_request mrq = {};
1024 	struct mmc_command cmd = {};
1025 	struct mmc_data data = {};
1026 	struct scatterlist sg;
1027 	u8 *reg_buf;
1028 
1029 	reg_buf = kzalloc(512, GFP_KERNEL);
1030 	if (!reg_buf)
1031 		return -ENOMEM;
1032 
1033 	mrq.cmd = &cmd;
1034 	mrq.data = &data;
1035 
1036 	/*
1037 	 * Arguments of CMD49:
1038 	 * [31:31] MIO (0 = memory).
1039 	 * [30:27] FNO (function number).
1040 	 * [26:26] MW - mask write mode (0 = disable).
1041 	 * [25:18] page number.
1042 	 * [17:9] offset address.
1043 	 * [8:0] length (0 = 1 byte).
1044 	 */
1045 	cmd.arg = fno << 27 | page << 18 | offset << 9;
1046 
1047 	/* The first byte in the buffer is the data to be written. */
1048 	reg_buf[0] = reg_data;
1049 
1050 	data.flags = MMC_DATA_WRITE;
1051 	data.blksz = 512;
1052 	data.blocks = 1;
1053 	data.sg = &sg;
1054 	data.sg_len = 1;
1055 	sg_init_one(&sg, reg_buf, 512);
1056 
1057 	cmd.opcode = SD_WRITE_EXTR_SINGLE;
1058 	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1059 
1060 	mmc_set_data_timeout(&data, card);
1061 	mmc_wait_for_req(host, &mrq);
1062 
1063 	kfree(reg_buf);
1064 
1065 	/*
1066 	 * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
1067 	 * after the CMD49. Although, let's leave this to be managed by the
1068 	 * caller.
1069 	 */
1070 
1071 	if (cmd.error)
1072 		return cmd.error;
1073 	if (data.error)
1074 		return data.error;
1075 
1076 	return 0;
1077 }
1078 
1079 static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
1080 			   u16 offset, u16 len, u8 *reg_buf)
1081 {
1082 	u32 cmd_args;
1083 
1084 	/*
1085 	 * Command arguments of CMD48:
1086 	 * [31:31] MIO (0 = memory).
1087 	 * [30:27] FNO (function number).
1088 	 * [26:26] reserved (0).
1089 	 * [25:18] page number.
1090 	 * [17:9] offset address.
1091 	 * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
1092 	 */
1093 	cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);
1094 
1095 	return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
1096 				  cmd_args, reg_buf, 512);
1097 }
1098 
1099 static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
1100 				  u16 offset)
1101 {
1102 	int err;
1103 	u8 *reg_buf;
1104 
1105 	reg_buf = kzalloc(512, GFP_KERNEL);
1106 	if (!reg_buf)
1107 		return -ENOMEM;
1108 
1109 	/* Read the extension register for power management function. */
1110 	err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1111 	if (err) {
1112 		pr_warn("%s: error %d reading PM func of ext reg\n",
1113 			mmc_hostname(card->host), err);
1114 		goto out;
1115 	}
1116 
1117 	/* PM revision consists of 4 bits. */
1118 	card->ext_power.rev = reg_buf[0] & 0xf;
1119 
1120 	/* Power Off Notification support at bit 4. */
1121 	if (reg_buf[1] & BIT(4))
1122 		card->ext_power.feature_support |= SD_EXT_POWER_OFF_NOTIFY;
1123 
1124 	/* Power Sustenance support at bit 5. */
1125 	if (reg_buf[1] & BIT(5))
1126 		card->ext_power.feature_support |= SD_EXT_POWER_SUSTENANCE;
1127 
1128 	/* Power Down Mode support at bit 6. */
1129 	if (reg_buf[1] & BIT(6))
1130 		card->ext_power.feature_support |= SD_EXT_POWER_DOWN_MODE;
1131 
1132 	card->ext_power.fno = fno;
1133 	card->ext_power.page = page;
1134 	card->ext_power.offset = offset;
1135 
1136 out:
1137 	kfree(reg_buf);
1138 	return err;
1139 }
1140 
1141 static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,
1142 				 u16 offset)
1143 {
1144 	int err;
1145 	u8 *reg_buf;
1146 
1147 	reg_buf = kzalloc(512, GFP_KERNEL);
1148 	if (!reg_buf)
1149 		return -ENOMEM;
1150 
1151 	err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1152 	if (err) {
1153 		pr_warn("%s: error %d reading PERF func of ext reg\n",
1154 			mmc_hostname(card->host), err);
1155 		goto out;
1156 	}
1157 
1158 	/* PERF revision. */
1159 	card->ext_perf.rev = reg_buf[0];
1160 
1161 	/* FX_EVENT support at bit 0. */
1162 	if (reg_buf[1] & BIT(0))
1163 		card->ext_perf.feature_support |= SD_EXT_PERF_FX_EVENT;
1164 
1165 	/* Card initiated self-maintenance support at bit 0. */
1166 	if (reg_buf[2] & BIT(0))
1167 		card->ext_perf.feature_support |= SD_EXT_PERF_CARD_MAINT;
1168 
1169 	/* Host initiated self-maintenance support at bit 1. */
1170 	if (reg_buf[2] & BIT(1))
1171 		card->ext_perf.feature_support |= SD_EXT_PERF_HOST_MAINT;
1172 
1173 	/* Cache support at bit 0. */
1174 	if ((reg_buf[4] & BIT(0)) && !mmc_card_broken_sd_cache(card))
1175 		card->ext_perf.feature_support |= SD_EXT_PERF_CACHE;
1176 
1177 	/* Command queue support indicated via queue depth bits (0 to 4). */
1178 	if (reg_buf[6] & 0x1f)
1179 		card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE;
1180 
1181 	card->ext_perf.fno = fno;
1182 	card->ext_perf.page = page;
1183 	card->ext_perf.offset = offset;
1184 
1185 out:
1186 	kfree(reg_buf);
1187 	return err;
1188 }
1189 
1190 static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf,
1191 			    u16 *next_ext_addr)
1192 {
1193 	u8 num_regs, fno, page;
1194 	u16 sfc, offset, ext = *next_ext_addr;
1195 	u32 reg_addr;
1196 
1197 	/*
1198 	 * Parse only one register set per extension, as that is sufficient to
1199 	 * support the standard functions. This means another 48 bytes in the
1200 	 * buffer must be available.
1201 	 */
1202 	if (ext + 48 > 512)
1203 		return -EFAULT;
1204 
1205 	/* Standard Function Code */
1206 	memcpy(&sfc, &gen_info_buf[ext], 2);
1207 
1208 	/* Address to the next extension. */
1209 	memcpy(next_ext_addr, &gen_info_buf[ext + 40], 2);
1210 
1211 	/* Number of registers for this extension. */
1212 	num_regs = gen_info_buf[ext + 42];
1213 
1214 	/* We support only one register per extension. */
1215 	if (num_regs != 1)
1216 		return 0;
1217 
1218 	/* Extension register address. */
1219 	memcpy(&reg_addr, &gen_info_buf[ext + 44], 4);
1220 
1221 	/* 9 bits (0 to 8) contains the offset address. */
1222 	offset = reg_addr & 0x1ff;
1223 
1224 	/* 8 bits (9 to 16) contains the page number. */
1225 	page = reg_addr >> 9 & 0xff ;
1226 
1227 	/* 4 bits (18 to 21) contains the function number. */
1228 	fno = reg_addr >> 18 & 0xf;
1229 
1230 	/* Standard Function Code for power management. */
1231 	if (sfc == 0x1)
1232 		return sd_parse_ext_reg_power(card, fno, page, offset);
1233 
1234 	/* Standard Function Code for performance enhancement. */
1235 	if (sfc == 0x2)
1236 		return sd_parse_ext_reg_perf(card, fno, page, offset);
1237 
1238 	return 0;
1239 }
1240 
1241 static int sd_read_ext_regs(struct mmc_card *card)
1242 {
1243 	int err, i;
1244 	u8 num_ext, *gen_info_buf;
1245 	u16 rev, len, next_ext_addr;
1246 
1247 	if (mmc_host_is_spi(card->host))
1248 		return 0;
1249 
1250 	if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT))
1251 		return 0;
1252 
1253 	gen_info_buf = kzalloc(512, GFP_KERNEL);
1254 	if (!gen_info_buf)
1255 		return -ENOMEM;
1256 
1257 	/*
1258 	 * Read 512 bytes of general info, which is found at function number 0,
1259 	 * at page 0 and with no offset.
1260 	 */
1261 	err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
1262 	if (err) {
1263 		pr_err("%s: error %d reading general info of SD ext reg\n",
1264 			mmc_hostname(card->host), err);
1265 		goto out;
1266 	}
1267 
1268 	/* General info structure revision. */
1269 	memcpy(&rev, &gen_info_buf[0], 2);
1270 
1271 	/* Length of general info in bytes. */
1272 	memcpy(&len, &gen_info_buf[2], 2);
1273 
1274 	/* Number of extensions to be find. */
1275 	num_ext = gen_info_buf[4];
1276 
1277 	/*
1278 	 * We only support revision 0 and limit it to 512 bytes for simplicity.
1279 	 * No matter what, let's return zero to allow us to continue using the
1280 	 * card, even if we can't support the features from the SD function
1281 	 * extensions registers.
1282 	 */
1283 	if (rev != 0 || len > 512) {
1284 		pr_warn("%s: non-supported SD ext reg layout\n",
1285 			mmc_hostname(card->host));
1286 		goto out;
1287 	}
1288 
1289 	/*
1290 	 * Parse the extension registers. The first extension should start
1291 	 * immediately after the general info header (16 bytes).
1292 	 */
1293 	next_ext_addr = 16;
1294 	for (i = 0; i < num_ext; i++) {
1295 		err = sd_parse_ext_reg(card, gen_info_buf, &next_ext_addr);
1296 		if (err) {
1297 			pr_err("%s: error %d parsing SD ext reg\n",
1298 				mmc_hostname(card->host), err);
1299 			goto out;
1300 		}
1301 	}
1302 
1303 out:
1304 	kfree(gen_info_buf);
1305 	return err;
1306 }
1307 
1308 static bool sd_cache_enabled(struct mmc_host *host)
1309 {
1310 	return host->card->ext_perf.feature_enabled & SD_EXT_PERF_CACHE;
1311 }
1312 
1313 static int sd_flush_cache(struct mmc_host *host)
1314 {
1315 	struct mmc_card *card = host->card;
1316 	u8 *reg_buf, fno, page;
1317 	u16 offset;
1318 	int err;
1319 
1320 	if (!sd_cache_enabled(host))
1321 		return 0;
1322 
1323 	reg_buf = kzalloc(512, GFP_KERNEL);
1324 	if (!reg_buf)
1325 		return -ENOMEM;
1326 
1327 	/*
1328 	 * Set Flush Cache at bit 0 in the performance enhancement register at
1329 	 * 261 bytes offset.
1330 	 */
1331 	fno = card->ext_perf.fno;
1332 	page = card->ext_perf.page;
1333 	offset = card->ext_perf.offset + 261;
1334 
1335 	err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
1336 	if (err) {
1337 		pr_warn("%s: error %d writing Cache Flush bit\n",
1338 			mmc_hostname(host), err);
1339 		goto out;
1340 	}
1341 
1342 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1343 				MMC_BUSY_EXTR_SINGLE);
1344 	if (err)
1345 		goto out;
1346 
1347 	/*
1348 	 * Read the Flush Cache bit. The card shall reset it, to confirm that
1349 	 * it's has completed the flushing of the cache.
1350 	 */
1351 	err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
1352 	if (err) {
1353 		pr_warn("%s: error %d reading Cache Flush bit\n",
1354 			mmc_hostname(host), err);
1355 		goto out;
1356 	}
1357 
1358 	if (reg_buf[0] & BIT(0))
1359 		err = -ETIMEDOUT;
1360 out:
1361 	kfree(reg_buf);
1362 	return err;
1363 }
1364 
1365 static int sd_enable_cache(struct mmc_card *card)
1366 {
1367 	u8 *reg_buf;
1368 	int err;
1369 
1370 	card->ext_perf.feature_enabled &= ~SD_EXT_PERF_CACHE;
1371 
1372 	reg_buf = kzalloc(512, GFP_KERNEL);
1373 	if (!reg_buf)
1374 		return -ENOMEM;
1375 
1376 	/*
1377 	 * Set Cache Enable at bit 0 in the performance enhancement register at
1378 	 * 260 bytes offset.
1379 	 */
1380 	err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
1381 			       card->ext_perf.offset + 260, BIT(0));
1382 	if (err) {
1383 		pr_warn("%s: error %d writing Cache Enable bit\n",
1384 			mmc_hostname(card->host), err);
1385 		goto out;
1386 	}
1387 
1388 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1389 				MMC_BUSY_EXTR_SINGLE);
1390 	if (!err)
1391 		card->ext_perf.feature_enabled |= SD_EXT_PERF_CACHE;
1392 
1393 out:
1394 	kfree(reg_buf);
1395 	return err;
1396 }
1397 
1398 /*
1399  * Handle the detection and initialisation of a card.
1400  *
1401  * In the case of a resume, "oldcard" will contain the card
1402  * we're trying to reinitialise.
1403  */
1404 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1405 	struct mmc_card *oldcard)
1406 {
1407 	struct mmc_card *card;
1408 	int err;
1409 	u32 cid[4];
1410 	u32 rocr = 0;
1411 	bool v18_fixup_failed = false;
1412 
1413 	WARN_ON(!host->claimed);
1414 retry:
1415 	err = mmc_sd_get_cid(host, ocr, cid, &rocr);
1416 	if (err)
1417 		return err;
1418 
1419 	if (oldcard) {
1420 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1421 			pr_debug("%s: Perhaps the card was replaced\n",
1422 				mmc_hostname(host));
1423 			return -ENOENT;
1424 		}
1425 
1426 		card = oldcard;
1427 	} else {
1428 		/*
1429 		 * Allocate card structure.
1430 		 */
1431 		card = mmc_alloc_card(host, &sd_type);
1432 		if (IS_ERR(card))
1433 			return PTR_ERR(card);
1434 
1435 		card->ocr = ocr;
1436 		card->type = MMC_TYPE_SD;
1437 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1438 	}
1439 
1440 	/*
1441 	 * Call the optional HC's init_card function to handle quirks.
1442 	 */
1443 	if (host->ops->init_card)
1444 		host->ops->init_card(host, card);
1445 
1446 	/*
1447 	 * For native busses:  get card RCA and quit open drain mode.
1448 	 */
1449 	if (!mmc_host_is_spi(host)) {
1450 		err = mmc_send_relative_addr(host, &card->rca);
1451 		if (err)
1452 			goto free_card;
1453 	}
1454 
1455 	if (!oldcard) {
1456 		err = mmc_sd_get_csd(card);
1457 		if (err)
1458 			goto free_card;
1459 
1460 		mmc_decode_cid(card);
1461 	}
1462 
1463 	/*
1464 	 * handling only for cards supporting DSR and hosts requesting
1465 	 * DSR configuration
1466 	 */
1467 	if (card->csd.dsr_imp && host->dsr_req)
1468 		mmc_set_dsr(host);
1469 
1470 	/*
1471 	 * Select card, as all following commands rely on that.
1472 	 */
1473 	if (!mmc_host_is_spi(host)) {
1474 		err = mmc_select_card(card);
1475 		if (err)
1476 			goto free_card;
1477 	}
1478 
1479 	/* Apply quirks prior to card setup */
1480 	mmc_fixup_device(card, mmc_sd_fixups);
1481 
1482 	err = mmc_sd_setup_card(host, card, oldcard != NULL);
1483 	if (err)
1484 		goto free_card;
1485 
1486 	/*
1487 	 * If the card has not been power cycled, it may still be using 1.8V
1488 	 * signaling. Detect that situation and try to initialize a UHS-I (1.8V)
1489 	 * transfer mode.
1490 	 */
1491 	if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
1492 	    mmc_sd_card_using_v18(card) &&
1493 	    host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1494 		if (mmc_host_set_uhs_voltage(host) ||
1495 		    mmc_sd_init_uhs_card(card)) {
1496 			v18_fixup_failed = true;
1497 			mmc_power_cycle(host, ocr);
1498 			if (!oldcard)
1499 				mmc_remove_card(card);
1500 			goto retry;
1501 		}
1502 		goto cont;
1503 	}
1504 
1505 	/* Initialization sequence for UHS-I cards */
1506 	if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) {
1507 		err = mmc_sd_init_uhs_card(card);
1508 		if (err)
1509 			goto free_card;
1510 	} else {
1511 		/*
1512 		 * Attempt to change to high-speed (if supported)
1513 		 */
1514 		err = mmc_sd_switch_hs(card);
1515 		if (err > 0)
1516 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
1517 		else if (err)
1518 			goto free_card;
1519 
1520 		/*
1521 		 * Set bus speed.
1522 		 */
1523 		mmc_set_clock(host, mmc_sd_get_max_clock(card));
1524 
1525 		if (host->ios.timing == MMC_TIMING_SD_HS &&
1526 			host->ops->prepare_sd_hs_tuning) {
1527 			err = host->ops->prepare_sd_hs_tuning(host, card);
1528 			if (err)
1529 				goto free_card;
1530 		}
1531 
1532 		/*
1533 		 * Switch to wider bus (if supported).
1534 		 */
1535 		if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1536 			(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1537 			err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1538 			if (err)
1539 				goto free_card;
1540 
1541 			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1542 		}
1543 
1544 		if (host->ios.timing == MMC_TIMING_SD_HS &&
1545 			host->ops->execute_sd_hs_tuning) {
1546 			err = host->ops->execute_sd_hs_tuning(host, card);
1547 			if (err)
1548 				goto free_card;
1549 		}
1550 	}
1551 cont:
1552 	if (!oldcard) {
1553 		/* Read/parse the extension registers. */
1554 		err = sd_read_ext_regs(card);
1555 		if (err)
1556 			goto free_card;
1557 	}
1558 
1559 	/* Enable internal SD cache if supported. */
1560 	if (card->ext_perf.feature_support & SD_EXT_PERF_CACHE) {
1561 		err = sd_enable_cache(card);
1562 		if (err)
1563 			goto free_card;
1564 	}
1565 
1566 	if (host->cqe_ops && !host->cqe_enabled) {
1567 		err = host->cqe_ops->cqe_enable(host, card);
1568 		if (!err) {
1569 			host->cqe_enabled = true;
1570 			host->hsq_enabled = true;
1571 			pr_info("%s: Host Software Queue enabled\n",
1572 				mmc_hostname(host));
1573 		}
1574 	}
1575 
1576 	if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1577 	    host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1578 		pr_err("%s: Host failed to negotiate down from 3.3V\n",
1579 			mmc_hostname(host));
1580 		err = -EINVAL;
1581 		goto free_card;
1582 	}
1583 
1584 	host->card = card;
1585 	return 0;
1586 
1587 free_card:
1588 	if (!oldcard)
1589 		mmc_remove_card(card);
1590 
1591 	return err;
1592 }
1593 
1594 /*
1595  * Host is being removed. Free up the current card.
1596  */
1597 static void mmc_sd_remove(struct mmc_host *host)
1598 {
1599 	mmc_remove_card(host->card);
1600 	host->card = NULL;
1601 }
1602 
1603 /*
1604  * Card detection - card is alive.
1605  */
1606 static int mmc_sd_alive(struct mmc_host *host)
1607 {
1608 	return mmc_send_status(host->card, NULL);
1609 }
1610 
1611 /*
1612  * Card detection callback from host.
1613  */
1614 static void mmc_sd_detect(struct mmc_host *host)
1615 {
1616 	int err;
1617 
1618 	mmc_get_card(host->card, NULL);
1619 
1620 	/*
1621 	 * Just check if our card has been removed.
1622 	 */
1623 	err = _mmc_detect_card_removed(host);
1624 
1625 	mmc_put_card(host->card, NULL);
1626 
1627 	if (err) {
1628 		mmc_sd_remove(host);
1629 
1630 		mmc_claim_host(host);
1631 		mmc_detach_bus(host);
1632 		mmc_power_off(host);
1633 		mmc_release_host(host);
1634 	}
1635 }
1636 
1637 static int sd_can_poweroff_notify(struct mmc_card *card)
1638 {
1639 	return card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY;
1640 }
1641 
1642 static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy)
1643 {
1644 	struct sd_busy_data *data = cb_data;
1645 	struct mmc_card *card = data->card;
1646 	int err;
1647 
1648 	/*
1649 	 * Read the status register for the power management function. It's at
1650 	 * one byte offset and is one byte long. The Power Off Notification
1651 	 * Ready is bit 0.
1652 	 */
1653 	err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1654 			      card->ext_power.offset + 1, 1, data->reg_buf);
1655 	if (err) {
1656 		pr_warn("%s: error %d reading status reg of PM func\n",
1657 			mmc_hostname(card->host), err);
1658 		return err;
1659 	}
1660 
1661 	*busy = !(data->reg_buf[0] & BIT(0));
1662 	return 0;
1663 }
1664 
1665 static int sd_poweroff_notify(struct mmc_card *card)
1666 {
1667 	struct sd_busy_data cb_data;
1668 	u8 *reg_buf;
1669 	int err;
1670 
1671 	reg_buf = kzalloc(512, GFP_KERNEL);
1672 	if (!reg_buf)
1673 		return -ENOMEM;
1674 
1675 	/*
1676 	 * Set the Power Off Notification bit in the power management settings
1677 	 * register at 2 bytes offset.
1678 	 */
1679 	err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1680 			       card->ext_power.offset + 2, BIT(0));
1681 	if (err) {
1682 		pr_warn("%s: error %d writing Power Off Notify bit\n",
1683 			mmc_hostname(card->host), err);
1684 		goto out;
1685 	}
1686 
1687 	/* Find out when the command is completed. */
1688 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1689 				MMC_BUSY_EXTR_SINGLE);
1690 	if (err)
1691 		goto out;
1692 
1693 	cb_data.card = card;
1694 	cb_data.reg_buf = reg_buf;
1695 	err = __mmc_poll_for_busy(card->host, 0, SD_POWEROFF_NOTIFY_TIMEOUT_MS,
1696 				  &sd_busy_poweroff_notify_cb, &cb_data);
1697 
1698 out:
1699 	kfree(reg_buf);
1700 	return err;
1701 }
1702 
1703 static int _mmc_sd_suspend(struct mmc_host *host)
1704 {
1705 	struct mmc_card *card = host->card;
1706 	int err = 0;
1707 
1708 	mmc_claim_host(host);
1709 
1710 	if (mmc_card_suspended(card))
1711 		goto out;
1712 
1713 	if (sd_can_poweroff_notify(card))
1714 		err = sd_poweroff_notify(card);
1715 	else if (!mmc_host_is_spi(host))
1716 		err = mmc_deselect_cards(host);
1717 
1718 	if (!err) {
1719 		mmc_power_off(host);
1720 		mmc_card_set_suspended(card);
1721 	}
1722 
1723 out:
1724 	mmc_release_host(host);
1725 	return err;
1726 }
1727 
1728 /*
1729  * Callback for suspend
1730  */
1731 static int mmc_sd_suspend(struct mmc_host *host)
1732 {
1733 	int err;
1734 
1735 	err = _mmc_sd_suspend(host);
1736 	if (!err) {
1737 		pm_runtime_disable(&host->card->dev);
1738 		pm_runtime_set_suspended(&host->card->dev);
1739 	}
1740 
1741 	return err;
1742 }
1743 
1744 /*
1745  * This function tries to determine if the same card is still present
1746  * and, if so, restore all state to it.
1747  */
1748 static int _mmc_sd_resume(struct mmc_host *host)
1749 {
1750 	int err = 0;
1751 
1752 	mmc_claim_host(host);
1753 
1754 	if (!mmc_card_suspended(host->card))
1755 		goto out;
1756 
1757 	mmc_power_up(host, host->card->ocr);
1758 	err = mmc_sd_init_card(host, host->card->ocr, host->card);
1759 	mmc_card_clr_suspended(host->card);
1760 
1761 out:
1762 	mmc_release_host(host);
1763 	return err;
1764 }
1765 
1766 /*
1767  * Callback for resume
1768  */
1769 static int mmc_sd_resume(struct mmc_host *host)
1770 {
1771 	pm_runtime_enable(&host->card->dev);
1772 	return 0;
1773 }
1774 
1775 /*
1776  * Callback for runtime_suspend.
1777  */
1778 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1779 {
1780 	int err;
1781 
1782 	if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1783 		return 0;
1784 
1785 	err = _mmc_sd_suspend(host);
1786 	if (err)
1787 		pr_err("%s: error %d doing aggressive suspend\n",
1788 			mmc_hostname(host), err);
1789 
1790 	return err;
1791 }
1792 
1793 /*
1794  * Callback for runtime_resume.
1795  */
1796 static int mmc_sd_runtime_resume(struct mmc_host *host)
1797 {
1798 	int err;
1799 
1800 	err = _mmc_sd_resume(host);
1801 	if (err && err != -ENOMEDIUM)
1802 		pr_err("%s: error %d doing runtime resume\n",
1803 			mmc_hostname(host), err);
1804 
1805 	return 0;
1806 }
1807 
1808 static int mmc_sd_hw_reset(struct mmc_host *host)
1809 {
1810 	mmc_power_cycle(host, host->card->ocr);
1811 	return mmc_sd_init_card(host, host->card->ocr, host->card);
1812 }
1813 
1814 static const struct mmc_bus_ops mmc_sd_ops = {
1815 	.remove = mmc_sd_remove,
1816 	.detect = mmc_sd_detect,
1817 	.runtime_suspend = mmc_sd_runtime_suspend,
1818 	.runtime_resume = mmc_sd_runtime_resume,
1819 	.suspend = mmc_sd_suspend,
1820 	.resume = mmc_sd_resume,
1821 	.alive = mmc_sd_alive,
1822 	.shutdown = mmc_sd_suspend,
1823 	.hw_reset = mmc_sd_hw_reset,
1824 	.cache_enabled = sd_cache_enabled,
1825 	.flush_cache = sd_flush_cache,
1826 };
1827 
1828 /*
1829  * Starting point for SD card init.
1830  */
1831 int mmc_attach_sd(struct mmc_host *host)
1832 {
1833 	int err;
1834 	u32 ocr, rocr;
1835 
1836 	WARN_ON(!host->claimed);
1837 
1838 	err = mmc_send_app_op_cond(host, 0, &ocr);
1839 	if (err)
1840 		return err;
1841 
1842 	mmc_attach_bus(host, &mmc_sd_ops);
1843 	if (host->ocr_avail_sd)
1844 		host->ocr_avail = host->ocr_avail_sd;
1845 
1846 	/*
1847 	 * We need to get OCR a different way for SPI.
1848 	 */
1849 	if (mmc_host_is_spi(host)) {
1850 		mmc_go_idle(host);
1851 
1852 		err = mmc_spi_read_ocr(host, 0, &ocr);
1853 		if (err)
1854 			goto err;
1855 	}
1856 
1857 	/*
1858 	 * Some SD cards claims an out of spec VDD voltage range. Let's treat
1859 	 * these bits as being in-valid and especially also bit7.
1860 	 */
1861 	ocr &= ~0x7FFF;
1862 
1863 	rocr = mmc_select_voltage(host, ocr);
1864 
1865 	/*
1866 	 * Can we support the voltage(s) of the card(s)?
1867 	 */
1868 	if (!rocr) {
1869 		err = -EINVAL;
1870 		goto err;
1871 	}
1872 
1873 	/*
1874 	 * Detect and init the card.
1875 	 */
1876 	err = mmc_sd_init_card(host, rocr, NULL);
1877 	if (err)
1878 		goto err;
1879 
1880 	mmc_release_host(host);
1881 	err = mmc_add_card(host->card);
1882 	if (err)
1883 		goto remove_card;
1884 
1885 	mmc_claim_host(host);
1886 	return 0;
1887 
1888 remove_card:
1889 	mmc_remove_card(host->card);
1890 	host->card = NULL;
1891 	mmc_claim_host(host);
1892 err:
1893 	mmc_detach_bus(host);
1894 
1895 	pr_err("%s: error %d whilst initialising SD card\n",
1896 		mmc_hostname(host), err);
1897 
1898 	return err;
1899 }
1900