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