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