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