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