blob: 4e76f61613f8e09171cc8f06ca51023b5e98b898 [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>
Dmitry Shmidt4c5391f2009-07-29 10:22:03 -070013#include <linux/module.h>
Ohad Ben-Cohen81968562010-10-02 13:54:10 +020014#include <linux/pm_runtime.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020015
16#include <linux/mmc/host.h>
17#include <linux/mmc/card.h>
Girish K Sa4924c72012-01-11 14:04:52 -050018#include <linux/mmc/mmc.h>
Pierre Ossman35c66c12007-06-11 20:25:43 +020019#include <linux/mmc/sdio.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020020#include <linux/mmc/sdio_func.h>
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030021#include <linux/mmc/sdio_ids.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020022
23#include "core.h"
24#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
San Mehate01587a2008-04-14 15:22:49 -070032#ifdef CONFIG_MMC_EMBEDDED_SDIO
33#include <linux/mmc/sdio_ids.h>
34#endif
35
Pierre Ossman05970072007-06-11 21:01:00 +020036static int sdio_read_fbr(struct sdio_func *func)
37{
38 int ret;
39 unsigned char data;
40
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030041 if (mmc_card_nonstd_func_interface(func->card)) {
42 func->class = SDIO_CLASS_NONE;
43 return 0;
44 }
45
Pierre Ossman05970072007-06-11 21:01:00 +020046 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010047 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020048 if (ret)
49 goto out;
50
51 data &= 0x0f;
52
53 if (data == 0x0f) {
54 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010055 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020056 if (ret)
57 goto out;
58 }
59
60 func->class = data;
61
62out:
63 return ret;
64}
65
Pierre Ossmane29a7d72007-05-26 13:48:18 +020066static int sdio_init_func(struct mmc_card *card, unsigned int fn)
67{
Pierre Ossman05970072007-06-11 21:01:00 +020068 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020069 struct sdio_func *func;
70
71 BUG_ON(fn > SDIO_MAX_FUNCS);
72
73 func = sdio_alloc_func(card);
74 if (IS_ERR(func))
75 return PTR_ERR(func);
76
77 func->num = fn;
78
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070079 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
80 ret = sdio_read_fbr(func);
81 if (ret)
82 goto fail;
Pierre Ossman05970072007-06-11 21:01:00 +020083
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070084 ret = sdio_read_func_cis(func);
85 if (ret)
86 goto fail;
87 } else {
88 func->vendor = func->card->cis.vendor;
89 func->device = func->card->cis.device;
90 func->max_blksize = func->card->cis.blksize;
91 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040092
Pierre Ossmane29a7d72007-05-26 13:48:18 +020093 card->sdio_func[fn - 1] = func;
94
95 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020096
97fail:
98 /*
99 * It is okay to remove the function here even though we hold
100 * the host lock as we haven't registered the device yet.
101 */
102 sdio_remove_func(func);
103 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200104}
105
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800106static int sdio_read_cccr(struct mmc_card *card, u32 ocr)
Pierre Ossman35c66c12007-06-11 20:25:43 +0200107{
108 int ret;
109 int cccr_vsn;
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800110 int uhs = ocr & R4_18V_PRESENT;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200111 unsigned char data;
Philip Rakitya303c532011-11-14 19:14:38 -0800112 unsigned char speed;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200113
114 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
115
116 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
117 if (ret)
118 goto out;
119
120 cccr_vsn = data & 0x0f;
121
Bing Zhaob4625da2011-10-20 19:16:32 -0700122 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530123 pr_err("%s: unrecognised CCCR structure version %d\n",
Pierre Ossman35c66c12007-06-11 20:25:43 +0200124 mmc_hostname(card->host), cccr_vsn);
125 return -EINVAL;
126 }
127
128 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
129
130 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
131 if (ret)
132 goto out;
133
134 if (data & SDIO_CCCR_CAP_SMB)
135 card->cccr.multi_block = 1;
136 if (data & SDIO_CCCR_CAP_LSC)
137 card->cccr.low_speed = 1;
138 if (data & SDIO_CCCR_CAP_4BLS)
139 card->cccr.wide_bus = 1;
140
141 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
142 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
143 if (ret)
144 goto out;
145
146 if (data & SDIO_POWER_SMPC)
147 card->cccr.high_power = 1;
148 }
149
150 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
Philip Rakitya303c532011-11-14 19:14:38 -0800151 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
Pierre Ossman35c66c12007-06-11 20:25:43 +0200152 if (ret)
153 goto out;
154
Philip Rakitya303c532011-11-14 19:14:38 -0800155 card->scr.sda_spec3 = 0;
156 card->sw_caps.sd3_bus_mode = 0;
157 card->sw_caps.sd3_drv_type = 0;
Philip Rakity2d0d68f2012-01-26 06:57:10 -0800158 if (cccr_vsn >= SDIO_CCCR_REV_3_00 && uhs) {
Philip Rakitya303c532011-11-14 19:14:38 -0800159 card->scr.sda_spec3 = 1;
160 ret = mmc_io_rw_direct(card, 0, 0,
161 SDIO_CCCR_UHS, 0, &data);
162 if (ret)
163 goto out;
164
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +0530165 if (mmc_host_uhs(card->host)) {
Philip Rakitya303c532011-11-14 19:14:38 -0800166 if (data & SDIO_UHS_DDR50)
167 card->sw_caps.sd3_bus_mode
168 |= SD_MODE_UHS_DDR50;
169
170 if (data & SDIO_UHS_SDR50)
171 card->sw_caps.sd3_bus_mode
172 |= SD_MODE_UHS_SDR50;
173
174 if (data & SDIO_UHS_SDR104)
175 card->sw_caps.sd3_bus_mode
176 |= SD_MODE_UHS_SDR104;
177 }
178
179 ret = mmc_io_rw_direct(card, 0, 0,
180 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
181 if (ret)
182 goto out;
183
184 if (data & SDIO_DRIVE_SDTA)
185 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
186 if (data & SDIO_DRIVE_SDTC)
187 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
188 if (data & SDIO_DRIVE_SDTD)
189 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
190 }
191
192 /* if no uhs mode ensure we check for high speed */
193 if (!card->sw_caps.sd3_bus_mode) {
194 if (speed & SDIO_SPEED_SHS) {
195 card->cccr.high_speed = 1;
196 card->sw_caps.hs_max_dtr = 50000000;
197 } else {
198 card->cccr.high_speed = 0;
199 card->sw_caps.hs_max_dtr = 25000000;
200 }
201 }
Pierre Ossman35c66c12007-06-11 20:25:43 +0200202 }
203
204out:
205 return ret;
206}
207
Pierre Ossman4ff64712007-07-30 18:23:53 +0200208static int sdio_enable_wide(struct mmc_card *card)
209{
210 int ret;
211 u8 ctrl;
212
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700213 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Pierre Ossman4ff64712007-07-30 18:23:53 +0200214 return 0;
215
216 if (card->cccr.low_speed && !card->cccr.wide_bus)
217 return 0;
218
219 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
220 if (ret)
221 return ret;
222
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700223 if (card->host->caps & MMC_CAP_8_BIT_DATA)
224 ctrl |= SDIO_BUS_WIDTH_8BIT;
225 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
226 ctrl |= SDIO_BUS_WIDTH_4BIT;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200227
228 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
229 if (ret)
230 return ret;
231
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700232 return 1;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200233}
234
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200235/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700236 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
237 * of the card. This may be required on certain setups of boards,
238 * controllers and embedded sdio device which do not need the card's
239 * pull-up. As a result, card detection is disabled and power is saved.
240 */
241static int sdio_disable_cd(struct mmc_card *card)
242{
243 int ret;
244 u8 ctrl;
245
Ohad Ben-Cohen2059a022011-04-05 18:02:25 +0300246 if (!mmc_card_disable_cd(card))
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700247 return 0;
248
249 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
250 if (ret)
251 return ret;
252
253 ctrl |= SDIO_BUS_CD_DISABLE;
254
255 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
256}
257
258/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800259 * Devices that remain active during a system suspend are
260 * put back into 1-bit mode.
261 */
262static int sdio_disable_wide(struct mmc_card *card)
263{
264 int ret;
265 u8 ctrl;
266
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700267 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800268 return 0;
269
270 if (card->cccr.low_speed && !card->cccr.wide_bus)
271 return 0;
272
273 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
274 if (ret)
275 return ret;
276
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700277 if (!(ctrl & (SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800278 return 0;
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 ctrl &= ~(SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT);
Daniel Drake6b5eda32010-03-05 13:43:34 -0800281 ctrl |= SDIO_BUS_ASYNC_INT;
282
283 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
284 if (ret)
285 return ret;
286
287 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
288
289 return 0;
290}
291
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700292
293static int sdio_enable_4bit_bus(struct mmc_card *card)
294{
295 int err;
296
297 if (card->type == MMC_TYPE_SDIO)
298 return sdio_enable_wide(card);
299
300 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
301 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
302 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
303 if (err)
304 return err;
305 } else
306 return 0;
307
308 err = sdio_enable_wide(card);
309 if (err <= 0)
310 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
311
312 return err;
313}
314
315
Daniel Drake6b5eda32010-03-05 13:43:34 -0800316/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200317 * Test if the card supports high-speed mode and, if so, switch to it.
318 */
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700319static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
Pierre Ossmand16f5772008-08-31 17:22:46 +0200320{
321 int ret;
322 u8 speed;
323
324 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
325 return 0;
326
327 if (!card->cccr.high_speed)
328 return 0;
329
330 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
331 if (ret)
332 return ret;
333
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700334 if (enable)
335 speed |= SDIO_SPEED_EHS;
336 else
337 speed &= ~SDIO_SPEED_EHS;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200338
339 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
340 if (ret)
341 return ret;
342
Michal Miroslaw71578a12010-08-10 18:01:40 -0700343 return 1;
344}
Pierre Ossmand16f5772008-08-31 17:22:46 +0200345
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700346/*
347 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
348 */
349static int sdio_enable_hs(struct mmc_card *card)
350{
351 int ret;
352
353 ret = mmc_sdio_switch_hs(card, true);
354 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
355 return ret;
356
357 ret = mmc_sd_switch_hs(card);
358 if (ret <= 0)
359 mmc_sdio_switch_hs(card, false);
360
361 return ret;
362}
363
Michal Miroslaw71578a12010-08-10 18:01:40 -0700364static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
365{
366 unsigned max_dtr;
367
368 if (mmc_card_highspeed(card)) {
369 /*
370 * The SDIO specification doesn't mention how
371 * the CIS transfer speed register relates to
372 * high-speed, but it seems that 50 MHz is
373 * mandatory.
374 */
375 max_dtr = 50000000;
376 } else {
377 max_dtr = card->cis.max_dtr;
378 }
379
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700380 if (card->type == MMC_TYPE_SD_COMBO)
381 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
382
Michal Miroslaw71578a12010-08-10 18:01:40 -0700383 return max_dtr;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200384}
385
Philip Rakitya303c532011-11-14 19:14:38 -0800386static unsigned char host_drive_to_sdio_drive(int host_strength)
387{
388 switch (host_strength) {
389 case MMC_SET_DRIVER_TYPE_A:
390 return SDIO_DTSx_SET_TYPE_A;
391 case MMC_SET_DRIVER_TYPE_B:
392 return SDIO_DTSx_SET_TYPE_B;
393 case MMC_SET_DRIVER_TYPE_C:
394 return SDIO_DTSx_SET_TYPE_C;
395 case MMC_SET_DRIVER_TYPE_D:
396 return SDIO_DTSx_SET_TYPE_D;
397 default:
398 return SDIO_DTSx_SET_TYPE_B;
399 }
400}
401
402static void sdio_select_driver_type(struct mmc_card *card)
403{
404 int host_drv_type = SD_DRIVER_TYPE_B;
405 int card_drv_type = SD_DRIVER_TYPE_B;
406 int drive_strength;
407 unsigned char card_strength;
408 int err;
409
410 /*
411 * If the host doesn't support any of the Driver Types A,C or D,
412 * or there is no board specific handler then default Driver
413 * Type B is used.
414 */
415 if (!(card->host->caps &
416 (MMC_CAP_DRIVER_TYPE_A |
417 MMC_CAP_DRIVER_TYPE_C |
418 MMC_CAP_DRIVER_TYPE_D)))
419 return;
420
421 if (!card->host->ops->select_drive_strength)
422 return;
423
424 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
425 host_drv_type |= SD_DRIVER_TYPE_A;
426
427 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
428 host_drv_type |= SD_DRIVER_TYPE_C;
429
430 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
431 host_drv_type |= SD_DRIVER_TYPE_D;
432
433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
434 card_drv_type |= SD_DRIVER_TYPE_A;
435
436 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
437 card_drv_type |= SD_DRIVER_TYPE_C;
438
439 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
440 card_drv_type |= SD_DRIVER_TYPE_D;
441
442 /*
443 * The drive strength that the hardware can support
444 * depends on the board design. Pass the appropriate
445 * information and let the hardware specific code
446 * return what is possible given the options
447 */
448 drive_strength = card->host->ops->select_drive_strength(
449 card->sw_caps.uhs_max_dtr,
450 host_drv_type, card_drv_type);
451
452 /* if error just use default for drive strength B */
453 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
454 &card_strength);
455 if (err)
456 return;
457
458 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
459 card_strength |= host_drive_to_sdio_drive(drive_strength);
460
461 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
462 card_strength, NULL);
463
464 /* if error default to drive strength B */
465 if (!err)
466 mmc_set_driver_type(card->host, drive_strength);
467}
468
469
470static int sdio_set_bus_speed_mode(struct mmc_card *card)
471{
472 unsigned int bus_speed, timing;
473 int err;
474 unsigned char speed;
475
476 /*
477 * If the host doesn't support any of the UHS-I modes, fallback on
478 * default speed.
479 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +0530480 if (!mmc_host_uhs(card->host))
Philip Rakitya303c532011-11-14 19:14:38 -0800481 return 0;
482
483 bus_speed = SDIO_SPEED_SDR12;
484 timing = MMC_TIMING_UHS_SDR12;
485 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
487 bus_speed = SDIO_SPEED_SDR104;
488 timing = MMC_TIMING_UHS_SDR104;
489 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530490 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800491 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
492 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
493 bus_speed = SDIO_SPEED_DDR50;
494 timing = MMC_TIMING_UHS_DDR50;
495 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530496 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800497 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
498 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
499 SD_MODE_UHS_SDR50)) {
500 bus_speed = SDIO_SPEED_SDR50;
501 timing = MMC_TIMING_UHS_SDR50;
502 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530503 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800504 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
505 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
506 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
507 bus_speed = SDIO_SPEED_SDR25;
508 timing = MMC_TIMING_UHS_SDR25;
509 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530510 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800511 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
512 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
513 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
514 SD_MODE_UHS_SDR12)) {
515 bus_speed = SDIO_SPEED_SDR12;
516 timing = MMC_TIMING_UHS_SDR12;
517 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530518 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800519 }
520
521 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
522 if (err)
523 return err;
524
525 speed &= ~SDIO_SPEED_BSS_MASK;
526 speed |= bus_speed;
527 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
528 if (err)
529 return err;
530
531 if (bus_speed) {
532 mmc_set_timing(card->host, timing);
533 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
534 }
535
536 return 0;
537}
538
539/*
540 * UHS-I specific initialization procedure
541 */
542static int mmc_sdio_init_uhs_card(struct mmc_card *card)
543{
544 int err;
545
546 if (!card->scr.sda_spec3)
547 return 0;
548
549 /*
550 * Switch to wider bus (if supported).
551 */
552 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
553 err = sdio_enable_4bit_bus(card);
554 if (err > 0) {
555 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
556 err = 0;
557 }
558 }
559
560 /* Set the driver strength for the card */
561 sdio_select_driver_type(card);
562
563 /* Set bus speed mode of the card */
564 err = sdio_set_bus_speed_mode(card);
565 if (err)
566 goto out;
567
568 /* Initialize and start re-tuning timer */
569 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
Girish K Sa4924c72012-01-11 14:04:52 -0500570 err = card->host->ops->execute_tuning(card->host,
571 MMC_SEND_TUNING_BLOCK);
Philip Rakitya303c532011-11-14 19:14:38 -0800572
573out:
574
575 return err;
576}
577
Pierre Ossmand16f5772008-08-31 17:22:46 +0200578/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700579 * Handle the detection and initialisation of a card.
580 *
581 * In the case of a resume, "oldcard" will contain the card
582 * we're trying to reinitialise.
583 */
584static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf2010-03-05 13:43:33 -0800585 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700586{
587 struct mmc_card *card;
588 int err;
589
590 BUG_ON(!host);
591 WARN_ON(!host->claimed);
592
593 /*
594 * Inform the card of the voltage
595 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800596 if (!powered_resume) {
Ulf Hanssone7747472012-03-01 13:18:05 +0100597 /* The initialization should be done at 3.3 V I/O voltage. */
598 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
599
Chris Ball3bca4cf2010-03-05 13:43:33 -0800600 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
601 if (err)
602 goto err;
603 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700604
605 /*
606 * For SPI, enable CRC as appropriate.
607 */
608 if (mmc_host_is_spi(host)) {
609 err = mmc_spi_set_crc(host, use_spi_crc);
610 if (err)
611 goto err;
612 }
613
614 /*
615 * Allocate card structure.
616 */
617 card = mmc_alloc_card(host, NULL);
618 if (IS_ERR(card)) {
619 err = PTR_ERR(card);
620 goto err;
621 }
622
Arindam Nathd6d50a12011-05-05 12:18:59 +0530623 if ((ocr & R4_MEMORY_PRESENT) &&
624 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700625 card->type = MMC_TYPE_SD_COMBO;
626
627 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
628 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
629 mmc_remove_card(card);
630 return -ENOENT;
631 }
632 } else {
633 card->type = MMC_TYPE_SDIO;
634
635 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
636 mmc_remove_card(card);
637 return -ENOENT;
638 }
639 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700640
641 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200642 * Call the optional HC's init_card function to handle quirks.
643 */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530644 if (host->ops->init_card) {
645 mmc_host_clk_hold(host);
Daniel Mack3fcb0272010-04-01 10:03:25 +0200646 host->ops->init_card(host, card);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530647 mmc_host_clk_release(host);
648 }
Daniel Mack3fcb0272010-04-01 10:03:25 +0200649
650 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800651 * If the host and card support UHS-I mode request the card
652 * to switch to 1.8V signaling level. No 1.8v signalling if
653 * UHS mode is not enabled to maintain compatibilty and some
654 * systems that claim 1.8v signalling in fact do not support
655 * it.
656 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +0530657 if ((ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) {
Philip Rakitya303c532011-11-14 19:14:38 -0800658 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
659 true);
660 if (err) {
661 ocr &= ~R4_18V_PRESENT;
662 host->ocr &= ~R4_18V_PRESENT;
663 }
664 err = 0;
665 } else {
666 ocr &= ~R4_18V_PRESENT;
667 host->ocr &= ~R4_18V_PRESENT;
668 }
669
670 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700671 * For native busses: set card RCA and quit open drain mode.
672 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800673 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700674 err = mmc_send_relative_addr(host, &card->rca);
675 if (err)
676 goto remove;
677
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100678 /*
679 * Update oldcard with the new RCA received from the SDIO
680 * device -- we're doing this so that it's updated in the
681 * "card" struct when oldcard overwrites that later.
682 */
683 if (oldcard)
684 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700685 }
686
687 /*
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700688 * Read CSD, before selecting the card
689 */
690 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
691 err = mmc_sd_get_csd(host, card);
692 if (err)
693 return err;
694
695 mmc_decode_cid(card);
696 }
697
698 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700699 * Select card, as all following commands rely on that.
700 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800701 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700702 err = mmc_select_card(card);
703 if (err)
704 goto remove;
705 }
706
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700707 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
708 /*
709 * This is non-standard SDIO device, meaning it doesn't
710 * have any CIA (Common I/O area) registers present.
711 * It's host's responsibility to fill cccr and cis
712 * structures in init_card().
713 */
714 mmc_set_clock(host, card->cis.max_dtr);
715
716 if (card->cccr.high_speed) {
717 mmc_card_set_highspeed(card);
718 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
719 }
720
721 goto finish;
722 }
723
San Mehate01587a2008-04-14 15:22:49 -0700724#ifdef CONFIG_MMC_EMBEDDED_SDIO
725 if (host->embedded_sdio_data.cccr)
726 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
727 else {
728#endif
729 /*
730 * Read the common registers.
731 */
732 err = sdio_read_cccr(card, ocr);
733 if (err)
734 goto remove;
735#ifdef CONFIG_MMC_EMBEDDED_SDIO
736 }
737#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700738
San Mehate01587a2008-04-14 15:22:49 -0700739#ifdef CONFIG_MMC_EMBEDDED_SDIO
740 if (host->embedded_sdio_data.cis)
741 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
742 else {
743#endif
744 /*
745 * Read the common CIS tuples.
746 */
747 err = sdio_read_common_cis(card);
748 if (err)
749 goto remove;
750#ifdef CONFIG_MMC_EMBEDDED_SDIO
751 }
752#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700753
754 if (oldcard) {
755 int same = (card->cis.vendor == oldcard->cis.vendor &&
756 card->cis.device == oldcard->cis.device);
757 mmc_remove_card(card);
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700758 if (!same)
759 return -ENOENT;
760
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700761 card = oldcard;
762 }
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500763 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700764
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700765 if (card->type == MMC_TYPE_SD_COMBO) {
766 err = mmc_sd_setup_card(host, card, oldcard != NULL);
767 /* handle as SDIO-only card if memory init failed */
768 if (err) {
769 mmc_go_idle(host);
770 if (mmc_host_is_spi(host))
771 /* should not fail, as it worked previously */
772 mmc_spi_set_crc(host, use_spi_crc);
773 card->type = MMC_TYPE_SDIO;
774 } else
775 card->dev.type = &sd_type;
776 }
777
778 /*
779 * If needed, disconnect card detection pull-up resistor.
780 */
781 err = sdio_disable_cd(card);
782 if (err)
783 goto remove;
784
Philip Rakitya303c532011-11-14 19:14:38 -0800785 /* Initialization sequence for UHS-I cards */
786 /* Only if card supports 1.8v and UHS signaling */
787 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
788 err = mmc_sdio_init_uhs_card(card);
789 if (err)
790 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700791
Philip Rakitya303c532011-11-14 19:14:38 -0800792 /* Card is an ultra-high-speed card */
793 mmc_card_set_uhs(card);
794 } else {
795 /*
796 * Switch to high-speed (if supported).
797 */
798 err = sdio_enable_hs(card);
799 if (err > 0)
800 mmc_sd_go_highspeed(card);
801 else if (err)
802 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700803
Philip Rakitya303c532011-11-14 19:14:38 -0800804 /*
805 * Change to the card's maximum speed.
806 */
807 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700808
Philip Rakitya303c532011-11-14 19:14:38 -0800809 /*
810 * Switch to wider bus (if supported).
811 */
812 err = sdio_enable_4bit_bus(card);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700813 if (err > 0) {
814 if (card->host->caps & MMC_CAP_8_BIT_DATA)
815 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_8);
816 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
817 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
818 } else if (err)
Philip Rakitya303c532011-11-14 19:14:38 -0800819 goto remove;
820 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700821finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700822 if (!oldcard)
823 host->card = card;
824 return 0;
825
826remove:
827 if (!oldcard)
828 mmc_remove_card(card);
829
830err:
831 return err;
832}
833
834/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200835 * Host is being removed. Free up the current card.
836 */
837static void mmc_sdio_remove(struct mmc_host *host)
838{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200839 int i;
840
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200841 BUG_ON(!host);
842 BUG_ON(!host->card);
843
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200844 for (i = 0;i < host->card->sdio_funcs;i++) {
845 if (host->card->sdio_func[i]) {
846 sdio_remove_func(host->card->sdio_func[i]);
847 host->card->sdio_func[i] = NULL;
848 }
849 }
850
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200851 mmc_remove_card(host->card);
852 host->card = NULL;
853}
854
855/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200856 * Card detection - card is alive.
857 */
858static int mmc_sdio_alive(struct mmc_host *host)
859{
860 return mmc_select_card(host->card);
861}
862
863/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200864 * Card detection callback from host.
865 */
866static void mmc_sdio_detect(struct mmc_host *host)
867{
868 int err;
869
870 BUG_ON(!host);
871 BUG_ON(!host->card);
872
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200873 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200874 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
875 err = pm_runtime_get_sync(&host->card->dev);
876 if (err < 0)
877 goto out;
878 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200879
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200880 mmc_claim_host(host);
881
882 /*
883 * Just check if our card has been removed.
884 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200885 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200886
887 mmc_release_host(host);
888
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200889 /*
890 * Tell PM core it's OK to power off the card now.
891 *
892 * The _sync variant is used in order to ensure that the card
893 * is left powered off in case an error occurred, and the card
894 * is going to be removed.
895 *
896 * Since there is no specific reason to believe a new user
897 * is about to show up at this point, the _sync variant is
898 * desirable anyway.
899 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200900 if (host->caps & MMC_CAP_POWER_OFF_CARD)
901 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200902
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200903out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200904 if (err) {
905 mmc_sdio_remove(host);
906
907 mmc_claim_host(host);
908 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400909 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200910 mmc_release_host(host);
911 }
912}
913
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700914/*
915 * SDIO suspend. We need to suspend all functions separately.
916 * Therefore all registered functions must have drivers with suspend
917 * and resume methods. Failing that we simply remove the whole card.
918 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700919static int mmc_sdio_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700920{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700921 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700922
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700923 for (i = 0; i < host->card->sdio_funcs; i++) {
924 struct sdio_func *func = host->card->sdio_func[i];
925 if (func && sdio_func_present(func) && func->dev.driver) {
926 const struct dev_pm_ops *pmops = func->dev.driver->pm;
927 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700928 /* force removal of entire card in that case */
929 err = -ENOSYS;
930 } else
931 err = pmops->suspend(&func->dev);
932 if (err)
933 break;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700934 }
935 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700936 while (err && --i >= 0) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700937 struct sdio_func *func = host->card->sdio_func[i];
938 if (func && sdio_func_present(func) && func->dev.driver) {
939 const struct dev_pm_ops *pmops = func->dev.driver->pm;
940 pmops->resume(&func->dev);
941 }
942 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700943
Ohad Ben-Cohen6b93d01f2011-04-05 17:43:21 +0300944 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Daniel Drake6b5eda32010-03-05 13:43:34 -0800945 mmc_claim_host(host);
946 sdio_disable_wide(host->card);
947 mmc_release_host(host);
948 }
949
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700950 return err;
951}
952
953static int mmc_sdio_resume(struct mmc_host *host)
954{
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200955 int i, err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700956
957 BUG_ON(!host);
958 BUG_ON(!host->card);
959
960 /* Basic card reinitialization. */
961 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200962
963 /* No need to reinitialize powered-resumed nonremovable cards */
Subhash Jadavani26793072012-11-27 12:28:13 +0530964 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
965 sdio_reset(host);
966 mmc_go_idle(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200967 err = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +0300968 mmc_card_keep_power(host));
Subhash Jadavani26793072012-11-27 12:28:13 +0530969 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200970 /* We may have switched to 1-bit mode during suspend */
971 err = sdio_enable_4bit_bus(host->card);
972 if (err > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700973 if (host->caps & MMC_CAP_8_BIT_DATA)
974 mmc_set_bus_width(host, MMC_BUS_WIDTH_8);
975 else if (host->caps & MMC_CAP_4_BIT_DATA)
976 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200977 err = 0;
978 }
979 }
980
Nicolas Pitre40216842010-03-05 13:43:34 -0800981 if (!err && host->sdio_irqs)
Nicolas Pitrefed14d92012-04-16 19:16:54 -0400982 wake_up_process(host->sdio_irq_thread);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700983 mmc_release_host(host);
984
985 /*
986 * If the card looked to be the same as before suspending, then
987 * we proceed to resume all card functions. If one of them returns
988 * an error then we simply return that error to the core and the
989 * card will be redetected as new. It is the responsibility of
990 * the function driver to perform further tests with the extra
991 * knowledge it has of the card to confirm the card is indeed the
992 * same as before suspending (same MAC address for network cards,
993 * etc.) and return an error otherwise.
994 */
995 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
996 struct sdio_func *func = host->card->sdio_func[i];
997 if (func && sdio_func_present(func) && func->dev.driver) {
998 const struct dev_pm_ops *pmops = func->dev.driver->pm;
999 err = pmops->resume(&func->dev);
1000 }
1001 }
1002
1003 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001004}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001005
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001006static int mmc_sdio_power_restore(struct mmc_host *host)
1007{
1008 int ret;
Daniel Drakec6e633a2011-06-25 19:20:11 +01001009 u32 ocr;
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001010
1011 BUG_ON(!host);
1012 BUG_ON(!host->card);
1013
1014 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001015
1016 /*
1017 * Reset the card by performing the same steps that are taken by
1018 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1019 *
1020 * sdio_reset() is technically not needed. Having just powered up the
1021 * hardware, it should already be in reset state. However, some
1022 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1023 * meaning that a reset is required when restoring power soon after
1024 * powering off. It is harmless in other cases.
1025 *
1026 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1027 * is not necessary for non-removable cards. However, it is required
1028 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1029 * harmless in other situations.
1030 *
1031 * With these steps taken, mmc_select_voltage() is also required to
1032 * restore the correct voltage setting of the card.
1033 */
Ulf Hanssone7747472012-03-01 13:18:05 +01001034
1035 /* The initialization should be done at 3.3 V I/O voltage. */
1036 if (!mmc_card_keep_power(host))
1037 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
1038
Daniel Drakec6e633a2011-06-25 19:20:11 +01001039 sdio_reset(host);
1040 mmc_go_idle(host);
1041 mmc_send_if_cond(host, host->ocr_avail);
1042
1043 ret = mmc_send_io_op_cond(host, 0, &ocr);
1044 if (ret)
1045 goto out;
1046
1047 if (host->ocr_avail_sdio)
1048 host->ocr_avail = host->ocr_avail_sdio;
1049
1050 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1051 if (!host->ocr) {
1052 ret = -EINVAL;
1053 goto out;
1054 }
1055
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +05301056 if (mmc_host_uhs(host))
1057 /* to query card if 1.8V signalling is supported */
1058 host->ocr |= R4_18V_PRESENT;
1059
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001060 ret = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001061 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001062 if (!ret && host->sdio_irqs)
1063 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001064
1065out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001066 mmc_release_host(host);
1067
1068 return ret;
1069}
1070
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001071static const struct mmc_bus_ops mmc_sdio_ops = {
1072 .remove = mmc_sdio_remove,
1073 .detect = mmc_sdio_detect,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001074 .suspend = mmc_sdio_suspend,
1075 .resume = mmc_sdio_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001076 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001077 .alive = mmc_sdio_alive,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001078};
1079
1080
1081/*
1082 * Starting point for SDIO card init.
1083 */
Andy Ross807e8e42011-01-03 10:36:56 -08001084int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001085{
Andy Ross807e8e42011-01-03 10:36:56 -08001086 int err, i, funcs;
1087 u32 ocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001088 struct mmc_card *card;
1089
1090 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001091 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001092
Andy Ross807e8e42011-01-03 10:36:56 -08001093 err = mmc_send_io_op_cond(host, 0, &ocr);
1094 if (err)
1095 return err;
1096
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001097 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001098 if (host->ocr_avail_sdio)
1099 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001100
1101 /*
1102 * Sanity check the voltages that the card claims to
1103 * support.
1104 */
1105 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301106 pr_warning("%s: card claims to support voltages "
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001107 "below the defined range. These will be ignored.\n",
1108 mmc_hostname(host));
1109 ocr &= ~0x7F;
1110 }
1111
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001112 host->ocr = mmc_select_voltage(host, ocr);
1113
1114 /*
1115 * Can we support the voltage(s) of the card(s)?
1116 */
1117 if (!host->ocr) {
1118 err = -EINVAL;
1119 goto err;
1120 }
1121
1122 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001123 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001124 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +05301125 if (mmc_host_uhs(host))
1126 /* to query card if 1.8V signalling is supported */
1127 host->ocr |= R4_18V_PRESENT;
1128
Chris Ball3bca4cf2010-03-05 13:43:33 -08001129 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
Philip Rakitya303c532011-11-14 19:14:38 -08001130 if (err) {
1131 if (err == -EAGAIN) {
1132 /*
1133 * Retry initialization with S18R set to 0.
1134 */
1135 host->ocr &= ~R4_18V_PRESENT;
1136 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1137 }
1138 if (err)
1139 goto err;
1140 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001141 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001142
1143 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001144 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001145 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001146 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1147 /*
1148 * Let runtime PM core know our card is active
1149 */
1150 err = pm_runtime_set_active(&card->dev);
1151 if (err)
1152 goto remove;
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001153
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001154 /*
1155 * Enable runtime PM for this card
1156 */
1157 pm_runtime_enable(&card->dev);
1158 }
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001159
1160 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001161 * The number of functions on the card is encoded inside
1162 * the ocr.
1163 */
Matt Fleminge8812792009-12-17 15:27:18 -08001164 funcs = (ocr & 0x70000000) >> 28;
1165 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001166
San Mehate01587a2008-04-14 15:22:49 -07001167#ifdef CONFIG_MMC_EMBEDDED_SDIO
1168 if (host->embedded_sdio_data.funcs)
1169 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1170#endif
1171
Pierre Ossman4ff64712007-07-30 18:23:53 +02001172 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001173 * Initialize (but don't add) all present functions.
1174 */
Matt Fleminge8812792009-12-17 15:27:18 -08001175 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
San Mehate01587a2008-04-14 15:22:49 -07001176#ifdef CONFIG_MMC_EMBEDDED_SDIO
1177 if (host->embedded_sdio_data.funcs) {
1178 struct sdio_func *tmp;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001179
San Mehate01587a2008-04-14 15:22:49 -07001180 tmp = sdio_alloc_func(host->card);
1181 if (IS_ERR(tmp))
1182 goto remove;
1183 tmp->num = (i + 1);
1184 card->sdio_func[i] = tmp;
1185 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1186 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1187 tmp->vendor = card->cis.vendor;
1188 tmp->device = card->cis.device;
1189 } else {
1190#endif
1191 err = sdio_init_func(host->card, i + 1);
1192 if (err)
1193 goto remove;
1194#ifdef CONFIG_MMC_EMBEDDED_SDIO
1195 }
1196#endif
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001197 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001198 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001199 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001200 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1201 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001202 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001203
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001204 /*
1205 * First add the card to the driver model...
1206 */
Andy Ross807e8e42011-01-03 10:36:56 -08001207 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001208 err = mmc_add_card(host->card);
1209 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001210 goto remove_added;
1211
1212 /*
1213 * ...then the SDIO functions.
1214 */
1215 for (i = 0;i < funcs;i++) {
1216 err = sdio_add_func(host->card->sdio_func[i]);
1217 if (err)
1218 goto remove_added;
1219 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001220
Dmitry Shmidt34497912011-03-03 17:40:10 -05001221 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001222 return 0;
1223
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001224
1225remove_added:
1226 /* Remove without lock if the device has been added. */
1227 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001228 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001229remove:
1230 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001231 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001232 if (host->card)
1233 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001234 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001235err:
1236 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001237
Girish K Sa3c76eb2011-10-11 11:44:09 +05301238 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001239 mmc_hostname(host), err);
1240
1241 return err;
1242}
1243
San Mehat35fe42e2008-05-15 09:15:37 -07001244int sdio_reset_comm(struct mmc_card *card)
1245{
Subhash Jadavani06673c52012-11-26 22:20:56 +05301246 return mmc_power_restore_host(card->host);
San Mehat35fe42e2008-05-15 09:15:37 -07001247}
1248EXPORT_SYMBOL(sdio_reset_comm);