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