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