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