2 * linux/drivers/mmc/core/core.c
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.
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.
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>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
37 #include <linux/mmc/slot-gpio.h>
49 /* If the device is not responding */
50 #define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
53 * Background operations can take a long time, depending on the housekeeping
54 * operations the card has to perform.
56 #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */
58 static struct workqueue_struct *workqueue;
59 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
62 * Enabling software CRCs on the data blocks can be a significant (30%)
63 * performance cost, and for other reasons may not always be desired.
64 * So we allow it it to be disabled.
67 module_param(use_spi_crc, bool, 0);
70 * Internal function. Schedule delayed work in the MMC work queue.
72 static int mmc_schedule_delayed_work(struct delayed_work *work,
75 return queue_delayed_work(workqueue, work, delay);
79 * Internal function. Flush all scheduled work from the MMC work queue.
81 static void mmc_flush_scheduled_work(void)
83 flush_workqueue(workqueue);
86 #ifdef CONFIG_FAIL_MMC_REQUEST
89 * Internal function. Inject random data errors.
90 * If mmc_data is NULL no errors are injected.
92 static void mmc_should_fail_request(struct mmc_host *host,
93 struct mmc_request *mrq)
95 struct mmc_command *cmd = mrq->cmd;
96 struct mmc_data *data = mrq->data;
97 static const int data_errors[] = {
106 if (cmd->error || data->error ||
107 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
110 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
111 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
114 #else /* CONFIG_FAIL_MMC_REQUEST */
116 static inline void mmc_should_fail_request(struct mmc_host *host,
117 struct mmc_request *mrq)
121 #endif /* CONFIG_FAIL_MMC_REQUEST */
124 * mmc_request_done - finish processing an MMC request
125 * @host: MMC host which completed request
126 * @mrq: MMC request which request
128 * MMC drivers should call this function when they have completed
129 * their processing of a request.
131 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
133 struct mmc_command *cmd = mrq->cmd;
134 int err = cmd->error;
136 /* Flag re-tuning needed on CRC errors */
137 if ((cmd->opcode != MMC_SEND_TUNING_BLOCK &&
138 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
139 (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
140 (mrq->data && mrq->data->error == -EILSEQ) ||
141 (mrq->stop && mrq->stop->error == -EILSEQ)))
142 mmc_retune_needed(host);
144 if (err && cmd->retries && mmc_host_is_spi(host)) {
145 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
149 if (err && cmd->retries && !mmc_card_removed(host->card)) {
151 * Request starter must handle retries - see
152 * mmc_wait_for_req_done().
157 mmc_should_fail_request(host, mrq);
159 led_trigger_event(host->led, LED_OFF);
162 pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
163 mmc_hostname(host), mrq->sbc->opcode,
165 mrq->sbc->resp[0], mrq->sbc->resp[1],
166 mrq->sbc->resp[2], mrq->sbc->resp[3]);
169 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
170 mmc_hostname(host), cmd->opcode, err,
171 cmd->resp[0], cmd->resp[1],
172 cmd->resp[2], cmd->resp[3]);
175 pr_debug("%s: %d bytes transferred: %d\n",
177 mrq->data->bytes_xfered, mrq->data->error);
181 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
182 mmc_hostname(host), mrq->stop->opcode,
184 mrq->stop->resp[0], mrq->stop->resp[1],
185 mrq->stop->resp[2], mrq->stop->resp[3]);
191 mmc_host_clk_release(host);
195 EXPORT_SYMBOL(mmc_request_done);
197 static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
201 /* Assumes host controller has been runtime resumed by mmc_claim_host */
202 err = mmc_retune(host);
204 mrq->cmd->error = err;
205 mmc_request_done(host, mrq);
209 host->ops->request(host, mrq);
212 static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
214 #ifdef CONFIG_MMC_DEBUG
216 struct scatterlist *sg;
218 mmc_retune_hold(host);
220 if (mmc_card_removed(host->card))
224 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
225 mmc_hostname(host), mrq->sbc->opcode,
226 mrq->sbc->arg, mrq->sbc->flags);
229 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
230 mmc_hostname(host), mrq->cmd->opcode,
231 mrq->cmd->arg, mrq->cmd->flags);
234 pr_debug("%s: blksz %d blocks %d flags %08x "
235 "tsac %d ms nsac %d\n",
236 mmc_hostname(host), mrq->data->blksz,
237 mrq->data->blocks, mrq->data->flags,
238 mrq->data->timeout_ns / 1000000,
239 mrq->data->timeout_clks);
243 pr_debug("%s: CMD%u arg %08x flags %08x\n",
244 mmc_hostname(host), mrq->stop->opcode,
245 mrq->stop->arg, mrq->stop->flags);
248 WARN_ON(!host->claimed);
257 BUG_ON(mrq->data->blksz > host->max_blk_size);
258 BUG_ON(mrq->data->blocks > host->max_blk_count);
259 BUG_ON(mrq->data->blocks * mrq->data->blksz >
262 #ifdef CONFIG_MMC_DEBUG
264 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
266 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
269 mrq->cmd->data = mrq->data;
270 mrq->data->error = 0;
271 mrq->data->mrq = mrq;
273 mrq->data->stop = mrq->stop;
274 mrq->stop->error = 0;
275 mrq->stop->mrq = mrq;
278 mmc_host_clk_hold(host);
279 led_trigger_event(host->led, LED_FULL);
280 __mmc_start_request(host, mrq);
286 * mmc_start_bkops - start BKOPS for supported cards
287 * @card: MMC card to start BKOPS
288 * @form_exception: A flag to indicate if this function was
289 * called due to an exception raised by the card
291 * Start background operations whenever requested.
292 * When the urgent BKOPS bit is set in a R1 command response
293 * then background operations should be started immediately.
295 void mmc_start_bkops(struct mmc_card *card, bool from_exception)
299 bool use_busy_signal;
303 if (!card->ext_csd.man_bkops_en || mmc_card_doing_bkops(card))
306 err = mmc_read_bkops_status(card);
308 pr_err("%s: Failed to read bkops status: %d\n",
309 mmc_hostname(card->host), err);
313 if (!card->ext_csd.raw_bkops_status)
316 if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
320 mmc_claim_host(card->host);
321 if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
322 timeout = MMC_BKOPS_MAX_TIMEOUT;
323 use_busy_signal = true;
326 use_busy_signal = false;
329 mmc_retune_hold(card->host);
331 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
332 EXT_CSD_BKOPS_START, 1, timeout,
333 use_busy_signal, true, false);
335 pr_warn("%s: Error %d starting bkops\n",
336 mmc_hostname(card->host), err);
337 mmc_retune_release(card->host);
342 * For urgent bkops status (LEVEL_2 and more)
343 * bkops executed synchronously, otherwise
344 * the operation is in progress
346 if (!use_busy_signal)
347 mmc_card_set_doing_bkops(card);
349 mmc_retune_release(card->host);
351 mmc_release_host(card->host);
353 EXPORT_SYMBOL(mmc_start_bkops);
356 * mmc_wait_data_done() - done callback for data request
357 * @mrq: done data request
359 * Wakes up mmc context, passed as a callback to host controller driver
361 static void mmc_wait_data_done(struct mmc_request *mrq)
363 struct mmc_context_info *context_info = &mrq->host->context_info;
365 context_info->is_done_rcv = true;
366 wake_up_interruptible(&context_info->wait);
369 static void mmc_wait_done(struct mmc_request *mrq)
371 complete(&mrq->completion);
375 *__mmc_start_data_req() - starts data request
376 * @host: MMC host to start the request
377 * @mrq: data request to start
379 * Sets the done callback to be called when request is completed by the card.
380 * Starts data mmc request execution
382 static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
386 mrq->done = mmc_wait_data_done;
389 err = mmc_start_request(host, mrq);
391 mrq->cmd->error = err;
392 mmc_wait_data_done(mrq);
398 static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
402 init_completion(&mrq->completion);
403 mrq->done = mmc_wait_done;
405 err = mmc_start_request(host, mrq);
407 mrq->cmd->error = err;
408 complete(&mrq->completion);
415 * mmc_wait_for_data_req_done() - wait for request completed
416 * @host: MMC host to prepare the command.
417 * @mrq: MMC request to wait for
419 * Blocks MMC context till host controller will ack end of data request
420 * execution or new request notification arrives from the block layer.
421 * Handles command retries.
423 * Returns enum mmc_blk_status after checking errors.
425 static int mmc_wait_for_data_req_done(struct mmc_host *host,
426 struct mmc_request *mrq,
427 struct mmc_async_req *next_req)
429 struct mmc_command *cmd;
430 struct mmc_context_info *context_info = &host->context_info;
435 wait_event_interruptible(context_info->wait,
436 (context_info->is_done_rcv ||
437 context_info->is_new_req));
438 spin_lock_irqsave(&context_info->lock, flags);
439 context_info->is_waiting_last_req = false;
440 spin_unlock_irqrestore(&context_info->lock, flags);
441 if (context_info->is_done_rcv) {
442 context_info->is_done_rcv = false;
443 context_info->is_new_req = false;
446 if (!cmd->error || !cmd->retries ||
447 mmc_card_removed(host->card)) {
448 err = host->areq->err_check(host->card,
450 break; /* return err */
452 mmc_retune_recheck(host);
453 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
455 cmd->opcode, cmd->error);
458 __mmc_start_request(host, mrq);
459 continue; /* wait for done/new event again */
461 } else if (context_info->is_new_req) {
462 context_info->is_new_req = false;
464 return MMC_BLK_NEW_REQUEST;
467 mmc_retune_release(host);
471 static void mmc_wait_for_req_done(struct mmc_host *host,
472 struct mmc_request *mrq)
474 struct mmc_command *cmd;
477 wait_for_completion(&mrq->completion);
482 * If host has timed out waiting for the sanitize
483 * to complete, card might be still in programming state
484 * so let's try to bring the card out of programming
487 if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
488 if (!mmc_interrupt_hpi(host->card)) {
489 pr_warn("%s: %s: Interrupted sanitize\n",
490 mmc_hostname(host), __func__);
494 pr_err("%s: %s: Failed to interrupt sanitize\n",
495 mmc_hostname(host), __func__);
498 if (!cmd->error || !cmd->retries ||
499 mmc_card_removed(host->card))
502 mmc_retune_recheck(host);
504 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
505 mmc_hostname(host), cmd->opcode, cmd->error);
508 __mmc_start_request(host, mrq);
511 mmc_retune_release(host);
515 * mmc_pre_req - Prepare for a new request
516 * @host: MMC host to prepare command
517 * @mrq: MMC request to prepare for
518 * @is_first_req: true if there is no previous started request
519 * that may run in parellel to this call, otherwise false
521 * mmc_pre_req() is called in prior to mmc_start_req() to let
522 * host prepare for the new request. Preparation of a request may be
523 * performed while another request is running on the host.
525 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
528 if (host->ops->pre_req) {
529 mmc_host_clk_hold(host);
530 host->ops->pre_req(host, mrq, is_first_req);
531 mmc_host_clk_release(host);
536 * mmc_post_req - Post process a completed request
537 * @host: MMC host to post process command
538 * @mrq: MMC request to post process for
539 * @err: Error, if non zero, clean up any resources made in pre_req
541 * Let the host post process a completed request. Post processing of
542 * a request may be performed while another reuqest is running.
544 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
547 if (host->ops->post_req) {
548 mmc_host_clk_hold(host);
549 host->ops->post_req(host, mrq, err);
550 mmc_host_clk_release(host);
555 * mmc_start_req - start a non-blocking request
556 * @host: MMC host to start command
557 * @areq: async request to start
558 * @error: out parameter returns 0 for success, otherwise non zero
560 * Start a new MMC custom command request for a host.
561 * If there is on ongoing async request wait for completion
562 * of that request and start the new one and return.
563 * Does not wait for the new request to complete.
565 * Returns the completed request, NULL in case of none completed.
566 * Wait for the an ongoing request (previoulsy started) to complete and
567 * return the completed request. If there is no ongoing request, NULL
568 * is returned without waiting. NULL is not an error condition.
570 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
571 struct mmc_async_req *areq, int *error)
575 struct mmc_async_req *data = host->areq;
577 /* Prepare a new request */
579 mmc_pre_req(host, areq->mrq, !host->areq);
582 err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq);
583 if (err == MMC_BLK_NEW_REQUEST) {
587 * The previous request was not completed,
593 * Check BKOPS urgency for each R1 response
595 if (host->card && mmc_card_mmc(host->card) &&
596 ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
597 (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
598 (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
600 /* Cancel the prepared request */
602 mmc_post_req(host, areq->mrq, -EINVAL);
604 mmc_start_bkops(host->card, true);
606 /* prepare the request again */
608 mmc_pre_req(host, areq->mrq, !host->areq);
613 start_err = __mmc_start_data_req(host, areq->mrq);
616 mmc_post_req(host, host->areq->mrq, 0);
618 /* Cancel a prepared request if it was not started. */
619 if ((err || start_err) && areq)
620 mmc_post_req(host, areq->mrq, -EINVAL);
631 EXPORT_SYMBOL(mmc_start_req);
634 * mmc_wait_for_req - start a request and wait for completion
635 * @host: MMC host to start command
636 * @mrq: MMC request to start
638 * Start a new MMC custom command request for a host, and wait
639 * for the command to complete. Does not attempt to parse the
642 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
644 __mmc_start_req(host, mrq);
645 mmc_wait_for_req_done(host, mrq);
647 EXPORT_SYMBOL(mmc_wait_for_req);
650 * mmc_interrupt_hpi - Issue for High priority Interrupt
651 * @card: the MMC card associated with the HPI transfer
653 * Issued High Priority Interrupt, and check for card status
654 * until out-of prg-state.
656 int mmc_interrupt_hpi(struct mmc_card *card)
660 unsigned long prg_wait;
664 if (!card->ext_csd.hpi_en) {
665 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
669 mmc_claim_host(card->host);
670 err = mmc_send_status(card, &status);
672 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
676 switch (R1_CURRENT_STATE(status)) {
682 * In idle and transfer states, HPI is not needed and the caller
683 * can issue the next intended command immediately
689 /* In all other states, it's illegal to issue HPI */
690 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
691 mmc_hostname(card->host), R1_CURRENT_STATE(status));
696 err = mmc_send_hpi_cmd(card, &status);
700 prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
702 err = mmc_send_status(card, &status);
704 if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
706 if (time_after(jiffies, prg_wait))
711 mmc_release_host(card->host);
714 EXPORT_SYMBOL(mmc_interrupt_hpi);
717 * mmc_wait_for_cmd - start a command and wait for completion
718 * @host: MMC host to start command
719 * @cmd: MMC command to start
720 * @retries: maximum number of retries
722 * Start a new MMC command for a host, and wait for the command
723 * to complete. Return any error that occurred while the command
724 * was executing. Do not attempt to parse the response.
726 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
728 struct mmc_request mrq = {NULL};
730 WARN_ON(!host->claimed);
732 memset(cmd->resp, 0, sizeof(cmd->resp));
733 cmd->retries = retries;
738 mmc_wait_for_req(host, &mrq);
743 EXPORT_SYMBOL(mmc_wait_for_cmd);
746 * mmc_stop_bkops - stop ongoing BKOPS
747 * @card: MMC card to check BKOPS
749 * Send HPI command to stop ongoing background operations to
750 * allow rapid servicing of foreground operations, e.g. read/
751 * writes. Wait until the card comes out of the programming state
752 * to avoid errors in servicing read/write requests.
754 int mmc_stop_bkops(struct mmc_card *card)
759 err = mmc_interrupt_hpi(card);
762 * If err is EINVAL, we can't issue an HPI.
763 * It should complete the BKOPS.
765 if (!err || (err == -EINVAL)) {
766 mmc_card_clr_doing_bkops(card);
767 mmc_retune_release(card->host);
773 EXPORT_SYMBOL(mmc_stop_bkops);
775 int mmc_read_bkops_status(struct mmc_card *card)
780 mmc_claim_host(card->host);
781 err = mmc_get_ext_csd(card, &ext_csd);
782 mmc_release_host(card->host);
786 card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
787 card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
791 EXPORT_SYMBOL(mmc_read_bkops_status);
794 * mmc_set_data_timeout - set the timeout for a data command
795 * @data: data phase for command
796 * @card: the MMC card associated with the data transfer
798 * Computes the data timeout parameters according to the
799 * correct algorithm given the card type.
801 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
806 * SDIO cards only define an upper 1 s limit on access.
808 if (mmc_card_sdio(card)) {
809 data->timeout_ns = 1000000000;
810 data->timeout_clks = 0;
815 * SD cards use a 100 multiplier rather than 10
817 mult = mmc_card_sd(card) ? 100 : 10;
820 * Scale up the multiplier (and therefore the timeout) by
821 * the r2w factor for writes.
823 if (data->flags & MMC_DATA_WRITE)
824 mult <<= card->csd.r2w_factor;
826 data->timeout_ns = card->csd.tacc_ns * mult;
827 data->timeout_clks = card->csd.tacc_clks * mult;
830 * SD cards also have an upper limit on the timeout.
832 if (mmc_card_sd(card)) {
833 unsigned int timeout_us, limit_us;
835 timeout_us = data->timeout_ns / 1000;
836 if (mmc_host_clk_rate(card->host))
837 timeout_us += data->timeout_clks * 1000 /
838 (mmc_host_clk_rate(card->host) / 1000);
840 if (data->flags & MMC_DATA_WRITE)
842 * The MMC spec "It is strongly recommended
843 * for hosts to implement more than 500ms
844 * timeout value even if the card indicates
845 * the 250ms maximum busy length." Even the
846 * previous value of 300ms is known to be
847 * insufficient for some cards.
854 * SDHC cards always use these fixed values.
856 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
857 data->timeout_ns = limit_us * 1000;
858 data->timeout_clks = 0;
861 /* assign limit value if invalid */
863 data->timeout_ns = limit_us * 1000;
867 * Some cards require longer data read timeout than indicated in CSD.
868 * Address this by setting the read timeout to a "reasonably high"
869 * value. For the cards tested, 300ms has proven enough. If necessary,
870 * this value can be increased if other problematic cards require this.
872 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
873 data->timeout_ns = 300000000;
874 data->timeout_clks = 0;
878 * Some cards need very high timeouts if driven in SPI mode.
879 * The worst observed timeout was 900ms after writing a
880 * continuous stream of data until the internal logic
883 if (mmc_host_is_spi(card->host)) {
884 if (data->flags & MMC_DATA_WRITE) {
885 if (data->timeout_ns < 1000000000)
886 data->timeout_ns = 1000000000; /* 1s */
888 if (data->timeout_ns < 100000000)
889 data->timeout_ns = 100000000; /* 100ms */
893 EXPORT_SYMBOL(mmc_set_data_timeout);
896 * mmc_align_data_size - pads a transfer size to a more optimal value
897 * @card: the MMC card associated with the data transfer
898 * @sz: original transfer size
900 * Pads the original data size with a number of extra bytes in
901 * order to avoid controller bugs and/or performance hits
902 * (e.g. some controllers revert to PIO for certain sizes).
904 * Returns the improved size, which might be unmodified.
906 * Note that this function is only relevant when issuing a
907 * single scatter gather entry.
909 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
912 * FIXME: We don't have a system for the controller to tell
913 * the core about its problems yet, so for now we just 32-bit
916 sz = ((sz + 3) / 4) * 4;
920 EXPORT_SYMBOL(mmc_align_data_size);
923 * __mmc_claim_host - exclusively claim a host
924 * @host: mmc host to claim
925 * @abort: whether or not the operation should be aborted
927 * Claim a host for a set of operations. If @abort is non null and
928 * dereference a non-zero value then this will return prematurely with
929 * that non-zero value without acquiring the lock. Returns zero
930 * with the lock held otherwise.
932 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
934 DECLARE_WAITQUEUE(wait, current);
941 add_wait_queue(&host->wq, &wait);
942 spin_lock_irqsave(&host->lock, flags);
944 set_current_state(TASK_UNINTERRUPTIBLE);
945 stop = abort ? atomic_read(abort) : 0;
946 if (stop || !host->claimed || host->claimer == current)
948 spin_unlock_irqrestore(&host->lock, flags);
950 spin_lock_irqsave(&host->lock, flags);
952 set_current_state(TASK_RUNNING);
955 host->claimer = current;
956 host->claim_cnt += 1;
957 if (host->claim_cnt == 1)
961 spin_unlock_irqrestore(&host->lock, flags);
962 remove_wait_queue(&host->wq, &wait);
965 pm_runtime_get_sync(mmc_dev(host));
969 EXPORT_SYMBOL(__mmc_claim_host);
972 * mmc_release_host - release a host
973 * @host: mmc host to release
975 * Release a MMC host, allowing others to claim the host
976 * for their operations.
978 void mmc_release_host(struct mmc_host *host)
982 WARN_ON(!host->claimed);
984 spin_lock_irqsave(&host->lock, flags);
985 if (--host->claim_cnt) {
986 /* Release for nested claim */
987 spin_unlock_irqrestore(&host->lock, flags);
990 host->claimer = NULL;
991 spin_unlock_irqrestore(&host->lock, flags);
993 pm_runtime_mark_last_busy(mmc_dev(host));
994 pm_runtime_put_autosuspend(mmc_dev(host));
997 EXPORT_SYMBOL(mmc_release_host);
1000 * This is a helper function, which fetches a runtime pm reference for the
1001 * card device and also claims the host.
1003 void mmc_get_card(struct mmc_card *card)
1005 pm_runtime_get_sync(&card->dev);
1006 mmc_claim_host(card->host);
1008 EXPORT_SYMBOL(mmc_get_card);
1011 * This is a helper function, which releases the host and drops the runtime
1012 * pm reference for the card device.
1014 void mmc_put_card(struct mmc_card *card)
1016 mmc_release_host(card->host);
1017 pm_runtime_mark_last_busy(&card->dev);
1018 pm_runtime_put_autosuspend(&card->dev);
1020 EXPORT_SYMBOL(mmc_put_card);
1023 * Internal function that does the actual ios call to the host driver,
1024 * optionally printing some debug output.
1026 static inline void mmc_set_ios(struct mmc_host *host)
1028 struct mmc_ios *ios = &host->ios;
1030 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1031 "width %u timing %u\n",
1032 mmc_hostname(host), ios->clock, ios->bus_mode,
1033 ios->power_mode, ios->chip_select, ios->vdd,
1034 ios->bus_width, ios->timing);
1037 mmc_set_ungated(host);
1038 host->ops->set_ios(host, ios);
1042 * Control chip select pin on a host.
1044 void mmc_set_chip_select(struct mmc_host *host, int mode)
1046 mmc_host_clk_hold(host);
1047 host->ios.chip_select = mode;
1049 mmc_host_clk_release(host);
1053 * Sets the host clock to the highest possible frequency that
1056 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
1058 WARN_ON(hz && hz < host->f_min);
1060 if (hz > host->f_max)
1063 host->ios.clock = hz;
1067 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1069 mmc_host_clk_hold(host);
1070 __mmc_set_clock(host, hz);
1071 mmc_host_clk_release(host);
1074 #ifdef CONFIG_MMC_CLKGATE
1076 * This gates the clock by setting it to 0 Hz.
1078 void mmc_gate_clock(struct mmc_host *host)
1080 unsigned long flags;
1082 spin_lock_irqsave(&host->clk_lock, flags);
1083 host->clk_old = host->ios.clock;
1084 host->ios.clock = 0;
1085 host->clk_gated = true;
1086 spin_unlock_irqrestore(&host->clk_lock, flags);
1091 * This restores the clock from gating by using the cached
1094 void mmc_ungate_clock(struct mmc_host *host)
1097 * We should previously have gated the clock, so the clock shall
1098 * be 0 here! The clock may however be 0 during initialization,
1099 * when some request operations are performed before setting
1100 * the frequency. When ungate is requested in that situation
1101 * we just ignore the call.
1103 if (host->clk_old) {
1104 BUG_ON(host->ios.clock);
1105 /* This call will also set host->clk_gated to false */
1106 __mmc_set_clock(host, host->clk_old);
1110 void mmc_set_ungated(struct mmc_host *host)
1112 unsigned long flags;
1115 * We've been given a new frequency while the clock is gated,
1116 * so make sure we regard this as ungating it.
1118 spin_lock_irqsave(&host->clk_lock, flags);
1119 host->clk_gated = false;
1120 spin_unlock_irqrestore(&host->clk_lock, flags);
1124 void mmc_set_ungated(struct mmc_host *host)
1129 int mmc_execute_tuning(struct mmc_card *card)
1131 struct mmc_host *host = card->host;
1135 if (!host->ops->execute_tuning)
1138 if (mmc_card_mmc(card))
1139 opcode = MMC_SEND_TUNING_BLOCK_HS200;
1141 opcode = MMC_SEND_TUNING_BLOCK;
1143 mmc_host_clk_hold(host);
1144 err = host->ops->execute_tuning(host, opcode);
1145 mmc_host_clk_release(host);
1148 pr_err("%s: tuning execution failed\n", mmc_hostname(host));
1150 mmc_retune_enable(host);
1156 * Change the bus mode (open drain/push-pull) of a host.
1158 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1160 mmc_host_clk_hold(host);
1161 host->ios.bus_mode = mode;
1163 mmc_host_clk_release(host);
1167 * Change data bus width of a host.
1169 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1171 mmc_host_clk_hold(host);
1172 host->ios.bus_width = width;
1174 mmc_host_clk_release(host);
1178 * Set initial state after a power cycle or a hw_reset.
1180 void mmc_set_initial_state(struct mmc_host *host)
1182 mmc_retune_disable(host);
1184 if (mmc_host_is_spi(host))
1185 host->ios.chip_select = MMC_CS_HIGH;
1187 host->ios.chip_select = MMC_CS_DONTCARE;
1188 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1189 host->ios.bus_width = MMC_BUS_WIDTH_1;
1190 host->ios.timing = MMC_TIMING_LEGACY;
1191 host->ios.drv_type = 0;
1197 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1198 * @vdd: voltage (mV)
1199 * @low_bits: prefer low bits in boundary cases
1201 * This function returns the OCR bit number according to the provided @vdd
1202 * value. If conversion is not possible a negative errno value returned.
1204 * Depending on the @low_bits flag the function prefers low or high OCR bits
1205 * on boundary voltages. For example,
1206 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1207 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1209 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1211 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1213 const int max_bit = ilog2(MMC_VDD_35_36);
1216 if (vdd < 1650 || vdd > 3600)
1219 if (vdd >= 1650 && vdd <= 1950)
1220 return ilog2(MMC_VDD_165_195);
1225 /* Base 2000 mV, step 100 mV, bit's base 8. */
1226 bit = (vdd - 2000) / 100 + 8;
1233 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1234 * @vdd_min: minimum voltage value (mV)
1235 * @vdd_max: maximum voltage value (mV)
1237 * This function returns the OCR mask bits according to the provided @vdd_min
1238 * and @vdd_max values. If conversion is not possible the function returns 0.
1240 * Notes wrt boundary cases:
1241 * This function sets the OCR bits for all boundary voltages, for example
1242 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1243 * MMC_VDD_34_35 mask.
1245 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1249 if (vdd_max < vdd_min)
1252 /* Prefer high bits for the boundary vdd_max values. */
1253 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1257 /* Prefer low bits for the boundary vdd_min values. */
1258 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1262 /* Fill the mask, from max bit to min bit. */
1263 while (vdd_max >= vdd_min)
1264 mask |= 1 << vdd_max--;
1268 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1273 * mmc_of_parse_voltage - return mask of supported voltages
1274 * @np: The device node need to be parsed.
1275 * @mask: mask of voltages available for MMC/SD/SDIO
1277 * 1. Return zero on success.
1278 * 2. Return negative errno: voltage-range is invalid.
1280 int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1282 const u32 *voltage_ranges;
1285 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1286 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
1287 if (!voltage_ranges || !num_ranges) {
1288 pr_info("%s: voltage-ranges unspecified\n", np->full_name);
1292 for (i = 0; i < num_ranges; i++) {
1293 const int j = i * 2;
1296 ocr_mask = mmc_vddrange_to_ocrmask(
1297 be32_to_cpu(voltage_ranges[j]),
1298 be32_to_cpu(voltage_ranges[j + 1]));
1300 pr_err("%s: voltage-range #%d is invalid\n",
1309 EXPORT_SYMBOL(mmc_of_parse_voltage);
1311 #endif /* CONFIG_OF */
1313 static int mmc_of_get_func_num(struct device_node *node)
1318 ret = of_property_read_u32(node, "reg", ®);
1325 struct device_node *mmc_of_find_child_device(struct mmc_host *host,
1328 struct device_node *node;
1330 if (!host->parent || !host->parent->of_node)
1333 for_each_child_of_node(host->parent->of_node, node) {
1334 if (mmc_of_get_func_num(node) == func_num)
1341 #ifdef CONFIG_REGULATOR
1344 * mmc_regulator_get_ocrmask - return mask of supported voltages
1345 * @supply: regulator to use
1347 * This returns either a negative errno, or a mask of voltages that
1348 * can be provided to MMC/SD/SDIO devices using the specified voltage
1349 * regulator. This would normally be called before registering the
1352 int mmc_regulator_get_ocrmask(struct regulator *supply)
1360 count = regulator_count_voltages(supply);
1364 for (i = 0; i < count; i++) {
1365 vdd_uV = regulator_list_voltage(supply, i);
1369 vdd_mV = vdd_uV / 1000;
1370 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1374 vdd_uV = regulator_get_voltage(supply);
1378 vdd_mV = vdd_uV / 1000;
1379 result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1384 EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
1387 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1388 * @mmc: the host to regulate
1389 * @supply: regulator to use
1390 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1392 * Returns zero on success, else negative errno.
1394 * MMC host drivers may use this to enable or disable a regulator using
1395 * a particular supply voltage. This would normally be called from the
1398 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1399 struct regulator *supply,
1400 unsigned short vdd_bit)
1409 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1410 * bits this regulator doesn't quite support ... don't
1411 * be too picky, most cards and regulators are OK with
1412 * a 0.1V range goof (it's a small error percentage).
1414 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1416 min_uV = 1650 * 1000;
1417 max_uV = 1950 * 1000;
1419 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1420 max_uV = min_uV + 100 * 1000;
1423 result = regulator_set_voltage(supply, min_uV, max_uV);
1424 if (result == 0 && !mmc->regulator_enabled) {
1425 result = regulator_enable(supply);
1427 mmc->regulator_enabled = true;
1429 } else if (mmc->regulator_enabled) {
1430 result = regulator_disable(supply);
1432 mmc->regulator_enabled = false;
1436 dev_err(mmc_dev(mmc),
1437 "could not set regulator OCR (%d)\n", result);
1440 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
1442 #endif /* CONFIG_REGULATOR */
1444 int mmc_regulator_get_supply(struct mmc_host *mmc)
1446 struct device *dev = mmc_dev(mmc);
1449 mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
1450 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
1452 if (IS_ERR(mmc->supply.vmmc)) {
1453 if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
1454 return -EPROBE_DEFER;
1455 dev_info(dev, "No vmmc regulator found\n");
1457 ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
1459 mmc->ocr_avail = ret;
1461 dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
1464 if (IS_ERR(mmc->supply.vqmmc)) {
1465 if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
1466 return -EPROBE_DEFER;
1467 dev_info(dev, "No vqmmc regulator found\n");
1472 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1475 * Mask off any voltages we don't support and select
1476 * the lowest voltage
1478 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1483 * Sanity check the voltages that the card claims to
1487 dev_warn(mmc_dev(host),
1488 "card claims to support voltages below defined range\n");
1492 ocr &= host->ocr_avail;
1494 dev_warn(mmc_dev(host), "no support for card's volts\n");
1498 if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
1501 mmc_power_cycle(host, ocr);
1505 if (bit != host->ios.vdd)
1506 dev_warn(mmc_dev(host), "exceeding card's volts\n");
1512 int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1515 int old_signal_voltage = host->ios.signal_voltage;
1517 host->ios.signal_voltage = signal_voltage;
1518 if (host->ops->start_signal_voltage_switch) {
1519 mmc_host_clk_hold(host);
1520 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1521 mmc_host_clk_release(host);
1525 host->ios.signal_voltage = old_signal_voltage;
1531 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
1533 struct mmc_command cmd = {0};
1540 * Send CMD11 only if the request is to switch the card to
1543 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1544 return __mmc_set_signal_voltage(host, signal_voltage);
1547 * If we cannot switch voltages, return failure so the caller
1548 * can continue without UHS mode
1550 if (!host->ops->start_signal_voltage_switch)
1552 if (!host->ops->card_busy)
1553 pr_warn("%s: cannot verify signal voltage switch\n",
1554 mmc_hostname(host));
1556 mmc_host_clk_hold(host);
1558 cmd.opcode = SD_SWITCH_VOLTAGE;
1560 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1562 err = mmc_wait_for_cmd(host, &cmd, 0);
1566 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) {
1571 * The card should drive cmd and dat[0:3] low immediately
1572 * after the response of cmd11, but wait 1 ms to be sure
1575 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1580 * During a signal voltage level switch, the clock must be gated
1581 * for 5 ms according to the SD spec
1583 clock = host->ios.clock;
1584 host->ios.clock = 0;
1587 if (__mmc_set_signal_voltage(host, signal_voltage)) {
1589 * Voltages may not have been switched, but we've already
1590 * sent CMD11, so a power cycle is required anyway
1596 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
1598 host->ios.clock = clock;
1601 /* Wait for at least 1 ms according to spec */
1605 * Failure to switch is indicated by the card holding
1608 if (host->ops->card_busy && host->ops->card_busy(host))
1613 pr_debug("%s: Signal voltage switch failed, "
1614 "power cycling card\n", mmc_hostname(host));
1615 mmc_power_cycle(host, ocr);
1619 mmc_host_clk_release(host);
1625 * Select timing parameters for host.
1627 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1629 mmc_host_clk_hold(host);
1630 host->ios.timing = timing;
1632 mmc_host_clk_release(host);
1636 * Select appropriate driver type for host.
1638 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1640 mmc_host_clk_hold(host);
1641 host->ios.drv_type = drv_type;
1643 mmc_host_clk_release(host);
1646 int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
1647 int card_drv_type, int *drv_type)
1649 struct mmc_host *host = card->host;
1650 int host_drv_type = SD_DRIVER_TYPE_B;
1655 if (!host->ops->select_drive_strength)
1658 /* Use SD definition of driver strength for hosts */
1659 if (host->caps & MMC_CAP_DRIVER_TYPE_A)
1660 host_drv_type |= SD_DRIVER_TYPE_A;
1662 if (host->caps & MMC_CAP_DRIVER_TYPE_C)
1663 host_drv_type |= SD_DRIVER_TYPE_C;
1665 if (host->caps & MMC_CAP_DRIVER_TYPE_D)
1666 host_drv_type |= SD_DRIVER_TYPE_D;
1669 * The drive strength that the hardware can support
1670 * depends on the board design. Pass the appropriate
1671 * information and let the hardware specific code
1672 * return what is possible given the options
1674 mmc_host_clk_hold(host);
1675 drive_strength = host->ops->select_drive_strength(card, max_dtr,
1679 mmc_host_clk_release(host);
1681 return drive_strength;
1685 * Apply power to the MMC stack. This is a two-stage process.
1686 * First, we enable power to the card without the clock running.
1687 * We then wait a bit for the power to stabilise. Finally,
1688 * enable the bus drivers and clock to the card.
1690 * We must _NOT_ enable the clock prior to power stablising.
1692 * If a host does all the power sequencing itself, ignore the
1693 * initial MMC_POWER_UP stage.
1695 void mmc_power_up(struct mmc_host *host, u32 ocr)
1697 if (host->ios.power_mode == MMC_POWER_ON)
1700 mmc_host_clk_hold(host);
1702 mmc_pwrseq_pre_power_on(host);
1704 host->ios.vdd = fls(ocr) - 1;
1705 host->ios.power_mode = MMC_POWER_UP;
1706 /* Set initial state and call mmc_set_ios */
1707 mmc_set_initial_state(host);
1709 /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
1710 if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330) == 0)
1711 dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
1712 else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180) == 0)
1713 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
1714 else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120) == 0)
1715 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
1718 * This delay should be sufficient to allow the power supply
1719 * to reach the minimum voltage.
1723 mmc_pwrseq_post_power_on(host);
1725 host->ios.clock = host->f_init;
1727 host->ios.power_mode = MMC_POWER_ON;
1731 * This delay must be at least 74 clock sizes, or 1 ms, or the
1732 * time required to reach a stable voltage.
1736 mmc_host_clk_release(host);
1739 void mmc_power_off(struct mmc_host *host)
1741 if (host->ios.power_mode == MMC_POWER_OFF)
1744 mmc_host_clk_hold(host);
1746 mmc_pwrseq_power_off(host);
1748 host->ios.clock = 0;
1751 host->ios.power_mode = MMC_POWER_OFF;
1752 /* Set initial state and call mmc_set_ios */
1753 mmc_set_initial_state(host);
1756 * Some configurations, such as the 802.11 SDIO card in the OLPC
1757 * XO-1.5, require a short delay after poweroff before the card
1758 * can be successfully turned on again.
1762 mmc_host_clk_release(host);
1765 void mmc_power_cycle(struct mmc_host *host, u32 ocr)
1767 mmc_power_off(host);
1768 /* Wait at least 1 ms according to SD spec */
1770 mmc_power_up(host, ocr);
1774 * Cleanup when the last reference to the bus operator is dropped.
1776 static void __mmc_release_bus(struct mmc_host *host)
1779 BUG_ON(host->bus_refs);
1780 BUG_ON(!host->bus_dead);
1782 host->bus_ops = NULL;
1786 * Increase reference count of bus operator
1788 static inline void mmc_bus_get(struct mmc_host *host)
1790 unsigned long flags;
1792 spin_lock_irqsave(&host->lock, flags);
1794 spin_unlock_irqrestore(&host->lock, flags);
1798 * Decrease reference count of bus operator and free it if
1799 * it is the last reference.
1801 static inline void mmc_bus_put(struct mmc_host *host)
1803 unsigned long flags;
1805 spin_lock_irqsave(&host->lock, flags);
1807 if ((host->bus_refs == 0) && host->bus_ops)
1808 __mmc_release_bus(host);
1809 spin_unlock_irqrestore(&host->lock, flags);
1813 * Assign a mmc bus handler to a host. Only one bus handler may control a
1814 * host at any given time.
1816 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1818 unsigned long flags;
1823 WARN_ON(!host->claimed);
1825 spin_lock_irqsave(&host->lock, flags);
1827 BUG_ON(host->bus_ops);
1828 BUG_ON(host->bus_refs);
1830 host->bus_ops = ops;
1834 spin_unlock_irqrestore(&host->lock, flags);
1838 * Remove the current bus handler from a host.
1840 void mmc_detach_bus(struct mmc_host *host)
1842 unsigned long flags;
1846 WARN_ON(!host->claimed);
1847 WARN_ON(!host->bus_ops);
1849 spin_lock_irqsave(&host->lock, flags);
1853 spin_unlock_irqrestore(&host->lock, flags);
1858 static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
1861 #ifdef CONFIG_MMC_DEBUG
1862 unsigned long flags;
1863 spin_lock_irqsave(&host->lock, flags);
1864 WARN_ON(host->removed);
1865 spin_unlock_irqrestore(&host->lock, flags);
1869 * If the device is configured as wakeup, we prevent a new sleep for
1870 * 5 s to give provision for user space to consume the event.
1872 if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
1873 device_can_wakeup(mmc_dev(host)))
1874 pm_wakeup_event(mmc_dev(host), 5000);
1876 host->detect_change = 1;
1877 mmc_schedule_delayed_work(&host->detect, delay);
1881 * mmc_detect_change - process change of state on a MMC socket
1882 * @host: host which changed state.
1883 * @delay: optional delay to wait before detection (jiffies)
1885 * MMC drivers should call this when they detect a card has been
1886 * inserted or removed. The MMC layer will confirm that any
1887 * present card is still functional, and initialize any newly
1890 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1892 _mmc_detect_change(host, delay, true);
1894 EXPORT_SYMBOL(mmc_detect_change);
1896 void mmc_init_erase(struct mmc_card *card)
1900 if (is_power_of_2(card->erase_size))
1901 card->erase_shift = ffs(card->erase_size) - 1;
1903 card->erase_shift = 0;
1906 * It is possible to erase an arbitrarily large area of an SD or MMC
1907 * card. That is not desirable because it can take a long time
1908 * (minutes) potentially delaying more important I/O, and also the
1909 * timeout calculations become increasingly hugely over-estimated.
1910 * Consequently, 'pref_erase' is defined as a guide to limit erases
1911 * to that size and alignment.
1913 * For SD cards that define Allocation Unit size, limit erases to one
1914 * Allocation Unit at a time. For MMC cards that define High Capacity
1915 * Erase Size, whether it is switched on or not, limit to that size.
1916 * Otherwise just have a stab at a good value. For modern cards it
1917 * will end up being 4MiB. Note that if the value is too small, it
1918 * can end up taking longer to erase.
1920 if (mmc_card_sd(card) && card->ssr.au) {
1921 card->pref_erase = card->ssr.au;
1922 card->erase_shift = ffs(card->ssr.au) - 1;
1923 } else if (card->ext_csd.hc_erase_size) {
1924 card->pref_erase = card->ext_csd.hc_erase_size;
1925 } else if (card->erase_size) {
1926 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1928 card->pref_erase = 512 * 1024 / 512;
1930 card->pref_erase = 1024 * 1024 / 512;
1932 card->pref_erase = 2 * 1024 * 1024 / 512;
1934 card->pref_erase = 4 * 1024 * 1024 / 512;
1935 if (card->pref_erase < card->erase_size)
1936 card->pref_erase = card->erase_size;
1938 sz = card->pref_erase % card->erase_size;
1940 card->pref_erase += card->erase_size - sz;
1943 card->pref_erase = 0;
1946 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1947 unsigned int arg, unsigned int qty)
1949 unsigned int erase_timeout;
1951 if (arg == MMC_DISCARD_ARG ||
1952 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1953 erase_timeout = card->ext_csd.trim_timeout;
1954 } else if (card->ext_csd.erase_group_def & 1) {
1955 /* High Capacity Erase Group Size uses HC timeouts */
1956 if (arg == MMC_TRIM_ARG)
1957 erase_timeout = card->ext_csd.trim_timeout;
1959 erase_timeout = card->ext_csd.hc_erase_timeout;
1961 /* CSD Erase Group Size uses write timeout */
1962 unsigned int mult = (10 << card->csd.r2w_factor);
1963 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1964 unsigned int timeout_us;
1966 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1967 if (card->csd.tacc_ns < 1000000)
1968 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1970 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1973 * ios.clock is only a target. The real clock rate might be
1974 * less but not that much less, so fudge it by multiplying by 2.
1977 timeout_us += (timeout_clks * 1000) /
1978 (mmc_host_clk_rate(card->host) / 1000);
1980 erase_timeout = timeout_us / 1000;
1983 * Theoretically, the calculation could underflow so round up
1984 * to 1ms in that case.
1990 /* Multiplier for secure operations */
1991 if (arg & MMC_SECURE_ARGS) {
1992 if (arg == MMC_SECURE_ERASE_ARG)
1993 erase_timeout *= card->ext_csd.sec_erase_mult;
1995 erase_timeout *= card->ext_csd.sec_trim_mult;
1998 erase_timeout *= qty;
2001 * Ensure at least a 1 second timeout for SPI as per
2002 * 'mmc_set_data_timeout()'
2004 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
2005 erase_timeout = 1000;
2007 return erase_timeout;
2010 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
2014 unsigned int erase_timeout;
2016 if (card->ssr.erase_timeout) {
2017 /* Erase timeout specified in SD Status Register (SSR) */
2018 erase_timeout = card->ssr.erase_timeout * qty +
2019 card->ssr.erase_offset;
2022 * Erase timeout not specified in SD Status Register (SSR) so
2023 * use 250ms per write block.
2025 erase_timeout = 250 * qty;
2028 /* Must not be less than 1 second */
2029 if (erase_timeout < 1000)
2030 erase_timeout = 1000;
2032 return erase_timeout;
2035 static unsigned int mmc_erase_timeout(struct mmc_card *card,
2039 if (mmc_card_sd(card))
2040 return mmc_sd_erase_timeout(card, arg, qty);
2042 return mmc_mmc_erase_timeout(card, arg, qty);
2045 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
2046 unsigned int to, unsigned int arg)
2048 struct mmc_command cmd = {0};
2049 unsigned int qty = 0;
2050 unsigned long timeout;
2053 mmc_retune_hold(card->host);
2056 * qty is used to calculate the erase timeout which depends on how many
2057 * erase groups (or allocation units in SD terminology) are affected.
2058 * We count erasing part of an erase group as one erase group.
2059 * For SD, the allocation units are always a power of 2. For MMC, the
2060 * erase group size is almost certainly also power of 2, but it does not
2061 * seem to insist on that in the JEDEC standard, so we fall back to
2062 * division in that case. SD may not specify an allocation unit size,
2063 * in which case the timeout is based on the number of write blocks.
2065 * Note that the timeout for secure trim 2 will only be correct if the
2066 * number of erase groups specified is the same as the total of all
2067 * preceding secure trim 1 commands. Since the power may have been
2068 * lost since the secure trim 1 commands occurred, it is generally
2069 * impossible to calculate the secure trim 2 timeout correctly.
2071 if (card->erase_shift)
2072 qty += ((to >> card->erase_shift) -
2073 (from >> card->erase_shift)) + 1;
2074 else if (mmc_card_sd(card))
2075 qty += to - from + 1;
2077 qty += ((to / card->erase_size) -
2078 (from / card->erase_size)) + 1;
2080 if (!mmc_card_blockaddr(card)) {
2085 if (mmc_card_sd(card))
2086 cmd.opcode = SD_ERASE_WR_BLK_START;
2088 cmd.opcode = MMC_ERASE_GROUP_START;
2090 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2091 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2093 pr_err("mmc_erase: group start error %d, "
2094 "status %#x\n", err, cmd.resp[0]);
2099 memset(&cmd, 0, sizeof(struct mmc_command));
2100 if (mmc_card_sd(card))
2101 cmd.opcode = SD_ERASE_WR_BLK_END;
2103 cmd.opcode = MMC_ERASE_GROUP_END;
2105 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2106 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2108 pr_err("mmc_erase: group end error %d, status %#x\n",
2114 memset(&cmd, 0, sizeof(struct mmc_command));
2115 cmd.opcode = MMC_ERASE;
2117 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2118 cmd.busy_timeout = mmc_erase_timeout(card, arg, qty);
2119 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2121 pr_err("mmc_erase: erase error %d, status %#x\n",
2127 if (mmc_host_is_spi(card->host))
2130 timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
2132 memset(&cmd, 0, sizeof(struct mmc_command));
2133 cmd.opcode = MMC_SEND_STATUS;
2134 cmd.arg = card->rca << 16;
2135 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2136 /* Do not retry else we can't see errors */
2137 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2138 if (err || (cmd.resp[0] & 0xFDF92000)) {
2139 pr_err("error %d requesting status %#x\n",
2145 /* Timeout if the device never becomes ready for data and
2146 * never leaves the program state.
2148 if (time_after(jiffies, timeout)) {
2149 pr_err("%s: Card stuck in programming state! %s\n",
2150 mmc_hostname(card->host), __func__);
2155 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
2156 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
2158 mmc_retune_release(card->host);
2163 * mmc_erase - erase sectors.
2164 * @card: card to erase
2165 * @from: first sector to erase
2166 * @nr: number of sectors to erase
2167 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2169 * Caller must claim host before calling this function.
2171 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2174 unsigned int rem, to = from + nr;
2177 if (!(card->host->caps & MMC_CAP_ERASE) ||
2178 !(card->csd.cmdclass & CCC_ERASE))
2181 if (!card->erase_size)
2184 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2187 if ((arg & MMC_SECURE_ARGS) &&
2188 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2191 if ((arg & MMC_TRIM_ARGS) &&
2192 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2195 if (arg == MMC_SECURE_ERASE_ARG) {
2196 if (from % card->erase_size || nr % card->erase_size)
2200 if (arg == MMC_ERASE_ARG) {
2201 rem = from % card->erase_size;
2203 rem = card->erase_size - rem;
2210 rem = nr % card->erase_size;
2223 /* 'from' and 'to' are inclusive */
2227 * Special case where only one erase-group fits in the timeout budget:
2228 * If the region crosses an erase-group boundary on this particular
2229 * case, we will be trimming more than one erase-group which, does not
2230 * fit in the timeout budget of the controller, so we need to split it
2231 * and call mmc_do_erase() twice if necessary. This special case is
2232 * identified by the card->eg_boundary flag.
2234 rem = card->erase_size - (from % card->erase_size);
2235 if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
2236 err = mmc_do_erase(card, from, from + rem - 1, arg);
2238 if ((err) || (to <= from))
2242 return mmc_do_erase(card, from, to, arg);
2244 EXPORT_SYMBOL(mmc_erase);
2246 int mmc_can_erase(struct mmc_card *card)
2248 if ((card->host->caps & MMC_CAP_ERASE) &&
2249 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2253 EXPORT_SYMBOL(mmc_can_erase);
2255 int mmc_can_trim(struct mmc_card *card)
2257 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
2258 (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
2262 EXPORT_SYMBOL(mmc_can_trim);
2264 int mmc_can_discard(struct mmc_card *card)
2267 * As there's no way to detect the discard support bit at v4.5
2268 * use the s/w feature support filed.
2270 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2274 EXPORT_SYMBOL(mmc_can_discard);
2276 int mmc_can_sanitize(struct mmc_card *card)
2278 if (!mmc_can_trim(card) && !mmc_can_erase(card))
2280 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2284 EXPORT_SYMBOL(mmc_can_sanitize);
2286 int mmc_can_secure_erase_trim(struct mmc_card *card)
2288 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
2289 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
2293 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2295 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2298 if (!card->erase_size)
2300 if (from % card->erase_size || nr % card->erase_size)
2304 EXPORT_SYMBOL(mmc_erase_group_aligned);
2306 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2309 struct mmc_host *host = card->host;
2310 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2311 unsigned int last_timeout = 0;
2313 if (card->erase_shift)
2314 max_qty = UINT_MAX >> card->erase_shift;
2315 else if (mmc_card_sd(card))
2318 max_qty = UINT_MAX / card->erase_size;
2320 /* Find the largest qty with an OK timeout */
2323 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2324 timeout = mmc_erase_timeout(card, arg, qty + x);
2325 if (timeout > host->max_busy_timeout)
2327 if (timeout < last_timeout)
2329 last_timeout = timeout;
2339 * When specifying a sector range to trim, chances are we might cross
2340 * an erase-group boundary even if the amount of sectors is less than
2342 * If we can only fit one erase-group in the controller timeout budget,
2343 * we have to care that erase-group boundaries are not crossed by a
2344 * single trim operation. We flag that special case with "eg_boundary".
2345 * In all other cases we can just decrement qty and pretend that we
2346 * always touch (qty + 1) erase-groups as a simple optimization.
2349 card->eg_boundary = 1;
2353 /* Convert qty to sectors */
2354 if (card->erase_shift)
2355 max_discard = qty << card->erase_shift;
2356 else if (mmc_card_sd(card))
2357 max_discard = qty + 1;
2359 max_discard = qty * card->erase_size;
2364 unsigned int mmc_calc_max_discard(struct mmc_card *card)
2366 struct mmc_host *host = card->host;
2367 unsigned int max_discard, max_trim;
2369 if (!host->max_busy_timeout)
2373 * Without erase_group_def set, MMC erase timeout depends on clock
2374 * frequence which can change. In that case, the best choice is
2375 * just the preferred erase size.
2377 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2378 return card->pref_erase;
2380 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2381 if (mmc_can_trim(card)) {
2382 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2383 if (max_trim < max_discard)
2384 max_discard = max_trim;
2385 } else if (max_discard < card->erase_size) {
2388 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2389 mmc_hostname(host), max_discard, host->max_busy_timeout);
2392 EXPORT_SYMBOL(mmc_calc_max_discard);
2394 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2396 struct mmc_command cmd = {0};
2398 if (mmc_card_blockaddr(card) || mmc_card_ddr52(card))
2401 cmd.opcode = MMC_SET_BLOCKLEN;
2403 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2404 return mmc_wait_for_cmd(card->host, &cmd, 5);
2406 EXPORT_SYMBOL(mmc_set_blocklen);
2408 int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2411 struct mmc_command cmd = {0};
2413 cmd.opcode = MMC_SET_BLOCK_COUNT;
2414 cmd.arg = blockcount & 0x0000FFFF;
2417 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2418 return mmc_wait_for_cmd(card->host, &cmd, 5);
2420 EXPORT_SYMBOL(mmc_set_blockcount);
2422 static void mmc_hw_reset_for_init(struct mmc_host *host)
2424 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2426 mmc_host_clk_hold(host);
2427 host->ops->hw_reset(host);
2428 mmc_host_clk_release(host);
2431 int mmc_hw_reset(struct mmc_host *host)
2439 if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
2444 ret = host->bus_ops->reset(host);
2447 if (ret != -EOPNOTSUPP)
2448 pr_warn("%s: tried to reset card\n", mmc_hostname(host));
2452 EXPORT_SYMBOL(mmc_hw_reset);
2454 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2456 host->f_init = freq;
2458 #ifdef CONFIG_MMC_DEBUG
2459 pr_info("%s: %s: trying to init card at %u Hz\n",
2460 mmc_hostname(host), __func__, host->f_init);
2462 mmc_power_up(host, host->ocr_avail);
2465 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2466 * do a hardware reset if possible.
2468 mmc_hw_reset_for_init(host);
2471 * sdio_reset sends CMD52 to reset card. Since we do not know
2472 * if the card is being re-initialized, just send it. CMD52
2473 * should be ignored by SD/eMMC cards.
2478 mmc_send_if_cond(host, host->ocr_avail);
2480 /* Order's important: probe SDIO, then SD, then MMC */
2481 if (!mmc_attach_sdio(host))
2483 if (!mmc_attach_sd(host))
2485 if (!mmc_attach_mmc(host))
2488 mmc_power_off(host);
2492 int _mmc_detect_card_removed(struct mmc_host *host)
2496 if (host->caps & MMC_CAP_NONREMOVABLE)
2499 if (!host->card || mmc_card_removed(host->card))
2502 ret = host->bus_ops->alive(host);
2505 * Card detect status and alive check may be out of sync if card is
2506 * removed slowly, when card detect switch changes while card/slot
2507 * pads are still contacted in hardware (refer to "SD Card Mechanical
2508 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2509 * detect work 200ms later for this case.
2511 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2512 mmc_detect_change(host, msecs_to_jiffies(200));
2513 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2517 mmc_card_set_removed(host->card);
2518 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2524 int mmc_detect_card_removed(struct mmc_host *host)
2526 struct mmc_card *card = host->card;
2529 WARN_ON(!host->claimed);
2534 ret = mmc_card_removed(card);
2536 * The card will be considered unchanged unless we have been asked to
2537 * detect a change or host requires polling to provide card detection.
2539 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2542 host->detect_change = 0;
2544 ret = _mmc_detect_card_removed(host);
2545 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
2547 * Schedule a detect work as soon as possible to let a
2548 * rescan handle the card removal.
2550 cancel_delayed_work(&host->detect);
2551 _mmc_detect_change(host, 0, false);
2557 EXPORT_SYMBOL(mmc_detect_card_removed);
2559 void mmc_rescan(struct work_struct *work)
2561 struct mmc_host *host =
2562 container_of(work, struct mmc_host, detect.work);
2565 if (host->trigger_card_event && host->ops->card_event) {
2566 host->ops->card_event(host);
2567 host->trigger_card_event = false;
2570 if (host->rescan_disable)
2573 /* If there is a non-removable card registered, only scan once */
2574 if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2576 host->rescan_entered = 1;
2581 * if there is a _removable_ card registered, check whether it is
2584 if (host->bus_ops && !host->bus_dead
2585 && !(host->caps & MMC_CAP_NONREMOVABLE))
2586 host->bus_ops->detect(host);
2588 host->detect_change = 0;
2591 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2592 * the card is no longer present.
2597 /* if there still is a card present, stop here */
2598 if (host->bus_ops != NULL) {
2604 * Only we can add a new handler, so it's safe to
2605 * release the lock here.
2609 if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
2610 host->ops->get_cd(host) == 0) {
2611 mmc_claim_host(host);
2612 mmc_power_off(host);
2613 mmc_release_host(host);
2617 mmc_claim_host(host);
2618 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2619 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2621 if (freqs[i] <= host->f_min)
2624 mmc_release_host(host);
2627 if (host->caps & MMC_CAP_NEEDS_POLL)
2628 mmc_schedule_delayed_work(&host->detect, HZ);
2631 void mmc_start_host(struct mmc_host *host)
2633 host->f_init = max(freqs[0], host->f_min);
2634 host->rescan_disable = 0;
2635 host->ios.power_mode = MMC_POWER_UNDEFINED;
2636 if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
2637 mmc_power_off(host);
2639 mmc_power_up(host, host->ocr_avail);
2640 mmc_gpiod_request_cd_irq(host);
2641 _mmc_detect_change(host, 0, false);
2644 void mmc_stop_host(struct mmc_host *host)
2646 #ifdef CONFIG_MMC_DEBUG
2647 unsigned long flags;
2648 spin_lock_irqsave(&host->lock, flags);
2650 spin_unlock_irqrestore(&host->lock, flags);
2652 if (host->slot.cd_irq >= 0)
2653 disable_irq(host->slot.cd_irq);
2655 host->rescan_disable = 1;
2656 cancel_delayed_work_sync(&host->detect);
2657 mmc_flush_scheduled_work();
2659 /* clear pm flags now and let card drivers set them as needed */
2663 if (host->bus_ops && !host->bus_dead) {
2664 /* Calling bus_ops->remove() with a claimed host can deadlock */
2665 host->bus_ops->remove(host);
2666 mmc_claim_host(host);
2667 mmc_detach_bus(host);
2668 mmc_power_off(host);
2669 mmc_release_host(host);
2677 mmc_power_off(host);
2680 int mmc_power_save_host(struct mmc_host *host)
2684 #ifdef CONFIG_MMC_DEBUG
2685 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2690 if (!host->bus_ops || host->bus_dead) {
2695 if (host->bus_ops->power_save)
2696 ret = host->bus_ops->power_save(host);
2700 mmc_power_off(host);
2704 EXPORT_SYMBOL(mmc_power_save_host);
2706 int mmc_power_restore_host(struct mmc_host *host)
2710 #ifdef CONFIG_MMC_DEBUG
2711 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2716 if (!host->bus_ops || host->bus_dead) {
2721 mmc_power_up(host, host->card->ocr);
2722 ret = host->bus_ops->power_restore(host);
2728 EXPORT_SYMBOL(mmc_power_restore_host);
2731 * Flush the cache to the non-volatile storage.
2733 int mmc_flush_cache(struct mmc_card *card)
2737 if (mmc_card_mmc(card) &&
2738 (card->ext_csd.cache_size > 0) &&
2739 (card->ext_csd.cache_ctrl & 1)) {
2740 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2741 EXT_CSD_FLUSH_CACHE, 1, 0);
2743 pr_err("%s: cache flush error %d\n",
2744 mmc_hostname(card->host), err);
2749 EXPORT_SYMBOL(mmc_flush_cache);
2753 /* Do the card removal on suspend if card is assumed removeable
2754 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2757 int mmc_pm_notify(struct notifier_block *notify_block,
2758 unsigned long mode, void *unused)
2760 struct mmc_host *host = container_of(
2761 notify_block, struct mmc_host, pm_notify);
2762 unsigned long flags;
2766 case PM_HIBERNATION_PREPARE:
2767 case PM_SUSPEND_PREPARE:
2768 case PM_RESTORE_PREPARE:
2769 spin_lock_irqsave(&host->lock, flags);
2770 host->rescan_disable = 1;
2771 spin_unlock_irqrestore(&host->lock, flags);
2772 cancel_delayed_work_sync(&host->detect);
2777 /* Validate prerequisites for suspend */
2778 if (host->bus_ops->pre_suspend)
2779 err = host->bus_ops->pre_suspend(host);
2783 /* Calling bus_ops->remove() with a claimed host can deadlock */
2784 host->bus_ops->remove(host);
2785 mmc_claim_host(host);
2786 mmc_detach_bus(host);
2787 mmc_power_off(host);
2788 mmc_release_host(host);
2792 case PM_POST_SUSPEND:
2793 case PM_POST_HIBERNATION:
2794 case PM_POST_RESTORE:
2796 spin_lock_irqsave(&host->lock, flags);
2797 host->rescan_disable = 0;
2798 spin_unlock_irqrestore(&host->lock, flags);
2799 _mmc_detect_change(host, 0, false);
2808 * mmc_init_context_info() - init synchronization context
2811 * Init struct context_info needed to implement asynchronous
2812 * request mechanism, used by mmc core, host driver and mmc requests
2815 void mmc_init_context_info(struct mmc_host *host)
2817 spin_lock_init(&host->context_info.lock);
2818 host->context_info.is_new_req = false;
2819 host->context_info.is_done_rcv = false;
2820 host->context_info.is_waiting_last_req = false;
2821 init_waitqueue_head(&host->context_info.wait);
2824 static int __init mmc_init(void)
2828 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2832 ret = mmc_register_bus();
2834 goto destroy_workqueue;
2836 ret = mmc_register_host_class();
2838 goto unregister_bus;
2840 ret = sdio_register_bus();
2842 goto unregister_host_class;
2846 unregister_host_class:
2847 mmc_unregister_host_class();
2849 mmc_unregister_bus();
2851 destroy_workqueue(workqueue);
2856 static void __exit mmc_exit(void)
2858 sdio_unregister_bus();
2859 mmc_unregister_host_class();
2860 mmc_unregister_bus();
2861 destroy_workqueue(workqueue);
2864 subsys_initcall(mmc_init);
2865 module_exit(mmc_exit);
2867 MODULE_LICENSE("GPL");