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