1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * linux/drivers/mmc/sdio.c
4 *
5 * Copyright 2006-2007 Pierre Ossman
6 */
7
8 #include <linux/err.h>
9 #include <linux/pm_runtime.h>
10
11 #include <linux/mmc/host.h>
12 #include <linux/mmc/card.h>
13 #include <linux/mmc/mmc.h>
14 #include <linux/mmc/sdio.h>
15 #include <linux/mmc/sdio_func.h>
16 #include <linux/mmc/sdio_ids.h>
17
18 #include <trace/hooks/mmc.h>
19
20 #include "core.h"
21 #include "card.h"
22 #include "host.h"
23 #include "bus.h"
24 #include "quirks.h"
25 #include "sd.h"
26 #include "sdio_bus.h"
27 #include "mmc_ops.h"
28 #include "sd_ops.h"
29 #include "sdio_ops.h"
30 #include "sdio_cis.h"
31
32 MMC_DEV_ATTR(vendor, "0x%04x\n", card->cis.vendor);
33 MMC_DEV_ATTR(device, "0x%04x\n", card->cis.device);
34 MMC_DEV_ATTR(revision, "%u.%u\n", card->major_rev, card->minor_rev);
35 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
36 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
37
38 #define sdio_info_attr(num) \
39 static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf) \
40 { \
41 struct mmc_card *card = mmc_dev_to_card(dev); \
42 \
43 if (num > card->num_info) \
44 return -ENODATA; \
45 if (!card->info[num-1][0]) \
46 return 0; \
47 return sprintf(buf, "%s\n", card->info[num-1]); \
48 } \
49 static DEVICE_ATTR_RO(info##num)
50
51 sdio_info_attr(1);
52 sdio_info_attr(2);
53 sdio_info_attr(3);
54 sdio_info_attr(4);
55
56 static struct attribute *sdio_std_attrs[] = {
57 &dev_attr_vendor.attr,
58 &dev_attr_device.attr,
59 &dev_attr_revision.attr,
60 &dev_attr_info1.attr,
61 &dev_attr_info2.attr,
62 &dev_attr_info3.attr,
63 &dev_attr_info4.attr,
64 &dev_attr_ocr.attr,
65 &dev_attr_rca.attr,
66 NULL,
67 };
68 ATTRIBUTE_GROUPS(sdio_std);
69
70 static struct device_type sdio_type = {
71 .groups = sdio_std_groups,
72 };
73
sdio_read_fbr(struct sdio_func * func)74 static int sdio_read_fbr(struct sdio_func *func)
75 {
76 int ret;
77 unsigned char data;
78
79 if (mmc_card_nonstd_func_interface(func->card)) {
80 func->class = SDIO_CLASS_NONE;
81 return 0;
82 }
83
84 ret = mmc_io_rw_direct(func->card, 0, 0,
85 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
86 if (ret)
87 goto out;
88
89 data &= 0x0f;
90
91 if (data == 0x0f) {
92 ret = mmc_io_rw_direct(func->card, 0, 0,
93 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
94 if (ret)
95 goto out;
96 }
97
98 func->class = data;
99
100 out:
101 return ret;
102 }
103
sdio_init_func(struct mmc_card * card,unsigned int fn)104 static int sdio_init_func(struct mmc_card *card, unsigned int fn)
105 {
106 int ret;
107 struct sdio_func *func;
108
109 if (WARN_ON(fn > SDIO_MAX_FUNCS))
110 return -EINVAL;
111
112 func = sdio_alloc_func(card);
113 if (IS_ERR(func))
114 return PTR_ERR(func);
115
116 func->num = fn;
117
118 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
119 ret = sdio_read_fbr(func);
120 if (ret)
121 goto fail;
122
123 ret = sdio_read_func_cis(func);
124 if (ret)
125 goto fail;
126 } else {
127 func->vendor = func->card->cis.vendor;
128 func->device = func->card->cis.device;
129 func->max_blksize = func->card->cis.blksize;
130 }
131
132 card->sdio_func[fn - 1] = func;
133
134 return 0;
135
136 fail:
137 /*
138 * It is okay to remove the function here even though we hold
139 * the host lock as we haven't registered the device yet.
140 */
141 sdio_remove_func(func);
142 return ret;
143 }
144
sdio_read_cccr(struct mmc_card * card,u32 ocr)145 static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
146 {
147 int ret;
148 int cccr_vsn;
149 int uhs = ocr & R4_18V_PRESENT;
150 unsigned char data;
151 unsigned char speed;
152
153 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
154 if (ret)
155 goto out;
156
157 cccr_vsn = data & 0x0f;
158
159 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
160 pr_err("%s: unrecognised CCCR structure version %d\n",
161 mmc_hostname(card->host), cccr_vsn);
162 return -EINVAL;
163 }
164
165 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
166
167 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
168 if (ret)
169 goto out;
170
171 if (data & SDIO_CCCR_CAP_SMB)
172 card->cccr.multi_block = 1;
173 if (data & SDIO_CCCR_CAP_LSC)
174 card->cccr.low_speed = 1;
175 if (data & SDIO_CCCR_CAP_4BLS)
176 card->cccr.wide_bus = 1;
177
178 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
179 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
180 if (ret)
181 goto out;
182
183 if (data & SDIO_POWER_SMPC)
184 card->cccr.high_power = 1;
185 }
186
187 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
188 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
189 if (ret)
190 goto out;
191
192 card->scr.sda_spec3 = 0;
193 card->sw_caps.sd3_bus_mode = 0;
194 card->sw_caps.sd3_drv_type = 0;
195 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
196 card->scr.sda_spec3 = 1;
197 ret = mmc_io_rw_direct(card, 0, 0,
198 SDIO_CCCR_UHS, 0, &data);
199 if (ret)
200 goto out;
201
202 if (mmc_host_uhs(card->host)) {
203 if (data & SDIO_UHS_DDR50)
204 card->sw_caps.sd3_bus_mode
205 |= SD_MODE_UHS_DDR50 | SD_MODE_UHS_SDR50
206 | SD_MODE_UHS_SDR25 | SD_MODE_UHS_SDR12;
207
208 if (data & SDIO_UHS_SDR50)
209 card->sw_caps.sd3_bus_mode
210 |= SD_MODE_UHS_SDR50 | SD_MODE_UHS_SDR25
211 | SD_MODE_UHS_SDR12;
212
213 if (data & SDIO_UHS_SDR104)
214 card->sw_caps.sd3_bus_mode
215 |= SD_MODE_UHS_SDR104 | SD_MODE_UHS_SDR50
216 | SD_MODE_UHS_SDR25 | SD_MODE_UHS_SDR12;
217 }
218
219 ret = mmc_io_rw_direct(card, 0, 0,
220 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
221 if (ret)
222 goto out;
223
224 if (data & SDIO_DRIVE_SDTA)
225 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
226 if (data & SDIO_DRIVE_SDTC)
227 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
228 if (data & SDIO_DRIVE_SDTD)
229 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
230 }
231
232 /* if no uhs mode ensure we check for high speed */
233 if (!card->sw_caps.sd3_bus_mode) {
234 if (speed & SDIO_SPEED_SHS) {
235 card->cccr.high_speed = 1;
236 card->sw_caps.hs_max_dtr = 50000000;
237 } else {
238 card->cccr.high_speed = 0;
239 card->sw_caps.hs_max_dtr = 25000000;
240 }
241 }
242 }
243
244 out:
245 return ret;
246 }
247
sdio_enable_wide(struct mmc_card * card)248 static int sdio_enable_wide(struct mmc_card *card)
249 {
250 int ret;
251 u8 ctrl;
252
253 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
254 return 0;
255
256 if (card->cccr.low_speed && !card->cccr.wide_bus)
257 return 0;
258
259 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
260 if (ret)
261 return ret;
262
263 if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
264 pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
265 mmc_hostname(card->host), ctrl);
266
267 /* set as 4-bit bus width */
268 ctrl &= ~SDIO_BUS_WIDTH_MASK;
269 ctrl |= SDIO_BUS_WIDTH_4BIT;
270
271 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
272 if (ret)
273 return ret;
274
275 return 1;
276 }
277
278 /*
279 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
280 * of the card. This may be required on certain setups of boards,
281 * controllers and embedded sdio device which do not need the card's
282 * pull-up. As a result, card detection is disabled and power is saved.
283 */
sdio_disable_cd(struct mmc_card * card)284 static int sdio_disable_cd(struct mmc_card *card)
285 {
286 int ret;
287 u8 ctrl;
288
289 if (!mmc_card_disable_cd(card))
290 return 0;
291
292 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
293 if (ret)
294 return ret;
295
296 ctrl |= SDIO_BUS_CD_DISABLE;
297
298 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
299 }
300
301 /*
302 * Devices that remain active during a system suspend are
303 * put back into 1-bit mode.
304 */
sdio_disable_wide(struct mmc_card * card)305 static int sdio_disable_wide(struct mmc_card *card)
306 {
307 int ret;
308 u8 ctrl;
309
310 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
311 return 0;
312
313 if (card->cccr.low_speed && !card->cccr.wide_bus)
314 return 0;
315
316 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
317 if (ret)
318 return ret;
319
320 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
321 return 0;
322
323 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
324 ctrl |= SDIO_BUS_ASYNC_INT;
325
326 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
327 if (ret)
328 return ret;
329
330 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
331
332 return 0;
333 }
334
sdio_disable_4bit_bus(struct mmc_card * card)335 static int sdio_disable_4bit_bus(struct mmc_card *card)
336 {
337 int err;
338
339 if (card->type == MMC_TYPE_SDIO)
340 goto out;
341
342 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
343 return 0;
344
345 if (!(card->scr.bus_widths & SD_SCR_BUS_WIDTH_4))
346 return 0;
347
348 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
349 if (err)
350 return err;
351
352 out:
353 return sdio_disable_wide(card);
354 }
355
356
sdio_enable_4bit_bus(struct mmc_card * card)357 static int sdio_enable_4bit_bus(struct mmc_card *card)
358 {
359 int err;
360
361 err = sdio_enable_wide(card);
362 if (err <= 0)
363 return err;
364 if (card->type == MMC_TYPE_SDIO)
365 goto out;
366
367 if (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4) {
368 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
369 if (err) {
370 sdio_disable_wide(card);
371 return err;
372 }
373 }
374 out:
375 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
376
377 return 0;
378 }
379
380
381 /*
382 * Test if the card supports high-speed mode and, if so, switch to it.
383 */
mmc_sdio_switch_hs(struct mmc_card * card,int enable)384 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
385 {
386 int ret;
387 u8 speed;
388
389 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
390 return 0;
391
392 if (!card->cccr.high_speed)
393 return 0;
394
395 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
396 if (ret)
397 return ret;
398
399 if (enable)
400 speed |= SDIO_SPEED_EHS;
401 else
402 speed &= ~SDIO_SPEED_EHS;
403
404 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
405 if (ret)
406 return ret;
407
408 return 1;
409 }
410
411 /*
412 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
413 */
sdio_enable_hs(struct mmc_card * card)414 static int sdio_enable_hs(struct mmc_card *card)
415 {
416 int ret;
417
418 ret = mmc_sdio_switch_hs(card, true);
419 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
420 return ret;
421
422 ret = mmc_sd_switch_hs(card);
423 if (ret <= 0)
424 mmc_sdio_switch_hs(card, false);
425
426 return ret;
427 }
428
mmc_sdio_get_max_clock(struct mmc_card * card)429 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
430 {
431 unsigned max_dtr;
432
433 if (mmc_card_hs(card)) {
434 /*
435 * The SDIO specification doesn't mention how
436 * the CIS transfer speed register relates to
437 * high-speed, but it seems that 50 MHz is
438 * mandatory.
439 */
440 max_dtr = 50000000;
441 } else {
442 max_dtr = card->cis.max_dtr;
443 }
444
445 if (card->type == MMC_TYPE_SD_COMBO)
446 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
447
448 return max_dtr;
449 }
450
host_drive_to_sdio_drive(int host_strength)451 static unsigned char host_drive_to_sdio_drive(int host_strength)
452 {
453 switch (host_strength) {
454 case MMC_SET_DRIVER_TYPE_A:
455 return SDIO_DTSx_SET_TYPE_A;
456 case MMC_SET_DRIVER_TYPE_B:
457 return SDIO_DTSx_SET_TYPE_B;
458 case MMC_SET_DRIVER_TYPE_C:
459 return SDIO_DTSx_SET_TYPE_C;
460 case MMC_SET_DRIVER_TYPE_D:
461 return SDIO_DTSx_SET_TYPE_D;
462 default:
463 return SDIO_DTSx_SET_TYPE_B;
464 }
465 }
466
sdio_select_driver_type(struct mmc_card * card)467 static void sdio_select_driver_type(struct mmc_card *card)
468 {
469 int card_drv_type, drive_strength, drv_type;
470 unsigned char card_strength;
471 int err;
472
473 card->drive_strength = 0;
474
475 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
476
477 drive_strength = mmc_select_drive_strength(card,
478 card->sw_caps.uhs_max_dtr,
479 card_drv_type, &drv_type);
480
481 if (drive_strength) {
482 /* if error just use default for drive strength B */
483 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
484 &card_strength);
485 if (err)
486 return;
487
488 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
489 card_strength |= host_drive_to_sdio_drive(drive_strength);
490
491 /* if error default to drive strength B */
492 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
493 card_strength, NULL);
494 if (err)
495 return;
496 card->drive_strength = drive_strength;
497 }
498
499 if (drv_type)
500 mmc_set_driver_type(card->host, drv_type);
501 }
502
503
sdio_set_bus_speed_mode(struct mmc_card * card)504 static int sdio_set_bus_speed_mode(struct mmc_card *card)
505 {
506 unsigned int bus_speed, timing;
507 int err;
508 unsigned char speed;
509 unsigned int max_rate;
510
511 /*
512 * If the host doesn't support any of the UHS-I modes, fallback on
513 * default speed.
514 */
515 if (!mmc_host_uhs(card->host))
516 return 0;
517
518 bus_speed = SDIO_SPEED_SDR12;
519 timing = MMC_TIMING_UHS_SDR12;
520 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
521 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
522 bus_speed = SDIO_SPEED_SDR104;
523 timing = MMC_TIMING_UHS_SDR104;
524 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
525 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
526 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
527 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
528 bus_speed = SDIO_SPEED_DDR50;
529 timing = MMC_TIMING_UHS_DDR50;
530 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
531 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
532 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
533 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
534 SD_MODE_UHS_SDR50)) {
535 bus_speed = SDIO_SPEED_SDR50;
536 timing = MMC_TIMING_UHS_SDR50;
537 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
538 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
539 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
540 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
541 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
542 bus_speed = SDIO_SPEED_SDR25;
543 timing = MMC_TIMING_UHS_SDR25;
544 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
545 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
546 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
547 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
548 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
549 SD_MODE_UHS_SDR12)) {
550 bus_speed = SDIO_SPEED_SDR12;
551 timing = MMC_TIMING_UHS_SDR12;
552 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
553 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
554 }
555
556 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
557 if (err)
558 return err;
559
560 speed &= ~SDIO_SPEED_BSS_MASK;
561 speed |= bus_speed;
562 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
563 if (err)
564 return err;
565
566 max_rate = min_not_zero(card->quirk_max_rate,
567 card->sw_caps.uhs_max_dtr);
568
569 mmc_set_timing(card->host, timing);
570 mmc_set_clock(card->host, max_rate);
571
572 return 0;
573 }
574
575 /*
576 * UHS-I specific initialization procedure
577 */
mmc_sdio_init_uhs_card(struct mmc_card * card)578 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
579 {
580 int err;
581
582 if (!card->scr.sda_spec3)
583 return 0;
584
585 /* Switch to wider bus */
586 err = sdio_enable_4bit_bus(card);
587 if (err)
588 goto out;
589
590 /* Set the driver strength for the card */
591 sdio_select_driver_type(card);
592
593 /* Set bus speed mode of the card */
594 err = sdio_set_bus_speed_mode(card);
595 if (err)
596 goto out;
597
598 /*
599 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
600 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
601 */
602 if (!mmc_host_is_spi(card->host) &&
603 ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
604 (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
605 err = mmc_execute_tuning(card);
606 out:
607 return err;
608 }
609
mmc_sdio_pre_init(struct mmc_host * host,u32 ocr,struct mmc_card * card)610 static int mmc_sdio_pre_init(struct mmc_host *host, u32 ocr,
611 struct mmc_card *card)
612 {
613 if (card)
614 mmc_remove_card(card);
615
616 /*
617 * Reset the card by performing the same steps that are taken by
618 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
619 *
620 * sdio_reset() is technically not needed. Having just powered up the
621 * hardware, it should already be in reset state. However, some
622 * platforms (such as SD8686 on OLPC) do not instantly cut power,
623 * meaning that a reset is required when restoring power soon after
624 * powering off. It is harmless in other cases.
625 *
626 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
627 * is not necessary for non-removable cards. However, it is required
628 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
629 * harmless in other situations.
630 *
631 */
632
633 sdio_reset(host);
634 mmc_go_idle(host);
635 mmc_send_if_cond(host, ocr);
636 return mmc_send_io_op_cond(host, 0, NULL);
637 }
638
639 /*
640 * Handle the detection and initialisation of a card.
641 *
642 * In the case of a resume, "oldcard" will contain the card
643 * we're trying to reinitialise.
644 */
mmc_sdio_init_card(struct mmc_host * host,u32 ocr,struct mmc_card * oldcard)645 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
646 struct mmc_card *oldcard)
647 {
648 struct mmc_card *card;
649 int err;
650 int retries = 10;
651 u32 rocr = 0;
652 u32 ocr_card = ocr;
653
654 WARN_ON(!host->claimed);
655
656 /* to query card if 1.8V signalling is supported */
657 if (mmc_host_uhs(host))
658 ocr |= R4_18V_PRESENT;
659
660 try_again:
661 if (!retries) {
662 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
663 ocr &= ~R4_18V_PRESENT;
664 }
665
666 /*
667 * Inform the card of the voltage
668 */
669 err = mmc_send_io_op_cond(host, ocr, &rocr);
670 if (err)
671 return err;
672
673 /*
674 * For SPI, enable CRC as appropriate.
675 */
676 if (mmc_host_is_spi(host)) {
677 err = mmc_spi_set_crc(host, use_spi_crc);
678 if (err)
679 return err;
680 }
681
682 /*
683 * Allocate card structure.
684 */
685 card = mmc_alloc_card(host, &sdio_type);
686 if (IS_ERR(card))
687 return PTR_ERR(card);
688
689 if ((rocr & R4_MEMORY_PRESENT) &&
690 mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
691 card->type = MMC_TYPE_SD_COMBO;
692
693 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
694 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
695 err = -ENOENT;
696 goto mismatch;
697 }
698 } else {
699 card->type = MMC_TYPE_SDIO;
700
701 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
702 err = -ENOENT;
703 goto mismatch;
704 }
705 }
706
707 /*
708 * Call the optional HC's init_card function to handle quirks.
709 */
710 if (host->ops->init_card)
711 host->ops->init_card(host, card);
712
713 card->ocr = ocr_card;
714
715 /*
716 * If the host and card support UHS-I mode request the card
717 * to switch to 1.8V signaling level. No 1.8v signalling if
718 * UHS mode is not enabled to maintain compatibility and some
719 * systems that claim 1.8v signalling in fact do not support
720 * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
721 * 1.8v, the card sets S18A to 0 in the R4 response. So it will
722 * fails to check rocr & R4_18V_PRESENT, but we still need to
723 * try to init uhs card. sdio_read_cccr will take over this task
724 * to make sure which speed mode should work.
725 */
726 if (rocr & ocr & R4_18V_PRESENT) {
727 err = mmc_set_uhs_voltage(host, ocr_card);
728 if (err == -EAGAIN) {
729 mmc_sdio_pre_init(host, ocr_card, card);
730 retries--;
731 goto try_again;
732 } else if (err) {
733 ocr &= ~R4_18V_PRESENT;
734 }
735 }
736
737 /*
738 * For native busses: set card RCA and quit open drain mode.
739 */
740 if (!mmc_host_is_spi(host)) {
741 err = mmc_send_relative_addr(host, &card->rca);
742 if (err)
743 goto remove;
744
745 /*
746 * Update oldcard with the new RCA received from the SDIO
747 * device -- we're doing this so that it's updated in the
748 * "card" struct when oldcard overwrites that later.
749 */
750 if (oldcard)
751 oldcard->rca = card->rca;
752 }
753
754 /*
755 * Read CSD, before selecting the card
756 */
757 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
758 err = mmc_sd_get_csd(card);
759 if (err)
760 goto remove;
761
762 mmc_decode_cid(card);
763 }
764
765 /*
766 * Select card, as all following commands rely on that.
767 */
768 if (!mmc_host_is_spi(host)) {
769 err = mmc_select_card(card);
770 if (err)
771 goto remove;
772 }
773
774 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
775 /*
776 * This is non-standard SDIO device, meaning it doesn't
777 * have any CIA (Common I/O area) registers present.
778 * It's host's responsibility to fill cccr and cis
779 * structures in init_card().
780 */
781 mmc_set_clock(host, card->cis.max_dtr);
782
783 if (card->cccr.high_speed) {
784 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
785 }
786
787 if (oldcard)
788 mmc_remove_card(card);
789 else
790 host->card = card;
791
792 return 0;
793 }
794
795 /*
796 * Read the common registers. Note that we should try to
797 * validate whether UHS would work or not.
798 */
799 err = sdio_read_cccr(card, ocr);
800 if (err) {
801 mmc_sdio_pre_init(host, ocr_card, card);
802 if (ocr & R4_18V_PRESENT) {
803 /* Retry init sequence, but without R4_18V_PRESENT. */
804 retries = 0;
805 goto try_again;
806 }
807 return err;
808 }
809
810 /*
811 * Read the common CIS tuples.
812 */
813 err = sdio_read_common_cis(card);
814 if (err)
815 goto remove;
816
817 if (oldcard) {
818 if (card->cis.vendor == oldcard->cis.vendor &&
819 card->cis.device == oldcard->cis.device) {
820 mmc_remove_card(card);
821 card = oldcard;
822 } else {
823 err = -ENOENT;
824 goto mismatch;
825 }
826 }
827
828 mmc_fixup_device(card, sdio_fixup_methods);
829
830 if (card->type == MMC_TYPE_SD_COMBO) {
831 err = mmc_sd_setup_card(host, card, oldcard != NULL);
832 /* handle as SDIO-only card if memory init failed */
833 if (err) {
834 mmc_go_idle(host);
835 if (mmc_host_is_spi(host))
836 /* should not fail, as it worked previously */
837 mmc_spi_set_crc(host, use_spi_crc);
838 card->type = MMC_TYPE_SDIO;
839 } else
840 card->dev.type = &sd_type;
841 }
842
843 /*
844 * If needed, disconnect card detection pull-up resistor.
845 */
846 err = sdio_disable_cd(card);
847 if (err)
848 goto remove;
849
850 /* Initialization sequence for UHS-I cards */
851 /* Only if card supports 1.8v and UHS signaling */
852 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
853 err = mmc_sdio_init_uhs_card(card);
854 if (err)
855 goto remove;
856 } else {
857 /*
858 * Switch to high-speed (if supported).
859 */
860 err = sdio_enable_hs(card);
861 if (err > 0)
862 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
863 else if (err)
864 goto remove;
865
866 /*
867 * Change to the card's maximum speed.
868 */
869 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
870
871 /*
872 * Switch to wider bus (if supported).
873 */
874 err = sdio_enable_4bit_bus(card);
875 if (err)
876 goto remove;
877 }
878
879 if (host->caps2 & MMC_CAP2_AVOID_3_3V &&
880 host->ios.signal_voltage == MMC_SIGNAL_VOLTAGE_330) {
881 pr_err("%s: Host failed to negotiate down from 3.3V\n",
882 mmc_hostname(host));
883 err = -EINVAL;
884 goto remove;
885 }
886
887 host->card = card;
888 return 0;
889
890 mismatch:
891 pr_debug("%s: Perhaps the card was replaced\n", mmc_hostname(host));
892 remove:
893 if (oldcard != card)
894 mmc_remove_card(card);
895 return err;
896 }
897
mmc_sdio_reinit_card(struct mmc_host * host)898 static int mmc_sdio_reinit_card(struct mmc_host *host)
899 {
900 int ret;
901
902 ret = mmc_sdio_pre_init(host, host->card->ocr, NULL);
903 if (ret)
904 return ret;
905
906 return mmc_sdio_init_card(host, host->card->ocr, host->card);
907 }
908
909 /*
910 * Host is being removed. Free up the current card.
911 */
mmc_sdio_remove(struct mmc_host * host)912 static void mmc_sdio_remove(struct mmc_host *host)
913 {
914 int i;
915
916 for (i = 0;i < host->card->sdio_funcs;i++) {
917 if (host->card->sdio_func[i]) {
918 sdio_remove_func(host->card->sdio_func[i]);
919 host->card->sdio_func[i] = NULL;
920 }
921 }
922
923 mmc_remove_card(host->card);
924 host->card = NULL;
925 }
926
927 /*
928 * Card detection - card is alive.
929 */
mmc_sdio_alive(struct mmc_host * host)930 static int mmc_sdio_alive(struct mmc_host *host)
931 {
932 return mmc_select_card(host->card);
933 }
934
935 /*
936 * Card detection callback from host.
937 */
mmc_sdio_detect(struct mmc_host * host)938 static void mmc_sdio_detect(struct mmc_host *host)
939 {
940 int err;
941
942 /* Make sure card is powered before detecting it */
943 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
944 err = pm_runtime_resume_and_get(&host->card->dev);
945 if (err < 0)
946 goto out;
947 }
948
949 mmc_claim_host(host);
950
951 /*
952 * Just check if our card has been removed.
953 */
954 err = _mmc_detect_card_removed(host);
955
956 mmc_release_host(host);
957
958 /*
959 * Tell PM core it's OK to power off the card now.
960 *
961 * The _sync variant is used in order to ensure that the card
962 * is left powered off in case an error occurred, and the card
963 * is going to be removed.
964 *
965 * Since there is no specific reason to believe a new user
966 * is about to show up at this point, the _sync variant is
967 * desirable anyway.
968 */
969 if (host->caps & MMC_CAP_POWER_OFF_CARD)
970 pm_runtime_put_sync(&host->card->dev);
971
972 out:
973 if (err) {
974 mmc_sdio_remove(host);
975
976 mmc_claim_host(host);
977 mmc_detach_bus(host);
978 mmc_power_off(host);
979 mmc_release_host(host);
980 }
981 }
982
983 /*
984 * SDIO pre_suspend. We need to suspend all functions separately.
985 * Therefore all registered functions must have drivers with suspend
986 * and resume methods. Failing that we simply remove the whole card.
987 */
mmc_sdio_pre_suspend(struct mmc_host * host)988 static int mmc_sdio_pre_suspend(struct mmc_host *host)
989 {
990 int i;
991
992 for (i = 0; i < host->card->sdio_funcs; i++) {
993 struct sdio_func *func = host->card->sdio_func[i];
994 if (func && sdio_func_present(func) && func->dev.driver) {
995 const struct dev_pm_ops *pmops = func->dev.driver->pm;
996 if (!pmops || !pmops->suspend || !pmops->resume)
997 /* force removal of entire card in that case */
998 goto remove;
999 }
1000 }
1001
1002 return 0;
1003
1004 remove:
1005 if (!mmc_card_is_removable(host)) {
1006 dev_warn(mmc_dev(host),
1007 "missing suspend/resume ops for non-removable SDIO card\n");
1008 /* Don't remove a non-removable card - we can't re-detect it. */
1009 return 0;
1010 }
1011
1012 /* Remove the SDIO card and let it be re-detected later on. */
1013 mmc_sdio_remove(host);
1014 mmc_claim_host(host);
1015 mmc_detach_bus(host);
1016 mmc_power_off(host);
1017 mmc_release_host(host);
1018 host->pm_flags = 0;
1019
1020 return 0;
1021 }
1022
1023 /*
1024 * SDIO suspend. Suspend all functions separately.
1025 */
mmc_sdio_suspend(struct mmc_host * host)1026 static int mmc_sdio_suspend(struct mmc_host *host)
1027 {
1028 WARN_ON(host->sdio_irqs && !mmc_card_keep_power(host));
1029
1030 /* Prevent processing of SDIO IRQs in suspended state. */
1031 mmc_card_set_suspended(host->card);
1032 cancel_delayed_work_sync(&host->sdio_irq_work);
1033
1034 mmc_claim_host(host);
1035
1036 if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
1037 sdio_disable_4bit_bus(host->card);
1038
1039 if (!mmc_card_keep_power(host)) {
1040 mmc_power_off(host);
1041 } else if (host->retune_period) {
1042 mmc_retune_timer_stop(host);
1043 mmc_retune_needed(host);
1044 }
1045
1046 mmc_release_host(host);
1047
1048 return 0;
1049 }
1050
mmc_sdio_resume(struct mmc_host * host)1051 static int mmc_sdio_resume(struct mmc_host *host)
1052 {
1053 int err = 0;
1054
1055 /* Basic card reinitialization. */
1056 mmc_claim_host(host);
1057
1058 /*
1059 * Restore power and reinitialize the card when needed. Note that a
1060 * removable card is checked from a detect work later on in the resume
1061 * process.
1062 */
1063 if (!mmc_card_keep_power(host)) {
1064 mmc_power_up(host, host->card->ocr);
1065 /*
1066 * Tell runtime PM core we just powered up the card,
1067 * since it still believes the card is powered off.
1068 * Note that currently runtime PM is only enabled
1069 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1070 */
1071 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1072 pm_runtime_disable(&host->card->dev);
1073 pm_runtime_set_active(&host->card->dev);
1074 pm_runtime_enable(&host->card->dev);
1075 }
1076 err = mmc_sdio_reinit_card(host);
1077 } else if (mmc_card_wake_sdio_irq(host)) {
1078 /*
1079 * We may have switched to 1-bit mode during suspend,
1080 * need to hold retuning, because tuning only supprt
1081 * 4-bit mode or 8 bit mode.
1082 */
1083 mmc_retune_hold_now(host);
1084 err = sdio_enable_4bit_bus(host->card);
1085 mmc_retune_release(host);
1086 }
1087
1088 if (err)
1089 goto out;
1090
1091 /* Allow SDIO IRQs to be processed again. */
1092 mmc_card_clr_suspended(host->card);
1093
1094 if (host->sdio_irqs) {
1095 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
1096 wake_up_process(host->sdio_irq_thread);
1097 else if (host->caps & MMC_CAP_SDIO_IRQ)
1098 queue_delayed_work(system_wq, &host->sdio_irq_work, 0);
1099 }
1100
1101 out:
1102 mmc_release_host(host);
1103
1104 host->pm_flags &= ~MMC_PM_KEEP_POWER;
1105 trace_android_vh_mmc_sdio_pm_flag_set(host);
1106
1107 return err;
1108 }
1109
mmc_sdio_runtime_suspend(struct mmc_host * host)1110 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1111 {
1112 /* No references to the card, cut the power to it. */
1113 mmc_claim_host(host);
1114 mmc_power_off(host);
1115 mmc_release_host(host);
1116
1117 return 0;
1118 }
1119
mmc_sdio_runtime_resume(struct mmc_host * host)1120 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1121 {
1122 int ret;
1123
1124 /* Restore power and re-initialize. */
1125 mmc_claim_host(host);
1126 mmc_power_up(host, host->card->ocr);
1127 ret = mmc_sdio_reinit_card(host);
1128 mmc_release_host(host);
1129
1130 return ret;
1131 }
1132
1133 /*
1134 * SDIO HW reset
1135 *
1136 * Returns 0 if the HW reset was executed synchronously, returns 1 if the HW
1137 * reset was asynchronously scheduled, else a negative error code.
1138 */
mmc_sdio_hw_reset(struct mmc_host * host)1139 static int mmc_sdio_hw_reset(struct mmc_host *host)
1140 {
1141 struct mmc_card *card = host->card;
1142
1143 /*
1144 * In case the card is shared among multiple func drivers, reset the
1145 * card through a rescan work. In this way it will be removed and
1146 * re-detected, thus all func drivers becomes informed about it.
1147 */
1148 if (atomic_read(&card->sdio_funcs_probed) > 1) {
1149 if (mmc_card_removed(card))
1150 return 1;
1151 host->rescan_entered = 0;
1152 mmc_card_set_removed(card);
1153 _mmc_detect_change(host, 0, false);
1154 return 1;
1155 }
1156
1157 /*
1158 * A single func driver has been probed, then let's skip the heavy
1159 * hotplug dance above and execute the reset immediately.
1160 */
1161 mmc_power_cycle(host, card->ocr);
1162 return mmc_sdio_reinit_card(host);
1163 }
1164
mmc_sdio_sw_reset(struct mmc_host * host)1165 static int mmc_sdio_sw_reset(struct mmc_host *host)
1166 {
1167 mmc_set_clock(host, host->f_init);
1168 sdio_reset(host);
1169 mmc_go_idle(host);
1170
1171 mmc_set_initial_state(host);
1172 mmc_set_initial_signal_voltage(host);
1173
1174 return mmc_sdio_reinit_card(host);
1175 }
1176
1177 static const struct mmc_bus_ops mmc_sdio_ops = {
1178 .remove = mmc_sdio_remove,
1179 .detect = mmc_sdio_detect,
1180 .pre_suspend = mmc_sdio_pre_suspend,
1181 .suspend = mmc_sdio_suspend,
1182 .resume = mmc_sdio_resume,
1183 .runtime_suspend = mmc_sdio_runtime_suspend,
1184 .runtime_resume = mmc_sdio_runtime_resume,
1185 .alive = mmc_sdio_alive,
1186 .hw_reset = mmc_sdio_hw_reset,
1187 .sw_reset = mmc_sdio_sw_reset,
1188 };
1189
1190
1191 /*
1192 * Starting point for SDIO card init.
1193 */
mmc_attach_sdio(struct mmc_host * host)1194 int mmc_attach_sdio(struct mmc_host *host)
1195 {
1196 int err, i, funcs;
1197 u32 ocr, rocr;
1198 struct mmc_card *card;
1199
1200 WARN_ON(!host->claimed);
1201
1202 err = mmc_send_io_op_cond(host, 0, &ocr);
1203 if (err)
1204 return err;
1205
1206 mmc_attach_bus(host, &mmc_sdio_ops);
1207 if (host->ocr_avail_sdio)
1208 host->ocr_avail = host->ocr_avail_sdio;
1209
1210
1211 rocr = mmc_select_voltage(host, ocr);
1212
1213 /*
1214 * Can we support the voltage(s) of the card(s)?
1215 */
1216 if (!rocr) {
1217 err = -EINVAL;
1218 goto err;
1219 }
1220
1221 /*
1222 * Detect and init the card.
1223 */
1224 err = mmc_sdio_init_card(host, rocr, NULL);
1225 if (err)
1226 goto err;
1227
1228 card = host->card;
1229
1230 /*
1231 * Enable runtime PM only if supported by host+card+board
1232 */
1233 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1234 /*
1235 * Do not allow runtime suspend until after SDIO function
1236 * devices are added.
1237 */
1238 pm_runtime_get_noresume(&card->dev);
1239
1240 /*
1241 * Let runtime PM core know our card is active
1242 */
1243 err = pm_runtime_set_active(&card->dev);
1244 if (err)
1245 goto remove;
1246
1247 /*
1248 * Enable runtime PM for this card
1249 */
1250 pm_runtime_enable(&card->dev);
1251 }
1252
1253 /*
1254 * The number of functions on the card is encoded inside
1255 * the ocr.
1256 */
1257 funcs = (ocr & 0x70000000) >> 28;
1258 card->sdio_funcs = 0;
1259
1260 /*
1261 * Initialize (but don't add) all present functions.
1262 */
1263 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1264 err = sdio_init_func(host->card, i + 1);
1265 if (err)
1266 goto remove;
1267
1268 /*
1269 * Enable Runtime PM for this func (if supported)
1270 */
1271 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1272 pm_runtime_enable(&card->sdio_func[i]->dev);
1273 }
1274
1275 /*
1276 * First add the card to the driver model...
1277 */
1278 mmc_release_host(host);
1279 err = mmc_add_card(host->card);
1280 if (err)
1281 goto remove_added;
1282
1283 /*
1284 * ...then the SDIO functions.
1285 */
1286 for (i = 0;i < funcs;i++) {
1287 err = sdio_add_func(host->card->sdio_func[i]);
1288 if (err)
1289 goto remove_added;
1290 }
1291
1292 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1293 pm_runtime_put(&card->dev);
1294
1295 mmc_claim_host(host);
1296 return 0;
1297
1298
1299 remove:
1300 mmc_release_host(host);
1301 remove_added:
1302 /*
1303 * The devices are being deleted so it is not necessary to disable
1304 * runtime PM. Similarly we also don't pm_runtime_put() the SDIO card
1305 * because it needs to be active to remove any function devices that
1306 * were probed, and after that it gets deleted.
1307 */
1308 mmc_sdio_remove(host);
1309 mmc_claim_host(host);
1310 err:
1311 mmc_detach_bus(host);
1312
1313 pr_err("%s: error %d whilst initialising SDIO card\n",
1314 mmc_hostname(host), err);
1315
1316 return err;
1317 }
1318
1319