mmc: fix a deadlock between system suspend and MMC block IO
[linux-2.6.git] / drivers / mmc / core / core.c
blobbec0bf21c8793ba13d40701ca9f22ee1bb51db2d
1 /*
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/suspend.h>
27 #include <linux/fault-inject.h>
28 #include <linux/random.h>
30 #include <linux/mmc/card.h>
31 #include <linux/mmc/host.h>
32 #include <linux/mmc/mmc.h>
33 #include <linux/mmc/sd.h>
35 #include "core.h"
36 #include "bus.h"
37 #include "host.h"
38 #include "sdio_bus.h"
40 #include "mmc_ops.h"
41 #include "sd_ops.h"
42 #include "sdio_ops.h"
44 static struct workqueue_struct *workqueue;
47 * Enabling software CRCs on the data blocks can be a significant (30%)
48 * performance cost, and for other reasons may not always be desired.
49 * So we allow it it to be disabled.
51 int use_spi_crc = 1;
52 module_param(use_spi_crc, bool, 0);
55 * We normally treat cards as removed during suspend if they are not
56 * known to be on a non-removable bus, to avoid the risk of writing
57 * back data to a different card after resume. Allow this to be
58 * overridden if necessary.
60 #ifdef CONFIG_MMC_UNSAFE_RESUME
61 int mmc_assume_removable;
62 #else
63 int mmc_assume_removable = 1;
64 #endif
65 EXPORT_SYMBOL(mmc_assume_removable);
66 module_param_named(removable, mmc_assume_removable, bool, 0644);
67 MODULE_PARM_DESC(
68 removable,
69 "MMC/SD cards are removable and may be removed during suspend");
72 * Internal function. Schedule delayed work in the MMC work queue.
74 static int mmc_schedule_delayed_work(struct delayed_work *work,
75 unsigned long delay)
77 return queue_delayed_work(workqueue, work, delay);
81 * Internal function. Flush all scheduled work from the MMC work queue.
83 static void mmc_flush_scheduled_work(void)
85 flush_workqueue(workqueue);
88 #ifdef CONFIG_FAIL_MMC_REQUEST
91 * Internal function. Inject random data errors.
92 * If mmc_data is NULL no errors are injected.
94 static void mmc_should_fail_request(struct mmc_host *host,
95 struct mmc_request *mrq)
97 struct mmc_command *cmd = mrq->cmd;
98 struct mmc_data *data = mrq->data;
99 static const int data_errors[] = {
100 -ETIMEDOUT,
101 -EILSEQ,
102 -EIO,
105 if (!data)
106 return;
108 if (cmd->error || data->error ||
109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
110 return;
112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
116 #else /* CONFIG_FAIL_MMC_REQUEST */
118 static inline void mmc_should_fail_request(struct mmc_host *host,
119 struct mmc_request *mrq)
123 #endif /* CONFIG_FAIL_MMC_REQUEST */
126 * mmc_request_done - finish processing an MMC request
127 * @host: MMC host which completed request
128 * @mrq: MMC request which request
130 * MMC drivers should call this function when they have completed
131 * their processing of a request.
133 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
135 struct mmc_command *cmd = mrq->cmd;
136 int err = cmd->error;
138 if (err && cmd->retries && mmc_host_is_spi(host)) {
139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
140 cmd->retries = 0;
143 if (err && cmd->retries && !mmc_card_removed(host->card)) {
145 * Request starter must handle retries - see
146 * mmc_wait_for_req_done().
148 if (mrq->done)
149 mrq->done(mrq);
150 } else {
151 mmc_should_fail_request(host, mrq);
153 led_trigger_event(host->led, LED_OFF);
155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
156 mmc_hostname(host), cmd->opcode, err,
157 cmd->resp[0], cmd->resp[1],
158 cmd->resp[2], cmd->resp[3]);
160 if (mrq->data) {
161 pr_debug("%s: %d bytes transferred: %d\n",
162 mmc_hostname(host),
163 mrq->data->bytes_xfered, mrq->data->error);
166 if (mrq->stop) {
167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), mrq->stop->opcode,
169 mrq->stop->error,
170 mrq->stop->resp[0], mrq->stop->resp[1],
171 mrq->stop->resp[2], mrq->stop->resp[3]);
174 if (mrq->done)
175 mrq->done(mrq);
177 mmc_host_clk_release(host);
181 EXPORT_SYMBOL(mmc_request_done);
183 static void
184 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
186 #ifdef CONFIG_MMC_DEBUG
187 unsigned int i, sz;
188 struct scatterlist *sg;
189 #endif
191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
192 mmc_hostname(host), mrq->cmd->opcode,
193 mrq->cmd->arg, mrq->cmd->flags);
195 if (mrq->data) {
196 pr_debug("%s: blksz %d blocks %d flags %08x "
197 "tsac %d ms nsac %d\n",
198 mmc_hostname(host), mrq->data->blksz,
199 mrq->data->blocks, mrq->data->flags,
200 mrq->data->timeout_ns / 1000000,
201 mrq->data->timeout_clks);
204 if (mrq->stop) {
205 pr_debug("%s: CMD%u arg %08x flags %08x\n",
206 mmc_hostname(host), mrq->stop->opcode,
207 mrq->stop->arg, mrq->stop->flags);
210 WARN_ON(!host->claimed);
212 mrq->cmd->error = 0;
213 mrq->cmd->mrq = mrq;
214 if (mrq->data) {
215 BUG_ON(mrq->data->blksz > host->max_blk_size);
216 BUG_ON(mrq->data->blocks > host->max_blk_count);
217 BUG_ON(mrq->data->blocks * mrq->data->blksz >
218 host->max_req_size);
220 #ifdef CONFIG_MMC_DEBUG
221 sz = 0;
222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
223 sz += sg->length;
224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
225 #endif
227 mrq->cmd->data = mrq->data;
228 mrq->data->error = 0;
229 mrq->data->mrq = mrq;
230 if (mrq->stop) {
231 mrq->data->stop = mrq->stop;
232 mrq->stop->error = 0;
233 mrq->stop->mrq = mrq;
236 mmc_host_clk_hold(host);
237 led_trigger_event(host->led, LED_FULL);
238 host->ops->request(host, mrq);
241 static void mmc_wait_done(struct mmc_request *mrq)
243 complete(&mrq->completion);
246 static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
248 init_completion(&mrq->completion);
249 mrq->done = mmc_wait_done;
250 if (mmc_card_removed(host->card)) {
251 mrq->cmd->error = -ENOMEDIUM;
252 complete(&mrq->completion);
253 return;
255 mmc_start_request(host, mrq);
258 static void mmc_wait_for_req_done(struct mmc_host *host,
259 struct mmc_request *mrq)
261 struct mmc_command *cmd;
263 while (1) {
264 wait_for_completion(&mrq->completion);
266 cmd = mrq->cmd;
267 if (!cmd->error || !cmd->retries ||
268 mmc_card_removed(host->card))
269 break;
271 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
272 mmc_hostname(host), cmd->opcode, cmd->error);
273 cmd->retries--;
274 cmd->error = 0;
275 host->ops->request(host, mrq);
280 * mmc_pre_req - Prepare for a new request
281 * @host: MMC host to prepare command
282 * @mrq: MMC request to prepare for
283 * @is_first_req: true if there is no previous started request
284 * that may run in parellel to this call, otherwise false
286 * mmc_pre_req() is called in prior to mmc_start_req() to let
287 * host prepare for the new request. Preparation of a request may be
288 * performed while another request is running on the host.
290 static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
291 bool is_first_req)
293 if (host->ops->pre_req)
294 host->ops->pre_req(host, mrq, is_first_req);
298 * mmc_post_req - Post process a completed request
299 * @host: MMC host to post process command
300 * @mrq: MMC request to post process for
301 * @err: Error, if non zero, clean up any resources made in pre_req
303 * Let the host post process a completed request. Post processing of
304 * a request may be performed while another reuqest is running.
306 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
307 int err)
309 if (host->ops->post_req)
310 host->ops->post_req(host, mrq, err);
314 * mmc_start_req - start a non-blocking request
315 * @host: MMC host to start command
316 * @areq: async request to start
317 * @error: out parameter returns 0 for success, otherwise non zero
319 * Start a new MMC custom command request for a host.
320 * If there is on ongoing async request wait for completion
321 * of that request and start the new one and return.
322 * Does not wait for the new request to complete.
324 * Returns the completed request, NULL in case of none completed.
325 * Wait for the an ongoing request (previoulsy started) to complete and
326 * return the completed request. If there is no ongoing request, NULL
327 * is returned without waiting. NULL is not an error condition.
329 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
330 struct mmc_async_req *areq, int *error)
332 int err = 0;
333 struct mmc_async_req *data = host->areq;
335 /* Prepare a new request */
336 if (areq)
337 mmc_pre_req(host, areq->mrq, !host->areq);
339 if (host->areq) {
340 mmc_wait_for_req_done(host, host->areq->mrq);
341 err = host->areq->err_check(host->card, host->areq);
342 if (err) {
343 /* post process the completed failed request */
344 mmc_post_req(host, host->areq->mrq, 0);
345 if (areq)
347 * Cancel the new prepared request, because
348 * it can't run until the failed
349 * request has been properly handled.
351 mmc_post_req(host, areq->mrq, -EINVAL);
353 host->areq = NULL;
354 goto out;
358 if (areq)
359 __mmc_start_req(host, areq->mrq);
361 if (host->areq)
362 mmc_post_req(host, host->areq->mrq, 0);
364 host->areq = areq;
365 out:
366 if (error)
367 *error = err;
368 return data;
370 EXPORT_SYMBOL(mmc_start_req);
373 * mmc_wait_for_req - start a request and wait for completion
374 * @host: MMC host to start command
375 * @mrq: MMC request to start
377 * Start a new MMC custom command request for a host, and wait
378 * for the command to complete. Does not attempt to parse the
379 * response.
381 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
383 __mmc_start_req(host, mrq);
384 mmc_wait_for_req_done(host, mrq);
386 EXPORT_SYMBOL(mmc_wait_for_req);
389 * mmc_interrupt_hpi - Issue for High priority Interrupt
390 * @card: the MMC card associated with the HPI transfer
392 * Issued High Priority Interrupt, and check for card status
393 * util out-of prg-state.
395 int mmc_interrupt_hpi(struct mmc_card *card)
397 int err;
398 u32 status;
400 BUG_ON(!card);
402 if (!card->ext_csd.hpi_en) {
403 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
404 return 1;
407 mmc_claim_host(card->host);
408 err = mmc_send_status(card, &status);
409 if (err) {
410 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
411 goto out;
415 * If the card status is in PRG-state, we can send the HPI command.
417 if (R1_CURRENT_STATE(status) == R1_STATE_PRG) {
418 do {
420 * We don't know when the HPI command will finish
421 * processing, so we need to resend HPI until out
422 * of prg-state, and keep checking the card status
423 * with SEND_STATUS. If a timeout error occurs when
424 * sending the HPI command, we are already out of
425 * prg-state.
427 err = mmc_send_hpi_cmd(card, &status);
428 if (err)
429 pr_debug("%s: abort HPI (%d error)\n",
430 mmc_hostname(card->host), err);
432 err = mmc_send_status(card, &status);
433 if (err)
434 break;
435 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
436 } else
437 pr_debug("%s: Left prg-state\n", mmc_hostname(card->host));
439 out:
440 mmc_release_host(card->host);
441 return err;
443 EXPORT_SYMBOL(mmc_interrupt_hpi);
446 * mmc_wait_for_cmd - start a command and wait for completion
447 * @host: MMC host to start command
448 * @cmd: MMC command to start
449 * @retries: maximum number of retries
451 * Start a new MMC command for a host, and wait for the command
452 * to complete. Return any error that occurred while the command
453 * was executing. Do not attempt to parse the response.
455 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
457 struct mmc_request mrq = {NULL};
459 WARN_ON(!host->claimed);
461 memset(cmd->resp, 0, sizeof(cmd->resp));
462 cmd->retries = retries;
464 mrq.cmd = cmd;
465 cmd->data = NULL;
467 mmc_wait_for_req(host, &mrq);
469 return cmd->error;
472 EXPORT_SYMBOL(mmc_wait_for_cmd);
475 * mmc_set_data_timeout - set the timeout for a data command
476 * @data: data phase for command
477 * @card: the MMC card associated with the data transfer
479 * Computes the data timeout parameters according to the
480 * correct algorithm given the card type.
482 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
484 unsigned int mult;
487 * SDIO cards only define an upper 1 s limit on access.
489 if (mmc_card_sdio(card)) {
490 data->timeout_ns = 1000000000;
491 data->timeout_clks = 0;
492 return;
496 * SD cards use a 100 multiplier rather than 10
498 mult = mmc_card_sd(card) ? 100 : 10;
501 * Scale up the multiplier (and therefore the timeout) by
502 * the r2w factor for writes.
504 if (data->flags & MMC_DATA_WRITE)
505 mult <<= card->csd.r2w_factor;
507 data->timeout_ns = card->csd.tacc_ns * mult;
508 data->timeout_clks = card->csd.tacc_clks * mult;
511 * SD cards also have an upper limit on the timeout.
513 if (mmc_card_sd(card)) {
514 unsigned int timeout_us, limit_us;
516 timeout_us = data->timeout_ns / 1000;
517 if (mmc_host_clk_rate(card->host))
518 timeout_us += data->timeout_clks * 1000 /
519 (mmc_host_clk_rate(card->host) / 1000);
521 if (data->flags & MMC_DATA_WRITE)
523 * The limit is really 250 ms, but that is
524 * insufficient for some crappy cards.
526 limit_us = 300000;
527 else
528 limit_us = 100000;
531 * SDHC cards always use these fixed values.
533 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
534 data->timeout_ns = limit_us * 1000;
535 data->timeout_clks = 0;
540 * Some cards require longer data read timeout than indicated in CSD.
541 * Address this by setting the read timeout to a "reasonably high"
542 * value. For the cards tested, 300ms has proven enough. If necessary,
543 * this value can be increased if other problematic cards require this.
545 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
546 data->timeout_ns = 300000000;
547 data->timeout_clks = 0;
551 * Some cards need very high timeouts if driven in SPI mode.
552 * The worst observed timeout was 900ms after writing a
553 * continuous stream of data until the internal logic
554 * overflowed.
556 if (mmc_host_is_spi(card->host)) {
557 if (data->flags & MMC_DATA_WRITE) {
558 if (data->timeout_ns < 1000000000)
559 data->timeout_ns = 1000000000; /* 1s */
560 } else {
561 if (data->timeout_ns < 100000000)
562 data->timeout_ns = 100000000; /* 100ms */
566 EXPORT_SYMBOL(mmc_set_data_timeout);
569 * mmc_align_data_size - pads a transfer size to a more optimal value
570 * @card: the MMC card associated with the data transfer
571 * @sz: original transfer size
573 * Pads the original data size with a number of extra bytes in
574 * order to avoid controller bugs and/or performance hits
575 * (e.g. some controllers revert to PIO for certain sizes).
577 * Returns the improved size, which might be unmodified.
579 * Note that this function is only relevant when issuing a
580 * single scatter gather entry.
582 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
585 * FIXME: We don't have a system for the controller to tell
586 * the core about its problems yet, so for now we just 32-bit
587 * align the size.
589 sz = ((sz + 3) / 4) * 4;
591 return sz;
593 EXPORT_SYMBOL(mmc_align_data_size);
596 * mmc_host_enable - enable a host.
597 * @host: mmc host to enable
599 * Hosts that support power saving can use the 'enable' and 'disable'
600 * methods to exit and enter power saving states. For more information
601 * see comments for struct mmc_host_ops.
603 int mmc_host_enable(struct mmc_host *host)
605 if (!(host->caps & MMC_CAP_DISABLE))
606 return 0;
608 if (host->en_dis_recurs)
609 return 0;
611 if (host->nesting_cnt++)
612 return 0;
614 cancel_delayed_work_sync(&host->disable);
616 if (host->enabled)
617 return 0;
619 if (host->ops->enable) {
620 int err;
622 host->en_dis_recurs = 1;
623 err = host->ops->enable(host);
624 host->en_dis_recurs = 0;
626 if (err) {
627 pr_debug("%s: enable error %d\n",
628 mmc_hostname(host), err);
629 return err;
632 host->enabled = 1;
633 return 0;
635 EXPORT_SYMBOL(mmc_host_enable);
637 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
639 if (host->ops->disable) {
640 int err;
642 host->en_dis_recurs = 1;
643 err = host->ops->disable(host, lazy);
644 host->en_dis_recurs = 0;
646 if (err < 0) {
647 pr_debug("%s: disable error %d\n",
648 mmc_hostname(host), err);
649 return err;
651 if (err > 0) {
652 unsigned long delay = msecs_to_jiffies(err);
654 mmc_schedule_delayed_work(&host->disable, delay);
657 host->enabled = 0;
658 return 0;
662 * mmc_host_disable - disable a host.
663 * @host: mmc host to disable
665 * Hosts that support power saving can use the 'enable' and 'disable'
666 * methods to exit and enter power saving states. For more information
667 * see comments for struct mmc_host_ops.
669 int mmc_host_disable(struct mmc_host *host)
671 int err;
673 if (!(host->caps & MMC_CAP_DISABLE))
674 return 0;
676 if (host->en_dis_recurs)
677 return 0;
679 if (--host->nesting_cnt)
680 return 0;
682 if (!host->enabled)
683 return 0;
685 err = mmc_host_do_disable(host, 0);
686 return err;
688 EXPORT_SYMBOL(mmc_host_disable);
691 * __mmc_claim_host - exclusively claim a host
692 * @host: mmc host to claim
693 * @abort: whether or not the operation should be aborted
695 * Claim a host for a set of operations. If @abort is non null and
696 * dereference a non-zero value then this will return prematurely with
697 * that non-zero value without acquiring the lock. Returns zero
698 * with the lock held otherwise.
700 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
702 DECLARE_WAITQUEUE(wait, current);
703 unsigned long flags;
704 int stop;
706 might_sleep();
708 add_wait_queue(&host->wq, &wait);
709 spin_lock_irqsave(&host->lock, flags);
710 while (1) {
711 set_current_state(TASK_UNINTERRUPTIBLE);
712 stop = abort ? atomic_read(abort) : 0;
713 if (stop || !host->claimed || host->claimer == current)
714 break;
715 spin_unlock_irqrestore(&host->lock, flags);
716 schedule();
717 spin_lock_irqsave(&host->lock, flags);
719 set_current_state(TASK_RUNNING);
720 if (!stop) {
721 host->claimed = 1;
722 host->claimer = current;
723 host->claim_cnt += 1;
724 } else
725 wake_up(&host->wq);
726 spin_unlock_irqrestore(&host->lock, flags);
727 remove_wait_queue(&host->wq, &wait);
728 if (!stop)
729 mmc_host_enable(host);
730 return stop;
733 EXPORT_SYMBOL(__mmc_claim_host);
736 * mmc_try_claim_host - try exclusively to claim a host
737 * @host: mmc host to claim
739 * Returns %1 if the host is claimed, %0 otherwise.
741 int mmc_try_claim_host(struct mmc_host *host)
743 int claimed_host = 0;
744 unsigned long flags;
746 spin_lock_irqsave(&host->lock, flags);
747 if (!host->claimed || host->claimer == current) {
748 host->claimed = 1;
749 host->claimer = current;
750 host->claim_cnt += 1;
751 claimed_host = 1;
753 spin_unlock_irqrestore(&host->lock, flags);
754 return claimed_host;
756 EXPORT_SYMBOL(mmc_try_claim_host);
759 * mmc_do_release_host - release a claimed host
760 * @host: mmc host to release
762 * If you successfully claimed a host, this function will
763 * release it again.
765 void mmc_do_release_host(struct mmc_host *host)
767 unsigned long flags;
769 spin_lock_irqsave(&host->lock, flags);
770 if (--host->claim_cnt) {
771 /* Release for nested claim */
772 spin_unlock_irqrestore(&host->lock, flags);
773 } else {
774 host->claimed = 0;
775 host->claimer = NULL;
776 spin_unlock_irqrestore(&host->lock, flags);
777 wake_up(&host->wq);
780 EXPORT_SYMBOL(mmc_do_release_host);
782 void mmc_host_deeper_disable(struct work_struct *work)
784 struct mmc_host *host =
785 container_of(work, struct mmc_host, disable.work);
787 /* If the host is claimed then we do not want to disable it anymore */
788 if (!mmc_try_claim_host(host))
789 return;
790 mmc_host_do_disable(host, 1);
791 mmc_do_release_host(host);
795 * mmc_host_lazy_disable - lazily disable a host.
796 * @host: mmc host to disable
798 * Hosts that support power saving can use the 'enable' and 'disable'
799 * methods to exit and enter power saving states. For more information
800 * see comments for struct mmc_host_ops.
802 int mmc_host_lazy_disable(struct mmc_host *host)
804 if (!(host->caps & MMC_CAP_DISABLE))
805 return 0;
807 if (host->en_dis_recurs)
808 return 0;
810 if (--host->nesting_cnt)
811 return 0;
813 if (!host->enabled)
814 return 0;
816 if (host->disable_delay) {
817 mmc_schedule_delayed_work(&host->disable,
818 msecs_to_jiffies(host->disable_delay));
819 return 0;
820 } else
821 return mmc_host_do_disable(host, 1);
823 EXPORT_SYMBOL(mmc_host_lazy_disable);
826 * mmc_release_host - release a host
827 * @host: mmc host to release
829 * Release a MMC host, allowing others to claim the host
830 * for their operations.
832 void mmc_release_host(struct mmc_host *host)
834 WARN_ON(!host->claimed);
836 mmc_host_lazy_disable(host);
838 mmc_do_release_host(host);
841 EXPORT_SYMBOL(mmc_release_host);
844 * Internal function that does the actual ios call to the host driver,
845 * optionally printing some debug output.
847 static inline void mmc_set_ios(struct mmc_host *host)
849 struct mmc_ios *ios = &host->ios;
851 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
852 "width %u timing %u\n",
853 mmc_hostname(host), ios->clock, ios->bus_mode,
854 ios->power_mode, ios->chip_select, ios->vdd,
855 ios->bus_width, ios->timing);
857 if (ios->clock > 0)
858 mmc_set_ungated(host);
859 host->ops->set_ios(host, ios);
863 * Control chip select pin on a host.
865 void mmc_set_chip_select(struct mmc_host *host, int mode)
867 mmc_host_clk_hold(host);
868 host->ios.chip_select = mode;
869 mmc_set_ios(host);
870 mmc_host_clk_release(host);
874 * Sets the host clock to the highest possible frequency that
875 * is below "hz".
877 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
879 WARN_ON(hz < host->f_min);
881 if (hz > host->f_max)
882 hz = host->f_max;
884 host->ios.clock = hz;
885 mmc_set_ios(host);
888 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
890 mmc_host_clk_hold(host);
891 __mmc_set_clock(host, hz);
892 mmc_host_clk_release(host);
895 #ifdef CONFIG_MMC_CLKGATE
897 * This gates the clock by setting it to 0 Hz.
899 void mmc_gate_clock(struct mmc_host *host)
901 unsigned long flags;
903 spin_lock_irqsave(&host->clk_lock, flags);
904 host->clk_old = host->ios.clock;
905 host->ios.clock = 0;
906 host->clk_gated = true;
907 spin_unlock_irqrestore(&host->clk_lock, flags);
908 mmc_set_ios(host);
912 * This restores the clock from gating by using the cached
913 * clock value.
915 void mmc_ungate_clock(struct mmc_host *host)
918 * We should previously have gated the clock, so the clock shall
919 * be 0 here! The clock may however be 0 during initialization,
920 * when some request operations are performed before setting
921 * the frequency. When ungate is requested in that situation
922 * we just ignore the call.
924 if (host->clk_old) {
925 BUG_ON(host->ios.clock);
926 /* This call will also set host->clk_gated to false */
927 __mmc_set_clock(host, host->clk_old);
931 void mmc_set_ungated(struct mmc_host *host)
933 unsigned long flags;
936 * We've been given a new frequency while the clock is gated,
937 * so make sure we regard this as ungating it.
939 spin_lock_irqsave(&host->clk_lock, flags);
940 host->clk_gated = false;
941 spin_unlock_irqrestore(&host->clk_lock, flags);
944 #else
945 void mmc_set_ungated(struct mmc_host *host)
948 #endif
951 * Change the bus mode (open drain/push-pull) of a host.
953 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
955 mmc_host_clk_hold(host);
956 host->ios.bus_mode = mode;
957 mmc_set_ios(host);
958 mmc_host_clk_release(host);
962 * Change data bus width of a host.
964 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
966 mmc_host_clk_hold(host);
967 host->ios.bus_width = width;
968 mmc_set_ios(host);
969 mmc_host_clk_release(host);
973 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
974 * @vdd: voltage (mV)
975 * @low_bits: prefer low bits in boundary cases
977 * This function returns the OCR bit number according to the provided @vdd
978 * value. If conversion is not possible a negative errno value returned.
980 * Depending on the @low_bits flag the function prefers low or high OCR bits
981 * on boundary voltages. For example,
982 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
983 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
985 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
987 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
989 const int max_bit = ilog2(MMC_VDD_35_36);
990 int bit;
992 if (vdd < 1650 || vdd > 3600)
993 return -EINVAL;
995 if (vdd >= 1650 && vdd <= 1950)
996 return ilog2(MMC_VDD_165_195);
998 if (low_bits)
999 vdd -= 1;
1001 /* Base 2000 mV, step 100 mV, bit's base 8. */
1002 bit = (vdd - 2000) / 100 + 8;
1003 if (bit > max_bit)
1004 return max_bit;
1005 return bit;
1009 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1010 * @vdd_min: minimum voltage value (mV)
1011 * @vdd_max: maximum voltage value (mV)
1013 * This function returns the OCR mask bits according to the provided @vdd_min
1014 * and @vdd_max values. If conversion is not possible the function returns 0.
1016 * Notes wrt boundary cases:
1017 * This function sets the OCR bits for all boundary voltages, for example
1018 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1019 * MMC_VDD_34_35 mask.
1021 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1023 u32 mask = 0;
1025 if (vdd_max < vdd_min)
1026 return 0;
1028 /* Prefer high bits for the boundary vdd_max values. */
1029 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1030 if (vdd_max < 0)
1031 return 0;
1033 /* Prefer low bits for the boundary vdd_min values. */
1034 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1035 if (vdd_min < 0)
1036 return 0;
1038 /* Fill the mask, from max bit to min bit. */
1039 while (vdd_max >= vdd_min)
1040 mask |= 1 << vdd_max--;
1042 return mask;
1044 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1046 #ifdef CONFIG_REGULATOR
1049 * mmc_regulator_get_ocrmask - return mask of supported voltages
1050 * @supply: regulator to use
1052 * This returns either a negative errno, or a mask of voltages that
1053 * can be provided to MMC/SD/SDIO devices using the specified voltage
1054 * regulator. This would normally be called before registering the
1055 * MMC host adapter.
1057 int mmc_regulator_get_ocrmask(struct regulator *supply)
1059 int result = 0;
1060 int count;
1061 int i;
1063 count = regulator_count_voltages(supply);
1064 if (count < 0)
1065 return count;
1067 for (i = 0; i < count; i++) {
1068 int vdd_uV;
1069 int vdd_mV;
1071 vdd_uV = regulator_list_voltage(supply, i);
1072 if (vdd_uV <= 0)
1073 continue;
1075 vdd_mV = vdd_uV / 1000;
1076 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1079 return result;
1081 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
1084 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1085 * @mmc: the host to regulate
1086 * @supply: regulator to use
1087 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1089 * Returns zero on success, else negative errno.
1091 * MMC host drivers may use this to enable or disable a regulator using
1092 * a particular supply voltage. This would normally be called from the
1093 * set_ios() method.
1095 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1096 struct regulator *supply,
1097 unsigned short vdd_bit)
1099 int result = 0;
1100 int min_uV, max_uV;
1102 if (vdd_bit) {
1103 int tmp;
1104 int voltage;
1106 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1107 * bits this regulator doesn't quite support ... don't
1108 * be too picky, most cards and regulators are OK with
1109 * a 0.1V range goof (it's a small error percentage).
1111 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1112 if (tmp == 0) {
1113 min_uV = 1650 * 1000;
1114 max_uV = 1950 * 1000;
1115 } else {
1116 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1117 max_uV = min_uV + 100 * 1000;
1120 /* avoid needless changes to this voltage; the regulator
1121 * might not allow this operation
1123 voltage = regulator_get_voltage(supply);
1124 if (voltage < 0)
1125 result = voltage;
1126 else if (voltage < min_uV || voltage > max_uV)
1127 result = regulator_set_voltage(supply, min_uV, max_uV);
1128 else
1129 result = 0;
1131 if (result == 0 && !mmc->regulator_enabled) {
1132 result = regulator_enable(supply);
1133 if (!result)
1134 mmc->regulator_enabled = true;
1136 } else if (mmc->regulator_enabled) {
1137 result = regulator_disable(supply);
1138 if (result == 0)
1139 mmc->regulator_enabled = false;
1142 if (result)
1143 dev_err(mmc_dev(mmc),
1144 "could not set regulator OCR (%d)\n", result);
1145 return result;
1147 EXPORT_SYMBOL(mmc_regulator_set_ocr);
1149 #endif /* CONFIG_REGULATOR */
1152 * Mask off any voltages we don't support and select
1153 * the lowest voltage
1155 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1157 int bit;
1159 ocr &= host->ocr_avail;
1161 bit = ffs(ocr);
1162 if (bit) {
1163 bit -= 1;
1165 ocr &= 3 << bit;
1167 mmc_host_clk_hold(host);
1168 host->ios.vdd = bit;
1169 mmc_set_ios(host);
1170 mmc_host_clk_release(host);
1171 } else {
1172 pr_warning("%s: host doesn't support card's voltages\n",
1173 mmc_hostname(host));
1174 ocr = 0;
1177 return ocr;
1180 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
1182 struct mmc_command cmd = {0};
1183 int err = 0;
1185 BUG_ON(!host);
1188 * Send CMD11 only if the request is to switch the card to
1189 * 1.8V signalling.
1191 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1192 cmd.opcode = SD_SWITCH_VOLTAGE;
1193 cmd.arg = 0;
1194 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1196 err = mmc_wait_for_cmd(host, &cmd, 0);
1197 if (err)
1198 return err;
1200 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1201 return -EIO;
1204 host->ios.signal_voltage = signal_voltage;
1206 if (host->ops->start_signal_voltage_switch)
1207 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1209 return err;
1213 * Select timing parameters for host.
1215 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1217 mmc_host_clk_hold(host);
1218 host->ios.timing = timing;
1219 mmc_set_ios(host);
1220 mmc_host_clk_release(host);
1224 * Select appropriate driver type for host.
1226 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1228 mmc_host_clk_hold(host);
1229 host->ios.drv_type = drv_type;
1230 mmc_set_ios(host);
1231 mmc_host_clk_release(host);
1234 static void mmc_poweroff_notify(struct mmc_host *host)
1236 struct mmc_card *card;
1237 unsigned int timeout;
1238 unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION;
1239 int err = 0;
1241 card = host->card;
1244 * Send power notify command only if card
1245 * is mmc and notify state is powered ON
1247 if (card && mmc_card_mmc(card) &&
1248 (card->poweroff_notify_state == MMC_POWERED_ON)) {
1250 if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
1251 notify_type = EXT_CSD_POWER_OFF_SHORT;
1252 timeout = card->ext_csd.generic_cmd6_time;
1253 card->poweroff_notify_state = MMC_POWEROFF_SHORT;
1254 } else {
1255 notify_type = EXT_CSD_POWER_OFF_LONG;
1256 timeout = card->ext_csd.power_off_longtime;
1257 card->poweroff_notify_state = MMC_POWEROFF_LONG;
1260 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1261 EXT_CSD_POWER_OFF_NOTIFICATION,
1262 notify_type, timeout);
1264 if (err && err != -EBADMSG)
1265 pr_err("Device failed to respond within %d poweroff "
1266 "time. Forcefully powering down the device\n",
1267 timeout);
1269 /* Set the card state to no notification after the poweroff */
1270 card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
1275 * Apply power to the MMC stack. This is a two-stage process.
1276 * First, we enable power to the card without the clock running.
1277 * We then wait a bit for the power to stabilise. Finally,
1278 * enable the bus drivers and clock to the card.
1280 * We must _NOT_ enable the clock prior to power stablising.
1282 * If a host does all the power sequencing itself, ignore the
1283 * initial MMC_POWER_UP stage.
1285 static void mmc_power_up(struct mmc_host *host)
1287 int bit;
1289 mmc_host_clk_hold(host);
1291 /* If ocr is set, we use it */
1292 if (host->ocr)
1293 bit = ffs(host->ocr) - 1;
1294 else
1295 bit = fls(host->ocr_avail) - 1;
1297 host->ios.vdd = bit;
1298 if (mmc_host_is_spi(host))
1299 host->ios.chip_select = MMC_CS_HIGH;
1300 else
1301 host->ios.chip_select = MMC_CS_DONTCARE;
1302 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1303 host->ios.power_mode = MMC_POWER_UP;
1304 host->ios.bus_width = MMC_BUS_WIDTH_1;
1305 host->ios.timing = MMC_TIMING_LEGACY;
1306 mmc_set_ios(host);
1309 * This delay should be sufficient to allow the power supply
1310 * to reach the minimum voltage.
1312 mmc_delay(10);
1314 host->ios.clock = host->f_init;
1316 host->ios.power_mode = MMC_POWER_ON;
1317 mmc_set_ios(host);
1320 * This delay must be at least 74 clock sizes, or 1 ms, or the
1321 * time required to reach a stable voltage.
1323 mmc_delay(10);
1325 mmc_host_clk_release(host);
1328 void mmc_power_off(struct mmc_host *host)
1330 mmc_host_clk_hold(host);
1332 host->ios.clock = 0;
1333 host->ios.vdd = 0;
1335 mmc_poweroff_notify(host);
1338 * Reset ocr mask to be the highest possible voltage supported for
1339 * this mmc host. This value will be used at next power up.
1341 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1343 if (!mmc_host_is_spi(host)) {
1344 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1345 host->ios.chip_select = MMC_CS_DONTCARE;
1347 host->ios.power_mode = MMC_POWER_OFF;
1348 host->ios.bus_width = MMC_BUS_WIDTH_1;
1349 host->ios.timing = MMC_TIMING_LEGACY;
1350 mmc_set_ios(host);
1353 * Some configurations, such as the 802.11 SDIO card in the OLPC
1354 * XO-1.5, require a short delay after poweroff before the card
1355 * can be successfully turned on again.
1357 mmc_delay(1);
1359 mmc_host_clk_release(host);
1363 * Cleanup when the last reference to the bus operator is dropped.
1365 static void __mmc_release_bus(struct mmc_host *host)
1367 BUG_ON(!host);
1368 BUG_ON(host->bus_refs);
1369 BUG_ON(!host->bus_dead);
1371 host->bus_ops = NULL;
1375 * Increase reference count of bus operator
1377 static inline void mmc_bus_get(struct mmc_host *host)
1379 unsigned long flags;
1381 spin_lock_irqsave(&host->lock, flags);
1382 host->bus_refs++;
1383 spin_unlock_irqrestore(&host->lock, flags);
1387 * Decrease reference count of bus operator and free it if
1388 * it is the last reference.
1390 static inline void mmc_bus_put(struct mmc_host *host)
1392 unsigned long flags;
1394 spin_lock_irqsave(&host->lock, flags);
1395 host->bus_refs--;
1396 if ((host->bus_refs == 0) && host->bus_ops)
1397 __mmc_release_bus(host);
1398 spin_unlock_irqrestore(&host->lock, flags);
1402 * Assign a mmc bus handler to a host. Only one bus handler may control a
1403 * host at any given time.
1405 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1407 unsigned long flags;
1409 BUG_ON(!host);
1410 BUG_ON(!ops);
1412 WARN_ON(!host->claimed);
1414 spin_lock_irqsave(&host->lock, flags);
1416 BUG_ON(host->bus_ops);
1417 BUG_ON(host->bus_refs);
1419 host->bus_ops = ops;
1420 host->bus_refs = 1;
1421 host->bus_dead = 0;
1423 spin_unlock_irqrestore(&host->lock, flags);
1427 * Remove the current bus handler from a host.
1429 void mmc_detach_bus(struct mmc_host *host)
1431 unsigned long flags;
1433 BUG_ON(!host);
1435 WARN_ON(!host->claimed);
1436 WARN_ON(!host->bus_ops);
1438 spin_lock_irqsave(&host->lock, flags);
1440 host->bus_dead = 1;
1442 spin_unlock_irqrestore(&host->lock, flags);
1444 mmc_bus_put(host);
1448 * mmc_detect_change - process change of state on a MMC socket
1449 * @host: host which changed state.
1450 * @delay: optional delay to wait before detection (jiffies)
1452 * MMC drivers should call this when they detect a card has been
1453 * inserted or removed. The MMC layer will confirm that any
1454 * present card is still functional, and initialize any newly
1455 * inserted.
1457 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1459 #ifdef CONFIG_MMC_DEBUG
1460 unsigned long flags;
1461 spin_lock_irqsave(&host->lock, flags);
1462 WARN_ON(host->removed);
1463 spin_unlock_irqrestore(&host->lock, flags);
1464 #endif
1465 host->detect_change = 1;
1466 mmc_schedule_delayed_work(&host->detect, delay);
1469 EXPORT_SYMBOL(mmc_detect_change);
1471 void mmc_init_erase(struct mmc_card *card)
1473 unsigned int sz;
1475 if (is_power_of_2(card->erase_size))
1476 card->erase_shift = ffs(card->erase_size) - 1;
1477 else
1478 card->erase_shift = 0;
1481 * It is possible to erase an arbitrarily large area of an SD or MMC
1482 * card. That is not desirable because it can take a long time
1483 * (minutes) potentially delaying more important I/O, and also the
1484 * timeout calculations become increasingly hugely over-estimated.
1485 * Consequently, 'pref_erase' is defined as a guide to limit erases
1486 * to that size and alignment.
1488 * For SD cards that define Allocation Unit size, limit erases to one
1489 * Allocation Unit at a time. For MMC cards that define High Capacity
1490 * Erase Size, whether it is switched on or not, limit to that size.
1491 * Otherwise just have a stab at a good value. For modern cards it
1492 * will end up being 4MiB. Note that if the value is too small, it
1493 * can end up taking longer to erase.
1495 if (mmc_card_sd(card) && card->ssr.au) {
1496 card->pref_erase = card->ssr.au;
1497 card->erase_shift = ffs(card->ssr.au) - 1;
1498 } else if (card->ext_csd.hc_erase_size) {
1499 card->pref_erase = card->ext_csd.hc_erase_size;
1500 } else {
1501 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1502 if (sz < 128)
1503 card->pref_erase = 512 * 1024 / 512;
1504 else if (sz < 512)
1505 card->pref_erase = 1024 * 1024 / 512;
1506 else if (sz < 1024)
1507 card->pref_erase = 2 * 1024 * 1024 / 512;
1508 else
1509 card->pref_erase = 4 * 1024 * 1024 / 512;
1510 if (card->pref_erase < card->erase_size)
1511 card->pref_erase = card->erase_size;
1512 else {
1513 sz = card->pref_erase % card->erase_size;
1514 if (sz)
1515 card->pref_erase += card->erase_size - sz;
1520 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1521 unsigned int arg, unsigned int qty)
1523 unsigned int erase_timeout;
1525 if (card->ext_csd.erase_group_def & 1) {
1526 /* High Capacity Erase Group Size uses HC timeouts */
1527 if (arg == MMC_TRIM_ARG)
1528 erase_timeout = card->ext_csd.trim_timeout;
1529 else
1530 erase_timeout = card->ext_csd.hc_erase_timeout;
1531 } else {
1532 /* CSD Erase Group Size uses write timeout */
1533 unsigned int mult = (10 << card->csd.r2w_factor);
1534 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1535 unsigned int timeout_us;
1537 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1538 if (card->csd.tacc_ns < 1000000)
1539 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1540 else
1541 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1544 * ios.clock is only a target. The real clock rate might be
1545 * less but not that much less, so fudge it by multiplying by 2.
1547 timeout_clks <<= 1;
1548 timeout_us += (timeout_clks * 1000) /
1549 (mmc_host_clk_rate(card->host) / 1000);
1551 erase_timeout = timeout_us / 1000;
1554 * Theoretically, the calculation could underflow so round up
1555 * to 1ms in that case.
1557 if (!erase_timeout)
1558 erase_timeout = 1;
1561 /* Multiplier for secure operations */
1562 if (arg & MMC_SECURE_ARGS) {
1563 if (arg == MMC_SECURE_ERASE_ARG)
1564 erase_timeout *= card->ext_csd.sec_erase_mult;
1565 else
1566 erase_timeout *= card->ext_csd.sec_trim_mult;
1569 erase_timeout *= qty;
1572 * Ensure at least a 1 second timeout for SPI as per
1573 * 'mmc_set_data_timeout()'
1575 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1576 erase_timeout = 1000;
1578 return erase_timeout;
1581 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1582 unsigned int arg,
1583 unsigned int qty)
1585 unsigned int erase_timeout;
1587 if (card->ssr.erase_timeout) {
1588 /* Erase timeout specified in SD Status Register (SSR) */
1589 erase_timeout = card->ssr.erase_timeout * qty +
1590 card->ssr.erase_offset;
1591 } else {
1593 * Erase timeout not specified in SD Status Register (SSR) so
1594 * use 250ms per write block.
1596 erase_timeout = 250 * qty;
1599 /* Must not be less than 1 second */
1600 if (erase_timeout < 1000)
1601 erase_timeout = 1000;
1603 return erase_timeout;
1606 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1607 unsigned int arg,
1608 unsigned int qty)
1610 if (mmc_card_sd(card))
1611 return mmc_sd_erase_timeout(card, arg, qty);
1612 else
1613 return mmc_mmc_erase_timeout(card, arg, qty);
1616 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1617 unsigned int to, unsigned int arg)
1619 struct mmc_command cmd = {0};
1620 unsigned int qty = 0;
1621 int err;
1624 * qty is used to calculate the erase timeout which depends on how many
1625 * erase groups (or allocation units in SD terminology) are affected.
1626 * We count erasing part of an erase group as one erase group.
1627 * For SD, the allocation units are always a power of 2. For MMC, the
1628 * erase group size is almost certainly also power of 2, but it does not
1629 * seem to insist on that in the JEDEC standard, so we fall back to
1630 * division in that case. SD may not specify an allocation unit size,
1631 * in which case the timeout is based on the number of write blocks.
1633 * Note that the timeout for secure trim 2 will only be correct if the
1634 * number of erase groups specified is the same as the total of all
1635 * preceding secure trim 1 commands. Since the power may have been
1636 * lost since the secure trim 1 commands occurred, it is generally
1637 * impossible to calculate the secure trim 2 timeout correctly.
1639 if (card->erase_shift)
1640 qty += ((to >> card->erase_shift) -
1641 (from >> card->erase_shift)) + 1;
1642 else if (mmc_card_sd(card))
1643 qty += to - from + 1;
1644 else
1645 qty += ((to / card->erase_size) -
1646 (from / card->erase_size)) + 1;
1648 if (!mmc_card_blockaddr(card)) {
1649 from <<= 9;
1650 to <<= 9;
1653 if (mmc_card_sd(card))
1654 cmd.opcode = SD_ERASE_WR_BLK_START;
1655 else
1656 cmd.opcode = MMC_ERASE_GROUP_START;
1657 cmd.arg = from;
1658 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1659 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1660 if (err) {
1661 pr_err("mmc_erase: group start error %d, "
1662 "status %#x\n", err, cmd.resp[0]);
1663 err = -EIO;
1664 goto out;
1667 memset(&cmd, 0, sizeof(struct mmc_command));
1668 if (mmc_card_sd(card))
1669 cmd.opcode = SD_ERASE_WR_BLK_END;
1670 else
1671 cmd.opcode = MMC_ERASE_GROUP_END;
1672 cmd.arg = to;
1673 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1674 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1675 if (err) {
1676 pr_err("mmc_erase: group end error %d, status %#x\n",
1677 err, cmd.resp[0]);
1678 err = -EIO;
1679 goto out;
1682 memset(&cmd, 0, sizeof(struct mmc_command));
1683 cmd.opcode = MMC_ERASE;
1684 cmd.arg = arg;
1685 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1686 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1687 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1688 if (err) {
1689 pr_err("mmc_erase: erase error %d, status %#x\n",
1690 err, cmd.resp[0]);
1691 err = -EIO;
1692 goto out;
1695 if (mmc_host_is_spi(card->host))
1696 goto out;
1698 do {
1699 memset(&cmd, 0, sizeof(struct mmc_command));
1700 cmd.opcode = MMC_SEND_STATUS;
1701 cmd.arg = card->rca << 16;
1702 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1703 /* Do not retry else we can't see errors */
1704 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1705 if (err || (cmd.resp[0] & 0xFDF92000)) {
1706 pr_err("error %d requesting status %#x\n",
1707 err, cmd.resp[0]);
1708 err = -EIO;
1709 goto out;
1711 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1712 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
1713 out:
1714 return err;
1718 * mmc_erase - erase sectors.
1719 * @card: card to erase
1720 * @from: first sector to erase
1721 * @nr: number of sectors to erase
1722 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1724 * Caller must claim host before calling this function.
1726 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1727 unsigned int arg)
1729 unsigned int rem, to = from + nr;
1731 if (!(card->host->caps & MMC_CAP_ERASE) ||
1732 !(card->csd.cmdclass & CCC_ERASE))
1733 return -EOPNOTSUPP;
1735 if (!card->erase_size)
1736 return -EOPNOTSUPP;
1738 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1739 return -EOPNOTSUPP;
1741 if ((arg & MMC_SECURE_ARGS) &&
1742 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1743 return -EOPNOTSUPP;
1745 if ((arg & MMC_TRIM_ARGS) &&
1746 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1747 return -EOPNOTSUPP;
1749 if (arg == MMC_SECURE_ERASE_ARG) {
1750 if (from % card->erase_size || nr % card->erase_size)
1751 return -EINVAL;
1754 if (arg == MMC_ERASE_ARG) {
1755 rem = from % card->erase_size;
1756 if (rem) {
1757 rem = card->erase_size - rem;
1758 from += rem;
1759 if (nr > rem)
1760 nr -= rem;
1761 else
1762 return 0;
1764 rem = nr % card->erase_size;
1765 if (rem)
1766 nr -= rem;
1769 if (nr == 0)
1770 return 0;
1772 to = from + nr;
1774 if (to <= from)
1775 return -EINVAL;
1777 /* 'from' and 'to' are inclusive */
1778 to -= 1;
1780 return mmc_do_erase(card, from, to, arg);
1782 EXPORT_SYMBOL(mmc_erase);
1784 int mmc_can_erase(struct mmc_card *card)
1786 if ((card->host->caps & MMC_CAP_ERASE) &&
1787 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1788 return 1;
1789 return 0;
1791 EXPORT_SYMBOL(mmc_can_erase);
1793 int mmc_can_trim(struct mmc_card *card)
1795 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1796 return 1;
1797 if (mmc_can_discard(card))
1798 return 1;
1799 return 0;
1801 EXPORT_SYMBOL(mmc_can_trim);
1803 int mmc_can_discard(struct mmc_card *card)
1806 * As there's no way to detect the discard support bit at v4.5
1807 * use the s/w feature support filed.
1809 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
1810 return 1;
1811 return 0;
1813 EXPORT_SYMBOL(mmc_can_discard);
1815 int mmc_can_sanitize(struct mmc_card *card)
1817 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
1818 return 1;
1819 return 0;
1821 EXPORT_SYMBOL(mmc_can_sanitize);
1823 int mmc_can_secure_erase_trim(struct mmc_card *card)
1825 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1826 return 1;
1827 return 0;
1829 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1831 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1832 unsigned int nr)
1834 if (!card->erase_size)
1835 return 0;
1836 if (from % card->erase_size || nr % card->erase_size)
1837 return 0;
1838 return 1;
1840 EXPORT_SYMBOL(mmc_erase_group_aligned);
1842 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1843 unsigned int arg)
1845 struct mmc_host *host = card->host;
1846 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1847 unsigned int last_timeout = 0;
1849 if (card->erase_shift)
1850 max_qty = UINT_MAX >> card->erase_shift;
1851 else if (mmc_card_sd(card))
1852 max_qty = UINT_MAX;
1853 else
1854 max_qty = UINT_MAX / card->erase_size;
1856 /* Find the largest qty with an OK timeout */
1857 do {
1858 y = 0;
1859 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1860 timeout = mmc_erase_timeout(card, arg, qty + x);
1861 if (timeout > host->max_discard_to)
1862 break;
1863 if (timeout < last_timeout)
1864 break;
1865 last_timeout = timeout;
1866 y = x;
1868 qty += y;
1869 } while (y);
1871 if (!qty)
1872 return 0;
1874 if (qty == 1)
1875 return 1;
1877 /* Convert qty to sectors */
1878 if (card->erase_shift)
1879 max_discard = --qty << card->erase_shift;
1880 else if (mmc_card_sd(card))
1881 max_discard = qty;
1882 else
1883 max_discard = --qty * card->erase_size;
1885 return max_discard;
1888 unsigned int mmc_calc_max_discard(struct mmc_card *card)
1890 struct mmc_host *host = card->host;
1891 unsigned int max_discard, max_trim;
1893 if (!host->max_discard_to)
1894 return UINT_MAX;
1897 * Without erase_group_def set, MMC erase timeout depends on clock
1898 * frequence which can change. In that case, the best choice is
1899 * just the preferred erase size.
1901 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1902 return card->pref_erase;
1904 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1905 if (mmc_can_trim(card)) {
1906 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1907 if (max_trim < max_discard)
1908 max_discard = max_trim;
1909 } else if (max_discard < card->erase_size) {
1910 max_discard = 0;
1912 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1913 mmc_hostname(host), max_discard, host->max_discard_to);
1914 return max_discard;
1916 EXPORT_SYMBOL(mmc_calc_max_discard);
1918 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1920 struct mmc_command cmd = {0};
1922 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1923 return 0;
1925 cmd.opcode = MMC_SET_BLOCKLEN;
1926 cmd.arg = blocklen;
1927 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1928 return mmc_wait_for_cmd(card->host, &cmd, 5);
1930 EXPORT_SYMBOL(mmc_set_blocklen);
1932 static void mmc_hw_reset_for_init(struct mmc_host *host)
1934 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1935 return;
1936 mmc_host_clk_hold(host);
1937 host->ops->hw_reset(host);
1938 mmc_host_clk_release(host);
1941 int mmc_can_reset(struct mmc_card *card)
1943 u8 rst_n_function;
1945 if (!mmc_card_mmc(card))
1946 return 0;
1947 rst_n_function = card->ext_csd.rst_n_function;
1948 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1949 return 0;
1950 return 1;
1952 EXPORT_SYMBOL(mmc_can_reset);
1954 static int mmc_do_hw_reset(struct mmc_host *host, int check)
1956 struct mmc_card *card = host->card;
1958 if (!host->bus_ops->power_restore)
1959 return -EOPNOTSUPP;
1961 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1962 return -EOPNOTSUPP;
1964 if (!card)
1965 return -EINVAL;
1967 if (!mmc_can_reset(card))
1968 return -EOPNOTSUPP;
1970 mmc_host_clk_hold(host);
1971 mmc_set_clock(host, host->f_init);
1973 host->ops->hw_reset(host);
1975 /* If the reset has happened, then a status command will fail */
1976 if (check) {
1977 struct mmc_command cmd = {0};
1978 int err;
1980 cmd.opcode = MMC_SEND_STATUS;
1981 if (!mmc_host_is_spi(card->host))
1982 cmd.arg = card->rca << 16;
1983 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1984 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1985 if (!err) {
1986 mmc_host_clk_release(host);
1987 return -ENOSYS;
1991 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
1992 if (mmc_host_is_spi(host)) {
1993 host->ios.chip_select = MMC_CS_HIGH;
1994 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1995 } else {
1996 host->ios.chip_select = MMC_CS_DONTCARE;
1997 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1999 host->ios.bus_width = MMC_BUS_WIDTH_1;
2000 host->ios.timing = MMC_TIMING_LEGACY;
2001 mmc_set_ios(host);
2003 mmc_host_clk_release(host);
2005 return host->bus_ops->power_restore(host);
2008 int mmc_hw_reset(struct mmc_host *host)
2010 return mmc_do_hw_reset(host, 0);
2012 EXPORT_SYMBOL(mmc_hw_reset);
2014 int mmc_hw_reset_check(struct mmc_host *host)
2016 return mmc_do_hw_reset(host, 1);
2018 EXPORT_SYMBOL(mmc_hw_reset_check);
2020 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2022 host->f_init = freq;
2024 #ifdef CONFIG_MMC_DEBUG
2025 pr_info("%s: %s: trying to init card at %u Hz\n",
2026 mmc_hostname(host), __func__, host->f_init);
2027 #endif
2028 mmc_power_up(host);
2031 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2032 * do a hardware reset if possible.
2034 mmc_hw_reset_for_init(host);
2037 * sdio_reset sends CMD52 to reset card. Since we do not know
2038 * if the card is being re-initialized, just send it. CMD52
2039 * should be ignored by SD/eMMC cards.
2041 sdio_reset(host);
2042 mmc_go_idle(host);
2044 mmc_send_if_cond(host, host->ocr_avail);
2046 /* Order's important: probe SDIO, then SD, then MMC */
2047 if (!mmc_attach_sdio(host))
2048 return 0;
2049 if (!mmc_attach_sd(host))
2050 return 0;
2051 if (!mmc_attach_mmc(host))
2052 return 0;
2054 mmc_power_off(host);
2055 return -EIO;
2058 int _mmc_detect_card_removed(struct mmc_host *host)
2060 int ret;
2062 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2063 return 0;
2065 if (!host->card || mmc_card_removed(host->card))
2066 return 1;
2068 ret = host->bus_ops->alive(host);
2069 if (ret) {
2070 mmc_card_set_removed(host->card);
2071 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2074 return ret;
2077 int mmc_detect_card_removed(struct mmc_host *host)
2079 struct mmc_card *card = host->card;
2081 WARN_ON(!host->claimed);
2083 * The card will be considered unchanged unless we have been asked to
2084 * detect a change or host requires polling to provide card detection.
2086 if (card && !host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2087 return mmc_card_removed(card);
2089 host->detect_change = 0;
2091 return _mmc_detect_card_removed(host);
2093 EXPORT_SYMBOL(mmc_detect_card_removed);
2095 void mmc_rescan(struct work_struct *work)
2097 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
2098 struct mmc_host *host =
2099 container_of(work, struct mmc_host, detect.work);
2100 int i;
2102 if (host->rescan_disable)
2103 return;
2105 mmc_bus_get(host);
2108 * if there is a _removable_ card registered, check whether it is
2109 * still present
2111 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
2112 && !(host->caps & MMC_CAP_NONREMOVABLE))
2113 host->bus_ops->detect(host);
2115 host->detect_change = 0;
2118 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2119 * the card is no longer present.
2121 mmc_bus_put(host);
2122 mmc_bus_get(host);
2124 /* if there still is a card present, stop here */
2125 if (host->bus_ops != NULL) {
2126 mmc_bus_put(host);
2127 goto out;
2131 * Only we can add a new handler, so it's safe to
2132 * release the lock here.
2134 mmc_bus_put(host);
2136 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
2137 goto out;
2139 mmc_claim_host(host);
2140 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2141 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2142 break;
2143 if (freqs[i] <= host->f_min)
2144 break;
2146 mmc_release_host(host);
2148 out:
2149 if (host->caps & MMC_CAP_NEEDS_POLL)
2150 mmc_schedule_delayed_work(&host->detect, HZ);
2153 void mmc_start_host(struct mmc_host *host)
2155 mmc_power_off(host);
2156 mmc_detect_change(host, 0);
2159 void mmc_stop_host(struct mmc_host *host)
2161 #ifdef CONFIG_MMC_DEBUG
2162 unsigned long flags;
2163 spin_lock_irqsave(&host->lock, flags);
2164 host->removed = 1;
2165 spin_unlock_irqrestore(&host->lock, flags);
2166 #endif
2168 if (host->caps & MMC_CAP_DISABLE)
2169 cancel_delayed_work(&host->disable);
2170 cancel_delayed_work_sync(&host->detect);
2171 mmc_flush_scheduled_work();
2173 /* clear pm flags now and let card drivers set them as needed */
2174 host->pm_flags = 0;
2176 mmc_bus_get(host);
2177 if (host->bus_ops && !host->bus_dead) {
2178 /* Calling bus_ops->remove() with a claimed host can deadlock */
2179 if (host->bus_ops->remove)
2180 host->bus_ops->remove(host);
2182 mmc_claim_host(host);
2183 mmc_detach_bus(host);
2184 mmc_power_off(host);
2185 mmc_release_host(host);
2186 mmc_bus_put(host);
2187 return;
2189 mmc_bus_put(host);
2191 BUG_ON(host->card);
2193 mmc_power_off(host);
2196 int mmc_power_save_host(struct mmc_host *host)
2198 int ret = 0;
2200 #ifdef CONFIG_MMC_DEBUG
2201 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2202 #endif
2204 mmc_bus_get(host);
2206 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2207 mmc_bus_put(host);
2208 return -EINVAL;
2211 if (host->bus_ops->power_save)
2212 ret = host->bus_ops->power_save(host);
2214 mmc_bus_put(host);
2216 mmc_power_off(host);
2218 return ret;
2220 EXPORT_SYMBOL(mmc_power_save_host);
2222 int mmc_power_restore_host(struct mmc_host *host)
2224 int ret;
2226 #ifdef CONFIG_MMC_DEBUG
2227 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2228 #endif
2230 mmc_bus_get(host);
2232 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2233 mmc_bus_put(host);
2234 return -EINVAL;
2237 mmc_power_up(host);
2238 ret = host->bus_ops->power_restore(host);
2240 mmc_bus_put(host);
2242 return ret;
2244 EXPORT_SYMBOL(mmc_power_restore_host);
2246 int mmc_card_awake(struct mmc_host *host)
2248 int err = -ENOSYS;
2250 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2251 return 0;
2253 mmc_bus_get(host);
2255 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2256 err = host->bus_ops->awake(host);
2258 mmc_bus_put(host);
2260 return err;
2262 EXPORT_SYMBOL(mmc_card_awake);
2264 int mmc_card_sleep(struct mmc_host *host)
2266 int err = -ENOSYS;
2268 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2269 return 0;
2271 mmc_bus_get(host);
2273 if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
2274 err = host->bus_ops->sleep(host);
2276 mmc_bus_put(host);
2278 return err;
2280 EXPORT_SYMBOL(mmc_card_sleep);
2282 int mmc_card_can_sleep(struct mmc_host *host)
2284 struct mmc_card *card = host->card;
2286 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2287 return 1;
2288 return 0;
2290 EXPORT_SYMBOL(mmc_card_can_sleep);
2293 * Flush the cache to the non-volatile storage.
2295 int mmc_flush_cache(struct mmc_card *card)
2297 struct mmc_host *host = card->host;
2298 int err = 0;
2300 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2301 return err;
2303 if (mmc_card_mmc(card) &&
2304 (card->ext_csd.cache_size > 0) &&
2305 (card->ext_csd.cache_ctrl & 1)) {
2306 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2307 EXT_CSD_FLUSH_CACHE, 1, 0);
2308 if (err)
2309 pr_err("%s: cache flush error %d\n",
2310 mmc_hostname(card->host), err);
2313 return err;
2315 EXPORT_SYMBOL(mmc_flush_cache);
2318 * Turn the cache ON/OFF.
2319 * Turning the cache OFF shall trigger flushing of the data
2320 * to the non-volatile storage.
2322 int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2324 struct mmc_card *card = host->card;
2325 unsigned int timeout;
2326 int err = 0;
2328 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2329 mmc_card_is_removable(host))
2330 return err;
2332 if (card && mmc_card_mmc(card) &&
2333 (card->ext_csd.cache_size > 0)) {
2334 enable = !!enable;
2336 if (card->ext_csd.cache_ctrl ^ enable) {
2337 timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
2338 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2339 EXT_CSD_CACHE_CTRL, enable, timeout);
2340 if (err)
2341 pr_err("%s: cache %s error %d\n",
2342 mmc_hostname(card->host),
2343 enable ? "on" : "off",
2344 err);
2345 else
2346 card->ext_csd.cache_ctrl = enable;
2350 return err;
2352 EXPORT_SYMBOL(mmc_cache_ctrl);
2354 #ifdef CONFIG_PM
2357 * mmc_suspend_host - suspend a host
2358 * @host: mmc host
2360 int mmc_suspend_host(struct mmc_host *host)
2362 int err = 0;
2364 if (host->caps & MMC_CAP_DISABLE)
2365 cancel_delayed_work(&host->disable);
2366 cancel_delayed_work(&host->detect);
2367 mmc_flush_scheduled_work();
2368 if (mmc_try_claim_host(host)) {
2369 err = mmc_cache_ctrl(host, 0);
2370 mmc_do_release_host(host);
2371 } else {
2372 err = -EBUSY;
2375 if (err)
2376 goto out;
2378 mmc_bus_get(host);
2379 if (host->bus_ops && !host->bus_dead) {
2382 * A long response time is not acceptable for device drivers
2383 * when doing suspend. Prevent mmc_claim_host in the suspend
2384 * sequence, to potentially wait "forever" by trying to
2385 * pre-claim the host.
2387 if (mmc_try_claim_host(host)) {
2388 if (host->bus_ops->suspend) {
2390 * For eMMC 4.5 device send notify command
2391 * before sleep, because in sleep state eMMC 4.5
2392 * devices respond to only RESET and AWAKE cmd
2394 mmc_poweroff_notify(host);
2395 err = host->bus_ops->suspend(host);
2397 mmc_do_release_host(host);
2399 if (err == -ENOSYS || !host->bus_ops->resume) {
2401 * We simply "remove" the card in this case.
2402 * It will be redetected on resume. (Calling
2403 * bus_ops->remove() with a claimed host can
2404 * deadlock.)
2406 if (host->bus_ops->remove)
2407 host->bus_ops->remove(host);
2408 mmc_claim_host(host);
2409 mmc_detach_bus(host);
2410 mmc_power_off(host);
2411 mmc_release_host(host);
2412 host->pm_flags = 0;
2413 err = 0;
2415 } else {
2416 err = -EBUSY;
2419 mmc_bus_put(host);
2421 if (!err && !mmc_card_keep_power(host))
2422 mmc_power_off(host);
2424 out:
2425 return err;
2428 EXPORT_SYMBOL(mmc_suspend_host);
2431 * mmc_resume_host - resume a previously suspended host
2432 * @host: mmc host
2434 int mmc_resume_host(struct mmc_host *host)
2436 int err = 0;
2438 mmc_bus_get(host);
2439 if (host->bus_ops && !host->bus_dead) {
2440 if (!mmc_card_keep_power(host)) {
2441 mmc_power_up(host);
2442 mmc_select_voltage(host, host->ocr);
2444 * Tell runtime PM core we just powered up the card,
2445 * since it still believes the card is powered off.
2446 * Note that currently runtime PM is only enabled
2447 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2449 if (mmc_card_sdio(host->card) &&
2450 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2451 pm_runtime_disable(&host->card->dev);
2452 pm_runtime_set_active(&host->card->dev);
2453 pm_runtime_enable(&host->card->dev);
2456 BUG_ON(!host->bus_ops->resume);
2457 err = host->bus_ops->resume(host);
2458 if (err) {
2459 pr_warning("%s: error %d during resume "
2460 "(card was removed?)\n",
2461 mmc_hostname(host), err);
2462 err = 0;
2465 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2466 mmc_bus_put(host);
2468 return err;
2470 EXPORT_SYMBOL(mmc_resume_host);
2472 /* Do the card removal on suspend if card is assumed removeable
2473 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2474 to sync the card.
2476 int mmc_pm_notify(struct notifier_block *notify_block,
2477 unsigned long mode, void *unused)
2479 struct mmc_host *host = container_of(
2480 notify_block, struct mmc_host, pm_notify);
2481 unsigned long flags;
2484 switch (mode) {
2485 case PM_HIBERNATION_PREPARE:
2486 case PM_SUSPEND_PREPARE:
2488 spin_lock_irqsave(&host->lock, flags);
2489 host->rescan_disable = 1;
2490 host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2491 spin_unlock_irqrestore(&host->lock, flags);
2492 cancel_delayed_work_sync(&host->detect);
2494 if (!host->bus_ops || host->bus_ops->suspend)
2495 break;
2497 /* Calling bus_ops->remove() with a claimed host can deadlock */
2498 if (host->bus_ops->remove)
2499 host->bus_ops->remove(host);
2501 mmc_claim_host(host);
2502 mmc_detach_bus(host);
2503 mmc_power_off(host);
2504 mmc_release_host(host);
2505 host->pm_flags = 0;
2506 break;
2508 case PM_POST_SUSPEND:
2509 case PM_POST_HIBERNATION:
2510 case PM_POST_RESTORE:
2512 spin_lock_irqsave(&host->lock, flags);
2513 host->rescan_disable = 0;
2514 host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
2515 spin_unlock_irqrestore(&host->lock, flags);
2516 mmc_detect_change(host, 0);
2520 return 0;
2522 #endif
2524 static int __init mmc_init(void)
2526 int ret;
2528 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2529 if (!workqueue)
2530 return -ENOMEM;
2532 ret = mmc_register_bus();
2533 if (ret)
2534 goto destroy_workqueue;
2536 ret = mmc_register_host_class();
2537 if (ret)
2538 goto unregister_bus;
2540 ret = sdio_register_bus();
2541 if (ret)
2542 goto unregister_host_class;
2544 return 0;
2546 unregister_host_class:
2547 mmc_unregister_host_class();
2548 unregister_bus:
2549 mmc_unregister_bus();
2550 destroy_workqueue:
2551 destroy_workqueue(workqueue);
2553 return ret;
2556 static void __exit mmc_exit(void)
2558 sdio_unregister_bus();
2559 mmc_unregister_host_class();
2560 mmc_unregister_bus();
2561 destroy_workqueue(workqueue);
2564 subsys_initcall(mmc_init);
2565 module_exit(mmc_exit);
2567 MODULE_LICENSE("GPL");