OSDN Git Service

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