blob: 5419c0dfad31bb7b168fb93234b5a987a9f9788d [file] [log] [blame]
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001/*
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>
Ohad Ben-Cohen819685612010-10-02 13:54:10 +020013#include <linux/pm_runtime.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020014
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
Girish K Sa4924c72012-01-11 14:04:52 -050017#include <linux/mmc/mmc.h>
Pierre Ossman35c66c12007-06-11 20:25:43 +020018#include <linux/mmc/sdio.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020019#include <linux/mmc/sdio_func.h>
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030020#include <linux/mmc/sdio_ids.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020021
22#include "core.h"
Ulf Hansson4facdde2017-01-13 14:14:14 +010023#include "card.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020024#include "bus.h"
Michal Miroslaw71578a12010-08-10 18:01:40 -070025#include "sd.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020026#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020027#include "mmc_ops.h"
28#include "sd_ops.h"
29#include "sdio_ops.h"
Nicolas Pitreb7261262007-06-16 02:04:16 -040030#include "sdio_cis.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020031
Pierre Ossman05970072007-06-11 21:01:00 +020032static int sdio_read_fbr(struct sdio_func *func)
33{
34 int ret;
35 unsigned char data;
36
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030037 if (mmc_card_nonstd_func_interface(func->card)) {
38 func->class = SDIO_CLASS_NONE;
39 return 0;
40 }
41
Pierre Ossman05970072007-06-11 21:01:00 +020042 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010043 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020044 if (ret)
45 goto out;
46
47 data &= 0x0f;
48
49 if (data == 0x0f) {
50 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010051 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020052 if (ret)
53 goto out;
54 }
55
56 func->class = data;
57
58out:
59 return ret;
60}
61
Pierre Ossmane29a7d72007-05-26 13:48:18 +020062static int sdio_init_func(struct mmc_card *card, unsigned int fn)
63{
Pierre Ossman05970072007-06-11 21:01:00 +020064 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020065 struct sdio_func *func;
66
Shawn Lin5df0e8232016-11-02 15:24:00 +080067 if (WARN_ON(fn > SDIO_MAX_FUNCS))
68 return -EINVAL;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020069
70 func = sdio_alloc_func(card);
71 if (IS_ERR(func))
72 return PTR_ERR(func);
73
74 func->num = fn;
75
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070076 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
77 ret = sdio_read_fbr(func);
78 if (ret)
79 goto fail;
Pierre Ossman05970072007-06-11 21:01:00 +020080
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070081 ret = sdio_read_func_cis(func);
82 if (ret)
83 goto fail;
84 } else {
85 func->vendor = func->card->cis.vendor;
86 func->device = func->card->cis.device;
87 func->max_blksize = func->card->cis.blksize;
88 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040089
Pierre Ossmane29a7d72007-05-26 13:48:18 +020090 card->sdio_func[fn - 1] = func;
91
92 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020093
94fail:
95 /*
96 * It is okay to remove the function here even though we hold
97 * the host lock as we haven't registered the device yet.
98 */
99 sdio_remove_func(func);
100 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200101}
102
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800103static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
Pierre Ossman35c66c12007-06-11 20:25:43 +0200104{
105 int ret;
106 int cccr_vsn;
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800107 int uhs = ocr & R4_18V_PRESENT;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200108 unsigned char data;
Philip Rakitya303c532011-11-14 19:14:38 -0800109 unsigned char speed;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200110
Pierre Ossman35c66c12007-06-11 20:25:43 +0200111 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
112 if (ret)
113 goto out;
114
115 cccr_vsn = data & 0x0f;
116
Bing Zhaob4625da2011-10-20 19:16:32 -0700117 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530118 pr_err("%s: unrecognised CCCR structure version %d\n",
Pierre Ossman35c66c12007-06-11 20:25:43 +0200119 mmc_hostname(card->host), cccr_vsn);
120 return -EINVAL;
121 }
122
123 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
124
125 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
126 if (ret)
127 goto out;
128
129 if (data & SDIO_CCCR_CAP_SMB)
130 card->cccr.multi_block = 1;
131 if (data & SDIO_CCCR_CAP_LSC)
132 card->cccr.low_speed = 1;
133 if (data & SDIO_CCCR_CAP_4BLS)
134 card->cccr.wide_bus = 1;
135
136 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
137 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
138 if (ret)
139 goto out;
140
141 if (data & SDIO_POWER_SMPC)
142 card->cccr.high_power = 1;
143 }
144
145 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
Philip Rakitya303c532011-11-14 19:14:38 -0800146 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
Pierre Ossman35c66c12007-06-11 20:25:43 +0200147 if (ret)
148 goto out;
149
Philip Rakitya303c532011-11-14 19:14:38 -0800150 card->scr.sda_spec3 = 0;
151 card->sw_caps.sd3_bus_mode = 0;
152 card->sw_caps.sd3_drv_type = 0;
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800153 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
Philip Rakitya303c532011-11-14 19:14:38 -0800154 card->scr.sda_spec3 = 1;
155 ret = mmc_io_rw_direct(card, 0, 0,
156 SDIO_CCCR_UHS, 0, &data);
157 if (ret)
158 goto out;
159
Sujit Reddy Thumma41875e32012-12-04 17:06:19 +0530160 if (mmc_host_uhs(card->host)) {
Philip Rakitya303c532011-11-14 19:14:38 -0800161 if (data & SDIO_UHS_DDR50)
162 card->sw_caps.sd3_bus_mode
163 |= SD_MODE_UHS_DDR50;
164
165 if (data & SDIO_UHS_SDR50)
166 card->sw_caps.sd3_bus_mode
167 |= SD_MODE_UHS_SDR50;
168
169 if (data & SDIO_UHS_SDR104)
170 card->sw_caps.sd3_bus_mode
171 |= SD_MODE_UHS_SDR104;
172 }
173
174 ret = mmc_io_rw_direct(card, 0, 0,
175 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
176 if (ret)
177 goto out;
178
179 if (data & SDIO_DRIVE_SDTA)
180 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
181 if (data & SDIO_DRIVE_SDTC)
182 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
183 if (data & SDIO_DRIVE_SDTD)
184 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
185 }
186
187 /* if no uhs mode ensure we check for high speed */
188 if (!card->sw_caps.sd3_bus_mode) {
189 if (speed & SDIO_SPEED_SHS) {
190 card->cccr.high_speed = 1;
191 card->sw_caps.hs_max_dtr = 50000000;
192 } else {
193 card->cccr.high_speed = 0;
194 card->sw_caps.hs_max_dtr = 25000000;
195 }
196 }
Pierre Ossman35c66c12007-06-11 20:25:43 +0200197 }
198
199out:
200 return ret;
201}
202
Pierre Ossman4ff64712007-07-30 18:23:53 +0200203static int sdio_enable_wide(struct mmc_card *card)
204{
205 int ret;
206 u8 ctrl;
207
208 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
209 return 0;
210
211 if (card->cccr.low_speed && !card->cccr.wide_bus)
212 return 0;
213
214 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
215 if (ret)
216 return ret;
217
Yong Ding2a0fe912012-05-15 13:09:43 +0800218 if ((ctrl & SDIO_BUS_WIDTH_MASK) == SDIO_BUS_WIDTH_RESERVED)
Joe Perches66061102014-09-12 14:56:56 -0700219 pr_warn("%s: SDIO_CCCR_IF is invalid: 0x%02x\n",
220 mmc_hostname(card->host), ctrl);
Yong Ding2a0fe912012-05-15 13:09:43 +0800221
222 /* set as 4-bit bus width */
223 ctrl &= ~SDIO_BUS_WIDTH_MASK;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200224 ctrl |= SDIO_BUS_WIDTH_4BIT;
225
226 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
227 if (ret)
228 return ret;
229
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700230 return 1;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200231}
232
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200233/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700234 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
235 * of the card. This may be required on certain setups of boards,
236 * controllers and embedded sdio device which do not need the card's
237 * pull-up. As a result, card detection is disabled and power is saved.
238 */
239static int sdio_disable_cd(struct mmc_card *card)
240{
241 int ret;
242 u8 ctrl;
243
Ohad Ben-Cohen2059a022011-04-05 18:02:25 +0300244 if (!mmc_card_disable_cd(card))
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700245 return 0;
246
247 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
248 if (ret)
249 return ret;
250
251 ctrl |= SDIO_BUS_CD_DISABLE;
252
253 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
254}
255
256/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800257 * Devices that remain active during a system suspend are
258 * put back into 1-bit mode.
259 */
260static int sdio_disable_wide(struct mmc_card *card)
261{
262 int ret;
263 u8 ctrl;
264
265 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
266 return 0;
267
268 if (card->cccr.low_speed && !card->cccr.wide_bus)
269 return 0;
270
271 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
272 if (ret)
273 return ret;
274
275 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
276 return 0;
277
278 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
279 ctrl |= SDIO_BUS_ASYNC_INT;
280
281 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
282 if (ret)
283 return ret;
284
285 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
286
287 return 0;
288}
289
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700290
291static int sdio_enable_4bit_bus(struct mmc_card *card)
292{
293 int err;
294
295 if (card->type == MMC_TYPE_SDIO)
NeilBrown303dbed2015-02-24 13:42:23 +1100296 err = sdio_enable_wide(card);
297 else if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
298 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700299 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
300 if (err)
301 return err;
NeilBrown303dbed2015-02-24 13:42:23 +1100302 err = sdio_enable_wide(card);
303 if (err <= 0)
304 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700305 } else
306 return 0;
307
NeilBrown303dbed2015-02-24 13:42:23 +1100308 if (err > 0) {
309 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
310 err = 0;
311 }
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700312
313 return err;
314}
315
316
Daniel Drake6b5eda32010-03-05 13:43:34 -0800317/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200318 * Test if the card supports high-speed mode and, if so, switch to it.
319 */
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700320static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
Pierre Ossmand16f5772008-08-31 17:22:46 +0200321{
322 int ret;
323 u8 speed;
324
325 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
326 return 0;
327
328 if (!card->cccr.high_speed)
329 return 0;
330
331 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
332 if (ret)
333 return ret;
334
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700335 if (enable)
336 speed |= SDIO_SPEED_EHS;
337 else
338 speed &= ~SDIO_SPEED_EHS;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200339
340 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
341 if (ret)
342 return ret;
343
Michal Miroslaw71578a12010-08-10 18:01:40 -0700344 return 1;
345}
Pierre Ossmand16f5772008-08-31 17:22:46 +0200346
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700347/*
348 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
349 */
350static int sdio_enable_hs(struct mmc_card *card)
351{
352 int ret;
353
354 ret = mmc_sdio_switch_hs(card, true);
355 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
356 return ret;
357
358 ret = mmc_sd_switch_hs(card);
359 if (ret <= 0)
360 mmc_sdio_switch_hs(card, false);
361
362 return ret;
363}
364
Michal Miroslaw71578a12010-08-10 18:01:40 -0700365static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
366{
367 unsigned max_dtr;
368
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900369 if (mmc_card_hs(card)) {
Michal Miroslaw71578a12010-08-10 18:01:40 -0700370 /*
371 * The SDIO specification doesn't mention how
372 * the CIS transfer speed register relates to
373 * high-speed, but it seems that 50 MHz is
374 * mandatory.
375 */
376 max_dtr = 50000000;
377 } else {
378 max_dtr = card->cis.max_dtr;
379 }
380
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700381 if (card->type == MMC_TYPE_SD_COMBO)
382 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
383
Michal Miroslaw71578a12010-08-10 18:01:40 -0700384 return max_dtr;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200385}
386
Philip Rakitya303c532011-11-14 19:14:38 -0800387static unsigned char host_drive_to_sdio_drive(int host_strength)
388{
389 switch (host_strength) {
390 case MMC_SET_DRIVER_TYPE_A:
391 return SDIO_DTSx_SET_TYPE_A;
392 case MMC_SET_DRIVER_TYPE_B:
393 return SDIO_DTSx_SET_TYPE_B;
394 case MMC_SET_DRIVER_TYPE_C:
395 return SDIO_DTSx_SET_TYPE_C;
396 case MMC_SET_DRIVER_TYPE_D:
397 return SDIO_DTSx_SET_TYPE_D;
398 default:
399 return SDIO_DTSx_SET_TYPE_B;
400 }
401}
402
403static void sdio_select_driver_type(struct mmc_card *card)
404{
Adrian Hunterfa021ce2015-02-06 14:12:53 +0200405 int card_drv_type, drive_strength, drv_type;
Philip Rakitya303c532011-11-14 19:14:38 -0800406 unsigned char card_strength;
407 int err;
408
Adrian Hunter3853a042015-02-06 14:12:56 +0200409 card->drive_strength = 0;
410
Adrian Hunterfa021ce2015-02-06 14:12:53 +0200411 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
Philip Rakitya303c532011-11-14 19:14:38 -0800412
Adrian Huntere23350b2015-02-06 14:12:55 +0200413 drive_strength = mmc_select_drive_strength(card,
414 card->sw_caps.uhs_max_dtr,
415 card_drv_type, &drv_type);
Philip Rakitya303c532011-11-14 19:14:38 -0800416
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200417 if (drive_strength) {
418 /* if error just use default for drive strength B */
419 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
420 &card_strength);
421 if (err)
422 return;
Philip Rakitya303c532011-11-14 19:14:38 -0800423
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200424 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
425 card_strength |= host_drive_to_sdio_drive(drive_strength);
Philip Rakitya303c532011-11-14 19:14:38 -0800426
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200427 /* if error default to drive strength B */
428 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
429 card_strength, NULL);
430 if (err)
431 return;
Adrian Hunter3853a042015-02-06 14:12:56 +0200432 card->drive_strength = drive_strength;
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200433 }
Philip Rakitya303c532011-11-14 19:14:38 -0800434
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200435 if (drv_type)
436 mmc_set_driver_type(card->host, drv_type);
Philip Rakitya303c532011-11-14 19:14:38 -0800437}
438
439
440static int sdio_set_bus_speed_mode(struct mmc_card *card)
441{
442 unsigned int bus_speed, timing;
443 int err;
444 unsigned char speed;
445
446 /*
447 * If the host doesn't support any of the UHS-I modes, fallback on
448 * default speed.
449 */
Sujit Reddy Thumma41875e32012-12-04 17:06:19 +0530450 if (!mmc_host_uhs(card->host))
Philip Rakitya303c532011-11-14 19:14:38 -0800451 return 0;
452
453 bus_speed = SDIO_SPEED_SDR12;
454 timing = MMC_TIMING_UHS_SDR12;
455 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
456 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
457 bus_speed = SDIO_SPEED_SDR104;
458 timing = MMC_TIMING_UHS_SDR104;
459 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530460 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800461 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
462 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
463 bus_speed = SDIO_SPEED_DDR50;
464 timing = MMC_TIMING_UHS_DDR50;
465 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530466 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800467 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
468 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
469 SD_MODE_UHS_SDR50)) {
470 bus_speed = SDIO_SPEED_SDR50;
471 timing = MMC_TIMING_UHS_SDR50;
472 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530473 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800474 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
475 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
476 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
477 bus_speed = SDIO_SPEED_SDR25;
478 timing = MMC_TIMING_UHS_SDR25;
479 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530480 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800481 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
482 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
483 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
484 SD_MODE_UHS_SDR12)) {
485 bus_speed = SDIO_SPEED_SDR12;
486 timing = MMC_TIMING_UHS_SDR12;
487 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530488 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800489 }
490
491 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
492 if (err)
493 return err;
494
495 speed &= ~SDIO_SPEED_BSS_MASK;
496 speed |= bus_speed;
497 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
498 if (err)
499 return err;
500
501 if (bus_speed) {
502 mmc_set_timing(card->host, timing);
503 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
504 }
505
506 return 0;
507}
508
509/*
510 * UHS-I specific initialization procedure
511 */
512static int mmc_sdio_init_uhs_card(struct mmc_card *card)
513{
514 int err;
515
516 if (!card->scr.sda_spec3)
517 return 0;
518
519 /*
520 * Switch to wider bus (if supported).
521 */
NeilBrown303dbed2015-02-24 13:42:23 +1100522 if (card->host->caps & MMC_CAP_4_BIT_DATA)
Philip Rakitya303c532011-11-14 19:14:38 -0800523 err = sdio_enable_4bit_bus(card);
Philip Rakitya303c532011-11-14 19:14:38 -0800524
525 /* Set the driver strength for the card */
526 sdio_select_driver_type(card);
527
528 /* Set bus speed mode of the card */
529 err = sdio_set_bus_speed_mode(card);
530 if (err)
531 goto out;
532
Fredrik Soderstedt810e08e2013-04-17 13:50:53 +0200533 /*
534 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
535 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
536 */
Adrian Hunter63e415c2014-12-05 19:40:59 +0200537 if (!mmc_host_is_spi(card->host) &&
Carlo Caionee10c3212016-01-13 09:36:55 +0100538 ((card->host->ios.timing == MMC_TIMING_UHS_SDR50) ||
539 (card->host->ios.timing == MMC_TIMING_UHS_SDR104)))
Adrian Hunter63e415c2014-12-05 19:40:59 +0200540 err = mmc_execute_tuning(card);
Philip Rakitya303c532011-11-14 19:14:38 -0800541out:
Philip Rakitya303c532011-11-14 19:14:38 -0800542 return err;
543}
544
Shawn Lin0eb51a52017-01-09 16:56:19 +0800545static void mmc_sdio_resend_if_cond(struct mmc_host *host,
546 struct mmc_card *card)
547{
548 sdio_reset(host);
549 mmc_go_idle(host);
550 mmc_send_if_cond(host, host->ocr_avail);
551 mmc_remove_card(card);
552}
553
Pierre Ossmand16f5772008-08-31 17:22:46 +0200554/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700555 * Handle the detection and initialisation of a card.
556 *
557 * In the case of a resume, "oldcard" will contain the card
558 * we're trying to reinitialise.
559 */
560static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf72010-03-05 13:43:33 -0800561 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700562{
563 struct mmc_card *card;
564 int err;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100565 int retries = 10;
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200566 u32 rocr = 0;
Ulf Hansson69041152013-09-13 11:31:33 +0200567 u32 ocr_card = ocr;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700568
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700569 WARN_ON(!host->claimed);
570
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200571 /* to query card if 1.8V signalling is supported */
572 if (mmc_host_uhs(host))
573 ocr |= R4_18V_PRESENT;
574
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100575try_again:
576 if (!retries) {
Joe Perches66061102014-09-12 14:56:56 -0700577 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100578 ocr &= ~R4_18V_PRESENT;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100579 }
580
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700581 /*
582 * Inform the card of the voltage
583 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800584 if (!powered_resume) {
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200585 err = mmc_send_io_op_cond(host, ocr, &rocr);
Chris Ball3bca4cf72010-03-05 13:43:33 -0800586 if (err)
587 goto err;
588 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700589
590 /*
591 * For SPI, enable CRC as appropriate.
592 */
593 if (mmc_host_is_spi(host)) {
594 err = mmc_spi_set_crc(host, use_spi_crc);
595 if (err)
596 goto err;
597 }
598
599 /*
600 * Allocate card structure.
601 */
602 card = mmc_alloc_card(host, NULL);
603 if (IS_ERR(card)) {
604 err = PTR_ERR(card);
605 goto err;
606 }
607
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200608 if ((rocr & R4_MEMORY_PRESENT) &&
609 mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700610 card->type = MMC_TYPE_SD_COMBO;
611
612 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
613 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
614 mmc_remove_card(card);
615 return -ENOENT;
616 }
617 } else {
618 card->type = MMC_TYPE_SDIO;
619
620 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
621 mmc_remove_card(card);
622 return -ENOENT;
623 }
624 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700625
626 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200627 * Call the optional HC's init_card function to handle quirks.
628 */
629 if (host->ops->init_card)
630 host->ops->init_card(host, card);
631
632 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800633 * If the host and card support UHS-I mode request the card
634 * to switch to 1.8V signaling level. No 1.8v signalling if
Masanari Iida7122bbb2012-08-05 23:25:40 +0900635 * UHS mode is not enabled to maintain compatibility and some
Philip Rakitya303c532011-11-14 19:14:38 -0800636 * systems that claim 1.8v signalling in fact do not support
Shawn Lin5fc3d802017-01-09 16:56:20 +0800637 * it. Per SDIO spec v3, section 3.1.2, if the voltage is already
638 * 1.8v, the card sets S18A to 0 in the R4 response. So it will
639 * fails to check rocr & R4_18V_PRESENT, but we still need to
640 * try to init uhs card. sdio_read_cccr will take over this task
641 * to make sure which speed mode should work.
Philip Rakitya303c532011-11-14 19:14:38 -0800642 */
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200643 if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
Ulf Hansson0f791fd2013-09-12 15:36:34 +0200644 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
Adrian Hunterd9bfbb92015-11-26 14:00:47 +0200645 ocr_card);
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100646 if (err == -EAGAIN) {
Shawn Lin0eb51a52017-01-09 16:56:19 +0800647 mmc_sdio_resend_if_cond(host, card);
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100648 retries--;
649 goto try_again;
650 } else if (err) {
Philip Rakitya303c532011-11-14 19:14:38 -0800651 ocr &= ~R4_18V_PRESENT;
Philip Rakitya303c532011-11-14 19:14:38 -0800652 }
Philip Rakitya303c532011-11-14 19:14:38 -0800653 }
654
655 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700656 * For native busses: set card RCA and quit open drain mode.
657 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800658 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700659 err = mmc_send_relative_addr(host, &card->rca);
660 if (err)
661 goto remove;
662
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100663 /*
664 * Update oldcard with the new RCA received from the SDIO
665 * device -- we're doing this so that it's updated in the
666 * "card" struct when oldcard overwrites that later.
667 */
668 if (oldcard)
669 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700670 }
671
672 /*
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700673 * Read CSD, before selecting the card
674 */
675 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
676 err = mmc_sd_get_csd(host, card);
677 if (err)
678 return err;
679
680 mmc_decode_cid(card);
681 }
682
683 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700684 * Select card, as all following commands rely on that.
685 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800686 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700687 err = mmc_select_card(card);
688 if (err)
689 goto remove;
690 }
691
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700692 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
693 /*
694 * This is non-standard SDIO device, meaning it doesn't
695 * have any CIA (Common I/O area) registers present.
696 * It's host's responsibility to fill cccr and cis
697 * structures in init_card().
698 */
699 mmc_set_clock(host, card->cis.max_dtr);
700
701 if (card->cccr.high_speed) {
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700702 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
703 }
704
705 goto finish;
706 }
707
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700708 /*
Shawn Lin5fc3d802017-01-09 16:56:20 +0800709 * Read the common registers. Note that we should try to
710 * validate whether UHS would work or not.
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700711 */
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800712 err = sdio_read_cccr(card, ocr);
Shawn Lin5fc3d802017-01-09 16:56:20 +0800713 if (err) {
714 mmc_sdio_resend_if_cond(host, card);
715 if (ocr & R4_18V_PRESENT) {
716 /* Retry init sequence, but without R4_18V_PRESENT. */
717 retries = 0;
718 goto try_again;
719 } else {
720 goto remove;
721 }
722 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700723
724 /*
725 * Read the common CIS tuples.
726 */
727 err = sdio_read_common_cis(card);
728 if (err)
729 goto remove;
730
731 if (oldcard) {
732 int same = (card->cis.vendor == oldcard->cis.vendor &&
733 card->cis.device == oldcard->cis.device);
734 mmc_remove_card(card);
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700735 if (!same)
736 return -ENOENT;
737
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700738 card = oldcard;
739 }
Ulf Hansson69041152013-09-13 11:31:33 +0200740 card->ocr = ocr_card;
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500741 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700742
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700743 if (card->type == MMC_TYPE_SD_COMBO) {
744 err = mmc_sd_setup_card(host, card, oldcard != NULL);
745 /* handle as SDIO-only card if memory init failed */
746 if (err) {
747 mmc_go_idle(host);
748 if (mmc_host_is_spi(host))
749 /* should not fail, as it worked previously */
750 mmc_spi_set_crc(host, use_spi_crc);
751 card->type = MMC_TYPE_SDIO;
752 } else
753 card->dev.type = &sd_type;
754 }
755
756 /*
757 * If needed, disconnect card detection pull-up resistor.
758 */
759 err = sdio_disable_cd(card);
760 if (err)
761 goto remove;
762
Philip Rakitya303c532011-11-14 19:14:38 -0800763 /* Initialization sequence for UHS-I cards */
764 /* Only if card supports 1.8v and UHS signaling */
765 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
766 err = mmc_sdio_init_uhs_card(card);
767 if (err)
768 goto remove;
Philip Rakitya303c532011-11-14 19:14:38 -0800769 } else {
770 /*
771 * Switch to high-speed (if supported).
772 */
773 err = sdio_enable_hs(card);
774 if (err > 0)
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900775 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
Philip Rakitya303c532011-11-14 19:14:38 -0800776 else if (err)
777 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700778
Philip Rakitya303c532011-11-14 19:14:38 -0800779 /*
780 * Change to the card's maximum speed.
781 */
782 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700783
Philip Rakitya303c532011-11-14 19:14:38 -0800784 /*
785 * Switch to wider bus (if supported).
786 */
787 err = sdio_enable_4bit_bus(card);
NeilBrown303dbed2015-02-24 13:42:23 +1100788 if (err)
Philip Rakitya303c532011-11-14 19:14:38 -0800789 goto remove;
790 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700791finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700792 if (!oldcard)
793 host->card = card;
794 return 0;
795
796remove:
797 if (!oldcard)
798 mmc_remove_card(card);
799
800err:
801 return err;
802}
803
804/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200805 * Host is being removed. Free up the current card.
806 */
807static void mmc_sdio_remove(struct mmc_host *host)
808{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200809 int i;
810
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200811 for (i = 0;i < host->card->sdio_funcs;i++) {
812 if (host->card->sdio_func[i]) {
813 sdio_remove_func(host->card->sdio_func[i]);
814 host->card->sdio_func[i] = NULL;
815 }
816 }
817
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200818 mmc_remove_card(host->card);
819 host->card = NULL;
820}
821
822/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200823 * Card detection - card is alive.
824 */
825static int mmc_sdio_alive(struct mmc_host *host)
826{
827 return mmc_select_card(host->card);
828}
829
830/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200831 * Card detection callback from host.
832 */
833static void mmc_sdio_detect(struct mmc_host *host)
834{
835 int err;
836
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200837 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200838 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
839 err = pm_runtime_get_sync(&host->card->dev);
Li Fei3bffb802013-04-08 09:36:39 +0800840 if (err < 0) {
841 pm_runtime_put_noidle(&host->card->dev);
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200842 goto out;
Li Fei3bffb802013-04-08 09:36:39 +0800843 }
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200844 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200845
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200846 mmc_claim_host(host);
847
848 /*
849 * Just check if our card has been removed.
850 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200851 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200852
853 mmc_release_host(host);
854
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200855 /*
856 * Tell PM core it's OK to power off the card now.
857 *
858 * The _sync variant is used in order to ensure that the card
859 * is left powered off in case an error occurred, and the card
860 * is going to be removed.
861 *
862 * Since there is no specific reason to believe a new user
863 * is about to show up at this point, the _sync variant is
864 * desirable anyway.
865 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200866 if (host->caps & MMC_CAP_POWER_OFF_CARD)
867 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200868
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200869out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200870 if (err) {
871 mmc_sdio_remove(host);
872
873 mmc_claim_host(host);
874 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400875 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200876 mmc_release_host(host);
877 }
878}
879
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700880/*
Ulf Hansson810cadd2013-06-10 17:03:37 +0200881 * SDIO pre_suspend. We need to suspend all functions separately.
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700882 * Therefore all registered functions must have drivers with suspend
883 * and resume methods. Failing that we simply remove the whole card.
884 */
Ulf Hansson810cadd2013-06-10 17:03:37 +0200885static int mmc_sdio_pre_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700886{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700887 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700888
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700889 for (i = 0; i < host->card->sdio_funcs; i++) {
890 struct sdio_func *func = host->card->sdio_func[i];
891 if (func && sdio_func_present(func) && func->dev.driver) {
892 const struct dev_pm_ops *pmops = func->dev.driver->pm;
893 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700894 /* force removal of entire card in that case */
895 err = -ENOSYS;
Ulf Hansson810cadd2013-06-10 17:03:37 +0200896 break;
897 }
898 }
899 }
900
901 return err;
902}
903
904/*
905 * SDIO suspend. Suspend all functions separately.
906 */
907static int mmc_sdio_suspend(struct mmc_host *host)
908{
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +0200909 mmc_claim_host(host);
910
911 if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800912 sdio_disable_wide(host->card);
Daniel Drake6b5eda32010-03-05 13:43:34 -0800913
Adrian Hunter79d5a652015-05-07 13:10:13 +0300914 if (!mmc_card_keep_power(host)) {
Ulf Hansson74590262013-06-10 17:03:38 +0200915 mmc_power_off(host);
Adrian Hunter79d5a652015-05-07 13:10:13 +0300916 } else if (host->retune_period) {
917 mmc_retune_timer_stop(host);
918 mmc_retune_needed(host);
919 }
Ulf Hansson74590262013-06-10 17:03:38 +0200920
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +0200921 mmc_release_host(host);
922
Ulf Hansson573185c2014-02-28 12:49:00 +0100923 return 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700924}
925
926static int mmc_sdio_resume(struct mmc_host *host)
927{
Ulf Hansson573185c2014-02-28 12:49:00 +0100928 int err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700929
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700930 /* Basic card reinitialization. */
931 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200932
Ulf Hansson74590262013-06-10 17:03:38 +0200933 /* Restore power if needed */
934 if (!mmc_card_keep_power(host)) {
Ulf Hansson69041152013-09-13 11:31:33 +0200935 mmc_power_up(host, host->card->ocr);
Ulf Hansson74590262013-06-10 17:03:38 +0200936 /*
937 * Tell runtime PM core we just powered up the card,
938 * since it still believes the card is powered off.
939 * Note that currently runtime PM is only enabled
940 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
941 */
942 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
943 pm_runtime_disable(&host->card->dev);
944 pm_runtime_set_active(&host->card->dev);
945 pm_runtime_enable(&host->card->dev);
946 }
947 }
948
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200949 /* No need to reinitialize powered-resumed nonremovable cards */
Subhash Jadavani51aa66a2012-12-04 17:06:18 +0530950 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
951 sdio_reset(host);
952 mmc_go_idle(host);
NeilBrown006fc512014-11-12 14:55:19 +1100953 mmc_send_if_cond(host, host->card->ocr);
954 err = mmc_send_io_op_cond(host, 0, NULL);
955 if (!err)
956 err = mmc_sdio_init_card(host, host->card->ocr,
957 host->card,
958 mmc_card_keep_power(host));
Subhash Jadavani51aa66a2012-12-04 17:06:18 +0530959 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200960 /* We may have switched to 1-bit mode during suspend */
961 err = sdio_enable_4bit_bus(host->card);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200962 }
963
Fu Zhonghuidea67c42014-08-18 10:48:14 +0800964 if (!err && host->sdio_irqs) {
Ulf Hansson9eadcc02015-10-02 10:56:11 +0200965 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD))
Fu Zhonghuidea67c42014-08-18 10:48:14 +0800966 wake_up_process(host->sdio_irq_thread);
Ulf Hansson9eadcc02015-10-02 10:56:11 +0200967 else if (host->caps & MMC_CAP_SDIO_IRQ)
Fu Zhonghuidea67c42014-08-18 10:48:14 +0800968 host->ops->enable_sdio_irq(host, 1);
Fu Zhonghuidea67c42014-08-18 10:48:14 +0800969 }
970
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700971 mmc_release_host(host);
972
Ulf Hansson74590262013-06-10 17:03:38 +0200973 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700974 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700975}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200976
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +0200977static int mmc_sdio_power_restore(struct mmc_host *host)
978{
979 int ret;
980
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +0200981 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +0100982
983 /*
984 * Reset the card by performing the same steps that are taken by
985 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
986 *
987 * sdio_reset() is technically not needed. Having just powered up the
988 * hardware, it should already be in reset state. However, some
989 * platforms (such as SD8686 on OLPC) do not instantly cut power,
990 * meaning that a reset is required when restoring power soon after
991 * powering off. It is harmless in other cases.
992 *
993 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
994 * is not necessary for non-removable cards. However, it is required
995 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
996 * harmless in other situations.
997 *
Daniel Drakec6e633a2011-06-25 19:20:11 +0100998 */
Ulf Hanssone7747472012-03-01 13:18:05 +0100999
Daniel Drakec6e633a2011-06-25 19:20:11 +01001000 sdio_reset(host);
1001 mmc_go_idle(host);
NeilBrown59b6c9e2014-11-12 14:55:19 +11001002 mmc_send_if_cond(host, host->card->ocr);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001003
Ulf Hanssonce69d372013-09-16 11:28:42 +02001004 ret = mmc_send_io_op_cond(host, 0, NULL);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001005 if (ret)
1006 goto out;
1007
Ulf Hanssonce69d372013-09-16 11:28:42 +02001008 ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001009 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001010 if (!ret && host->sdio_irqs)
1011 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001012
1013out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001014 mmc_release_host(host);
1015
1016 return ret;
1017}
1018
Ulf Hansson12d01d02013-05-02 14:02:37 +02001019static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1020{
1021 /* No references to the card, cut the power to it. */
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02001022 mmc_claim_host(host);
Ulf Hansson12d01d02013-05-02 14:02:37 +02001023 mmc_power_off(host);
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02001024 mmc_release_host(host);
1025
Ulf Hansson12d01d02013-05-02 14:02:37 +02001026 return 0;
1027}
1028
1029static int mmc_sdio_runtime_resume(struct mmc_host *host)
1030{
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02001031 int ret;
1032
Ulf Hansson12d01d02013-05-02 14:02:37 +02001033 /* Restore power and re-initialize. */
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02001034 mmc_claim_host(host);
Ulf Hansson69041152013-09-13 11:31:33 +02001035 mmc_power_up(host, host->card->ocr);
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02001036 ret = mmc_sdio_power_restore(host);
1037 mmc_release_host(host);
1038
1039 return ret;
Ulf Hansson12d01d02013-05-02 14:02:37 +02001040}
1041
Andreas Fenkart1fb654f2015-04-23 13:15:42 +02001042static int mmc_sdio_reset(struct mmc_host *host)
1043{
1044 mmc_power_cycle(host, host->card->ocr);
1045 return mmc_sdio_power_restore(host);
1046}
1047
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001048static const struct mmc_bus_ops mmc_sdio_ops = {
1049 .remove = mmc_sdio_remove,
1050 .detect = mmc_sdio_detect,
Ulf Hansson810cadd2013-06-10 17:03:37 +02001051 .pre_suspend = mmc_sdio_pre_suspend,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001052 .suspend = mmc_sdio_suspend,
1053 .resume = mmc_sdio_resume,
Ulf Hansson12d01d02013-05-02 14:02:37 +02001054 .runtime_suspend = mmc_sdio_runtime_suspend,
1055 .runtime_resume = mmc_sdio_runtime_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001056 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001057 .alive = mmc_sdio_alive,
Andreas Fenkart1fb654f2015-04-23 13:15:42 +02001058 .reset = mmc_sdio_reset,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001059};
1060
1061
1062/*
1063 * Starting point for SDIO card init.
1064 */
Andy Ross807e8e42011-01-03 10:36:56 -08001065int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001066{
Andy Ross807e8e42011-01-03 10:36:56 -08001067 int err, i, funcs;
Ulf Hansson69041152013-09-13 11:31:33 +02001068 u32 ocr, rocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001069 struct mmc_card *card;
1070
Pierre Ossmand84075c82007-08-09 13:23:56 +02001071 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001072
Andy Ross807e8e42011-01-03 10:36:56 -08001073 err = mmc_send_io_op_cond(host, 0, &ocr);
1074 if (err)
1075 return err;
1076
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001077 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001078 if (host->ocr_avail_sdio)
1079 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001080
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001081
Ulf Hansson69041152013-09-13 11:31:33 +02001082 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001083
1084 /*
1085 * Can we support the voltage(s) of the card(s)?
1086 */
Ulf Hansson69041152013-09-13 11:31:33 +02001087 if (!rocr) {
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001088 err = -EINVAL;
1089 goto err;
1090 }
1091
1092 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001093 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001094 */
Ulf Hansson69041152013-09-13 11:31:33 +02001095 err = mmc_sdio_init_card(host, rocr, NULL, 0);
Ulf Hanssonec2ed702013-09-12 16:38:56 +02001096 if (err)
1097 goto err;
1098
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001099 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001100
1101 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001102 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001103 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001104 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1105 /*
1106 * Let runtime PM core know our card is active
1107 */
1108 err = pm_runtime_set_active(&card->dev);
1109 if (err)
1110 goto remove;
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001111
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001112 /*
1113 * Enable runtime PM for this card
1114 */
1115 pm_runtime_enable(&card->dev);
1116 }
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001117
1118 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001119 * The number of functions on the card is encoded inside
1120 * the ocr.
1121 */
Matt Fleminge8812792009-12-17 15:27:18 -08001122 funcs = (ocr & 0x70000000) >> 28;
1123 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001124
1125 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001126 * Initialize (but don't add) all present functions.
1127 */
Matt Fleminge8812792009-12-17 15:27:18 -08001128 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001129 err = sdio_init_func(host->card, i + 1);
1130 if (err)
1131 goto remove;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001132
1133 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001134 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001135 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001136 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1137 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001138 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001139
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001140 /*
1141 * First add the card to the driver model...
1142 */
Andy Ross807e8e42011-01-03 10:36:56 -08001143 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001144 err = mmc_add_card(host->card);
1145 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001146 goto remove_added;
1147
1148 /*
1149 * ...then the SDIO functions.
1150 */
1151 for (i = 0;i < funcs;i++) {
1152 err = sdio_add_func(host->card->sdio_func[i]);
1153 if (err)
1154 goto remove_added;
1155 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001156
Dmitry Shmidt34497912011-03-03 17:40:10 -05001157 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001158 return 0;
1159
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001160
1161remove_added:
1162 /* Remove without lock if the device has been added. */
1163 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001164 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001165remove:
1166 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001167 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001168 if (host->card)
1169 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001170 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001171err:
1172 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001173
Girish K Sa3c76eb2011-10-11 11:44:09 +05301174 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001175 mmc_hostname(host), err);
1176
1177 return err;
1178}
1179