blob: bd7bacc950dc03b386d2a6907aff047f870bc14d [file] [log] [blame]
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001/*
2 * linux/drivers/mmc/sdio.c
3 *
4 * Copyright 2006-2007 Pierre Ossman
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
10 */
11
12#include <linux/err.h>
Ohad Ben-Cohen81968562010-10-02 13:54:10 +020013#include <linux/pm_runtime.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020014
15#include <linux/mmc/host.h>
16#include <linux/mmc/card.h>
Girish K Sa4924c72012-01-11 14:04:52 -050017#include <linux/mmc/mmc.h>
Pierre Ossman35c66c12007-06-11 20:25:43 +020018#include <linux/mmc/sdio.h>
Pierre Ossmane29a7d72007-05-26 13:48:18 +020019#include <linux/mmc/sdio_func.h>
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030020#include <linux/mmc/sdio_ids.h>
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020021
22#include "core.h"
23#include "bus.h"
Michal Miroslaw71578a12010-08-10 18:01:40 -070024#include "sd.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020025#include "sdio_bus.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020026#include "mmc_ops.h"
27#include "sd_ops.h"
28#include "sdio_ops.h"
Nicolas Pitreb7261262007-06-16 02:04:16 -040029#include "sdio_cis.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020030
Pierre Ossman05970072007-06-11 21:01:00 +020031static int sdio_read_fbr(struct sdio_func *func)
32{
33 int ret;
34 unsigned char data;
35
Ohad Ben-Coheneab40682011-04-05 17:50:14 +030036 if (mmc_card_nonstd_func_interface(func->card)) {
37 func->class = SDIO_CLASS_NONE;
38 return 0;
39 }
40
Pierre Ossman05970072007-06-11 21:01:00 +020041 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010042 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020043 if (ret)
44 goto out;
45
46 data &= 0x0f;
47
48 if (data == 0x0f) {
49 ret = mmc_io_rw_direct(func->card, 0, 0,
David Vrabel7616ee92007-08-08 14:23:05 +010050 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data);
Pierre Ossman05970072007-06-11 21:01:00 +020051 if (ret)
52 goto out;
53 }
54
55 func->class = data;
56
57out:
58 return ret;
59}
60
Pierre Ossmane29a7d72007-05-26 13:48:18 +020061static int sdio_init_func(struct mmc_card *card, unsigned int fn)
62{
Pierre Ossman05970072007-06-11 21:01:00 +020063 int ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020064 struct sdio_func *func;
65
66 BUG_ON(fn > SDIO_MAX_FUNCS);
67
68 func = sdio_alloc_func(card);
69 if (IS_ERR(func))
70 return PTR_ERR(func);
71
72 func->num = fn;
73
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070074 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) {
75 ret = sdio_read_fbr(func);
76 if (ret)
77 goto fail;
Pierre Ossman05970072007-06-11 21:01:00 +020078
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -070079 ret = sdio_read_func_cis(func);
80 if (ret)
81 goto fail;
82 } else {
83 func->vendor = func->card->cis.vendor;
84 func->device = func->card->cis.device;
85 func->max_blksize = func->card->cis.blksize;
86 }
Nicolas Pitreb7261262007-06-16 02:04:16 -040087
Pierre Ossmane29a7d72007-05-26 13:48:18 +020088 card->sdio_func[fn - 1] = func;
89
90 return 0;
Pierre Ossman05970072007-06-11 21:01:00 +020091
92fail:
93 /*
94 * It is okay to remove the function here even though we hold
95 * the host lock as we haven't registered the device yet.
96 */
97 sdio_remove_func(func);
98 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +020099}
100
Pierre Ossman35c66c12007-06-11 20:25:43 +0200101static int sdio_read_cccr(struct mmc_card *card)
102{
103 int ret;
104 int cccr_vsn;
105 unsigned char data;
Philip Rakitya303c532011-11-14 19:14:38 -0800106 unsigned char speed;
Pierre Ossman35c66c12007-06-11 20:25:43 +0200107
108 memset(&card->cccr, 0, sizeof(struct sdio_cccr));
109
110 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data);
111 if (ret)
112 goto out;
113
114 cccr_vsn = data & 0x0f;
115
Bing Zhaob4625da2011-10-20 19:16:32 -0700116 if (cccr_vsn > SDIO_CCCR_REV_3_00) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530117 pr_err("%s: unrecognised CCCR structure version %d\n",
Pierre Ossman35c66c12007-06-11 20:25:43 +0200118 mmc_hostname(card->host), cccr_vsn);
119 return -EINVAL;
120 }
121
122 card->cccr.sdio_vsn = (data & 0xf0) >> 4;
123
124 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data);
125 if (ret)
126 goto out;
127
128 if (data & SDIO_CCCR_CAP_SMB)
129 card->cccr.multi_block = 1;
130 if (data & SDIO_CCCR_CAP_LSC)
131 card->cccr.low_speed = 1;
132 if (data & SDIO_CCCR_CAP_4BLS)
133 card->cccr.wide_bus = 1;
134
135 if (cccr_vsn >= SDIO_CCCR_REV_1_10) {
136 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data);
137 if (ret)
138 goto out;
139
140 if (data & SDIO_POWER_SMPC)
141 card->cccr.high_power = 1;
142 }
143
144 if (cccr_vsn >= SDIO_CCCR_REV_1_20) {
Philip Rakitya303c532011-11-14 19:14:38 -0800145 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
Pierre Ossman35c66c12007-06-11 20:25:43 +0200146 if (ret)
147 goto out;
148
Philip Rakitya303c532011-11-14 19:14:38 -0800149 card->scr.sda_spec3 = 0;
150 card->sw_caps.sd3_bus_mode = 0;
151 card->sw_caps.sd3_drv_type = 0;
152 if (cccr_vsn >= SDIO_CCCR_REV_3_00) {
153 card->scr.sda_spec3 = 1;
154 ret = mmc_io_rw_direct(card, 0, 0,
155 SDIO_CCCR_UHS, 0, &data);
156 if (ret)
157 goto out;
158
159 if (card->host->caps &
160 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
161 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
162 MMC_CAP_UHS_DDR50)) {
163 if (data & SDIO_UHS_DDR50)
164 card->sw_caps.sd3_bus_mode
165 |= SD_MODE_UHS_DDR50;
166
167 if (data & SDIO_UHS_SDR50)
168 card->sw_caps.sd3_bus_mode
169 |= SD_MODE_UHS_SDR50;
170
171 if (data & SDIO_UHS_SDR104)
172 card->sw_caps.sd3_bus_mode
173 |= SD_MODE_UHS_SDR104;
174 }
175
176 ret = mmc_io_rw_direct(card, 0, 0,
177 SDIO_CCCR_DRIVE_STRENGTH, 0, &data);
178 if (ret)
179 goto out;
180
181 if (data & SDIO_DRIVE_SDTA)
182 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A;
183 if (data & SDIO_DRIVE_SDTC)
184 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C;
185 if (data & SDIO_DRIVE_SDTD)
186 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D;
187 }
188
189 /* if no uhs mode ensure we check for high speed */
190 if (!card->sw_caps.sd3_bus_mode) {
191 if (speed & SDIO_SPEED_SHS) {
192 card->cccr.high_speed = 1;
193 card->sw_caps.hs_max_dtr = 50000000;
194 } else {
195 card->cccr.high_speed = 0;
196 card->sw_caps.hs_max_dtr = 25000000;
197 }
198 }
Pierre Ossman35c66c12007-06-11 20:25:43 +0200199 }
200
201out:
202 return ret;
203}
204
Pierre Ossman4ff64712007-07-30 18:23:53 +0200205static int sdio_enable_wide(struct mmc_card *card)
206{
207 int ret;
208 u8 ctrl;
209
210 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
211 return 0;
212
213 if (card->cccr.low_speed && !card->cccr.wide_bus)
214 return 0;
215
216 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
217 if (ret)
218 return ret;
219
220 ctrl |= SDIO_BUS_WIDTH_4BIT;
221
222 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
223 if (ret)
224 return ret;
225
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700226 return 1;
Pierre Ossman4ff64712007-07-30 18:23:53 +0200227}
228
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200229/*
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700230 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1)
231 * of the card. This may be required on certain setups of boards,
232 * controllers and embedded sdio device which do not need the card's
233 * pull-up. As a result, card detection is disabled and power is saved.
234 */
235static int sdio_disable_cd(struct mmc_card *card)
236{
237 int ret;
238 u8 ctrl;
239
Ohad Ben-Cohen2059a022011-04-05 18:02:25 +0300240 if (!mmc_card_disable_cd(card))
Ohad Ben-Cohen006ebd52009-09-22 16:45:07 -0700241 return 0;
242
243 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
244 if (ret)
245 return ret;
246
247 ctrl |= SDIO_BUS_CD_DISABLE;
248
249 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
250}
251
252/*
Daniel Drake6b5eda32010-03-05 13:43:34 -0800253 * Devices that remain active during a system suspend are
254 * put back into 1-bit mode.
255 */
256static int sdio_disable_wide(struct mmc_card *card)
257{
258 int ret;
259 u8 ctrl;
260
261 if (!(card->host->caps & MMC_CAP_4_BIT_DATA))
262 return 0;
263
264 if (card->cccr.low_speed && !card->cccr.wide_bus)
265 return 0;
266
267 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl);
268 if (ret)
269 return ret;
270
271 if (!(ctrl & SDIO_BUS_WIDTH_4BIT))
272 return 0;
273
274 ctrl &= ~SDIO_BUS_WIDTH_4BIT;
275 ctrl |= SDIO_BUS_ASYNC_INT;
276
277 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL);
278 if (ret)
279 return ret;
280
281 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1);
282
283 return 0;
284}
285
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700286
287static int sdio_enable_4bit_bus(struct mmc_card *card)
288{
289 int err;
290
291 if (card->type == MMC_TYPE_SDIO)
292 return sdio_enable_wide(card);
293
294 if ((card->host->caps & MMC_CAP_4_BIT_DATA) &&
295 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) {
296 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4);
297 if (err)
298 return err;
299 } else
300 return 0;
301
302 err = sdio_enable_wide(card);
303 if (err <= 0)
304 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1);
305
306 return err;
307}
308
309
Daniel Drake6b5eda32010-03-05 13:43:34 -0800310/*
Pierre Ossmand16f5772008-08-31 17:22:46 +0200311 * Test if the card supports high-speed mode and, if so, switch to it.
312 */
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700313static int mmc_sdio_switch_hs(struct mmc_card *card, int enable)
Pierre Ossmand16f5772008-08-31 17:22:46 +0200314{
315 int ret;
316 u8 speed;
317
318 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED))
319 return 0;
320
321 if (!card->cccr.high_speed)
322 return 0;
323
324 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
325 if (ret)
326 return ret;
327
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700328 if (enable)
329 speed |= SDIO_SPEED_EHS;
330 else
331 speed &= ~SDIO_SPEED_EHS;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200332
333 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
334 if (ret)
335 return ret;
336
Michal Miroslaw71578a12010-08-10 18:01:40 -0700337 return 1;
338}
Pierre Ossmand16f5772008-08-31 17:22:46 +0200339
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700340/*
341 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported.
342 */
343static int sdio_enable_hs(struct mmc_card *card)
344{
345 int ret;
346
347 ret = mmc_sdio_switch_hs(card, true);
348 if (ret <= 0 || card->type == MMC_TYPE_SDIO)
349 return ret;
350
351 ret = mmc_sd_switch_hs(card);
352 if (ret <= 0)
353 mmc_sdio_switch_hs(card, false);
354
355 return ret;
356}
357
Michal Miroslaw71578a12010-08-10 18:01:40 -0700358static unsigned mmc_sdio_get_max_clock(struct mmc_card *card)
359{
360 unsigned max_dtr;
361
362 if (mmc_card_highspeed(card)) {
363 /*
364 * The SDIO specification doesn't mention how
365 * the CIS transfer speed register relates to
366 * high-speed, but it seems that 50 MHz is
367 * mandatory.
368 */
369 max_dtr = 50000000;
370 } else {
371 max_dtr = card->cis.max_dtr;
372 }
373
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700374 if (card->type == MMC_TYPE_SD_COMBO)
375 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card));
376
Michal Miroslaw71578a12010-08-10 18:01:40 -0700377 return max_dtr;
Pierre Ossmand16f5772008-08-31 17:22:46 +0200378}
379
Philip Rakitya303c532011-11-14 19:14:38 -0800380static unsigned char host_drive_to_sdio_drive(int host_strength)
381{
382 switch (host_strength) {
383 case MMC_SET_DRIVER_TYPE_A:
384 return SDIO_DTSx_SET_TYPE_A;
385 case MMC_SET_DRIVER_TYPE_B:
386 return SDIO_DTSx_SET_TYPE_B;
387 case MMC_SET_DRIVER_TYPE_C:
388 return SDIO_DTSx_SET_TYPE_C;
389 case MMC_SET_DRIVER_TYPE_D:
390 return SDIO_DTSx_SET_TYPE_D;
391 default:
392 return SDIO_DTSx_SET_TYPE_B;
393 }
394}
395
396static void sdio_select_driver_type(struct mmc_card *card)
397{
398 int host_drv_type = SD_DRIVER_TYPE_B;
399 int card_drv_type = SD_DRIVER_TYPE_B;
400 int drive_strength;
401 unsigned char card_strength;
402 int err;
403
404 /*
405 * If the host doesn't support any of the Driver Types A,C or D,
406 * or there is no board specific handler then default Driver
407 * Type B is used.
408 */
409 if (!(card->host->caps &
410 (MMC_CAP_DRIVER_TYPE_A |
411 MMC_CAP_DRIVER_TYPE_C |
412 MMC_CAP_DRIVER_TYPE_D)))
413 return;
414
415 if (!card->host->ops->select_drive_strength)
416 return;
417
418 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A)
419 host_drv_type |= SD_DRIVER_TYPE_A;
420
421 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C)
422 host_drv_type |= SD_DRIVER_TYPE_C;
423
424 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D)
425 host_drv_type |= SD_DRIVER_TYPE_D;
426
427 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A)
428 card_drv_type |= SD_DRIVER_TYPE_A;
429
430 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C)
431 card_drv_type |= SD_DRIVER_TYPE_C;
432
433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D)
434 card_drv_type |= SD_DRIVER_TYPE_D;
435
436 /*
437 * The drive strength that the hardware can support
438 * depends on the board design. Pass the appropriate
439 * information and let the hardware specific code
440 * return what is possible given the options
441 */
442 drive_strength = card->host->ops->select_drive_strength(
443 card->sw_caps.uhs_max_dtr,
444 host_drv_type, card_drv_type);
445
446 /* if error just use default for drive strength B */
447 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0,
448 &card_strength);
449 if (err)
450 return;
451
452 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT);
453 card_strength |= host_drive_to_sdio_drive(drive_strength);
454
455 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH,
456 card_strength, NULL);
457
458 /* if error default to drive strength B */
459 if (!err)
460 mmc_set_driver_type(card->host, drive_strength);
461}
462
463
464static int sdio_set_bus_speed_mode(struct mmc_card *card)
465{
466 unsigned int bus_speed, timing;
467 int err;
468 unsigned char speed;
469
470 /*
471 * If the host doesn't support any of the UHS-I modes, fallback on
472 * default speed.
473 */
474 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
475 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50)))
476 return 0;
477
478 bus_speed = SDIO_SPEED_SDR12;
479 timing = MMC_TIMING_UHS_SDR12;
480 if ((card->host->caps & MMC_CAP_UHS_SDR104) &&
481 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) {
482 bus_speed = SDIO_SPEED_SDR104;
483 timing = MMC_TIMING_UHS_SDR104;
484 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR;
485 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) &&
486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) {
487 bus_speed = SDIO_SPEED_DDR50;
488 timing = MMC_TIMING_UHS_DDR50;
489 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR;
490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
491 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode &
492 SD_MODE_UHS_SDR50)) {
493 bus_speed = SDIO_SPEED_SDR50;
494 timing = MMC_TIMING_UHS_SDR50;
495 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR;
496 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
497 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) &&
498 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) {
499 bus_speed = SDIO_SPEED_SDR25;
500 timing = MMC_TIMING_UHS_SDR25;
501 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR;
502 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 |
503 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 |
504 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode &
505 SD_MODE_UHS_SDR12)) {
506 bus_speed = SDIO_SPEED_SDR12;
507 timing = MMC_TIMING_UHS_SDR12;
508 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR;
509 }
510
511 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed);
512 if (err)
513 return err;
514
515 speed &= ~SDIO_SPEED_BSS_MASK;
516 speed |= bus_speed;
517 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL);
518 if (err)
519 return err;
520
521 if (bus_speed) {
522 mmc_set_timing(card->host, timing);
523 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr);
524 }
525
526 return 0;
527}
528
529/*
530 * UHS-I specific initialization procedure
531 */
532static int mmc_sdio_init_uhs_card(struct mmc_card *card)
533{
534 int err;
535
536 if (!card->scr.sda_spec3)
537 return 0;
538
539 /*
540 * Switch to wider bus (if supported).
541 */
542 if (card->host->caps & MMC_CAP_4_BIT_DATA) {
543 err = sdio_enable_4bit_bus(card);
544 if (err > 0) {
545 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
546 err = 0;
547 }
548 }
549
550 /* Set the driver strength for the card */
551 sdio_select_driver_type(card);
552
553 /* Set bus speed mode of the card */
554 err = sdio_set_bus_speed_mode(card);
555 if (err)
556 goto out;
557
558 /* Initialize and start re-tuning timer */
559 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning)
Girish K Sa4924c72012-01-11 14:04:52 -0500560 err = card->host->ops->execute_tuning(card->host,
561 MMC_SEND_TUNING_BLOCK);
Philip Rakitya303c532011-11-14 19:14:38 -0800562
563out:
564
565 return err;
566}
567
Pierre Ossmand16f5772008-08-31 17:22:46 +0200568/*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700569 * Handle the detection and initialisation of a card.
570 *
571 * In the case of a resume, "oldcard" will contain the card
572 * we're trying to reinitialise.
573 */
574static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr,
Chris Ball3bca4cf2010-03-05 13:43:33 -0800575 struct mmc_card *oldcard, int powered_resume)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700576{
577 struct mmc_card *card;
578 int err;
579
580 BUG_ON(!host);
581 WARN_ON(!host->claimed);
582
583 /*
584 * Inform the card of the voltage
585 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800586 if (!powered_resume) {
587 err = mmc_send_io_op_cond(host, host->ocr, &ocr);
588 if (err)
589 goto err;
590 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700591
592 /*
593 * For SPI, enable CRC as appropriate.
594 */
595 if (mmc_host_is_spi(host)) {
596 err = mmc_spi_set_crc(host, use_spi_crc);
597 if (err)
598 goto err;
599 }
600
601 /*
602 * Allocate card structure.
603 */
604 card = mmc_alloc_card(host, NULL);
605 if (IS_ERR(card)) {
606 err = PTR_ERR(card);
607 goto err;
608 }
609
Arindam Nathd6d50a12011-05-05 12:18:59 +0530610 if ((ocr & R4_MEMORY_PRESENT) &&
611 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) {
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700612 card->type = MMC_TYPE_SD_COMBO;
613
614 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO ||
615 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) {
616 mmc_remove_card(card);
617 return -ENOENT;
618 }
619 } else {
620 card->type = MMC_TYPE_SDIO;
621
622 if (oldcard && oldcard->type != MMC_TYPE_SDIO) {
623 mmc_remove_card(card);
624 return -ENOENT;
625 }
626 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700627
628 /*
Daniel Mack3fcb0272010-04-01 10:03:25 +0200629 * Call the optional HC's init_card function to handle quirks.
630 */
631 if (host->ops->init_card)
632 host->ops->init_card(host, card);
633
634 /*
Philip Rakitya303c532011-11-14 19:14:38 -0800635 * If the host and card support UHS-I mode request the card
636 * to switch to 1.8V signaling level. No 1.8v signalling if
637 * UHS mode is not enabled to maintain compatibilty and some
638 * systems that claim 1.8v signalling in fact do not support
639 * it.
640 */
641 if ((ocr & R4_18V_PRESENT) &&
642 (host->caps &
643 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
644 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
645 MMC_CAP_UHS_DDR50))) {
646 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180,
647 true);
648 if (err) {
649 ocr &= ~R4_18V_PRESENT;
650 host->ocr &= ~R4_18V_PRESENT;
651 }
652 err = 0;
653 } else {
654 ocr &= ~R4_18V_PRESENT;
655 host->ocr &= ~R4_18V_PRESENT;
656 }
657
658 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700659 * For native busses: set card RCA and quit open drain mode.
660 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800661 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700662 err = mmc_send_relative_addr(host, &card->rca);
663 if (err)
664 goto remove;
665
Stefan Nilsson XK0aab3992011-03-01 14:41:04 +0100666 /*
667 * Update oldcard with the new RCA received from the SDIO
668 * device -- we're doing this so that it's updated in the
669 * "card" struct when oldcard overwrites that later.
670 */
671 if (oldcard)
672 oldcard->rca = card->rca;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700673 }
674
675 /*
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700676 * Read CSD, before selecting the card
677 */
678 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) {
679 err = mmc_sd_get_csd(host, card);
680 if (err)
681 return err;
682
683 mmc_decode_cid(card);
684 }
685
686 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700687 * Select card, as all following commands rely on that.
688 */
Chris Ball3bca4cf2010-03-05 13:43:33 -0800689 if (!powered_resume && !mmc_host_is_spi(host)) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700690 err = mmc_select_card(card);
691 if (err)
692 goto remove;
693 }
694
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700695 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) {
696 /*
697 * This is non-standard SDIO device, meaning it doesn't
698 * have any CIA (Common I/O area) registers present.
699 * It's host's responsibility to fill cccr and cis
700 * structures in init_card().
701 */
702 mmc_set_clock(host, card->cis.max_dtr);
703
704 if (card->cccr.high_speed) {
705 mmc_card_set_highspeed(card);
706 mmc_set_timing(card->host, MMC_TIMING_SD_HS);
707 }
708
709 goto finish;
710 }
711
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700712 /*
713 * Read the common registers.
714 */
715 err = sdio_read_cccr(card);
716 if (err)
717 goto remove;
718
719 /*
720 * Read the common CIS tuples.
721 */
722 err = sdio_read_common_cis(card);
723 if (err)
724 goto remove;
725
726 if (oldcard) {
727 int same = (card->cis.vendor == oldcard->cis.vendor &&
728 card->cis.device == oldcard->cis.device);
729 mmc_remove_card(card);
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700730 if (!same)
731 return -ENOENT;
732
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700733 card = oldcard;
734 }
Andrei Warkentin32780cd2011-04-11 17:02:15 -0500735 mmc_fixup_device(card, NULL);
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700736
Michal Miroslaw7310ece2010-08-10 18:01:40 -0700737 if (card->type == MMC_TYPE_SD_COMBO) {
738 err = mmc_sd_setup_card(host, card, oldcard != NULL);
739 /* handle as SDIO-only card if memory init failed */
740 if (err) {
741 mmc_go_idle(host);
742 if (mmc_host_is_spi(host))
743 /* should not fail, as it worked previously */
744 mmc_spi_set_crc(host, use_spi_crc);
745 card->type = MMC_TYPE_SDIO;
746 } else
747 card->dev.type = &sd_type;
748 }
749
750 /*
751 * If needed, disconnect card detection pull-up resistor.
752 */
753 err = sdio_disable_cd(card);
754 if (err)
755 goto remove;
756
Philip Rakitya303c532011-11-14 19:14:38 -0800757 /* Initialization sequence for UHS-I cards */
758 /* Only if card supports 1.8v and UHS signaling */
759 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) {
760 err = mmc_sdio_init_uhs_card(card);
761 if (err)
762 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700763
Philip Rakitya303c532011-11-14 19:14:38 -0800764 /* Card is an ultra-high-speed card */
765 mmc_card_set_uhs(card);
766 } else {
767 /*
768 * Switch to high-speed (if supported).
769 */
770 err = sdio_enable_hs(card);
771 if (err > 0)
772 mmc_sd_go_highspeed(card);
773 else if (err)
774 goto remove;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700775
Philip Rakitya303c532011-11-14 19:14:38 -0800776 /*
777 * Change to the card's maximum speed.
778 */
779 mmc_set_clock(host, mmc_sdio_get_max_clock(card));
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700780
Philip Rakitya303c532011-11-14 19:14:38 -0800781 /*
782 * Switch to wider bus (if supported).
783 */
784 err = sdio_enable_4bit_bus(card);
785 if (err > 0)
786 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4);
787 else if (err)
788 goto remove;
789 }
Grazvydas Ignotas6f51be32010-08-10 18:01:50 -0700790finish:
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700791 if (!oldcard)
792 host->card = card;
793 return 0;
794
795remove:
796 if (!oldcard)
797 mmc_remove_card(card);
798
799err:
800 return err;
801}
802
803/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200804 * Host is being removed. Free up the current card.
805 */
806static void mmc_sdio_remove(struct mmc_host *host)
807{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200808 int i;
809
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200810 BUG_ON(!host);
811 BUG_ON(!host->card);
812
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200813 for (i = 0;i < host->card->sdio_funcs;i++) {
814 if (host->card->sdio_func[i]) {
815 sdio_remove_func(host->card->sdio_func[i]);
816 host->card->sdio_func[i] = NULL;
817 }
818 }
819
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200820 mmc_remove_card(host->card);
821 host->card = NULL;
822}
823
824/*
Adrian Hunterd3049502011-11-28 16:22:00 +0200825 * Card detection - card is alive.
826 */
827static int mmc_sdio_alive(struct mmc_host *host)
828{
829 return mmc_select_card(host->card);
830}
831
832/*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200833 * Card detection callback from host.
834 */
835static void mmc_sdio_detect(struct mmc_host *host)
836{
837 int err;
838
839 BUG_ON(!host);
840 BUG_ON(!host->card);
841
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200842 /* Make sure card is powered before detecting it */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200843 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
844 err = pm_runtime_get_sync(&host->card->dev);
845 if (err < 0)
846 goto out;
847 }
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200848
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200849 mmc_claim_host(host);
850
851 /*
852 * Just check if our card has been removed.
853 */
Adrian Hunterd3049502011-11-28 16:22:00 +0200854 err = _mmc_detect_card_removed(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200855
856 mmc_release_host(host);
857
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200858 /*
859 * Tell PM core it's OK to power off the card now.
860 *
861 * The _sync variant is used in order to ensure that the card
862 * is left powered off in case an error occurred, and the card
863 * is going to be removed.
864 *
865 * Since there is no specific reason to believe a new user
866 * is about to show up at this point, the _sync variant is
867 * desirable anyway.
868 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +0200869 if (host->caps & MMC_CAP_POWER_OFF_CARD)
870 pm_runtime_put_sync(&host->card->dev);
Ohad Ben-Cohen4d0812c2010-11-14 12:40:33 +0200871
Ohad Ben-Cohen87973ba2010-10-02 13:54:12 +0200872out:
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200873 if (err) {
874 mmc_sdio_remove(host);
875
876 mmc_claim_host(host);
877 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -0400878 mmc_power_off(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200879 mmc_release_host(host);
880 }
881}
882
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700883/*
884 * SDIO suspend. We need to suspend all functions separately.
885 * Therefore all registered functions must have drivers with suspend
886 * and resume methods. Failing that we simply remove the whole card.
887 */
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700888static int mmc_sdio_suspend(struct mmc_host *host)
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700889{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700890 int i, err = 0;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700891
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700892 for (i = 0; i < host->card->sdio_funcs; i++) {
893 struct sdio_func *func = host->card->sdio_func[i];
894 if (func && sdio_func_present(func) && func->dev.driver) {
895 const struct dev_pm_ops *pmops = func->dev.driver->pm;
896 if (!pmops || !pmops->suspend || !pmops->resume) {
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700897 /* force removal of entire card in that case */
898 err = -ENOSYS;
899 } else
900 err = pmops->suspend(&func->dev);
901 if (err)
902 break;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700903 }
904 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700905 while (err && --i >= 0) {
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700906 struct sdio_func *func = host->card->sdio_func[i];
907 if (func && sdio_func_present(func) && func->dev.driver) {
908 const struct dev_pm_ops *pmops = func->dev.driver->pm;
909 pmops->resume(&func->dev);
910 }
911 }
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700912
Ohad Ben-Cohen6b93d01f2011-04-05 17:43:21 +0300913 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Daniel Drake6b5eda32010-03-05 13:43:34 -0800914 mmc_claim_host(host);
915 sdio_disable_wide(host->card);
916 mmc_release_host(host);
917 }
918
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700919 return err;
920}
921
922static int mmc_sdio_resume(struct mmc_host *host)
923{
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200924 int i, err = 0;
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700925
926 BUG_ON(!host);
927 BUG_ON(!host->card);
928
929 /* Basic card reinitialization. */
930 mmc_claim_host(host);
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200931
932 /* No need to reinitialize powered-resumed nonremovable cards */
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +0300933 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host))
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200934 err = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +0300935 mmc_card_keep_power(host));
Ohad Ben-Cohen6b93d01f2011-04-05 17:43:21 +0300936 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) {
Ohad Ben-Cohen080bc972010-11-28 07:21:29 +0200937 /* We may have switched to 1-bit mode during suspend */
938 err = sdio_enable_4bit_bus(host->card);
939 if (err > 0) {
940 mmc_set_bus_width(host, MMC_BUS_WIDTH_4);
941 err = 0;
942 }
943 }
944
Nicolas Pitre40216842010-03-05 13:43:34 -0800945 if (!err && host->sdio_irqs)
946 mmc_signal_sdio_irq(host);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -0700947 mmc_release_host(host);
948
949 /*
950 * If the card looked to be the same as before suspending, then
951 * we proceed to resume all card functions. If one of them returns
952 * an error then we simply return that error to the core and the
953 * card will be redetected as new. It is the responsibility of
954 * the function driver to perform further tests with the extra
955 * knowledge it has of the card to confirm the card is indeed the
956 * same as before suspending (same MAC address for network cards,
957 * etc.) and return an error otherwise.
958 */
959 for (i = 0; !err && i < host->card->sdio_funcs; i++) {
960 struct sdio_func *func = host->card->sdio_func[i];
961 if (func && sdio_func_present(func) && func->dev.driver) {
962 const struct dev_pm_ops *pmops = func->dev.driver->pm;
963 err = pmops->resume(&func->dev);
964 }
965 }
966
967 return err;
Nicolas Pitre17d33e12009-09-22 16:45:28 -0700968}
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200969
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +0200970static int mmc_sdio_power_restore(struct mmc_host *host)
971{
972 int ret;
Daniel Drakec6e633a2011-06-25 19:20:11 +0100973 u32 ocr;
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +0200974
975 BUG_ON(!host);
976 BUG_ON(!host->card);
977
978 mmc_claim_host(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +0100979
980 /*
981 * Reset the card by performing the same steps that are taken by
982 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe.
983 *
984 * sdio_reset() is technically not needed. Having just powered up the
985 * hardware, it should already be in reset state. However, some
986 * platforms (such as SD8686 on OLPC) do not instantly cut power,
987 * meaning that a reset is required when restoring power soon after
988 * powering off. It is harmless in other cases.
989 *
990 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec,
991 * is not necessary for non-removable cards. However, it is required
992 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and
993 * harmless in other situations.
994 *
995 * With these steps taken, mmc_select_voltage() is also required to
996 * restore the correct voltage setting of the card.
997 */
998 sdio_reset(host);
999 mmc_go_idle(host);
1000 mmc_send_if_cond(host, host->ocr_avail);
1001
1002 ret = mmc_send_io_op_cond(host, 0, &ocr);
1003 if (ret)
1004 goto out;
1005
1006 if (host->ocr_avail_sdio)
1007 host->ocr_avail = host->ocr_avail_sdio;
1008
1009 host->ocr = mmc_select_voltage(host, ocr & ~0x7F);
1010 if (!host->ocr) {
1011 ret = -EINVAL;
1012 goto out;
1013 }
1014
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001015 ret = mmc_sdio_init_card(host, host->ocr, host->card,
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001016 mmc_card_keep_power(host));
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001017 if (!ret && host->sdio_irqs)
1018 mmc_signal_sdio_irq(host);
Daniel Drakec6e633a2011-06-25 19:20:11 +01001019
1020out:
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001021 mmc_release_host(host);
1022
1023 return ret;
1024}
1025
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001026static const struct mmc_bus_ops mmc_sdio_ops = {
1027 .remove = mmc_sdio_remove,
1028 .detect = mmc_sdio_detect,
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001029 .suspend = mmc_sdio_suspend,
1030 .resume = mmc_sdio_resume,
Ohad Ben-Cohend3fe37b2010-10-02 13:54:07 +02001031 .power_restore = mmc_sdio_power_restore,
Adrian Hunterd3049502011-11-28 16:22:00 +02001032 .alive = mmc_sdio_alive,
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001033};
1034
1035
1036/*
1037 * Starting point for SDIO card init.
1038 */
Andy Ross807e8e42011-01-03 10:36:56 -08001039int mmc_attach_sdio(struct mmc_host *host)
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001040{
Andy Ross807e8e42011-01-03 10:36:56 -08001041 int err, i, funcs;
1042 u32 ocr;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001043 struct mmc_card *card;
1044
1045 BUG_ON(!host);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001046 WARN_ON(!host->claimed);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001047
Andy Ross807e8e42011-01-03 10:36:56 -08001048 err = mmc_send_io_op_cond(host, 0, &ocr);
1049 if (err)
1050 return err;
1051
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001052 mmc_attach_bus(host, &mmc_sdio_ops);
Takashi Iwai8f230f42010-12-08 10:04:30 +01001053 if (host->ocr_avail_sdio)
1054 host->ocr_avail = host->ocr_avail_sdio;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001055
1056 /*
1057 * Sanity check the voltages that the card claims to
1058 * support.
1059 */
1060 if (ocr & 0x7F) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301061 pr_warning("%s: card claims to support voltages "
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001062 "below the defined range. These will be ignored.\n",
1063 mmc_hostname(host));
1064 ocr &= ~0x7F;
1065 }
1066
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001067 host->ocr = mmc_select_voltage(host, ocr);
1068
1069 /*
1070 * Can we support the voltage(s) of the card(s)?
1071 */
1072 if (!host->ocr) {
1073 err = -EINVAL;
1074 goto err;
1075 }
1076
1077 /*
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001078 * Detect and init the card.
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001079 */
Chris Ball3bca4cf2010-03-05 13:43:33 -08001080 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
Philip Rakitya303c532011-11-14 19:14:38 -08001081 if (err) {
1082 if (err == -EAGAIN) {
1083 /*
1084 * Retry initialization with S18R set to 0.
1085 */
1086 host->ocr &= ~R4_18V_PRESENT;
1087 err = mmc_sdio_init_card(host, host->ocr, NULL, 0);
1088 }
1089 if (err)
1090 goto err;
1091 }
Nicolas Pitre17d33e12009-09-22 16:45:28 -07001092 card = host->card;
David Brownellaf517152007-08-08 09:11:32 -07001093
1094 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001095 * Enable runtime PM only if supported by host+card+board
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001096 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001097 if (host->caps & MMC_CAP_POWER_OFF_CARD) {
1098 /*
1099 * Let runtime PM core know our card is active
1100 */
1101 err = pm_runtime_set_active(&card->dev);
1102 if (err)
1103 goto remove;
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001104
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001105 /*
1106 * Enable runtime PM for this card
1107 */
1108 pm_runtime_enable(&card->dev);
1109 }
Ohad Ben-Cohen81968562010-10-02 13:54:10 +02001110
1111 /*
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001112 * The number of functions on the card is encoded inside
1113 * the ocr.
1114 */
Matt Fleminge8812792009-12-17 15:27:18 -08001115 funcs = (ocr & 0x70000000) >> 28;
1116 card->sdio_funcs = 0;
Pierre Ossman4ff64712007-07-30 18:23:53 +02001117
1118 /*
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001119 * Initialize (but don't add) all present functions.
1120 */
Matt Fleminge8812792009-12-17 15:27:18 -08001121 for (i = 0; i < funcs; i++, card->sdio_funcs++) {
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001122 err = sdio_init_func(host->card, i + 1);
1123 if (err)
1124 goto remove;
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001125
1126 /*
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001127 * Enable Runtime PM for this func (if supported)
Ohad Ben-Cohen40bba0c2010-10-02 13:54:11 +02001128 */
Ohad Ben-Cohened919b02010-11-19 09:29:09 +02001129 if (host->caps & MMC_CAP_POWER_OFF_CARD)
1130 pm_runtime_enable(&card->sdio_func[i]->dev);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001131 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001132
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001133 /*
1134 * First add the card to the driver model...
1135 */
Andy Ross807e8e42011-01-03 10:36:56 -08001136 mmc_release_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001137 err = mmc_add_card(host->card);
1138 if (err)
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001139 goto remove_added;
1140
1141 /*
1142 * ...then the SDIO functions.
1143 */
1144 for (i = 0;i < funcs;i++) {
1145 err = sdio_add_func(host->card->sdio_func[i]);
1146 if (err)
1147 goto remove_added;
1148 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001149
Dmitry Shmidt34497912011-03-03 17:40:10 -05001150 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001151 return 0;
1152
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001153
1154remove_added:
1155 /* Remove without lock if the device has been added. */
1156 mmc_sdio_remove(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001157 mmc_claim_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001158remove:
1159 /* And with lock if it hasn't been added. */
Andy Ross807e8e42011-01-03 10:36:56 -08001160 mmc_release_host(host);
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001161 if (host->card)
1162 mmc_sdio_remove(host);
Andy Ross807e8e42011-01-03 10:36:56 -08001163 mmc_claim_host(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001164err:
1165 mmc_detach_bus(host);
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001166
Girish K Sa3c76eb2011-10-11 11:44:09 +05301167 pr_err("%s: error %d whilst initialising SDIO card\n",
Pierre Ossman5c4e6f12007-05-21 20:23:20 +02001168 mmc_hostname(host), err);
1169
1170 return err;
1171}
1172