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
16 #include <linux/mmc/host.h>
17 #include <linux/mmc/card.h>
18 #include <linux/mmc/mmc.h>
19 #include <linux/mmc/sd.h>
20
21 #include <trace/hooks/mmc_core.h>
22
23 #include "core.h"
24 #include "card.h"
25 #include "host.h"
26 #include "bus.h"
27 #include "mmc_ops.h"
28 #include "sd.h"
29 #include "sd_ops.h"
30
31 static const unsigned int tran_exp[] = {
32 10000, 100000, 1000000, 10000000,
33 0, 0, 0, 0
34 };
35
36 static const unsigned char tran_mant[] = {
37 0, 10, 12, 13, 15, 20, 25, 30,
38 35, 40, 45, 50, 55, 60, 70, 80,
39 };
40
41 static const unsigned int taac_exp[] = {
42 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
43 };
44
45 static const unsigned int taac_mant[] = {
46 0, 10, 12, 13, 15, 20, 25, 30,
47 35, 40, 45, 50, 55, 60, 70, 80,
48 };
49
50 static const unsigned int sd_au_size[] = {
51 0, SZ_16K / 512, SZ_32K / 512, SZ_64K / 512,
52 SZ_128K / 512, SZ_256K / 512, SZ_512K / 512, SZ_1M / 512,
53 SZ_2M / 512, SZ_4M / 512, SZ_8M / 512, (SZ_8M + SZ_4M) / 512,
54 SZ_16M / 512, (SZ_16M + SZ_8M) / 512, SZ_32M / 512, SZ_64M / 512,
55 };
56
57 #define UNSTUFF_BITS(resp,start,size) \
58 ({ \
59 const int __size = size; \
60 const u32 __mask = (__size < 32 ? 1 << __size : 0) - 1; \
61 const int __off = 3 - ((start) / 32); \
62 const int __shft = (start) & 31; \
63 u32 __res; \
64 \
65 __res = resp[__off] >> __shft; \
66 if (__size + __shft > 32) \
67 __res |= resp[__off-1] << ((32 - __shft) % 32); \
68 __res & __mask; \
69 })
70
71 /*
72 * Given the decoded CSD structure, decode the raw CID to our CID structure.
73 */
mmc_decode_cid(struct mmc_card * card)74 void mmc_decode_cid(struct mmc_card *card)
75 {
76 u32 *resp = card->raw_cid;
77
78 /*
79 * SD doesn't currently have a version field so we will
80 * have to assume we can parse this.
81 */
82 card->cid.manfid = UNSTUFF_BITS(resp, 120, 8);
83 card->cid.oemid = UNSTUFF_BITS(resp, 104, 16);
84 card->cid.prod_name[0] = UNSTUFF_BITS(resp, 96, 8);
85 card->cid.prod_name[1] = UNSTUFF_BITS(resp, 88, 8);
86 card->cid.prod_name[2] = UNSTUFF_BITS(resp, 80, 8);
87 card->cid.prod_name[3] = UNSTUFF_BITS(resp, 72, 8);
88 card->cid.prod_name[4] = UNSTUFF_BITS(resp, 64, 8);
89 card->cid.hwrev = UNSTUFF_BITS(resp, 60, 4);
90 card->cid.fwrev = UNSTUFF_BITS(resp, 56, 4);
91 card->cid.serial = UNSTUFF_BITS(resp, 24, 32);
92 card->cid.year = UNSTUFF_BITS(resp, 12, 8);
93 card->cid.month = UNSTUFF_BITS(resp, 8, 4);
94
95 card->cid.year += 2000; /* SD cards year offset */
96 }
97
98 /*
99 * Given a 128-bit response, decode to our card CSD structure.
100 */
mmc_decode_csd(struct mmc_card * card)101 static int mmc_decode_csd(struct mmc_card *card)
102 {
103 struct mmc_csd *csd = &card->csd;
104 unsigned int e, m, csd_struct;
105 u32 *resp = card->raw_csd;
106
107 csd_struct = UNSTUFF_BITS(resp, 126, 2);
108
109 switch (csd_struct) {
110 case 0:
111 m = UNSTUFF_BITS(resp, 115, 4);
112 e = UNSTUFF_BITS(resp, 112, 3);
113 csd->taac_ns = (taac_exp[e] * taac_mant[m] + 9) / 10;
114 csd->taac_clks = UNSTUFF_BITS(resp, 104, 8) * 100;
115
116 m = UNSTUFF_BITS(resp, 99, 4);
117 e = UNSTUFF_BITS(resp, 96, 3);
118 csd->max_dtr = tran_exp[e] * tran_mant[m];
119 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
120
121 e = UNSTUFF_BITS(resp, 47, 3);
122 m = UNSTUFF_BITS(resp, 62, 12);
123 csd->capacity = (1 + m) << (e + 2);
124
125 csd->read_blkbits = UNSTUFF_BITS(resp, 80, 4);
126 csd->read_partial = UNSTUFF_BITS(resp, 79, 1);
127 csd->write_misalign = UNSTUFF_BITS(resp, 78, 1);
128 csd->read_misalign = UNSTUFF_BITS(resp, 77, 1);
129 csd->dsr_imp = UNSTUFF_BITS(resp, 76, 1);
130 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
131 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
132 csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
133
134 if (UNSTUFF_BITS(resp, 46, 1)) {
135 csd->erase_size = 1;
136 } else if (csd->write_blkbits >= 9) {
137 csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
138 csd->erase_size <<= csd->write_blkbits - 9;
139 }
140
141 if (UNSTUFF_BITS(resp, 13, 1))
142 mmc_card_set_readonly(card);
143 break;
144 case 1:
145 /*
146 * This is a block-addressed SDHC or SDXC card. Most
147 * interesting fields are unused and have fixed
148 * values. To avoid getting tripped by buggy cards,
149 * we assume those fixed values ourselves.
150 */
151 mmc_card_set_blockaddr(card);
152
153 csd->taac_ns = 0; /* Unused */
154 csd->taac_clks = 0; /* Unused */
155
156 m = UNSTUFF_BITS(resp, 99, 4);
157 e = UNSTUFF_BITS(resp, 96, 3);
158 csd->max_dtr = tran_exp[e] * tran_mant[m];
159 csd->cmdclass = UNSTUFF_BITS(resp, 84, 12);
160 csd->c_size = UNSTUFF_BITS(resp, 48, 22);
161
162 /* SDXC cards have a minimum C_SIZE of 0x00FFFF */
163 if (csd->c_size >= 0xFFFF)
164 mmc_card_set_ext_capacity(card);
165
166 m = UNSTUFF_BITS(resp, 48, 22);
167 csd->capacity = (1 + m) << 10;
168
169 csd->read_blkbits = 9;
170 csd->read_partial = 0;
171 csd->write_misalign = 0;
172 csd->read_misalign = 0;
173 csd->r2w_factor = 4; /* Unused */
174 csd->write_blkbits = 9;
175 csd->write_partial = 0;
176 csd->erase_size = 1;
177
178 if (UNSTUFF_BITS(resp, 13, 1))
179 mmc_card_set_readonly(card);
180 break;
181 default:
182 pr_err("%s: unrecognised CSD structure version %d\n",
183 mmc_hostname(card->host), csd_struct);
184 return -EINVAL;
185 }
186
187 card->erase_size = csd->erase_size;
188
189 return 0;
190 }
191
192 /*
193 * Given a 64-bit response, decode to our card SCR structure.
194 */
mmc_decode_scr(struct mmc_card * card)195 static int mmc_decode_scr(struct mmc_card *card)
196 {
197 struct sd_scr *scr = &card->scr;
198 unsigned int scr_struct;
199 u32 resp[4];
200
201 resp[3] = card->raw_scr[1];
202 resp[2] = card->raw_scr[0];
203
204 scr_struct = UNSTUFF_BITS(resp, 60, 4);
205 if (scr_struct != 0) {
206 pr_err("%s: unrecognised SCR structure version %d\n",
207 mmc_hostname(card->host), scr_struct);
208 return -EINVAL;
209 }
210
211 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
212 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);
213 if (scr->sda_vsn == SCR_SPEC_VER_2)
214 /* Check if Physical Layer Spec v3.0 is supported */
215 scr->sda_spec3 = UNSTUFF_BITS(resp, 47, 1);
216
217 if (scr->sda_spec3) {
218 scr->sda_spec4 = UNSTUFF_BITS(resp, 42, 1);
219 scr->sda_specx = UNSTUFF_BITS(resp, 38, 4);
220 }
221
222 if (UNSTUFF_BITS(resp, 55, 1))
223 card->erased_byte = 0xFF;
224 else
225 card->erased_byte = 0x0;
226
227 if (scr->sda_spec3)
228 scr->cmds = UNSTUFF_BITS(resp, 32, 2);
229
230 /* SD Spec says: any SD Card shall set at least bits 0 and 2 */
231 if (!(scr->bus_widths & SD_SCR_BUS_WIDTH_1) ||
232 !(scr->bus_widths & SD_SCR_BUS_WIDTH_4)) {
233 pr_err("%s: invalid bus width\n", mmc_hostname(card->host));
234 return -EINVAL;
235 }
236
237 return 0;
238 }
239
240 /*
241 * Fetch and process SD Status register.
242 */
mmc_read_ssr(struct mmc_card * card)243 static int mmc_read_ssr(struct mmc_card *card)
244 {
245 unsigned int au, es, et, eo;
246 __be32 *raw_ssr;
247 u32 resp[4] = {};
248 u8 discard_support;
249 int i;
250
251 if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
252 pr_warn("%s: card lacks mandatory SD Status function\n",
253 mmc_hostname(card->host));
254 return 0;
255 }
256
257 raw_ssr = kmalloc(sizeof(card->raw_ssr), GFP_KERNEL);
258 if (!raw_ssr)
259 return -ENOMEM;
260
261 if (mmc_app_sd_status(card, raw_ssr)) {
262 pr_warn("%s: problem reading SD Status register\n",
263 mmc_hostname(card->host));
264 kfree(raw_ssr);
265 return 0;
266 }
267
268 for (i = 0; i < 16; i++)
269 card->raw_ssr[i] = be32_to_cpu(raw_ssr[i]);
270
271 kfree(raw_ssr);
272
273 /*
274 * UNSTUFF_BITS only works with four u32s so we have to offset the
275 * bitfield positions accordingly.
276 */
277 au = UNSTUFF_BITS(card->raw_ssr, 428 - 384, 4);
278 if (au) {
279 if (au <= 9 || card->scr.sda_spec3) {
280 card->ssr.au = sd_au_size[au];
281 es = UNSTUFF_BITS(card->raw_ssr, 408 - 384, 16);
282 et = UNSTUFF_BITS(card->raw_ssr, 402 - 384, 6);
283 if (es && et) {
284 eo = UNSTUFF_BITS(card->raw_ssr, 400 - 384, 2);
285 card->ssr.erase_timeout = (et * 1000) / es;
286 card->ssr.erase_offset = eo * 1000;
287 }
288 } else {
289 pr_warn("%s: SD Status: Invalid Allocation Unit size\n",
290 mmc_hostname(card->host));
291 }
292 }
293
294 /*
295 * starting SD5.1 discard is supported if DISCARD_SUPPORT (b313) is set
296 */
297 resp[3] = card->raw_ssr[6];
298 discard_support = UNSTUFF_BITS(resp, 313 - 288, 1);
299 card->erase_arg = (card->scr.sda_specx && discard_support) ?
300 SD_DISCARD_ARG : SD_ERASE_ARG;
301
302 return 0;
303 }
304
305 /*
306 * Fetches and decodes switch information
307 */
mmc_read_switch(struct mmc_card * card)308 static int mmc_read_switch(struct mmc_card *card)
309 {
310 int err;
311 u8 *status;
312
313 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
314 return 0;
315
316 if (!(card->csd.cmdclass & CCC_SWITCH)) {
317 pr_warn("%s: card lacks mandatory switch function, performance might suffer\n",
318 mmc_hostname(card->host));
319 return 0;
320 }
321
322 status = kmalloc(64, GFP_KERNEL);
323 if (!status)
324 return -ENOMEM;
325
326 /*
327 * Find out the card's support bits with a mode 0 operation.
328 * The argument does not matter, as the support bits do not
329 * change with the arguments.
330 */
331 err = mmc_sd_switch(card, 0, 0, 0, status);
332 if (err) {
333 /*
334 * If the host or the card can't do the switch,
335 * fail more gracefully.
336 */
337 if (err != -EINVAL && err != -ENOSYS && err != -EFAULT)
338 goto out;
339
340 pr_warn("%s: problem reading Bus Speed modes\n",
341 mmc_hostname(card->host));
342 err = 0;
343
344 goto out;
345 }
346
347 if (status[13] & SD_MODE_HIGH_SPEED)
348 card->sw_caps.hs_max_dtr = HIGH_SPEED_MAX_DTR;
349
350 if (card->scr.sda_spec3) {
351 card->sw_caps.sd3_bus_mode = status[13];
352 /* Driver Strengths supported by the card */
353 card->sw_caps.sd3_drv_type = status[9];
354 card->sw_caps.sd3_curr_limit = status[7] | status[6] << 8;
355 }
356
357 out:
358 kfree(status);
359
360 return err;
361 }
362
363 /*
364 * Test if the card supports high-speed mode and, if so, switch to it.
365 */
mmc_sd_switch_hs(struct mmc_card * card)366 int mmc_sd_switch_hs(struct mmc_card *card)
367 {
368 int err;
369 u8 *status;
370
371 if (card->scr.sda_vsn < SCR_SPEC_VER_1)
372 return 0;
373
374 if (!(card->csd.cmdclass & CCC_SWITCH))
375 return 0;
376
377 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
378 return 0;
379
380 if (card->sw_caps.hs_max_dtr == 0)
381 return 0;
382
383 status = kmalloc(64, GFP_KERNEL);
384 if (!status)
385 return -ENOMEM;
386
387 err = mmc_sd_switch(card, 1, 0, HIGH_SPEED_BUS_SPEED, status);
388 if (err)
389 goto out;
390
391 if ((status[16] & 0xF) != HIGH_SPEED_BUS_SPEED) {
392 pr_warn("%s: Problem switching card into high-speed mode!\n",
393 mmc_hostname(card->host));
394 err = 0;
395 } else {
396 err = 1;
397 }
398
399 out:
400 kfree(status);
401
402 return err;
403 }
404
sd_select_driver_type(struct mmc_card * card,u8 * status)405 static int sd_select_driver_type(struct mmc_card *card, u8 *status)
406 {
407 int card_drv_type, drive_strength, drv_type;
408 int err;
409
410 card->drive_strength = 0;
411
412 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
413
414 drive_strength = mmc_select_drive_strength(card,
415 card->sw_caps.uhs_max_dtr,
416 card_drv_type, &drv_type);
417
418 if (drive_strength) {
419 err = mmc_sd_switch(card, 1, 2, drive_strength, status);
420 if (err)
421 return err;
422 if ((status[15] & 0xF) != drive_strength) {
423 pr_warn("%s: Problem setting drive strength!\n",
424 mmc_hostname(card->host));
425 return 0;
426 }
427 card->drive_strength = drive_strength;
428 }
429
430 if (drv_type)
431 mmc_set_driver_type(card->host, drv_type);
432
433 return 0;
434 }
435
sd_update_bus_speed_mode(struct mmc_card * card)436 static void sd_update_bus_speed_mode(struct mmc_card *card)
437 {
438 /*
439 * If the host doesn't support any of the UHS-I modes, fallback on
440 * default speed.
441 */
442 if (!mmc_host_uhs(card->host)) {
443 card->sd_bus_speed = 0;
444 return;
445 }
446
447 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
448 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
449 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
450 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
451 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
452 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
453 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
454 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
455 SD_MODE_UHS_SDR50)) {
456 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
457 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
458 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
459 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
460 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
461 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
462 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
463 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
464 SD_MODE_UHS_SDR12)) {
465 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
466 }
467
468 trace_android_vh_sd_update_bus_speed_mode(card);
469 }
470
sd_set_bus_speed_mode(struct mmc_card * card,u8 * status)471 static int sd_set_bus_speed_mode(struct mmc_card *card, u8 *status)
472 {
473 int err;
474 unsigned int timing = 0;
475
476 switch (card->sd_bus_speed) {
477 case UHS_SDR104_BUS_SPEED:
478 timing = MMC_TIMING_UHS_SDR104;
479 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
480 break;
481 case UHS_DDR50_BUS_SPEED:
482 timing = MMC_TIMING_UHS_DDR50;
483 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
484 break;
485 case UHS_SDR50_BUS_SPEED:
486 timing = MMC_TIMING_UHS_SDR50;
487 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
488 break;
489 case UHS_SDR25_BUS_SPEED:
490 timing = MMC_TIMING_UHS_SDR25;
491 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
492 break;
493 case UHS_SDR12_BUS_SPEED:
494 timing = MMC_TIMING_UHS_SDR12;
495 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
496 break;
497 default:
498 return 0;
499 }
500
501 err = mmc_sd_switch(card, 1, 0, card->sd_bus_speed, status);
502 if (err)
503 return err;
504
505 if ((status[16] & 0xF) != card->sd_bus_speed)
506 pr_warn("%s: Problem setting bus speed mode!\n",
507 mmc_hostname(card->host));
508 else {
509 mmc_set_timing(card->host, timing);
510 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
511 }
512
513 return 0;
514 }
515
516 /* Get host's max current setting at its current voltage */
sd_get_host_max_current(struct mmc_host * host)517 static u32 sd_get_host_max_current(struct mmc_host *host)
518 {
519 u32 voltage, max_current;
520
521 voltage = 1 << host->ios.vdd;
522 switch (voltage) {
523 case MMC_VDD_165_195:
524 max_current = host->max_current_180;
525 break;
526 case MMC_VDD_29_30:
527 case MMC_VDD_30_31:
528 max_current = host->max_current_300;
529 break;
530 case MMC_VDD_32_33:
531 case MMC_VDD_33_34:
532 max_current = host->max_current_330;
533 break;
534 default:
535 max_current = 0;
536 }
537
538 return max_current;
539 }
540
sd_set_current_limit(struct mmc_card * card,u8 * status)541 static int sd_set_current_limit(struct mmc_card *card, u8 *status)
542 {
543 int current_limit = SD_SET_CURRENT_NO_CHANGE;
544 int err;
545 u32 max_current;
546
547 /*
548 * Current limit switch is only defined for SDR50, SDR104, and DDR50
549 * bus speed modes. For other bus speed modes, we do not change the
550 * current limit.
551 */
552 if ((card->sd_bus_speed != UHS_SDR50_BUS_SPEED) &&
553 (card->sd_bus_speed != UHS_SDR104_BUS_SPEED) &&
554 (card->sd_bus_speed != UHS_DDR50_BUS_SPEED))
555 return 0;
556
557 /*
558 * Host has different current capabilities when operating at
559 * different voltages, so find out its max current first.
560 */
561 max_current = sd_get_host_max_current(card->host);
562
563 /*
564 * We only check host's capability here, if we set a limit that is
565 * higher than the card's maximum current, the card will be using its
566 * maximum current, e.g. if the card's maximum current is 300ma, and
567 * when we set current limit to 200ma, the card will draw 200ma, and
568 * when we set current limit to 400/600/800ma, the card will draw its
569 * maximum 300ma from the host.
570 *
571 * The above is incorrect: if we try to set a current limit that is
572 * not supported by the card, the card can rightfully error out the
573 * attempt, and remain at the default current limit. This results
574 * in a 300mA card being limited to 200mA even though the host
575 * supports 800mA. Failures seen with SanDisk 8GB UHS cards with
576 * an iMX6 host. --rmk
577 */
578 if (max_current >= 800 &&
579 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_800)
580 current_limit = SD_SET_CURRENT_LIMIT_800;
581 else if (max_current >= 600 &&
582 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_600)
583 current_limit = SD_SET_CURRENT_LIMIT_600;
584 else if (max_current >= 400 &&
585 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_400)
586 current_limit = SD_SET_CURRENT_LIMIT_400;
587 else if (max_current >= 200 &&
588 card->sw_caps.sd3_curr_limit & SD_MAX_CURRENT_200)
589 current_limit = SD_SET_CURRENT_LIMIT_200;
590
591 if (current_limit != SD_SET_CURRENT_NO_CHANGE) {
592 err = mmc_sd_switch(card, 1, 3, current_limit, status);
593 if (err)
594 return err;
595
596 if (((status[15] >> 4) & 0x0F) != current_limit)
597 pr_warn("%s: Problem setting current limit!\n",
598 mmc_hostname(card->host));
599
600 }
601
602 return 0;
603 }
604
605 /*
606 * UHS-I specific initialization procedure
607 */
mmc_sd_init_uhs_card(struct mmc_card * card)608 static int mmc_sd_init_uhs_card(struct mmc_card *card)
609 {
610 int err;
611 u8 *status;
612
613 if (!(card->csd.cmdclass & CCC_SWITCH))
614 return 0;
615
616 status = kmalloc(64, GFP_KERNEL);
617 if (!status)
618 return -ENOMEM;
619
620 /* Set 4-bit bus width */
621 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
622 if (err)
623 goto out;
624
625 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
626
627 /*
628 * Select the bus speed mode depending on host
629 * and card capability.
630 */
631 sd_update_bus_speed_mode(card);
632
633 /* Set the driver strength for the card */
634 err = sd_select_driver_type(card, status);
635 if (err)
636 goto out;
637
638 /* Set current limit for the card */
639 err = sd_set_current_limit(card, status);
640 if (err)
641 goto out;
642
643 /* Set bus speed mode of the card */
644 err = sd_set_bus_speed_mode(card, status);
645 if (err)
646 goto out;
647
648 /*
649 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
650 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
651 */
652 if (!mmc_host_is_spi(card->host) &&
653 (card->host->ios.timing == MMC_TIMING_UHS_SDR50 ||
654 card->host->ios.timing == MMC_TIMING_UHS_DDR50 ||
655 card->host->ios.timing == MMC_TIMING_UHS_SDR104)) {
656 err = mmc_execute_tuning(card);
657
658 /*
659 * As SD Specifications Part1 Physical Layer Specification
660 * Version 3.01 says, CMD19 tuning is available for unlocked
661 * cards in transfer state of 1.8V signaling mode. The small
662 * difference between v3.00 and 3.01 spec means that CMD19
663 * tuning is also available for DDR50 mode.
664 */
665 if (err && card->host->ios.timing == MMC_TIMING_UHS_DDR50) {
666 pr_warn("%s: ddr50 tuning failed\n",
667 mmc_hostname(card->host));
668 err = 0;
669 }
670 }
671
672 out:
673 kfree(status);
674
675 return err;
676 }
677
678 MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
679 card->raw_cid[2], card->raw_cid[3]);
680 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
681 card->raw_csd[2], card->raw_csd[3]);
682 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
683 MMC_DEV_ATTR(ssr,
684 "%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x%08x\n",
685 card->raw_ssr[0], card->raw_ssr[1], card->raw_ssr[2],
686 card->raw_ssr[3], card->raw_ssr[4], card->raw_ssr[5],
687 card->raw_ssr[6], card->raw_ssr[7], card->raw_ssr[8],
688 card->raw_ssr[9], card->raw_ssr[10], card->raw_ssr[11],
689 card->raw_ssr[12], card->raw_ssr[13], card->raw_ssr[14],
690 card->raw_ssr[15]);
691 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
692 MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
693 MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
694 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
695 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
696 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
697 MMC_DEV_ATTR(name, "%s\n", card->cid.prod_name);
698 MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
699 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
700 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
701 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
702
703
mmc_dsr_show(struct device * dev,struct device_attribute * attr,char * buf)704 static ssize_t mmc_dsr_show(struct device *dev,
705 struct device_attribute *attr,
706 char *buf)
707 {
708 struct mmc_card *card = mmc_dev_to_card(dev);
709 struct mmc_host *host = card->host;
710
711 if (card->csd.dsr_imp && host->dsr_req)
712 return sprintf(buf, "0x%x\n", host->dsr);
713 else
714 /* return default DSR value */
715 return sprintf(buf, "0x%x\n", 0x404);
716 }
717
718 static DEVICE_ATTR(dsr, S_IRUGO, mmc_dsr_show, NULL);
719
720 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
721 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
722 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
723
724 #define sdio_info_attr(num) \
725 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
726 { \
727 struct mmc_card *card = mmc_dev_to_card(dev); \
728 \
729 if (num > card->num_info) \
730 return -ENODATA; \
731 if (!card->info[num-1][0]) \
732 return 0; \
733 return sprintf(buf, "%s\n", card->info[num-1]); \
734 } \
735 static DEVICE_ATTR_RO(info##num)
736
737 sdio_info_attr(1);
738 sdio_info_attr(2);
739 sdio_info_attr(3);
740 sdio_info_attr(4);
741
742 static struct attribute *sd_std_attrs[] = {
743 &dev_attr_vendor.attr,
744 &dev_attr_device.attr,
745 &dev_attr_revision.attr,
746 &dev_attr_info1.attr,
747 &dev_attr_info2.attr,
748 &dev_attr_info3.attr,
749 &dev_attr_info4.attr,
750 &dev_attr_cid.attr,
751 &dev_attr_csd.attr,
752 &dev_attr_scr.attr,
753 &dev_attr_ssr.attr,
754 &dev_attr_date.attr,
755 &dev_attr_erase_size.attr,
756 &dev_attr_preferred_erase_size.attr,
757 &dev_attr_fwrev.attr,
758 &dev_attr_hwrev.attr,
759 &dev_attr_manfid.attr,
760 &dev_attr_name.attr,
761 &dev_attr_oemid.attr,
762 &dev_attr_serial.attr,
763 &dev_attr_ocr.attr,
764 &dev_attr_rca.attr,
765 &dev_attr_dsr.attr,
766 NULL,
767 };
768
sd_std_is_visible(struct kobject * kobj,struct attribute * attr,int index)769 static umode_t sd_std_is_visible(struct kobject *kobj, struct attribute *attr,
770 int index)
771 {
772 struct device *dev = kobj_to_dev(kobj);
773 struct mmc_card *card = mmc_dev_to_card(dev);
774
775 /* CIS vendor and device ids, revision and info string are available only for Combo cards */
776 if ((attr == &dev_attr_vendor.attr ||
777 attr == &dev_attr_device.attr ||
778 attr == &dev_attr_revision.attr ||
779 attr == &dev_attr_info1.attr ||
780 attr == &dev_attr_info2.attr ||
781 attr == &dev_attr_info3.attr ||
782 attr == &dev_attr_info4.attr
783 ) && card->type != MMC_TYPE_SD_COMBO)
784 return 0;
785
786 return attr->mode;
787 }
788
789 static const struct attribute_group sd_std_group = {
790 .attrs = sd_std_attrs,
791 .is_visible = sd_std_is_visible,
792 };
793 __ATTRIBUTE_GROUPS(sd_std);
794
795 struct device_type sd_type = {
796 .groups = sd_std_groups,
797 };
798
799 /*
800 * Fetch CID from card.
801 */
mmc_sd_get_cid(struct mmc_host * host,u32 ocr,u32 * cid,u32 * rocr)802 int mmc_sd_get_cid(struct mmc_host *host, u32 ocr, u32 *cid, u32 *rocr)
803 {
804 int err;
805 u32 max_current;
806 int retries = 10;
807 u32 pocr = ocr;
808
809 try_again:
810 if (!retries) {
811 ocr &= ~SD_OCR_S18R;
812 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
813 }
814
815 /*
816 * Since we're changing the OCR value, we seem to
817 * need to tell some cards to go back to the idle
818 * state. We wait 1ms to give cards time to
819 * respond.
820 */
821 mmc_go_idle(host);
822
823 /*
824 * If SD_SEND_IF_COND indicates an SD 2.0
825 * compliant card and we should set bit 30
826 * of the ocr to indicate that we can handle
827 * block-addressed SDHC cards.
828 */
829 err = mmc_send_if_cond(host, ocr);
830 if (!err)
831 ocr |= SD_OCR_CCS;
832
833 /*
834 * If the host supports one of UHS-I modes, request the card
835 * to switch to 1.8V signaling level. If the card has failed
836 * repeatedly to switch however, skip this.
837 */
838 if (retries && mmc_host_uhs(host))
839 ocr |= SD_OCR_S18R;
840
841 /*
842 * If the host can supply more than 150mA at current voltage,
843 * XPC should be set to 1.
844 */
845 max_current = sd_get_host_max_current(host);
846 if (max_current > 150)
847 ocr |= SD_OCR_XPC;
848
849 err = mmc_send_app_op_cond(host, ocr, rocr);
850 if (err)
851 return err;
852
853 /*
854 * In case the S18A bit is set in the response, let's start the signal
855 * voltage switch procedure. SPI mode doesn't support CMD11.
856 * Note that, according to the spec, the S18A bit is not valid unless
857 * the CCS bit is set as well. We deliberately deviate from the spec in
858 * regards to this, which allows UHS-I to be supported for SDSC cards.
859 */
860 if (!mmc_host_is_spi(host) && (ocr & SD_OCR_S18R) &&
861 rocr && (*rocr & SD_ROCR_S18A)) {
862 err = mmc_set_uhs_voltage(host, pocr);
863 if (err == -EAGAIN) {
864 retries--;
865 goto try_again;
866 } else if (err) {
867 retries = 0;
868 goto try_again;
869 }
870 }
871
872 err = mmc_send_cid(host, cid);
873 return err;
874 }
875
mmc_sd_get_csd(struct mmc_host * host,struct mmc_card * card)876 int mmc_sd_get_csd(struct mmc_host *host, struct mmc_card *card)
877 {
878 int err;
879
880 /*
881 * Fetch CSD from card.
882 */
883 err = mmc_send_csd(card, card->raw_csd);
884 if (err)
885 return err;
886
887 err = mmc_decode_csd(card);
888 if (err)
889 return err;
890
891 return 0;
892 }
893
mmc_sd_get_ro(struct mmc_host * host)894 static int mmc_sd_get_ro(struct mmc_host *host)
895 {
896 int ro;
897
898 /*
899 * Some systems don't feature a write-protect pin and don't need one.
900 * E.g. because they only have micro-SD card slot. For those systems
901 * assume that the SD card is always read-write.
902 */
903 if (host->caps2 & MMC_CAP2_NO_WRITE_PROTECT)
904 return 0;
905
906 if (!host->ops->get_ro)
907 return -1;
908
909 ro = host->ops->get_ro(host);
910
911 return ro;
912 }
913
mmc_sd_setup_card(struct mmc_host * host,struct mmc_card * card,bool reinit)914 int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
915 bool reinit)
916 {
917 int err;
918
919 if (!reinit) {
920 /*
921 * Fetch SCR from card.
922 */
923 err = mmc_app_send_scr(card);
924 if (err)
925 return err;
926
927 err = mmc_decode_scr(card);
928 if (err)
929 return err;
930
931 /*
932 * Fetch and process SD Status register.
933 */
934 err = mmc_read_ssr(card);
935 if (err)
936 return err;
937
938 /* Erase init depends on CSD and SSR */
939 mmc_init_erase(card);
940 }
941
942 /*
943 * Fetch switch information from card. Note, sd3_bus_mode can change if
944 * voltage switch outcome changes, so do this always.
945 */
946 err = mmc_read_switch(card);
947 if (err)
948 return err;
949
950 /*
951 * For SPI, enable CRC as appropriate.
952 * This CRC enable is located AFTER the reading of the
953 * card registers because some SDHC cards are not able
954 * to provide valid CRCs for non-512-byte blocks.
955 */
956 if (mmc_host_is_spi(host)) {
957 err = mmc_spi_set_crc(host, use_spi_crc);
958 if (err)
959 return err;
960 }
961
962 /*
963 * Check if read-only switch is active.
964 */
965 if (!reinit) {
966 int ro = mmc_sd_get_ro(host);
967
968 if (ro < 0) {
969 pr_warn("%s: host does not support reading read-only switch, assuming write-enable\n",
970 mmc_hostname(host));
971 } else if (ro > 0) {
972 mmc_card_set_readonly(card);
973 }
974 }
975
976 return 0;
977 }
978
mmc_sd_get_max_clock(struct mmc_card * card)979 unsigned mmc_sd_get_max_clock(struct mmc_card *card)
980 {
981 unsigned max_dtr = (unsigned int)-1;
982
983 if (mmc_card_hs(card)) {
984 if (max_dtr > card->sw_caps.hs_max_dtr)
985 max_dtr = card->sw_caps.hs_max_dtr;
986 } else if (max_dtr > card->csd.max_dtr) {
987 max_dtr = card->csd.max_dtr;
988 }
989
990 return max_dtr;
991 }
992
mmc_sd_card_using_v18(struct mmc_card * card)993 static bool mmc_sd_card_using_v18(struct mmc_card *card)
994 {
995 /*
996 * According to the SD spec., the Bus Speed Mode (function group 1) bits
997 * 2 to 4 are zero if the card is initialized at 3.3V signal level. Thus
998 * they can be used to determine if the card has already switched to
999 * 1.8V signaling.
1000 */
1001 return card->sw_caps.sd3_bus_mode &
1002 (SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR104 | SD_MODE_UHS_DDR50);
1003 }
1004
1005 /*
1006 * Handle the detection and initialisation of a card.
1007 *
1008 * In the case of a resume, "oldcard" will contain the card
1009 * we're trying to reinitialise.
1010 */
mmc_sd_init_card(struct mmc_host * host,u32 ocr,struct mmc_card * oldcard)1011 static int mmc_sd_init_card(struct mmc_host *host, u32 ocr,
1012 struct mmc_card *oldcard)
1013 {
1014 struct mmc_card *card;
1015 int err;
1016 u32 cid[4];
1017 u32 rocr = 0;
1018 bool v18_fixup_failed = false;
1019
1020 WARN_ON(!host->claimed);
1021 retry:
1022 err = mmc_sd_get_cid(host, ocr, cid, &rocr);
1023 if (err)
1024 return err;
1025
1026 if (oldcard) {
1027 if (memcmp(cid, oldcard->raw_cid, sizeof(cid)) != 0) {
1028 pr_debug("%s: Perhaps the card was replaced\n",
1029 mmc_hostname(host));
1030 return -ENOENT;
1031 }
1032
1033 card = oldcard;
1034 } else {
1035 /*
1036 * Allocate card structure.
1037 */
1038 card = mmc_alloc_card(host, &sd_type);
1039 if (IS_ERR(card))
1040 return PTR_ERR(card);
1041
1042 card->ocr = ocr;
1043 card->type = MMC_TYPE_SD;
1044 memcpy(card->raw_cid, cid, sizeof(card->raw_cid));
1045 }
1046
1047 /*
1048 * Call the optional HC's init_card function to handle quirks.
1049 */
1050 if (host->ops->init_card)
1051 host->ops->init_card(host, card);
1052
1053 /*
1054 * For native busses: get card RCA and quit open drain mode.
1055 */
1056 if (!mmc_host_is_spi(host)) {
1057 err = mmc_send_relative_addr(host, &card->rca);
1058 if (err)
1059 goto free_card;
1060 }
1061
1062 if (!oldcard) {
1063 err = mmc_sd_get_csd(host, card);
1064 if (err)
1065 goto free_card;
1066
1067 mmc_decode_cid(card);
1068 }
1069
1070 /*
1071 * handling only for cards supporting DSR and hosts requesting
1072 * DSR configuration
1073 */
1074 if (card->csd.dsr_imp && host->dsr_req)
1075 mmc_set_dsr(host);
1076
1077 /*
1078 * Select card, as all following commands rely on that.
1079 */
1080 if (!mmc_host_is_spi(host)) {
1081 err = mmc_select_card(card);
1082 if (err)
1083 goto free_card;
1084 }
1085
1086 err = mmc_sd_setup_card(host, card, oldcard != NULL);
1087 if (err)
1088 goto free_card;
1089
1090 /*
1091 * If the card has not been power cycled, it may still be using 1.8V
1092 * signaling. Detect that situation and try to initialize a UHS-I (1.8V)
1093 * transfer mode.
1094 */
1095 if (!v18_fixup_failed && !mmc_host_is_spi(host) && mmc_host_uhs(host) &&
1096 mmc_sd_card_using_v18(card) &&
1097 host->ios.signal_voltage != MMC_SIGNAL_VOLTAGE_180) {
1098 if (mmc_host_set_uhs_voltage(host) ||
1099 mmc_sd_init_uhs_card(card)) {
1100 v18_fixup_failed = true;
1101 mmc_power_cycle(host, ocr);
1102 if (!oldcard)
1103 mmc_remove_card(card);
1104 goto retry;
1105 }
1106 goto cont;
1107 }
1108
1109 /* Initialization sequence for UHS-I cards */
1110 if (rocr & SD_ROCR_S18A && mmc_host_uhs(host)) {
1111 err = mmc_sd_init_uhs_card(card);
1112 if (err)
1113 goto free_card;
1114 } else {
1115 /*
1116 * Attempt to change to high-speed (if supported)
1117 */
1118 err = mmc_sd_switch_hs(card);
1119 if (err > 0)
1120 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
1121 else if (err)
1122 goto free_card;
1123
1124 /*
1125 * Set bus speed.
1126 */
1127 mmc_set_clock(host, mmc_sd_get_max_clock(card));
1128
1129 /*
1130 * Switch to wider bus (if supported).
1131 */
1132 if ((host->caps & MMC_CAP_4_BIT_DATA) &&
1133 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
1134 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
1135 if (err)
1136 goto free_card;
1137
1138 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1139 }
1140 }
1141 cont:
1142 if (host->cqe_ops && !host->cqe_enabled) {
1143 err = host->cqe_ops->cqe_enable(host, card);
1144 if (!err) {
1145 host->cqe_enabled = true;
1146 host->hsq_enabled = true;
1147 pr_info("%s: Host Software Queue enabled\n",
1148 mmc_hostname(host));
1149 }
1150 }
1151
1152 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
1153 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
1154 pr_err("%s: Host failed to negotiate down from 3.3V\n",
1155 mmc_hostname(host));
1156 err = -EINVAL;
1157 goto free_card;
1158 }
1159
1160 host->card = card;
1161 return 0;
1162
1163 free_card:
1164 if (!oldcard)
1165 mmc_remove_card(card);
1166
1167 return err;
1168 }
1169
1170 /*
1171 * Host is being removed. Free up the current card.
1172 */
mmc_sd_remove(struct mmc_host * host)1173 static void mmc_sd_remove(struct mmc_host *host)
1174 {
1175 mmc_remove_card(host->card);
1176 host->card = NULL;
1177 }
1178
1179 /*
1180 * Card detection - card is alive.
1181 */
mmc_sd_alive(struct mmc_host * host)1182 static int mmc_sd_alive(struct mmc_host *host)
1183 {
1184 return mmc_send_status(host->card, NULL);
1185 }
1186
1187 /*
1188 * Card detection callback from host.
1189 */
mmc_sd_detect(struct mmc_host * host)1190 static void mmc_sd_detect(struct mmc_host *host)
1191 {
1192 int err;
1193
1194 mmc_get_card(host->card, NULL);
1195
1196 /*
1197 * Just check if our card has been removed.
1198 */
1199 err = _mmc_detect_card_removed(host);
1200
1201 mmc_put_card(host->card, NULL);
1202
1203 if (err) {
1204 mmc_sd_remove(host);
1205
1206 mmc_claim_host(host);
1207 mmc_detach_bus(host);
1208 mmc_power_off(host);
1209 mmc_release_host(host);
1210 }
1211 }
1212
_mmc_sd_suspend(struct mmc_host * host)1213 static int _mmc_sd_suspend(struct mmc_host *host)
1214 {
1215 int err = 0;
1216
1217 mmc_claim_host(host);
1218
1219 if (mmc_card_suspended(host->card))
1220 goto out;
1221
1222 if (!mmc_host_is_spi(host))
1223 err = mmc_deselect_cards(host);
1224
1225 if (!err) {
1226 mmc_power_off(host);
1227 mmc_card_set_suspended(host->card);
1228 }
1229
1230 out:
1231 mmc_release_host(host);
1232 return err;
1233 }
1234
_mmc_sd_shutdown(struct mmc_host * host)1235 static int _mmc_sd_shutdown(struct mmc_host *host)
1236 {
1237 int err = 0;
1238
1239 if (WARN_ON(!host) || WARN_ON(!host->card))
1240 return 0;
1241
1242 mmc_claim_host(host);
1243
1244 if (mmc_card_suspended(host->card))
1245 goto out;
1246
1247 if (!mmc_host_is_spi(host))
1248 err = mmc_deselect_cards(host);
1249
1250 if (!err) {
1251 mmc_power_off(host);
1252 mmc_card_set_suspended(host->card);
1253 }
1254
1255 host->ios.signal_voltage = MMC_SIGNAL_VOLTAGE_330;
1256 host->ios.vdd = fls(host->ocr_avail) - 1;
1257 mmc_regulator_set_vqmmc(host, &host->ios);
1258 pr_info("Set signal voltage to initial state\n");
1259
1260 out:
1261 mmc_release_host(host);
1262 return err;
1263 }
1264
mmc_sd_shutdown(struct mmc_host * host)1265 static int mmc_sd_shutdown(struct mmc_host *host)
1266 {
1267 int err;
1268
1269 err = _mmc_sd_shutdown(host);
1270 if (!err) {
1271 pm_runtime_disable(&host->card->dev);
1272 pm_runtime_set_suspended(&host->card->dev);
1273 }
1274
1275 return err;
1276 }
1277
1278 /*
1279 * Callback for suspend
1280 */
mmc_sd_suspend(struct mmc_host * host)1281 static int mmc_sd_suspend(struct mmc_host *host)
1282 {
1283 int err;
1284
1285 err = _mmc_sd_suspend(host);
1286 if (!err) {
1287 pm_runtime_disable(&host->card->dev);
1288 pm_runtime_set_suspended(&host->card->dev);
1289 }
1290
1291 return err;
1292 }
1293
1294 /*
1295 * This function tries to determine if the same card is still present
1296 * and, if so, restore all state to it.
1297 */
_mmc_sd_resume(struct mmc_host * host)1298 static int _mmc_sd_resume(struct mmc_host *host)
1299 {
1300 int err = 0;
1301
1302 mmc_claim_host(host);
1303
1304 if (!mmc_card_suspended(host->card))
1305 goto out;
1306
1307 mmc_power_up(host, host->card->ocr);
1308 err = mmc_sd_init_card(host, host->card->ocr, host->card);
1309 mmc_card_clr_suspended(host->card);
1310
1311 out:
1312 mmc_release_host(host);
1313 return err;
1314 }
1315
1316 /*
1317 * Callback for resume
1318 */
mmc_sd_resume(struct mmc_host * host)1319 static int mmc_sd_resume(struct mmc_host *host)
1320 {
1321 pm_runtime_enable(&host->card->dev);
1322 return 0;
1323 }
1324
1325 /*
1326 * Callback for runtime_suspend.
1327 */
mmc_sd_runtime_suspend(struct mmc_host * host)1328 static int mmc_sd_runtime_suspend(struct mmc_host *host)
1329 {
1330 int err;
1331
1332 if (!(host->caps & MMC_CAP_AGGRESSIVE_PM))
1333 return 0;
1334
1335 err = _mmc_sd_suspend(host);
1336 if (err)
1337 pr_err("%s: error %d doing aggressive suspend\n",
1338 mmc_hostname(host), err);
1339
1340 return err;
1341 }
1342
1343 /*
1344 * Callback for runtime_resume.
1345 */
mmc_sd_runtime_resume(struct mmc_host * host)1346 static int mmc_sd_runtime_resume(struct mmc_host *host)
1347 {
1348 int err;
1349
1350 err = _mmc_sd_resume(host);
1351 if (err && err != -ENOMEDIUM)
1352 pr_err("%s: error %d doing runtime resume\n",
1353 mmc_hostname(host), err);
1354
1355 return 0;
1356 }
1357
mmc_sd_hw_reset(struct mmc_host * host)1358 static int mmc_sd_hw_reset(struct mmc_host *host)
1359 {
1360 mmc_power_cycle(host, host->card->ocr);
1361 return mmc_sd_init_card(host, host->card->ocr, host->card);
1362 }
1363
1364 static const struct mmc_bus_ops mmc_sd_ops = {
1365 .remove = mmc_sd_remove,
1366 .detect = mmc_sd_detect,
1367 .runtime_suspend = mmc_sd_runtime_suspend,
1368 .runtime_resume = mmc_sd_runtime_resume,
1369 .suspend = mmc_sd_suspend,
1370 .resume = mmc_sd_resume,
1371 .alive = mmc_sd_alive,
1372 .shutdown = mmc_sd_shutdown,
1373 .hw_reset = mmc_sd_hw_reset,
1374 };
1375
1376 /*
1377 * Starting point for SD card init.
1378 */
mmc_attach_sd(struct mmc_host * host)1379 int mmc_attach_sd(struct mmc_host *host)
1380 {
1381 int err;
1382 u32 ocr, rocr;
1383
1384 WARN_ON(!host->claimed);
1385
1386 err = mmc_send_app_op_cond(host, 0, &ocr);
1387 if (err)
1388 return err;
1389
1390 mmc_attach_bus(host, &mmc_sd_ops);
1391 if (host->ocr_avail_sd)
1392 host->ocr_avail = host->ocr_avail_sd;
1393
1394 /*
1395 * We need to get OCR a different way for SPI.
1396 */
1397 if (mmc_host_is_spi(host)) {
1398 mmc_go_idle(host);
1399
1400 err = mmc_spi_read_ocr(host, 0, &ocr);
1401 if (err)
1402 goto err;
1403 }
1404
1405 /*
1406 * Some SD cards claims an out of spec VDD voltage range. Let's treat
1407 * these bits as being in-valid and especially also bit7.
1408 */
1409 ocr &= ~0x7FFF;
1410
1411 rocr = mmc_select_voltage(host, ocr);
1412
1413 /*
1414 * Can we support the voltage(s) of the card(s)?
1415 */
1416 if (!rocr) {
1417 err = -EINVAL;
1418 goto err;
1419 }
1420
1421 /*
1422 * Detect and init the card.
1423 */
1424 mdelay(1000);
1425 err = mmc_sd_init_card(host, rocr, NULL);
1426 if (err)
1427 goto err;
1428
1429 mmc_release_host(host);
1430 err = mmc_add_card(host->card);
1431 if (err)
1432 goto remove_card;
1433
1434 mmc_claim_host(host);
1435 return 0;
1436
1437 remove_card:
1438 mmc_remove_card(host->card);
1439 host->card = NULL;
1440 mmc_claim_host(host);
1441 err:
1442 mmc_detach_bus(host);
1443
1444 pr_err("%s: error %d whilst initialising SD card\n",
1445 mmc_hostname(host), err);
1446
1447 trace_android_vh_mmc_attach_sd(host, ocr, err);
1448
1449 return err;
1450 }
1451