af9a058fddbbb840d475bc46bb23761f0b569902
[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_warning("%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                 return sdio_enable_wide(card);
302
303         if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
304                 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
305                 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
306                 if (err)
307                         return err;
308         } else
309                 return 0;
310
311         err = sdio_enable_wide(card);
312         if (err <= 0)
313                 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
314
315         return err;
316 }
317
318
319 /*
320  * Test if the card supports high-speed mode and, if so, switch to it.
321  */
322 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
323 {
324         int ret;
325         u8 speed;
326
327         if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
328                 return 0;
329
330         if (!card->cccr.high_speed)
331                 return 0;
332
333         ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
334         if (ret)
335                 return ret;
336
337         if (enable)
338                 speed |= SDIO_SPEED_EHS;
339         else
340                 speed &= ~SDIO_SPEED_EHS;
341
342         ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
343         if (ret)
344                 return ret;
345
346         return 1;
347 }
348
349 /*
350  * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
351  */
352 static int sdio_enable_hs(struct mmc_card *card)
353 {
354         int ret;
355
356         ret = mmc_sdio_switch_hs(card, true);
357         if (ret <= 0 || card->type == MMC_TYPE_SDIO)
358                 return ret;
359
360         ret = mmc_sd_switch_hs(card);
361         if (ret <= 0)
362                 mmc_sdio_switch_hs(card, false);
363
364         return ret;
365 }
366
367 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
368 {
369         unsigned max_dtr;
370
371         if (mmc_card_highspeed(card)) {
372                 /*
373                  * The SDIO specification doesn't mention how
374                  * the CIS transfer speed register relates to
375                  * high-speed, but it seems that 50 MHz is
376                  * mandatory.
377                  */
378                 max_dtr = 50000000;
379         } else {
380                 max_dtr = card->cis.max_dtr;
381         }
382
383         if (card->type == MMC_TYPE_SD_COMBO)
384                 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
385
386         return max_dtr;
387 }
388
389 static unsigned char host_drive_to_sdio_drive(int host_strength)
390 {
391         switch (host_strength) {
392         case MMC_SET_DRIVER_TYPE_A:
393                 return SDIO_DTSx_SET_TYPE_A;
394         case MMC_SET_DRIVER_TYPE_B:
395                 return SDIO_DTSx_SET_TYPE_B;
396         case MMC_SET_DRIVER_TYPE_C:
397                 return SDIO_DTSx_SET_TYPE_C;
398         case MMC_SET_DRIVER_TYPE_D:
399                 return SDIO_DTSx_SET_TYPE_D;
400         default:
401                 return SDIO_DTSx_SET_TYPE_B;
402         }
403 }
404
405 static void sdio_select_driver_type(struct mmc_card *card)
406 {
407         int host_drv_type = SD_DRIVER_TYPE_B;
408         int card_drv_type = SD_DRIVER_TYPE_B;
409         int drive_strength;
410         unsigned char card_strength;
411         int err;
412
413         /*
414          * If the host doesn't support any of the Driver Types A,C or D,
415          * or there is no board specific handler then default Driver
416          * Type B is used.
417          */
418         if (!(card->host->caps &
419                 (MMC_CAP_DRIVER_TYPE_A |
420                  MMC_CAP_DRIVER_TYPE_C |
421                  MMC_CAP_DRIVER_TYPE_D)))
422                 return;
423
424         if (!card->host->ops->select_drive_strength)
425                 return;
426
427         if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
428                 host_drv_type |= SD_DRIVER_TYPE_A;
429
430         if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
431                 host_drv_type |= SD_DRIVER_TYPE_C;
432
433         if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
434                 host_drv_type |= SD_DRIVER_TYPE_D;
435
436         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
437                 card_drv_type |= SD_DRIVER_TYPE_A;
438
439         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
440                 card_drv_type |= SD_DRIVER_TYPE_C;
441
442         if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
443                 card_drv_type |= SD_DRIVER_TYPE_D;
444
445         /*
446          * The drive strength that the hardware can support
447          * depends on the board design.  Pass the appropriate
448          * information and let the hardware specific code
449          * return what is possible given the options
450          */
451         drive_strength = card->host->ops->select_drive_strength(
452                 card->sw_caps.uhs_max_dtr,
453                 host_drv_type, card_drv_type);
454
455         /* if error just use default for drive strength B */
456         err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
457                 &card_strength);
458         if (err)
459                 return;
460
461         card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
462         card_strength |= host_drive_to_sdio_drive(drive_strength);
463
464         err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
465                 card_strength, NULL);
466
467         /* if error default to drive strength B */
468         if (!err)
469                 mmc_set_driver_type(card->host, drive_strength);
470 }
471
472
473 static int sdio_set_bus_speed_mode(struct mmc_card *card)
474 {
475         unsigned int bus_speed, timing;
476         int err;
477         unsigned char speed;
478
479         /*
480          * If the host doesn't support any of the UHS-I modes, fallback on
481          * default speed.
482          */
483         if (!mmc_host_uhs(card->host))
484                 return 0;
485
486         bus_speed = SDIO_SPEED_SDR12;
487         timing = MMC_TIMING_UHS_SDR12;
488         if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
489             (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
490                         bus_speed = SDIO_SPEED_SDR104;
491                         timing = MMC_TIMING_UHS_SDR104;
492                         card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
493                         card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
494         } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
495                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
496                         bus_speed = SDIO_SPEED_DDR50;
497                         timing = MMC_TIMING_UHS_DDR50;
498                         card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
499                         card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
500         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
501                     MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
502                     SD_MODE_UHS_SDR50)) {
503                         bus_speed = SDIO_SPEED_SDR50;
504                         timing = MMC_TIMING_UHS_SDR50;
505                         card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
506                         card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
507         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
508                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
509                    (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
510                         bus_speed = SDIO_SPEED_SDR25;
511                         timing = MMC_TIMING_UHS_SDR25;
512                         card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
513                         card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
514         } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
515                     MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
516                     MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
517                     SD_MODE_UHS_SDR12)) {
518                         bus_speed = SDIO_SPEED_SDR12;
519                         timing = MMC_TIMING_UHS_SDR12;
520                         card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
521                         card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
522         }
523
524         err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
525         if (err)
526                 return err;
527
528         speed &= ~SDIO_SPEED_BSS_MASK;
529         speed |= bus_speed;
530         err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
531         if (err)
532                 return err;
533
534         if (bus_speed) {
535                 mmc_set_timing(card->host, timing);
536                 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
537         }
538
539         return 0;
540 }
541
542 /*
543  * UHS-I specific initialization procedure
544  */
545 static int mmc_sdio_init_uhs_card(struct mmc_card *card)
546 {
547         int err;
548
549         if (!card->scr.sda_spec3)
550                 return 0;
551
552         /*
553          * Switch to wider bus (if supported).
554          */
555         if (card->host->caps & MMC_CAP_4_BIT_DATA) {
556                 err = sdio_enable_4bit_bus(card);
557                 if (err > 0) {
558                         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
559                         err = 0;
560                 }
561         }
562
563         /* Set the driver strength for the card */
564         sdio_select_driver_type(card);
565
566         /* Set bus speed mode of the card */
567         err = sdio_set_bus_speed_mode(card);
568         if (err)
569                 goto out;
570
571         /*
572          * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
573          * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
574          */
575         if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning &&
576                         ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
577                          (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104))) {
578                 mmc_host_clk_hold(card->host);
579                 err = card->host->ops->execute_tuning(card->host,
580                                                       MMC_SEND_TUNING_BLOCK);
581                 mmc_host_clk_release(card->host);
582         }
583
584 out:
585
586         return err;
587 }
588
589 /*
590  * Handle the detection and initialisation of a card.
591  *
592  * In the case of a resume, "oldcard" will contain the card
593  * we're trying to reinitialise.
594  */
595 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
596                               struct mmc_card *oldcard, int powered_resume)
597 {
598         struct mmc_card *card;
599         int err;
600         int retries = 10;
601
602         BUG_ON(!host);
603         WARN_ON(!host->claimed);
604
605 try_again:
606         if (!retries) {
607                 pr_warning("%s: Skipping voltage switch\n",
608                                 mmc_hostname(host));
609                 ocr &= ~R4_18V_PRESENT;
610                 host->ocr &= ~R4_18V_PRESENT;
611         }
612
613         /*
614          * Inform the card of the voltage
615          */
616         if (!powered_resume) {
617                 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
618                 if (err)
619                         goto err;
620         }
621
622         /*
623          * For SPI, enable CRC as appropriate.
624          */
625         if (mmc_host_is_spi(host)) {
626                 err = mmc_spi_set_crc(host, use_spi_crc);
627                 if (err)
628                         goto err;
629         }
630
631         /*
632          * Allocate card structure.
633          */
634         card = mmc_alloc_card(host, NULL);
635         if (IS_ERR(card)) {
636                 err = PTR_ERR(card);
637                 goto err;
638         }
639
640         if ((ocr & R4_MEMORY_PRESENT) &&
641             mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
642                 card->type = MMC_TYPE_SD_COMBO;
643
644                 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
645                     memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
646                         mmc_remove_card(card);
647                         return -ENOENT;
648                 }
649         } else {
650                 card->type = MMC_TYPE_SDIO;
651
652                 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
653                         mmc_remove_card(card);
654                         return -ENOENT;
655                 }
656         }
657
658         /*
659          * Call the optional HC's init_card function to handle quirks.
660          */
661         if (host->ops->init_card)
662                 host->ops->init_card(host, card);
663
664         /*
665          * If the host and card support UHS-I mode request the card
666          * to switch to 1.8V signaling level.  No 1.8v signalling if
667          * UHS mode is not enabled to maintain compatibility and some
668          * systems that claim 1.8v signalling in fact do not support
669          * it.
670          */
671         if (!powered_resume && (ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) {
672                 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180);
673                 if (err == -EAGAIN) {
674                         sdio_reset(host);
675                         mmc_go_idle(host);
676                         mmc_send_if_cond(host, host->ocr_avail);
677                         mmc_remove_card(card);
678                         retries--;
679                         goto try_again;
680                 } else if (err) {
681                         ocr &= ~R4_18V_PRESENT;
682                         host->ocr &= ~R4_18V_PRESENT;
683                 }
684                 err = 0;
685         } else {
686                 ocr &= ~R4_18V_PRESENT;
687                 host->ocr &= ~R4_18V_PRESENT;
688         }
689
690         /*
691          * For native busses:  set card RCA and quit open drain mode.
692          */
693         if (!powered_resume && !mmc_host_is_spi(host)) {
694                 err = mmc_send_relative_addr(host, &card->rca);
695                 if (err)
696                         goto remove;
697
698                 /*
699                  * Update oldcard with the new RCA received from the SDIO
700                  * device -- we're doing this so that it's updated in the
701                  * "card" struct when oldcard overwrites that later.
702                  */
703                 if (oldcard)
704                         oldcard->rca = card->rca;
705         }
706
707         /*
708          * Read CSD, before selecting the card
709          */
710         if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
711                 err = mmc_sd_get_csd(host, card);
712                 if (err)
713                         return err;
714
715                 mmc_decode_cid(card);
716         }
717
718         /*
719          * Select card, as all following commands rely on that.
720          */
721         if (!powered_resume && !mmc_host_is_spi(host)) {
722                 err = mmc_select_card(card);
723                 if (err)
724                         goto remove;
725         }
726
727         if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
728                 /*
729                  * This is non-standard SDIO device, meaning it doesn't
730                  * have any CIA (Common I/O area) registers present.
731                  * It's host's responsibility to fill cccr and cis
732                  * structures in init_card().
733                  */
734                 mmc_set_clock(host, card->cis.max_dtr);
735
736                 if (card->cccr.high_speed) {
737                         mmc_card_set_highspeed(card);
738                         mmc_set_timing(card->host, MMC_TIMING_SD_HS);
739                 }
740
741                 goto finish;
742         }
743
744 #ifdef CONFIG_MMC_EMBEDDED_SDIO
745         if (host->embedded_sdio_data.cccr)
746                 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
747         else {
748 #endif
749                 /*
750                  * Read the common registers.
751                  */
752                 err = sdio_read_cccr(card,  ocr);
753                 if (err)
754                         goto remove;
755 #ifdef CONFIG_MMC_EMBEDDED_SDIO
756         }
757 #endif
758
759 #ifdef CONFIG_MMC_EMBEDDED_SDIO
760         if (host->embedded_sdio_data.cis)
761                 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
762         else {
763 #endif
764                 /*
765                  * Read the common CIS tuples.
766                  */
767                 err = sdio_read_common_cis(card);
768                 if (err)
769                         goto remove;
770 #ifdef CONFIG_MMC_EMBEDDED_SDIO
771         }
772 #endif
773
774         if (oldcard) {
775                 int same = (card->cis.vendor == oldcard->cis.vendor &&
776                             card->cis.device == oldcard->cis.device);
777                 mmc_remove_card(card);
778                 if (!same)
779                         return -ENOENT;
780
781                 card = oldcard;
782         }
783         mmc_fixup_device(card, NULL);
784
785         if (card->type == MMC_TYPE_SD_COMBO) {
786                 err = mmc_sd_setup_card(host, card, oldcard != NULL);
787                 /* handle as SDIO-only card if memory init failed */
788                 if (err) {
789                         mmc_go_idle(host);
790                         if (mmc_host_is_spi(host))
791                                 /* should not fail, as it worked previously */
792                                 mmc_spi_set_crc(host, use_spi_crc);
793                         card->type = MMC_TYPE_SDIO;
794                 } else
795                         card->dev.type = &sd_type;
796         }
797
798         /*
799          * If needed, disconnect card detection pull-up resistor.
800          */
801         err = sdio_disable_cd(card);
802         if (err)
803                 goto remove;
804
805         /* Initialization sequence for UHS-I cards */
806         /* Only if card supports 1.8v and UHS signaling */
807         if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
808                 err = mmc_sdio_init_uhs_card(card);
809                 if (err)
810                         goto remove;
811
812                 /* Card is an ultra-high-speed card */
813                 mmc_card_set_uhs(card);
814         } else {
815                 /*
816                  * Switch to high-speed (if supported).
817                  */
818                 err = sdio_enable_hs(card);
819                 if (err > 0)
820                         mmc_sd_go_highspeed(card);
821                 else if (err)
822                         goto remove;
823
824                 /*
825                  * Change to the card's maximum speed.
826                  */
827                 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
828
829                 /*
830                  * Switch to wider bus (if supported).
831                  */
832                 err = sdio_enable_4bit_bus(card);
833                 if (err > 0)
834                         mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
835                 else if (err)
836                         goto remove;
837         }
838 finish:
839         if (!oldcard)
840                 host->card = card;
841         return 0;
842
843 remove:
844         if (!oldcard)
845                 mmc_remove_card(card);
846
847 err:
848         return err;
849 }
850
851 /*
852  * Host is being removed. Free up the current card.
853  */
854 static void mmc_sdio_remove(struct mmc_host *host)
855 {
856         int i;
857
858         BUG_ON(!host);
859         BUG_ON(!host->card);
860
861         for (i = 0;i < host->card->sdio_funcs;i++) {
862                 if (host->card->sdio_func[i]) {
863                         sdio_remove_func(host->card->sdio_func[i]);
864                         host->card->sdio_func[i] = NULL;
865                 }
866         }
867
868         mmc_remove_card(host->card);
869         host->card = NULL;
870 }
871
872 /*
873  * Card detection - card is alive.
874  */
875 static int mmc_sdio_alive(struct mmc_host *host)
876 {
877         return mmc_select_card(host->card);
878 }
879
880 /*
881  * Card detection callback from host.
882  */
883 static void mmc_sdio_detect(struct mmc_host *host)
884 {
885         int err;
886
887         BUG_ON(!host);
888         BUG_ON(!host->card);
889
890         /* Make sure card is powered before detecting it */
891         if (host->caps & MMC_CAP_POWER_OFF_CARD) {
892                 err = pm_runtime_get_sync(&host->card->dev);
893                 if (err < 0) {
894                         pm_runtime_put_noidle(&host->card->dev);
895                         goto out;
896                 }
897         }
898
899         mmc_claim_host(host);
900
901         /*
902          * Just check if our card has been removed.
903          */
904         err = _mmc_detect_card_removed(host);
905
906         mmc_release_host(host);
907
908         /*
909          * Tell PM core it's OK to power off the card now.
910          *
911          * The _sync variant is used in order to ensure that the card
912          * is left powered off in case an error occurred, and the card
913          * is going to be removed.
914          *
915          * Since there is no specific reason to believe a new user
916          * is about to show up at this point, the _sync variant is
917          * desirable anyway.
918          */
919         if (host->caps & MMC_CAP_POWER_OFF_CARD)
920                 pm_runtime_put_sync(&host->card->dev);
921
922 out:
923         if (err) {
924                 mmc_sdio_remove(host);
925
926                 mmc_claim_host(host);
927                 mmc_detach_bus(host);
928                 mmc_power_off(host);
929                 mmc_release_host(host);
930         }
931 }
932
933 /*
934  * SDIO pre_suspend.  We need to suspend all functions separately.
935  * Therefore all registered functions must have drivers with suspend
936  * and resume methods.  Failing that we simply remove the whole card.
937  */
938 static int mmc_sdio_pre_suspend(struct mmc_host *host)
939 {
940         int i, err = 0;
941
942         for (i = 0; i < host->card->sdio_funcs; i++) {
943                 struct sdio_func *func = host->card->sdio_func[i];
944                 if (func && sdio_func_present(func) && func->dev.driver) {
945                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
946                         if (!pmops || !pmops->suspend || !pmops->resume) {
947                                 /* force removal of entire card in that case */
948                                 err = -ENOSYS;
949                                 break;
950                         }
951                 }
952         }
953
954         return err;
955 }
956
957 /*
958  * SDIO suspend.  Suspend all functions separately.
959  */
960 static int mmc_sdio_suspend(struct mmc_host *host)
961 {
962         int i, err = 0;
963
964         for (i = 0; i < host->card->sdio_funcs; i++) {
965                 struct sdio_func *func = host->card->sdio_func[i];
966                 if (func && sdio_func_present(func) && func->dev.driver) {
967                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
968                         err = pmops->suspend(&func->dev);
969                         if (err)
970                                 break;
971                 }
972         }
973         while (err && --i >= 0) {
974                 struct sdio_func *func = host->card->sdio_func[i];
975                 if (func && sdio_func_present(func) && func->dev.driver) {
976                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
977                         pmops->resume(&func->dev);
978                 }
979         }
980
981         if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
982                 mmc_claim_host(host);
983                 sdio_disable_wide(host->card);
984                 mmc_release_host(host);
985         }
986
987         if (!err && !mmc_card_keep_power(host))
988                 mmc_power_off(host);
989
990         return err;
991 }
992
993 static int mmc_sdio_resume(struct mmc_host *host)
994 {
995         int i, err = 0;
996
997         BUG_ON(!host);
998         BUG_ON(!host->card);
999
1000         /* Basic card reinitialization. */
1001         mmc_claim_host(host);
1002
1003         /* Restore power if needed */
1004         if (!mmc_card_keep_power(host)) {
1005                 mmc_power_up(host);
1006                 mmc_select_voltage(host, host->ocr);
1007                 /*
1008                  * Tell runtime PM core we just powered up the card,
1009                  * since it still believes the card is powered off.
1010                  * Note that currently runtime PM is only enabled
1011                  * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1012                  */
1013                 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1014                         pm_runtime_disable(&host->card->dev);
1015                         pm_runtime_set_active(&host->card->dev);
1016                         pm_runtime_enable(&host->card->dev);
1017                 }
1018         }
1019
1020         /* No need to reinitialize powered-resumed nonremovable cards */
1021         if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
1022                 sdio_reset(host);
1023                 mmc_go_idle(host);
1024                 err = mmc_sdio_init_card(host, host->ocr, host->card,
1025                                         mmc_card_keep_power(host));
1026         } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
1027                 /* We may have switched to 1-bit mode during suspend */
1028                 err = sdio_enable_4bit_bus(host->card);
1029                 if (err > 0) {
1030                         mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1031                         err = 0;
1032                 }
1033         }
1034
1035         if (!err && host->sdio_irqs)
1036                 wake_up_process(host->sdio_irq_thread);
1037         mmc_release_host(host);
1038
1039         /*
1040          * If the card looked to be the same as before suspending, then
1041          * we proceed to resume all card functions.  If one of them returns
1042          * an error then we simply return that error to the core and the
1043          * card will be redetected as new.  It is the responsibility of
1044          * the function driver to perform further tests with the extra
1045          * knowledge it has of the card to confirm the card is indeed the
1046          * same as before suspending (same MAC address for network cards,
1047          * etc.) and return an error otherwise.
1048          */
1049         for (i = 0; !err && i < host->card->sdio_funcs; i++) {
1050                 struct sdio_func *func = host->card->sdio_func[i];
1051                 if (func && sdio_func_present(func) && func->dev.driver) {
1052                         const struct dev_pm_ops *pmops = func->dev.driver->pm;
1053                         err = pmops->resume(&func->dev);
1054                 }
1055         }
1056
1057         host->pm_flags &= ~MMC_PM_KEEP_POWER;
1058         return err;
1059 }
1060
1061 static int mmc_sdio_power_restore(struct mmc_host *host)
1062 {
1063         int ret;
1064         u32 ocr;
1065
1066         BUG_ON(!host);
1067         BUG_ON(!host->card);
1068
1069         mmc_claim_host(host);
1070
1071         /*
1072          * Reset the card by performing the same steps that are taken by
1073          * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1074          *
1075          * sdio_reset() is technically not needed. Having just powered up the
1076          * hardware, it should already be in reset state. However, some
1077          * platforms (such as SD8686 on OLPC) do not instantly cut power,
1078          * meaning that a reset is required when restoring power soon after
1079          * powering off. It is harmless in other cases.
1080          *
1081          * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1082          * is not necessary for non-removable cards. However, it is required
1083          * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1084          * harmless in other situations.
1085          *
1086          * With these steps taken, mmc_select_voltage() is also required to
1087          * restore the correct voltage setting of the card.
1088          */
1089
1090         sdio_reset(host);
1091         mmc_go_idle(host);
1092         mmc_send_if_cond(host, host->ocr_avail);
1093
1094         ret = mmc_send_io_op_cond(host, 0, &ocr);
1095         if (ret)
1096                 goto out;
1097
1098         if (host->ocr_avail_sdio)
1099                 host->ocr_avail = host->ocr_avail_sdio;
1100
1101         host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1102         if (!host->ocr) {
1103                 ret = -EINVAL;
1104                 goto out;
1105         }
1106
1107         if (mmc_host_uhs(host))
1108                 /* to query card if 1.8V signalling is supported */
1109                 host->ocr |= R4_18V_PRESENT;
1110
1111         ret = mmc_sdio_init_card(host, host->ocr, host->card,
1112                                 mmc_card_keep_power(host));
1113         if (!ret && host->sdio_irqs)
1114                 mmc_signal_sdio_irq(host);
1115
1116 out:
1117         mmc_release_host(host);
1118
1119         return ret;
1120 }
1121
1122 static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1123 {
1124         /* No references to the card, cut the power to it. */
1125         mmc_power_off(host);
1126         return 0;
1127 }
1128
1129 static int mmc_sdio_runtime_resume(struct mmc_host *host)
1130 {
1131         /* Restore power and re-initialize. */
1132         mmc_power_up(host);
1133         return mmc_sdio_power_restore(host);
1134 }
1135
1136 static const struct mmc_bus_ops mmc_sdio_ops = {
1137         .remove = mmc_sdio_remove,
1138         .detect = mmc_sdio_detect,
1139         .pre_suspend = mmc_sdio_pre_suspend,
1140         .suspend = mmc_sdio_suspend,
1141         .resume = mmc_sdio_resume,
1142         .runtime_suspend = mmc_sdio_runtime_suspend,
1143         .runtime_resume = mmc_sdio_runtime_resume,
1144         .power_restore = mmc_sdio_power_restore,
1145         .alive = mmc_sdio_alive,
1146 };
1147
1148
1149 /*
1150  * Starting point for SDIO card init.
1151  */
1152 int mmc_attach_sdio(struct mmc_host *host)
1153 {
1154         int err, i, funcs;
1155         u32 ocr;
1156         struct mmc_card *card;
1157
1158         BUG_ON(!host);
1159         WARN_ON(!host->claimed);
1160
1161         err = mmc_send_io_op_cond(host, 0, &ocr);
1162         if (err)
1163                 return err;
1164
1165         mmc_attach_bus(host, &mmc_sdio_ops);
1166         if (host->ocr_avail_sdio)
1167                 host->ocr_avail = host->ocr_avail_sdio;
1168
1169         /*
1170          * Sanity check the voltages that the card claims to
1171          * support.
1172          */
1173         if (ocr & 0x7F) {
1174                 pr_warning("%s: card claims to support voltages "
1175                        "below the defined range. These will be ignored.\n",
1176                        mmc_hostname(host));
1177                 ocr &= ~0x7F;
1178         }
1179
1180         host->ocr = mmc_select_voltage(host, ocr);
1181
1182         /*
1183          * Can we support the voltage(s) of the card(s)?
1184          */
1185         if (!host->ocr) {
1186                 err = -EINVAL;
1187                 goto err;
1188         }
1189
1190         /*
1191          * Detect and init the card.
1192          */
1193         if (mmc_host_uhs(host))
1194                 /* to query card if 1.8V signalling is supported */
1195                 host->ocr |= R4_18V_PRESENT;
1196
1197         err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1198         if (err) {
1199                 if (err == -EAGAIN) {
1200                         /*
1201                          * Retry initialization with S18R set to 0.
1202                          */
1203                         host->ocr &= ~R4_18V_PRESENT;
1204                         err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1205                 }
1206                 if (err)
1207                         goto err;
1208         }
1209         card = host->card;
1210
1211         /*
1212          * Enable runtime PM only if supported by host+card+board
1213          */
1214         if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1215                 /*
1216                  * Let runtime PM core know our card is active
1217                  */
1218                 err = pm_runtime_set_active(&card->dev);
1219                 if (err)
1220                         goto remove;
1221
1222                 /*
1223                  * Enable runtime PM for this card
1224                  */
1225                 pm_runtime_enable(&card->dev);
1226         }
1227
1228         /*
1229          * The number of functions on the card is encoded inside
1230          * the ocr.
1231          */
1232         funcs = (ocr & 0x70000000) >> 28;
1233         card->sdio_funcs = 0;
1234
1235 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1236         if (host->embedded_sdio_data.funcs)
1237                 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1238 #endif
1239
1240         /*
1241          * Initialize (but don't add) all present functions.
1242          */
1243         for (i = 0; i < funcs; i++, card->sdio_funcs++) {
1244 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1245                 if (host->embedded_sdio_data.funcs) {
1246                         struct sdio_func *tmp;
1247
1248                         tmp = sdio_alloc_func(host->card);
1249                         if (IS_ERR(tmp))
1250                                 goto remove;
1251                         tmp->num = (i + 1);
1252                         card->sdio_func[i] = tmp;
1253                         tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1254                         tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1255                         tmp->vendor = card->cis.vendor;
1256                         tmp->device = card->cis.device;
1257                 } else {
1258 #endif
1259                         err = sdio_init_func(host->card, i + 1);
1260                         if (err)
1261                                 goto remove;
1262 #ifdef CONFIG_MMC_EMBEDDED_SDIO
1263                 }
1264 #endif
1265                 /*
1266                  * Enable Runtime PM for this func (if supported)
1267                  */
1268                 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1269                         pm_runtime_enable(&card->sdio_func[i]->dev);
1270         }
1271
1272         /*
1273          * First add the card to the driver model...
1274          */
1275         mmc_release_host(host);
1276         err = mmc_add_card(host->card);
1277         if (err)
1278                 goto remove_added;
1279
1280         /*
1281          * ...then the SDIO functions.
1282          */
1283         for (i = 0;i < funcs;i++) {
1284                 err = sdio_add_func(host->card->sdio_func[i]);
1285                 if (err)
1286                         goto remove_added;
1287         }
1288
1289         mmc_claim_host(host);
1290         return 0;
1291
1292
1293 remove_added:
1294         /* Remove without lock if the device has been added. */
1295         mmc_sdio_remove(host);
1296         mmc_claim_host(host);
1297 remove:
1298         /* And with lock if it hasn't been added. */
1299         mmc_release_host(host);
1300         if (host->card)
1301                 mmc_sdio_remove(host);
1302         mmc_claim_host(host);
1303 err:
1304         mmc_detach_bus(host);
1305
1306         pr_err("%s: error %d whilst initialising SDIO card\n",
1307                 mmc_hostname(host), err);
1308
1309         return err;
1310 }
1311
1312 int sdio_reset_comm(struct mmc_card *card)
1313 {
1314         struct mmc_host *host = card->host;
1315         u32 ocr;
1316         int err;
1317
1318         printk("%s():\n", __func__);
1319         mmc_claim_host(host);
1320
1321         mmc_go_idle(host);
1322
1323         mmc_set_clock(host, host->f_min);
1324
1325         err = mmc_send_io_op_cond(host, 0, &ocr);
1326         if (err)
1327                 goto err;
1328
1329         host->ocr = mmc_select_voltage(host, ocr);
1330         if (!host->ocr) {
1331                 err = -EINVAL;
1332                 goto err;
1333         }
1334
1335         err = mmc_sdio_init_card(host, host->ocr, card, 0);
1336         if (err)
1337                 goto err;
1338
1339         mmc_release_host(host);
1340         return 0;
1341 err:
1342         printk("%s: Error resetting SDIO communications (%d)\n",
1343                mmc_hostname(host), err);
1344         mmc_release_host(host);
1345         return err;
1346 }
1347 EXPORT_SYMBOL(sdio_reset_comm);