blob: 73b091331c9699901c261a5528912e582f38bba8 [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"
23#include "bus.h"
Michal Miroslaw71578a12010-08-10 18:01:40 -070024#include "sd.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020025#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020026#include "mmc_ops.h"
27#include "sd_ops.h"
28#include "sdio_ops.h"
Nicolas Pitreb7261262007-06-16 02:04:16 -040029#include "sdio_cis.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020030
Pierre Ossman05970072007-06-11 21:01:00 +020031static int sdio_read_fbr(struct sdio_func *func)
32{
33 int ret;
34 unsigned char data;
35
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030036 if (mmc_card_nonstd_func_interface(func->card)) {
37 func->class = SDIO_CLASS_NONE;
38 return 0;
39 }
40
Pierre Ossman05970072007-06-11 21:01:00 +020041 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010042 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020043 if (ret)
44 goto out;
45
46 data &= 0x0f;
47
48 if (data == 0x0f) {
49 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010050 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020051 if (ret)
52 goto out;
53 }
54
55 func->class = data;
56
57out:
58 return ret;
59}
60
Pierre Ossmane29a7d72007-05-26 13:48:18 +020061static int sdio_init_func(struct mmc_card *card, unsigned int fn)
62{
Pierre Ossman05970072007-06-11 21:01:00 +020063 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020064 struct sdio_func *func;
65
66 BUG_ON(fn > SDIO_MAX_FUNCS);
67
68 func = sdio_alloc_func(card);
69 if (IS_ERR(func))
70 return PTR_ERR(func);
71
72 func->num = fn;
73
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070074 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
75 ret = sdio_read_fbr(func);
76 if (ret)
77 goto fail;
Pierre Ossman05970072007-06-11 21:01:00 +020078
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070079 ret = sdio_read_func_cis(func);
80 if (ret)
81 goto fail;
82 } else {
83 func->vendor = func->card->cis.vendor;
84 func->device = func->card->cis.device;
85 func->max_blksize = func->card->cis.blksize;
86 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040087
Pierre Ossmane29a7d72007-05-26 13:48:18 +020088 card->sdio_func[fn - 1] = func;
89
90 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020091
92fail:
93 /*
94 * It is okay to remove the function here even though we hold
95 * the host lock as we haven't registered the device yet.
96 */
97 sdio_remove_func(func);
98 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020099}
100
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800101static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
Pierre Ossman35c66c12007-06-11 20:25:43 +0200102{
103 int ret;
104 int cccr_vsn;
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800105 int uhs = ocr & R4_18V_PRESENT;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200106 unsigned char data;
Philip Rakitya303c532011-11-14 19:14:38 -0800107 unsigned char speed;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200108
109 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
110
111 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 Hunterfa021ce2015-02-06 14:12:53 +0200409 card_drv_type = card->sw_caps.sd3_drv_type | SD_DRIVER_TYPE_B;
Philip Rakitya303c532011-11-14 19:14:38 -0800410
Adrian Huntere23350b2015-02-06 14:12:55 +0200411 drive_strength = mmc_select_drive_strength(card,
412 card->sw_caps.uhs_max_dtr,
413 card_drv_type, &drv_type);
Philip Rakitya303c532011-11-14 19:14:38 -0800414
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200415 if (drive_strength) {
416 /* if error just use default for drive strength B */
417 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
418 &card_strength);
419 if (err)
420 return;
Philip Rakitya303c532011-11-14 19:14:38 -0800421
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200422 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
423 card_strength |= host_drive_to_sdio_drive(drive_strength);
Philip Rakitya303c532011-11-14 19:14:38 -0800424
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200425 /* if error default to drive strength B */
426 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
427 card_strength, NULL);
428 if (err)
429 return;
430 }
Philip Rakitya303c532011-11-14 19:14:38 -0800431
Adrian Hunterb4f30a12015-02-06 14:12:52 +0200432 if (drv_type)
433 mmc_set_driver_type(card->host, drv_type);
Philip Rakitya303c532011-11-14 19:14:38 -0800434}
435
436
437static int sdio_set_bus_speed_mode(struct mmc_card *card)
438{
439 unsigned int bus_speed, timing;
440 int err;
441 unsigned char speed;
442
443 /*
444 * If the host doesn't support any of the UHS-I modes, fallback on
445 * default speed.
446 */
Sujit Reddy Thumma41875e32012-12-04 17:06:19 +0530447 if (!mmc_host_uhs(card->host))
Philip Rakitya303c532011-11-14 19:14:38 -0800448 return 0;
449
450 bus_speed = SDIO_SPEED_SDR12;
451 timing = MMC_TIMING_UHS_SDR12;
452 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
453 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
454 bus_speed = SDIO_SPEED_SDR104;
455 timing = MMC_TIMING_UHS_SDR104;
456 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530457 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800458 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
459 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
460 bus_speed = SDIO_SPEED_DDR50;
461 timing = MMC_TIMING_UHS_DDR50;
462 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530463 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800464 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
465 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
466 SD_MODE_UHS_SDR50)) {
467 bus_speed = SDIO_SPEED_SDR50;
468 timing = MMC_TIMING_UHS_SDR50;
469 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530470 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800471 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
472 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
473 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
474 bus_speed = SDIO_SPEED_SDR25;
475 timing = MMC_TIMING_UHS_SDR25;
476 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530477 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800478 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
479 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
480 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
481 SD_MODE_UHS_SDR12)) {
482 bus_speed = SDIO_SPEED_SDR12;
483 timing = MMC_TIMING_UHS_SDR12;
484 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
Subhash Jadavani77e2ff02012-12-04 17:06:20 +0530485 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800486 }
487
488 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
489 if (err)
490 return err;
491
492 speed &= ~SDIO_SPEED_BSS_MASK;
493 speed |= bus_speed;
494 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
495 if (err)
496 return err;
497
498 if (bus_speed) {
499 mmc_set_timing(card->host, timing);
500 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
501 }
502
503 return 0;
504}
505
506/*
507 * UHS-I specific initialization procedure
508 */
509static int mmc_sdio_init_uhs_card(struct mmc_card *card)
510{
511 int err;
512
513 if (!card->scr.sda_spec3)
514 return 0;
515
516 /*
517 * Switch to wider bus (if supported).
518 */
NeilBrown303dbed2015-02-24 13:42:23 +1100519 if (card->host->caps & MMC_CAP_4_BIT_DATA)
Philip Rakitya303c532011-11-14 19:14:38 -0800520 err = sdio_enable_4bit_bus(card);
Philip Rakitya303c532011-11-14 19:14:38 -0800521
522 /* Set the driver strength for the card */
523 sdio_select_driver_type(card);
524
525 /* Set bus speed mode of the card */
526 err = sdio_set_bus_speed_mode(card);
527 if (err)
528 goto out;
529
Fredrik Soderstedt810e08e2013-04-17 13:50:53 +0200530 /*
531 * SPI mode doesn't define CMD19 and tuning is only valid for SDR50 and
532 * SDR104 mode SD-cards. Note that tuning is mandatory for SDR104.
533 */
Adrian Hunter63e415c2014-12-05 19:40:59 +0200534 if (!mmc_host_is_spi(card->host) &&
535 ((card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR50) ||
536 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)))
537 err = mmc_execute_tuning(card);
Philip Rakitya303c532011-11-14 19:14:38 -0800538out:
Philip Rakitya303c532011-11-14 19:14:38 -0800539 return err;
540}
541
Pierre Ossmand16f5772008-08-31 17:22:46 +0200542/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700543 * Handle the detection and initialisation of a card.
544 *
545 * In the case of a resume, "oldcard" will contain the card
546 * we're trying to reinitialise.
547 */
548static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf72010-03-05 13:43:33 -0800549 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700550{
551 struct mmc_card *card;
552 int err;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100553 int retries = 10;
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200554 u32 rocr = 0;
Ulf Hansson69041152013-09-13 11:31:33 +0200555 u32 ocr_card = ocr;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700556
557 BUG_ON(!host);
558 WARN_ON(!host->claimed);
559
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200560 /* to query card if 1.8V signalling is supported */
561 if (mmc_host_uhs(host))
562 ocr |= R4_18V_PRESENT;
563
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100564try_again:
565 if (!retries) {
Joe Perches66061102014-09-12 14:56:56 -0700566 pr_warn("%s: Skipping voltage switch\n", mmc_hostname(host));
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100567 ocr &= ~R4_18V_PRESENT;
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100568 }
569
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700570 /*
571 * Inform the card of the voltage
572 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800573 if (!powered_resume) {
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200574 err = mmc_send_io_op_cond(host, ocr, &rocr);
Chris Ball3bca4cf72010-03-05 13:43:33 -0800575 if (err)
576 goto err;
577 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700578
579 /*
580 * For SPI, enable CRC as appropriate.
581 */
582 if (mmc_host_is_spi(host)) {
583 err = mmc_spi_set_crc(host, use_spi_crc);
584 if (err)
585 goto err;
586 }
587
588 /*
589 * Allocate card structure.
590 */
591 card = mmc_alloc_card(host, NULL);
592 if (IS_ERR(card)) {
593 err = PTR_ERR(card);
594 goto err;
595 }
596
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200597 if ((rocr & R4_MEMORY_PRESENT) &&
598 mmc_sd_get_cid(host, ocr & rocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700599 card->type = MMC_TYPE_SD_COMBO;
600
601 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
602 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
603 mmc_remove_card(card);
604 return -ENOENT;
605 }
606 } else {
607 card->type = MMC_TYPE_SDIO;
608
609 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
610 mmc_remove_card(card);
611 return -ENOENT;
612 }
613 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700614
615 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200616 * Call the optional HC's init_card function to handle quirks.
617 */
618 if (host->ops->init_card)
619 host->ops->init_card(host, card);
620
621 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800622 * If the host and card support UHS-I mode request the card
623 * to switch to 1.8V signaling level. No 1.8v signalling if
Masanari Iida7122bbb2012-08-05 23:25:40 +0900624 * UHS mode is not enabled to maintain compatibility and some
Philip Rakitya303c532011-11-14 19:14:38 -0800625 * systems that claim 1.8v signalling in fact do not support
626 * it.
627 */
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200628 if (!powered_resume && (rocr & ocr & R4_18V_PRESENT)) {
Ulf Hansson0f791fd2013-09-12 15:36:34 +0200629 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
Ulf Hanssondb4a0d02013-09-13 10:49:34 +0200630 ocr);
Johan Rudholm0797e5f2013-01-28 15:08:28 +0100631 if (err == -EAGAIN) {
632 sdio_reset(host);
633 mmc_go_idle(host);
634 mmc_send_if_cond(host, host->ocr_avail);
635 mmc_remove_card(card);
636 retries--;
637 goto try_again;
638 } else if (err) {
Philip Rakitya303c532011-11-14 19:14:38 -0800639 ocr &= ~R4_18V_PRESENT;
Philip Rakitya303c532011-11-14 19:14:38 -0800640 }
641 err = 0;
642 } else {
643 ocr &= ~R4_18V_PRESENT;
Philip Rakitya303c532011-11-14 19:14:38 -0800644 }
645
646 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700647 * For native busses: set card RCA and quit open drain mode.
648 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800649 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700650 err = mmc_send_relative_addr(host, &card->rca);
651 if (err)
652 goto remove;
653
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100654 /*
655 * Update oldcard with the new RCA received from the SDIO
656 * device -- we're doing this so that it's updated in the
657 * "card" struct when oldcard overwrites that later.
658 */
659 if (oldcard)
660 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700661 }
662
663 /*
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700664 * Read CSD, before selecting the card
665 */
666 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
667 err = mmc_sd_get_csd(host, card);
668 if (err)
669 return err;
670
671 mmc_decode_cid(card);
672 }
673
674 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700675 * Select card, as all following commands rely on that.
676 */
Chris Ball3bca4cf72010-03-05 13:43:33 -0800677 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700678 err = mmc_select_card(card);
679 if (err)
680 goto remove;
681 }
682
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700683 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
684 /*
685 * This is non-standard SDIO device, meaning it doesn't
686 * have any CIA (Common I/O area) registers present.
687 * It's host's responsibility to fill cccr and cis
688 * structures in init_card().
689 */
690 mmc_set_clock(host, card->cis.max_dtr);
691
692 if (card->cccr.high_speed) {
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700693 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
694 }
695
696 goto finish;
697 }
698
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700699 /*
700 * Read the common registers.
701 */
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800702 err = sdio_read_cccr(card, ocr);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700703 if (err)
704 goto remove;
705
706 /*
707 * Read the common CIS tuples.
708 */
709 err = sdio_read_common_cis(card);
710 if (err)
711 goto remove;
712
713 if (oldcard) {
714 int same = (card->cis.vendor == oldcard->cis.vendor &&
715 card->cis.device == oldcard->cis.device);
716 mmc_remove_card(card);
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700717 if (!same)
718 return -ENOENT;
719
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700720 card = oldcard;
721 }
Ulf Hansson69041152013-09-13 11:31:33 +0200722 card->ocr = ocr_card;
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500723 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700724
Michal Miroslaw7310ece82010-08-10 18:01:40 -0700725 if (card->type == MMC_TYPE_SD_COMBO) {
726 err = mmc_sd_setup_card(host, card, oldcard != NULL);
727 /* handle as SDIO-only card if memory init failed */
728 if (err) {
729 mmc_go_idle(host);
730 if (mmc_host_is_spi(host))
731 /* should not fail, as it worked previously */
732 mmc_spi_set_crc(host, use_spi_crc);
733 card->type = MMC_TYPE_SDIO;
734 } else
735 card->dev.type = &sd_type;
736 }
737
738 /*
739 * If needed, disconnect card detection pull-up resistor.
740 */
741 err = sdio_disable_cd(card);
742 if (err)
743 goto remove;
744
Philip Rakitya303c532011-11-14 19:14:38 -0800745 /* Initialization sequence for UHS-I cards */
746 /* Only if card supports 1.8v and UHS signaling */
747 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
748 err = mmc_sdio_init_uhs_card(card);
749 if (err)
750 goto remove;
Philip Rakitya303c532011-11-14 19:14:38 -0800751 } else {
752 /*
753 * Switch to high-speed (if supported).
754 */
755 err = sdio_enable_hs(card);
756 if (err > 0)
Seungwon Jeoncdc99172014-04-23 17:07:35 +0900757 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
Philip Rakitya303c532011-11-14 19:14:38 -0800758 else if (err)
759 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700760
Philip Rakitya303c532011-11-14 19:14:38 -0800761 /*
762 * Change to the card's maximum speed.
763 */
764 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700765
Philip Rakitya303c532011-11-14 19:14:38 -0800766 /*
767 * Switch to wider bus (if supported).
768 */
769 err = sdio_enable_4bit_bus(card);
NeilBrown303dbed2015-02-24 13:42:23 +1100770 if (err)
Philip Rakitya303c532011-11-14 19:14:38 -0800771 goto remove;
772 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700773finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700774 if (!oldcard)
775 host->card = card;
776 return 0;
777
778remove:
779 if (!oldcard)
780 mmc_remove_card(card);
781
782err:
783 return err;
784}
785
786/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200787 * Host is being removed. Free up the current card.
788 */
789static void mmc_sdio_remove(struct mmc_host *host)
790{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200791 int i;
792
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200793 BUG_ON(!host);
794 BUG_ON(!host->card);
795
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200796 for (i = 0;i < host->card->sdio_funcs;i++) {
797 if (host->card->sdio_func[i]) {
798 sdio_remove_func(host->card->sdio_func[i]);
799 host->card->sdio_func[i] = NULL;
800 }
801 }
802
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200803 mmc_remove_card(host->card);
804 host->card = NULL;
805}
806
807/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200808 * Card detection - card is alive.
809 */
810static int mmc_sdio_alive(struct mmc_host *host)
811{
812 return mmc_select_card(host->card);
813}
814
815/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200816 * Card detection callback from host.
817 */
818static void mmc_sdio_detect(struct mmc_host *host)
819{
820 int err;
821
822 BUG_ON(!host);
823 BUG_ON(!host->card);
824
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200825 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200826 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
827 err = pm_runtime_get_sync(&host->card->dev);
Li Fei3bffb802013-04-08 09:36:39 +0800828 if (err < 0) {
829 pm_runtime_put_noidle(&host->card->dev);
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200830 goto out;
Li Fei3bffb802013-04-08 09:36:39 +0800831 }
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200832 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200833
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200834 mmc_claim_host(host);
835
836 /*
837 * Just check if our card has been removed.
838 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200839 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200840
841 mmc_release_host(host);
842
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200843 /*
844 * Tell PM core it's OK to power off the card now.
845 *
846 * The _sync variant is used in order to ensure that the card
847 * is left powered off in case an error occurred, and the card
848 * is going to be removed.
849 *
850 * Since there is no specific reason to believe a new user
851 * is about to show up at this point, the _sync variant is
852 * desirable anyway.
853 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200854 if (host->caps & MMC_CAP_POWER_OFF_CARD)
855 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200856
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200857out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200858 if (err) {
859 mmc_sdio_remove(host);
860
861 mmc_claim_host(host);
862 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400863 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200864 mmc_release_host(host);
865 }
866}
867
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700868/*
Ulf Hansson810cadd2013-06-10 17:03:37 +0200869 * SDIO pre_suspend. We need to suspend all functions separately.
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700870 * Therefore all registered functions must have drivers with suspend
871 * and resume methods. Failing that we simply remove the whole card.
872 */
Ulf Hansson810cadd2013-06-10 17:03:37 +0200873static int mmc_sdio_pre_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700874{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700875 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700876
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700877 for (i = 0; i < host->card->sdio_funcs; i++) {
878 struct sdio_func *func = host->card->sdio_func[i];
879 if (func && sdio_func_present(func) && func->dev.driver) {
880 const struct dev_pm_ops *pmops = func->dev.driver->pm;
881 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700882 /* force removal of entire card in that case */
883 err = -ENOSYS;
Ulf Hansson810cadd2013-06-10 17:03:37 +0200884 break;
885 }
886 }
887 }
888
889 return err;
890}
891
892/*
893 * SDIO suspend. Suspend all functions separately.
894 */
895static int mmc_sdio_suspend(struct mmc_host *host)
896{
Ulf Hansson573185c2014-02-28 12:49:00 +0100897 if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Daniel Drake6b5eda32010-03-05 13:43:34 -0800898 mmc_claim_host(host);
899 sdio_disable_wide(host->card);
900 mmc_release_host(host);
901 }
902
Adrian Hunter79d5a652015-05-07 13:10:13 +0300903 if (!mmc_card_keep_power(host)) {
Ulf Hansson74590262013-06-10 17:03:38 +0200904 mmc_power_off(host);
Adrian Hunter79d5a652015-05-07 13:10:13 +0300905 } else if (host->retune_period) {
906 mmc_retune_timer_stop(host);
907 mmc_retune_needed(host);
908 }
Ulf Hansson74590262013-06-10 17:03:38 +0200909
Ulf Hansson573185c2014-02-28 12:49:00 +0100910 return 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700911}
912
913static int mmc_sdio_resume(struct mmc_host *host)
914{
Ulf Hansson573185c2014-02-28 12:49:00 +0100915 int err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700916
917 BUG_ON(!host);
918 BUG_ON(!host->card);
919
920 /* Basic card reinitialization. */
921 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200922
Ulf Hansson74590262013-06-10 17:03:38 +0200923 /* Restore power if needed */
924 if (!mmc_card_keep_power(host)) {
Ulf Hansson69041152013-09-13 11:31:33 +0200925 mmc_power_up(host, host->card->ocr);
Ulf Hansson74590262013-06-10 17:03:38 +0200926 /*
927 * Tell runtime PM core we just powered up the card,
928 * since it still believes the card is powered off.
929 * Note that currently runtime PM is only enabled
930 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
931 */
932 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
933 pm_runtime_disable(&host->card->dev);
934 pm_runtime_set_active(&host->card->dev);
935 pm_runtime_enable(&host->card->dev);
936 }
937 }
938
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200939 /* No need to reinitialize powered-resumed nonremovable cards */
Subhash Jadavani51aa66a2012-12-04 17:06:18 +0530940 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
941 sdio_reset(host);
942 mmc_go_idle(host);
NeilBrown006fc512014-11-12 14:55:19 +1100943 mmc_send_if_cond(host, host->card->ocr);
944 err = mmc_send_io_op_cond(host, 0, NULL);
945 if (!err)
946 err = mmc_sdio_init_card(host, host->card->ocr,
947 host->card,
948 mmc_card_keep_power(host));
Subhash Jadavani51aa66a2012-12-04 17:06:18 +0530949 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200950 /* We may have switched to 1-bit mode during suspend */
951 err = sdio_enable_4bit_bus(host->card);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200952 }
953
Fu Zhonghuidea67c42014-08-18 10:48:14 +0800954 if (!err && host->sdio_irqs) {
955 if (!(host->caps2 & MMC_CAP2_SDIO_IRQ_NOTHREAD)) {
956 wake_up_process(host->sdio_irq_thread);
957 } else if (host->caps & MMC_CAP_SDIO_IRQ) {
958 mmc_host_clk_hold(host);
959 host->ops->enable_sdio_irq(host, 1);
960 mmc_host_clk_release(host);
961 }
962 }
963
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700964 mmc_release_host(host);
965
Ulf Hansson74590262013-06-10 17:03:38 +0200966 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700967 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700968}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200969
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +0200970static int mmc_sdio_power_restore(struct mmc_host *host)
971{
972 int ret;
973
974 BUG_ON(!host);
975 BUG_ON(!host->card);
976
977 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +0100978
979 /*
980 * Reset the card by performing the same steps that are taken by
981 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
982 *
983 * sdio_reset() is technically not needed. Having just powered up the
984 * hardware, it should already be in reset state. However, some
985 * platforms (such as SD8686 on OLPC) do not instantly cut power,
986 * meaning that a reset is required when restoring power soon after
987 * powering off. It is harmless in other cases.
988 *
989 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
990 * is not necessary for non-removable cards. However, it is required
991 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
992 * harmless in other situations.
993 *
Daniel Drakec6e633a2011-06-25 19:20:11 +0100994 */
Ulf Hanssone7747472012-03-01 13:18:05 +0100995
Daniel Drakec6e633a2011-06-25 19:20:11 +0100996 sdio_reset(host);
997 mmc_go_idle(host);
NeilBrown59b6c9e2014-11-12 14:55:19 +1100998 mmc_send_if_cond(host, host->card->ocr);
Daniel Drakec6e633a2011-06-25 19:20:11 +0100999
Ulf Hanssonce69d372013-09-16 11:28:42 +02001000 ret = mmc_send_io_op_cond(host, 0, NULL);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001001 if (ret)
1002 goto out;
1003
Ulf Hanssonce69d372013-09-16 11:28:42 +02001004 ret = mmc_sdio_init_card(host, host->card->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001005 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001006 if (!ret && host->sdio_irqs)
1007 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001008
1009out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001010 mmc_release_host(host);
1011
1012 return ret;
1013}
1014
Ulf Hansson12d01d02013-05-02 14:02:37 +02001015static int mmc_sdio_runtime_suspend(struct mmc_host *host)
1016{
1017 /* No references to the card, cut the power to it. */
1018 mmc_power_off(host);
1019 return 0;
1020}
1021
1022static int mmc_sdio_runtime_resume(struct mmc_host *host)
1023{
1024 /* Restore power and re-initialize. */
Ulf Hansson69041152013-09-13 11:31:33 +02001025 mmc_power_up(host, host->card->ocr);
Ulf Hansson12d01d02013-05-02 14:02:37 +02001026 return mmc_sdio_power_restore(host);
1027}
1028
Andreas Fenkart1fb654f2015-04-23 13:15:42 +02001029static int mmc_sdio_reset(struct mmc_host *host)
1030{
1031 mmc_power_cycle(host, host->card->ocr);
1032 return mmc_sdio_power_restore(host);
1033}
1034
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001035static const struct mmc_bus_ops mmc_sdio_ops = {
1036 .remove = mmc_sdio_remove,
1037 .detect = mmc_sdio_detect,
Ulf Hansson810cadd2013-06-10 17:03:37 +02001038 .pre_suspend = mmc_sdio_pre_suspend,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001039 .suspend = mmc_sdio_suspend,
1040 .resume = mmc_sdio_resume,
Ulf Hansson12d01d02013-05-02 14:02:37 +02001041 .runtime_suspend = mmc_sdio_runtime_suspend,
1042 .runtime_resume = mmc_sdio_runtime_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001043 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001044 .alive = mmc_sdio_alive,
Andreas Fenkart1fb654f2015-04-23 13:15:42 +02001045 .reset = mmc_sdio_reset,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001046};
1047
1048
1049/*
1050 * Starting point for SDIO card init.
1051 */
Andy Ross807e8e42011-01-03 10:36:56 -08001052int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001053{
Andy Ross807e8e42011-01-03 10:36:56 -08001054 int err, i, funcs;
Ulf Hansson69041152013-09-13 11:31:33 +02001055 u32 ocr, rocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001056 struct mmc_card *card;
1057
1058 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001059 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001060
Andy Ross807e8e42011-01-03 10:36:56 -08001061 err = mmc_send_io_op_cond(host, 0, &ocr);
1062 if (err)
1063 return err;
1064
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001065 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001066 if (host->ocr_avail_sdio)
1067 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001068
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001069
Ulf Hansson69041152013-09-13 11:31:33 +02001070 rocr = mmc_select_voltage(host, ocr);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001071
1072 /*
1073 * Can we support the voltage(s) of the card(s)?
1074 */
Ulf Hansson69041152013-09-13 11:31:33 +02001075 if (!rocr) {
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001076 err = -EINVAL;
1077 goto err;
1078 }
1079
1080 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001081 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001082 */
Ulf Hansson69041152013-09-13 11:31:33 +02001083 err = mmc_sdio_init_card(host, rocr, NULL, 0);
Ulf Hanssonec2ed702013-09-12 16:38:56 +02001084 if (err)
1085 goto err;
1086
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001087 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001088
1089 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001090 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001091 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001092 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1093 /*
1094 * Let runtime PM core know our card is active
1095 */
1096 err = pm_runtime_set_active(&card->dev);
1097 if (err)
1098 goto remove;
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001099
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001100 /*
1101 * Enable runtime PM for this card
1102 */
1103 pm_runtime_enable(&card->dev);
1104 }
Ohad Ben-Cohen819685612010-10-02 13:54:10 +02001105
1106 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001107 * The number of functions on the card is encoded inside
1108 * the ocr.
1109 */
Matt Fleminge8812792009-12-17 15:27:18 -08001110 funcs = (ocr & 0x70000000) >> 28;
1111 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001112
1113 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001114 * Initialize (but don't add) all present functions.
1115 */
Matt Fleminge8812792009-12-17 15:27:18 -08001116 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001117 err = sdio_init_func(host->card, i + 1);
1118 if (err)
1119 goto remove;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001120
1121 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001122 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001123 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001124 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1125 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001126 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001127
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001128 /*
1129 * First add the card to the driver model...
1130 */
Andy Ross807e8e42011-01-03 10:36:56 -08001131 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001132 err = mmc_add_card(host->card);
1133 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001134 goto remove_added;
1135
1136 /*
1137 * ...then the SDIO functions.
1138 */
1139 for (i = 0;i < funcs;i++) {
1140 err = sdio_add_func(host->card->sdio_func[i]);
1141 if (err)
1142 goto remove_added;
1143 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001144
Dmitry Shmidt34497912011-03-03 17:40:10 -05001145 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001146 return 0;
1147
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001148
1149remove_added:
1150 /* Remove without lock if the device has been added. */
1151 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001152 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001153remove:
1154 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001155 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001156 if (host->card)
1157 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001158 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001159err:
1160 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001161
Girish K Sa3c76eb2011-10-11 11:44:09 +05301162 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001163 mmc_hostname(host), err);
1164
1165 return err;
1166}
1167