xref: /openbmc/linux/drivers/mmc/core/sd.c (revision af9b2ff010f593d81e2f5fb04155e9fc25b9dfd0)
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  */
mmc_decode_cid(struct mmc_card * card)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  */
mmc_decode_csd(struct mmc_card * card)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  */
mmc_decode_scr(struct mmc_card * card)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  */
mmc_read_ssr(struct mmc_card * card)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  */
mmc_read_switch(struct mmc_card * card)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  */
mmc_sd_switch_hs(struct mmc_card * card)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 
sd_select_driver_type(struct mmc_card * card,u8 * status)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 
sd_update_bus_speed_mode(struct mmc_card * card)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 
sd_set_bus_speed_mode(struct mmc_card * card,u8 * status)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 */
sd_get_host_max_current(struct mmc_host * host)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 
sd_set_current_limit(struct mmc_card * card,u8 * status)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  * Determine if the card should tune or not.
623  */
mmc_sd_use_tuning(struct mmc_card * card)624 static bool mmc_sd_use_tuning(struct mmc_card *card)
625 {
626 	/*
627 	 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
628 	 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
629 	 */
630 	if (mmc_host_is_spi(card->host))
631 		return false;
632 
633 	switch (card->host->ios.timing) {
634 	case MMC_TIMING_UHS_SDR50:
635 	case MMC_TIMING_UHS_SDR104:
636 		return true;
637 	case MMC_TIMING_UHS_DDR50:
638 		return !mmc_card_no_uhs_ddr50_tuning(card);
639 	}
640 
641 	return false;
642 }
643 
644 /*
645  * UHS-I specific initialization procedure
646  */
mmc_sd_init_uhs_card(struct mmc_card * card)647 static int mmc_sd_init_uhs_card(struct mmc_card *card)
648 {
649 	int err;
650 	u8 *status;
651 
652 	if (!(card->csd.cmdclass & CCC_SWITCH))
653 		return 0;
654 
655 	status = kmalloc(64, GFP_KERNEL);
656 	if (!status)
657 		return -ENOMEM;
658 
659 	/* Set 4-bit bus width */
660 	err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
661 	if (err)
662 		goto out;
663 
664 	mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
665 
666 	/*
667 	 * Select the bus speed mode depending on host
668 	 * and card capability.
669 	 */
670 	sd_update_bus_speed_mode(card);
671 
672 	/* Set the driver strength for the card */
673 	err = sd_select_driver_type(card, status);
674 	if (err)
675 		goto out;
676 
677 	/* Set current limit for the card */
678 	err = sd_set_current_limit(card, status);
679 	if (err)
680 		goto out;
681 
682 	/* Set bus speed mode of the card */
683 	err = sd_set_bus_speed_mode(card, status);
684 	if (err)
685 		goto out;
686 
687 	if (mmc_sd_use_tuning(card)) {
688 		err = mmc_execute_tuning(card);
689 
690 		/*
691 		 * As SD Specifications Part1 Physical Layer Specification
692 		 * Version 3.01 says, CMD19 tuning is available for unlocked
693 		 * cards in transfer state of 1.8V signaling mode. The small
694 		 * difference between v3.00 and 3.01 spec means that CMD19
695 		 * tuning is also available for DDR50 mode.
696 		 */
697 		if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
698 			pr_warn("%s: ddr50 tuning failed\n",
699 				mmc_hostname(card->host));
700 			err = 0;
701 		}
702 	}
703 
704 out:
705 	kfree(status);
706 
707 	return err;
708 }
709 
710 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
711 	card->raw_cid[2], card->raw_cid[3]);
712 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
713 	card->raw_csd[2], card->raw_csd[3]);
714 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
715 MMC_DEV_ATTR(ssr,
716 	"%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
717 		card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
718 		card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
719 		card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
720 		card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
721 		card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
722 		card->raw_ssr[15]);
723 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
724 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
725 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
726 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
727 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
728 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
729 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
730 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
731 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
732 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
733 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
734 
735 
mmc_dsr_show(struct device * dev,struct device_attribute * attr,char * buf)736 static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
737 			    char *buf)
738 {
739 	struct mmc_card *card = mmc_dev_to_card(dev);
740 	struct mmc_host *host = card->host;
741 
742 	if (card->csd.dsr_imp && host->dsr_req)
743 		return sysfs_emit(buf, "0x%x\n", host->dsr);
744 	/* return default DSR value */
745 	return sysfs_emit(buf, "0x%x\n", 0x404);
746 }
747 
748 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
749 
750 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
751 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
752 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
753 
754 #define sdio_info_attr(num)									\
755 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf)	\
756 {												\
757 	struct mmc_card *card = mmc_dev_to_card(dev);						\
758 												\
759 	if (num > card->num_info)								\
760 		return -ENODATA;								\
761 	if (!card->info[num - 1][0])								\
762 		return 0;									\
763 	return sysfs_emit(buf, "%s\n", card->info[num - 1]);					\
764 }												\
765 static DEVICE_ATTR_RO(info##num)
766 
767 sdio_info_attr(1);
768 sdio_info_attr(2);
769 sdio_info_attr(3);
770 sdio_info_attr(4);
771 
772 static struct attribute *sd_std_attrs[] = {
773 	&dev_attr_vendor.attr,
774 	&dev_attr_device.attr,
775 	&dev_attr_revision.attr,
776 	&dev_attr_info1.attr,
777 	&dev_attr_info2.attr,
778 	&dev_attr_info3.attr,
779 	&dev_attr_info4.attr,
780 	&dev_attr_cid.attr,
781 	&dev_attr_csd.attr,
782 	&dev_attr_scr.attr,
783 	&dev_attr_ssr.attr,
784 	&dev_attr_date.attr,
785 	&dev_attr_erase_size.attr,
786 	&dev_attr_preferred_erase_size.attr,
787 	&dev_attr_fwrev.attr,
788 	&dev_attr_hwrev.attr,
789 	&dev_attr_manfid.attr,
790 	&dev_attr_name.attr,
791 	&dev_attr_oemid.attr,
792 	&dev_attr_serial.attr,
793 	&dev_attr_ocr.attr,
794 	&dev_attr_rca.attr,
795 	&dev_attr_dsr.attr,
796 	NULL,
797 };
798 
sd_std_is_visible(struct kobject * kobj,struct attribute * attr,int index)799 static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
800 				 int index)
801 {
802 	struct device *dev = kobj_to_dev(kobj);
803 	struct mmc_card *card = mmc_dev_to_card(dev);
804 
805 	/* CIS vendor and device ids, revision and info string are available only for Combo cards */
806 	if ((attr == &dev_attr_vendor.attr ||
807 	     attr == &dev_attr_device.attr ||
808 	     attr == &dev_attr_revision.attr ||
809 	     attr == &dev_attr_info1.attr ||
810 	     attr == &dev_attr_info2.attr ||
811 	     attr == &dev_attr_info3.attr ||
812 	     attr == &dev_attr_info4.attr
813 	    ) &&!mmc_card_sd_combo(card))
814 		return 0;
815 
816 	return attr->mode;
817 }
818 
819 static const struct attribute_group sd_std_group = {
820 	.attrs = sd_std_attrs,
821 	.is_visible = sd_std_is_visible,
822 };
823 __ATTRIBUTE_GROUPS(sd_std);
824 
825 struct device_type sd_type = {
826 	.groups = sd_std_groups,
827 };
828 
829 /*
830  * Fetch CID from card.
831  */
mmc_sd_get_cid(struct mmc_host * host,u32 ocr,u32 * cid,u32 * rocr)832 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
833 {
834 	int err;
835 	u32 max_current;
836 	int retries = 10;
837 	u32 pocr = ocr;
838 
839 try_again:
840 	if (!retries) {
841 		ocr &= ~SD_OCR_S18R;
842 		pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
843 	}
844 
845 	/*
846 	 * Since we're changing the OCR value, we seem to
847 	 * need to tell some cards to go back to the idle
848 	 * state.  We wait 1ms to give cards time to
849 	 * respond.
850 	 */
851 	mmc_go_idle(host);
852 
853 	/*
854 	 * If SD_SEND_IF_COND indicates an SD 2.0
855 	 * compliant card and we should set bit 30
856 	 * of the ocr to indicate that we can handle
857 	 * block-addressed SDHC cards.
858 	 */
859 	err = mmc_send_if_cond(host, ocr);
860 	if (!err)
861 		ocr |= SD_OCR_CCS;
862 
863 	/*
864 	 * If the host supports one of UHS-I modes, request the card
865 	 * to switch to 1.8V signaling level. If the card has failed
866 	 * repeatedly to switch however, skip this.
867 	 */
868 	if (retries && mmc_host_uhs(host))
869 		ocr |= SD_OCR_S18R;
870 
871 	/*
872 	 * If the host can supply more than 150mA at current voltage,
873 	 * XPC should be set to 1.
874 	 */
875 	max_current = sd_get_host_max_current(host);
876 	if (max_current > 150)
877 		ocr |= SD_OCR_XPC;
878 
879 	err = mmc_send_app_op_cond(host, ocr, rocr);
880 	if (err)
881 		return err;
882 
883 	/*
884 	 * In case the S18A bit is set in the response, let's start the signal
885 	 * voltage switch procedure. SPI mode doesn't support CMD11.
886 	 * Note that, according to the spec, the S18A bit is not valid unless
887 	 * the CCS bit is set as well. We deliberately deviate from the spec in
888 	 * regards to this, which allows UHS-I to be supported for SDSC cards.
889 	 */
890 	if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
891 	    rocr && (*rocr & SD_ROCR_S18A)) {
892 		err = mmc_set_uhs_voltage(host, pocr);
893 		if (err == -EAGAIN) {
894 			retries--;
895 			goto try_again;
896 		} else if (err) {
897 			retries = 0;
898 			goto try_again;
899 		}
900 	}
901 
902 	err = mmc_send_cid(host, cid);
903 	return err;
904 }
905 
mmc_sd_get_csd(struct mmc_card * card)906 int mmc_sd_get_csd(struct mmc_card *card)
907 {
908 	int err;
909 
910 	/*
911 	 * Fetch CSD from card.
912 	 */
913 	err = mmc_send_csd(card, card->raw_csd);
914 	if (err)
915 		return err;
916 
917 	err = mmc_decode_csd(card);
918 	if (err)
919 		return err;
920 
921 	return 0;
922 }
923 
mmc_sd_get_ro(struct mmc_host * host)924 static int mmc_sd_get_ro(struct mmc_host *host)
925 {
926 	int ro;
927 
928 	/*
929 	 * Some systems don't feature a write-protect pin and don't need one.
930 	 * E.g. because they only have micro-SD card slot. For those systems
931 	 * assume that the SD card is always read-write.
932 	 */
933 	if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
934 		return 0;
935 
936 	if (!host->ops->get_ro)
937 		return -1;
938 
939 	ro = host->ops->get_ro(host);
940 
941 	return ro;
942 }
943 
mmc_sd_setup_card(struct mmc_host * host,struct mmc_card * card,bool reinit)944 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
945 	bool reinit)
946 {
947 	int err;
948 
949 	if (!reinit) {
950 		/*
951 		 * Fetch SCR from card.
952 		 */
953 		err = mmc_app_send_scr(card);
954 		if (err)
955 			return err;
956 
957 		err = mmc_decode_scr(card);
958 		if (err)
959 			return err;
960 
961 		/*
962 		 * Fetch and process SD Status register.
963 		 */
964 		err = mmc_read_ssr(card);
965 		if (err)
966 			return err;
967 
968 		/* Erase init depends on CSD and SSR */
969 		mmc_init_erase(card);
970 	}
971 
972 	/*
973 	 * Fetch switch information from card. Note, sd3_bus_mode can change if
974 	 * voltage switch outcome changes, so do this always.
975 	 */
976 	err = mmc_read_switch(card);
977 	if (err)
978 		return err;
979 
980 	/*
981 	 * For SPI, enable CRC as appropriate.
982 	 * This CRC enable is located AFTER the reading of the
983 	 * card registers because some SDHC cards are not able
984 	 * to provide valid CRCs for non-512-byte blocks.
985 	 */
986 	if (mmc_host_is_spi(host)) {
987 		err = mmc_spi_set_crc(host, use_spi_crc);
988 		if (err)
989 			return err;
990 	}
991 
992 	/*
993 	 * Check if read-only switch is active.
994 	 */
995 	if (!reinit) {
996 		int ro = mmc_sd_get_ro(host);
997 
998 		if (ro < 0) {
999 			pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
1000 				mmc_hostname(host));
1001 		} else if (ro > 0) {
1002 			mmc_card_set_readonly(card);
1003 		}
1004 	}
1005 
1006 	return 0;
1007 }
1008 
mmc_sd_get_max_clock(struct mmc_card * card)1009 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
1010 {
1011 	unsigned max_dtr = (unsigned int)-1;
1012 
1013 	if (mmc_card_hs(card)) {
1014 		if (max_dtr > card->sw_caps.hs_max_dtr)
1015 			max_dtr = card->sw_caps.hs_max_dtr;
1016 	} else if (max_dtr > card->csd.max_dtr) {
1017 		max_dtr = card->csd.max_dtr;
1018 	}
1019 
1020 	return max_dtr;
1021 }
1022 
mmc_sd_card_using_v18(struct mmc_card * card)1023 static bool mmc_sd_card_using_v18(struct mmc_card *card)
1024 {
1025 	/*
1026 	 * According to the SD spec., the Bus Speed Mode (function group 1) bits
1027 	 * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus
1028 	 * they can be used to determine if the card has already switched to
1029 	 * 1.8V signaling.
1030 	 */
1031 	return card->sw_caps.sd3_bus_mode &
1032 	       (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
1033 }
1034 
sd_write_ext_reg(struct mmc_card * card,u8 fno,u8 page,u16 offset,u8 reg_data)1035 static int sd_write_ext_reg(struct mmc_card *card, u8 fno, u8 page, u16 offset,
1036 			    u8 reg_data)
1037 {
1038 	struct mmc_host *host = card->host;
1039 	struct mmc_request mrq = {};
1040 	struct mmc_command cmd = {};
1041 	struct mmc_data data = {};
1042 	struct scatterlist sg;
1043 	u8 *reg_buf;
1044 
1045 	reg_buf = kzalloc(512, GFP_KERNEL);
1046 	if (!reg_buf)
1047 		return -ENOMEM;
1048 
1049 	mrq.cmd = &cmd;
1050 	mrq.data = &data;
1051 
1052 	/*
1053 	 * Arguments of CMD49:
1054 	 * [31:31] MIO (0 = memory).
1055 	 * [30:27] FNO (function number).
1056 	 * [26:26] MW - mask write mode (0 = disable).
1057 	 * [25:18] page number.
1058 	 * [17:9] offset address.
1059 	 * [8:0] length (0 = 1 byte).
1060 	 */
1061 	cmd.arg = fno << 27 | page << 18 | offset << 9;
1062 
1063 	/* The first byte in the buffer is the data to be written. */
1064 	reg_buf[0] = reg_data;
1065 
1066 	data.flags = MMC_DATA_WRITE;
1067 	data.blksz = 512;
1068 	data.blocks = 1;
1069 	data.sg = &sg;
1070 	data.sg_len = 1;
1071 	sg_init_one(&sg, reg_buf, 512);
1072 
1073 	cmd.opcode = SD_WRITE_EXTR_SINGLE;
1074 	cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1075 
1076 	mmc_set_data_timeout(&data, card);
1077 	mmc_wait_for_req(host, &mrq);
1078 
1079 	kfree(reg_buf);
1080 
1081 	/*
1082 	 * Note that, the SD card is allowed to signal busy on DAT0 up to 1s
1083 	 * after the CMD49. Although, let's leave this to be managed by the
1084 	 * caller.
1085 	 */
1086 
1087 	if (cmd.error)
1088 		return cmd.error;
1089 	if (data.error)
1090 		return data.error;
1091 
1092 	return 0;
1093 }
1094 
sd_read_ext_reg(struct mmc_card * card,u8 fno,u8 page,u16 offset,u16 len,u8 * reg_buf)1095 static int sd_read_ext_reg(struct mmc_card *card, u8 fno, u8 page,
1096 			   u16 offset, u16 len, u8 *reg_buf)
1097 {
1098 	u32 cmd_args;
1099 
1100 	/*
1101 	 * Command arguments of CMD48:
1102 	 * [31:31] MIO (0 = memory).
1103 	 * [30:27] FNO (function number).
1104 	 * [26:26] reserved (0).
1105 	 * [25:18] page number.
1106 	 * [17:9] offset address.
1107 	 * [8:0] length (0 = 1 byte, 1ff = 512 bytes).
1108 	 */
1109 	cmd_args = fno << 27 | page << 18 | offset << 9 | (len -1);
1110 
1111 	return mmc_send_adtc_data(card, card->host, SD_READ_EXTR_SINGLE,
1112 				  cmd_args, reg_buf, 512);
1113 }
1114 
sd_parse_ext_reg_power(struct mmc_card * card,u8 fno,u8 page,u16 offset)1115 static int sd_parse_ext_reg_power(struct mmc_card *card, u8 fno, u8 page,
1116 				  u16 offset)
1117 {
1118 	int err;
1119 	u8 *reg_buf;
1120 
1121 	reg_buf = kzalloc(512, GFP_KERNEL);
1122 	if (!reg_buf)
1123 		return -ENOMEM;
1124 
1125 	/* Read the extension register for power management function. */
1126 	err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1127 	if (err) {
1128 		pr_warn("%s: error %d reading PM func of ext reg\n",
1129 			mmc_hostname(card->host), err);
1130 		goto out;
1131 	}
1132 
1133 	/* PM revision consists of 4 bits. */
1134 	card->ext_power.rev = reg_buf[0] & 0xf;
1135 
1136 	/* Power Off Notification support at bit 4. */
1137 	if ((reg_buf[1] & BIT(4)) && !mmc_card_broken_sd_poweroff_notify(card))
1138 		card->ext_power.feature_support |= SD_EXT_POWER_OFF_NOTIFY;
1139 
1140 	/* Power Sustenance support at bit 5. */
1141 	if (reg_buf[1] & BIT(5))
1142 		card->ext_power.feature_support |= SD_EXT_POWER_SUSTENANCE;
1143 
1144 	/* Power Down Mode support at bit 6. */
1145 	if (reg_buf[1] & BIT(6))
1146 		card->ext_power.feature_support |= SD_EXT_POWER_DOWN_MODE;
1147 
1148 	card->ext_power.fno = fno;
1149 	card->ext_power.page = page;
1150 	card->ext_power.offset = offset;
1151 
1152 out:
1153 	kfree(reg_buf);
1154 	return err;
1155 }
1156 
sd_parse_ext_reg_perf(struct mmc_card * card,u8 fno,u8 page,u16 offset)1157 static int sd_parse_ext_reg_perf(struct mmc_card *card, u8 fno, u8 page,
1158 				 u16 offset)
1159 {
1160 	int err;
1161 	u8 *reg_buf;
1162 
1163 	reg_buf = kzalloc(512, GFP_KERNEL);
1164 	if (!reg_buf)
1165 		return -ENOMEM;
1166 
1167 	err = sd_read_ext_reg(card, fno, page, offset, 512, reg_buf);
1168 	if (err) {
1169 		pr_warn("%s: error %d reading PERF func of ext reg\n",
1170 			mmc_hostname(card->host), err);
1171 		goto out;
1172 	}
1173 
1174 	/* PERF revision. */
1175 	card->ext_perf.rev = reg_buf[0];
1176 
1177 	/* FX_EVENT support at bit 0. */
1178 	if (reg_buf[1] & BIT(0))
1179 		card->ext_perf.feature_support |= SD_EXT_PERF_FX_EVENT;
1180 
1181 	/* Card initiated self-maintenance support at bit 0. */
1182 	if (reg_buf[2] & BIT(0))
1183 		card->ext_perf.feature_support |= SD_EXT_PERF_CARD_MAINT;
1184 
1185 	/* Host initiated self-maintenance support at bit 1. */
1186 	if (reg_buf[2] & BIT(1))
1187 		card->ext_perf.feature_support |= SD_EXT_PERF_HOST_MAINT;
1188 
1189 	/* Cache support at bit 0. */
1190 	if ((reg_buf[4] & BIT(0)) && !mmc_card_broken_sd_cache(card))
1191 		card->ext_perf.feature_support |= SD_EXT_PERF_CACHE;
1192 
1193 	/* Command queue support indicated via queue depth bits (0 to 4). */
1194 	if (reg_buf[6] & 0x1f)
1195 		card->ext_perf.feature_support |= SD_EXT_PERF_CMD_QUEUE;
1196 
1197 	card->ext_perf.fno = fno;
1198 	card->ext_perf.page = page;
1199 	card->ext_perf.offset = offset;
1200 
1201 out:
1202 	kfree(reg_buf);
1203 	return err;
1204 }
1205 
sd_parse_ext_reg(struct mmc_card * card,u8 * gen_info_buf,u16 * next_ext_addr)1206 static int sd_parse_ext_reg(struct mmc_card *card, u8 *gen_info_buf,
1207 			    u16 *next_ext_addr)
1208 {
1209 	u8 num_regs, fno, page;
1210 	u16 sfc, offset, ext = *next_ext_addr;
1211 	u32 reg_addr;
1212 
1213 	/*
1214 	 * Parse only one register set per extension, as that is sufficient to
1215 	 * support the standard functions. This means another 48 bytes in the
1216 	 * buffer must be available.
1217 	 */
1218 	if (ext + 48 > 512)
1219 		return -EFAULT;
1220 
1221 	/* Standard Function Code */
1222 	memcpy(&sfc, &gen_info_buf[ext], 2);
1223 
1224 	/* Address to the next extension. */
1225 	memcpy(next_ext_addr, &gen_info_buf[ext + 40], 2);
1226 
1227 	/* Number of registers for this extension. */
1228 	num_regs = gen_info_buf[ext + 42];
1229 
1230 	/* We support only one register per extension. */
1231 	if (num_regs != 1)
1232 		return 0;
1233 
1234 	/* Extension register address. */
1235 	memcpy(&reg_addr, &gen_info_buf[ext + 44], 4);
1236 
1237 	/* 9 bits (0 to 8) contains the offset address. */
1238 	offset = reg_addr & 0x1ff;
1239 
1240 	/* 8 bits (9 to 16) contains the page number. */
1241 	page = reg_addr >> 9 & 0xff ;
1242 
1243 	/* 4 bits (18 to 21) contains the function number. */
1244 	fno = reg_addr >> 18 & 0xf;
1245 
1246 	/* Standard Function Code for power management. */
1247 	if (sfc == 0x1)
1248 		return sd_parse_ext_reg_power(card, fno, page, offset);
1249 
1250 	/* Standard Function Code for performance enhancement. */
1251 	if (sfc == 0x2)
1252 		return sd_parse_ext_reg_perf(card, fno, page, offset);
1253 
1254 	return 0;
1255 }
1256 
sd_read_ext_regs(struct mmc_card * card)1257 static int sd_read_ext_regs(struct mmc_card *card)
1258 {
1259 	int err, i;
1260 	u8 num_ext, *gen_info_buf;
1261 	u16 rev, len, next_ext_addr;
1262 
1263 	if (mmc_host_is_spi(card->host))
1264 		return 0;
1265 
1266 	if (!(card->scr.cmds & SD_SCR_CMD48_SUPPORT))
1267 		return 0;
1268 
1269 	gen_info_buf = kzalloc(512, GFP_KERNEL);
1270 	if (!gen_info_buf)
1271 		return -ENOMEM;
1272 
1273 	/*
1274 	 * Read 512 bytes of general info, which is found at function number 0,
1275 	 * at page 0 and with no offset.
1276 	 */
1277 	err = sd_read_ext_reg(card, 0, 0, 0, 512, gen_info_buf);
1278 	if (err) {
1279 		pr_err("%s: error %d reading general info of SD ext reg\n",
1280 			mmc_hostname(card->host), err);
1281 		goto out;
1282 	}
1283 
1284 	/* General info structure revision. */
1285 	memcpy(&rev, &gen_info_buf[0], 2);
1286 
1287 	/* Length of general info in bytes. */
1288 	memcpy(&len, &gen_info_buf[2], 2);
1289 
1290 	/* Number of extensions to be find. */
1291 	num_ext = gen_info_buf[4];
1292 
1293 	/*
1294 	 * We only support revision 0 and limit it to 512 bytes for simplicity.
1295 	 * No matter what, let's return zero to allow us to continue using the
1296 	 * card, even if we can't support the features from the SD function
1297 	 * extensions registers.
1298 	 */
1299 	if (rev != 0 || len > 512) {
1300 		pr_warn("%s: non-supported SD ext reg layout\n",
1301 			mmc_hostname(card->host));
1302 		goto out;
1303 	}
1304 
1305 	/*
1306 	 * Parse the extension registers. The first extension should start
1307 	 * immediately after the general info header (16 bytes).
1308 	 */
1309 	next_ext_addr = 16;
1310 	for (i = 0; i < num_ext; i++) {
1311 		err = sd_parse_ext_reg(card, gen_info_buf, &next_ext_addr);
1312 		if (err) {
1313 			pr_err("%s: error %d parsing SD ext reg\n",
1314 				mmc_hostname(card->host), err);
1315 			goto out;
1316 		}
1317 	}
1318 
1319 out:
1320 	kfree(gen_info_buf);
1321 	return err;
1322 }
1323 
sd_cache_enabled(struct mmc_host * host)1324 static bool sd_cache_enabled(struct mmc_host *host)
1325 {
1326 	return host->card->ext_perf.feature_enabled & SD_EXT_PERF_CACHE;
1327 }
1328 
sd_flush_cache(struct mmc_host * host)1329 static int sd_flush_cache(struct mmc_host *host)
1330 {
1331 	struct mmc_card *card = host->card;
1332 	u8 *reg_buf, fno, page;
1333 	u16 offset;
1334 	int err;
1335 
1336 	if (!sd_cache_enabled(host))
1337 		return 0;
1338 
1339 	reg_buf = kzalloc(512, GFP_KERNEL);
1340 	if (!reg_buf)
1341 		return -ENOMEM;
1342 
1343 	/*
1344 	 * Set Flush Cache at bit 0 in the performance enhancement register at
1345 	 * 261 bytes offset.
1346 	 */
1347 	fno = card->ext_perf.fno;
1348 	page = card->ext_perf.page;
1349 	offset = card->ext_perf.offset + 261;
1350 
1351 	err = sd_write_ext_reg(card, fno, page, offset, BIT(0));
1352 	if (err) {
1353 		pr_warn("%s: error %d writing Cache Flush bit\n",
1354 			mmc_hostname(host), err);
1355 		goto out;
1356 	}
1357 
1358 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1359 				MMC_BUSY_EXTR_SINGLE);
1360 	if (err)
1361 		goto out;
1362 
1363 	/*
1364 	 * Read the Flush Cache bit. The card shall reset it, to confirm that
1365 	 * it's has completed the flushing of the cache.
1366 	 */
1367 	err = sd_read_ext_reg(card, fno, page, offset, 1, reg_buf);
1368 	if (err) {
1369 		pr_warn("%s: error %d reading Cache Flush bit\n",
1370 			mmc_hostname(host), err);
1371 		goto out;
1372 	}
1373 
1374 	if (reg_buf[0] & BIT(0))
1375 		err = -ETIMEDOUT;
1376 out:
1377 	kfree(reg_buf);
1378 	return err;
1379 }
1380 
sd_enable_cache(struct mmc_card * card)1381 static int sd_enable_cache(struct mmc_card *card)
1382 {
1383 	u8 *reg_buf;
1384 	int err;
1385 
1386 	card->ext_perf.feature_enabled &= ~SD_EXT_PERF_CACHE;
1387 
1388 	reg_buf = kzalloc(512, GFP_KERNEL);
1389 	if (!reg_buf)
1390 		return -ENOMEM;
1391 
1392 	/*
1393 	 * Set Cache Enable at bit 0 in the performance enhancement register at
1394 	 * 260 bytes offset.
1395 	 */
1396 	err = sd_write_ext_reg(card, card->ext_perf.fno, card->ext_perf.page,
1397 			       card->ext_perf.offset + 260, BIT(0));
1398 	if (err) {
1399 		pr_warn("%s: error %d writing Cache Enable bit\n",
1400 			mmc_hostname(card->host), err);
1401 		goto out;
1402 	}
1403 
1404 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1405 				MMC_BUSY_EXTR_SINGLE);
1406 	if (!err)
1407 		card->ext_perf.feature_enabled |= SD_EXT_PERF_CACHE;
1408 
1409 out:
1410 	kfree(reg_buf);
1411 	return err;
1412 }
1413 
1414 /*
1415  * Handle the detection and initialisation of a card.
1416  *
1417  * In the case of a resume, "oldcard" will contain the card
1418  * we're trying to reinitialise.
1419  */
mmc_sd_init_card(struct mmc_host * host,u32 ocr,struct mmc_card * oldcard)1420 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1421 	struct mmc_card *oldcard)
1422 {
1423 	struct mmc_card *card;
1424 	int err;
1425 	u32 cid[4];
1426 	u32 rocr = 0;
1427 	bool v18_fixup_failed = false;
1428 
1429 	WARN_ON(!host->claimed);
1430 retry:
1431 	err = mmc_sd_get_cid(host, ocr, cid, &rocr);
1432 	if (err)
1433 		return err;
1434 
1435 	if (oldcard) {
1436 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1437 			pr_debug("%s: Perhaps the card was replaced\n",
1438 				mmc_hostname(host));
1439 			return -ENOENT;
1440 		}
1441 
1442 		card = oldcard;
1443 	} else {
1444 		/*
1445 		 * Allocate card structure.
1446 		 */
1447 		card = mmc_alloc_card(host, &sd_type);
1448 		if (IS_ERR(card))
1449 			return PTR_ERR(card);
1450 
1451 		card->ocr = ocr;
1452 		card->type = MMC_TYPE_SD;
1453 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1454 	}
1455 
1456 	/*
1457 	 * Call the optional HC's init_card function to handle quirks.
1458 	 */
1459 	if (host->ops->init_card)
1460 		host->ops->init_card(host, card);
1461 
1462 	/*
1463 	 * For native busses:  get card RCA and quit open drain mode.
1464 	 */
1465 	if (!mmc_host_is_spi(host)) {
1466 		err = mmc_send_relative_addr(host, &card->rca);
1467 		if (err)
1468 			goto free_card;
1469 	}
1470 
1471 	if (!oldcard) {
1472 		err = mmc_sd_get_csd(card);
1473 		if (err)
1474 			goto free_card;
1475 
1476 		mmc_decode_cid(card);
1477 	}
1478 
1479 	/*
1480 	 * handling only for cards supporting DSR and hosts requesting
1481 	 * DSR configuration
1482 	 */
1483 	if (card->csd.dsr_imp && host->dsr_req)
1484 		mmc_set_dsr(host);
1485 
1486 	/*
1487 	 * Select card, as all following commands rely on that.
1488 	 */
1489 	if (!mmc_host_is_spi(host)) {
1490 		err = mmc_select_card(card);
1491 		if (err)
1492 			goto free_card;
1493 	}
1494 
1495 	/* Apply quirks prior to card setup */
1496 	mmc_fixup_device(card, mmc_sd_fixups);
1497 
1498 	err = mmc_sd_setup_card(host, card, oldcard != NULL);
1499 	if (err)
1500 		goto free_card;
1501 
1502 	/*
1503 	 * If the card has not been power cycled, it may still be using 1.8V
1504 	 * signaling. Detect that situation and try to initialize a UHS-I (1.8V)
1505 	 * transfer mode.
1506 	 */
1507 	if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
1508 	    mmc_sd_card_using_v18(card) &&
1509 	    host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1510 		if (mmc_host_set_uhs_voltage(host) ||
1511 		    mmc_sd_init_uhs_card(card)) {
1512 			v18_fixup_failed = true;
1513 			mmc_power_cycle(host, ocr);
1514 			if (!oldcard)
1515 				mmc_remove_card(card);
1516 			goto retry;
1517 		}
1518 		goto cont;
1519 	}
1520 
1521 	/* Initialization sequence for UHS-I cards */
1522 	if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) {
1523 		err = mmc_sd_init_uhs_card(card);
1524 		if (err)
1525 			goto free_card;
1526 	} else {
1527 		/*
1528 		 * Attempt to change to high-speed (if supported)
1529 		 */
1530 		err = mmc_sd_switch_hs(card);
1531 		if (err > 0)
1532 			mmc_set_timing(card->host, MMC_TIMING_SD_HS);
1533 		else if (err)
1534 			goto free_card;
1535 
1536 		/*
1537 		 * Set bus speed.
1538 		 */
1539 		mmc_set_clock(host, mmc_sd_get_max_clock(card));
1540 
1541 		if (host->ios.timing == MMC_TIMING_SD_HS &&
1542 			host->ops->prepare_sd_hs_tuning) {
1543 			err = host->ops->prepare_sd_hs_tuning(host, card);
1544 			if (err)
1545 				goto free_card;
1546 		}
1547 
1548 		/*
1549 		 * Switch to wider bus (if supported).
1550 		 */
1551 		if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1552 			(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1553 			err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1554 			if (err)
1555 				goto free_card;
1556 
1557 			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1558 		}
1559 
1560 		if (host->ios.timing == MMC_TIMING_SD_HS &&
1561 			host->ops->execute_sd_hs_tuning) {
1562 			err = host->ops->execute_sd_hs_tuning(host, card);
1563 			if (err)
1564 				goto free_card;
1565 		}
1566 	}
1567 cont:
1568 	if (!oldcard) {
1569 		/* Read/parse the extension registers. */
1570 		err = sd_read_ext_regs(card);
1571 		if (err)
1572 			goto free_card;
1573 	}
1574 
1575 	/* Enable internal SD cache if supported. */
1576 	if (card->ext_perf.feature_support & SD_EXT_PERF_CACHE) {
1577 		err = sd_enable_cache(card);
1578 		if (err)
1579 			goto free_card;
1580 	}
1581 
1582 	if (host->cqe_ops && !host->cqe_enabled) {
1583 		err = host->cqe_ops->cqe_enable(host, card);
1584 		if (!err) {
1585 			host->cqe_enabled = true;
1586 			host->hsq_enabled = true;
1587 			pr_info("%s: Host Software Queue enabled\n",
1588 				mmc_hostname(host));
1589 		}
1590 	}
1591 
1592 	if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1593 	    host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1594 		pr_err("%s: Host failed to negotiate down from 3.3V\n",
1595 			mmc_hostname(host));
1596 		err = -EINVAL;
1597 		goto free_card;
1598 	}
1599 
1600 	host->card = card;
1601 	return 0;
1602 
1603 free_card:
1604 	if (!oldcard)
1605 		mmc_remove_card(card);
1606 
1607 	return err;
1608 }
1609 
1610 /*
1611  * Host is being removed. Free up the current card.
1612  */
mmc_sd_remove(struct mmc_host * host)1613 static void mmc_sd_remove(struct mmc_host *host)
1614 {
1615 	mmc_remove_card(host->card);
1616 	host->card = NULL;
1617 }
1618 
1619 /*
1620  * Card detection - card is alive.
1621  */
mmc_sd_alive(struct mmc_host * host)1622 static int mmc_sd_alive(struct mmc_host *host)
1623 {
1624 	return mmc_send_status(host->card, NULL);
1625 }
1626 
1627 /*
1628  * Card detection callback from host.
1629  */
mmc_sd_detect(struct mmc_host * host)1630 static void mmc_sd_detect(struct mmc_host *host)
1631 {
1632 	int err;
1633 
1634 	mmc_get_card(host->card, NULL);
1635 
1636 	/*
1637 	 * Just check if our card has been removed.
1638 	 */
1639 	err = _mmc_detect_card_removed(host);
1640 
1641 	mmc_put_card(host->card, NULL);
1642 
1643 	if (err) {
1644 		mmc_sd_remove(host);
1645 
1646 		mmc_claim_host(host);
1647 		mmc_detach_bus(host);
1648 		mmc_power_off(host);
1649 		mmc_release_host(host);
1650 	}
1651 }
1652 
sd_can_poweroff_notify(struct mmc_card * card)1653 static int sd_can_poweroff_notify(struct mmc_card *card)
1654 {
1655 	return card->ext_power.feature_support & SD_EXT_POWER_OFF_NOTIFY;
1656 }
1657 
sd_busy_poweroff_notify_cb(void * cb_data,bool * busy)1658 static int sd_busy_poweroff_notify_cb(void *cb_data, bool *busy)
1659 {
1660 	struct sd_busy_data *data = cb_data;
1661 	struct mmc_card *card = data->card;
1662 	int err;
1663 
1664 	/*
1665 	 * Read the status register for the power management function. It's at
1666 	 * one byte offset and is one byte long. The Power Off Notification
1667 	 * Ready is bit 0.
1668 	 */
1669 	err = sd_read_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1670 			      card->ext_power.offset + 1, 1, data->reg_buf);
1671 	if (err) {
1672 		pr_warn("%s: error %d reading status reg of PM func\n",
1673 			mmc_hostname(card->host), err);
1674 		return err;
1675 	}
1676 
1677 	*busy = !(data->reg_buf[0] & BIT(0));
1678 	return 0;
1679 }
1680 
sd_poweroff_notify(struct mmc_card * card)1681 static int sd_poweroff_notify(struct mmc_card *card)
1682 {
1683 	struct sd_busy_data cb_data;
1684 	u8 *reg_buf;
1685 	int err;
1686 
1687 	reg_buf = kzalloc(512, GFP_KERNEL);
1688 	if (!reg_buf)
1689 		return -ENOMEM;
1690 
1691 	/*
1692 	 * Set the Power Off Notification bit in the power management settings
1693 	 * register at 2 bytes offset.
1694 	 */
1695 	err = sd_write_ext_reg(card, card->ext_power.fno, card->ext_power.page,
1696 			       card->ext_power.offset + 2, BIT(0));
1697 	if (err) {
1698 		pr_warn("%s: error %d writing Power Off Notify bit\n",
1699 			mmc_hostname(card->host), err);
1700 		goto out;
1701 	}
1702 
1703 	/* Find out when the command is completed. */
1704 	err = mmc_poll_for_busy(card, SD_WRITE_EXTR_SINGLE_TIMEOUT_MS, false,
1705 				MMC_BUSY_EXTR_SINGLE);
1706 	if (err)
1707 		goto out;
1708 
1709 	cb_data.card = card;
1710 	cb_data.reg_buf = reg_buf;
1711 	err = __mmc_poll_for_busy(card->host, 0, SD_POWEROFF_NOTIFY_TIMEOUT_MS,
1712 				  &sd_busy_poweroff_notify_cb, &cb_data);
1713 
1714 out:
1715 	kfree(reg_buf);
1716 	return err;
1717 }
1718 
_mmc_sd_suspend(struct mmc_host * host)1719 static int _mmc_sd_suspend(struct mmc_host *host)
1720 {
1721 	struct mmc_card *card = host->card;
1722 	int err = 0;
1723 
1724 	mmc_claim_host(host);
1725 
1726 	if (mmc_card_suspended(card))
1727 		goto out;
1728 
1729 	if (sd_can_poweroff_notify(card))
1730 		err = sd_poweroff_notify(card);
1731 	else if (!mmc_host_is_spi(host))
1732 		err = mmc_deselect_cards(host);
1733 
1734 	if (!err) {
1735 		mmc_power_off(host);
1736 		mmc_card_set_suspended(card);
1737 	}
1738 
1739 out:
1740 	mmc_release_host(host);
1741 	return err;
1742 }
1743 
1744 /*
1745  * Callback for suspend
1746  */
mmc_sd_suspend(struct mmc_host * host)1747 static int mmc_sd_suspend(struct mmc_host *host)
1748 {
1749 	int err;
1750 
1751 	err = _mmc_sd_suspend(host);
1752 	if (!err) {
1753 		pm_runtime_disable(&host->card->dev);
1754 		pm_runtime_set_suspended(&host->card->dev);
1755 	}
1756 
1757 	return err;
1758 }
1759 
1760 /*
1761  * This function tries to determine if the same card is still present
1762  * and, if so, restore all state to it.
1763  */
_mmc_sd_resume(struct mmc_host * host)1764 static int _mmc_sd_resume(struct mmc_host *host)
1765 {
1766 	int err = 0;
1767 
1768 	mmc_claim_host(host);
1769 
1770 	if (!mmc_card_suspended(host->card))
1771 		goto out;
1772 
1773 	mmc_power_up(host, host->card->ocr);
1774 	err = mmc_sd_init_card(host, host->card->ocr, host->card);
1775 	mmc_card_clr_suspended(host->card);
1776 
1777 out:
1778 	mmc_release_host(host);
1779 	return err;
1780 }
1781 
1782 /*
1783  * Callback for resume
1784  */
mmc_sd_resume(struct mmc_host * host)1785 static int mmc_sd_resume(struct mmc_host *host)
1786 {
1787 	pm_runtime_enable(&host->card->dev);
1788 	return 0;
1789 }
1790 
1791 /*
1792  * Callback for runtime_suspend.
1793  */
mmc_sd_runtime_suspend(struct mmc_host * host)1794 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1795 {
1796 	int err;
1797 
1798 	if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1799 		return 0;
1800 
1801 	err = _mmc_sd_suspend(host);
1802 	if (err)
1803 		pr_err("%s: error %d doing aggressive suspend\n",
1804 			mmc_hostname(host), err);
1805 
1806 	return err;
1807 }
1808 
1809 /*
1810  * Callback for runtime_resume.
1811  */
mmc_sd_runtime_resume(struct mmc_host * host)1812 static int mmc_sd_runtime_resume(struct mmc_host *host)
1813 {
1814 	int err;
1815 
1816 	err = _mmc_sd_resume(host);
1817 	if (err && err != -ENOMEDIUM)
1818 		pr_err("%s: error %d doing runtime resume\n",
1819 			mmc_hostname(host), err);
1820 
1821 	return 0;
1822 }
1823 
mmc_sd_hw_reset(struct mmc_host * host)1824 static int mmc_sd_hw_reset(struct mmc_host *host)
1825 {
1826 	mmc_power_cycle(host, host->card->ocr);
1827 	return mmc_sd_init_card(host, host->card->ocr, host->card);
1828 }
1829 
1830 static const struct mmc_bus_ops mmc_sd_ops = {
1831 	.remove = mmc_sd_remove,
1832 	.detect = mmc_sd_detect,
1833 	.runtime_suspend = mmc_sd_runtime_suspend,
1834 	.runtime_resume = mmc_sd_runtime_resume,
1835 	.suspend = mmc_sd_suspend,
1836 	.resume = mmc_sd_resume,
1837 	.alive = mmc_sd_alive,
1838 	.shutdown = mmc_sd_suspend,
1839 	.hw_reset = mmc_sd_hw_reset,
1840 	.cache_enabled = sd_cache_enabled,
1841 	.flush_cache = sd_flush_cache,
1842 };
1843 
1844 /*
1845  * Starting point for SD card init.
1846  */
mmc_attach_sd(struct mmc_host * host)1847 int mmc_attach_sd(struct mmc_host *host)
1848 {
1849 	int err;
1850 	u32 ocr, rocr;
1851 
1852 	WARN_ON(!host->claimed);
1853 
1854 	err = mmc_send_app_op_cond(host, 0, &ocr);
1855 	if (err)
1856 		return err;
1857 
1858 	mmc_attach_bus(host, &mmc_sd_ops);
1859 	if (host->ocr_avail_sd)
1860 		host->ocr_avail = host->ocr_avail_sd;
1861 
1862 	/*
1863 	 * We need to get OCR a different way for SPI.
1864 	 */
1865 	if (mmc_host_is_spi(host)) {
1866 		mmc_go_idle(host);
1867 
1868 		err = mmc_spi_read_ocr(host, 0, &ocr);
1869 		if (err)
1870 			goto err;
1871 	}
1872 
1873 	/*
1874 	 * Some SD cards claims an out of spec VDD voltage range. Let's treat
1875 	 * these bits as being in-valid and especially also bit7.
1876 	 */
1877 	ocr &= ~0x7FFF;
1878 
1879 	rocr = mmc_select_voltage(host, ocr);
1880 
1881 	/*
1882 	 * Can we support the voltage(s) of the card(s)?
1883 	 */
1884 	if (!rocr) {
1885 		err = -EINVAL;
1886 		goto err;
1887 	}
1888 
1889 	/*
1890 	 * Detect and init the card.
1891 	 */
1892 	err = mmc_sd_init_card(host, rocr, NULL);
1893 	if (err)
1894 		goto err;
1895 
1896 	mmc_release_host(host);
1897 	err = mmc_add_card(host->card);
1898 	if (err)
1899 		goto remove_card;
1900 
1901 	mmc_claim_host(host);
1902 	return 0;
1903 
1904 remove_card:
1905 	mmc_remove_card(host->card);
1906 	host->card = NULL;
1907 	mmc_claim_host(host);
1908 err:
1909 	mmc_detach_bus(host);
1910 
1911 	pr_err("%s: error %d whilst initialising SD card\n",
1912 		mmc_hostname(host), err);
1913 
1914 	return err;
1915 }
1916