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