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 #include <linux/pm_runtime.h> 14 15 #include <linux/mmc/host.h> 16 #include <linux/mmc/card.h> 17 #include <linux/mmc/mmc.h> 18 #include <linux/mmc/sdio.h> 19 #include <linux/mmc/sdio_func.h> 20 #include <linux/mmc/sdio_ids.h> 21 22 #include "core.h" 23 #include "bus.h" 24 #include "sd.h" 25 #include "sdio_bus.h" 26 #include "mmc_ops.h" 27 #include "sd_ops.h" 28 #include "sdio_ops.h" 29 #include "sdio_cis.h" 30 31 static int sdio_read_fbr(struct sdio_func *func) 32 { 33 int ret; 34 unsigned char data; 35 36 if (mmc_card_nonstd_func_interface(func->card)) { 37 func->class = SDIO_CLASS_NONE; 38 return 0; 39 } 40 41 ret = mmc_io_rw_direct(func->card, 0, 0, 42 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF, 0, &data); 43 if (ret) 44 goto out; 45 46 data &= 0x0f; 47 48 if (data == 0x0f) { 49 ret = mmc_io_rw_direct(func->card, 0, 0, 50 SDIO_FBR_BASE(func->num) + SDIO_FBR_STD_IF_EXT, 0, &data); 51 if (ret) 52 goto out; 53 } 54 55 func->class = data; 56 57 out: 58 return ret; 59 } 60 61 static int sdio_init_func(struct mmc_card *card, unsigned int fn) 62 { 63 int ret; 64 struct sdio_func *func; 65 66 BUG_ON(fn > SDIO_MAX_FUNCS); 67 68 func = sdio_alloc_func(card); 69 if (IS_ERR(func)) 70 return PTR_ERR(func); 71 72 func->num = fn; 73 74 if (!(card->quirks & MMC_QUIRK_NONSTD_SDIO)) { 75 ret = sdio_read_fbr(func); 76 if (ret) 77 goto fail; 78 79 ret = sdio_read_func_cis(func); 80 if (ret) 81 goto fail; 82 } else { 83 func->vendor = func->card->cis.vendor; 84 func->device = func->card->cis.device; 85 func->max_blksize = func->card->cis.blksize; 86 } 87 88 card->sdio_func[fn - 1] = func; 89 90 return 0; 91 92 fail: 93 /* 94 * It is okay to remove the function here even though we hold 95 * the host lock as we haven't registered the device yet. 96 */ 97 sdio_remove_func(func); 98 return ret; 99 } 100 101 static int sdio_read_cccr(struct mmc_card *card) 102 { 103 int ret; 104 int cccr_vsn; 105 unsigned char data; 106 unsigned char speed; 107 108 memset(&card->cccr, 0, sizeof(struct sdio_cccr)); 109 110 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CCCR, 0, &data); 111 if (ret) 112 goto out; 113 114 cccr_vsn = data & 0x0f; 115 116 if (cccr_vsn > SDIO_CCCR_REV_3_00) { 117 pr_err("%s: unrecognised CCCR structure version %d\n", 118 mmc_hostname(card->host), cccr_vsn); 119 return -EINVAL; 120 } 121 122 card->cccr.sdio_vsn = (data & 0xf0) >> 4; 123 124 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_CAPS, 0, &data); 125 if (ret) 126 goto out; 127 128 if (data & SDIO_CCCR_CAP_SMB) 129 card->cccr.multi_block = 1; 130 if (data & SDIO_CCCR_CAP_LSC) 131 card->cccr.low_speed = 1; 132 if (data & SDIO_CCCR_CAP_4BLS) 133 card->cccr.wide_bus = 1; 134 135 if (cccr_vsn >= SDIO_CCCR_REV_1_10) { 136 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_POWER, 0, &data); 137 if (ret) 138 goto out; 139 140 if (data & SDIO_POWER_SMPC) 141 card->cccr.high_power = 1; 142 } 143 144 if (cccr_vsn >= SDIO_CCCR_REV_1_20) { 145 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 146 if (ret) 147 goto out; 148 149 card->scr.sda_spec3 = 0; 150 card->sw_caps.sd3_bus_mode = 0; 151 card->sw_caps.sd3_drv_type = 0; 152 if (cccr_vsn >= SDIO_CCCR_REV_3_00) { 153 card->scr.sda_spec3 = 1; 154 ret = mmc_io_rw_direct(card, 0, 0, 155 SDIO_CCCR_UHS, 0, &data); 156 if (ret) 157 goto out; 158 159 if (card->host->caps & 160 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 161 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | 162 MMC_CAP_UHS_DDR50)) { 163 if (data & SDIO_UHS_DDR50) 164 card->sw_caps.sd3_bus_mode 165 |= SD_MODE_UHS_DDR50; 166 167 if (data & SDIO_UHS_SDR50) 168 card->sw_caps.sd3_bus_mode 169 |= SD_MODE_UHS_SDR50; 170 171 if (data & SDIO_UHS_SDR104) 172 card->sw_caps.sd3_bus_mode 173 |= SD_MODE_UHS_SDR104; 174 } 175 176 ret = mmc_io_rw_direct(card, 0, 0, 177 SDIO_CCCR_DRIVE_STRENGTH, 0, &data); 178 if (ret) 179 goto out; 180 181 if (data & SDIO_DRIVE_SDTA) 182 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_A; 183 if (data & SDIO_DRIVE_SDTC) 184 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_C; 185 if (data & SDIO_DRIVE_SDTD) 186 card->sw_caps.sd3_drv_type |= SD_DRIVER_TYPE_D; 187 } 188 189 /* if no uhs mode ensure we check for high speed */ 190 if (!card->sw_caps.sd3_bus_mode) { 191 if (speed & SDIO_SPEED_SHS) { 192 card->cccr.high_speed = 1; 193 card->sw_caps.hs_max_dtr = 50000000; 194 } else { 195 card->cccr.high_speed = 0; 196 card->sw_caps.hs_max_dtr = 25000000; 197 } 198 } 199 } 200 201 out: 202 return ret; 203 } 204 205 static int sdio_enable_wide(struct mmc_card *card) 206 { 207 int ret; 208 u8 ctrl; 209 210 if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 211 return 0; 212 213 if (card->cccr.low_speed && !card->cccr.wide_bus) 214 return 0; 215 216 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 217 if (ret) 218 return ret; 219 220 ctrl |= SDIO_BUS_WIDTH_4BIT; 221 222 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 223 if (ret) 224 return ret; 225 226 return 1; 227 } 228 229 /* 230 * If desired, disconnect the pull-up resistor on CD/DAT[3] (pin 1) 231 * of the card. This may be required on certain setups of boards, 232 * controllers and embedded sdio device which do not need the card's 233 * pull-up. As a result, card detection is disabled and power is saved. 234 */ 235 static int sdio_disable_cd(struct mmc_card *card) 236 { 237 int ret; 238 u8 ctrl; 239 240 if (!mmc_card_disable_cd(card)) 241 return 0; 242 243 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 244 if (ret) 245 return ret; 246 247 ctrl |= SDIO_BUS_CD_DISABLE; 248 249 return mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 250 } 251 252 /* 253 * Devices that remain active during a system suspend are 254 * put back into 1-bit mode. 255 */ 256 static int sdio_disable_wide(struct mmc_card *card) 257 { 258 int ret; 259 u8 ctrl; 260 261 if (!(card->host->caps & MMC_CAP_4_BIT_DATA)) 262 return 0; 263 264 if (card->cccr.low_speed && !card->cccr.wide_bus) 265 return 0; 266 267 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_IF, 0, &ctrl); 268 if (ret) 269 return ret; 270 271 if (!(ctrl & SDIO_BUS_WIDTH_4BIT)) 272 return 0; 273 274 ctrl &= ~SDIO_BUS_WIDTH_4BIT; 275 ctrl |= SDIO_BUS_ASYNC_INT; 276 277 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_IF, ctrl, NULL); 278 if (ret) 279 return ret; 280 281 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_1); 282 283 return 0; 284 } 285 286 287 static int sdio_enable_4bit_bus(struct mmc_card *card) 288 { 289 int err; 290 291 if (card->type == MMC_TYPE_SDIO) 292 return sdio_enable_wide(card); 293 294 if ((card->host->caps & MMC_CAP_4_BIT_DATA) && 295 (card->scr.bus_widths & SD_SCR_BUS_WIDTH_4)) { 296 err = mmc_app_set_bus_width(card, MMC_BUS_WIDTH_4); 297 if (err) 298 return err; 299 } else 300 return 0; 301 302 err = sdio_enable_wide(card); 303 if (err <= 0) 304 mmc_app_set_bus_width(card, MMC_BUS_WIDTH_1); 305 306 return err; 307 } 308 309 310 /* 311 * Test if the card supports high-speed mode and, if so, switch to it. 312 */ 313 static int mmc_sdio_switch_hs(struct mmc_card *card, int enable) 314 { 315 int ret; 316 u8 speed; 317 318 if (!(card->host->caps & MMC_CAP_SD_HIGHSPEED)) 319 return 0; 320 321 if (!card->cccr.high_speed) 322 return 0; 323 324 ret = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 325 if (ret) 326 return ret; 327 328 if (enable) 329 speed |= SDIO_SPEED_EHS; 330 else 331 speed &= ~SDIO_SPEED_EHS; 332 333 ret = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 334 if (ret) 335 return ret; 336 337 return 1; 338 } 339 340 /* 341 * Enable SDIO/combo card's high-speed mode. Return 0/1 if [not]supported. 342 */ 343 static int sdio_enable_hs(struct mmc_card *card) 344 { 345 int ret; 346 347 ret = mmc_sdio_switch_hs(card, true); 348 if (ret <= 0 || card->type == MMC_TYPE_SDIO) 349 return ret; 350 351 ret = mmc_sd_switch_hs(card); 352 if (ret <= 0) 353 mmc_sdio_switch_hs(card, false); 354 355 return ret; 356 } 357 358 static unsigned mmc_sdio_get_max_clock(struct mmc_card *card) 359 { 360 unsigned max_dtr; 361 362 if (mmc_card_highspeed(card)) { 363 /* 364 * The SDIO specification doesn't mention how 365 * the CIS transfer speed register relates to 366 * high-speed, but it seems that 50 MHz is 367 * mandatory. 368 */ 369 max_dtr = 50000000; 370 } else { 371 max_dtr = card->cis.max_dtr; 372 } 373 374 if (card->type == MMC_TYPE_SD_COMBO) 375 max_dtr = min(max_dtr, mmc_sd_get_max_clock(card)); 376 377 return max_dtr; 378 } 379 380 static unsigned char host_drive_to_sdio_drive(int host_strength) 381 { 382 switch (host_strength) { 383 case MMC_SET_DRIVER_TYPE_A: 384 return SDIO_DTSx_SET_TYPE_A; 385 case MMC_SET_DRIVER_TYPE_B: 386 return SDIO_DTSx_SET_TYPE_B; 387 case MMC_SET_DRIVER_TYPE_C: 388 return SDIO_DTSx_SET_TYPE_C; 389 case MMC_SET_DRIVER_TYPE_D: 390 return SDIO_DTSx_SET_TYPE_D; 391 default: 392 return SDIO_DTSx_SET_TYPE_B; 393 } 394 } 395 396 static void sdio_select_driver_type(struct mmc_card *card) 397 { 398 int host_drv_type = SD_DRIVER_TYPE_B; 399 int card_drv_type = SD_DRIVER_TYPE_B; 400 int drive_strength; 401 unsigned char card_strength; 402 int err; 403 404 /* 405 * If the host doesn't support any of the Driver Types A,C or D, 406 * or there is no board specific handler then default Driver 407 * Type B is used. 408 */ 409 if (!(card->host->caps & 410 (MMC_CAP_DRIVER_TYPE_A | 411 MMC_CAP_DRIVER_TYPE_C | 412 MMC_CAP_DRIVER_TYPE_D))) 413 return; 414 415 if (!card->host->ops->select_drive_strength) 416 return; 417 418 if (card->host->caps & MMC_CAP_DRIVER_TYPE_A) 419 host_drv_type |= SD_DRIVER_TYPE_A; 420 421 if (card->host->caps & MMC_CAP_DRIVER_TYPE_C) 422 host_drv_type |= SD_DRIVER_TYPE_C; 423 424 if (card->host->caps & MMC_CAP_DRIVER_TYPE_D) 425 host_drv_type |= SD_DRIVER_TYPE_D; 426 427 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_A) 428 card_drv_type |= SD_DRIVER_TYPE_A; 429 430 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_C) 431 card_drv_type |= SD_DRIVER_TYPE_C; 432 433 if (card->sw_caps.sd3_drv_type & SD_DRIVER_TYPE_D) 434 card_drv_type |= SD_DRIVER_TYPE_D; 435 436 /* 437 * The drive strength that the hardware can support 438 * depends on the board design. Pass the appropriate 439 * information and let the hardware specific code 440 * return what is possible given the options 441 */ 442 drive_strength = card->host->ops->select_drive_strength( 443 card->sw_caps.uhs_max_dtr, 444 host_drv_type, card_drv_type); 445 446 /* if error just use default for drive strength B */ 447 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_DRIVE_STRENGTH, 0, 448 &card_strength); 449 if (err) 450 return; 451 452 card_strength &= ~(SDIO_DRIVE_DTSx_MASK<<SDIO_DRIVE_DTSx_SHIFT); 453 card_strength |= host_drive_to_sdio_drive(drive_strength); 454 455 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_DRIVE_STRENGTH, 456 card_strength, NULL); 457 458 /* if error default to drive strength B */ 459 if (!err) 460 mmc_set_driver_type(card->host, drive_strength); 461 } 462 463 464 static int sdio_set_bus_speed_mode(struct mmc_card *card) 465 { 466 unsigned int bus_speed, timing; 467 int err; 468 unsigned char speed; 469 470 /* 471 * If the host doesn't support any of the UHS-I modes, fallback on 472 * default speed. 473 */ 474 if (!(card->host->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 475 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_DDR50))) 476 return 0; 477 478 bus_speed = SDIO_SPEED_SDR12; 479 timing = MMC_TIMING_UHS_SDR12; 480 if ((card->host->caps & MMC_CAP_UHS_SDR104) && 481 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR104)) { 482 bus_speed = SDIO_SPEED_SDR104; 483 timing = MMC_TIMING_UHS_SDR104; 484 card->sw_caps.uhs_max_dtr = UHS_SDR104_MAX_DTR; 485 } else if ((card->host->caps & MMC_CAP_UHS_DDR50) && 486 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_DDR50)) { 487 bus_speed = SDIO_SPEED_DDR50; 488 timing = MMC_TIMING_UHS_DDR50; 489 card->sw_caps.uhs_max_dtr = UHS_DDR50_MAX_DTR; 490 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 491 MMC_CAP_UHS_SDR50)) && (card->sw_caps.sd3_bus_mode & 492 SD_MODE_UHS_SDR50)) { 493 bus_speed = SDIO_SPEED_SDR50; 494 timing = MMC_TIMING_UHS_SDR50; 495 card->sw_caps.uhs_max_dtr = UHS_SDR50_MAX_DTR; 496 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 497 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25)) && 498 (card->sw_caps.sd3_bus_mode & SD_MODE_UHS_SDR25)) { 499 bus_speed = SDIO_SPEED_SDR25; 500 timing = MMC_TIMING_UHS_SDR25; 501 card->sw_caps.uhs_max_dtr = UHS_SDR25_MAX_DTR; 502 } else if ((card->host->caps & (MMC_CAP_UHS_SDR104 | 503 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR25 | 504 MMC_CAP_UHS_SDR12)) && (card->sw_caps.sd3_bus_mode & 505 SD_MODE_UHS_SDR12)) { 506 bus_speed = SDIO_SPEED_SDR12; 507 timing = MMC_TIMING_UHS_SDR12; 508 card->sw_caps.uhs_max_dtr = UHS_SDR12_MAX_DTR; 509 } 510 511 err = mmc_io_rw_direct(card, 0, 0, SDIO_CCCR_SPEED, 0, &speed); 512 if (err) 513 return err; 514 515 speed &= ~SDIO_SPEED_BSS_MASK; 516 speed |= bus_speed; 517 err = mmc_io_rw_direct(card, 1, 0, SDIO_CCCR_SPEED, speed, NULL); 518 if (err) 519 return err; 520 521 if (bus_speed) { 522 mmc_set_timing(card->host, timing); 523 mmc_set_clock(card->host, card->sw_caps.uhs_max_dtr); 524 } 525 526 return 0; 527 } 528 529 /* 530 * UHS-I specific initialization procedure 531 */ 532 static int mmc_sdio_init_uhs_card(struct mmc_card *card) 533 { 534 int err; 535 536 if (!card->scr.sda_spec3) 537 return 0; 538 539 /* 540 * Switch to wider bus (if supported). 541 */ 542 if (card->host->caps & MMC_CAP_4_BIT_DATA) { 543 err = sdio_enable_4bit_bus(card); 544 if (err > 0) { 545 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 546 err = 0; 547 } 548 } 549 550 /* Set the driver strength for the card */ 551 sdio_select_driver_type(card); 552 553 /* Set bus speed mode of the card */ 554 err = sdio_set_bus_speed_mode(card); 555 if (err) 556 goto out; 557 558 /* Initialize and start re-tuning timer */ 559 if (!mmc_host_is_spi(card->host) && card->host->ops->execute_tuning) 560 err = card->host->ops->execute_tuning(card->host, 561 MMC_SEND_TUNING_BLOCK); 562 563 out: 564 565 return err; 566 } 567 568 /* 569 * Handle the detection and initialisation of a card. 570 * 571 * In the case of a resume, "oldcard" will contain the card 572 * we're trying to reinitialise. 573 */ 574 static int mmc_sdio_init_card(struct mmc_host *host, u32 ocr, 575 struct mmc_card *oldcard, int powered_resume) 576 { 577 struct mmc_card *card; 578 int err; 579 580 BUG_ON(!host); 581 WARN_ON(!host->claimed); 582 583 /* 584 * Inform the card of the voltage 585 */ 586 if (!powered_resume) { 587 err = mmc_send_io_op_cond(host, host->ocr, &ocr); 588 if (err) 589 goto err; 590 } 591 592 /* 593 * For SPI, enable CRC as appropriate. 594 */ 595 if (mmc_host_is_spi(host)) { 596 err = mmc_spi_set_crc(host, use_spi_crc); 597 if (err) 598 goto err; 599 } 600 601 /* 602 * Allocate card structure. 603 */ 604 card = mmc_alloc_card(host, NULL); 605 if (IS_ERR(card)) { 606 err = PTR_ERR(card); 607 goto err; 608 } 609 610 if ((ocr & R4_MEMORY_PRESENT) && 611 mmc_sd_get_cid(host, host->ocr & ocr, card->raw_cid, NULL) == 0) { 612 card->type = MMC_TYPE_SD_COMBO; 613 614 if (oldcard && (oldcard->type != MMC_TYPE_SD_COMBO || 615 memcmp(card->raw_cid, oldcard->raw_cid, sizeof(card->raw_cid)) != 0)) { 616 mmc_remove_card(card); 617 return -ENOENT; 618 } 619 } else { 620 card->type = MMC_TYPE_SDIO; 621 622 if (oldcard && oldcard->type != MMC_TYPE_SDIO) { 623 mmc_remove_card(card); 624 return -ENOENT; 625 } 626 } 627 628 /* 629 * Call the optional HC's init_card function to handle quirks. 630 */ 631 if (host->ops->init_card) 632 host->ops->init_card(host, card); 633 634 /* 635 * If the host and card support UHS-I mode request the card 636 * to switch to 1.8V signaling level. No 1.8v signalling if 637 * UHS mode is not enabled to maintain compatibilty and some 638 * systems that claim 1.8v signalling in fact do not support 639 * it. 640 */ 641 if ((ocr & R4_18V_PRESENT) && 642 (host->caps & 643 (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 | 644 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 | 645 MMC_CAP_UHS_DDR50))) { 646 err = mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180, 647 true); 648 if (err) { 649 ocr &= ~R4_18V_PRESENT; 650 host->ocr &= ~R4_18V_PRESENT; 651 } 652 err = 0; 653 } else { 654 ocr &= ~R4_18V_PRESENT; 655 host->ocr &= ~R4_18V_PRESENT; 656 } 657 658 /* 659 * For native busses: set card RCA and quit open drain mode. 660 */ 661 if (!powered_resume && !mmc_host_is_spi(host)) { 662 err = mmc_send_relative_addr(host, &card->rca); 663 if (err) 664 goto remove; 665 666 /* 667 * Update oldcard with the new RCA received from the SDIO 668 * device -- we're doing this so that it's updated in the 669 * "card" struct when oldcard overwrites that later. 670 */ 671 if (oldcard) 672 oldcard->rca = card->rca; 673 } 674 675 /* 676 * Read CSD, before selecting the card 677 */ 678 if (!oldcard && card->type == MMC_TYPE_SD_COMBO) { 679 err = mmc_sd_get_csd(host, card); 680 if (err) 681 return err; 682 683 mmc_decode_cid(card); 684 } 685 686 /* 687 * Select card, as all following commands rely on that. 688 */ 689 if (!powered_resume && !mmc_host_is_spi(host)) { 690 err = mmc_select_card(card); 691 if (err) 692 goto remove; 693 } 694 695 if (card->quirks & MMC_QUIRK_NONSTD_SDIO) { 696 /* 697 * This is non-standard SDIO device, meaning it doesn't 698 * have any CIA (Common I/O area) registers present. 699 * It's host's responsibility to fill cccr and cis 700 * structures in init_card(). 701 */ 702 mmc_set_clock(host, card->cis.max_dtr); 703 704 if (card->cccr.high_speed) { 705 mmc_card_set_highspeed(card); 706 mmc_set_timing(card->host, MMC_TIMING_SD_HS); 707 } 708 709 goto finish; 710 } 711 712 /* 713 * Read the common registers. 714 */ 715 err = sdio_read_cccr(card); 716 if (err) 717 goto remove; 718 719 /* 720 * Read the common CIS tuples. 721 */ 722 err = sdio_read_common_cis(card); 723 if (err) 724 goto remove; 725 726 if (oldcard) { 727 int same = (card->cis.vendor == oldcard->cis.vendor && 728 card->cis.device == oldcard->cis.device); 729 mmc_remove_card(card); 730 if (!same) 731 return -ENOENT; 732 733 card = oldcard; 734 } 735 mmc_fixup_device(card, NULL); 736 737 if (card->type == MMC_TYPE_SD_COMBO) { 738 err = mmc_sd_setup_card(host, card, oldcard != NULL); 739 /* handle as SDIO-only card if memory init failed */ 740 if (err) { 741 mmc_go_idle(host); 742 if (mmc_host_is_spi(host)) 743 /* should not fail, as it worked previously */ 744 mmc_spi_set_crc(host, use_spi_crc); 745 card->type = MMC_TYPE_SDIO; 746 } else 747 card->dev.type = &sd_type; 748 } 749 750 /* 751 * If needed, disconnect card detection pull-up resistor. 752 */ 753 err = sdio_disable_cd(card); 754 if (err) 755 goto remove; 756 757 /* Initialization sequence for UHS-I cards */ 758 /* Only if card supports 1.8v and UHS signaling */ 759 if ((ocr & R4_18V_PRESENT) && card->sw_caps.sd3_bus_mode) { 760 err = mmc_sdio_init_uhs_card(card); 761 if (err) 762 goto remove; 763 764 /* Card is an ultra-high-speed card */ 765 mmc_card_set_uhs(card); 766 } else { 767 /* 768 * Switch to high-speed (if supported). 769 */ 770 err = sdio_enable_hs(card); 771 if (err > 0) 772 mmc_sd_go_highspeed(card); 773 else if (err) 774 goto remove; 775 776 /* 777 * Change to the card's maximum speed. 778 */ 779 mmc_set_clock(host, mmc_sdio_get_max_clock(card)); 780 781 /* 782 * Switch to wider bus (if supported). 783 */ 784 err = sdio_enable_4bit_bus(card); 785 if (err > 0) 786 mmc_set_bus_width(card->host, MMC_BUS_WIDTH_4); 787 else if (err) 788 goto remove; 789 } 790 finish: 791 if (!oldcard) 792 host->card = card; 793 return 0; 794 795 remove: 796 if (!oldcard) 797 mmc_remove_card(card); 798 799 err: 800 return err; 801 } 802 803 /* 804 * Host is being removed. Free up the current card. 805 */ 806 static void mmc_sdio_remove(struct mmc_host *host) 807 { 808 int i; 809 810 BUG_ON(!host); 811 BUG_ON(!host->card); 812 813 for (i = 0;i < host->card->sdio_funcs;i++) { 814 if (host->card->sdio_func[i]) { 815 sdio_remove_func(host->card->sdio_func[i]); 816 host->card->sdio_func[i] = NULL; 817 } 818 } 819 820 mmc_remove_card(host->card); 821 host->card = NULL; 822 } 823 824 /* 825 * Card detection - card is alive. 826 */ 827 static int mmc_sdio_alive(struct mmc_host *host) 828 { 829 return mmc_select_card(host->card); 830 } 831 832 /* 833 * Card detection callback from host. 834 */ 835 static void mmc_sdio_detect(struct mmc_host *host) 836 { 837 int err; 838 839 BUG_ON(!host); 840 BUG_ON(!host->card); 841 842 /* Make sure card is powered before detecting it */ 843 if (host->caps & MMC_CAP_POWER_OFF_CARD) { 844 err = pm_runtime_get_sync(&host->card->dev); 845 if (err < 0) 846 goto out; 847 } 848 849 mmc_claim_host(host); 850 851 /* 852 * Just check if our card has been removed. 853 */ 854 err = _mmc_detect_card_removed(host); 855 856 mmc_release_host(host); 857 858 /* 859 * Tell PM core it's OK to power off the card now. 860 * 861 * The _sync variant is used in order to ensure that the card 862 * is left powered off in case an error occurred, and the card 863 * is going to be removed. 864 * 865 * Since there is no specific reason to believe a new user 866 * is about to show up at this point, the _sync variant is 867 * desirable anyway. 868 */ 869 if (host->caps & MMC_CAP_POWER_OFF_CARD) 870 pm_runtime_put_sync(&host->card->dev); 871 872 out: 873 if (err) { 874 mmc_sdio_remove(host); 875 876 mmc_claim_host(host); 877 mmc_detach_bus(host); 878 mmc_power_off(host); 879 mmc_release_host(host); 880 } 881 } 882 883 /* 884 * SDIO suspend. We need to suspend all functions separately. 885 * Therefore all registered functions must have drivers with suspend 886 * and resume methods. Failing that we simply remove the whole card. 887 */ 888 static int mmc_sdio_suspend(struct mmc_host *host) 889 { 890 int i, err = 0; 891 892 for (i = 0; i < host->card->sdio_funcs; i++) { 893 struct sdio_func *func = host->card->sdio_func[i]; 894 if (func && sdio_func_present(func) && func->dev.driver) { 895 const struct dev_pm_ops *pmops = func->dev.driver->pm; 896 if (!pmops || !pmops->suspend || !pmops->resume) { 897 /* force removal of entire card in that case */ 898 err = -ENOSYS; 899 } else 900 err = pmops->suspend(&func->dev); 901 if (err) 902 break; 903 } 904 } 905 while (err && --i >= 0) { 906 struct sdio_func *func = host->card->sdio_func[i]; 907 if (func && sdio_func_present(func) && func->dev.driver) { 908 const struct dev_pm_ops *pmops = func->dev.driver->pm; 909 pmops->resume(&func->dev); 910 } 911 } 912 913 if (!err && mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 914 mmc_claim_host(host); 915 sdio_disable_wide(host->card); 916 mmc_release_host(host); 917 } 918 919 return err; 920 } 921 922 static int mmc_sdio_resume(struct mmc_host *host) 923 { 924 int i, err = 0; 925 926 BUG_ON(!host); 927 BUG_ON(!host->card); 928 929 /* Basic card reinitialization. */ 930 mmc_claim_host(host); 931 932 /* No need to reinitialize powered-resumed nonremovable cards */ 933 if (mmc_card_is_removable(host) || !mmc_card_keep_power(host)) 934 err = mmc_sdio_init_card(host, host->ocr, host->card, 935 mmc_card_keep_power(host)); 936 else if (mmc_card_keep_power(host) && mmc_card_wake_sdio_irq(host)) { 937 /* We may have switched to 1-bit mode during suspend */ 938 err = sdio_enable_4bit_bus(host->card); 939 if (err > 0) { 940 mmc_set_bus_width(host, MMC_BUS_WIDTH_4); 941 err = 0; 942 } 943 } 944 945 if (!err && host->sdio_irqs) 946 mmc_signal_sdio_irq(host); 947 mmc_release_host(host); 948 949 /* 950 * If the card looked to be the same as before suspending, then 951 * we proceed to resume all card functions. If one of them returns 952 * an error then we simply return that error to the core and the 953 * card will be redetected as new. It is the responsibility of 954 * the function driver to perform further tests with the extra 955 * knowledge it has of the card to confirm the card is indeed the 956 * same as before suspending (same MAC address for network cards, 957 * etc.) and return an error otherwise. 958 */ 959 for (i = 0; !err && i < host->card->sdio_funcs; i++) { 960 struct sdio_func *func = host->card->sdio_func[i]; 961 if (func && sdio_func_present(func) && func->dev.driver) { 962 const struct dev_pm_ops *pmops = func->dev.driver->pm; 963 err = pmops->resume(&func->dev); 964 } 965 } 966 967 return err; 968 } 969 970 static int mmc_sdio_power_restore(struct mmc_host *host) 971 { 972 int ret; 973 u32 ocr; 974 975 BUG_ON(!host); 976 BUG_ON(!host->card); 977 978 mmc_claim_host(host); 979 980 /* 981 * Reset the card by performing the same steps that are taken by 982 * mmc_rescan_try_freq() and mmc_attach_sdio() during a "normal" probe. 983 * 984 * sdio_reset() is technically not needed. Having just powered up the 985 * hardware, it should already be in reset state. However, some 986 * platforms (such as SD8686 on OLPC) do not instantly cut power, 987 * meaning that a reset is required when restoring power soon after 988 * powering off. It is harmless in other cases. 989 * 990 * The CMD5 reset (mmc_send_io_op_cond()), according to the SDIO spec, 991 * is not necessary for non-removable cards. However, it is required 992 * for OLPC SD8686 (which expects a [CMD5,5,3,7] init sequence), and 993 * harmless in other situations. 994 * 995 * With these steps taken, mmc_select_voltage() is also required to 996 * restore the correct voltage setting of the card. 997 */ 998 sdio_reset(host); 999 mmc_go_idle(host); 1000 mmc_send_if_cond(host, host->ocr_avail); 1001 1002 ret = mmc_send_io_op_cond(host, 0, &ocr); 1003 if (ret) 1004 goto out; 1005 1006 if (host->ocr_avail_sdio) 1007 host->ocr_avail = host->ocr_avail_sdio; 1008 1009 host->ocr = mmc_select_voltage(host, ocr & ~0x7F); 1010 if (!host->ocr) { 1011 ret = -EINVAL; 1012 goto out; 1013 } 1014 1015 ret = mmc_sdio_init_card(host, host->ocr, host->card, 1016 mmc_card_keep_power(host)); 1017 if (!ret && host->sdio_irqs) 1018 mmc_signal_sdio_irq(host); 1019 1020 out: 1021 mmc_release_host(host); 1022 1023 return ret; 1024 } 1025 1026 static const struct mmc_bus_ops mmc_sdio_ops = { 1027 .remove = mmc_sdio_remove, 1028 .detect = mmc_sdio_detect, 1029 .suspend = mmc_sdio_suspend, 1030 .resume = mmc_sdio_resume, 1031 .power_restore = mmc_sdio_power_restore, 1032 .alive = mmc_sdio_alive, 1033 }; 1034 1035 1036 /* 1037 * Starting point for SDIO card init. 1038 */ 1039 int mmc_attach_sdio(struct mmc_host *host) 1040 { 1041 int err, i, funcs; 1042 u32 ocr; 1043 struct mmc_card *card; 1044 1045 BUG_ON(!host); 1046 WARN_ON(!host->claimed); 1047 1048 err = mmc_send_io_op_cond(host, 0, &ocr); 1049 if (err) 1050 return err; 1051 1052 mmc_attach_bus(host, &mmc_sdio_ops); 1053 if (host->ocr_avail_sdio) 1054 host->ocr_avail = host->ocr_avail_sdio; 1055 1056 /* 1057 * Sanity check the voltages that the card claims to 1058 * support. 1059 */ 1060 if (ocr & 0x7F) { 1061 pr_warning("%s: card claims to support voltages " 1062 "below the defined range. These will be ignored.\n", 1063 mmc_hostname(host)); 1064 ocr &= ~0x7F; 1065 } 1066 1067 host->ocr = mmc_select_voltage(host, ocr); 1068 1069 /* 1070 * Can we support the voltage(s) of the card(s)? 1071 */ 1072 if (!host->ocr) { 1073 err = -EINVAL; 1074 goto err; 1075 } 1076 1077 /* 1078 * Detect and init the card. 1079 */ 1080 err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 1081 if (err) { 1082 if (err == -EAGAIN) { 1083 /* 1084 * Retry initialization with S18R set to 0. 1085 */ 1086 host->ocr &= ~R4_18V_PRESENT; 1087 err = mmc_sdio_init_card(host, host->ocr, NULL, 0); 1088 } 1089 if (err) 1090 goto err; 1091 } 1092 card = host->card; 1093 1094 /* 1095 * Enable runtime PM only if supported by host+card+board 1096 */ 1097 if (host->caps & MMC_CAP_POWER_OFF_CARD) { 1098 /* 1099 * Let runtime PM core know our card is active 1100 */ 1101 err = pm_runtime_set_active(&card->dev); 1102 if (err) 1103 goto remove; 1104 1105 /* 1106 * Enable runtime PM for this card 1107 */ 1108 pm_runtime_enable(&card->dev); 1109 } 1110 1111 /* 1112 * The number of functions on the card is encoded inside 1113 * the ocr. 1114 */ 1115 funcs = (ocr & 0x70000000) >> 28; 1116 card->sdio_funcs = 0; 1117 1118 /* 1119 * Initialize (but don't add) all present functions. 1120 */ 1121 for (i = 0; i < funcs; i++, card->sdio_funcs++) { 1122 err = sdio_init_func(host->card, i + 1); 1123 if (err) 1124 goto remove; 1125 1126 /* 1127 * Enable Runtime PM for this func (if supported) 1128 */ 1129 if (host->caps & MMC_CAP_POWER_OFF_CARD) 1130 pm_runtime_enable(&card->sdio_func[i]->dev); 1131 } 1132 1133 /* 1134 * First add the card to the driver model... 1135 */ 1136 mmc_release_host(host); 1137 err = mmc_add_card(host->card); 1138 if (err) 1139 goto remove_added; 1140 1141 /* 1142 * ...then the SDIO functions. 1143 */ 1144 for (i = 0;i < funcs;i++) { 1145 err = sdio_add_func(host->card->sdio_func[i]); 1146 if (err) 1147 goto remove_added; 1148 } 1149 1150 mmc_claim_host(host); 1151 return 0; 1152 1153 1154 remove_added: 1155 /* Remove without lock if the device has been added. */ 1156 mmc_sdio_remove(host); 1157 mmc_claim_host(host); 1158 remove: 1159 /* And with lock if it hasn't been added. */ 1160 mmc_release_host(host); 1161 if (host->card) 1162 mmc_sdio_remove(host); 1163 mmc_claim_host(host); 1164 err: 1165 mmc_detach_bus(host); 1166 1167 pr_err("%s: error %d whilst initialising SDIO card\n", 1168 mmc_hostname(host), err); 1169 1170 return err; 1171 } 1172 1173