blob: fd18560e40b47c180ce2fdd93f969fda25afad16 [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
165 if (card->host->caps &
166 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
167 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
168 MMC_CAP_UHS_DDR50)) {
169 if (data & SDIO_UHS_DDR50)
170 card->sw_caps.sd3_bus_mode
171 |= SD_MODE_UHS_DDR50;
172
173 if (data & SDIO_UHS_SDR50)
174 card->sw_caps.sd3_bus_mode
175 |= SD_MODE_UHS_SDR50;
176
177 if (data & SDIO_UHS_SDR104)
178 card->sw_caps.sd3_bus_mode
179 |= SD_MODE_UHS_SDR104;
180 }
181
182 ret = mmc_io_rw_direct(card, 0, 0,
183 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
184 if (ret)
185 goto out;
186
187 if (data & SDIO_DRIVE_SDTA)
188 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
189 if (data & SDIO_DRIVE_SDTC)
190 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
191 if (data & SDIO_DRIVE_SDTD)
192 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
193 }
194
195 /* if no uhs mode ensure we check for high speed */
196 if (!card->sw_caps.sd3_bus_mode) {
197 if (speed & SDIO_SPEED_SHS) {
198 card->cccr.high_speed = 1;
199 card->sw_caps.hs_max_dtr = 50000000;
200 } else {
201 card->cccr.high_speed = 0;
202 card->sw_caps.hs_max_dtr = 25000000;
203 }
204 }
Pierre Ossman35c66c12007-06-11 20:25:43 +0200205 }
206
207out:
208 return ret;
209}
210
Pierre Ossman4ff64712007-07-30 18:23:53 +0200211static int sdio_enable_wide(struct mmc_card *card)
212{
213 int ret;
214 u8 ctrl;
215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700216 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Pierre Ossman4ff64712007-07-30 18:23:53 +0200217 return 0;
218
219 if (card->cccr.low_speed && !card->cccr.wide_bus)
220 return 0;
221
222 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
223 if (ret)
224 return ret;
225
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700226 if (card->host->caps & MMC_CAP_8_BIT_DATA)
227 ctrl |= SDIO_BUS_WIDTH_8BIT;
228 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
229 ctrl |= SDIO_BUS_WIDTH_4BIT;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200230
231 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
232 if (ret)
233 return ret;
234
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700235 return 1;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200236}
237
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200238/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700239 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
240 * of the card. This may be required on certain setups of boards,
241 * controllers and embedded sdio device which do not need the card's
242 * pull-up. As a result, card detection is disabled and power is saved.
243 */
244static int sdio_disable_cd(struct mmc_card *card)
245{
246 int ret;
247 u8 ctrl;
248
Ohad Ben-Cohen2059a022011-04-05 18:02:25 +0300249 if (!mmc_card_disable_cd(card))
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700250 return 0;
251
252 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
253 if (ret)
254 return ret;
255
256 ctrl |= SDIO_BUS_CD_DISABLE;
257
258 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
259}
260
261/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800262 * Devices that remain active during a system suspend are
263 * put back into 1-bit mode.
264 */
265static int sdio_disable_wide(struct mmc_card *card)
266{
267 int ret;
268 u8 ctrl;
269
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700270 if (!(card->host->caps & (MMC_CAP_4_BIT_DATA | MMC_CAP_8_BIT_DATA)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800271 return 0;
272
273 if (card->cccr.low_speed && !card->cccr.wide_bus)
274 return 0;
275
276 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
277 if (ret)
278 return ret;
279
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700280 if (!(ctrl & (SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT)))
Daniel Drake6b5eda32010-03-05 13:43:34 -0800281 return 0;
282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700283 ctrl &= ~(SDIO_BUS_WIDTH_4BIT | SDIO_BUS_WIDTH_8BIT);
Daniel Drake6b5eda32010-03-05 13:43:34 -0800284 ctrl |= SDIO_BUS_ASYNC_INT;
285
286 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
287 if (ret)
288 return ret;
289
290 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
291
292 return 0;
293}
294
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700295
296static int sdio_enable_4bit_bus(struct mmc_card *card)
297{
298 int err;
299
300 if (card->type == MMC_TYPE_SDIO)
301 return sdio_enable_wide(card);
302
303 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
304 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
305 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
306 if (err)
307 return err;
308 } else
309 return 0;
310
311 err = sdio_enable_wide(card);
312 if (err <= 0)
313 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
314
315 return err;
316}
317
318
Daniel Drake6b5eda32010-03-05 13:43:34 -0800319/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200320 * Test if the card supports high-speed mode and, if so, switch to it.
321 */
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700322static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
Pierre Ossmand16f5772008-08-31 17:22:46 +0200323{
324 int ret;
325 u8 speed;
326
327 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
328 return 0;
329
330 if (!card->cccr.high_speed)
331 return 0;
332
333 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
334 if (ret)
335 return ret;
336
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700337 if (enable)
338 speed |= SDIO_SPEED_EHS;
339 else
340 speed &= ~SDIO_SPEED_EHS;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200341
342 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
343 if (ret)
344 return ret;
345
Michal Miroslaw71578a12010-08-10 18:01:40 -0700346 return 1;
347}
Pierre Ossmand16f5772008-08-31 17:22:46 +0200348
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700349/*
350 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
351 */
352static int sdio_enable_hs(struct mmc_card *card)
353{
354 int ret;
355
356 ret = mmc_sdio_switch_hs(card, true);
357 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
358 return ret;
359
360 ret = mmc_sd_switch_hs(card);
361 if (ret <= 0)
362 mmc_sdio_switch_hs(card, false);
363
364 return ret;
365}
366
Michal Miroslaw71578a12010-08-10 18:01:40 -0700367static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
368{
369 unsigned max_dtr;
370
371 if (mmc_card_highspeed(card)) {
372 /*
373 * The SDIO specification doesn't mention how
374 * the CIS transfer speed register relates to
375 * high-speed, but it seems that 50 MHz is
376 * mandatory.
377 */
378 max_dtr = 50000000;
379 } else {
380 max_dtr = card->cis.max_dtr;
381 }
382
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700383 if (card->type == MMC_TYPE_SD_COMBO)
384 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
385
Michal Miroslaw71578a12010-08-10 18:01:40 -0700386 return max_dtr;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200387}
388
Philip Rakitya303c532011-11-14 19:14:38 -0800389static unsigned char host_drive_to_sdio_drive(int host_strength)
390{
391 switch (host_strength) {
392 case MMC_SET_DRIVER_TYPE_A:
393 return SDIO_DTSx_SET_TYPE_A;
394 case MMC_SET_DRIVER_TYPE_B:
395 return SDIO_DTSx_SET_TYPE_B;
396 case MMC_SET_DRIVER_TYPE_C:
397 return SDIO_DTSx_SET_TYPE_C;
398 case MMC_SET_DRIVER_TYPE_D:
399 return SDIO_DTSx_SET_TYPE_D;
400 default:
401 return SDIO_DTSx_SET_TYPE_B;
402 }
403}
404
405static void sdio_select_driver_type(struct mmc_card *card)
406{
407 int host_drv_type = SD_DRIVER_TYPE_B;
408 int card_drv_type = SD_DRIVER_TYPE_B;
409 int drive_strength;
410 unsigned char card_strength;
411 int err;
412
413 /*
414 * If the host doesn't support any of the Driver Types A,C or D,
415 * or there is no board specific handler then default Driver
416 * Type B is used.
417 */
418 if (!(card->host->caps &
419 (MMC_CAP_DRIVER_TYPE_A |
420 MMC_CAP_DRIVER_TYPE_C |
421 MMC_CAP_DRIVER_TYPE_D)))
422 return;
423
424 if (!card->host->ops->select_drive_strength)
425 return;
426
427 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
428 host_drv_type |= SD_DRIVER_TYPE_A;
429
430 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
431 host_drv_type |= SD_DRIVER_TYPE_C;
432
433 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
434 host_drv_type |= SD_DRIVER_TYPE_D;
435
436 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
437 card_drv_type |= SD_DRIVER_TYPE_A;
438
439 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
440 card_drv_type |= SD_DRIVER_TYPE_C;
441
442 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
443 card_drv_type |= SD_DRIVER_TYPE_D;
444
445 /*
446 * The drive strength that the hardware can support
447 * depends on the board design. Pass the appropriate
448 * information and let the hardware specific code
449 * return what is possible given the options
450 */
451 drive_strength = card->host->ops->select_drive_strength(
452 card->sw_caps.uhs_max_dtr,
453 host_drv_type, card_drv_type);
454
455 /* if error just use default for drive strength B */
456 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
457 &card_strength);
458 if (err)
459 return;
460
461 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
462 card_strength |= host_drive_to_sdio_drive(drive_strength);
463
464 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
465 card_strength, NULL);
466
467 /* if error default to drive strength B */
468 if (!err)
469 mmc_set_driver_type(card->host, drive_strength);
470}
471
472
473static int sdio_set_bus_speed_mode(struct mmc_card *card)
474{
475 unsigned int bus_speed, timing;
476 int err;
477 unsigned char speed;
478
479 /*
480 * If the host doesn't support any of the UHS-I modes, fallback on
481 * default speed.
482 */
483 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
484 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
485 return 0;
486
487 bus_speed = SDIO_SPEED_SDR12;
488 timing = MMC_TIMING_UHS_SDR12;
489 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
490 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
491 bus_speed = SDIO_SPEED_SDR104;
492 timing = MMC_TIMING_UHS_SDR104;
493 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
494 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
495 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
496 bus_speed = SDIO_SPEED_DDR50;
497 timing = MMC_TIMING_UHS_DDR50;
498 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
499 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
500 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
501 SD_MODE_UHS_SDR50)) {
502 bus_speed = SDIO_SPEED_SDR50;
503 timing = MMC_TIMING_UHS_SDR50;
504 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
505 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
506 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
507 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
508 bus_speed = SDIO_SPEED_SDR25;
509 timing = MMC_TIMING_UHS_SDR25;
510 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
511 } 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;
518 }
519
520 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
521 if (err)
522 return err;
523
524 speed &= ~SDIO_SPEED_BSS_MASK;
525 speed |= bus_speed;
526 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
527 if (err)
528 return err;
529
530 if (bus_speed) {
531 mmc_set_timing(card->host, timing);
532 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
533 }
534
535 return 0;
536}
537
538/*
539 * UHS-I specific initialization procedure
540 */
541static int mmc_sdio_init_uhs_card(struct mmc_card *card)
542{
543 int err;
544
545 if (!card->scr.sda_spec3)
546 return 0;
547
548 /*
549 * Switch to wider bus (if supported).
550 */
551 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
552 err = sdio_enable_4bit_bus(card);
553 if (err > 0) {
554 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
555 err = 0;
556 }
557 }
558
559 /* Set the driver strength for the card */
560 sdio_select_driver_type(card);
561
562 /* Set bus speed mode of the card */
563 err = sdio_set_bus_speed_mode(card);
564 if (err)
565 goto out;
566
567 /* Initialize and start re-tuning timer */
568 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
Girish K Sa4924c72012-01-11 14:04:52 -0500569 err = card->host->ops->execute_tuning(card->host,
570 MMC_SEND_TUNING_BLOCK);
Philip Rakitya303c532011-11-14 19:14:38 -0800571
572out:
573
574 return err;
575}
576
Pierre Ossmand16f5772008-08-31 17:22:46 +0200577/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700578 * Handle the detection and initialisation of a card.
579 *
580 * In the case of a resume, "oldcard" will contain the card
581 * we're trying to reinitialise.
582 */
583static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf2010-03-05 13:43:33 -0800584 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700585{
586 struct mmc_card *card;
587 int err;
588
589 BUG_ON(!host);
590 WARN_ON(!host->claimed);
591
592 /*
593 * Inform the card of the voltage
594 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800595 if (!powered_resume) {
Ulf Hanssone7747472012-03-01 13:18:05 +0100596 /* The initialization should be done at 3.3 V I/O voltage. */
597 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
598
Chris Ball3bca4cf2010-03-05 13:43:33 -0800599 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
600 if (err)
601 goto err;
602 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700603
604 /*
605 * For SPI, enable CRC as appropriate.
606 */
607 if (mmc_host_is_spi(host)) {
608 err = mmc_spi_set_crc(host, use_spi_crc);
609 if (err)
610 goto err;
611 }
612
613 /*
614 * Allocate card structure.
615 */
616 card = mmc_alloc_card(host, NULL);
617 if (IS_ERR(card)) {
618 err = PTR_ERR(card);
619 goto err;
620 }
621
Arindam Nathd6d50a12011-05-05 12:18:59 +0530622 if ((ocr & R4_MEMORY_PRESENT) &&
623 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700624 card->type = MMC_TYPE_SD_COMBO;
625
626 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
627 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
628 mmc_remove_card(card);
629 return -ENOENT;
630 }
631 } else {
632 card->type = MMC_TYPE_SDIO;
633
634 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
635 mmc_remove_card(card);
636 return -ENOENT;
637 }
638 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700639
640 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200641 * Call the optional HC's init_card function to handle quirks.
642 */
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530643 if (host->ops->init_card) {
644 mmc_host_clk_hold(host);
Daniel Mack3fcb0272010-04-01 10:03:25 +0200645 host->ops->init_card(host, card);
Sujit Reddy Thummaabb96812011-12-08 14:53:14 +0530646 mmc_host_clk_release(host);
647 }
Daniel Mack3fcb0272010-04-01 10:03:25 +0200648
649 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800650 * If the host and card support UHS-I mode request the card
651 * to switch to 1.8V signaling level. No 1.8v signalling if
652 * UHS mode is not enabled to maintain compatibilty and some
653 * systems that claim 1.8v signalling in fact do not support
654 * it.
655 */
656 if ((ocr & R4_18V_PRESENT) &&
657 (host->caps &
658 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
659 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
660 MMC_CAP_UHS_DDR50))) {
661 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
662 true);
663 if (err) {
664 ocr &= ~R4_18V_PRESENT;
665 host->ocr &= ~R4_18V_PRESENT;
666 }
667 err = 0;
668 } else {
669 ocr &= ~R4_18V_PRESENT;
670 host->ocr &= ~R4_18V_PRESENT;
671 }
672
673 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700674 * For native busses: set card RCA and quit open drain mode.
675 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800676 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700677 err = mmc_send_relative_addr(host, &card->rca);
678 if (err)
679 goto remove;
680
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100681 /*
682 * Update oldcard with the new RCA received from the SDIO
683 * device -- we're doing this so that it's updated in the
684 * "card" struct when oldcard overwrites that later.
685 */
686 if (oldcard)
687 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700688 }
689
690 /*
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700691 * Read CSD, before selecting the card
692 */
693 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
694 err = mmc_sd_get_csd(host, card);
695 if (err)
696 return err;
697
698 mmc_decode_cid(card);
699 }
700
701 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700702 * Select card, as all following commands rely on that.
703 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800704 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700705 err = mmc_select_card(card);
706 if (err)
707 goto remove;
708 }
709
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700710 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
711 /*
712 * This is non-standard SDIO device, meaning it doesn't
713 * have any CIA (Common I/O area) registers present.
714 * It's host's responsibility to fill cccr and cis
715 * structures in init_card().
716 */
717 mmc_set_clock(host, card->cis.max_dtr);
718
719 if (card->cccr.high_speed) {
720 mmc_card_set_highspeed(card);
721 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
722 }
723
724 goto finish;
725 }
726
San Mehate01587a2008-04-14 15:22:49 -0700727#ifdef CONFIG_MMC_EMBEDDED_SDIO
728 if (host->embedded_sdio_data.cccr)
729 memcpy(&card->cccr, host->embedded_sdio_data.cccr, sizeof(struct sdio_cccr));
730 else {
731#endif
732 /*
733 * Read the common registers.
734 */
735 err = sdio_read_cccr(card, ocr);
736 if (err)
737 goto remove;
738#ifdef CONFIG_MMC_EMBEDDED_SDIO
739 }
740#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700741
San Mehate01587a2008-04-14 15:22:49 -0700742#ifdef CONFIG_MMC_EMBEDDED_SDIO
743 if (host->embedded_sdio_data.cis)
744 memcpy(&card->cis, host->embedded_sdio_data.cis, sizeof(struct sdio_cis));
745 else {
746#endif
747 /*
748 * Read the common CIS tuples.
749 */
750 err = sdio_read_common_cis(card);
751 if (err)
752 goto remove;
753#ifdef CONFIG_MMC_EMBEDDED_SDIO
754 }
755#endif
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700756
757 if (oldcard) {
758 int same = (card->cis.vendor == oldcard->cis.vendor &&
759 card->cis.device == oldcard->cis.device);
760 mmc_remove_card(card);
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700761 if (!same)
762 return -ENOENT;
763
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700764 card = oldcard;
765 }
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500766 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700767
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700768 if (card->type == MMC_TYPE_SD_COMBO) {
769 err = mmc_sd_setup_card(host, card, oldcard != NULL);
770 /* handle as SDIO-only card if memory init failed */
771 if (err) {
772 mmc_go_idle(host);
773 if (mmc_host_is_spi(host))
774 /* should not fail, as it worked previously */
775 mmc_spi_set_crc(host, use_spi_crc);
776 card->type = MMC_TYPE_SDIO;
777 } else
778 card->dev.type = &sd_type;
779 }
780
781 /*
782 * If needed, disconnect card detection pull-up resistor.
783 */
784 err = sdio_disable_cd(card);
785 if (err)
786 goto remove;
787
Philip Rakitya303c532011-11-14 19:14:38 -0800788 /* Initialization sequence for UHS-I cards */
789 /* Only if card supports 1.8v and UHS signaling */
790 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
791 err = mmc_sdio_init_uhs_card(card);
792 if (err)
793 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700794
Philip Rakitya303c532011-11-14 19:14:38 -0800795 /* Card is an ultra-high-speed card */
796 mmc_card_set_uhs(card);
797 } else {
798 /*
799 * Switch to high-speed (if supported).
800 */
801 err = sdio_enable_hs(card);
802 if (err > 0)
803 mmc_sd_go_highspeed(card);
804 else if (err)
805 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700806
Philip Rakitya303c532011-11-14 19:14:38 -0800807 /*
808 * Change to the card's maximum speed.
809 */
810 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700811
Philip Rakitya303c532011-11-14 19:14:38 -0800812 /*
813 * Switch to wider bus (if supported).
814 */
815 err = sdio_enable_4bit_bus(card);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700816 if (err > 0) {
817 if (card->host->caps & MMC_CAP_8_BIT_DATA)
818 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_8);
819 else if (card->host->caps & MMC_CAP_4_BIT_DATA)
820 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
821 } else if (err)
Philip Rakitya303c532011-11-14 19:14:38 -0800822 goto remove;
823 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700824finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700825 if (!oldcard)
826 host->card = card;
827 return 0;
828
829remove:
830 if (!oldcard)
831 mmc_remove_card(card);
832
833err:
834 return err;
835}
836
837/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200838 * Host is being removed. Free up the current card.
839 */
840static void mmc_sdio_remove(struct mmc_host *host)
841{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200842 int i;
843
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200844 BUG_ON(!host);
845 BUG_ON(!host->card);
846
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200847 for (i = 0;i < host->card->sdio_funcs;i++) {
848 if (host->card->sdio_func[i]) {
849 sdio_remove_func(host->card->sdio_func[i]);
850 host->card->sdio_func[i] = NULL;
851 }
852 }
853
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200854 mmc_remove_card(host->card);
855 host->card = NULL;
856}
857
858/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200859 * Card detection - card is alive.
860 */
861static int mmc_sdio_alive(struct mmc_host *host)
862{
863 return mmc_select_card(host->card);
864}
865
866/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200867 * Card detection callback from host.
868 */
869static void mmc_sdio_detect(struct mmc_host *host)
870{
871 int err;
872
873 BUG_ON(!host);
874 BUG_ON(!host->card);
875
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200876 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200877 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
878 err = pm_runtime_get_sync(&host->card->dev);
879 if (err < 0)
880 goto out;
881 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200882
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200883 mmc_claim_host(host);
884
885 /*
886 * Just check if our card has been removed.
887 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200888 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200889
890 mmc_release_host(host);
891
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200892 /*
893 * Tell PM core it's OK to power off the card now.
894 *
895 * The _sync variant is used in order to ensure that the card
896 * is left powered off in case an error occurred, and the card
897 * is going to be removed.
898 *
899 * Since there is no specific reason to believe a new user
900 * is about to show up at this point, the _sync variant is
901 * desirable anyway.
902 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200903 if (host->caps & MMC_CAP_POWER_OFF_CARD)
904 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200905
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200906out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200907 if (err) {
908 mmc_sdio_remove(host);
909
910 mmc_claim_host(host);
911 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400912 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200913 mmc_release_host(host);
914 }
915}
916
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700917/*
918 * SDIO suspend. We need to suspend all functions separately.
919 * Therefore all registered functions must have drivers with suspend
920 * and resume methods. Failing that we simply remove the whole card.
921 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700922static int mmc_sdio_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700923{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700924 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700925
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700926 for (i = 0; i < host->card->sdio_funcs; i++) {
927 struct sdio_func *func = host->card->sdio_func[i];
928 if (func && sdio_func_present(func) && func->dev.driver) {
929 const struct dev_pm_ops *pmops = func->dev.driver->pm;
930 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700931 /* force removal of entire card in that case */
932 err = -ENOSYS;
933 } else
934 err = pmops->suspend(&func->dev);
935 if (err)
936 break;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700937 }
938 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700939 while (err && --i >= 0) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700940 struct sdio_func *func = host->card->sdio_func[i];
941 if (func && sdio_func_present(func) && func->dev.driver) {
942 const struct dev_pm_ops *pmops = func->dev.driver->pm;
943 pmops->resume(&func->dev);
944 }
945 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700946
Ohad Ben-Cohen6b93d01f2011-04-05 17:43:21 +0300947 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Daniel Drake6b5eda32010-03-05 13:43:34 -0800948 mmc_claim_host(host);
949 sdio_disable_wide(host->card);
950 mmc_release_host(host);
951 }
952
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700953 return err;
954}
955
956static int mmc_sdio_resume(struct mmc_host *host)
957{
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200958 int i, err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700959
960 BUG_ON(!host);
961 BUG_ON(!host->card);
962
963 /* Basic card reinitialization. */
964 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200965
966 /* No need to reinitialize powered-resumed nonremovable cards */
Subhash Jadavani26793072012-11-27 12:28:13 +0530967 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) {
968 sdio_reset(host);
969 mmc_go_idle(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200970 err = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +0300971 mmc_card_keep_power(host));
Subhash Jadavani26793072012-11-27 12:28:13 +0530972 } else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200973 /* We may have switched to 1-bit mode during suspend */
974 err = sdio_enable_4bit_bus(host->card);
975 if (err > 0) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 if (host->caps & MMC_CAP_8_BIT_DATA)
977 mmc_set_bus_width(host, MMC_BUS_WIDTH_8);
978 else if (host->caps & MMC_CAP_4_BIT_DATA)
979 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200980 err = 0;
981 }
982 }
983
Nicolas Pitre40216842010-03-05 13:43:34 -0800984 if (!err && host->sdio_irqs)
Nicolas Pitrefed14d92012-04-16 19:16:54 -0400985 wake_up_process(host->sdio_irq_thread);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700986 mmc_release_host(host);
987
988 /*
989 * If the card looked to be the same as before suspending, then
990 * we proceed to resume all card functions. If one of them returns
991 * an error then we simply return that error to the core and the
992 * card will be redetected as new. It is the responsibility of
993 * the function driver to perform further tests with the extra
994 * knowledge it has of the card to confirm the card is indeed the
995 * same as before suspending (same MAC address for network cards,
996 * etc.) and return an error otherwise.
997 */
998 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
999 struct sdio_func *func = host->card->sdio_func[i];
1000 if (func && sdio_func_present(func) && func->dev.driver) {
1001 const struct dev_pm_ops *pmops = func->dev.driver->pm;
1002 err = pmops->resume(&func->dev);
1003 }
1004 }
1005
1006 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001007}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001008
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001009static int mmc_sdio_power_restore(struct mmc_host *host)
1010{
1011 int ret;
Daniel Drakec6e633a2011-06-25 19:20:11 +01001012 u32 ocr;
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001013
1014 BUG_ON(!host);
1015 BUG_ON(!host->card);
1016
1017 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001018
1019 /*
1020 * Reset the card by performing the same steps that are taken by
1021 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
1022 *
1023 * sdio_reset() is technically not needed. Having just powered up the
1024 * hardware, it should already be in reset state. However, some
1025 * platforms (such as SD8686 on OLPC) do not instantly cut power,
1026 * meaning that a reset is required when restoring power soon after
1027 * powering off. It is harmless in other cases.
1028 *
1029 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
1030 * is not necessary for non-removable cards. However, it is required
1031 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
1032 * harmless in other situations.
1033 *
1034 * With these steps taken, mmc_select_voltage() is also required to
1035 * restore the correct voltage setting of the card.
1036 */
Ulf Hanssone7747472012-03-01 13:18:05 +01001037
1038 /* The initialization should be done at 3.3 V I/O voltage. */
1039 if (!mmc_card_keep_power(host))
1040 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
1041
Daniel Drakec6e633a2011-06-25 19:20:11 +01001042 sdio_reset(host);
1043 mmc_go_idle(host);
1044 mmc_send_if_cond(host, host->ocr_avail);
1045
1046 ret = mmc_send_io_op_cond(host, 0, &ocr);
1047 if (ret)
1048 goto out;
1049
1050 if (host->ocr_avail_sdio)
1051 host->ocr_avail = host->ocr_avail_sdio;
1052
1053 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1054 if (!host->ocr) {
1055 ret = -EINVAL;
1056 goto out;
1057 }
1058
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001059 ret = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001060 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001061 if (!ret && host->sdio_irqs)
1062 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001063
1064out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001065 mmc_release_host(host);
1066
1067 return ret;
1068}
1069
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001070static const struct mmc_bus_ops mmc_sdio_ops = {
1071 .remove = mmc_sdio_remove,
1072 .detect = mmc_sdio_detect,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001073 .suspend = mmc_sdio_suspend,
1074 .resume = mmc_sdio_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001075 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001076 .alive = mmc_sdio_alive,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001077};
1078
1079
1080/*
1081 * Starting point for SDIO card init.
1082 */
Andy Ross807e8e42011-01-03 10:36:56 -08001083int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001084{
Andy Ross807e8e42011-01-03 10:36:56 -08001085 int err, i, funcs;
1086 u32 ocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001087 struct mmc_card *card;
1088
1089 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001090 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001091
Andy Ross807e8e42011-01-03 10:36:56 -08001092 err = mmc_send_io_op_cond(host, 0, &ocr);
1093 if (err)
1094 return err;
1095
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001096 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001097 if (host->ocr_avail_sdio)
1098 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001099
1100 /*
1101 * Sanity check the voltages that the card claims to
1102 * support.
1103 */
1104 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301105 pr_warning("%s: card claims to support voltages "
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001106 "below the defined range. These will be ignored.\n",
1107 mmc_hostname(host));
1108 ocr &= ~0x7F;
1109 }
1110
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001111 host->ocr = mmc_select_voltage(host, ocr);
1112
1113 /*
1114 * Can we support the voltage(s) of the card(s)?
1115 */
1116 if (!host->ocr) {
1117 err = -EINVAL;
1118 goto err;
1119 }
1120
1121 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001122 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001123 */
Chris Ball3bca4cf2010-03-05 13:43:33 -08001124 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
Philip Rakitya303c532011-11-14 19:14:38 -08001125 if (err) {
1126 if (err == -EAGAIN) {
1127 /*
1128 * Retry initialization with S18R set to 0.
1129 */
1130 host->ocr &= ~R4_18V_PRESENT;
1131 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1132 }
1133 if (err)
1134 goto err;
1135 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001136 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001137
1138 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001139 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001140 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001141 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1142 /*
1143 * Let runtime PM core know our card is active
1144 */
1145 err = pm_runtime_set_active(&card->dev);
1146 if (err)
1147 goto remove;
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001148
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001149 /*
1150 * Enable runtime PM for this card
1151 */
1152 pm_runtime_enable(&card->dev);
1153 }
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001154
1155 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001156 * The number of functions on the card is encoded inside
1157 * the ocr.
1158 */
Matt Fleminge8812792009-12-17 15:27:18 -08001159 funcs = (ocr & 0x70000000) >> 28;
1160 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001161
San Mehate01587a2008-04-14 15:22:49 -07001162#ifdef CONFIG_MMC_EMBEDDED_SDIO
1163 if (host->embedded_sdio_data.funcs)
1164 card->sdio_funcs = funcs = host->embedded_sdio_data.num_funcs;
1165#endif
1166
Pierre Ossman4ff64712007-07-30 18:23:53 +02001167 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001168 * Initialize (but don't add) all present functions.
1169 */
Matt Fleminge8812792009-12-17 15:27:18 -08001170 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
San Mehate01587a2008-04-14 15:22:49 -07001171#ifdef CONFIG_MMC_EMBEDDED_SDIO
1172 if (host->embedded_sdio_data.funcs) {
1173 struct sdio_func *tmp;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001174
San Mehate01587a2008-04-14 15:22:49 -07001175 tmp = sdio_alloc_func(host->card);
1176 if (IS_ERR(tmp))
1177 goto remove;
1178 tmp->num = (i + 1);
1179 card->sdio_func[i] = tmp;
1180 tmp->class = host->embedded_sdio_data.funcs[i].f_class;
1181 tmp->max_blksize = host->embedded_sdio_data.funcs[i].f_maxblksize;
1182 tmp->vendor = card->cis.vendor;
1183 tmp->device = card->cis.device;
1184 } else {
1185#endif
1186 err = sdio_init_func(host->card, i + 1);
1187 if (err)
1188 goto remove;
1189#ifdef CONFIG_MMC_EMBEDDED_SDIO
1190 }
1191#endif
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001192 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001193 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001194 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001195 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1196 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001197 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001198
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001199 /*
1200 * First add the card to the driver model...
1201 */
Andy Ross807e8e42011-01-03 10:36:56 -08001202 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001203 err = mmc_add_card(host->card);
1204 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001205 goto remove_added;
1206
1207 /*
1208 * ...then the SDIO functions.
1209 */
1210 for (i = 0;i < funcs;i++) {
1211 err = sdio_add_func(host->card->sdio_func[i]);
1212 if (err)
1213 goto remove_added;
1214 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001215
Dmitry Shmidt34497912011-03-03 17:40:10 -05001216 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001217 return 0;
1218
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001219
1220remove_added:
1221 /* Remove without lock if the device has been added. */
1222 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001223 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001224remove:
1225 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001226 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001227 if (host->card)
1228 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001229 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001230err:
1231 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001232
Girish K Sa3c76eb2011-10-11 11:44:09 +05301233 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001234 mmc_hostname(host), err);
1235
1236 return err;
1237}
1238
San Mehat35fe42e2008-05-15 09:15:37 -07001239int sdio_reset_comm(struct mmc_card *card)
1240{
1241 struct mmc_host *host = card->host;
1242 u32 ocr;
1243 int err;
1244
1245 printk("%s():\n", __func__);
Dmitry Shmidt4c5391f2009-07-29 10:22:03 -07001246 mmc_claim_host(host);
1247
San Mehat35fe42e2008-05-15 09:15:37 -07001248 mmc_go_idle(host);
1249
1250 mmc_set_clock(host, host->f_min);
1251
1252 err = mmc_send_io_op_cond(host, 0, &ocr);
1253 if (err)
1254 goto err;
1255
1256 host->ocr = mmc_select_voltage(host, ocr);
1257 if (!host->ocr) {
1258 err = -EINVAL;
1259 goto err;
1260 }
1261
1262 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
1263 if (err)
1264 goto err;
1265
1266 if (mmc_host_is_spi(host)) {
1267 err = mmc_spi_set_crc(host, use_spi_crc);
1268 if (err)
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001269 goto err;
San Mehat35fe42e2008-05-15 09:15:37 -07001270 }
1271
1272 if (!mmc_host_is_spi(host)) {
1273 err = mmc_send_relative_addr(host, &card->rca);
1274 if (err)
1275 goto err;
1276 mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL);
1277 }
1278 if (!mmc_host_is_spi(host)) {
1279 err = mmc_select_card(card);
1280 if (err)
1281 goto err;
1282 }
1283
Daniel Chen676751d2009-12-09 09:45:36 -08001284 /*
1285 * Switch to high-speed (if supported).
1286 */
1287 err = sdio_enable_hs(card);
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001288 if (err > 0)
1289 mmc_sd_go_highspeed(card);
1290 else if (err)
Daniel Chen676751d2009-12-09 09:45:36 -08001291 goto err;
1292
1293 /*
1294 * Change to the card's maximum speed.
1295 */
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001296 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Daniel Chen676751d2009-12-09 09:45:36 -08001297
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001298 err = sdio_enable_4bit_bus(card);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001299 if (err > 0) {
1300 if (host->caps & MMC_CAP_8_BIT_DATA)
1301 mmc_set_bus_width(host, MMC_BUS_WIDTH_8);
1302 else if (host->caps & MMC_CAP_4_BIT_DATA)
1303 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
1304 }
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001305 else if (err)
San Mehat35fe42e2008-05-15 09:15:37 -07001306 goto err;
Dmitry Shmidtfe6271e2010-10-06 17:25:02 -07001307
Dmitry Shmidt4c5391f2009-07-29 10:22:03 -07001308 mmc_release_host(host);
San Mehat35fe42e2008-05-15 09:15:37 -07001309 return 0;
Dmitry Shmidt4c5391f2009-07-29 10:22:03 -07001310err:
San Mehat35fe42e2008-05-15 09:15:37 -07001311 printk("%s: Error resetting SDIO communications (%d)\n",
1312 mmc_hostname(host), err);
Dmitry Shmidt4c5391f2009-07-29 10:22:03 -07001313 mmc_release_host(host);
San Mehat35fe42e2008-05-15 09:15:37 -07001314 return err;
1315}
1316EXPORT_SYMBOL(sdio_reset_comm);