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