]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - drivers/mmc/core/sdio.c
mmc: mmc_card_keep_power cleanups
[mirror_ubuntu-jammy-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>
35c66c19 17#include <linux/mmc/sdio.h>
e29a7d73 18#include <linux/mmc/sdio_func.h>
5c4e6f13
PO
19
20#include "core.h"
21#include "bus.h"
71578a1e 22#include "sd.h"
e29a7d73 23#include "sdio_bus.h"
5c4e6f13
PO
24#include "mmc_ops.h"
25#include "sd_ops.h"
26#include "sdio_ops.h"
b7261261 27#include "sdio_cis.h"
5c4e6f13 28
0597007f
PO
29static int sdio_read_fbr(struct sdio_func *func)
30{
31 int ret;
32 unsigned char data;
33
34 ret = mmc_io_rw_direct(func->card, 0, 0,
7616ee95 35 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
0597007f
PO
36 if (ret)
37 goto out;
38
39 data &= 0x0f;
40
41 if (data == 0x0f) {
42 ret = mmc_io_rw_direct(func->card, 0, 0,
7616ee95 43 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
0597007f
PO
44 if (ret)
45 goto out;
46 }
47
48 func->class = data;
49
50out:
51 return ret;
52}
53
e29a7d73
PO
54static int sdio_init_func(struct mmc_card *card, unsigned int fn)
55{
0597007f 56 int ret;
e29a7d73
PO
57 struct sdio_func *func;
58
59 BUG_ON(fn > SDIO_MAX_FUNCS);
60
61 func = sdio_alloc_func(card);
62 if (IS_ERR(func))
63 return PTR_ERR(func);
64
65 func->num = fn;
66
6f51be3d
GI
67 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
68 ret = sdio_read_fbr(func);
69 if (ret)
70 goto fail;
0597007f 71
6f51be3d
GI
72 ret = sdio_read_func_cis(func);
73 if (ret)
74 goto fail;
75 } else {
76 func->vendor = func->card->cis.vendor;
77 func->device = func->card->cis.device;
78 func->max_blksize = func->card->cis.blksize;
79 }
b7261261 80
e29a7d73
PO
81 card->sdio_func[fn - 1] = func;
82
83 return 0;
0597007f
PO
84
85fail:
86 /*
87 * It is okay to remove the function here even though we hold
88 * the host lock as we haven't registered the device yet.
89 */
90 sdio_remove_func(func);
91 return ret;
e29a7d73
PO
92}
93
35c66c19
PO
94static int sdio_read_cccr(struct mmc_card *card)
95{
96 int ret;
97 int cccr_vsn;
98 unsigned char data;
99
100 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
101
102 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
103 if (ret)
104 goto out;
105
106 cccr_vsn = data & 0x0f;
107
108 if (cccr_vsn > SDIO_CCCR_REV_1_20) {
109 printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n",
110 mmc_hostname(card->host), cccr_vsn);
111 return -EINVAL;
112 }
113
114 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
115
116 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
117 if (ret)
118 goto out;
119
120 if (data & SDIO_CCCR_CAP_SMB)
121 card->cccr.multi_block = 1;
122 if (data & SDIO_CCCR_CAP_LSC)
123 card->cccr.low_speed = 1;
124 if (data & SDIO_CCCR_CAP_4BLS)
125 card->cccr.wide_bus = 1;
126
127 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
128 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
129 if (ret)
130 goto out;
131
132 if (data & SDIO_POWER_SMPC)
133 card->cccr.high_power = 1;
134 }
135
136 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
137 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data);
138 if (ret)
139 goto out;
140
141 if (data & SDIO_SPEED_SHS)
142 card->cccr.high_speed = 1;
143 }
144
145out:
146 return ret;
147}
148
4ff6471c
PO
149static int sdio_enable_wide(struct mmc_card *card)
150{
151 int ret;
152 u8 ctrl;
153
154 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
155 return 0;
156
157 if (card->cccr.low_speed && !card->cccr.wide_bus)
158 return 0;
159
160 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
161 if (ret)
162 return ret;
163
164 ctrl |= SDIO_BUS_WIDTH_4BIT;
165
166 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
167 if (ret)
168 return ret;
169
7310ece8 170 return 1;
4ff6471c
PO
171}
172
006ebd5d
OBC
173/*
174 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
175 * of the card. This may be required on certain setups of boards,
176 * controllers and embedded sdio device which do not need the card's
177 * pull-up. As a result, card detection is disabled and power is saved.
178 */
179static int sdio_disable_cd(struct mmc_card *card)
180{
181 int ret;
182 u8 ctrl;
183
184 if (!card->cccr.disable_cd)
185 return 0;
186
187 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
188 if (ret)
189 return ret;
190
191 ctrl |= SDIO_BUS_CD_DISABLE;
192
193 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
194}
195
6b5eda36
DD
196/*
197 * Devices that remain active during a system suspend are
198 * put back into 1-bit mode.
199 */
200static int sdio_disable_wide(struct mmc_card *card)
201{
202 int ret;
203 u8 ctrl;
204
205 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
206 return 0;
207
208 if (card->cccr.low_speed && !card->cccr.wide_bus)
209 return 0;
210
211 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
212 if (ret)
213 return ret;
214
215 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
216 return 0;
217
218 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
219 ctrl |= SDIO_BUS_ASYNC_INT;
220
221 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
222 if (ret)
223 return ret;
224
225 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
226
227 return 0;
228}
229
7310ece8
MM
230
231static int sdio_enable_4bit_bus(struct mmc_card *card)
232{
233 int err;
234
235 if (card->type == MMC_TYPE_SDIO)
236 return sdio_enable_wide(card);
237
238 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
239 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
240 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
241 if (err)
242 return err;
243 } else
244 return 0;
245
246 err = sdio_enable_wide(card);
247 if (err <= 0)
248 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
249
250 return err;
251}
252
253
d16f5770
PO
254/*
255 * Test if the card supports high-speed mode and, if so, switch to it.
256 */
7310ece8 257static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
d16f5770
PO
258{
259 int ret;
260 u8 speed;
261
262 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
263 return 0;
264
265 if (!card->cccr.high_speed)
266 return 0;
267
268 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
269 if (ret)
270 return ret;
271
7310ece8
MM
272 if (enable)
273 speed |= SDIO_SPEED_EHS;
274 else
275 speed &= ~SDIO_SPEED_EHS;
d16f5770
PO
276
277 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
278 if (ret)
279 return ret;
280
71578a1e
MM
281 return 1;
282}
d16f5770 283
7310ece8
MM
284/*
285 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
286 */
287static int sdio_enable_hs(struct mmc_card *card)
288{
289 int ret;
290
291 ret = mmc_sdio_switch_hs(card, true);
292 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
293 return ret;
294
295 ret = mmc_sd_switch_hs(card);
296 if (ret <= 0)
297 mmc_sdio_switch_hs(card, false);
298
299 return ret;
300}
301
71578a1e
MM
302static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
303{
304 unsigned max_dtr;
305
306 if (mmc_card_highspeed(card)) {
307 /*
308 * The SDIO specification doesn't mention how
309 * the CIS transfer speed register relates to
310 * high-speed, but it seems that 50 MHz is
311 * mandatory.
312 */
313 max_dtr = 50000000;
314 } else {
315 max_dtr = card->cis.max_dtr;
316 }
317
7310ece8
MM
318 if (card->type == MMC_TYPE_SD_COMBO)
319 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
320
71578a1e 321 return max_dtr;
d16f5770
PO
322}
323
17d33e14
NP
324/*
325 * Handle the detection and initialisation of a card.
326 *
327 * In the case of a resume, "oldcard" will contain the card
328 * we're trying to reinitialise.
329 */
330static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
3bca4cf7 331 struct mmc_card *oldcard, int powered_resume)
17d33e14
NP
332{
333 struct mmc_card *card;
334 int err;
335
336 BUG_ON(!host);
337 WARN_ON(!host->claimed);
338
339 /*
340 * Inform the card of the voltage
341 */
3bca4cf7
CB
342 if (!powered_resume) {
343 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
344 if (err)
345 goto err;
346 }
17d33e14
NP
347
348 /*
349 * For SPI, enable CRC as appropriate.
350 */
351 if (mmc_host_is_spi(host)) {
352 err = mmc_spi_set_crc(host, use_spi_crc);
353 if (err)
354 goto err;
355 }
356
357 /*
358 * Allocate card structure.
359 */
360 card = mmc_alloc_card(host, NULL);
361 if (IS_ERR(card)) {
362 err = PTR_ERR(card);
363 goto err;
364 }
365
f3c65b28
DV
366 if (ocr & R4_MEMORY_PRESENT
367 && mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid) == 0) {
7310ece8
MM
368 card->type = MMC_TYPE_SD_COMBO;
369
370 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
371 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
372 mmc_remove_card(card);
373 return -ENOENT;
374 }
375 } else {
376 card->type = MMC_TYPE_SDIO;
377
378 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
379 mmc_remove_card(card);
380 return -ENOENT;
381 }
382 }
17d33e14 383
3fcb027d
DM
384 /*
385 * Call the optional HC's init_card function to handle quirks.
386 */
387 if (host->ops->init_card)
388 host->ops->init_card(host, card);
389
17d33e14
NP
390 /*
391 * For native busses: set card RCA and quit open drain mode.
392 */
3bca4cf7 393 if (!powered_resume && !mmc_host_is_spi(host)) {
17d33e14
NP
394 err = mmc_send_relative_addr(host, &card->rca);
395 if (err)
396 goto remove;
397
0aab3995
SNX
398 /*
399 * Update oldcard with the new RCA received from the SDIO
400 * device -- we're doing this so that it's updated in the
401 * "card" struct when oldcard overwrites that later.
402 */
403 if (oldcard)
404 oldcard->rca = card->rca;
405
17d33e14
NP
406 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
407 }
408
7310ece8
MM
409 /*
410 * Read CSD, before selecting the card
411 */
412 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
413 err = mmc_sd_get_csd(host, card);
414 if (err)
415 return err;
416
417 mmc_decode_cid(card);
418 }
419
17d33e14
NP
420 /*
421 * Select card, as all following commands rely on that.
422 */
3bca4cf7 423 if (!powered_resume && !mmc_host_is_spi(host)) {
17d33e14
NP
424 err = mmc_select_card(card);
425 if (err)
426 goto remove;
427 }
428
6f51be3d
GI
429 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
430 /*
431 * This is non-standard SDIO device, meaning it doesn't
432 * have any CIA (Common I/O area) registers present.
433 * It's host's responsibility to fill cccr and cis
434 * structures in init_card().
435 */
436 mmc_set_clock(host, card->cis.max_dtr);
437
438 if (card->cccr.high_speed) {
439 mmc_card_set_highspeed(card);
440 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
441 }
442
443 goto finish;
444 }
445
17d33e14
NP
446 /*
447 * Read the common registers.
448 */
449 err = sdio_read_cccr(card);
450 if (err)
451 goto remove;
452
453 /*
454 * Read the common CIS tuples.
455 */
456 err = sdio_read_common_cis(card);
457 if (err)
458 goto remove;
459
460 if (oldcard) {
461 int same = (card->cis.vendor == oldcard->cis.vendor &&
462 card->cis.device == oldcard->cis.device);
463 mmc_remove_card(card);
7310ece8
MM
464 if (!same)
465 return -ENOENT;
466
17d33e14
NP
467 card = oldcard;
468 }
57f0adc7 469 mmc_fixup_device(card);
17d33e14 470
7310ece8
MM
471 if (card->type == MMC_TYPE_SD_COMBO) {
472 err = mmc_sd_setup_card(host, card, oldcard != NULL);
473 /* handle as SDIO-only card if memory init failed */
474 if (err) {
475 mmc_go_idle(host);
476 if (mmc_host_is_spi(host))
477 /* should not fail, as it worked previously */
478 mmc_spi_set_crc(host, use_spi_crc);
479 card->type = MMC_TYPE_SDIO;
480 } else
481 card->dev.type = &sd_type;
482 }
483
484 /*
485 * If needed, disconnect card detection pull-up resistor.
486 */
487 err = sdio_disable_cd(card);
488 if (err)
489 goto remove;
490
17d33e14
NP
491 /*
492 * Switch to high-speed (if supported).
493 */
494 err = sdio_enable_hs(card);
71578a1e
MM
495 if (err > 0)
496 mmc_sd_go_highspeed(card);
497 else if (err)
17d33e14
NP
498 goto remove;
499
500 /*
501 * Change to the card's maximum speed.
502 */
71578a1e 503 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
17d33e14
NP
504
505 /*
506 * Switch to wider bus (if supported).
507 */
7310ece8
MM
508 err = sdio_enable_4bit_bus(card);
509 if (err > 0)
510 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
511 else if (err)
17d33e14
NP
512 goto remove;
513
6f51be3d 514finish:
17d33e14
NP
515 if (!oldcard)
516 host->card = card;
517 return 0;
518
519remove:
520 if (!oldcard)
521 mmc_remove_card(card);
522
523err:
524 return err;
525}
526
5c4e6f13
PO
527/*
528 * Host is being removed. Free up the current card.
529 */
530static void mmc_sdio_remove(struct mmc_host *host)
531{
e29a7d73
PO
532 int i;
533
5c4e6f13
PO
534 BUG_ON(!host);
535 BUG_ON(!host->card);
536
e29a7d73
PO
537 for (i = 0;i < host->card->sdio_funcs;i++) {
538 if (host->card->sdio_func[i]) {
539 sdio_remove_func(host->card->sdio_func[i]);
540 host->card->sdio_func[i] = NULL;
541 }
542 }
543
5c4e6f13
PO
544 mmc_remove_card(host->card);
545 host->card = NULL;
546}
547
548/*
549 * Card detection callback from host.
550 */
551static void mmc_sdio_detect(struct mmc_host *host)
552{
553 int err;
554
555 BUG_ON(!host);
556 BUG_ON(!host->card);
557
87973ba2 558 /* Make sure card is powered before detecting it */
ed919b01
OBC
559 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
560 err = pm_runtime_get_sync(&host->card->dev);
561 if (err < 0)
562 goto out;
563 }
87973ba2 564
5c4e6f13
PO
565 mmc_claim_host(host);
566
567 /*
568 * Just check if our card has been removed.
569 */
570 err = mmc_select_card(host->card);
571
572 mmc_release_host(host);
573
4d0812c3
OBC
574 /*
575 * Tell PM core it's OK to power off the card now.
576 *
577 * The _sync variant is used in order to ensure that the card
578 * is left powered off in case an error occurred, and the card
579 * is going to be removed.
580 *
581 * Since there is no specific reason to believe a new user
582 * is about to show up at this point, the _sync variant is
583 * desirable anyway.
584 */
ed919b01
OBC
585 if (host->caps & MMC_CAP_POWER_OFF_CARD)
586 pm_runtime_put_sync(&host->card->dev);
4d0812c3 587
87973ba2 588out:
5c4e6f13
PO
589 if (err) {
590 mmc_sdio_remove(host);
591
592 mmc_claim_host(host);
593 mmc_detach_bus(host);
594 mmc_release_host(host);
595 }
596}
597
17d33e14
NP
598/*
599 * SDIO suspend. We need to suspend all functions separately.
600 * Therefore all registered functions must have drivers with suspend
601 * and resume methods. Failing that we simply remove the whole card.
602 */
95cdfb72 603static int mmc_sdio_suspend(struct mmc_host *host)
17d33e14 604{
95cdfb72 605 int i, err = 0;
17d33e14 606
17d33e14
NP
607 for (i = 0; i < host->card->sdio_funcs; i++) {
608 struct sdio_func *func = host->card->sdio_func[i];
609 if (func && sdio_func_present(func) && func->dev.driver) {
610 const struct dev_pm_ops *pmops = func->dev.driver->pm;
611 if (!pmops || !pmops->suspend || !pmops->resume) {
95cdfb72
NP
612 /* force removal of entire card in that case */
613 err = -ENOSYS;
614 } else
615 err = pmops->suspend(&func->dev);
616 if (err)
617 break;
17d33e14
NP
618 }
619 }
95cdfb72 620 while (err && --i >= 0) {
17d33e14
NP
621 struct sdio_func *func = host->card->sdio_func[i];
622 if (func && sdio_func_present(func) && func->dev.driver) {
623 const struct dev_pm_ops *pmops = func->dev.driver->pm;
95cdfb72 624 pmops->resume(&func->dev);
17d33e14
NP
625 }
626 }
95cdfb72 627
a5e9425d 628 if (!err && mmc_card_keep_power(host)) {
6b5eda36
DD
629 mmc_claim_host(host);
630 sdio_disable_wide(host->card);
631 mmc_release_host(host);
632 }
633
95cdfb72 634 return err;
17d33e14
NP
635}
636
95cdfb72 637static int mmc_sdio_resume(struct mmc_host *host)
17d33e14 638{
080bc977 639 int i, err = 0;
17d33e14
NP
640
641 BUG_ON(!host);
642 BUG_ON(!host->card);
643
95cdfb72 644 /* Basic card reinitialization. */
17d33e14 645 mmc_claim_host(host);
080bc977
OBC
646
647 /* No need to reinitialize powered-resumed nonremovable cards */
a5e9425d 648 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
080bc977 649 err = mmc_sdio_init_card(host, host->ocr, host->card,
a5e9425d
OBC
650 mmc_card_keep_power(host));
651 else if (mmc_card_keep_power(host)) {
080bc977
OBC
652 /* We may have switched to 1-bit mode during suspend */
653 err = sdio_enable_4bit_bus(host->card);
654 if (err > 0) {
655 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
656 err = 0;
657 }
658 }
659
40216842
NP
660 if (!err && host->sdio_irqs)
661 mmc_signal_sdio_irq(host);
17d33e14 662 mmc_release_host(host);
17d33e14 663
95cdfb72
NP
664 /*
665 * If the card looked to be the same as before suspending, then
666 * we proceed to resume all card functions. If one of them returns
667 * an error then we simply return that error to the core and the
668 * card will be redetected as new. It is the responsibility of
669 * the function driver to perform further tests with the extra
670 * knowledge it has of the card to confirm the card is indeed the
671 * same as before suspending (same MAC address for network cards,
672 * etc.) and return an error otherwise.
673 */
674 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
17d33e14
NP
675 struct sdio_func *func = host->card->sdio_func[i];
676 if (func && sdio_func_present(func) && func->dev.driver) {
677 const struct dev_pm_ops *pmops = func->dev.driver->pm;
95cdfb72 678 err = pmops->resume(&func->dev);
17d33e14
NP
679 }
680 }
95cdfb72
NP
681
682 return err;
17d33e14 683}
5c4e6f13 684
d3fe37b1
OBC
685static int mmc_sdio_power_restore(struct mmc_host *host)
686{
687 int ret;
688
689 BUG_ON(!host);
690 BUG_ON(!host->card);
691
692 mmc_claim_host(host);
693 ret = mmc_sdio_init_card(host, host->ocr, host->card,
a5e9425d 694 mmc_card_keep_power(host));
d3fe37b1
OBC
695 if (!ret && host->sdio_irqs)
696 mmc_signal_sdio_irq(host);
697 mmc_release_host(host);
698
699 return ret;
700}
701
5c4e6f13
PO
702static const struct mmc_bus_ops mmc_sdio_ops = {
703 .remove = mmc_sdio_remove,
704 .detect = mmc_sdio_detect,
17d33e14
NP
705 .suspend = mmc_sdio_suspend,
706 .resume = mmc_sdio_resume,
d3fe37b1 707 .power_restore = mmc_sdio_power_restore,
5c4e6f13
PO
708};
709
710
711/*
712 * Starting point for SDIO card init.
713 */
807e8e40 714int mmc_attach_sdio(struct mmc_host *host)
5c4e6f13 715{
807e8e40
AR
716 int err, i, funcs;
717 u32 ocr;
5c4e6f13
PO
718 struct mmc_card *card;
719
720 BUG_ON(!host);
d84075c8 721 WARN_ON(!host->claimed);
5c4e6f13 722
807e8e40
AR
723 err = mmc_send_io_op_cond(host, 0, &ocr);
724 if (err)
725 return err;
726
5c4e6f13 727 mmc_attach_bus(host, &mmc_sdio_ops);
8f230f45
TI
728 if (host->ocr_avail_sdio)
729 host->ocr_avail = host->ocr_avail_sdio;
5c4e6f13
PO
730
731 /*
732 * Sanity check the voltages that the card claims to
733 * support.
734 */
735 if (ocr & 0x7F) {
736 printk(KERN_WARNING "%s: card claims to support voltages "
737 "below the defined range. These will be ignored.\n",
738 mmc_hostname(host));
739 ocr &= ~0x7F;
740 }
741
5c4e6f13
PO
742 host->ocr = mmc_select_voltage(host, ocr);
743
744 /*
745 * Can we support the voltage(s) of the card(s)?
746 */
747 if (!host->ocr) {
748 err = -EINVAL;
749 goto err;
750 }
751
752 /*
17d33e14 753 * Detect and init the card.
5c4e6f13 754 */
3bca4cf7 755 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
5c4e6f13
PO
756 if (err)
757 goto err;
17d33e14 758 card = host->card;
af517150 759
81968561 760 /*
ed919b01 761 * Enable runtime PM only if supported by host+card+board
81968561 762 */
ed919b01
OBC
763 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
764 /*
765 * Let runtime PM core know our card is active
766 */
767 err = pm_runtime_set_active(&card->dev);
768 if (err)
769 goto remove;
81968561 770
ed919b01
OBC
771 /*
772 * Enable runtime PM for this card
773 */
774 pm_runtime_enable(&card->dev);
775 }
81968561 776
5c4e6f13
PO
777 /*
778 * The number of functions on the card is encoded inside
779 * the ocr.
780 */
e8812793
MF
781 funcs = (ocr & 0x70000000) >> 28;
782 card->sdio_funcs = 0;
4ff6471c 783
e29a7d73
PO
784 /*
785 * Initialize (but don't add) all present functions.
786 */
e8812793 787 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
e29a7d73
PO
788 err = sdio_init_func(host->card, i + 1);
789 if (err)
790 goto remove;
40bba0c1
OBC
791
792 /*
ed919b01 793 * Enable Runtime PM for this func (if supported)
40bba0c1 794 */
ed919b01
OBC
795 if (host->caps & MMC_CAP_POWER_OFF_CARD)
796 pm_runtime_enable(&card->sdio_func[i]->dev);
e29a7d73 797 }
5c4e6f13 798
e29a7d73
PO
799 /*
800 * First add the card to the driver model...
801 */
807e8e40 802 mmc_release_host(host);
5c4e6f13
PO
803 err = mmc_add_card(host->card);
804 if (err)
e29a7d73
PO
805 goto remove_added;
806
807 /*
808 * ...then the SDIO functions.
809 */
810 for (i = 0;i < funcs;i++) {
811 err = sdio_add_func(host->card->sdio_func[i]);
812 if (err)
813 goto remove_added;
814 }
5c4e6f13 815
34497913 816 mmc_claim_host(host);
5c4e6f13
PO
817 return 0;
818
e29a7d73
PO
819
820remove_added:
821 /* Remove without lock if the device has been added. */
822 mmc_sdio_remove(host);
5c4e6f13 823 mmc_claim_host(host);
e29a7d73
PO
824remove:
825 /* And with lock if it hasn't been added. */
807e8e40 826 mmc_release_host(host);
e29a7d73
PO
827 if (host->card)
828 mmc_sdio_remove(host);
807e8e40 829 mmc_claim_host(host);
5c4e6f13
PO
830err:
831 mmc_detach_bus(host);
5c4e6f13
PO
832
833 printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n",
834 mmc_hostname(host), err);
835
836 return err;
837}
838