Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 1 | /* |
| 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> |
| 13 | |
| 14 | #include <linux/mmc/host.h> |
| 15 | #include <linux/mmc/card.h> |
Pierre Ossman | 35c66c1 | 2007-06-11 20:25:43 +0200 | [diff] [blame] | 16 | #include <linux/mmc/sdio.h> |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 17 | #include <linux/mmc/sdio_func.h> |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 18 | |
| 19 | #include "core.h" |
| 20 | #include "bus.h" |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame^] | 21 | #include "sd.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 22 | #include "sdio_bus.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 23 | #include "mmc_ops.h" |
| 24 | #include "sd_ops.h" |
| 25 | #include "sdio_ops.h" |
Nicolas Pitre | b726126 | 2007-06-16 02:04:16 -0400 | [diff] [blame] | 26 | #include "sdio_cis.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 27 | |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 28 | static int sdio_read_fbr(struct sdio_func *func) |
| 29 | { |
| 30 | int ret; |
| 31 | unsigned char data; |
| 32 | |
| 33 | ret = mmc_io_rw_direct(func->card, 0, 0, |
David Vrabel | 7616ee9 | 2007-08-08 14:23:05 +0100 | [diff] [blame] | 34 | SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 35 | if (ret) |
| 36 | goto out; |
| 37 | |
| 38 | data &= 0x0f; |
| 39 | |
| 40 | if (data == 0x0f) { |
| 41 | ret = mmc_io_rw_direct(func->card, 0, 0, |
David Vrabel | 7616ee9 | 2007-08-08 14:23:05 +0100 | [diff] [blame] | 42 | SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 43 | if (ret) |
| 44 | goto out; |
| 45 | } |
| 46 | |
| 47 | func->class = data; |
| 48 | |
| 49 | out: |
| 50 | return ret; |
| 51 | } |
| 52 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 53 | static int sdio_init_func(struct mmc_card *card, unsigned int fn) |
| 54 | { |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 55 | int ret; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 56 | struct sdio_func *func; |
| 57 | |
| 58 | BUG_ON(fn > SDIO_MAX_FUNCS); |
| 59 | |
| 60 | func = sdio_alloc_func(card); |
| 61 | if (IS_ERR(func)) |
| 62 | return PTR_ERR(func); |
| 63 | |
| 64 | func->num = fn; |
| 65 | |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 66 | ret = sdio_read_fbr(func); |
| 67 | if (ret) |
| 68 | goto fail; |
| 69 | |
Pierre Ossman | 1a632f8 | 2007-07-30 15:15:30 +0200 | [diff] [blame] | 70 | ret = sdio_read_func_cis(func); |
Nicolas Pitre | b726126 | 2007-06-16 02:04:16 -0400 | [diff] [blame] | 71 | if (ret) |
| 72 | goto fail; |
| 73 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 74 | card->sdio_func[fn - 1] = func; |
| 75 | |
| 76 | return 0; |
Pierre Ossman | 0597007 | 2007-06-11 21:01:00 +0200 | [diff] [blame] | 77 | |
| 78 | fail: |
| 79 | /* |
| 80 | * It is okay to remove the function here even though we hold |
| 81 | * the host lock as we haven't registered the device yet. |
| 82 | */ |
| 83 | sdio_remove_func(func); |
| 84 | return ret; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 85 | } |
| 86 | |
Pierre Ossman | 35c66c1 | 2007-06-11 20:25:43 +0200 | [diff] [blame] | 87 | static int sdio_read_cccr(struct mmc_card *card) |
| 88 | { |
| 89 | int ret; |
| 90 | int cccr_vsn; |
| 91 | unsigned char data; |
| 92 | |
| 93 | memset(&card->cccr, 0, sizeof(struct sdio_cccr)); |
| 94 | |
| 95 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data); |
| 96 | if (ret) |
| 97 | goto out; |
| 98 | |
| 99 | cccr_vsn = data & 0x0f; |
| 100 | |
| 101 | if (cccr_vsn > SDIO_CCCR_REV_1_20) { |
| 102 | printk(KERN_ERR "%s: unrecognised CCCR structure version %d\n", |
| 103 | mmc_hostname(card->host), cccr_vsn); |
| 104 | return -EINVAL; |
| 105 | } |
| 106 | |
| 107 | card->cccr.sdio_vsn = (data & 0xf0) >> 4; |
| 108 | |
| 109 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data); |
| 110 | if (ret) |
| 111 | goto out; |
| 112 | |
| 113 | if (data & SDIO_CCCR_CAP_SMB) |
| 114 | card->cccr.multi_block = 1; |
| 115 | if (data & SDIO_CCCR_CAP_LSC) |
| 116 | card->cccr.low_speed = 1; |
| 117 | if (data & SDIO_CCCR_CAP_4BLS) |
| 118 | card->cccr.wide_bus = 1; |
| 119 | |
| 120 | if (cccr_vsn >= SDIO_CCCR_REV_1_10) { |
| 121 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data); |
| 122 | if (ret) |
| 123 | goto out; |
| 124 | |
| 125 | if (data & SDIO_POWER_SMPC) |
| 126 | card->cccr.high_power = 1; |
| 127 | } |
| 128 | |
| 129 | if (cccr_vsn >= SDIO_CCCR_REV_1_20) { |
| 130 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &data); |
| 131 | if (ret) |
| 132 | goto out; |
| 133 | |
| 134 | if (data & SDIO_SPEED_SHS) |
| 135 | card->cccr.high_speed = 1; |
| 136 | } |
| 137 | |
| 138 | out: |
| 139 | return ret; |
| 140 | } |
| 141 | |
Pierre Ossman | 4ff6471 | 2007-07-30 18:23:53 +0200 | [diff] [blame] | 142 | static int sdio_enable_wide(struct mmc_card *card) |
| 143 | { |
| 144 | int ret; |
| 145 | u8 ctrl; |
| 146 | |
| 147 | if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) |
| 148 | return 0; |
| 149 | |
| 150 | if (card->cccr.low_speed && !card->cccr.wide_bus) |
| 151 | return 0; |
| 152 | |
| 153 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | ctrl |= SDIO_BUS_WIDTH_4BIT; |
| 158 | |
| 159 | ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
| 163 | mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 168 | /* |
Ohad Ben-Cohen | 006ebd5 | 2009-09-22 16:45:07 -0700 | [diff] [blame] | 169 | * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) |
| 170 | * of the card. This may be required on certain setups of boards, |
| 171 | * controllers and embedded sdio device which do not need the card's |
| 172 | * pull-up. As a result, card detection is disabled and power is saved. |
| 173 | */ |
| 174 | static int sdio_disable_cd(struct mmc_card *card) |
| 175 | { |
| 176 | int ret; |
| 177 | u8 ctrl; |
| 178 | |
| 179 | if (!card->cccr.disable_cd) |
| 180 | return 0; |
| 181 | |
| 182 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); |
| 183 | if (ret) |
| 184 | return ret; |
| 185 | |
| 186 | ctrl |= SDIO_BUS_CD_DISABLE; |
| 187 | |
| 188 | return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); |
| 189 | } |
| 190 | |
| 191 | /* |
Daniel Drake | 6b5eda3 | 2010-03-05 13:43:34 -0800 | [diff] [blame] | 192 | * Devices that remain active during a system suspend are |
| 193 | * put back into 1-bit mode. |
| 194 | */ |
| 195 | static int sdio_disable_wide(struct mmc_card *card) |
| 196 | { |
| 197 | int ret; |
| 198 | u8 ctrl; |
| 199 | |
| 200 | if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) |
| 201 | return 0; |
| 202 | |
| 203 | if (card->cccr.low_speed && !card->cccr.wide_bus) |
| 204 | return 0; |
| 205 | |
| 206 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); |
| 207 | if (ret) |
| 208 | return ret; |
| 209 | |
| 210 | if (!(ctrl & SDIO_BUS_WIDTH_4BIT)) |
| 211 | return 0; |
| 212 | |
| 213 | ctrl &= ~SDIO_BUS_WIDTH_4BIT; |
| 214 | ctrl |= SDIO_BUS_ASYNC_INT; |
| 215 | |
| 216 | ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); |
| 217 | if (ret) |
| 218 | return ret; |
| 219 | |
| 220 | mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1); |
| 221 | |
| 222 | return 0; |
| 223 | } |
| 224 | |
| 225 | /* |
Pierre Ossman | d16f577 | 2008-08-31 17:22:46 +0200 | [diff] [blame] | 226 | * Test if the card supports high-speed mode and, if so, switch to it. |
| 227 | */ |
| 228 | static int sdio_enable_hs(struct mmc_card *card) |
| 229 | { |
| 230 | int ret; |
| 231 | u8 speed; |
| 232 | |
| 233 | if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) |
| 234 | return 0; |
| 235 | |
| 236 | if (!card->cccr.high_speed) |
| 237 | return 0; |
| 238 | |
| 239 | ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); |
| 240 | if (ret) |
| 241 | return ret; |
| 242 | |
| 243 | speed |= SDIO_SPEED_EHS; |
| 244 | |
| 245 | ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); |
| 246 | if (ret) |
| 247 | return ret; |
| 248 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame^] | 249 | return 1; |
| 250 | } |
Pierre Ossman | d16f577 | 2008-08-31 17:22:46 +0200 | [diff] [blame] | 251 | |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame^] | 252 | static unsigned mmc_sdio_get_max_clock(struct mmc_card *card) |
| 253 | { |
| 254 | unsigned max_dtr; |
| 255 | |
| 256 | if (mmc_card_highspeed(card)) { |
| 257 | /* |
| 258 | * The SDIO specification doesn't mention how |
| 259 | * the CIS transfer speed register relates to |
| 260 | * high-speed, but it seems that 50 MHz is |
| 261 | * mandatory. |
| 262 | */ |
| 263 | max_dtr = 50000000; |
| 264 | } else { |
| 265 | max_dtr = card->cis.max_dtr; |
| 266 | } |
| 267 | |
| 268 | return max_dtr; |
Pierre Ossman | d16f577 | 2008-08-31 17:22:46 +0200 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /* |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 272 | * Handle the detection and initialisation of a card. |
| 273 | * |
| 274 | * In the case of a resume, "oldcard" will contain the card |
| 275 | * we're trying to reinitialise. |
| 276 | */ |
| 277 | static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 278 | struct mmc_card *oldcard, int powered_resume) |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 279 | { |
| 280 | struct mmc_card *card; |
| 281 | int err; |
| 282 | |
| 283 | BUG_ON(!host); |
| 284 | WARN_ON(!host->claimed); |
| 285 | |
| 286 | /* |
| 287 | * Inform the card of the voltage |
| 288 | */ |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 289 | if (!powered_resume) { |
| 290 | err = mmc_send_io_op_cond(host, host->ocr, &ocr); |
| 291 | if (err) |
| 292 | goto err; |
| 293 | } |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * For SPI, enable CRC as appropriate. |
| 297 | */ |
| 298 | if (mmc_host_is_spi(host)) { |
| 299 | err = mmc_spi_set_crc(host, use_spi_crc); |
| 300 | if (err) |
| 301 | goto err; |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * Allocate card structure. |
| 306 | */ |
| 307 | card = mmc_alloc_card(host, NULL); |
| 308 | if (IS_ERR(card)) { |
| 309 | err = PTR_ERR(card); |
| 310 | goto err; |
| 311 | } |
| 312 | |
| 313 | card->type = MMC_TYPE_SDIO; |
| 314 | |
| 315 | /* |
Daniel Mack | 3fcb027 | 2010-04-01 10:03:25 +0200 | [diff] [blame] | 316 | * Call the optional HC's init_card function to handle quirks. |
| 317 | */ |
| 318 | if (host->ops->init_card) |
| 319 | host->ops->init_card(host, card); |
| 320 | |
| 321 | /* |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 322 | * For native busses: set card RCA and quit open drain mode. |
| 323 | */ |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 324 | if (!powered_resume && !mmc_host_is_spi(host)) { |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 325 | err = mmc_send_relative_addr(host, &card->rca); |
| 326 | if (err) |
| 327 | goto remove; |
| 328 | |
| 329 | mmc_set_bus_mode(host, MMC_BUSMODE_PUSHPULL); |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * Select card, as all following commands rely on that. |
| 334 | */ |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 335 | if (!powered_resume && !mmc_host_is_spi(host)) { |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 336 | err = mmc_select_card(card); |
| 337 | if (err) |
| 338 | goto remove; |
| 339 | } |
| 340 | |
| 341 | /* |
| 342 | * Read the common registers. |
| 343 | */ |
| 344 | err = sdio_read_cccr(card); |
| 345 | if (err) |
| 346 | goto remove; |
| 347 | |
| 348 | /* |
| 349 | * Read the common CIS tuples. |
| 350 | */ |
| 351 | err = sdio_read_common_cis(card); |
| 352 | if (err) |
| 353 | goto remove; |
| 354 | |
| 355 | if (oldcard) { |
| 356 | int same = (card->cis.vendor == oldcard->cis.vendor && |
| 357 | card->cis.device == oldcard->cis.device); |
| 358 | mmc_remove_card(card); |
| 359 | if (!same) { |
| 360 | err = -ENOENT; |
| 361 | goto err; |
| 362 | } |
| 363 | card = oldcard; |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 364 | return 0; |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /* |
| 368 | * Switch to high-speed (if supported). |
| 369 | */ |
| 370 | err = sdio_enable_hs(card); |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame^] | 371 | if (err > 0) |
| 372 | mmc_sd_go_highspeed(card); |
| 373 | else if (err) |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 374 | goto remove; |
| 375 | |
| 376 | /* |
| 377 | * Change to the card's maximum speed. |
| 378 | */ |
Michal Miroslaw | 71578a1 | 2010-08-10 18:01:40 -0700 | [diff] [blame^] | 379 | mmc_set_clock(host, mmc_sdio_get_max_clock(card)); |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 380 | |
| 381 | /* |
| 382 | * Switch to wider bus (if supported). |
| 383 | */ |
| 384 | err = sdio_enable_wide(card); |
| 385 | if (err) |
| 386 | goto remove; |
| 387 | |
| 388 | if (!oldcard) |
| 389 | host->card = card; |
| 390 | return 0; |
| 391 | |
| 392 | remove: |
| 393 | if (!oldcard) |
| 394 | mmc_remove_card(card); |
| 395 | |
| 396 | err: |
| 397 | return err; |
| 398 | } |
| 399 | |
| 400 | /* |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 401 | * Host is being removed. Free up the current card. |
| 402 | */ |
| 403 | static void mmc_sdio_remove(struct mmc_host *host) |
| 404 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 405 | int i; |
| 406 | |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 407 | BUG_ON(!host); |
| 408 | BUG_ON(!host->card); |
| 409 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 410 | for (i = 0;i < host->card->sdio_funcs;i++) { |
| 411 | if (host->card->sdio_func[i]) { |
| 412 | sdio_remove_func(host->card->sdio_func[i]); |
| 413 | host->card->sdio_func[i] = NULL; |
| 414 | } |
| 415 | } |
| 416 | |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 417 | mmc_remove_card(host->card); |
| 418 | host->card = NULL; |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * Card detection callback from host. |
| 423 | */ |
| 424 | static void mmc_sdio_detect(struct mmc_host *host) |
| 425 | { |
| 426 | int err; |
| 427 | |
| 428 | BUG_ON(!host); |
| 429 | BUG_ON(!host->card); |
| 430 | |
| 431 | mmc_claim_host(host); |
| 432 | |
| 433 | /* |
| 434 | * Just check if our card has been removed. |
| 435 | */ |
| 436 | err = mmc_select_card(host->card); |
| 437 | |
| 438 | mmc_release_host(host); |
| 439 | |
| 440 | if (err) { |
| 441 | mmc_sdio_remove(host); |
| 442 | |
| 443 | mmc_claim_host(host); |
| 444 | mmc_detach_bus(host); |
| 445 | mmc_release_host(host); |
| 446 | } |
| 447 | } |
| 448 | |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 449 | /* |
| 450 | * SDIO suspend. We need to suspend all functions separately. |
| 451 | * Therefore all registered functions must have drivers with suspend |
| 452 | * and resume methods. Failing that we simply remove the whole card. |
| 453 | */ |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 454 | static int mmc_sdio_suspend(struct mmc_host *host) |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 455 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 456 | int i, err = 0; |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 457 | |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 458 | for (i = 0; i < host->card->sdio_funcs; i++) { |
| 459 | struct sdio_func *func = host->card->sdio_func[i]; |
| 460 | if (func && sdio_func_present(func) && func->dev.driver) { |
| 461 | const struct dev_pm_ops *pmops = func->dev.driver->pm; |
| 462 | if (!pmops || !pmops->suspend || !pmops->resume) { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 463 | /* force removal of entire card in that case */ |
| 464 | err = -ENOSYS; |
| 465 | } else |
| 466 | err = pmops->suspend(&func->dev); |
| 467 | if (err) |
| 468 | break; |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 469 | } |
| 470 | } |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 471 | while (err && --i >= 0) { |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 472 | struct sdio_func *func = host->card->sdio_func[i]; |
| 473 | if (func && sdio_func_present(func) && func->dev.driver) { |
| 474 | const struct dev_pm_ops *pmops = func->dev.driver->pm; |
| 475 | pmops->resume(&func->dev); |
| 476 | } |
| 477 | } |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 478 | |
Daniel Drake | 6b5eda3 | 2010-03-05 13:43:34 -0800 | [diff] [blame] | 479 | if (!err && host->pm_flags & MMC_PM_KEEP_POWER) { |
| 480 | mmc_claim_host(host); |
| 481 | sdio_disable_wide(host->card); |
| 482 | mmc_release_host(host); |
| 483 | } |
| 484 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 485 | return err; |
| 486 | } |
| 487 | |
| 488 | static int mmc_sdio_resume(struct mmc_host *host) |
| 489 | { |
| 490 | int i, err; |
| 491 | |
| 492 | BUG_ON(!host); |
| 493 | BUG_ON(!host->card); |
| 494 | |
| 495 | /* Basic card reinitialization. */ |
| 496 | mmc_claim_host(host); |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 497 | err = mmc_sdio_init_card(host, host->ocr, host->card, |
| 498 | (host->pm_flags & MMC_PM_KEEP_POWER)); |
Daniel Drake | 6b5eda3 | 2010-03-05 13:43:34 -0800 | [diff] [blame] | 499 | if (!err) |
| 500 | /* We may have switched to 1-bit mode during suspend. */ |
| 501 | err = sdio_enable_wide(host->card); |
Nicolas Pitre | 4021684 | 2010-03-05 13:43:34 -0800 | [diff] [blame] | 502 | if (!err && host->sdio_irqs) |
| 503 | mmc_signal_sdio_irq(host); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 504 | mmc_release_host(host); |
| 505 | |
| 506 | /* |
| 507 | * If the card looked to be the same as before suspending, then |
| 508 | * we proceed to resume all card functions. If one of them returns |
| 509 | * an error then we simply return that error to the core and the |
| 510 | * card will be redetected as new. It is the responsibility of |
| 511 | * the function driver to perform further tests with the extra |
| 512 | * knowledge it has of the card to confirm the card is indeed the |
| 513 | * same as before suspending (same MAC address for network cards, |
| 514 | * etc.) and return an error otherwise. |
| 515 | */ |
| 516 | for (i = 0; !err && i < host->card->sdio_funcs; i++) { |
| 517 | struct sdio_func *func = host->card->sdio_func[i]; |
| 518 | if (func && sdio_func_present(func) && func->dev.driver) { |
| 519 | const struct dev_pm_ops *pmops = func->dev.driver->pm; |
| 520 | err = pmops->resume(&func->dev); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | return err; |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 525 | } |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 526 | |
| 527 | static const struct mmc_bus_ops mmc_sdio_ops = { |
| 528 | .remove = mmc_sdio_remove, |
| 529 | .detect = mmc_sdio_detect, |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 530 | .suspend = mmc_sdio_suspend, |
| 531 | .resume = mmc_sdio_resume, |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 532 | }; |
| 533 | |
| 534 | |
| 535 | /* |
| 536 | * Starting point for SDIO card init. |
| 537 | */ |
| 538 | int mmc_attach_sdio(struct mmc_host *host, u32 ocr) |
| 539 | { |
| 540 | int err; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 541 | int i, funcs; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 542 | struct mmc_card *card; |
| 543 | |
| 544 | BUG_ON(!host); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 545 | WARN_ON(!host->claimed); |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 546 | |
| 547 | mmc_attach_bus(host, &mmc_sdio_ops); |
| 548 | |
| 549 | /* |
| 550 | * Sanity check the voltages that the card claims to |
| 551 | * support. |
| 552 | */ |
| 553 | if (ocr & 0x7F) { |
| 554 | printk(KERN_WARNING "%s: card claims to support voltages " |
| 555 | "below the defined range. These will be ignored.\n", |
| 556 | mmc_hostname(host)); |
| 557 | ocr &= ~0x7F; |
| 558 | } |
| 559 | |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 560 | host->ocr = mmc_select_voltage(host, ocr); |
| 561 | |
| 562 | /* |
| 563 | * Can we support the voltage(s) of the card(s)? |
| 564 | */ |
| 565 | if (!host->ocr) { |
| 566 | err = -EINVAL; |
| 567 | goto err; |
| 568 | } |
| 569 | |
| 570 | /* |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 571 | * Detect and init the card. |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 572 | */ |
Chris Ball | 3bca4cf7 | 2010-03-05 13:43:33 -0800 | [diff] [blame] | 573 | err = mmc_sdio_init_card(host, host->ocr, NULL, 0); |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 574 | if (err) |
| 575 | goto err; |
Nicolas Pitre | 17d33e1 | 2009-09-22 16:45:28 -0700 | [diff] [blame] | 576 | card = host->card; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 577 | |
| 578 | /* |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 579 | * The number of functions on the card is encoded inside |
| 580 | * the ocr. |
| 581 | */ |
Matt Fleming | e881279 | 2009-12-17 15:27:18 -0800 | [diff] [blame] | 582 | funcs = (ocr & 0x70000000) >> 28; |
| 583 | card->sdio_funcs = 0; |
Pierre Ossman | 4ff6471 | 2007-07-30 18:23:53 +0200 | [diff] [blame] | 584 | |
| 585 | /* |
Ohad Ben-Cohen | 006ebd5 | 2009-09-22 16:45:07 -0700 | [diff] [blame] | 586 | * If needed, disconnect card detection pull-up resistor. |
| 587 | */ |
| 588 | err = sdio_disable_cd(card); |
| 589 | if (err) |
| 590 | goto remove; |
| 591 | |
| 592 | /* |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 593 | * Initialize (but don't add) all present functions. |
| 594 | */ |
Matt Fleming | e881279 | 2009-12-17 15:27:18 -0800 | [diff] [blame] | 595 | for (i = 0; i < funcs; i++, card->sdio_funcs++) { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 596 | err = sdio_init_func(host->card, i + 1); |
| 597 | if (err) |
| 598 | goto remove; |
| 599 | } |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 600 | |
| 601 | mmc_release_host(host); |
| 602 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 603 | /* |
| 604 | * First add the card to the driver model... |
| 605 | */ |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 606 | err = mmc_add_card(host->card); |
| 607 | if (err) |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 608 | goto remove_added; |
| 609 | |
| 610 | /* |
| 611 | * ...then the SDIO functions. |
| 612 | */ |
| 613 | for (i = 0;i < funcs;i++) { |
| 614 | err = sdio_add_func(host->card->sdio_func[i]); |
| 615 | if (err) |
| 616 | goto remove_added; |
| 617 | } |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 618 | |
| 619 | return 0; |
| 620 | |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 621 | |
| 622 | remove_added: |
| 623 | /* Remove without lock if the device has been added. */ |
| 624 | mmc_sdio_remove(host); |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 625 | mmc_claim_host(host); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 626 | remove: |
| 627 | /* And with lock if it hasn't been added. */ |
| 628 | if (host->card) |
| 629 | mmc_sdio_remove(host); |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 630 | err: |
| 631 | mmc_detach_bus(host); |
| 632 | mmc_release_host(host); |
| 633 | |
| 634 | printk(KERN_ERR "%s: error %d whilst initialising SDIO card\n", |
| 635 | mmc_hostname(host), err); |
| 636 | |
| 637 | return err; |
| 638 | } |
| 639 | |