xref: /openbmc/linux/drivers/mmc/core/sd.c (revision 31b90347)
1 /*
2  *  linux/drivers/mmc/core/sd.c
3  *
4  *  Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5  *  SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6  *  Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  */
12 
13 #include <linux/err.h>
14 #include <linux/slab.h>
15 #include <linux/stat.h>
16 #include <linux/pm_runtime.h>
17 
18 #include <linux/mmc/host.h>
19 #include <linux/mmc/card.h>
20 #include <linux/mmc/mmc.h>
21 #include <linux/mmc/sd.h>
22 
23 #include "core.h"
24 #include "bus.h"
25 #include "mmc_ops.h"
26 #include "sd.h"
27 #include "sd_ops.h"
28 
29 static const unsigned int tran_exp[] = {
30 	10000,		100000,		1000000,	10000000,
31 	0,		0,		0,		0
32 };
33 
34 static const unsigned char tran_mant[] = {
35 	0,	10,	12,	13,	15,	20,	25,	30,
36 	35,	40,	45,	50,	55,	60,	70,	80,
37 };
38 
39 static const unsigned int tacc_exp[] = {
40 	1,	10,	100,	1000,	10000,	100000,	1000000, 10000000,
41 };
42 
43 static const unsigned int tacc_mant[] = {
44 	0,	10,	12,	13,	15,	20,	25,	30,
45 	35,	40,	45,	50,	55,	60,	70,	80,
46 };
47 
48 #define UNSTUFF_BITS(resp,start,size)					\
49 	({								\
50 		const int __size = size;				\
51 		const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1;	\
52 		const int __off = 3 - ((start) / 32);			\
53 		const int __shft = (start) & 31;			\
54 		u32 __res;						\
55 									\
56 		__res = resp[__off] >> __shft;				\
57 		if (__size + __shft > 32)				\
58 			__res |= resp[__off-1] << ((32 - __shft) % 32);	\
59 		__res & __mask;						\
60 	})
61 
62 /*
63  * Given the decoded CSD structure, decode the raw CID to our CID structure.
64  */
65 void mmc_decode_cid(struct mmc_card *card)
66 {
67 	u32 *resp = card->raw_cid;
68 
69 	memset(&card->cid, 0, sizeof(struct mmc_cid));
70 
71 	/*
72 	 * SD doesn't currently have a version field so we will
73 	 * have to assume we can parse this.
74 	 */
75 	card->cid.manfid		= UNSTUFF_BITS(resp, 120, 8);
76 	card->cid.oemid			= UNSTUFF_BITS(resp, 104, 16);
77 	card->cid.prod_name[0]		= UNSTUFF_BITS(resp, 96, 8);
78 	card->cid.prod_name[1]		= UNSTUFF_BITS(resp, 88, 8);
79 	card->cid.prod_name[2]		= UNSTUFF_BITS(resp, 80, 8);
80 	card->cid.prod_name[3]		= UNSTUFF_BITS(resp, 72, 8);
81 	card->cid.prod_name[4]		= UNSTUFF_BITS(resp, 64, 8);
82 	card->cid.hwrev			= UNSTUFF_BITS(resp, 60, 4);
83 	card->cid.fwrev			= UNSTUFF_BITS(resp, 56, 4);
84 	card->cid.serial		= UNSTUFF_BITS(resp, 24, 32);
85 	card->cid.year			= UNSTUFF_BITS(resp, 12, 8);
86 	card->cid.month			= UNSTUFF_BITS(resp, 8, 4);
87 
88 	card->cid.year += 2000; /* SD cards year offset */
89 }
90 
91 /*
92  * Given a 128-bit response, decode to our card CSD structure.
93  */
94 static int mmc_decode_csd(struct mmc_card *card)
95 {
96 	struct mmc_csd *csd = &card->csd;
97 	unsigned int e, m, csd_struct;
98 	u32 *resp = card->raw_csd;
99 
100 	csd_struct = UNSTUFF_BITS(resp, 126, 2);
101 
102 	switch (csd_struct) {
103 	case 0:
104 		m = UNSTUFF_BITS(resp, 115, 4);
105 		e = UNSTUFF_BITS(resp, 112, 3);
106 		csd->tacc_ns	 = (tacc_exp[e] * tacc_mant[m] + 9) / 10;
107 		csd->tacc_clks	 = UNSTUFF_BITS(resp, 104, 8) * 100;
108 
109 		m = UNSTUFF_BITS(resp, 99, 4);
110 		e = UNSTUFF_BITS(resp, 96, 3);
111 		csd->max_dtr	  = tran_exp[e] * tran_mant[m];
112 		csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
113 
114 		e = UNSTUFF_BITS(resp, 47, 3);
115 		m = UNSTUFF_BITS(resp, 62, 12);
116 		csd->capacity	  = (1 + m) << (e + 2);
117 
118 		csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
119 		csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
120 		csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
121 		csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
122 		csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
123 		csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
124 		csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
125 
126 		if (UNSTUFF_BITS(resp, 46, 1)) {
127 			csd->erase_size = 1;
128 		} else if (csd->write_blkbits >= 9) {
129 			csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
130 			csd->erase_size <<= csd->write_blkbits - 9;
131 		}
132 		break;
133 	case 1:
134 		/*
135 		 * This is a block-addressed SDHC or SDXC card. Most
136 		 * interesting fields are unused and have fixed
137 		 * values. To avoid getting tripped by buggy cards,
138 		 * we assume those fixed values ourselves.
139 		 */
140 		mmc_card_set_blockaddr(card);
141 
142 		csd->tacc_ns	 = 0; /* Unused */
143 		csd->tacc_clks	 = 0; /* Unused */
144 
145 		m = UNSTUFF_BITS(resp, 99, 4);
146 		e = UNSTUFF_BITS(resp, 96, 3);
147 		csd->max_dtr	  = tran_exp[e] * tran_mant[m];
148 		csd->cmdclass	  = UNSTUFF_BITS(resp, 84, 12);
149 		csd->c_size	  = UNSTUFF_BITS(resp, 48, 22);
150 
151 		/* SDXC cards have a minimum C_SIZE of 0x00FFFF */
152 		if (csd->c_size >= 0xFFFF)
153 			mmc_card_set_ext_capacity(card);
154 
155 		m = UNSTUFF_BITS(resp, 48, 22);
156 		csd->capacity     = (1 + m) << 10;
157 
158 		csd->read_blkbits = 9;
159 		csd->read_partial = 0;
160 		csd->write_misalign = 0;
161 		csd->read_misalign = 0;
162 		csd->r2w_factor = 4; /* Unused */
163 		csd->write_blkbits = 9;
164 		csd->write_partial = 0;
165 		csd->erase_size = 1;
166 		break;
167 	default:
168 		pr_err("%s: unrecognised CSD structure version %d\n",
169 			mmc_hostname(card->host), csd_struct);
170 		return -EINVAL;
171 	}
172 
173 	card->erase_size = csd->erase_size;
174 
175 	return 0;
176 }
177 
178 /*
179  * Given a 64-bit response, decode to our card SCR structure.
180  */
181 static int mmc_decode_scr(struct mmc_card *card)
182 {
183 	struct sd_scr *scr = &card->scr;
184 	unsigned int scr_struct;
185 	u32 resp[4];
186 
187 	resp[3] = card->raw_scr[1];
188 	resp[2] = card->raw_scr[0];
189 
190 	scr_struct = UNSTUFF_BITS(resp, 60, 4);
191 	if (scr_struct != 0) {
192 		pr_err("%s: unrecognised SCR structure version %d\n",
193 			mmc_hostname(card->host), scr_struct);
194 		return -EINVAL;
195 	}
196 
197 	scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
198 	scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
199 	if (scr->sda_vsn == SCR_SPEC_VER_2)
200 		/* Check if Physical Layer Spec v3.0 is supported */
201 		scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
202 
203 	if (UNSTUFF_BITS(resp, 55, 1))
204 		card->erased_byte = 0xFF;
205 	else
206 		card->erased_byte = 0x0;
207 
208 	if (scr->sda_spec3)
209 		scr->cmds = UNSTUFF_BITS(resp, 32, 2);
210 	return 0;
211 }
212 
213 /*
214  * Fetch and process SD Status register.
215  */
216 static int mmc_read_ssr(struct mmc_card *card)
217 {
218 	unsigned int au, es, et, eo;
219 	int err, i, max_au;
220 	u32 *ssr;
221 
222 	if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
223 		pr_warning("%s: card lacks mandatory SD Status "
224 			"function.\n", mmc_hostname(card->host));
225 		return 0;
226 	}
227 
228 	ssr = kmalloc(64, GFP_KERNEL);
229 	if (!ssr)
230 		return -ENOMEM;
231 
232 	err = mmc_app_sd_status(card, ssr);
233 	if (err) {
234 		pr_warning("%s: problem reading SD Status "
235 			"register.\n", mmc_hostname(card->host));
236 		err = 0;
237 		goto out;
238 	}
239 
240 	for (i = 0; i < 16; i++)
241 		ssr[i] = be32_to_cpu(ssr[i]);
242 
243 	/* SD3.0 increases max AU size to 64MB (0xF) from 4MB (0x9) */
244 	max_au = card->scr.sda_spec3 ? 0xF : 0x9;
245 
246 	/*
247 	 * UNSTUFF_BITS only works with four u32s so we have to offset the
248 	 * bitfield positions accordingly.
249 	 */
250 	au = UNSTUFF_BITS(ssr, 428 - 384, 4);
251 	if (au > 0 && au <= max_au) {
252 		card->ssr.au = 1 << (au + 4);
253 		es = UNSTUFF_BITS(ssr, 408 - 384, 16);
254 		et = UNSTUFF_BITS(ssr, 402 - 384, 6);
255 		eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
256 		if (es && et) {
257 			card->ssr.erase_timeout = (et * 1000) / es;
258 			card->ssr.erase_offset = eo * 1000;
259 		}
260 	} else {
261 		pr_warning("%s: SD Status: Invalid Allocation Unit "
262 			"size.\n", mmc_hostname(card->host));
263 	}
264 out:
265 	kfree(ssr);
266 	return err;
267 }
268 
269 /*
270  * Fetches and decodes switch information
271  */
272 static int mmc_read_switch(struct mmc_card *card)
273 {
274 	int err;
275 	u8 *status;
276 
277 	if (card->scr.sda_vsn < SCR_SPEC_VER_1)
278 		return 0;
279 
280 	if (!(card->csd.cmdclass & CCC_SWITCH)) {
281 		pr_warning("%s: card lacks mandatory switch "
282 			"function, performance might suffer.\n",
283 			mmc_hostname(card->host));
284 		return 0;
285 	}
286 
287 	err = -EIO;
288 
289 	status = kmalloc(64, GFP_KERNEL);
290 	if (!status) {
291 		pr_err("%s: could not allocate a buffer for "
292 			"switch capabilities.\n",
293 			mmc_hostname(card->host));
294 		return -ENOMEM;
295 	}
296 
297 	/*
298 	 * Find out the card's support bits with a mode 0 operation.
299 	 * The argument does not matter, as the support bits do not
300 	 * change with the arguments.
301 	 */
302 	err = mmc_sd_switch(card, 0, 0, 0, status);
303 	if (err) {
304 		/*
305 		 * If the host or the card can't do the switch,
306 		 * fail more gracefully.
307 		 */
308 		if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
309 			goto out;
310 
311 		pr_warning("%s: problem reading Bus Speed modes.\n",
312 			mmc_hostname(card->host));
313 		err = 0;
314 
315 		goto out;
316 	}
317 
318 	if (status[13] & SD_MODE_HIGH_SPEED)
319 		card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
320 
321 	if (card->scr.sda_spec3) {
322 		card->sw_caps.sd3_bus_mode = status[13];
323 		/* Driver Strengths supported by the card */
324 		card->sw_caps.sd3_drv_type = status[9];
325 	}
326 
327 out:
328 	kfree(status);
329 
330 	return err;
331 }
332 
333 /*
334  * Test if the card supports high-speed mode and, if so, switch to it.
335  */
336 int mmc_sd_switch_hs(struct mmc_card *card)
337 {
338 	int err;
339 	u8 *status;
340 
341 	if (card->scr.sda_vsn < SCR_SPEC_VER_1)
342 		return 0;
343 
344 	if (!(card->csd.cmdclass & CCC_SWITCH))
345 		return 0;
346 
347 	if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
348 		return 0;
349 
350 	if (card->sw_caps.hs_max_dtr == 0)
351 		return 0;
352 
353 	err = -EIO;
354 
355 	status = kmalloc(64, GFP_KERNEL);
356 	if (!status) {
357 		pr_err("%s: could not allocate a buffer for "
358 			"switch capabilities.\n", mmc_hostname(card->host));
359 		return -ENOMEM;
360 	}
361 
362 	err = mmc_sd_switch(card, 1, 0, 1, status);
363 	if (err)
364 		goto out;
365 
366 	if ((status[16] & 0xF) != 1) {
367 		pr_warning("%s: Problem switching card "
368 			"into high-speed mode!\n",
369 			mmc_hostname(card->host));
370 		err = 0;
371 	} else {
372 		err = 1;
373 	}
374 
375 out:
376 	kfree(status);
377 
378 	return err;
379 }
380 
381 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
382 {
383 	int host_drv_type = SD_DRIVER_TYPE_B;
384 	int card_drv_type = SD_DRIVER_TYPE_B;
385 	int drive_strength;
386 	int err;
387 
388 	/*
389 	 * If the host doesn't support any of the Driver Types A,C or D,
390 	 * or there is no board specific handler then default Driver
391 	 * Type B is used.
392 	 */
393 	if (!(card->host->caps & (MMC_CAP_DRIVER_TYPE_A | MMC_CAP_DRIVER_TYPE_C
394 	    | MMC_CAP_DRIVER_TYPE_D)))
395 		return 0;
396 
397 	if (!card->host->ops->select_drive_strength)
398 		return 0;
399 
400 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
401 		host_drv_type |= SD_DRIVER_TYPE_A;
402 
403 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
404 		host_drv_type |= SD_DRIVER_TYPE_C;
405 
406 	if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
407 		host_drv_type |= SD_DRIVER_TYPE_D;
408 
409 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
410 		card_drv_type |= SD_DRIVER_TYPE_A;
411 
412 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
413 		card_drv_type |= SD_DRIVER_TYPE_C;
414 
415 	if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
416 		card_drv_type |= SD_DRIVER_TYPE_D;
417 
418 	/*
419 	 * The drive strength that the hardware can support
420 	 * depends on the board design.  Pass the appropriate
421 	 * information and let the hardware specific code
422 	 * return what is possible given the options
423 	 */
424 	mmc_host_clk_hold(card->host);
425 	drive_strength = card->host->ops->select_drive_strength(
426 		card->sw_caps.uhs_max_dtr,
427 		host_drv_type, card_drv_type);
428 	mmc_host_clk_release(card->host);
429 
430 	err = mmc_sd_switch(card, 1, 2, drive_strength, status);
431 	if (err)
432 		return err;
433 
434 	if ((status[15] & 0xF) != drive_strength) {
435 		pr_warning("%s: Problem setting drive strength!\n",
436 			mmc_hostname(card->host));
437 		return 0;
438 	}
439 
440 	mmc_set_driver_type(card->host, drive_strength);
441 
442 	return 0;
443 }
444 
445 static void sd_update_bus_speed_mode(struct mmc_card *card)
446 {
447 	/*
448 	 * If the host doesn't support any of the UHS-I modes, fallback on
449 	 * default speed.
450 	 */
451 	if (!mmc_host_uhs(card->host)) {
452 		card->sd_bus_speed = 0;
453 		return;
454 	}
455 
456 	if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
457 	    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
458 			card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
459 	} else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
460 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
461 			card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
462 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
463 		    MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
464 		    SD_MODE_UHS_SDR50)) {
465 			card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
466 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
467 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
468 		   (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
469 			card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
470 	} else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
471 		    MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
472 		    MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
473 		    SD_MODE_UHS_SDR12)) {
474 			card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
475 	}
476 }
477 
478 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
479 {
480 	int err;
481 	unsigned int timing = 0;
482 
483 	switch (card->sd_bus_speed) {
484 	case UHS_SDR104_BUS_SPEED:
485 		timing = MMC_TIMING_UHS_SDR104;
486 		card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
487 		break;
488 	case UHS_DDR50_BUS_SPEED:
489 		timing = MMC_TIMING_UHS_DDR50;
490 		card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
491 		break;
492 	case UHS_SDR50_BUS_SPEED:
493 		timing = MMC_TIMING_UHS_SDR50;
494 		card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
495 		break;
496 	case UHS_SDR25_BUS_SPEED:
497 		timing = MMC_TIMING_UHS_SDR25;
498 		card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
499 		break;
500 	case UHS_SDR12_BUS_SPEED:
501 		timing = MMC_TIMING_UHS_SDR12;
502 		card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
503 		break;
504 	default:
505 		return 0;
506 	}
507 
508 	err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
509 	if (err)
510 		return err;
511 
512 	if ((status[16] & 0xF) != card->sd_bus_speed)
513 		pr_warning("%s: Problem setting bus speed mode!\n",
514 			mmc_hostname(card->host));
515 	else {
516 		mmc_set_timing(card->host, timing);
517 		mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
518 	}
519 
520 	return 0;
521 }
522 
523 /* Get host's max current setting at its current voltage */
524 static u32 sd_get_host_max_current(struct mmc_host *host)
525 {
526 	u32 voltage, max_current;
527 
528 	voltage = 1 << host->ios.vdd;
529 	switch (voltage) {
530 	case MMC_VDD_165_195:
531 		max_current = host->max_current_180;
532 		break;
533 	case MMC_VDD_29_30:
534 	case MMC_VDD_30_31:
535 		max_current = host->max_current_300;
536 		break;
537 	case MMC_VDD_32_33:
538 	case MMC_VDD_33_34:
539 		max_current = host->max_current_330;
540 		break;
541 	default:
542 		max_current = 0;
543 	}
544 
545 	return max_current;
546 }
547 
548 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
549 {
550 	int current_limit = SD_SET_CURRENT_NO_CHANGE;
551 	int err;
552 	u32 max_current;
553 
554 	/*
555 	 * Current limit switch is only defined for SDR50, SDR104, and DDR50
556 	 * bus speed modes. For other bus speed modes, we do not change the
557 	 * current limit.
558 	 */
559 	if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
560 	    (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
561 	    (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
562 		return 0;
563 
564 	/*
565 	 * Host has different current capabilities when operating at
566 	 * different voltages, so find out its max current first.
567 	 */
568 	max_current = sd_get_host_max_current(card->host);
569 
570 	/*
571 	 * We only check host's capability here, if we set a limit that is
572 	 * higher than the card's maximum current, the card will be using its
573 	 * maximum current, e.g. if the card's maximum current is 300ma, and
574 	 * when we set current limit to 200ma, the card will draw 200ma, and
575 	 * when we set current limit to 400/600/800ma, the card will draw its
576 	 * maximum 300ma from the host.
577 	 */
578 	if (max_current >= 800)
579 		current_limit = SD_SET_CURRENT_LIMIT_800;
580 	else if (max_current >= 600)
581 		current_limit = SD_SET_CURRENT_LIMIT_600;
582 	else if (max_current >= 400)
583 		current_limit = SD_SET_CURRENT_LIMIT_400;
584 	else if (max_current >= 200)
585 		current_limit = SD_SET_CURRENT_LIMIT_200;
586 
587 	if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
588 		err = mmc_sd_switch(card, 1, 3, current_limit, status);
589 		if (err)
590 			return err;
591 
592 		if (((status[15] >> 4) & 0x0F) != current_limit)
593 			pr_warning("%s: Problem setting current limit!\n",
594 				mmc_hostname(card->host));
595 
596 	}
597 
598 	return 0;
599 }
600 
601 /*
602  * UHS-I specific initialization procedure
603  */
604 static int mmc_sd_init_uhs_card(struct mmc_card *card)
605 {
606 	int err;
607 	u8 *status;
608 
609 	if (!card->scr.sda_spec3)
610 		return 0;
611 
612 	if (!(card->csd.cmdclass & CCC_SWITCH))
613 		return 0;
614 
615 	status = kmalloc(64, GFP_KERNEL);
616 	if (!status) {
617 		pr_err("%s: could not allocate a buffer for "
618 			"switch capabilities.\n", mmc_hostname(card->host));
619 		return -ENOMEM;
620 	}
621 
622 	/* Set 4-bit bus width */
623 	if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
624 	    (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
625 		err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
626 		if (err)
627 			goto out;
628 
629 		mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
630 	}
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) && card->host->ops->execute_tuning &&
658 			(card->sd_bus_speed == UHS_SDR50_BUS_SPEED ||
659 			 card->sd_bus_speed == UHS_SDR104_BUS_SPEED)) {
660 		mmc_host_clk_hold(card->host);
661 		err = card->host->ops->execute_tuning(card->host,
662 						      MMC_SEND_TUNING_BLOCK);
663 		mmc_host_clk_release(card->host);
664 	}
665 
666 out:
667 	kfree(status);
668 
669 	return err;
670 }
671 
672 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
673 	card->raw_cid[2], card->raw_cid[3]);
674 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
675 	card->raw_csd[2], card->raw_csd[3]);
676 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
677 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
678 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
679 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
680 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
681 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
682 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
683 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
684 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
685 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
686 
687 
688 static struct attribute *sd_std_attrs[] = {
689 	&dev_attr_cid.attr,
690 	&dev_attr_csd.attr,
691 	&dev_attr_scr.attr,
692 	&dev_attr_date.attr,
693 	&dev_attr_erase_size.attr,
694 	&dev_attr_preferred_erase_size.attr,
695 	&dev_attr_fwrev.attr,
696 	&dev_attr_hwrev.attr,
697 	&dev_attr_manfid.attr,
698 	&dev_attr_name.attr,
699 	&dev_attr_oemid.attr,
700 	&dev_attr_serial.attr,
701 	NULL,
702 };
703 
704 static struct attribute_group sd_std_attr_group = {
705 	.attrs = sd_std_attrs,
706 };
707 
708 static const struct attribute_group *sd_attr_groups[] = {
709 	&sd_std_attr_group,
710 	NULL,
711 };
712 
713 struct device_type sd_type = {
714 	.groups = sd_attr_groups,
715 };
716 
717 /*
718  * Fetch CID from card.
719  */
720 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
721 {
722 	int err;
723 	u32 max_current;
724 	int retries = 10;
725 	u32 pocr = ocr;
726 
727 try_again:
728 	if (!retries) {
729 		ocr &= ~SD_OCR_S18R;
730 		pr_warning("%s: Skipping voltage switch\n",
731 			mmc_hostname(host));
732 	}
733 
734 	/*
735 	 * Since we're changing the OCR value, we seem to
736 	 * need to tell some cards to go back to the idle
737 	 * state.  We wait 1ms to give cards time to
738 	 * respond.
739 	 */
740 	mmc_go_idle(host);
741 
742 	/*
743 	 * If SD_SEND_IF_COND indicates an SD 2.0
744 	 * compliant card and we should set bit 30
745 	 * of the ocr to indicate that we can handle
746 	 * block-addressed SDHC cards.
747 	 */
748 	err = mmc_send_if_cond(host, ocr);
749 	if (!err)
750 		ocr |= SD_OCR_CCS;
751 
752 	/*
753 	 * If the host supports one of UHS-I modes, request the card
754 	 * to switch to 1.8V signaling level. If the card has failed
755 	 * repeatedly to switch however, skip this.
756 	 */
757 	if (retries && mmc_host_uhs(host))
758 		ocr |= SD_OCR_S18R;
759 
760 	/*
761 	 * If the host can supply more than 150mA at current voltage,
762 	 * XPC should be set to 1.
763 	 */
764 	max_current = sd_get_host_max_current(host);
765 	if (max_current > 150)
766 		ocr |= SD_OCR_XPC;
767 
768 	err = mmc_send_app_op_cond(host, ocr, rocr);
769 	if (err)
770 		return err;
771 
772 	/*
773 	 * In case CCS and S18A in the response is set, start Signal Voltage
774 	 * Switch procedure. SPI mode doesn't support CMD11.
775 	 */
776 	if (!mmc_host_is_spi(host) && rocr &&
777 	   ((*rocr & 0x41000000) == 0x41000000)) {
778 		err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
779 					pocr);
780 		if (err == -EAGAIN) {
781 			retries--;
782 			goto try_again;
783 		} else if (err) {
784 			retries = 0;
785 			goto try_again;
786 		}
787 	}
788 
789 	if (mmc_host_is_spi(host))
790 		err = mmc_send_cid(host, cid);
791 	else
792 		err = mmc_all_send_cid(host, cid);
793 
794 	return err;
795 }
796 
797 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
798 {
799 	int err;
800 
801 	/*
802 	 * Fetch CSD from card.
803 	 */
804 	err = mmc_send_csd(card, card->raw_csd);
805 	if (err)
806 		return err;
807 
808 	err = mmc_decode_csd(card);
809 	if (err)
810 		return err;
811 
812 	return 0;
813 }
814 
815 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
816 	bool reinit)
817 {
818 	int err;
819 
820 	if (!reinit) {
821 		/*
822 		 * Fetch SCR from card.
823 		 */
824 		err = mmc_app_send_scr(card, card->raw_scr);
825 		if (err)
826 			return err;
827 
828 		err = mmc_decode_scr(card);
829 		if (err)
830 			return err;
831 
832 		/*
833 		 * Fetch and process SD Status register.
834 		 */
835 		err = mmc_read_ssr(card);
836 		if (err)
837 			return err;
838 
839 		/* Erase init depends on CSD and SSR */
840 		mmc_init_erase(card);
841 
842 		/*
843 		 * Fetch switch information from card.
844 		 */
845 		err = mmc_read_switch(card);
846 		if (err)
847 			return err;
848 	}
849 
850 	/*
851 	 * For SPI, enable CRC as appropriate.
852 	 * This CRC enable is located AFTER the reading of the
853 	 * card registers because some SDHC cards are not able
854 	 * to provide valid CRCs for non-512-byte blocks.
855 	 */
856 	if (mmc_host_is_spi(host)) {
857 		err = mmc_spi_set_crc(host, use_spi_crc);
858 		if (err)
859 			return err;
860 	}
861 
862 	/*
863 	 * Check if read-only switch is active.
864 	 */
865 	if (!reinit) {
866 		int ro = -1;
867 
868 		if (host->ops->get_ro) {
869 			mmc_host_clk_hold(card->host);
870 			ro = host->ops->get_ro(host);
871 			mmc_host_clk_release(card->host);
872 		}
873 
874 		if (ro < 0) {
875 			pr_warning("%s: host does not "
876 				"support reading read-only "
877 				"switch. assuming write-enable.\n",
878 				mmc_hostname(host));
879 		} else if (ro > 0) {
880 			mmc_card_set_readonly(card);
881 		}
882 	}
883 
884 	return 0;
885 }
886 
887 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
888 {
889 	unsigned max_dtr = (unsigned int)-1;
890 
891 	if (mmc_card_highspeed(card)) {
892 		if (max_dtr > card->sw_caps.hs_max_dtr)
893 			max_dtr = card->sw_caps.hs_max_dtr;
894 	} else if (max_dtr > card->csd.max_dtr) {
895 		max_dtr = card->csd.max_dtr;
896 	}
897 
898 	return max_dtr;
899 }
900 
901 void mmc_sd_go_highspeed(struct mmc_card *card)
902 {
903 	mmc_card_set_highspeed(card);
904 	mmc_set_timing(card->host, MMC_TIMING_SD_HS);
905 }
906 
907 /*
908  * Handle the detection and initialisation of a card.
909  *
910  * In the case of a resume, "oldcard" will contain the card
911  * we're trying to reinitialise.
912  */
913 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
914 	struct mmc_card *oldcard)
915 {
916 	struct mmc_card *card;
917 	int err;
918 	u32 cid[4];
919 	u32 rocr = 0;
920 
921 	BUG_ON(!host);
922 	WARN_ON(!host->claimed);
923 
924 	err = mmc_sd_get_cid(host, ocr, cid, &rocr);
925 	if (err)
926 		return err;
927 
928 	if (oldcard) {
929 		if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0)
930 			return -ENOENT;
931 
932 		card = oldcard;
933 	} else {
934 		/*
935 		 * Allocate card structure.
936 		 */
937 		card = mmc_alloc_card(host, &sd_type);
938 		if (IS_ERR(card))
939 			return PTR_ERR(card);
940 
941 		card->ocr = ocr;
942 		card->type = MMC_TYPE_SD;
943 		memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
944 	}
945 
946 	/*
947 	 * For native busses:  get card RCA and quit open drain mode.
948 	 */
949 	if (!mmc_host_is_spi(host)) {
950 		err = mmc_send_relative_addr(host, &card->rca);
951 		if (err)
952 			goto free_card;
953 	}
954 
955 	if (!oldcard) {
956 		err = mmc_sd_get_csd(host, card);
957 		if (err)
958 			goto free_card;
959 
960 		mmc_decode_cid(card);
961 	}
962 
963 	/*
964 	 * Select card, as all following commands rely on that.
965 	 */
966 	if (!mmc_host_is_spi(host)) {
967 		err = mmc_select_card(card);
968 		if (err)
969 			goto free_card;
970 	}
971 
972 	err = mmc_sd_setup_card(host, card, oldcard != NULL);
973 	if (err)
974 		goto free_card;
975 
976 	/* Initialization sequence for UHS-I cards */
977 	if (rocr & SD_ROCR_S18A) {
978 		err = mmc_sd_init_uhs_card(card);
979 		if (err)
980 			goto free_card;
981 
982 		/* Card is an ultra-high-speed card */
983 		mmc_card_set_uhs(card);
984 	} else {
985 		/*
986 		 * Attempt to change to high-speed (if supported)
987 		 */
988 		err = mmc_sd_switch_hs(card);
989 		if (err > 0)
990 			mmc_sd_go_highspeed(card);
991 		else if (err)
992 			goto free_card;
993 
994 		/*
995 		 * Set bus speed.
996 		 */
997 		mmc_set_clock(host, mmc_sd_get_max_clock(card));
998 
999 		/*
1000 		 * Switch to wider bus (if supported).
1001 		 */
1002 		if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1003 			(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1004 			err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1005 			if (err)
1006 				goto free_card;
1007 
1008 			mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1009 		}
1010 	}
1011 
1012 	host->card = card;
1013 	return 0;
1014 
1015 free_card:
1016 	if (!oldcard)
1017 		mmc_remove_card(card);
1018 
1019 	return err;
1020 }
1021 
1022 /*
1023  * Host is being removed. Free up the current card.
1024  */
1025 static void mmc_sd_remove(struct mmc_host *host)
1026 {
1027 	BUG_ON(!host);
1028 	BUG_ON(!host->card);
1029 
1030 	mmc_remove_card(host->card);
1031 	host->card = NULL;
1032 }
1033 
1034 /*
1035  * Card detection - card is alive.
1036  */
1037 static int mmc_sd_alive(struct mmc_host *host)
1038 {
1039 	return mmc_send_status(host->card, NULL);
1040 }
1041 
1042 /*
1043  * Card detection callback from host.
1044  */
1045 static void mmc_sd_detect(struct mmc_host *host)
1046 {
1047 	int err;
1048 
1049 	BUG_ON(!host);
1050 	BUG_ON(!host->card);
1051 
1052 	mmc_get_card(host->card);
1053 
1054 	/*
1055 	 * Just check if our card has been removed.
1056 	 */
1057 	err = _mmc_detect_card_removed(host);
1058 
1059 	mmc_put_card(host->card);
1060 
1061 	if (err) {
1062 		mmc_sd_remove(host);
1063 
1064 		mmc_claim_host(host);
1065 		mmc_detach_bus(host);
1066 		mmc_power_off(host);
1067 		mmc_release_host(host);
1068 	}
1069 }
1070 
1071 static int _mmc_sd_suspend(struct mmc_host *host)
1072 {
1073 	int err = 0;
1074 
1075 	BUG_ON(!host);
1076 	BUG_ON(!host->card);
1077 
1078 	mmc_claim_host(host);
1079 
1080 	if (mmc_card_suspended(host->card))
1081 		goto out;
1082 
1083 	if (!mmc_host_is_spi(host))
1084 		err = mmc_deselect_cards(host);
1085 	host->card->state &= ~MMC_STATE_HIGHSPEED;
1086 	if (!err) {
1087 		mmc_power_off(host);
1088 		mmc_card_set_suspended(host->card);
1089 	}
1090 
1091 out:
1092 	mmc_release_host(host);
1093 	return err;
1094 }
1095 
1096 /*
1097  * Callback for suspend
1098  */
1099 static int mmc_sd_suspend(struct mmc_host *host)
1100 {
1101 	int err;
1102 
1103 	err = _mmc_sd_suspend(host);
1104 	if (!err) {
1105 		pm_runtime_disable(&host->card->dev);
1106 		pm_runtime_set_suspended(&host->card->dev);
1107 	}
1108 
1109 	return err;
1110 }
1111 
1112 /*
1113  * This function tries to determine if the same card is still present
1114  * and, if so, restore all state to it.
1115  */
1116 static int _mmc_sd_resume(struct mmc_host *host)
1117 {
1118 	int err = 0;
1119 
1120 	BUG_ON(!host);
1121 	BUG_ON(!host->card);
1122 
1123 	mmc_claim_host(host);
1124 
1125 	if (!mmc_card_suspended(host->card))
1126 		goto out;
1127 
1128 	mmc_power_up(host, host->card->ocr);
1129 	err = mmc_sd_init_card(host, host->card->ocr, host->card);
1130 	mmc_card_clr_suspended(host->card);
1131 
1132 out:
1133 	mmc_release_host(host);
1134 	return err;
1135 }
1136 
1137 /*
1138  * Callback for resume
1139  */
1140 static int mmc_sd_resume(struct mmc_host *host)
1141 {
1142 	int err = 0;
1143 
1144 	if (!(host->caps & MMC_CAP_RUNTIME_RESUME)) {
1145 		err = _mmc_sd_resume(host);
1146 		pm_runtime_set_active(&host->card->dev);
1147 		pm_runtime_mark_last_busy(&host->card->dev);
1148 	}
1149 	pm_runtime_enable(&host->card->dev);
1150 
1151 	return err;
1152 }
1153 
1154 /*
1155  * Callback for runtime_suspend.
1156  */
1157 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1158 {
1159 	int err;
1160 
1161 	if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1162 		return 0;
1163 
1164 	err = _mmc_sd_suspend(host);
1165 	if (err)
1166 		pr_err("%s: error %d doing aggessive suspend\n",
1167 			mmc_hostname(host), err);
1168 
1169 	return err;
1170 }
1171 
1172 /*
1173  * Callback for runtime_resume.
1174  */
1175 static int mmc_sd_runtime_resume(struct mmc_host *host)
1176 {
1177 	int err;
1178 
1179 	if (!(host->caps & (MMC_CAP_AGGRESSIVE_PM | MMC_CAP_RUNTIME_RESUME)))
1180 		return 0;
1181 
1182 	err = _mmc_sd_resume(host);
1183 	if (err)
1184 		pr_err("%s: error %d doing aggessive resume\n",
1185 			mmc_hostname(host), err);
1186 
1187 	return 0;
1188 }
1189 
1190 static int mmc_sd_power_restore(struct mmc_host *host)
1191 {
1192 	int ret;
1193 
1194 	host->card->state &= ~MMC_STATE_HIGHSPEED;
1195 	mmc_claim_host(host);
1196 	ret = mmc_sd_init_card(host, host->card->ocr, host->card);
1197 	mmc_release_host(host);
1198 
1199 	return ret;
1200 }
1201 
1202 static const struct mmc_bus_ops mmc_sd_ops = {
1203 	.remove = mmc_sd_remove,
1204 	.detect = mmc_sd_detect,
1205 	.suspend = NULL,
1206 	.resume = NULL,
1207 	.power_restore = mmc_sd_power_restore,
1208 	.alive = mmc_sd_alive,
1209 	.shutdown = mmc_sd_suspend,
1210 };
1211 
1212 static const struct mmc_bus_ops mmc_sd_ops_unsafe = {
1213 	.remove = mmc_sd_remove,
1214 	.detect = mmc_sd_detect,
1215 	.runtime_suspend = mmc_sd_runtime_suspend,
1216 	.runtime_resume = mmc_sd_runtime_resume,
1217 	.suspend = mmc_sd_suspend,
1218 	.resume = mmc_sd_resume,
1219 	.power_restore = mmc_sd_power_restore,
1220 	.alive = mmc_sd_alive,
1221 	.shutdown = mmc_sd_suspend,
1222 };
1223 
1224 static void mmc_sd_attach_bus_ops(struct mmc_host *host)
1225 {
1226 	const struct mmc_bus_ops *bus_ops;
1227 
1228 	if (!mmc_card_is_removable(host))
1229 		bus_ops = &mmc_sd_ops_unsafe;
1230 	else
1231 		bus_ops = &mmc_sd_ops;
1232 	mmc_attach_bus(host, bus_ops);
1233 }
1234 
1235 /*
1236  * Starting point for SD card init.
1237  */
1238 int mmc_attach_sd(struct mmc_host *host)
1239 {
1240 	int err;
1241 	u32 ocr, rocr;
1242 
1243 	BUG_ON(!host);
1244 	WARN_ON(!host->claimed);
1245 
1246 	err = mmc_send_app_op_cond(host, 0, &ocr);
1247 	if (err)
1248 		return err;
1249 
1250 	mmc_sd_attach_bus_ops(host);
1251 	if (host->ocr_avail_sd)
1252 		host->ocr_avail = host->ocr_avail_sd;
1253 
1254 	/*
1255 	 * We need to get OCR a different way for SPI.
1256 	 */
1257 	if (mmc_host_is_spi(host)) {
1258 		mmc_go_idle(host);
1259 
1260 		err = mmc_spi_read_ocr(host, 0, &ocr);
1261 		if (err)
1262 			goto err;
1263 	}
1264 
1265 	rocr = mmc_select_voltage(host, ocr);
1266 
1267 	/*
1268 	 * Can we support the voltage(s) of the card(s)?
1269 	 */
1270 	if (!rocr) {
1271 		err = -EINVAL;
1272 		goto err;
1273 	}
1274 
1275 	/*
1276 	 * Detect and init the card.
1277 	 */
1278 	err = mmc_sd_init_card(host, rocr, NULL);
1279 	if (err)
1280 		goto err;
1281 
1282 	mmc_release_host(host);
1283 	err = mmc_add_card(host->card);
1284 	mmc_claim_host(host);
1285 	if (err)
1286 		goto remove_card;
1287 
1288 	return 0;
1289 
1290 remove_card:
1291 	mmc_release_host(host);
1292 	mmc_remove_card(host->card);
1293 	host->card = NULL;
1294 	mmc_claim_host(host);
1295 err:
1296 	mmc_detach_bus(host);
1297 
1298 	pr_err("%s: error %d whilst initialising SD card\n",
1299 		mmc_hostname(host), err);
1300 
1301 	return err;
1302 }
1303 
1304