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