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