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