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