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