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