blob: 91e23ca97ecc696360b0f7488b4d928345dd3d03 [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;
Subhash Jadavani5c4970f2013-05-24 19:22:10 +0530190
191 ret = mmc_io_rw_direct(card, 0, 0,
192 SDIO_CCCR_INTERRUPT_EXTENSION, 0, &data);
193 if (ret)
194 goto out;
195 if (data & SDIO_SUPPORT_ASYNC_INTR) {
196 if (card->host->caps2 &
197 MMC_CAP2_ASYNC_SDIO_IRQ_4BIT_MODE) {
198 data |= SDIO_ENABLE_ASYNC_INTR;
199 ret = mmc_io_rw_direct(card, 1, 0,
200 SDIO_CCCR_INTERRUPT_EXTENSION,
201 data, NULL);
202 if (ret)
203 goto out;
204 card->cccr.async_intr_sup = 1;
205 }
206 }
Philip Rakitya303c532011-11-14 19:14:38 -0800207 }
208
209 /* if no uhs mode ensure we check for high speed */
210 if (!card->sw_caps.sd3_bus_mode) {
211 if (speed & SDIO_SPEED_SHS) {
212 card->cccr.high_speed = 1;
213 card->sw_caps.hs_max_dtr = 50000000;
214 } else {
215 card->cccr.high_speed = 0;
216 card->sw_caps.hs_max_dtr = 25000000;
217 }
218 }
Pierre Ossman35c66c12007-06-11 20:25:43 +0200219 }
220
221out:
222 return ret;
223}
224
Pierre Ossman4ff64712007-07-30 18:23:53 +0200225static int sdio_enable_wide(struct mmc_card *card)
226{
227 int ret;
228 u8 ctrl;
229
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700230 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Pierre Ossman4ff64712007-07-30 18:23:53 +0200231 return 0;
232
233 if (card->cccr.low_speed && !card->cccr.wide_bus)
234 return 0;
235
236 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
237 if (ret)
238 return ret;
239
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700240 if (card->host->caps & MMC_CAP_8_BIT_DATA)
241 ctrl |= SDIO_BUS_WIDTH_8BIT;
242 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
243 ctrl |= SDIO_BUS_WIDTH_4BIT;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200244
245 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
246 if (ret)
247 return ret;
248
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700249 return 1;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200250}
251
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200252/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700253 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
254 * of the card. This may be required on certain setups of boards,
255 * controllers and embedded sdio device which do not need the card's
256 * pull-up. As a result, card detection is disabled and power is saved.
257 */
258static int sdio_disable_cd(struct mmc_card *card)
259{
260 int ret;
261 u8 ctrl;
262
Ohad Ben-Cohen2059a022011-04-05 18:02:25 +0300263 if (!mmc_card_disable_cd(card))
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700264 return 0;
265
266 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
267 if (ret)
268 return ret;
269
270 ctrl |= SDIO_BUS_CD_DISABLE;
271
272 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
273}
274
275/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800276 * Devices that remain active during a system suspend are
277 * put back into 1-bit mode.
278 */
279static int sdio_disable_wide(struct mmc_card *card)
280{
281 int ret;
282 u8 ctrl;
283
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700284 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800285 return 0;
286
287 if (card->cccr.low_speed && !card->cccr.wide_bus)
288 return 0;
289
290 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
291 if (ret)
292 return ret;
293
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700294 if (!(ctrl & (SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800295 return 0;
296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297 ctrl &= ~(SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT);
Daniel Drake6b5eda32010-03-05 13:43:34 -0800298 ctrl |= SDIO_BUS_ASYNC_INT;
299
300 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
301 if (ret)
302 return ret;
303
304 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
305
306 return 0;
307}
308
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700309
310static int sdio_enable_4bit_bus(struct mmc_card *card)
311{
312 int err;
313
314 if (card->type == MMC_TYPE_SDIO)
315 return sdio_enable_wide(card);
316
317 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
318 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
319 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
320 if (err)
321 return err;
322 } else
323 return 0;
324
325 err = sdio_enable_wide(card);
326 if (err <= 0)
327 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
328
329 return err;
330}
331
332
Daniel Drake6b5eda32010-03-05 13:43:34 -0800333/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200334 * Test if the card supports high-speed mode and, if so, switch to it.
335 */
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700336static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
Pierre Ossmand16f5772008-08-31 17:22:46 +0200337{
338 int ret;
339 u8 speed;
340
341 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
342 return 0;
343
344 if (!card->cccr.high_speed)
345 return 0;
346
347 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
348 if (ret)
349 return ret;
350
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700351 if (enable)
352 speed |= SDIO_SPEED_EHS;
353 else
354 speed &= ~SDIO_SPEED_EHS;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200355
356 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
357 if (ret)
358 return ret;
359
Michal Miroslaw71578a12010-08-10 18:01:40 -0700360 return 1;
361}
Pierre Ossmand16f5772008-08-31 17:22:46 +0200362
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700363/*
364 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
365 */
366static int sdio_enable_hs(struct mmc_card *card)
367{
368 int ret;
369
370 ret = mmc_sdio_switch_hs(card, true);
371 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
372 return ret;
373
374 ret = mmc_sd_switch_hs(card);
375 if (ret <= 0)
376 mmc_sdio_switch_hs(card, false);
377
378 return ret;
379}
380
Michal Miroslaw71578a12010-08-10 18:01:40 -0700381static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
382{
383 unsigned max_dtr;
384
385 if (mmc_card_highspeed(card)) {
386 /*
387 * The SDIO specification doesn't mention how
388 * the CIS transfer speed register relates to
389 * high-speed, but it seems that 50 MHz is
390 * mandatory.
391 */
392 max_dtr = 50000000;
393 } else {
394 max_dtr = card->cis.max_dtr;
395 }
396
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700397 if (card->type == MMC_TYPE_SD_COMBO)
398 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
399
Michal Miroslaw71578a12010-08-10 18:01:40 -0700400 return max_dtr;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200401}
402
Philip Rakitya303c532011-11-14 19:14:38 -0800403static unsigned char host_drive_to_sdio_drive(int host_strength)
404{
405 switch (host_strength) {
406 case MMC_SET_DRIVER_TYPE_A:
407 return SDIO_DTSx_SET_TYPE_A;
408 case MMC_SET_DRIVER_TYPE_B:
409 return SDIO_DTSx_SET_TYPE_B;
410 case MMC_SET_DRIVER_TYPE_C:
411 return SDIO_DTSx_SET_TYPE_C;
412 case MMC_SET_DRIVER_TYPE_D:
413 return SDIO_DTSx_SET_TYPE_D;
414 default:
415 return SDIO_DTSx_SET_TYPE_B;
416 }
417}
418
419static void sdio_select_driver_type(struct mmc_card *card)
420{
421 int host_drv_type = SD_DRIVER_TYPE_B;
422 int card_drv_type = SD_DRIVER_TYPE_B;
423 int drive_strength;
424 unsigned char card_strength;
425 int err;
426
427 /*
428 * If the host doesn't support any of the Driver Types A,C or D,
429 * or there is no board specific handler then default Driver
430 * Type B is used.
431 */
432 if (!(card->host->caps &
433 (MMC_CAP_DRIVER_TYPE_A |
434 MMC_CAP_DRIVER_TYPE_C |
435 MMC_CAP_DRIVER_TYPE_D)))
436 return;
437
438 if (!card->host->ops->select_drive_strength)
439 return;
440
441 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
442 host_drv_type |= SD_DRIVER_TYPE_A;
443
444 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
445 host_drv_type |= SD_DRIVER_TYPE_C;
446
447 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
448 host_drv_type |= SD_DRIVER_TYPE_D;
449
450 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
451 card_drv_type |= SD_DRIVER_TYPE_A;
452
453 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
454 card_drv_type |= SD_DRIVER_TYPE_C;
455
456 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
457 card_drv_type |= SD_DRIVER_TYPE_D;
458
459 /*
460 * The drive strength that the hardware can support
461 * depends on the board design. Pass the appropriate
462 * information and let the hardware specific code
463 * return what is possible given the options
464 */
465 drive_strength = card->host->ops->select_drive_strength(
466 card->sw_caps.uhs_max_dtr,
467 host_drv_type, card_drv_type);
468
469 /* if error just use default for drive strength B */
470 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
471 &card_strength);
472 if (err)
473 return;
474
475 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
476 card_strength |= host_drive_to_sdio_drive(drive_strength);
477
478 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
479 card_strength, NULL);
480
481 /* if error default to drive strength B */
482 if (!err)
483 mmc_set_driver_type(card->host, drive_strength);
484}
485
486
487static int sdio_set_bus_speed_mode(struct mmc_card *card)
488{
489 unsigned int bus_speed, timing;
490 int err;
491 unsigned char speed;
492
493 /*
494 * If the host doesn't support any of the UHS-I modes, fallback on
495 * default speed.
496 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +0530497 if (!mmc_host_uhs(card->host))
Philip Rakitya303c532011-11-14 19:14:38 -0800498 return 0;
499
500 bus_speed = SDIO_SPEED_SDR12;
501 timing = MMC_TIMING_UHS_SDR12;
502 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
503 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
504 bus_speed = SDIO_SPEED_SDR104;
505 timing = MMC_TIMING_UHS_SDR104;
506 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530507 card->sd_bus_speed = UHS_SDR104_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800508 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
509 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
510 bus_speed = SDIO_SPEED_DDR50;
511 timing = MMC_TIMING_UHS_DDR50;
512 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530513 card->sd_bus_speed = UHS_DDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800514 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
515 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
516 SD_MODE_UHS_SDR50)) {
517 bus_speed = SDIO_SPEED_SDR50;
518 timing = MMC_TIMING_UHS_SDR50;
519 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530520 card->sd_bus_speed = UHS_SDR50_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800521 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
522 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
523 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
524 bus_speed = SDIO_SPEED_SDR25;
525 timing = MMC_TIMING_UHS_SDR25;
526 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530527 card->sd_bus_speed = UHS_SDR25_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800528 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
529 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
530 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
531 SD_MODE_UHS_SDR12)) {
532 bus_speed = SDIO_SPEED_SDR12;
533 timing = MMC_TIMING_UHS_SDR12;
534 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
Subhash Jadavani177bf0c2012-10-06 12:27:19 +0530535 card->sd_bus_speed = UHS_SDR12_BUS_SPEED;
Philip Rakitya303c532011-11-14 19:14:38 -0800536 }
537
538 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
539 if (err)
540 return err;
541
542 speed &= ~SDIO_SPEED_BSS_MASK;
543 speed |= bus_speed;
544 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
545 if (err)
546 return err;
547
548 if (bus_speed) {
549 mmc_set_timing(card->host, timing);
550 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
551 }
552
553 return 0;
554}
555
556/*
557 * UHS-I specific initialization procedure
558 */
559static int mmc_sdio_init_uhs_card(struct mmc_card *card)
560{
561 int err;
562
563 if (!card->scr.sda_spec3)
564 return 0;
565
566 /*
567 * Switch to wider bus (if supported).
568 */
569 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
570 err = sdio_enable_4bit_bus(card);
571 if (err > 0) {
572 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
573 err = 0;
574 }
575 }
576
577 /* Set the driver strength for the card */
578 sdio_select_driver_type(card);
579
580 /* Set bus speed mode of the card */
581 err = sdio_set_bus_speed_mode(card);
582 if (err)
583 goto out;
584
585 /* Initialize and start re-tuning timer */
586 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
Girish K Sa4924c72012-01-11 14:04:52 -0500587 err = card->host->ops->execute_tuning(card->host,
588 MMC_SEND_TUNING_BLOCK);
Philip Rakitya303c532011-11-14 19:14:38 -0800589
590out:
591
592 return err;
593}
594
Pierre Ossmand16f5772008-08-31 17:22:46 +0200595/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700596 * Handle the detection and initialisation of a card.
597 *
598 * In the case of a resume, "oldcard" will contain the card
599 * we're trying to reinitialise.
600 */
601static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf2010-03-05 13:43:33 -0800602 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700603{
604 struct mmc_card *card;
605 int err;
606
607 BUG_ON(!host);
608 WARN_ON(!host->claimed);
609
610 /*
611 * Inform the card of the voltage
612 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800613 if (!powered_resume) {
Ulf Hanssone7747472012-03-01 13:18:05 +0100614 /* The initialization should be done at 3.3 V I/O voltage. */
615 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
616
Chris Ball3bca4cf2010-03-05 13:43:33 -0800617 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
618 if (err)
619 goto err;
620 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700621
622 /*
623 * For SPI, enable CRC as appropriate.
624 */
625 if (mmc_host_is_spi(host)) {
626 err = mmc_spi_set_crc(host, use_spi_crc);
627 if (err)
628 goto err;
629 }
630
631 /*
632 * Allocate card structure.
633 */
634 card = mmc_alloc_card(host, NULL);
635 if (IS_ERR(card)) {
636 err = PTR_ERR(card);
637 goto err;
638 }
639
Arindam Nathd6d50a12011-05-05 12:18:59 +0530640 if ((ocr & R4_MEMORY_PRESENT) &&
641 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700642 card->type = MMC_TYPE_SD_COMBO;
643
644 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
645 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
646 mmc_remove_card(card);
647 return -ENOENT;
648 }
649 } else {
650 card->type = MMC_TYPE_SDIO;
651
652 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
653 mmc_remove_card(card);
654 return -ENOENT;
655 }
656 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700657
658 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200659 * Call the optional HC's init_card function to handle quirks.
660 */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530661 if (host->ops->init_card) {
662 mmc_host_clk_hold(host);
Daniel Mack3fcb0272010-04-01 10:03:25 +0200663 host->ops->init_card(host, card);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530664 mmc_host_clk_release(host);
665 }
Daniel Mack3fcb0272010-04-01 10:03:25 +0200666
667 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800668 * If the host and card support UHS-I mode request the card
669 * to switch to 1.8V signaling level. No 1.8v signalling if
670 * UHS mode is not enabled to maintain compatibilty and some
671 * systems that claim 1.8v signalling in fact do not support
672 * it.
673 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +0530674 if ((ocr & R4_18V_PRESENT) && mmc_host_uhs(host)) {
Philip Rakitya303c532011-11-14 19:14:38 -0800675 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
676 true);
677 if (err) {
678 ocr &= ~R4_18V_PRESENT;
679 host->ocr &= ~R4_18V_PRESENT;
680 }
681 err = 0;
682 } else {
683 ocr &= ~R4_18V_PRESENT;
684 host->ocr &= ~R4_18V_PRESENT;
685 }
686
687 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700688 * For native busses: set card RCA and quit open drain mode.
689 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800690 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700691 err = mmc_send_relative_addr(host, &card->rca);
692 if (err)
693 goto remove;
694
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100695 /*
696 * Update oldcard with the new RCA received from the SDIO
697 * device -- we're doing this so that it's updated in the
698 * "card" struct when oldcard overwrites that later.
699 */
700 if (oldcard)
701 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700702 }
703
704 /*
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700705 * Read CSD, before selecting the card
706 */
707 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
708 err = mmc_sd_get_csd(host, card);
709 if (err)
710 return err;
711
712 mmc_decode_cid(card);
713 }
714
715 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700716 * Select card, as all following commands rely on that.
717 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800718 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700719 err = mmc_select_card(card);
720 if (err)
721 goto remove;
722 }
723
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700724 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
725 /*
726 * This is non-standard SDIO device, meaning it doesn't
727 * have any CIA (Common I/O area) registers present.
728 * It's host's responsibility to fill cccr and cis
729 * structures in init_card().
730 */
731 mmc_set_clock(host, card->cis.max_dtr);
732
733 if (card->cccr.high_speed) {
734 mmc_card_set_highspeed(card);
735 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
736 }
737
738 goto finish;
739 }
740
San Mehate01587a2008-04-14 15:22:49 -0700741#ifdef CONFIG_MMC_EMBEDDED_SDIO
742 if (host->embedded_sdio_data.cccr)
743 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
744 else {
745#endif
746 /*
747 * Read the common registers.
748 */
749 err = sdio_read_cccr(card, ocr);
750 if (err)
751 goto remove;
752#ifdef CONFIG_MMC_EMBEDDED_SDIO
753 }
754#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700755
San Mehate01587a2008-04-14 15:22:49 -0700756#ifdef CONFIG_MMC_EMBEDDED_SDIO
757 if (host->embedded_sdio_data.cis)
758 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
759 else {
760#endif
761 /*
762 * Read the common CIS tuples.
763 */
764 err = sdio_read_common_cis(card);
765 if (err)
766 goto remove;
767#ifdef CONFIG_MMC_EMBEDDED_SDIO
768 }
769#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700770
771 if (oldcard) {
772 int same = (card->cis.vendor == oldcard->cis.vendor &&
773 card->cis.device == oldcard->cis.device);
774 mmc_remove_card(card);
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700775 if (!same)
776 return -ENOENT;
777
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700778 card = oldcard;
779 }
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500780 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700781
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700782 if (card->type == MMC_TYPE_SD_COMBO) {
783 err = mmc_sd_setup_card(host, card, oldcard != NULL);
784 /* handle as SDIO-only card if memory init failed */
785 if (err) {
786 mmc_go_idle(host);
787 if (mmc_host_is_spi(host))
788 /* should not fail, as it worked previously */
789 mmc_spi_set_crc(host, use_spi_crc);
790 card->type = MMC_TYPE_SDIO;
791 } else
792 card->dev.type = &sd_type;
793 }
794
795 /*
796 * If needed, disconnect card detection pull-up resistor.
797 */
798 err = sdio_disable_cd(card);
799 if (err)
800 goto remove;
801
Philip Rakitya303c532011-11-14 19:14:38 -0800802 /* Initialization sequence for UHS-I cards */
803 /* Only if card supports 1.8v and UHS signaling */
804 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
805 err = mmc_sdio_init_uhs_card(card);
806 if (err)
807 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700808
Philip Rakitya303c532011-11-14 19:14:38 -0800809 /* Card is an ultra-high-speed card */
810 mmc_card_set_uhs(card);
811 } else {
812 /*
813 * Switch to high-speed (if supported).
814 */
815 err = sdio_enable_hs(card);
816 if (err > 0)
817 mmc_sd_go_highspeed(card);
818 else if (err)
819 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700820
Philip Rakitya303c532011-11-14 19:14:38 -0800821 /*
822 * Change to the card's maximum speed.
823 */
824 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700825
Philip Rakitya303c532011-11-14 19:14:38 -0800826 /*
827 * Switch to wider bus (if supported).
828 */
829 err = sdio_enable_4bit_bus(card);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700830 if (err > 0) {
831 if (card->host->caps & MMC_CAP_8_BIT_DATA)
832 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_8);
833 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
834 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
835 } else if (err)
Philip Rakitya303c532011-11-14 19:14:38 -0800836 goto remove;
837 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700838finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700839 if (!oldcard)
840 host->card = card;
841 return 0;
842
843remove:
844 if (!oldcard)
845 mmc_remove_card(card);
846
847err:
848 return err;
849}
850
851/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200852 * Host is being removed. Free up the current card.
853 */
854static void mmc_sdio_remove(struct mmc_host *host)
855{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200856 int i;
857
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200858 BUG_ON(!host);
859 BUG_ON(!host->card);
860
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200861 for (i = 0;i < host->card->sdio_funcs;i++) {
862 if (host->card->sdio_func[i]) {
863 sdio_remove_func(host->card->sdio_func[i]);
864 host->card->sdio_func[i] = NULL;
865 }
866 }
867
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200868 mmc_remove_card(host->card);
869 host->card = NULL;
870}
871
872/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200873 * Card detection - card is alive.
874 */
875static int mmc_sdio_alive(struct mmc_host *host)
876{
877 return mmc_select_card(host->card);
878}
879
880/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200881 * Card detection callback from host.
882 */
883static void mmc_sdio_detect(struct mmc_host *host)
884{
885 int err;
886
887 BUG_ON(!host);
888 BUG_ON(!host->card);
889
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200890 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200891 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
892 err = pm_runtime_get_sync(&host->card->dev);
893 if (err < 0)
894 goto out;
895 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200896
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200897 mmc_claim_host(host);
898
899 /*
900 * Just check if our card has been removed.
901 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200902 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200903
904 mmc_release_host(host);
905
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200906 /*
907 * Tell PM core it's OK to power off the card now.
908 *
909 * The _sync variant is used in order to ensure that the card
910 * is left powered off in case an error occurred, and the card
911 * is going to be removed.
912 *
913 * Since there is no specific reason to believe a new user
914 * is about to show up at this point, the _sync variant is
915 * desirable anyway.
916 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200917 if (host->caps & MMC_CAP_POWER_OFF_CARD)
918 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200919
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200920out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200921 if (err) {
922 mmc_sdio_remove(host);
923
924 mmc_claim_host(host);
925 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400926 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200927 mmc_release_host(host);
928 }
929}
930
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700931/*
932 * SDIO suspend. We need to suspend all functions separately.
933 * Therefore all registered functions must have drivers with suspend
934 * and resume methods. Failing that we simply remove the whole card.
935 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700936static int mmc_sdio_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700937{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700938 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700939
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700940 for (i = 0; i < host->card->sdio_funcs; i++) {
941 struct sdio_func *func = host->card->sdio_func[i];
942 if (func && sdio_func_present(func) && func->dev.driver) {
943 const struct dev_pm_ops *pmops = func->dev.driver->pm;
944 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700945 /* force removal of entire card in that case */
946 err = -ENOSYS;
947 } else
948 err = pmops->suspend(&func->dev);
949 if (err)
950 break;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700951 }
952 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700953 while (err && --i >= 0) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700954 struct sdio_func *func = host->card->sdio_func[i];
955 if (func && sdio_func_present(func) && func->dev.driver) {
956 const struct dev_pm_ops *pmops = func->dev.driver->pm;
957 pmops->resume(&func->dev);
958 }
959 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700960
Ohad Ben-Cohen6b93d01f2011-04-05 17:43:21 +0300961 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Daniel Drake6b5eda32010-03-05 13:43:34 -0800962 mmc_claim_host(host);
963 sdio_disable_wide(host->card);
964 mmc_release_host(host);
965 }
966
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700967 return err;
968}
969
970static int mmc_sdio_resume(struct mmc_host *host)
971{
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200972 int i, err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700973
974 BUG_ON(!host);
975 BUG_ON(!host->card);
976
977 /* Basic card reinitialization. */
978 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200979
980 /* No need to reinitialize powered-resumed nonremovable cards */
Subhash Jadavani26793072012-11-27 12:28:13 +0530981 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
982 sdio_reset(host);
983 mmc_go_idle(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200984 err = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +0300985 mmc_card_keep_power(host));
Subhash Jadavani26793072012-11-27 12:28:13 +0530986 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200987 /* We may have switched to 1-bit mode during suspend */
988 err = sdio_enable_4bit_bus(host->card);
989 if (err > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990 if (host->caps & MMC_CAP_8_BIT_DATA)
991 mmc_set_bus_width(host, MMC_BUS_WIDTH_8);
992 else if (host->caps & MMC_CAP_4_BIT_DATA)
993 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200994 err = 0;
995 }
996 }
997
Nicolas Pitre40216842010-03-05 13:43:34 -0800998 if (!err && host->sdio_irqs)
Nicolas Pitrefed14d92012-04-16 19:16:54 -0400999 wake_up_process(host->sdio_irq_thread);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001000 mmc_release_host(host);
1001
1002 /*
1003 * If the card looked to be the same as before suspending, then
1004 * we proceed to resume all card functions. If one of them returns
1005 * an error then we simply return that error to the core and the
1006 * card will be redetected as new. It is the responsibility of
1007 * the function driver to perform further tests with the extra
1008 * knowledge it has of the card to confirm the card is indeed the
1009 * same as before suspending (same MAC address for network cards,
1010 * etc.) and return an error otherwise.
1011 */
1012 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
1013 struct sdio_func *func = host->card->sdio_func[i];
1014 if (func && sdio_func_present(func) && func->dev.driver) {
1015 const struct dev_pm_ops *pmops = func->dev.driver->pm;
1016 err = pmops->resume(&func->dev);
1017 }
1018 }
1019
1020 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001021}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001022
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001023static int mmc_sdio_power_restore(struct mmc_host *host)
1024{
1025 int ret;
Daniel Drakec6e633a2011-06-25 19:20:11 +01001026 u32 ocr;
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001027
1028 BUG_ON(!host);
1029 BUG_ON(!host->card);
1030
1031 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001032
1033 /*
1034 * Reset the card by performing the same steps that are taken by
1035 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1036 *
1037 * sdio_reset() is technically not needed. Having just powered up the
1038 * hardware, it should already be in reset state. However, some
1039 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1040 * meaning that a reset is required when restoring power soon after
1041 * powering off. It is harmless in other cases.
1042 *
1043 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1044 * is not necessary for non-removable cards. However, it is required
1045 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1046 * harmless in other situations.
1047 *
1048 * With these steps taken, mmc_select_voltage() is also required to
1049 * restore the correct voltage setting of the card.
1050 */
Ulf Hanssone7747472012-03-01 13:18:05 +01001051
1052 /* The initialization should be done at 3.3 V I/O voltage. */
1053 if (!mmc_card_keep_power(host))
1054 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
1055
Daniel Drakec6e633a2011-06-25 19:20:11 +01001056 sdio_reset(host);
1057 mmc_go_idle(host);
1058 mmc_send_if_cond(host, host->ocr_avail);
1059
1060 ret = mmc_send_io_op_cond(host, 0, &ocr);
1061 if (ret)
1062 goto out;
1063
1064 if (host->ocr_avail_sdio)
1065 host->ocr_avail = host->ocr_avail_sdio;
1066
1067 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1068 if (!host->ocr) {
1069 ret = -EINVAL;
1070 goto out;
1071 }
1072
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +05301073 if (mmc_host_uhs(host))
1074 /* to query card if 1.8V signalling is supported */
1075 host->ocr |= R4_18V_PRESENT;
1076
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001077 ret = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001078 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001079 if (!ret && host->sdio_irqs)
1080 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001081
1082out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001083 mmc_release_host(host);
1084
1085 return ret;
1086}
1087
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001088static const struct mmc_bus_ops mmc_sdio_ops = {
1089 .remove = mmc_sdio_remove,
1090 .detect = mmc_sdio_detect,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001091 .suspend = mmc_sdio_suspend,
1092 .resume = mmc_sdio_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001093 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001094 .alive = mmc_sdio_alive,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001095};
1096
1097
1098/*
1099 * Starting point for SDIO card init.
1100 */
Andy Ross807e8e42011-01-03 10:36:56 -08001101int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001102{
Andy Ross807e8e42011-01-03 10:36:56 -08001103 int err, i, funcs;
1104 u32 ocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001105 struct mmc_card *card;
1106
1107 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001108 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001109
Andy Ross807e8e42011-01-03 10:36:56 -08001110 err = mmc_send_io_op_cond(host, 0, &ocr);
1111 if (err)
1112 return err;
1113
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001114 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001115 if (host->ocr_avail_sdio)
1116 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001117
1118 /*
1119 * Sanity check the voltages that the card claims to
1120 * support.
1121 */
1122 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301123 pr_warning("%s: card claims to support voltages "
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001124 "below the defined range. These will be ignored.\n",
1125 mmc_hostname(host));
1126 ocr &= ~0x7F;
1127 }
1128
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001129 host->ocr = mmc_select_voltage(host, ocr);
1130
1131 /*
1132 * Can we support the voltage(s) of the card(s)?
1133 */
1134 if (!host->ocr) {
1135 err = -EINVAL;
1136 goto err;
1137 }
1138
1139 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001140 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001141 */
Sujit Reddy Thumma25248b62012-08-03 11:24:04 +05301142 if (mmc_host_uhs(host))
1143 /* to query card if 1.8V signalling is supported */
1144 host->ocr |= R4_18V_PRESENT;
1145
Chris Ball3bca4cf2010-03-05 13:43:33 -08001146 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
Philip Rakitya303c532011-11-14 19:14:38 -08001147 if (err) {
1148 if (err == -EAGAIN) {
1149 /*
1150 * Retry initialization with S18R set to 0.
1151 */
1152 host->ocr &= ~R4_18V_PRESENT;
1153 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1154 }
1155 if (err)
1156 goto err;
1157 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001158 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001159
1160 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001161 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001162 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001163 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1164 /*
1165 * Let runtime PM core know our card is active
1166 */
1167 err = pm_runtime_set_active(&card->dev);
1168 if (err)
1169 goto remove;
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001170
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001171 /*
1172 * Enable runtime PM for this card
1173 */
1174 pm_runtime_enable(&card->dev);
1175 }
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001176
1177 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001178 * The number of functions on the card is encoded inside
1179 * the ocr.
1180 */
Matt Fleminge8812792009-12-17 15:27:18 -08001181 funcs = (ocr & 0x70000000) >> 28;
1182 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001183
San Mehate01587a2008-04-14 15:22:49 -07001184#ifdef CONFIG_MMC_EMBEDDED_SDIO
1185 if (host->embedded_sdio_data.funcs)
1186 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1187#endif
1188
Pierre Ossman4ff64712007-07-30 18:23:53 +02001189 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001190 * Initialize (but don't add) all present functions.
1191 */
Matt Fleminge8812792009-12-17 15:27:18 -08001192 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
San Mehate01587a2008-04-14 15:22:49 -07001193#ifdef CONFIG_MMC_EMBEDDED_SDIO
1194 if (host->embedded_sdio_data.funcs) {
1195 struct sdio_func *tmp;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001196
San Mehate01587a2008-04-14 15:22:49 -07001197 tmp = sdio_alloc_func(host->card);
1198 if (IS_ERR(tmp))
1199 goto remove;
1200 tmp->num = (i + 1);
1201 card->sdio_func[i] = tmp;
1202 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1203 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1204 tmp->vendor = card->cis.vendor;
1205 tmp->device = card->cis.device;
1206 } else {
1207#endif
1208 err = sdio_init_func(host->card, i + 1);
1209 if (err)
1210 goto remove;
1211#ifdef CONFIG_MMC_EMBEDDED_SDIO
1212 }
1213#endif
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001214 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001215 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001216 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001217 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1218 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001219 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001220
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001221 /*
1222 * First add the card to the driver model...
1223 */
Andy Ross807e8e42011-01-03 10:36:56 -08001224 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001225 err = mmc_add_card(host->card);
1226 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001227 goto remove_added;
1228
1229 /*
1230 * ...then the SDIO functions.
1231 */
1232 for (i = 0;i < funcs;i++) {
1233 err = sdio_add_func(host->card->sdio_func[i]);
1234 if (err)
1235 goto remove_added;
1236 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001237
Dmitry Shmidt34497912011-03-03 17:40:10 -05001238 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001239 return 0;
1240
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001241
1242remove_added:
1243 /* Remove without lock if the device has been added. */
1244 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001245 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001246remove:
1247 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001248 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001249 if (host->card)
1250 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001251 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001252err:
1253 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001254
Girish K Sa3c76eb2011-10-11 11:44:09 +05301255 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001256 mmc_hostname(host), err);
1257
1258 return err;
1259}
1260
San Mehat35fe42e2008-05-15 09:15:37 -07001261int sdio_reset_comm(struct mmc_card *card)
1262{
Subhash Jadavani06673c52012-11-26 22:20:56 +05301263 return mmc_power_restore_host(card->host);
San Mehat35fe42e2008-05-15 09:15:37 -07001264}
1265EXPORT_SYMBOL(sdio_reset_comm);