1 /* 2 * linux/drivers/mmc/core/core.c 3 * 4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved. 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. 6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License version 2 as 11 * published by the Free Software Foundation. 12 */ 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/interrupt.h> 16 #include <linux/completion.h> 17 #include <linux/device.h> 18 #include <linux/delay.h> 19 #include <linux/pagemap.h> 20 #include <linux/err.h> 21 #include <linux/leds.h> 22 #include <linux/scatterlist.h> 23 #include <linux/log2.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/suspend.h> 27 #include <linux/fault-inject.h> 28 #include <linux/random.h> 29 30 #include <linux/mmc/card.h> 31 #include <linux/mmc/host.h> 32 #include <linux/mmc/mmc.h> 33 #include <linux/mmc/sd.h> 34 35 #include "core.h" 36 #include "bus.h" 37 #include "host.h" 38 #include "sdio_bus.h" 39 40 #include "mmc_ops.h" 41 #include "sd_ops.h" 42 #include "sdio_ops.h" 43 44 static struct workqueue_struct *workqueue; 45 46 /* 47 * Enabling software CRCs on the data blocks can be a significant (30%) 48 * performance cost, and for other reasons may not always be desired. 49 * So we allow it it to be disabled. 50 */ 51 bool use_spi_crc = 1; 52 module_param(use_spi_crc, bool, 0); 53 54 /* 55 * We normally treat cards as removed during suspend if they are not 56 * known to be on a non-removable bus, to avoid the risk of writing 57 * back data to a different card after resume. Allow this to be 58 * overridden if necessary. 59 */ 60 #ifdef CONFIG_MMC_UNSAFE_RESUME 61 bool mmc_assume_removable; 62 #else 63 bool mmc_assume_removable = 1; 64 #endif 65 EXPORT_SYMBOL(mmc_assume_removable); 66 module_param_named(removable, mmc_assume_removable, bool, 0644); 67 MODULE_PARM_DESC( 68 removable, 69 "MMC/SD cards are removable and may be removed during suspend"); 70 71 /* 72 * Internal function. Schedule delayed work in the MMC work queue. 73 */ 74 static int mmc_schedule_delayed_work(struct delayed_work *work, 75 unsigned long delay) 76 { 77 return queue_delayed_work(workqueue, work, delay); 78 } 79 80 /* 81 * Internal function. Flush all scheduled work from the MMC work queue. 82 */ 83 static void mmc_flush_scheduled_work(void) 84 { 85 flush_workqueue(workqueue); 86 } 87 88 #ifdef CONFIG_FAIL_MMC_REQUEST 89 90 /* 91 * Internal function. Inject random data errors. 92 * If mmc_data is NULL no errors are injected. 93 */ 94 static void mmc_should_fail_request(struct mmc_host *host, 95 struct mmc_request *mrq) 96 { 97 struct mmc_command *cmd = mrq->cmd; 98 struct mmc_data *data = mrq->data; 99 static const int data_errors[] = { 100 -ETIMEDOUT, 101 -EILSEQ, 102 -EIO, 103 }; 104 105 if (!data) 106 return; 107 108 if (cmd->error || data->error || 109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) 110 return; 111 112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)]; 113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9; 114 } 115 116 #else /* CONFIG_FAIL_MMC_REQUEST */ 117 118 static inline void mmc_should_fail_request(struct mmc_host *host, 119 struct mmc_request *mrq) 120 { 121 } 122 123 #endif /* CONFIG_FAIL_MMC_REQUEST */ 124 125 /** 126 * mmc_request_done - finish processing an MMC request 127 * @host: MMC host which completed request 128 * @mrq: MMC request which request 129 * 130 * MMC drivers should call this function when they have completed 131 * their processing of a request. 132 */ 133 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) 134 { 135 struct mmc_command *cmd = mrq->cmd; 136 int err = cmd->error; 137 138 if (err && cmd->retries && mmc_host_is_spi(host)) { 139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) 140 cmd->retries = 0; 141 } 142 143 if (err && cmd->retries && !mmc_card_removed(host->card)) { 144 /* 145 * Request starter must handle retries - see 146 * mmc_wait_for_req_done(). 147 */ 148 if (mrq->done) 149 mrq->done(mrq); 150 } else { 151 mmc_should_fail_request(host, mrq); 152 153 led_trigger_event(host->led, LED_OFF); 154 155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", 156 mmc_hostname(host), cmd->opcode, err, 157 cmd->resp[0], cmd->resp[1], 158 cmd->resp[2], cmd->resp[3]); 159 160 if (mrq->data) { 161 pr_debug("%s: %d bytes transferred: %d\n", 162 mmc_hostname(host), 163 mrq->data->bytes_xfered, mrq->data->error); 164 } 165 166 if (mrq->stop) { 167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", 168 mmc_hostname(host), mrq->stop->opcode, 169 mrq->stop->error, 170 mrq->stop->resp[0], mrq->stop->resp[1], 171 mrq->stop->resp[2], mrq->stop->resp[3]); 172 } 173 174 if (mrq->done) 175 mrq->done(mrq); 176 177 mmc_host_clk_release(host); 178 } 179 } 180 181 EXPORT_SYMBOL(mmc_request_done); 182 183 static void 184 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) 185 { 186 #ifdef CONFIG_MMC_DEBUG 187 unsigned int i, sz; 188 struct scatterlist *sg; 189 #endif 190 191 if (mrq->sbc) { 192 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n", 193 mmc_hostname(host), mrq->sbc->opcode, 194 mrq->sbc->arg, mrq->sbc->flags); 195 } 196 197 pr_debug("%s: starting CMD%u arg %08x flags %08x\n", 198 mmc_hostname(host), mrq->cmd->opcode, 199 mrq->cmd->arg, mrq->cmd->flags); 200 201 if (mrq->data) { 202 pr_debug("%s: blksz %d blocks %d flags %08x " 203 "tsac %d ms nsac %d\n", 204 mmc_hostname(host), mrq->data->blksz, 205 mrq->data->blocks, mrq->data->flags, 206 mrq->data->timeout_ns / 1000000, 207 mrq->data->timeout_clks); 208 } 209 210 if (mrq->stop) { 211 pr_debug("%s: CMD%u arg %08x flags %08x\n", 212 mmc_hostname(host), mrq->stop->opcode, 213 mrq->stop->arg, mrq->stop->flags); 214 } 215 216 WARN_ON(!host->claimed); 217 218 mrq->cmd->error = 0; 219 mrq->cmd->mrq = mrq; 220 if (mrq->data) { 221 BUG_ON(mrq->data->blksz > host->max_blk_size); 222 BUG_ON(mrq->data->blocks > host->max_blk_count); 223 BUG_ON(mrq->data->blocks * mrq->data->blksz > 224 host->max_req_size); 225 226 #ifdef CONFIG_MMC_DEBUG 227 sz = 0; 228 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) 229 sz += sg->length; 230 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); 231 #endif 232 233 mrq->cmd->data = mrq->data; 234 mrq->data->error = 0; 235 mrq->data->mrq = mrq; 236 if (mrq->stop) { 237 mrq->data->stop = mrq->stop; 238 mrq->stop->error = 0; 239 mrq->stop->mrq = mrq; 240 } 241 } 242 mmc_host_clk_hold(host); 243 led_trigger_event(host->led, LED_FULL); 244 host->ops->request(host, mrq); 245 } 246 247 static void mmc_wait_done(struct mmc_request *mrq) 248 { 249 complete(&mrq->completion); 250 } 251 252 static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) 253 { 254 init_completion(&mrq->completion); 255 mrq->done = mmc_wait_done; 256 if (mmc_card_removed(host->card)) { 257 mrq->cmd->error = -ENOMEDIUM; 258 complete(&mrq->completion); 259 return -ENOMEDIUM; 260 } 261 mmc_start_request(host, mrq); 262 return 0; 263 } 264 265 static void mmc_wait_for_req_done(struct mmc_host *host, 266 struct mmc_request *mrq) 267 { 268 struct mmc_command *cmd; 269 270 while (1) { 271 wait_for_completion(&mrq->completion); 272 273 cmd = mrq->cmd; 274 if (!cmd->error || !cmd->retries || 275 mmc_card_removed(host->card)) 276 break; 277 278 pr_debug("%s: req failed (CMD%u): %d, retrying...\n", 279 mmc_hostname(host), cmd->opcode, cmd->error); 280 cmd->retries--; 281 cmd->error = 0; 282 host->ops->request(host, mrq); 283 } 284 } 285 286 /** 287 * mmc_pre_req - Prepare for a new request 288 * @host: MMC host to prepare command 289 * @mrq: MMC request to prepare for 290 * @is_first_req: true if there is no previous started request 291 * that may run in parellel to this call, otherwise false 292 * 293 * mmc_pre_req() is called in prior to mmc_start_req() to let 294 * host prepare for the new request. Preparation of a request may be 295 * performed while another request is running on the host. 296 */ 297 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq, 298 bool is_first_req) 299 { 300 if (host->ops->pre_req) { 301 mmc_host_clk_hold(host); 302 host->ops->pre_req(host, mrq, is_first_req); 303 mmc_host_clk_release(host); 304 } 305 } 306 307 /** 308 * mmc_post_req - Post process a completed request 309 * @host: MMC host to post process command 310 * @mrq: MMC request to post process for 311 * @err: Error, if non zero, clean up any resources made in pre_req 312 * 313 * Let the host post process a completed request. Post processing of 314 * a request may be performed while another reuqest is running. 315 */ 316 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq, 317 int err) 318 { 319 if (host->ops->post_req) { 320 mmc_host_clk_hold(host); 321 host->ops->post_req(host, mrq, err); 322 mmc_host_clk_release(host); 323 } 324 } 325 326 /** 327 * mmc_start_req - start a non-blocking request 328 * @host: MMC host to start command 329 * @areq: async request to start 330 * @error: out parameter returns 0 for success, otherwise non zero 331 * 332 * Start a new MMC custom command request for a host. 333 * If there is on ongoing async request wait for completion 334 * of that request and start the new one and return. 335 * Does not wait for the new request to complete. 336 * 337 * Returns the completed request, NULL in case of none completed. 338 * Wait for the an ongoing request (previoulsy started) to complete and 339 * return the completed request. If there is no ongoing request, NULL 340 * is returned without waiting. NULL is not an error condition. 341 */ 342 struct mmc_async_req *mmc_start_req(struct mmc_host *host, 343 struct mmc_async_req *areq, int *error) 344 { 345 int err = 0; 346 int start_err = 0; 347 struct mmc_async_req *data = host->areq; 348 349 /* Prepare a new request */ 350 if (areq) 351 mmc_pre_req(host, areq->mrq, !host->areq); 352 353 if (host->areq) { 354 mmc_wait_for_req_done(host, host->areq->mrq); 355 err = host->areq->err_check(host->card, host->areq); 356 } 357 358 if (!err && areq) 359 start_err = __mmc_start_req(host, areq->mrq); 360 361 if (host->areq) 362 mmc_post_req(host, host->areq->mrq, 0); 363 364 /* Cancel a prepared request if it was not started. */ 365 if ((err || start_err) && areq) 366 mmc_post_req(host, areq->mrq, -EINVAL); 367 368 if (err) 369 host->areq = NULL; 370 else 371 host->areq = areq; 372 373 if (error) 374 *error = err; 375 return data; 376 } 377 EXPORT_SYMBOL(mmc_start_req); 378 379 /** 380 * mmc_wait_for_req - start a request and wait for completion 381 * @host: MMC host to start command 382 * @mrq: MMC request to start 383 * 384 * Start a new MMC custom command request for a host, and wait 385 * for the command to complete. Does not attempt to parse the 386 * response. 387 */ 388 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) 389 { 390 __mmc_start_req(host, mrq); 391 mmc_wait_for_req_done(host, mrq); 392 } 393 EXPORT_SYMBOL(mmc_wait_for_req); 394 395 /** 396 * mmc_interrupt_hpi - Issue for High priority Interrupt 397 * @card: the MMC card associated with the HPI transfer 398 * 399 * Issued High Priority Interrupt, and check for card status 400 * util out-of prg-state. 401 */ 402 int mmc_interrupt_hpi(struct mmc_card *card) 403 { 404 int err; 405 u32 status; 406 407 BUG_ON(!card); 408 409 if (!card->ext_csd.hpi_en) { 410 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host)); 411 return 1; 412 } 413 414 mmc_claim_host(card->host); 415 err = mmc_send_status(card, &status); 416 if (err) { 417 pr_err("%s: Get card status fail\n", mmc_hostname(card->host)); 418 goto out; 419 } 420 421 /* 422 * If the card status is in PRG-state, we can send the HPI command. 423 */ 424 if (R1_CURRENT_STATE(status) == R1_STATE_PRG) { 425 do { 426 /* 427 * We don't know when the HPI command will finish 428 * processing, so we need to resend HPI until out 429 * of prg-state, and keep checking the card status 430 * with SEND_STATUS. If a timeout error occurs when 431 * sending the HPI command, we are already out of 432 * prg-state. 433 */ 434 err = mmc_send_hpi_cmd(card, &status); 435 if (err) 436 pr_debug("%s: abort HPI (%d error)\n", 437 mmc_hostname(card->host), err); 438 439 err = mmc_send_status(card, &status); 440 if (err) 441 break; 442 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG); 443 } else 444 pr_debug("%s: Left prg-state\n", mmc_hostname(card->host)); 445 446 out: 447 mmc_release_host(card->host); 448 return err; 449 } 450 EXPORT_SYMBOL(mmc_interrupt_hpi); 451 452 /** 453 * mmc_wait_for_cmd - start a command and wait for completion 454 * @host: MMC host to start command 455 * @cmd: MMC command to start 456 * @retries: maximum number of retries 457 * 458 * Start a new MMC command for a host, and wait for the command 459 * to complete. Return any error that occurred while the command 460 * was executing. Do not attempt to parse the response. 461 */ 462 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) 463 { 464 struct mmc_request mrq = {NULL}; 465 466 WARN_ON(!host->claimed); 467 468 memset(cmd->resp, 0, sizeof(cmd->resp)); 469 cmd->retries = retries; 470 471 mrq.cmd = cmd; 472 cmd->data = NULL; 473 474 mmc_wait_for_req(host, &mrq); 475 476 return cmd->error; 477 } 478 479 EXPORT_SYMBOL(mmc_wait_for_cmd); 480 481 /** 482 * mmc_set_data_timeout - set the timeout for a data command 483 * @data: data phase for command 484 * @card: the MMC card associated with the data transfer 485 * 486 * Computes the data timeout parameters according to the 487 * correct algorithm given the card type. 488 */ 489 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) 490 { 491 unsigned int mult; 492 493 /* 494 * SDIO cards only define an upper 1 s limit on access. 495 */ 496 if (mmc_card_sdio(card)) { 497 data->timeout_ns = 1000000000; 498 data->timeout_clks = 0; 499 return; 500 } 501 502 /* 503 * SD cards use a 100 multiplier rather than 10 504 */ 505 mult = mmc_card_sd(card) ? 100 : 10; 506 507 /* 508 * Scale up the multiplier (and therefore the timeout) by 509 * the r2w factor for writes. 510 */ 511 if (data->flags & MMC_DATA_WRITE) 512 mult <<= card->csd.r2w_factor; 513 514 data->timeout_ns = card->csd.tacc_ns * mult; 515 data->timeout_clks = card->csd.tacc_clks * mult; 516 517 /* 518 * SD cards also have an upper limit on the timeout. 519 */ 520 if (mmc_card_sd(card)) { 521 unsigned int timeout_us, limit_us; 522 523 timeout_us = data->timeout_ns / 1000; 524 if (mmc_host_clk_rate(card->host)) 525 timeout_us += data->timeout_clks * 1000 / 526 (mmc_host_clk_rate(card->host) / 1000); 527 528 if (data->flags & MMC_DATA_WRITE) 529 /* 530 * The limit is really 250 ms, but that is 531 * insufficient for some crappy cards. 532 */ 533 limit_us = 300000; 534 else 535 limit_us = 100000; 536 537 /* 538 * SDHC cards always use these fixed values. 539 */ 540 if (timeout_us > limit_us || mmc_card_blockaddr(card)) { 541 data->timeout_ns = limit_us * 1000; 542 data->timeout_clks = 0; 543 } 544 } 545 546 /* 547 * Some cards require longer data read timeout than indicated in CSD. 548 * Address this by setting the read timeout to a "reasonably high" 549 * value. For the cards tested, 300ms has proven enough. If necessary, 550 * this value can be increased if other problematic cards require this. 551 */ 552 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) { 553 data->timeout_ns = 300000000; 554 data->timeout_clks = 0; 555 } 556 557 /* 558 * Some cards need very high timeouts if driven in SPI mode. 559 * The worst observed timeout was 900ms after writing a 560 * continuous stream of data until the internal logic 561 * overflowed. 562 */ 563 if (mmc_host_is_spi(card->host)) { 564 if (data->flags & MMC_DATA_WRITE) { 565 if (data->timeout_ns < 1000000000) 566 data->timeout_ns = 1000000000; /* 1s */ 567 } else { 568 if (data->timeout_ns < 100000000) 569 data->timeout_ns = 100000000; /* 100ms */ 570 } 571 } 572 } 573 EXPORT_SYMBOL(mmc_set_data_timeout); 574 575 /** 576 * mmc_align_data_size - pads a transfer size to a more optimal value 577 * @card: the MMC card associated with the data transfer 578 * @sz: original transfer size 579 * 580 * Pads the original data size with a number of extra bytes in 581 * order to avoid controller bugs and/or performance hits 582 * (e.g. some controllers revert to PIO for certain sizes). 583 * 584 * Returns the improved size, which might be unmodified. 585 * 586 * Note that this function is only relevant when issuing a 587 * single scatter gather entry. 588 */ 589 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) 590 { 591 /* 592 * FIXME: We don't have a system for the controller to tell 593 * the core about its problems yet, so for now we just 32-bit 594 * align the size. 595 */ 596 sz = ((sz + 3) / 4) * 4; 597 598 return sz; 599 } 600 EXPORT_SYMBOL(mmc_align_data_size); 601 602 /** 603 * __mmc_claim_host - exclusively claim a host 604 * @host: mmc host to claim 605 * @abort: whether or not the operation should be aborted 606 * 607 * Claim a host for a set of operations. If @abort is non null and 608 * dereference a non-zero value then this will return prematurely with 609 * that non-zero value without acquiring the lock. Returns zero 610 * with the lock held otherwise. 611 */ 612 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) 613 { 614 DECLARE_WAITQUEUE(wait, current); 615 unsigned long flags; 616 int stop; 617 618 might_sleep(); 619 620 add_wait_queue(&host->wq, &wait); 621 spin_lock_irqsave(&host->lock, flags); 622 while (1) { 623 set_current_state(TASK_UNINTERRUPTIBLE); 624 stop = abort ? atomic_read(abort) : 0; 625 if (stop || !host->claimed || host->claimer == current) 626 break; 627 spin_unlock_irqrestore(&host->lock, flags); 628 schedule(); 629 spin_lock_irqsave(&host->lock, flags); 630 } 631 set_current_state(TASK_RUNNING); 632 if (!stop) { 633 host->claimed = 1; 634 host->claimer = current; 635 host->claim_cnt += 1; 636 } else 637 wake_up(&host->wq); 638 spin_unlock_irqrestore(&host->lock, flags); 639 remove_wait_queue(&host->wq, &wait); 640 if (host->ops->enable && !stop && host->claim_cnt == 1) 641 host->ops->enable(host); 642 return stop; 643 } 644 645 EXPORT_SYMBOL(__mmc_claim_host); 646 647 /** 648 * mmc_try_claim_host - try exclusively to claim a host 649 * @host: mmc host to claim 650 * 651 * Returns %1 if the host is claimed, %0 otherwise. 652 */ 653 int mmc_try_claim_host(struct mmc_host *host) 654 { 655 int claimed_host = 0; 656 unsigned long flags; 657 658 spin_lock_irqsave(&host->lock, flags); 659 if (!host->claimed || host->claimer == current) { 660 host->claimed = 1; 661 host->claimer = current; 662 host->claim_cnt += 1; 663 claimed_host = 1; 664 } 665 spin_unlock_irqrestore(&host->lock, flags); 666 if (host->ops->enable && claimed_host && host->claim_cnt == 1) 667 host->ops->enable(host); 668 return claimed_host; 669 } 670 EXPORT_SYMBOL(mmc_try_claim_host); 671 672 /** 673 * mmc_release_host - release a host 674 * @host: mmc host to release 675 * 676 * Release a MMC host, allowing others to claim the host 677 * for their operations. 678 */ 679 void mmc_release_host(struct mmc_host *host) 680 { 681 unsigned long flags; 682 683 WARN_ON(!host->claimed); 684 685 if (host->ops->disable && host->claim_cnt == 1) 686 host->ops->disable(host); 687 688 spin_lock_irqsave(&host->lock, flags); 689 if (--host->claim_cnt) { 690 /* Release for nested claim */ 691 spin_unlock_irqrestore(&host->lock, flags); 692 } else { 693 host->claimed = 0; 694 host->claimer = NULL; 695 spin_unlock_irqrestore(&host->lock, flags); 696 wake_up(&host->wq); 697 } 698 } 699 EXPORT_SYMBOL(mmc_release_host); 700 701 /* 702 * Internal function that does the actual ios call to the host driver, 703 * optionally printing some debug output. 704 */ 705 static inline void mmc_set_ios(struct mmc_host *host) 706 { 707 struct mmc_ios *ios = &host->ios; 708 709 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " 710 "width %u timing %u\n", 711 mmc_hostname(host), ios->clock, ios->bus_mode, 712 ios->power_mode, ios->chip_select, ios->vdd, 713 ios->bus_width, ios->timing); 714 715 if (ios->clock > 0) 716 mmc_set_ungated(host); 717 host->ops->set_ios(host, ios); 718 } 719 720 /* 721 * Control chip select pin on a host. 722 */ 723 void mmc_set_chip_select(struct mmc_host *host, int mode) 724 { 725 mmc_host_clk_hold(host); 726 host->ios.chip_select = mode; 727 mmc_set_ios(host); 728 mmc_host_clk_release(host); 729 } 730 731 /* 732 * Sets the host clock to the highest possible frequency that 733 * is below "hz". 734 */ 735 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) 736 { 737 WARN_ON(hz < host->f_min); 738 739 if (hz > host->f_max) 740 hz = host->f_max; 741 742 host->ios.clock = hz; 743 mmc_set_ios(host); 744 } 745 746 void mmc_set_clock(struct mmc_host *host, unsigned int hz) 747 { 748 mmc_host_clk_hold(host); 749 __mmc_set_clock(host, hz); 750 mmc_host_clk_release(host); 751 } 752 753 #ifdef CONFIG_MMC_CLKGATE 754 /* 755 * This gates the clock by setting it to 0 Hz. 756 */ 757 void mmc_gate_clock(struct mmc_host *host) 758 { 759 unsigned long flags; 760 761 spin_lock_irqsave(&host->clk_lock, flags); 762 host->clk_old = host->ios.clock; 763 host->ios.clock = 0; 764 host->clk_gated = true; 765 spin_unlock_irqrestore(&host->clk_lock, flags); 766 mmc_set_ios(host); 767 } 768 769 /* 770 * This restores the clock from gating by using the cached 771 * clock value. 772 */ 773 void mmc_ungate_clock(struct mmc_host *host) 774 { 775 /* 776 * We should previously have gated the clock, so the clock shall 777 * be 0 here! The clock may however be 0 during initialization, 778 * when some request operations are performed before setting 779 * the frequency. When ungate is requested in that situation 780 * we just ignore the call. 781 */ 782 if (host->clk_old) { 783 BUG_ON(host->ios.clock); 784 /* This call will also set host->clk_gated to false */ 785 __mmc_set_clock(host, host->clk_old); 786 } 787 } 788 789 void mmc_set_ungated(struct mmc_host *host) 790 { 791 unsigned long flags; 792 793 /* 794 * We've been given a new frequency while the clock is gated, 795 * so make sure we regard this as ungating it. 796 */ 797 spin_lock_irqsave(&host->clk_lock, flags); 798 host->clk_gated = false; 799 spin_unlock_irqrestore(&host->clk_lock, flags); 800 } 801 802 #else 803 void mmc_set_ungated(struct mmc_host *host) 804 { 805 } 806 #endif 807 808 /* 809 * Change the bus mode (open drain/push-pull) of a host. 810 */ 811 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) 812 { 813 mmc_host_clk_hold(host); 814 host->ios.bus_mode = mode; 815 mmc_set_ios(host); 816 mmc_host_clk_release(host); 817 } 818 819 /* 820 * Change data bus width of a host. 821 */ 822 void mmc_set_bus_width(struct mmc_host *host, unsigned int width) 823 { 824 mmc_host_clk_hold(host); 825 host->ios.bus_width = width; 826 mmc_set_ios(host); 827 mmc_host_clk_release(host); 828 } 829 830 /** 831 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number 832 * @vdd: voltage (mV) 833 * @low_bits: prefer low bits in boundary cases 834 * 835 * This function returns the OCR bit number according to the provided @vdd 836 * value. If conversion is not possible a negative errno value returned. 837 * 838 * Depending on the @low_bits flag the function prefers low or high OCR bits 839 * on boundary voltages. For example, 840 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); 841 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); 842 * 843 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). 844 */ 845 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) 846 { 847 const int max_bit = ilog2(MMC_VDD_35_36); 848 int bit; 849 850 if (vdd < 1650 || vdd > 3600) 851 return -EINVAL; 852 853 if (vdd >= 1650 && vdd <= 1950) 854 return ilog2(MMC_VDD_165_195); 855 856 if (low_bits) 857 vdd -= 1; 858 859 /* Base 2000 mV, step 100 mV, bit's base 8. */ 860 bit = (vdd - 2000) / 100 + 8; 861 if (bit > max_bit) 862 return max_bit; 863 return bit; 864 } 865 866 /** 867 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask 868 * @vdd_min: minimum voltage value (mV) 869 * @vdd_max: maximum voltage value (mV) 870 * 871 * This function returns the OCR mask bits according to the provided @vdd_min 872 * and @vdd_max values. If conversion is not possible the function returns 0. 873 * 874 * Notes wrt boundary cases: 875 * This function sets the OCR bits for all boundary voltages, for example 876 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | 877 * MMC_VDD_34_35 mask. 878 */ 879 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) 880 { 881 u32 mask = 0; 882 883 if (vdd_max < vdd_min) 884 return 0; 885 886 /* Prefer high bits for the boundary vdd_max values. */ 887 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); 888 if (vdd_max < 0) 889 return 0; 890 891 /* Prefer low bits for the boundary vdd_min values. */ 892 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); 893 if (vdd_min < 0) 894 return 0; 895 896 /* Fill the mask, from max bit to min bit. */ 897 while (vdd_max >= vdd_min) 898 mask |= 1 << vdd_max--; 899 900 return mask; 901 } 902 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); 903 904 #ifdef CONFIG_REGULATOR 905 906 /** 907 * mmc_regulator_get_ocrmask - return mask of supported voltages 908 * @supply: regulator to use 909 * 910 * This returns either a negative errno, or a mask of voltages that 911 * can be provided to MMC/SD/SDIO devices using the specified voltage 912 * regulator. This would normally be called before registering the 913 * MMC host adapter. 914 */ 915 int mmc_regulator_get_ocrmask(struct regulator *supply) 916 { 917 int result = 0; 918 int count; 919 int i; 920 921 count = regulator_count_voltages(supply); 922 if (count < 0) 923 return count; 924 925 for (i = 0; i < count; i++) { 926 int vdd_uV; 927 int vdd_mV; 928 929 vdd_uV = regulator_list_voltage(supply, i); 930 if (vdd_uV <= 0) 931 continue; 932 933 vdd_mV = vdd_uV / 1000; 934 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); 935 } 936 937 return result; 938 } 939 EXPORT_SYMBOL(mmc_regulator_get_ocrmask); 940 941 /** 942 * mmc_regulator_set_ocr - set regulator to match host->ios voltage 943 * @mmc: the host to regulate 944 * @supply: regulator to use 945 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) 946 * 947 * Returns zero on success, else negative errno. 948 * 949 * MMC host drivers may use this to enable or disable a regulator using 950 * a particular supply voltage. This would normally be called from the 951 * set_ios() method. 952 */ 953 int mmc_regulator_set_ocr(struct mmc_host *mmc, 954 struct regulator *supply, 955 unsigned short vdd_bit) 956 { 957 int result = 0; 958 int min_uV, max_uV; 959 960 if (vdd_bit) { 961 int tmp; 962 int voltage; 963 964 /* REVISIT mmc_vddrange_to_ocrmask() may have set some 965 * bits this regulator doesn't quite support ... don't 966 * be too picky, most cards and regulators are OK with 967 * a 0.1V range goof (it's a small error percentage). 968 */ 969 tmp = vdd_bit - ilog2(MMC_VDD_165_195); 970 if (tmp == 0) { 971 min_uV = 1650 * 1000; 972 max_uV = 1950 * 1000; 973 } else { 974 min_uV = 1900 * 1000 + tmp * 100 * 1000; 975 max_uV = min_uV + 100 * 1000; 976 } 977 978 /* avoid needless changes to this voltage; the regulator 979 * might not allow this operation 980 */ 981 voltage = regulator_get_voltage(supply); 982 983 if (mmc->caps2 & MMC_CAP2_BROKEN_VOLTAGE) 984 min_uV = max_uV = voltage; 985 986 if (voltage < 0) 987 result = voltage; 988 else if (voltage < min_uV || voltage > max_uV) 989 result = regulator_set_voltage(supply, min_uV, max_uV); 990 else 991 result = 0; 992 993 if (result == 0 && !mmc->regulator_enabled) { 994 result = regulator_enable(supply); 995 if (!result) 996 mmc->regulator_enabled = true; 997 } 998 } else if (mmc->regulator_enabled) { 999 result = regulator_disable(supply); 1000 if (result == 0) 1001 mmc->regulator_enabled = false; 1002 } 1003 1004 if (result) 1005 dev_err(mmc_dev(mmc), 1006 "could not set regulator OCR (%d)\n", result); 1007 return result; 1008 } 1009 EXPORT_SYMBOL(mmc_regulator_set_ocr); 1010 1011 #endif /* CONFIG_REGULATOR */ 1012 1013 /* 1014 * Mask off any voltages we don't support and select 1015 * the lowest voltage 1016 */ 1017 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) 1018 { 1019 int bit; 1020 1021 ocr &= host->ocr_avail; 1022 1023 bit = ffs(ocr); 1024 if (bit) { 1025 bit -= 1; 1026 1027 ocr &= 3 << bit; 1028 1029 mmc_host_clk_hold(host); 1030 host->ios.vdd = bit; 1031 mmc_set_ios(host); 1032 mmc_host_clk_release(host); 1033 } else { 1034 pr_warning("%s: host doesn't support card's voltages\n", 1035 mmc_hostname(host)); 1036 ocr = 0; 1037 } 1038 1039 return ocr; 1040 } 1041 1042 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11) 1043 { 1044 struct mmc_command cmd = {0}; 1045 int err = 0; 1046 1047 BUG_ON(!host); 1048 1049 /* 1050 * Send CMD11 only if the request is to switch the card to 1051 * 1.8V signalling. 1052 */ 1053 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) { 1054 cmd.opcode = SD_SWITCH_VOLTAGE; 1055 cmd.arg = 0; 1056 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1057 1058 err = mmc_wait_for_cmd(host, &cmd, 0); 1059 if (err) 1060 return err; 1061 1062 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) 1063 return -EIO; 1064 } 1065 1066 host->ios.signal_voltage = signal_voltage; 1067 1068 if (host->ops->start_signal_voltage_switch) { 1069 mmc_host_clk_hold(host); 1070 err = host->ops->start_signal_voltage_switch(host, &host->ios); 1071 mmc_host_clk_release(host); 1072 } 1073 1074 return err; 1075 } 1076 1077 /* 1078 * Select timing parameters for host. 1079 */ 1080 void mmc_set_timing(struct mmc_host *host, unsigned int timing) 1081 { 1082 mmc_host_clk_hold(host); 1083 host->ios.timing = timing; 1084 mmc_set_ios(host); 1085 mmc_host_clk_release(host); 1086 } 1087 1088 /* 1089 * Select appropriate driver type for host. 1090 */ 1091 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) 1092 { 1093 mmc_host_clk_hold(host); 1094 host->ios.drv_type = drv_type; 1095 mmc_set_ios(host); 1096 mmc_host_clk_release(host); 1097 } 1098 1099 static void mmc_poweroff_notify(struct mmc_host *host) 1100 { 1101 struct mmc_card *card; 1102 unsigned int timeout; 1103 unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION; 1104 int err = 0; 1105 1106 card = host->card; 1107 mmc_claim_host(host); 1108 1109 /* 1110 * Send power notify command only if card 1111 * is mmc and notify state is powered ON 1112 */ 1113 if (card && mmc_card_mmc(card) && 1114 (card->poweroff_notify_state == MMC_POWERED_ON)) { 1115 1116 if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) { 1117 notify_type = EXT_CSD_POWER_OFF_SHORT; 1118 timeout = card->ext_csd.generic_cmd6_time; 1119 card->poweroff_notify_state = MMC_POWEROFF_SHORT; 1120 } else { 1121 notify_type = EXT_CSD_POWER_OFF_LONG; 1122 timeout = card->ext_csd.power_off_longtime; 1123 card->poweroff_notify_state = MMC_POWEROFF_LONG; 1124 } 1125 1126 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 1127 EXT_CSD_POWER_OFF_NOTIFICATION, 1128 notify_type, timeout); 1129 1130 if (err && err != -EBADMSG) 1131 pr_err("Device failed to respond within %d poweroff " 1132 "time. Forcefully powering down the device\n", 1133 timeout); 1134 1135 /* Set the card state to no notification after the poweroff */ 1136 card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION; 1137 } 1138 mmc_release_host(host); 1139 } 1140 1141 /* 1142 * Apply power to the MMC stack. This is a two-stage process. 1143 * First, we enable power to the card without the clock running. 1144 * We then wait a bit for the power to stabilise. Finally, 1145 * enable the bus drivers and clock to the card. 1146 * 1147 * We must _NOT_ enable the clock prior to power stablising. 1148 * 1149 * If a host does all the power sequencing itself, ignore the 1150 * initial MMC_POWER_UP stage. 1151 */ 1152 static void mmc_power_up(struct mmc_host *host) 1153 { 1154 int bit; 1155 1156 mmc_host_clk_hold(host); 1157 1158 /* If ocr is set, we use it */ 1159 if (host->ocr) 1160 bit = ffs(host->ocr) - 1; 1161 else 1162 bit = fls(host->ocr_avail) - 1; 1163 1164 host->ios.vdd = bit; 1165 if (mmc_host_is_spi(host)) 1166 host->ios.chip_select = MMC_CS_HIGH; 1167 else 1168 host->ios.chip_select = MMC_CS_DONTCARE; 1169 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 1170 host->ios.power_mode = MMC_POWER_UP; 1171 host->ios.bus_width = MMC_BUS_WIDTH_1; 1172 host->ios.timing = MMC_TIMING_LEGACY; 1173 mmc_set_ios(host); 1174 1175 /* 1176 * This delay should be sufficient to allow the power supply 1177 * to reach the minimum voltage. 1178 */ 1179 mmc_delay(10); 1180 1181 host->ios.clock = host->f_init; 1182 1183 host->ios.power_mode = MMC_POWER_ON; 1184 mmc_set_ios(host); 1185 1186 /* 1187 * This delay must be at least 74 clock sizes, or 1 ms, or the 1188 * time required to reach a stable voltage. 1189 */ 1190 mmc_delay(10); 1191 1192 mmc_host_clk_release(host); 1193 } 1194 1195 void mmc_power_off(struct mmc_host *host) 1196 { 1197 int err = 0; 1198 mmc_host_clk_hold(host); 1199 1200 host->ios.clock = 0; 1201 host->ios.vdd = 0; 1202 1203 /* 1204 * For eMMC 4.5 device send AWAKE command before 1205 * POWER_OFF_NOTIFY command, because in sleep state 1206 * eMMC 4.5 devices respond to only RESET and AWAKE cmd 1207 */ 1208 if (host->card && mmc_card_is_sleep(host->card) && 1209 host->bus_ops->resume) { 1210 err = host->bus_ops->resume(host); 1211 1212 if (!err) 1213 mmc_poweroff_notify(host); 1214 else 1215 pr_warning("%s: error %d during resume " 1216 "(continue with poweroff sequence)\n", 1217 mmc_hostname(host), err); 1218 } 1219 1220 /* 1221 * Reset ocr mask to be the highest possible voltage supported for 1222 * this mmc host. This value will be used at next power up. 1223 */ 1224 host->ocr = 1 << (fls(host->ocr_avail) - 1); 1225 1226 if (!mmc_host_is_spi(host)) { 1227 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 1228 host->ios.chip_select = MMC_CS_DONTCARE; 1229 } 1230 host->ios.power_mode = MMC_POWER_OFF; 1231 host->ios.bus_width = MMC_BUS_WIDTH_1; 1232 host->ios.timing = MMC_TIMING_LEGACY; 1233 mmc_set_ios(host); 1234 1235 /* 1236 * Some configurations, such as the 802.11 SDIO card in the OLPC 1237 * XO-1.5, require a short delay after poweroff before the card 1238 * can be successfully turned on again. 1239 */ 1240 mmc_delay(1); 1241 1242 mmc_host_clk_release(host); 1243 } 1244 1245 /* 1246 * Cleanup when the last reference to the bus operator is dropped. 1247 */ 1248 static void __mmc_release_bus(struct mmc_host *host) 1249 { 1250 BUG_ON(!host); 1251 BUG_ON(host->bus_refs); 1252 BUG_ON(!host->bus_dead); 1253 1254 host->bus_ops = NULL; 1255 } 1256 1257 /* 1258 * Increase reference count of bus operator 1259 */ 1260 static inline void mmc_bus_get(struct mmc_host *host) 1261 { 1262 unsigned long flags; 1263 1264 spin_lock_irqsave(&host->lock, flags); 1265 host->bus_refs++; 1266 spin_unlock_irqrestore(&host->lock, flags); 1267 } 1268 1269 /* 1270 * Decrease reference count of bus operator and free it if 1271 * it is the last reference. 1272 */ 1273 static inline void mmc_bus_put(struct mmc_host *host) 1274 { 1275 unsigned long flags; 1276 1277 spin_lock_irqsave(&host->lock, flags); 1278 host->bus_refs--; 1279 if ((host->bus_refs == 0) && host->bus_ops) 1280 __mmc_release_bus(host); 1281 spin_unlock_irqrestore(&host->lock, flags); 1282 } 1283 1284 /* 1285 * Assign a mmc bus handler to a host. Only one bus handler may control a 1286 * host at any given time. 1287 */ 1288 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) 1289 { 1290 unsigned long flags; 1291 1292 BUG_ON(!host); 1293 BUG_ON(!ops); 1294 1295 WARN_ON(!host->claimed); 1296 1297 spin_lock_irqsave(&host->lock, flags); 1298 1299 BUG_ON(host->bus_ops); 1300 BUG_ON(host->bus_refs); 1301 1302 host->bus_ops = ops; 1303 host->bus_refs = 1; 1304 host->bus_dead = 0; 1305 1306 spin_unlock_irqrestore(&host->lock, flags); 1307 } 1308 1309 /* 1310 * Remove the current bus handler from a host. 1311 */ 1312 void mmc_detach_bus(struct mmc_host *host) 1313 { 1314 unsigned long flags; 1315 1316 BUG_ON(!host); 1317 1318 WARN_ON(!host->claimed); 1319 WARN_ON(!host->bus_ops); 1320 1321 spin_lock_irqsave(&host->lock, flags); 1322 1323 host->bus_dead = 1; 1324 1325 spin_unlock_irqrestore(&host->lock, flags); 1326 1327 mmc_bus_put(host); 1328 } 1329 1330 /** 1331 * mmc_detect_change - process change of state on a MMC socket 1332 * @host: host which changed state. 1333 * @delay: optional delay to wait before detection (jiffies) 1334 * 1335 * MMC drivers should call this when they detect a card has been 1336 * inserted or removed. The MMC layer will confirm that any 1337 * present card is still functional, and initialize any newly 1338 * inserted. 1339 */ 1340 void mmc_detect_change(struct mmc_host *host, unsigned long delay) 1341 { 1342 #ifdef CONFIG_MMC_DEBUG 1343 unsigned long flags; 1344 spin_lock_irqsave(&host->lock, flags); 1345 WARN_ON(host->removed); 1346 spin_unlock_irqrestore(&host->lock, flags); 1347 #endif 1348 host->detect_change = 1; 1349 mmc_schedule_delayed_work(&host->detect, delay); 1350 } 1351 1352 EXPORT_SYMBOL(mmc_detect_change); 1353 1354 void mmc_init_erase(struct mmc_card *card) 1355 { 1356 unsigned int sz; 1357 1358 if (is_power_of_2(card->erase_size)) 1359 card->erase_shift = ffs(card->erase_size) - 1; 1360 else 1361 card->erase_shift = 0; 1362 1363 /* 1364 * It is possible to erase an arbitrarily large area of an SD or MMC 1365 * card. That is not desirable because it can take a long time 1366 * (minutes) potentially delaying more important I/O, and also the 1367 * timeout calculations become increasingly hugely over-estimated. 1368 * Consequently, 'pref_erase' is defined as a guide to limit erases 1369 * to that size and alignment. 1370 * 1371 * For SD cards that define Allocation Unit size, limit erases to one 1372 * Allocation Unit at a time. For MMC cards that define High Capacity 1373 * Erase Size, whether it is switched on or not, limit to that size. 1374 * Otherwise just have a stab at a good value. For modern cards it 1375 * will end up being 4MiB. Note that if the value is too small, it 1376 * can end up taking longer to erase. 1377 */ 1378 if (mmc_card_sd(card) && card->ssr.au) { 1379 card->pref_erase = card->ssr.au; 1380 card->erase_shift = ffs(card->ssr.au) - 1; 1381 } else if (card->ext_csd.hc_erase_size) { 1382 card->pref_erase = card->ext_csd.hc_erase_size; 1383 } else { 1384 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; 1385 if (sz < 128) 1386 card->pref_erase = 512 * 1024 / 512; 1387 else if (sz < 512) 1388 card->pref_erase = 1024 * 1024 / 512; 1389 else if (sz < 1024) 1390 card->pref_erase = 2 * 1024 * 1024 / 512; 1391 else 1392 card->pref_erase = 4 * 1024 * 1024 / 512; 1393 if (card->pref_erase < card->erase_size) 1394 card->pref_erase = card->erase_size; 1395 else { 1396 sz = card->pref_erase % card->erase_size; 1397 if (sz) 1398 card->pref_erase += card->erase_size - sz; 1399 } 1400 } 1401 } 1402 1403 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, 1404 unsigned int arg, unsigned int qty) 1405 { 1406 unsigned int erase_timeout; 1407 1408 if (card->ext_csd.erase_group_def & 1) { 1409 /* High Capacity Erase Group Size uses HC timeouts */ 1410 if (arg == MMC_TRIM_ARG) 1411 erase_timeout = card->ext_csd.trim_timeout; 1412 else 1413 erase_timeout = card->ext_csd.hc_erase_timeout; 1414 } else { 1415 /* CSD Erase Group Size uses write timeout */ 1416 unsigned int mult = (10 << card->csd.r2w_factor); 1417 unsigned int timeout_clks = card->csd.tacc_clks * mult; 1418 unsigned int timeout_us; 1419 1420 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ 1421 if (card->csd.tacc_ns < 1000000) 1422 timeout_us = (card->csd.tacc_ns * mult) / 1000; 1423 else 1424 timeout_us = (card->csd.tacc_ns / 1000) * mult; 1425 1426 /* 1427 * ios.clock is only a target. The real clock rate might be 1428 * less but not that much less, so fudge it by multiplying by 2. 1429 */ 1430 timeout_clks <<= 1; 1431 timeout_us += (timeout_clks * 1000) / 1432 (mmc_host_clk_rate(card->host) / 1000); 1433 1434 erase_timeout = timeout_us / 1000; 1435 1436 /* 1437 * Theoretically, the calculation could underflow so round up 1438 * to 1ms in that case. 1439 */ 1440 if (!erase_timeout) 1441 erase_timeout = 1; 1442 } 1443 1444 /* Multiplier for secure operations */ 1445 if (arg & MMC_SECURE_ARGS) { 1446 if (arg == MMC_SECURE_ERASE_ARG) 1447 erase_timeout *= card->ext_csd.sec_erase_mult; 1448 else 1449 erase_timeout *= card->ext_csd.sec_trim_mult; 1450 } 1451 1452 erase_timeout *= qty; 1453 1454 /* 1455 * Ensure at least a 1 second timeout for SPI as per 1456 * 'mmc_set_data_timeout()' 1457 */ 1458 if (mmc_host_is_spi(card->host) && erase_timeout < 1000) 1459 erase_timeout = 1000; 1460 1461 return erase_timeout; 1462 } 1463 1464 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, 1465 unsigned int arg, 1466 unsigned int qty) 1467 { 1468 unsigned int erase_timeout; 1469 1470 if (card->ssr.erase_timeout) { 1471 /* Erase timeout specified in SD Status Register (SSR) */ 1472 erase_timeout = card->ssr.erase_timeout * qty + 1473 card->ssr.erase_offset; 1474 } else { 1475 /* 1476 * Erase timeout not specified in SD Status Register (SSR) so 1477 * use 250ms per write block. 1478 */ 1479 erase_timeout = 250 * qty; 1480 } 1481 1482 /* Must not be less than 1 second */ 1483 if (erase_timeout < 1000) 1484 erase_timeout = 1000; 1485 1486 return erase_timeout; 1487 } 1488 1489 static unsigned int mmc_erase_timeout(struct mmc_card *card, 1490 unsigned int arg, 1491 unsigned int qty) 1492 { 1493 if (mmc_card_sd(card)) 1494 return mmc_sd_erase_timeout(card, arg, qty); 1495 else 1496 return mmc_mmc_erase_timeout(card, arg, qty); 1497 } 1498 1499 static int mmc_do_erase(struct mmc_card *card, unsigned int from, 1500 unsigned int to, unsigned int arg) 1501 { 1502 struct mmc_command cmd = {0}; 1503 unsigned int qty = 0; 1504 int err; 1505 1506 /* 1507 * qty is used to calculate the erase timeout which depends on how many 1508 * erase groups (or allocation units in SD terminology) are affected. 1509 * We count erasing part of an erase group as one erase group. 1510 * For SD, the allocation units are always a power of 2. For MMC, the 1511 * erase group size is almost certainly also power of 2, but it does not 1512 * seem to insist on that in the JEDEC standard, so we fall back to 1513 * division in that case. SD may not specify an allocation unit size, 1514 * in which case the timeout is based on the number of write blocks. 1515 * 1516 * Note that the timeout for secure trim 2 will only be correct if the 1517 * number of erase groups specified is the same as the total of all 1518 * preceding secure trim 1 commands. Since the power may have been 1519 * lost since the secure trim 1 commands occurred, it is generally 1520 * impossible to calculate the secure trim 2 timeout correctly. 1521 */ 1522 if (card->erase_shift) 1523 qty += ((to >> card->erase_shift) - 1524 (from >> card->erase_shift)) + 1; 1525 else if (mmc_card_sd(card)) 1526 qty += to - from + 1; 1527 else 1528 qty += ((to / card->erase_size) - 1529 (from / card->erase_size)) + 1; 1530 1531 if (!mmc_card_blockaddr(card)) { 1532 from <<= 9; 1533 to <<= 9; 1534 } 1535 1536 if (mmc_card_sd(card)) 1537 cmd.opcode = SD_ERASE_WR_BLK_START; 1538 else 1539 cmd.opcode = MMC_ERASE_GROUP_START; 1540 cmd.arg = from; 1541 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1542 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1543 if (err) { 1544 pr_err("mmc_erase: group start error %d, " 1545 "status %#x\n", err, cmd.resp[0]); 1546 err = -EIO; 1547 goto out; 1548 } 1549 1550 memset(&cmd, 0, sizeof(struct mmc_command)); 1551 if (mmc_card_sd(card)) 1552 cmd.opcode = SD_ERASE_WR_BLK_END; 1553 else 1554 cmd.opcode = MMC_ERASE_GROUP_END; 1555 cmd.arg = to; 1556 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1557 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1558 if (err) { 1559 pr_err("mmc_erase: group end error %d, status %#x\n", 1560 err, cmd.resp[0]); 1561 err = -EIO; 1562 goto out; 1563 } 1564 1565 memset(&cmd, 0, sizeof(struct mmc_command)); 1566 cmd.opcode = MMC_ERASE; 1567 cmd.arg = arg; 1568 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1569 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); 1570 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1571 if (err) { 1572 pr_err("mmc_erase: erase error %d, status %#x\n", 1573 err, cmd.resp[0]); 1574 err = -EIO; 1575 goto out; 1576 } 1577 1578 if (mmc_host_is_spi(card->host)) 1579 goto out; 1580 1581 do { 1582 memset(&cmd, 0, sizeof(struct mmc_command)); 1583 cmd.opcode = MMC_SEND_STATUS; 1584 cmd.arg = card->rca << 16; 1585 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1586 /* Do not retry else we can't see errors */ 1587 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1588 if (err || (cmd.resp[0] & 0xFDF92000)) { 1589 pr_err("error %d requesting status %#x\n", 1590 err, cmd.resp[0]); 1591 err = -EIO; 1592 goto out; 1593 } 1594 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || 1595 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG); 1596 out: 1597 return err; 1598 } 1599 1600 /** 1601 * mmc_erase - erase sectors. 1602 * @card: card to erase 1603 * @from: first sector to erase 1604 * @nr: number of sectors to erase 1605 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) 1606 * 1607 * Caller must claim host before calling this function. 1608 */ 1609 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, 1610 unsigned int arg) 1611 { 1612 unsigned int rem, to = from + nr; 1613 1614 if (!(card->host->caps & MMC_CAP_ERASE) || 1615 !(card->csd.cmdclass & CCC_ERASE)) 1616 return -EOPNOTSUPP; 1617 1618 if (!card->erase_size) 1619 return -EOPNOTSUPP; 1620 1621 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) 1622 return -EOPNOTSUPP; 1623 1624 if ((arg & MMC_SECURE_ARGS) && 1625 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) 1626 return -EOPNOTSUPP; 1627 1628 if ((arg & MMC_TRIM_ARGS) && 1629 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) 1630 return -EOPNOTSUPP; 1631 1632 if (arg == MMC_SECURE_ERASE_ARG) { 1633 if (from % card->erase_size || nr % card->erase_size) 1634 return -EINVAL; 1635 } 1636 1637 if (arg == MMC_ERASE_ARG) { 1638 rem = from % card->erase_size; 1639 if (rem) { 1640 rem = card->erase_size - rem; 1641 from += rem; 1642 if (nr > rem) 1643 nr -= rem; 1644 else 1645 return 0; 1646 } 1647 rem = nr % card->erase_size; 1648 if (rem) 1649 nr -= rem; 1650 } 1651 1652 if (nr == 0) 1653 return 0; 1654 1655 to = from + nr; 1656 1657 if (to <= from) 1658 return -EINVAL; 1659 1660 /* 'from' and 'to' are inclusive */ 1661 to -= 1; 1662 1663 return mmc_do_erase(card, from, to, arg); 1664 } 1665 EXPORT_SYMBOL(mmc_erase); 1666 1667 int mmc_can_erase(struct mmc_card *card) 1668 { 1669 if ((card->host->caps & MMC_CAP_ERASE) && 1670 (card->csd.cmdclass & CCC_ERASE) && card->erase_size) 1671 return 1; 1672 return 0; 1673 } 1674 EXPORT_SYMBOL(mmc_can_erase); 1675 1676 int mmc_can_trim(struct mmc_card *card) 1677 { 1678 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) 1679 return 1; 1680 if (mmc_can_discard(card)) 1681 return 1; 1682 return 0; 1683 } 1684 EXPORT_SYMBOL(mmc_can_trim); 1685 1686 int mmc_can_discard(struct mmc_card *card) 1687 { 1688 /* 1689 * As there's no way to detect the discard support bit at v4.5 1690 * use the s/w feature support filed. 1691 */ 1692 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE) 1693 return 1; 1694 return 0; 1695 } 1696 EXPORT_SYMBOL(mmc_can_discard); 1697 1698 int mmc_can_sanitize(struct mmc_card *card) 1699 { 1700 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) 1701 return 1; 1702 return 0; 1703 } 1704 EXPORT_SYMBOL(mmc_can_sanitize); 1705 1706 int mmc_can_secure_erase_trim(struct mmc_card *card) 1707 { 1708 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) 1709 return 1; 1710 return 0; 1711 } 1712 EXPORT_SYMBOL(mmc_can_secure_erase_trim); 1713 1714 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, 1715 unsigned int nr) 1716 { 1717 if (!card->erase_size) 1718 return 0; 1719 if (from % card->erase_size || nr % card->erase_size) 1720 return 0; 1721 return 1; 1722 } 1723 EXPORT_SYMBOL(mmc_erase_group_aligned); 1724 1725 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, 1726 unsigned int arg) 1727 { 1728 struct mmc_host *host = card->host; 1729 unsigned int max_discard, x, y, qty = 0, max_qty, timeout; 1730 unsigned int last_timeout = 0; 1731 1732 if (card->erase_shift) 1733 max_qty = UINT_MAX >> card->erase_shift; 1734 else if (mmc_card_sd(card)) 1735 max_qty = UINT_MAX; 1736 else 1737 max_qty = UINT_MAX / card->erase_size; 1738 1739 /* Find the largest qty with an OK timeout */ 1740 do { 1741 y = 0; 1742 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { 1743 timeout = mmc_erase_timeout(card, arg, qty + x); 1744 if (timeout > host->max_discard_to) 1745 break; 1746 if (timeout < last_timeout) 1747 break; 1748 last_timeout = timeout; 1749 y = x; 1750 } 1751 qty += y; 1752 } while (y); 1753 1754 if (!qty) 1755 return 0; 1756 1757 if (qty == 1) 1758 return 1; 1759 1760 /* Convert qty to sectors */ 1761 if (card->erase_shift) 1762 max_discard = --qty << card->erase_shift; 1763 else if (mmc_card_sd(card)) 1764 max_discard = qty; 1765 else 1766 max_discard = --qty * card->erase_size; 1767 1768 return max_discard; 1769 } 1770 1771 unsigned int mmc_calc_max_discard(struct mmc_card *card) 1772 { 1773 struct mmc_host *host = card->host; 1774 unsigned int max_discard, max_trim; 1775 1776 if (!host->max_discard_to) 1777 return UINT_MAX; 1778 1779 /* 1780 * Without erase_group_def set, MMC erase timeout depends on clock 1781 * frequence which can change. In that case, the best choice is 1782 * just the preferred erase size. 1783 */ 1784 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) 1785 return card->pref_erase; 1786 1787 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); 1788 if (mmc_can_trim(card)) { 1789 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); 1790 if (max_trim < max_discard) 1791 max_discard = max_trim; 1792 } else if (max_discard < card->erase_size) { 1793 max_discard = 0; 1794 } 1795 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", 1796 mmc_hostname(host), max_discard, host->max_discard_to); 1797 return max_discard; 1798 } 1799 EXPORT_SYMBOL(mmc_calc_max_discard); 1800 1801 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) 1802 { 1803 struct mmc_command cmd = {0}; 1804 1805 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card)) 1806 return 0; 1807 1808 cmd.opcode = MMC_SET_BLOCKLEN; 1809 cmd.arg = blocklen; 1810 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1811 return mmc_wait_for_cmd(card->host, &cmd, 5); 1812 } 1813 EXPORT_SYMBOL(mmc_set_blocklen); 1814 1815 static void mmc_hw_reset_for_init(struct mmc_host *host) 1816 { 1817 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) 1818 return; 1819 mmc_host_clk_hold(host); 1820 host->ops->hw_reset(host); 1821 mmc_host_clk_release(host); 1822 } 1823 1824 int mmc_can_reset(struct mmc_card *card) 1825 { 1826 u8 rst_n_function; 1827 1828 if (!mmc_card_mmc(card)) 1829 return 0; 1830 rst_n_function = card->ext_csd.rst_n_function; 1831 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED) 1832 return 0; 1833 return 1; 1834 } 1835 EXPORT_SYMBOL(mmc_can_reset); 1836 1837 static int mmc_do_hw_reset(struct mmc_host *host, int check) 1838 { 1839 struct mmc_card *card = host->card; 1840 1841 if (!host->bus_ops->power_restore) 1842 return -EOPNOTSUPP; 1843 1844 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) 1845 return -EOPNOTSUPP; 1846 1847 if (!card) 1848 return -EINVAL; 1849 1850 if (!mmc_can_reset(card)) 1851 return -EOPNOTSUPP; 1852 1853 mmc_host_clk_hold(host); 1854 mmc_set_clock(host, host->f_init); 1855 1856 host->ops->hw_reset(host); 1857 1858 /* If the reset has happened, then a status command will fail */ 1859 if (check) { 1860 struct mmc_command cmd = {0}; 1861 int err; 1862 1863 cmd.opcode = MMC_SEND_STATUS; 1864 if (!mmc_host_is_spi(card->host)) 1865 cmd.arg = card->rca << 16; 1866 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; 1867 err = mmc_wait_for_cmd(card->host, &cmd, 0); 1868 if (!err) { 1869 mmc_host_clk_release(host); 1870 return -ENOSYS; 1871 } 1872 } 1873 1874 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR); 1875 if (mmc_host_is_spi(host)) { 1876 host->ios.chip_select = MMC_CS_HIGH; 1877 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; 1878 } else { 1879 host->ios.chip_select = MMC_CS_DONTCARE; 1880 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; 1881 } 1882 host->ios.bus_width = MMC_BUS_WIDTH_1; 1883 host->ios.timing = MMC_TIMING_LEGACY; 1884 mmc_set_ios(host); 1885 1886 mmc_host_clk_release(host); 1887 1888 return host->bus_ops->power_restore(host); 1889 } 1890 1891 int mmc_hw_reset(struct mmc_host *host) 1892 { 1893 return mmc_do_hw_reset(host, 0); 1894 } 1895 EXPORT_SYMBOL(mmc_hw_reset); 1896 1897 int mmc_hw_reset_check(struct mmc_host *host) 1898 { 1899 return mmc_do_hw_reset(host, 1); 1900 } 1901 EXPORT_SYMBOL(mmc_hw_reset_check); 1902 1903 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) 1904 { 1905 host->f_init = freq; 1906 1907 #ifdef CONFIG_MMC_DEBUG 1908 pr_info("%s: %s: trying to init card at %u Hz\n", 1909 mmc_hostname(host), __func__, host->f_init); 1910 #endif 1911 mmc_power_up(host); 1912 1913 /* 1914 * Some eMMCs (with VCCQ always on) may not be reset after power up, so 1915 * do a hardware reset if possible. 1916 */ 1917 mmc_hw_reset_for_init(host); 1918 1919 /* Initialization should be done at 3.3 V I/O voltage. */ 1920 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0); 1921 1922 /* 1923 * sdio_reset sends CMD52 to reset card. Since we do not know 1924 * if the card is being re-initialized, just send it. CMD52 1925 * should be ignored by SD/eMMC cards. 1926 */ 1927 sdio_reset(host); 1928 mmc_go_idle(host); 1929 1930 mmc_send_if_cond(host, host->ocr_avail); 1931 1932 /* Order's important: probe SDIO, then SD, then MMC */ 1933 if (!mmc_attach_sdio(host)) 1934 return 0; 1935 if (!mmc_attach_sd(host)) 1936 return 0; 1937 if (!mmc_attach_mmc(host)) 1938 return 0; 1939 1940 mmc_power_off(host); 1941 return -EIO; 1942 } 1943 1944 int _mmc_detect_card_removed(struct mmc_host *host) 1945 { 1946 int ret; 1947 1948 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive) 1949 return 0; 1950 1951 if (!host->card || mmc_card_removed(host->card)) 1952 return 1; 1953 1954 ret = host->bus_ops->alive(host); 1955 if (ret) { 1956 mmc_card_set_removed(host->card); 1957 pr_debug("%s: card remove detected\n", mmc_hostname(host)); 1958 } 1959 1960 return ret; 1961 } 1962 1963 int mmc_detect_card_removed(struct mmc_host *host) 1964 { 1965 struct mmc_card *card = host->card; 1966 int ret; 1967 1968 WARN_ON(!host->claimed); 1969 1970 if (!card) 1971 return 1; 1972 1973 ret = mmc_card_removed(card); 1974 /* 1975 * The card will be considered unchanged unless we have been asked to 1976 * detect a change or host requires polling to provide card detection. 1977 */ 1978 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL) && 1979 !(host->caps2 & MMC_CAP2_DETECT_ON_ERR)) 1980 return ret; 1981 1982 host->detect_change = 0; 1983 if (!ret) { 1984 ret = _mmc_detect_card_removed(host); 1985 if (ret && (host->caps2 & MMC_CAP2_DETECT_ON_ERR)) { 1986 /* 1987 * Schedule a detect work as soon as possible to let a 1988 * rescan handle the card removal. 1989 */ 1990 cancel_delayed_work(&host->detect); 1991 mmc_detect_change(host, 0); 1992 } 1993 } 1994 1995 return ret; 1996 } 1997 EXPORT_SYMBOL(mmc_detect_card_removed); 1998 1999 void mmc_rescan(struct work_struct *work) 2000 { 2001 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; 2002 struct mmc_host *host = 2003 container_of(work, struct mmc_host, detect.work); 2004 int i; 2005 2006 if (host->rescan_disable) 2007 return; 2008 2009 mmc_bus_get(host); 2010 2011 /* 2012 * if there is a _removable_ card registered, check whether it is 2013 * still present 2014 */ 2015 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead 2016 && !(host->caps & MMC_CAP_NONREMOVABLE)) 2017 host->bus_ops->detect(host); 2018 2019 host->detect_change = 0; 2020 2021 /* 2022 * Let mmc_bus_put() free the bus/bus_ops if we've found that 2023 * the card is no longer present. 2024 */ 2025 mmc_bus_put(host); 2026 mmc_bus_get(host); 2027 2028 /* if there still is a card present, stop here */ 2029 if (host->bus_ops != NULL) { 2030 mmc_bus_put(host); 2031 goto out; 2032 } 2033 2034 /* 2035 * Only we can add a new handler, so it's safe to 2036 * release the lock here. 2037 */ 2038 mmc_bus_put(host); 2039 2040 if (host->ops->get_cd && host->ops->get_cd(host) == 0) 2041 goto out; 2042 2043 mmc_claim_host(host); 2044 for (i = 0; i < ARRAY_SIZE(freqs); i++) { 2045 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) 2046 break; 2047 if (freqs[i] <= host->f_min) 2048 break; 2049 } 2050 mmc_release_host(host); 2051 2052 out: 2053 if (host->caps & MMC_CAP_NEEDS_POLL) 2054 mmc_schedule_delayed_work(&host->detect, HZ); 2055 } 2056 2057 void mmc_start_host(struct mmc_host *host) 2058 { 2059 mmc_power_off(host); 2060 mmc_detect_change(host, 0); 2061 } 2062 2063 void mmc_stop_host(struct mmc_host *host) 2064 { 2065 #ifdef CONFIG_MMC_DEBUG 2066 unsigned long flags; 2067 spin_lock_irqsave(&host->lock, flags); 2068 host->removed = 1; 2069 spin_unlock_irqrestore(&host->lock, flags); 2070 #endif 2071 2072 cancel_delayed_work_sync(&host->detect); 2073 mmc_flush_scheduled_work(); 2074 2075 /* clear pm flags now and let card drivers set them as needed */ 2076 host->pm_flags = 0; 2077 2078 mmc_bus_get(host); 2079 if (host->bus_ops && !host->bus_dead) { 2080 /* Calling bus_ops->remove() with a claimed host can deadlock */ 2081 if (host->bus_ops->remove) 2082 host->bus_ops->remove(host); 2083 2084 mmc_claim_host(host); 2085 mmc_detach_bus(host); 2086 mmc_power_off(host); 2087 mmc_release_host(host); 2088 mmc_bus_put(host); 2089 return; 2090 } 2091 mmc_bus_put(host); 2092 2093 BUG_ON(host->card); 2094 2095 mmc_power_off(host); 2096 } 2097 2098 int mmc_power_save_host(struct mmc_host *host) 2099 { 2100 int ret = 0; 2101 2102 #ifdef CONFIG_MMC_DEBUG 2103 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__); 2104 #endif 2105 2106 mmc_bus_get(host); 2107 2108 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 2109 mmc_bus_put(host); 2110 return -EINVAL; 2111 } 2112 2113 if (host->bus_ops->power_save) 2114 ret = host->bus_ops->power_save(host); 2115 2116 mmc_bus_put(host); 2117 2118 mmc_power_off(host); 2119 2120 return ret; 2121 } 2122 EXPORT_SYMBOL(mmc_power_save_host); 2123 2124 int mmc_power_restore_host(struct mmc_host *host) 2125 { 2126 int ret; 2127 2128 #ifdef CONFIG_MMC_DEBUG 2129 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__); 2130 #endif 2131 2132 mmc_bus_get(host); 2133 2134 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { 2135 mmc_bus_put(host); 2136 return -EINVAL; 2137 } 2138 2139 mmc_power_up(host); 2140 ret = host->bus_ops->power_restore(host); 2141 2142 mmc_bus_put(host); 2143 2144 return ret; 2145 } 2146 EXPORT_SYMBOL(mmc_power_restore_host); 2147 2148 int mmc_card_awake(struct mmc_host *host) 2149 { 2150 int err = -ENOSYS; 2151 2152 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD) 2153 return 0; 2154 2155 mmc_bus_get(host); 2156 2157 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) 2158 err = host->bus_ops->awake(host); 2159 2160 mmc_bus_put(host); 2161 2162 return err; 2163 } 2164 EXPORT_SYMBOL(mmc_card_awake); 2165 2166 int mmc_card_sleep(struct mmc_host *host) 2167 { 2168 int err = -ENOSYS; 2169 2170 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD) 2171 return 0; 2172 2173 mmc_bus_get(host); 2174 2175 if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep) 2176 err = host->bus_ops->sleep(host); 2177 2178 mmc_bus_put(host); 2179 2180 return err; 2181 } 2182 EXPORT_SYMBOL(mmc_card_sleep); 2183 2184 int mmc_card_can_sleep(struct mmc_host *host) 2185 { 2186 struct mmc_card *card = host->card; 2187 2188 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) 2189 return 1; 2190 return 0; 2191 } 2192 EXPORT_SYMBOL(mmc_card_can_sleep); 2193 2194 /* 2195 * Flush the cache to the non-volatile storage. 2196 */ 2197 int mmc_flush_cache(struct mmc_card *card) 2198 { 2199 struct mmc_host *host = card->host; 2200 int err = 0; 2201 2202 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL)) 2203 return err; 2204 2205 if (mmc_card_mmc(card) && 2206 (card->ext_csd.cache_size > 0) && 2207 (card->ext_csd.cache_ctrl & 1)) { 2208 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 2209 EXT_CSD_FLUSH_CACHE, 1, 0); 2210 if (err) 2211 pr_err("%s: cache flush error %d\n", 2212 mmc_hostname(card->host), err); 2213 } 2214 2215 return err; 2216 } 2217 EXPORT_SYMBOL(mmc_flush_cache); 2218 2219 /* 2220 * Turn the cache ON/OFF. 2221 * Turning the cache OFF shall trigger flushing of the data 2222 * to the non-volatile storage. 2223 */ 2224 int mmc_cache_ctrl(struct mmc_host *host, u8 enable) 2225 { 2226 struct mmc_card *card = host->card; 2227 unsigned int timeout; 2228 int err = 0; 2229 2230 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) || 2231 mmc_card_is_removable(host)) 2232 return err; 2233 2234 if (card && mmc_card_mmc(card) && 2235 (card->ext_csd.cache_size > 0)) { 2236 enable = !!enable; 2237 2238 if (card->ext_csd.cache_ctrl ^ enable) { 2239 timeout = enable ? card->ext_csd.generic_cmd6_time : 0; 2240 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, 2241 EXT_CSD_CACHE_CTRL, enable, timeout); 2242 if (err) 2243 pr_err("%s: cache %s error %d\n", 2244 mmc_hostname(card->host), 2245 enable ? "on" : "off", 2246 err); 2247 else 2248 card->ext_csd.cache_ctrl = enable; 2249 } 2250 } 2251 2252 return err; 2253 } 2254 EXPORT_SYMBOL(mmc_cache_ctrl); 2255 2256 #ifdef CONFIG_PM 2257 2258 /** 2259 * mmc_suspend_host - suspend a host 2260 * @host: mmc host 2261 */ 2262 int mmc_suspend_host(struct mmc_host *host) 2263 { 2264 int err = 0; 2265 2266 cancel_delayed_work(&host->detect); 2267 mmc_flush_scheduled_work(); 2268 if (mmc_try_claim_host(host)) { 2269 err = mmc_cache_ctrl(host, 0); 2270 mmc_release_host(host); 2271 } else { 2272 err = -EBUSY; 2273 } 2274 2275 if (err) 2276 goto out; 2277 2278 mmc_bus_get(host); 2279 if (host->bus_ops && !host->bus_dead) { 2280 2281 /* 2282 * A long response time is not acceptable for device drivers 2283 * when doing suspend. Prevent mmc_claim_host in the suspend 2284 * sequence, to potentially wait "forever" by trying to 2285 * pre-claim the host. 2286 */ 2287 if (mmc_try_claim_host(host)) { 2288 if (host->bus_ops->suspend) { 2289 err = host->bus_ops->suspend(host); 2290 } 2291 mmc_release_host(host); 2292 2293 if (err == -ENOSYS || !host->bus_ops->resume) { 2294 /* 2295 * We simply "remove" the card in this case. 2296 * It will be redetected on resume. (Calling 2297 * bus_ops->remove() with a claimed host can 2298 * deadlock.) 2299 */ 2300 if (host->bus_ops->remove) 2301 host->bus_ops->remove(host); 2302 mmc_claim_host(host); 2303 mmc_detach_bus(host); 2304 mmc_power_off(host); 2305 mmc_release_host(host); 2306 host->pm_flags = 0; 2307 err = 0; 2308 } 2309 } else { 2310 err = -EBUSY; 2311 } 2312 } 2313 mmc_bus_put(host); 2314 2315 if (!err && !mmc_card_keep_power(host)) 2316 mmc_power_off(host); 2317 2318 out: 2319 return err; 2320 } 2321 2322 EXPORT_SYMBOL(mmc_suspend_host); 2323 2324 /** 2325 * mmc_resume_host - resume a previously suspended host 2326 * @host: mmc host 2327 */ 2328 int mmc_resume_host(struct mmc_host *host) 2329 { 2330 int err = 0; 2331 2332 mmc_bus_get(host); 2333 if (host->bus_ops && !host->bus_dead) { 2334 if (!mmc_card_keep_power(host)) { 2335 mmc_power_up(host); 2336 mmc_select_voltage(host, host->ocr); 2337 /* 2338 * Tell runtime PM core we just powered up the card, 2339 * since it still believes the card is powered off. 2340 * Note that currently runtime PM is only enabled 2341 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD 2342 */ 2343 if (mmc_card_sdio(host->card) && 2344 (host->caps & MMC_CAP_POWER_OFF_CARD)) { 2345 pm_runtime_disable(&host->card->dev); 2346 pm_runtime_set_active(&host->card->dev); 2347 pm_runtime_enable(&host->card->dev); 2348 } 2349 } 2350 BUG_ON(!host->bus_ops->resume); 2351 err = host->bus_ops->resume(host); 2352 if (err) { 2353 pr_warning("%s: error %d during resume " 2354 "(card was removed?)\n", 2355 mmc_hostname(host), err); 2356 err = 0; 2357 } 2358 } 2359 host->pm_flags &= ~MMC_PM_KEEP_POWER; 2360 mmc_bus_put(host); 2361 2362 return err; 2363 } 2364 EXPORT_SYMBOL(mmc_resume_host); 2365 2366 /* Do the card removal on suspend if card is assumed removeable 2367 * Do that in pm notifier while userspace isn't yet frozen, so we will be able 2368 to sync the card. 2369 */ 2370 int mmc_pm_notify(struct notifier_block *notify_block, 2371 unsigned long mode, void *unused) 2372 { 2373 struct mmc_host *host = container_of( 2374 notify_block, struct mmc_host, pm_notify); 2375 unsigned long flags; 2376 2377 2378 switch (mode) { 2379 case PM_HIBERNATION_PREPARE: 2380 case PM_SUSPEND_PREPARE: 2381 2382 spin_lock_irqsave(&host->lock, flags); 2383 host->rescan_disable = 1; 2384 host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT; 2385 spin_unlock_irqrestore(&host->lock, flags); 2386 cancel_delayed_work_sync(&host->detect); 2387 2388 if (!host->bus_ops || host->bus_ops->suspend) 2389 break; 2390 2391 /* Calling bus_ops->remove() with a claimed host can deadlock */ 2392 if (host->bus_ops->remove) 2393 host->bus_ops->remove(host); 2394 2395 mmc_claim_host(host); 2396 mmc_detach_bus(host); 2397 mmc_power_off(host); 2398 mmc_release_host(host); 2399 host->pm_flags = 0; 2400 break; 2401 2402 case PM_POST_SUSPEND: 2403 case PM_POST_HIBERNATION: 2404 case PM_POST_RESTORE: 2405 2406 spin_lock_irqsave(&host->lock, flags); 2407 host->rescan_disable = 0; 2408 host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG; 2409 spin_unlock_irqrestore(&host->lock, flags); 2410 mmc_detect_change(host, 0); 2411 2412 } 2413 2414 return 0; 2415 } 2416 #endif 2417 2418 static int __init mmc_init(void) 2419 { 2420 int ret; 2421 2422 workqueue = alloc_ordered_workqueue("kmmcd", 0); 2423 if (!workqueue) 2424 return -ENOMEM; 2425 2426 ret = mmc_register_bus(); 2427 if (ret) 2428 goto destroy_workqueue; 2429 2430 ret = mmc_register_host_class(); 2431 if (ret) 2432 goto unregister_bus; 2433 2434 ret = sdio_register_bus(); 2435 if (ret) 2436 goto unregister_host_class; 2437 2438 return 0; 2439 2440 unregister_host_class: 2441 mmc_unregister_host_class(); 2442 unregister_bus: 2443 mmc_unregister_bus(); 2444 destroy_workqueue: 2445 destroy_workqueue(workqueue); 2446 2447 return ret; 2448 } 2449 2450 static void __exit mmc_exit(void) 2451 { 2452 sdio_unregister_bus(); 2453 mmc_unregister_host_class(); 2454 mmc_unregister_bus(); 2455 destroy_workqueue(workqueue); 2456 } 2457 2458 subsys_initcall(mmc_init); 2459 module_exit(mmc_exit); 2460 2461 MODULE_LICENSE("GPL"); 2462