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