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