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