Merge tag 'v3.3.7' into 3.3/master
[zen-stable.git] / drivers / mmc / core / core.c
blob64da83f6b3785103e3cfbdce58cbd0e4f12993f1
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 bool 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 bool mmc_assume_removable;
62 #else
63 bool mmc_assume_removable = 1;
64 #endif
65 EXPORT_SYMBOL(mmc_assume_removable);
66 module_param_named(removable, mmc_assume_removable, bool, 0644);
67 MODULE_PARM_DESC(
68 removable,
69 "MMC/SD cards are removable and may be removed during suspend");
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 mmc_host_clk_hold(host);
295 host->ops->pre_req(host, mrq, is_first_req);
296 mmc_host_clk_release(host);
301 * mmc_post_req - Post process a completed request
302 * @host: MMC host to post process command
303 * @mrq: MMC request to post process for
304 * @err: Error, if non zero, clean up any resources made in pre_req
306 * Let the host post process a completed request. Post processing of
307 * a request may be performed while another reuqest is running.
309 static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
310 int err)
312 if (host->ops->post_req) {
313 mmc_host_clk_hold(host);
314 host->ops->post_req(host, mrq, err);
315 mmc_host_clk_release(host);
320 * mmc_start_req - start a non-blocking request
321 * @host: MMC host to start command
322 * @areq: async request to start
323 * @error: out parameter returns 0 for success, otherwise non zero
325 * Start a new MMC custom command request for a host.
326 * If there is on ongoing async request wait for completion
327 * of that request and start the new one and return.
328 * Does not wait for the new request to complete.
330 * Returns the completed request, NULL in case of none completed.
331 * Wait for the an ongoing request (previoulsy started) to complete and
332 * return the completed request. If there is no ongoing request, NULL
333 * is returned without waiting. NULL is not an error condition.
335 struct mmc_async_req *mmc_start_req(struct mmc_host *host,
336 struct mmc_async_req *areq, int *error)
338 int err = 0;
339 struct mmc_async_req *data = host->areq;
341 /* Prepare a new request */
342 if (areq)
343 mmc_pre_req(host, areq->mrq, !host->areq);
345 if (host->areq) {
346 mmc_wait_for_req_done(host, host->areq->mrq);
347 err = host->areq->err_check(host->card, host->areq);
348 if (err) {
349 /* post process the completed failed request */
350 mmc_post_req(host, host->areq->mrq, 0);
351 if (areq)
353 * Cancel the new prepared request, because
354 * it can't run until the failed
355 * request has been properly handled.
357 mmc_post_req(host, areq->mrq, -EINVAL);
359 host->areq = NULL;
360 goto out;
364 if (areq)
365 __mmc_start_req(host, areq->mrq);
367 if (host->areq)
368 mmc_post_req(host, host->areq->mrq, 0);
370 host->areq = areq;
371 out:
372 if (error)
373 *error = err;
374 return data;
376 EXPORT_SYMBOL(mmc_start_req);
379 * mmc_wait_for_req - start a request and wait for completion
380 * @host: MMC host to start command
381 * @mrq: MMC request to start
383 * Start a new MMC custom command request for a host, and wait
384 * for the command to complete. Does not attempt to parse the
385 * response.
387 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
389 __mmc_start_req(host, mrq);
390 mmc_wait_for_req_done(host, mrq);
392 EXPORT_SYMBOL(mmc_wait_for_req);
395 * mmc_interrupt_hpi - Issue for High priority Interrupt
396 * @card: the MMC card associated with the HPI transfer
398 * Issued High Priority Interrupt, and check for card status
399 * util out-of prg-state.
401 int mmc_interrupt_hpi(struct mmc_card *card)
403 int err;
404 u32 status;
406 BUG_ON(!card);
408 if (!card->ext_csd.hpi_en) {
409 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
410 return 1;
413 mmc_claim_host(card->host);
414 err = mmc_send_status(card, &status);
415 if (err) {
416 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
417 goto out;
421 * If the card status is in PRG-state, we can send the HPI command.
423 if (R1_CURRENT_STATE(status) == R1_STATE_PRG) {
424 do {
426 * We don't know when the HPI command will finish
427 * processing, so we need to resend HPI until out
428 * of prg-state, and keep checking the card status
429 * with SEND_STATUS. If a timeout error occurs when
430 * sending the HPI command, we are already out of
431 * prg-state.
433 err = mmc_send_hpi_cmd(card, &status);
434 if (err)
435 pr_debug("%s: abort HPI (%d error)\n",
436 mmc_hostname(card->host), err);
438 err = mmc_send_status(card, &status);
439 if (err)
440 break;
441 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
442 } else
443 pr_debug("%s: Left prg-state\n", mmc_hostname(card->host));
445 out:
446 mmc_release_host(card->host);
447 return err;
449 EXPORT_SYMBOL(mmc_interrupt_hpi);
452 * mmc_wait_for_cmd - start a command and wait for completion
453 * @host: MMC host to start command
454 * @cmd: MMC command to start
455 * @retries: maximum number of retries
457 * Start a new MMC command for a host, and wait for the command
458 * to complete. Return any error that occurred while the command
459 * was executing. Do not attempt to parse the response.
461 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
463 struct mmc_request mrq = {NULL};
465 WARN_ON(!host->claimed);
467 memset(cmd->resp, 0, sizeof(cmd->resp));
468 cmd->retries = retries;
470 mrq.cmd = cmd;
471 cmd->data = NULL;
473 mmc_wait_for_req(host, &mrq);
475 return cmd->error;
478 EXPORT_SYMBOL(mmc_wait_for_cmd);
481 * mmc_set_data_timeout - set the timeout for a data command
482 * @data: data phase for command
483 * @card: the MMC card associated with the data transfer
485 * Computes the data timeout parameters according to the
486 * correct algorithm given the card type.
488 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
490 unsigned int mult;
493 * SDIO cards only define an upper 1 s limit on access.
495 if (mmc_card_sdio(card)) {
496 data->timeout_ns = 1000000000;
497 data->timeout_clks = 0;
498 return;
502 * SD cards use a 100 multiplier rather than 10
504 mult = mmc_card_sd(card) ? 100 : 10;
507 * Scale up the multiplier (and therefore the timeout) by
508 * the r2w factor for writes.
510 if (data->flags & MMC_DATA_WRITE)
511 mult <<= card->csd.r2w_factor;
513 data->timeout_ns = card->csd.tacc_ns * mult;
514 data->timeout_clks = card->csd.tacc_clks * mult;
517 * SD cards also have an upper limit on the timeout.
519 if (mmc_card_sd(card)) {
520 unsigned int timeout_us, limit_us;
522 timeout_us = data->timeout_ns / 1000;
523 if (mmc_host_clk_rate(card->host))
524 timeout_us += data->timeout_clks * 1000 /
525 (mmc_host_clk_rate(card->host) / 1000);
527 if (data->flags & MMC_DATA_WRITE)
529 * The limit is really 250 ms, but that is
530 * insufficient for some crappy cards.
532 limit_us = 300000;
533 else
534 limit_us = 100000;
537 * SDHC cards always use these fixed values.
539 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
540 data->timeout_ns = limit_us * 1000;
541 data->timeout_clks = 0;
546 * Some cards require longer data read timeout than indicated in CSD.
547 * Address this by setting the read timeout to a "reasonably high"
548 * value. For the cards tested, 300ms has proven enough. If necessary,
549 * this value can be increased if other problematic cards require this.
551 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
552 data->timeout_ns = 300000000;
553 data->timeout_clks = 0;
557 * Some cards need very high timeouts if driven in SPI mode.
558 * The worst observed timeout was 900ms after writing a
559 * continuous stream of data until the internal logic
560 * overflowed.
562 if (mmc_host_is_spi(card->host)) {
563 if (data->flags & MMC_DATA_WRITE) {
564 if (data->timeout_ns < 1000000000)
565 data->timeout_ns = 1000000000; /* 1s */
566 } else {
567 if (data->timeout_ns < 100000000)
568 data->timeout_ns = 100000000; /* 100ms */
572 EXPORT_SYMBOL(mmc_set_data_timeout);
575 * mmc_align_data_size - pads a transfer size to a more optimal value
576 * @card: the MMC card associated with the data transfer
577 * @sz: original transfer size
579 * Pads the original data size with a number of extra bytes in
580 * order to avoid controller bugs and/or performance hits
581 * (e.g. some controllers revert to PIO for certain sizes).
583 * Returns the improved size, which might be unmodified.
585 * Note that this function is only relevant when issuing a
586 * single scatter gather entry.
588 unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
591 * FIXME: We don't have a system for the controller to tell
592 * the core about its problems yet, so for now we just 32-bit
593 * align the size.
595 sz = ((sz + 3) / 4) * 4;
597 return sz;
599 EXPORT_SYMBOL(mmc_align_data_size);
602 * mmc_host_enable - enable a host.
603 * @host: mmc host to enable
605 * Hosts that support power saving can use the 'enable' and 'disable'
606 * methods to exit and enter power saving states. For more information
607 * see comments for struct mmc_host_ops.
609 int mmc_host_enable(struct mmc_host *host)
611 if (!(host->caps & MMC_CAP_DISABLE))
612 return 0;
614 if (host->en_dis_recurs)
615 return 0;
617 if (host->nesting_cnt++)
618 return 0;
620 cancel_delayed_work_sync(&host->disable);
622 if (host->enabled)
623 return 0;
625 if (host->ops->enable) {
626 int err;
628 host->en_dis_recurs = 1;
629 mmc_host_clk_hold(host);
630 err = host->ops->enable(host);
631 mmc_host_clk_release(host);
632 host->en_dis_recurs = 0;
634 if (err) {
635 pr_debug("%s: enable error %d\n",
636 mmc_hostname(host), err);
637 return err;
640 host->enabled = 1;
641 return 0;
643 EXPORT_SYMBOL(mmc_host_enable);
645 static int mmc_host_do_disable(struct mmc_host *host, int lazy)
647 if (host->ops->disable) {
648 int err;
650 host->en_dis_recurs = 1;
651 mmc_host_clk_hold(host);
652 err = host->ops->disable(host, lazy);
653 mmc_host_clk_release(host);
654 host->en_dis_recurs = 0;
656 if (err < 0) {
657 pr_debug("%s: disable error %d\n",
658 mmc_hostname(host), err);
659 return err;
661 if (err > 0) {
662 unsigned long delay = msecs_to_jiffies(err);
664 mmc_schedule_delayed_work(&host->disable, delay);
667 host->enabled = 0;
668 return 0;
672 * mmc_host_disable - disable a host.
673 * @host: mmc host to disable
675 * Hosts that support power saving can use the 'enable' and 'disable'
676 * methods to exit and enter power saving states. For more information
677 * see comments for struct mmc_host_ops.
679 int mmc_host_disable(struct mmc_host *host)
681 int err;
683 if (!(host->caps & MMC_CAP_DISABLE))
684 return 0;
686 if (host->en_dis_recurs)
687 return 0;
689 if (--host->nesting_cnt)
690 return 0;
692 if (!host->enabled)
693 return 0;
695 err = mmc_host_do_disable(host, 0);
696 return err;
698 EXPORT_SYMBOL(mmc_host_disable);
701 * __mmc_claim_host - exclusively claim a host
702 * @host: mmc host to claim
703 * @abort: whether or not the operation should be aborted
705 * Claim a host for a set of operations. If @abort is non null and
706 * dereference a non-zero value then this will return prematurely with
707 * that non-zero value without acquiring the lock. Returns zero
708 * with the lock held otherwise.
710 int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
712 DECLARE_WAITQUEUE(wait, current);
713 unsigned long flags;
714 int stop;
716 might_sleep();
718 add_wait_queue(&host->wq, &wait);
719 spin_lock_irqsave(&host->lock, flags);
720 while (1) {
721 set_current_state(TASK_UNINTERRUPTIBLE);
722 stop = abort ? atomic_read(abort) : 0;
723 if (stop || !host->claimed || host->claimer == current)
724 break;
725 spin_unlock_irqrestore(&host->lock, flags);
726 schedule();
727 spin_lock_irqsave(&host->lock, flags);
729 set_current_state(TASK_RUNNING);
730 if (!stop) {
731 host->claimed = 1;
732 host->claimer = current;
733 host->claim_cnt += 1;
734 } else
735 wake_up(&host->wq);
736 spin_unlock_irqrestore(&host->lock, flags);
737 remove_wait_queue(&host->wq, &wait);
738 if (!stop)
739 mmc_host_enable(host);
740 return stop;
743 EXPORT_SYMBOL(__mmc_claim_host);
746 * mmc_try_claim_host - try exclusively to claim a host
747 * @host: mmc host to claim
749 * Returns %1 if the host is claimed, %0 otherwise.
751 int mmc_try_claim_host(struct mmc_host *host)
753 int claimed_host = 0;
754 unsigned long flags;
756 spin_lock_irqsave(&host->lock, flags);
757 if (!host->claimed || host->claimer == current) {
758 host->claimed = 1;
759 host->claimer = current;
760 host->claim_cnt += 1;
761 claimed_host = 1;
763 spin_unlock_irqrestore(&host->lock, flags);
764 return claimed_host;
766 EXPORT_SYMBOL(mmc_try_claim_host);
769 * mmc_do_release_host - release a claimed host
770 * @host: mmc host to release
772 * If you successfully claimed a host, this function will
773 * release it again.
775 void mmc_do_release_host(struct mmc_host *host)
777 unsigned long flags;
779 spin_lock_irqsave(&host->lock, flags);
780 if (--host->claim_cnt) {
781 /* Release for nested claim */
782 spin_unlock_irqrestore(&host->lock, flags);
783 } else {
784 host->claimed = 0;
785 host->claimer = NULL;
786 spin_unlock_irqrestore(&host->lock, flags);
787 wake_up(&host->wq);
790 EXPORT_SYMBOL(mmc_do_release_host);
792 void mmc_host_deeper_disable(struct work_struct *work)
794 struct mmc_host *host =
795 container_of(work, struct mmc_host, disable.work);
797 /* If the host is claimed then we do not want to disable it anymore */
798 if (!mmc_try_claim_host(host))
799 return;
800 mmc_host_do_disable(host, 1);
801 mmc_do_release_host(host);
805 * mmc_host_lazy_disable - lazily disable a host.
806 * @host: mmc host to disable
808 * Hosts that support power saving can use the 'enable' and 'disable'
809 * methods to exit and enter power saving states. For more information
810 * see comments for struct mmc_host_ops.
812 int mmc_host_lazy_disable(struct mmc_host *host)
814 if (!(host->caps & MMC_CAP_DISABLE))
815 return 0;
817 if (host->en_dis_recurs)
818 return 0;
820 if (--host->nesting_cnt)
821 return 0;
823 if (!host->enabled)
824 return 0;
826 if (host->disable_delay) {
827 mmc_schedule_delayed_work(&host->disable,
828 msecs_to_jiffies(host->disable_delay));
829 return 0;
830 } else
831 return mmc_host_do_disable(host, 1);
833 EXPORT_SYMBOL(mmc_host_lazy_disable);
836 * mmc_release_host - release a host
837 * @host: mmc host to release
839 * Release a MMC host, allowing others to claim the host
840 * for their operations.
842 void mmc_release_host(struct mmc_host *host)
844 WARN_ON(!host->claimed);
846 mmc_host_lazy_disable(host);
848 mmc_do_release_host(host);
851 EXPORT_SYMBOL(mmc_release_host);
854 * Internal function that does the actual ios call to the host driver,
855 * optionally printing some debug output.
857 static inline void mmc_set_ios(struct mmc_host *host)
859 struct mmc_ios *ios = &host->ios;
861 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
862 "width %u timing %u\n",
863 mmc_hostname(host), ios->clock, ios->bus_mode,
864 ios->power_mode, ios->chip_select, ios->vdd,
865 ios->bus_width, ios->timing);
867 if (ios->clock > 0)
868 mmc_set_ungated(host);
869 host->ops->set_ios(host, ios);
873 * Control chip select pin on a host.
875 void mmc_set_chip_select(struct mmc_host *host, int mode)
877 mmc_host_clk_hold(host);
878 host->ios.chip_select = mode;
879 mmc_set_ios(host);
880 mmc_host_clk_release(host);
884 * Sets the host clock to the highest possible frequency that
885 * is below "hz".
887 static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
889 WARN_ON(hz < host->f_min);
891 if (hz > host->f_max)
892 hz = host->f_max;
894 host->ios.clock = hz;
895 mmc_set_ios(host);
898 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
900 mmc_host_clk_hold(host);
901 __mmc_set_clock(host, hz);
902 mmc_host_clk_release(host);
905 #ifdef CONFIG_MMC_CLKGATE
907 * This gates the clock by setting it to 0 Hz.
909 void mmc_gate_clock(struct mmc_host *host)
911 unsigned long flags;
913 spin_lock_irqsave(&host->clk_lock, flags);
914 host->clk_old = host->ios.clock;
915 host->ios.clock = 0;
916 host->clk_gated = true;
917 spin_unlock_irqrestore(&host->clk_lock, flags);
918 mmc_set_ios(host);
922 * This restores the clock from gating by using the cached
923 * clock value.
925 void mmc_ungate_clock(struct mmc_host *host)
928 * We should previously have gated the clock, so the clock shall
929 * be 0 here! The clock may however be 0 during initialization,
930 * when some request operations are performed before setting
931 * the frequency. When ungate is requested in that situation
932 * we just ignore the call.
934 if (host->clk_old) {
935 BUG_ON(host->ios.clock);
936 /* This call will also set host->clk_gated to false */
937 __mmc_set_clock(host, host->clk_old);
941 void mmc_set_ungated(struct mmc_host *host)
943 unsigned long flags;
946 * We've been given a new frequency while the clock is gated,
947 * so make sure we regard this as ungating it.
949 spin_lock_irqsave(&host->clk_lock, flags);
950 host->clk_gated = false;
951 spin_unlock_irqrestore(&host->clk_lock, flags);
954 #else
955 void mmc_set_ungated(struct mmc_host *host)
958 #endif
961 * Change the bus mode (open drain/push-pull) of a host.
963 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
965 mmc_host_clk_hold(host);
966 host->ios.bus_mode = mode;
967 mmc_set_ios(host);
968 mmc_host_clk_release(host);
972 * Change data bus width of a host.
974 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
976 mmc_host_clk_hold(host);
977 host->ios.bus_width = width;
978 mmc_set_ios(host);
979 mmc_host_clk_release(host);
983 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
984 * @vdd: voltage (mV)
985 * @low_bits: prefer low bits in boundary cases
987 * This function returns the OCR bit number according to the provided @vdd
988 * value. If conversion is not possible a negative errno value returned.
990 * Depending on the @low_bits flag the function prefers low or high OCR bits
991 * on boundary voltages. For example,
992 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
993 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
995 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
997 static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
999 const int max_bit = ilog2(MMC_VDD_35_36);
1000 int bit;
1002 if (vdd < 1650 || vdd > 3600)
1003 return -EINVAL;
1005 if (vdd >= 1650 && vdd <= 1950)
1006 return ilog2(MMC_VDD_165_195);
1008 if (low_bits)
1009 vdd -= 1;
1011 /* Base 2000 mV, step 100 mV, bit's base 8. */
1012 bit = (vdd - 2000) / 100 + 8;
1013 if (bit > max_bit)
1014 return max_bit;
1015 return bit;
1019 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1020 * @vdd_min: minimum voltage value (mV)
1021 * @vdd_max: maximum voltage value (mV)
1023 * This function returns the OCR mask bits according to the provided @vdd_min
1024 * and @vdd_max values. If conversion is not possible the function returns 0.
1026 * Notes wrt boundary cases:
1027 * This function sets the OCR bits for all boundary voltages, for example
1028 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1029 * MMC_VDD_34_35 mask.
1031 u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1033 u32 mask = 0;
1035 if (vdd_max < vdd_min)
1036 return 0;
1038 /* Prefer high bits for the boundary vdd_max values. */
1039 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1040 if (vdd_max < 0)
1041 return 0;
1043 /* Prefer low bits for the boundary vdd_min values. */
1044 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1045 if (vdd_min < 0)
1046 return 0;
1048 /* Fill the mask, from max bit to min bit. */
1049 while (vdd_max >= vdd_min)
1050 mask |= 1 << vdd_max--;
1052 return mask;
1054 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1056 #ifdef CONFIG_REGULATOR
1059 * mmc_regulator_get_ocrmask - return mask of supported voltages
1060 * @supply: regulator to use
1062 * This returns either a negative errno, or a mask of voltages that
1063 * can be provided to MMC/SD/SDIO devices using the specified voltage
1064 * regulator. This would normally be called before registering the
1065 * MMC host adapter.
1067 int mmc_regulator_get_ocrmask(struct regulator *supply)
1069 int result = 0;
1070 int count;
1071 int i;
1073 count = regulator_count_voltages(supply);
1074 if (count < 0)
1075 return count;
1077 for (i = 0; i < count; i++) {
1078 int vdd_uV;
1079 int vdd_mV;
1081 vdd_uV = regulator_list_voltage(supply, i);
1082 if (vdd_uV <= 0)
1083 continue;
1085 vdd_mV = vdd_uV / 1000;
1086 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1089 return result;
1091 EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
1094 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1095 * @mmc: the host to regulate
1096 * @supply: regulator to use
1097 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1099 * Returns zero on success, else negative errno.
1101 * MMC host drivers may use this to enable or disable a regulator using
1102 * a particular supply voltage. This would normally be called from the
1103 * set_ios() method.
1105 int mmc_regulator_set_ocr(struct mmc_host *mmc,
1106 struct regulator *supply,
1107 unsigned short vdd_bit)
1109 int result = 0;
1110 int min_uV, max_uV;
1112 if (vdd_bit) {
1113 int tmp;
1114 int voltage;
1116 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1117 * bits this regulator doesn't quite support ... don't
1118 * be too picky, most cards and regulators are OK with
1119 * a 0.1V range goof (it's a small error percentage).
1121 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1122 if (tmp == 0) {
1123 min_uV = 1650 * 1000;
1124 max_uV = 1950 * 1000;
1125 } else {
1126 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1127 max_uV = min_uV + 100 * 1000;
1130 /* avoid needless changes to this voltage; the regulator
1131 * might not allow this operation
1133 voltage = regulator_get_voltage(supply);
1135 if (mmc->caps2 & MMC_CAP2_BROKEN_VOLTAGE)
1136 min_uV = max_uV = voltage;
1138 if (voltage < 0)
1139 result = voltage;
1140 else if (voltage < min_uV || voltage > max_uV)
1141 result = regulator_set_voltage(supply, min_uV, max_uV);
1142 else
1143 result = 0;
1145 if (result == 0 && !mmc->regulator_enabled) {
1146 result = regulator_enable(supply);
1147 if (!result)
1148 mmc->regulator_enabled = true;
1150 } else if (mmc->regulator_enabled) {
1151 result = regulator_disable(supply);
1152 if (result == 0)
1153 mmc->regulator_enabled = false;
1156 if (result)
1157 dev_err(mmc_dev(mmc),
1158 "could not set regulator OCR (%d)\n", result);
1159 return result;
1161 EXPORT_SYMBOL(mmc_regulator_set_ocr);
1163 #endif /* CONFIG_REGULATOR */
1166 * Mask off any voltages we don't support and select
1167 * the lowest voltage
1169 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1171 int bit;
1173 ocr &= host->ocr_avail;
1175 bit = ffs(ocr);
1176 if (bit) {
1177 bit -= 1;
1179 ocr &= 3 << bit;
1181 mmc_host_clk_hold(host);
1182 host->ios.vdd = bit;
1183 mmc_set_ios(host);
1184 mmc_host_clk_release(host);
1185 } else {
1186 pr_warning("%s: host doesn't support card's voltages\n",
1187 mmc_hostname(host));
1188 ocr = 0;
1191 return ocr;
1194 int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
1196 struct mmc_command cmd = {0};
1197 int err = 0;
1199 BUG_ON(!host);
1202 * Send CMD11 only if the request is to switch the card to
1203 * 1.8V signalling.
1205 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
1206 cmd.opcode = SD_SWITCH_VOLTAGE;
1207 cmd.arg = 0;
1208 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1210 err = mmc_wait_for_cmd(host, &cmd, 0);
1211 if (err)
1212 return err;
1214 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1215 return -EIO;
1218 host->ios.signal_voltage = signal_voltage;
1220 if (host->ops->start_signal_voltage_switch) {
1221 mmc_host_clk_hold(host);
1222 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1223 mmc_host_clk_release(host);
1226 return err;
1230 * Select timing parameters for host.
1232 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
1234 mmc_host_clk_hold(host);
1235 host->ios.timing = timing;
1236 mmc_set_ios(host);
1237 mmc_host_clk_release(host);
1241 * Select appropriate driver type for host.
1243 void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1245 mmc_host_clk_hold(host);
1246 host->ios.drv_type = drv_type;
1247 mmc_set_ios(host);
1248 mmc_host_clk_release(host);
1251 static void mmc_poweroff_notify(struct mmc_host *host)
1253 struct mmc_card *card;
1254 unsigned int timeout;
1255 unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION;
1256 int err = 0;
1258 card = host->card;
1259 mmc_claim_host(host);
1262 * Send power notify command only if card
1263 * is mmc and notify state is powered ON
1265 if (card && mmc_card_mmc(card) &&
1266 (card->poweroff_notify_state == MMC_POWERED_ON)) {
1268 if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
1269 notify_type = EXT_CSD_POWER_OFF_SHORT;
1270 timeout = card->ext_csd.generic_cmd6_time;
1271 card->poweroff_notify_state = MMC_POWEROFF_SHORT;
1272 } else {
1273 notify_type = EXT_CSD_POWER_OFF_LONG;
1274 timeout = card->ext_csd.power_off_longtime;
1275 card->poweroff_notify_state = MMC_POWEROFF_LONG;
1278 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1279 EXT_CSD_POWER_OFF_NOTIFICATION,
1280 notify_type, timeout);
1282 if (err && err != -EBADMSG)
1283 pr_err("Device failed to respond within %d poweroff "
1284 "time. Forcefully powering down the device\n",
1285 timeout);
1287 /* Set the card state to no notification after the poweroff */
1288 card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
1290 mmc_release_host(host);
1294 * Apply power to the MMC stack. This is a two-stage process.
1295 * First, we enable power to the card without the clock running.
1296 * We then wait a bit for the power to stabilise. Finally,
1297 * enable the bus drivers and clock to the card.
1299 * We must _NOT_ enable the clock prior to power stablising.
1301 * If a host does all the power sequencing itself, ignore the
1302 * initial MMC_POWER_UP stage.
1304 static void mmc_power_up(struct mmc_host *host)
1306 int bit;
1308 mmc_host_clk_hold(host);
1310 /* If ocr is set, we use it */
1311 if (host->ocr)
1312 bit = ffs(host->ocr) - 1;
1313 else
1314 bit = fls(host->ocr_avail) - 1;
1316 host->ios.vdd = bit;
1317 if (mmc_host_is_spi(host))
1318 host->ios.chip_select = MMC_CS_HIGH;
1319 else
1320 host->ios.chip_select = MMC_CS_DONTCARE;
1321 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1322 host->ios.power_mode = MMC_POWER_UP;
1323 host->ios.bus_width = MMC_BUS_WIDTH_1;
1324 host->ios.timing = MMC_TIMING_LEGACY;
1325 mmc_set_ios(host);
1328 * This delay should be sufficient to allow the power supply
1329 * to reach the minimum voltage.
1331 mmc_delay(10);
1333 host->ios.clock = host->f_init;
1335 host->ios.power_mode = MMC_POWER_ON;
1336 mmc_set_ios(host);
1339 * This delay must be at least 74 clock sizes, or 1 ms, or the
1340 * time required to reach a stable voltage.
1342 mmc_delay(10);
1344 mmc_host_clk_release(host);
1347 void mmc_power_off(struct mmc_host *host)
1349 int err = 0;
1350 mmc_host_clk_hold(host);
1352 host->ios.clock = 0;
1353 host->ios.vdd = 0;
1356 * For eMMC 4.5 device send AWAKE command before
1357 * POWER_OFF_NOTIFY command, because in sleep state
1358 * eMMC 4.5 devices respond to only RESET and AWAKE cmd
1360 if (host->card && mmc_card_is_sleep(host->card) &&
1361 host->bus_ops->resume) {
1362 err = host->bus_ops->resume(host);
1364 if (!err)
1365 mmc_poweroff_notify(host);
1366 else
1367 pr_warning("%s: error %d during resume "
1368 "(continue with poweroff sequence)\n",
1369 mmc_hostname(host), err);
1373 * Reset ocr mask to be the highest possible voltage supported for
1374 * this mmc host. This value will be used at next power up.
1376 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1378 if (!mmc_host_is_spi(host)) {
1379 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1380 host->ios.chip_select = MMC_CS_DONTCARE;
1382 host->ios.power_mode = MMC_POWER_OFF;
1383 host->ios.bus_width = MMC_BUS_WIDTH_1;
1384 host->ios.timing = MMC_TIMING_LEGACY;
1385 mmc_set_ios(host);
1388 * Some configurations, such as the 802.11 SDIO card in the OLPC
1389 * XO-1.5, require a short delay after poweroff before the card
1390 * can be successfully turned on again.
1392 mmc_delay(1);
1394 mmc_host_clk_release(host);
1398 * Cleanup when the last reference to the bus operator is dropped.
1400 static void __mmc_release_bus(struct mmc_host *host)
1402 BUG_ON(!host);
1403 BUG_ON(host->bus_refs);
1404 BUG_ON(!host->bus_dead);
1406 host->bus_ops = NULL;
1410 * Increase reference count of bus operator
1412 static inline void mmc_bus_get(struct mmc_host *host)
1414 unsigned long flags;
1416 spin_lock_irqsave(&host->lock, flags);
1417 host->bus_refs++;
1418 spin_unlock_irqrestore(&host->lock, flags);
1422 * Decrease reference count of bus operator and free it if
1423 * it is the last reference.
1425 static inline void mmc_bus_put(struct mmc_host *host)
1427 unsigned long flags;
1429 spin_lock_irqsave(&host->lock, flags);
1430 host->bus_refs--;
1431 if ((host->bus_refs == 0) && host->bus_ops)
1432 __mmc_release_bus(host);
1433 spin_unlock_irqrestore(&host->lock, flags);
1437 * Assign a mmc bus handler to a host. Only one bus handler may control a
1438 * host at any given time.
1440 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1442 unsigned long flags;
1444 BUG_ON(!host);
1445 BUG_ON(!ops);
1447 WARN_ON(!host->claimed);
1449 spin_lock_irqsave(&host->lock, flags);
1451 BUG_ON(host->bus_ops);
1452 BUG_ON(host->bus_refs);
1454 host->bus_ops = ops;
1455 host->bus_refs = 1;
1456 host->bus_dead = 0;
1458 spin_unlock_irqrestore(&host->lock, flags);
1462 * Remove the current bus handler from a host.
1464 void mmc_detach_bus(struct mmc_host *host)
1466 unsigned long flags;
1468 BUG_ON(!host);
1470 WARN_ON(!host->claimed);
1471 WARN_ON(!host->bus_ops);
1473 spin_lock_irqsave(&host->lock, flags);
1475 host->bus_dead = 1;
1477 spin_unlock_irqrestore(&host->lock, flags);
1479 mmc_bus_put(host);
1483 * mmc_detect_change - process change of state on a MMC socket
1484 * @host: host which changed state.
1485 * @delay: optional delay to wait before detection (jiffies)
1487 * MMC drivers should call this when they detect a card has been
1488 * inserted or removed. The MMC layer will confirm that any
1489 * present card is still functional, and initialize any newly
1490 * inserted.
1492 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1494 #ifdef CONFIG_MMC_DEBUG
1495 unsigned long flags;
1496 spin_lock_irqsave(&host->lock, flags);
1497 WARN_ON(host->removed);
1498 spin_unlock_irqrestore(&host->lock, flags);
1499 #endif
1500 host->detect_change = 1;
1501 mmc_schedule_delayed_work(&host->detect, delay);
1504 EXPORT_SYMBOL(mmc_detect_change);
1506 void mmc_init_erase(struct mmc_card *card)
1508 unsigned int sz;
1510 if (is_power_of_2(card->erase_size))
1511 card->erase_shift = ffs(card->erase_size) - 1;
1512 else
1513 card->erase_shift = 0;
1516 * It is possible to erase an arbitrarily large area of an SD or MMC
1517 * card. That is not desirable because it can take a long time
1518 * (minutes) potentially delaying more important I/O, and also the
1519 * timeout calculations become increasingly hugely over-estimated.
1520 * Consequently, 'pref_erase' is defined as a guide to limit erases
1521 * to that size and alignment.
1523 * For SD cards that define Allocation Unit size, limit erases to one
1524 * Allocation Unit at a time. For MMC cards that define High Capacity
1525 * Erase Size, whether it is switched on or not, limit to that size.
1526 * Otherwise just have a stab at a good value. For modern cards it
1527 * will end up being 4MiB. Note that if the value is too small, it
1528 * can end up taking longer to erase.
1530 if (mmc_card_sd(card) && card->ssr.au) {
1531 card->pref_erase = card->ssr.au;
1532 card->erase_shift = ffs(card->ssr.au) - 1;
1533 } else if (card->ext_csd.hc_erase_size) {
1534 card->pref_erase = card->ext_csd.hc_erase_size;
1535 } else {
1536 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1537 if (sz < 128)
1538 card->pref_erase = 512 * 1024 / 512;
1539 else if (sz < 512)
1540 card->pref_erase = 1024 * 1024 / 512;
1541 else if (sz < 1024)
1542 card->pref_erase = 2 * 1024 * 1024 / 512;
1543 else
1544 card->pref_erase = 4 * 1024 * 1024 / 512;
1545 if (card->pref_erase < card->erase_size)
1546 card->pref_erase = card->erase_size;
1547 else {
1548 sz = card->pref_erase % card->erase_size;
1549 if (sz)
1550 card->pref_erase += card->erase_size - sz;
1555 static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1556 unsigned int arg, unsigned int qty)
1558 unsigned int erase_timeout;
1560 if (arg == MMC_DISCARD_ARG ||
1561 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1562 erase_timeout = card->ext_csd.trim_timeout;
1563 } else if (card->ext_csd.erase_group_def & 1) {
1564 /* High Capacity Erase Group Size uses HC timeouts */
1565 if (arg == MMC_TRIM_ARG)
1566 erase_timeout = card->ext_csd.trim_timeout;
1567 else
1568 erase_timeout = card->ext_csd.hc_erase_timeout;
1569 } else {
1570 /* CSD Erase Group Size uses write timeout */
1571 unsigned int mult = (10 << card->csd.r2w_factor);
1572 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1573 unsigned int timeout_us;
1575 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1576 if (card->csd.tacc_ns < 1000000)
1577 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1578 else
1579 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1582 * ios.clock is only a target. The real clock rate might be
1583 * less but not that much less, so fudge it by multiplying by 2.
1585 timeout_clks <<= 1;
1586 timeout_us += (timeout_clks * 1000) /
1587 (mmc_host_clk_rate(card->host) / 1000);
1589 erase_timeout = timeout_us / 1000;
1592 * Theoretically, the calculation could underflow so round up
1593 * to 1ms in that case.
1595 if (!erase_timeout)
1596 erase_timeout = 1;
1599 /* Multiplier for secure operations */
1600 if (arg & MMC_SECURE_ARGS) {
1601 if (arg == MMC_SECURE_ERASE_ARG)
1602 erase_timeout *= card->ext_csd.sec_erase_mult;
1603 else
1604 erase_timeout *= card->ext_csd.sec_trim_mult;
1607 erase_timeout *= qty;
1610 * Ensure at least a 1 second timeout for SPI as per
1611 * 'mmc_set_data_timeout()'
1613 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1614 erase_timeout = 1000;
1616 return erase_timeout;
1619 static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1620 unsigned int arg,
1621 unsigned int qty)
1623 unsigned int erase_timeout;
1625 if (card->ssr.erase_timeout) {
1626 /* Erase timeout specified in SD Status Register (SSR) */
1627 erase_timeout = card->ssr.erase_timeout * qty +
1628 card->ssr.erase_offset;
1629 } else {
1631 * Erase timeout not specified in SD Status Register (SSR) so
1632 * use 250ms per write block.
1634 erase_timeout = 250 * qty;
1637 /* Must not be less than 1 second */
1638 if (erase_timeout < 1000)
1639 erase_timeout = 1000;
1641 return erase_timeout;
1644 static unsigned int mmc_erase_timeout(struct mmc_card *card,
1645 unsigned int arg,
1646 unsigned int qty)
1648 if (mmc_card_sd(card))
1649 return mmc_sd_erase_timeout(card, arg, qty);
1650 else
1651 return mmc_mmc_erase_timeout(card, arg, qty);
1654 static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1655 unsigned int to, unsigned int arg)
1657 struct mmc_command cmd = {0};
1658 unsigned int qty = 0;
1659 int err;
1662 * qty is used to calculate the erase timeout which depends on how many
1663 * erase groups (or allocation units in SD terminology) are affected.
1664 * We count erasing part of an erase group as one erase group.
1665 * For SD, the allocation units are always a power of 2. For MMC, the
1666 * erase group size is almost certainly also power of 2, but it does not
1667 * seem to insist on that in the JEDEC standard, so we fall back to
1668 * division in that case. SD may not specify an allocation unit size,
1669 * in which case the timeout is based on the number of write blocks.
1671 * Note that the timeout for secure trim 2 will only be correct if the
1672 * number of erase groups specified is the same as the total of all
1673 * preceding secure trim 1 commands. Since the power may have been
1674 * lost since the secure trim 1 commands occurred, it is generally
1675 * impossible to calculate the secure trim 2 timeout correctly.
1677 if (card->erase_shift)
1678 qty += ((to >> card->erase_shift) -
1679 (from >> card->erase_shift)) + 1;
1680 else if (mmc_card_sd(card))
1681 qty += to - from + 1;
1682 else
1683 qty += ((to / card->erase_size) -
1684 (from / card->erase_size)) + 1;
1686 if (!mmc_card_blockaddr(card)) {
1687 from <<= 9;
1688 to <<= 9;
1691 if (mmc_card_sd(card))
1692 cmd.opcode = SD_ERASE_WR_BLK_START;
1693 else
1694 cmd.opcode = MMC_ERASE_GROUP_START;
1695 cmd.arg = from;
1696 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1697 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1698 if (err) {
1699 pr_err("mmc_erase: group start error %d, "
1700 "status %#x\n", err, cmd.resp[0]);
1701 err = -EIO;
1702 goto out;
1705 memset(&cmd, 0, sizeof(struct mmc_command));
1706 if (mmc_card_sd(card))
1707 cmd.opcode = SD_ERASE_WR_BLK_END;
1708 else
1709 cmd.opcode = MMC_ERASE_GROUP_END;
1710 cmd.arg = to;
1711 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1712 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1713 if (err) {
1714 pr_err("mmc_erase: group end error %d, status %#x\n",
1715 err, cmd.resp[0]);
1716 err = -EIO;
1717 goto out;
1720 memset(&cmd, 0, sizeof(struct mmc_command));
1721 cmd.opcode = MMC_ERASE;
1722 cmd.arg = arg;
1723 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1724 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
1725 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1726 if (err) {
1727 pr_err("mmc_erase: erase error %d, status %#x\n",
1728 err, cmd.resp[0]);
1729 err = -EIO;
1730 goto out;
1733 if (mmc_host_is_spi(card->host))
1734 goto out;
1736 do {
1737 memset(&cmd, 0, sizeof(struct mmc_command));
1738 cmd.opcode = MMC_SEND_STATUS;
1739 cmd.arg = card->rca << 16;
1740 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1741 /* Do not retry else we can't see errors */
1742 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1743 if (err || (cmd.resp[0] & 0xFDF92000)) {
1744 pr_err("error %d requesting status %#x\n",
1745 err, cmd.resp[0]);
1746 err = -EIO;
1747 goto out;
1749 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1750 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
1751 out:
1752 return err;
1756 * mmc_erase - erase sectors.
1757 * @card: card to erase
1758 * @from: first sector to erase
1759 * @nr: number of sectors to erase
1760 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1762 * Caller must claim host before calling this function.
1764 int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1765 unsigned int arg)
1767 unsigned int rem, to = from + nr;
1769 if (!(card->host->caps & MMC_CAP_ERASE) ||
1770 !(card->csd.cmdclass & CCC_ERASE))
1771 return -EOPNOTSUPP;
1773 if (!card->erase_size)
1774 return -EOPNOTSUPP;
1776 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1777 return -EOPNOTSUPP;
1779 if ((arg & MMC_SECURE_ARGS) &&
1780 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1781 return -EOPNOTSUPP;
1783 if ((arg & MMC_TRIM_ARGS) &&
1784 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1785 return -EOPNOTSUPP;
1787 if (arg == MMC_SECURE_ERASE_ARG) {
1788 if (from % card->erase_size || nr % card->erase_size)
1789 return -EINVAL;
1792 if (arg == MMC_ERASE_ARG) {
1793 rem = from % card->erase_size;
1794 if (rem) {
1795 rem = card->erase_size - rem;
1796 from += rem;
1797 if (nr > rem)
1798 nr -= rem;
1799 else
1800 return 0;
1802 rem = nr % card->erase_size;
1803 if (rem)
1804 nr -= rem;
1807 if (nr == 0)
1808 return 0;
1810 to = from + nr;
1812 if (to <= from)
1813 return -EINVAL;
1815 /* 'from' and 'to' are inclusive */
1816 to -= 1;
1818 return mmc_do_erase(card, from, to, arg);
1820 EXPORT_SYMBOL(mmc_erase);
1822 int mmc_can_erase(struct mmc_card *card)
1824 if ((card->host->caps & MMC_CAP_ERASE) &&
1825 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1826 return 1;
1827 return 0;
1829 EXPORT_SYMBOL(mmc_can_erase);
1831 int mmc_can_trim(struct mmc_card *card)
1833 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1834 return 1;
1835 return 0;
1837 EXPORT_SYMBOL(mmc_can_trim);
1839 int mmc_can_discard(struct mmc_card *card)
1842 * As there's no way to detect the discard support bit at v4.5
1843 * use the s/w feature support filed.
1845 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
1846 return 1;
1847 return 0;
1849 EXPORT_SYMBOL(mmc_can_discard);
1851 int mmc_can_sanitize(struct mmc_card *card)
1853 if (!mmc_can_trim(card) && !mmc_can_erase(card))
1854 return 0;
1855 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
1856 return 1;
1857 return 0;
1859 EXPORT_SYMBOL(mmc_can_sanitize);
1861 int mmc_can_secure_erase_trim(struct mmc_card *card)
1863 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1864 return 1;
1865 return 0;
1867 EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1869 int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1870 unsigned int nr)
1872 if (!card->erase_size)
1873 return 0;
1874 if (from % card->erase_size || nr % card->erase_size)
1875 return 0;
1876 return 1;
1878 EXPORT_SYMBOL(mmc_erase_group_aligned);
1880 static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1881 unsigned int arg)
1883 struct mmc_host *host = card->host;
1884 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1885 unsigned int last_timeout = 0;
1887 if (card->erase_shift)
1888 max_qty = UINT_MAX >> card->erase_shift;
1889 else if (mmc_card_sd(card))
1890 max_qty = UINT_MAX;
1891 else
1892 max_qty = UINT_MAX / card->erase_size;
1894 /* Find the largest qty with an OK timeout */
1895 do {
1896 y = 0;
1897 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1898 timeout = mmc_erase_timeout(card, arg, qty + x);
1899 if (timeout > host->max_discard_to)
1900 break;
1901 if (timeout < last_timeout)
1902 break;
1903 last_timeout = timeout;
1904 y = x;
1906 qty += y;
1907 } while (y);
1909 if (!qty)
1910 return 0;
1912 if (qty == 1)
1913 return 1;
1915 /* Convert qty to sectors */
1916 if (card->erase_shift)
1917 max_discard = --qty << card->erase_shift;
1918 else if (mmc_card_sd(card))
1919 max_discard = qty;
1920 else
1921 max_discard = --qty * card->erase_size;
1923 return max_discard;
1926 unsigned int mmc_calc_max_discard(struct mmc_card *card)
1928 struct mmc_host *host = card->host;
1929 unsigned int max_discard, max_trim;
1931 if (!host->max_discard_to)
1932 return UINT_MAX;
1935 * Without erase_group_def set, MMC erase timeout depends on clock
1936 * frequence which can change. In that case, the best choice is
1937 * just the preferred erase size.
1939 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1940 return card->pref_erase;
1942 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1943 if (mmc_can_trim(card)) {
1944 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1945 if (max_trim < max_discard)
1946 max_discard = max_trim;
1947 } else if (max_discard < card->erase_size) {
1948 max_discard = 0;
1950 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1951 mmc_hostname(host), max_discard, host->max_discard_to);
1952 return max_discard;
1954 EXPORT_SYMBOL(mmc_calc_max_discard);
1956 int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1958 struct mmc_command cmd = {0};
1960 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1961 return 0;
1963 cmd.opcode = MMC_SET_BLOCKLEN;
1964 cmd.arg = blocklen;
1965 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1966 return mmc_wait_for_cmd(card->host, &cmd, 5);
1968 EXPORT_SYMBOL(mmc_set_blocklen);
1970 static void mmc_hw_reset_for_init(struct mmc_host *host)
1972 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1973 return;
1974 mmc_host_clk_hold(host);
1975 host->ops->hw_reset(host);
1976 mmc_host_clk_release(host);
1979 int mmc_can_reset(struct mmc_card *card)
1981 u8 rst_n_function;
1983 if (!mmc_card_mmc(card))
1984 return 0;
1985 rst_n_function = card->ext_csd.rst_n_function;
1986 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1987 return 0;
1988 return 1;
1990 EXPORT_SYMBOL(mmc_can_reset);
1992 static int mmc_do_hw_reset(struct mmc_host *host, int check)
1994 struct mmc_card *card = host->card;
1996 if (!host->bus_ops->power_restore)
1997 return -EOPNOTSUPP;
1999 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2000 return -EOPNOTSUPP;
2002 if (!card)
2003 return -EINVAL;
2005 if (!mmc_can_reset(card))
2006 return -EOPNOTSUPP;
2008 mmc_host_clk_hold(host);
2009 mmc_set_clock(host, host->f_init);
2011 host->ops->hw_reset(host);
2013 /* If the reset has happened, then a status command will fail */
2014 if (check) {
2015 struct mmc_command cmd = {0};
2016 int err;
2018 cmd.opcode = MMC_SEND_STATUS;
2019 if (!mmc_host_is_spi(card->host))
2020 cmd.arg = card->rca << 16;
2021 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2022 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2023 if (!err) {
2024 mmc_host_clk_release(host);
2025 return -ENOSYS;
2029 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
2030 if (mmc_host_is_spi(host)) {
2031 host->ios.chip_select = MMC_CS_HIGH;
2032 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
2033 } else {
2034 host->ios.chip_select = MMC_CS_DONTCARE;
2035 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
2037 host->ios.bus_width = MMC_BUS_WIDTH_1;
2038 host->ios.timing = MMC_TIMING_LEGACY;
2039 mmc_set_ios(host);
2041 mmc_host_clk_release(host);
2043 return host->bus_ops->power_restore(host);
2046 int mmc_hw_reset(struct mmc_host *host)
2048 return mmc_do_hw_reset(host, 0);
2050 EXPORT_SYMBOL(mmc_hw_reset);
2052 int mmc_hw_reset_check(struct mmc_host *host)
2054 return mmc_do_hw_reset(host, 1);
2056 EXPORT_SYMBOL(mmc_hw_reset_check);
2058 static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2060 host->f_init = freq;
2062 #ifdef CONFIG_MMC_DEBUG
2063 pr_info("%s: %s: trying to init card at %u Hz\n",
2064 mmc_hostname(host), __func__, host->f_init);
2065 #endif
2066 mmc_power_up(host);
2069 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2070 * do a hardware reset if possible.
2072 mmc_hw_reset_for_init(host);
2074 /* Initialization should be done at 3.3 V I/O voltage. */
2075 mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, 0);
2078 * sdio_reset sends CMD52 to reset card. Since we do not know
2079 * if the card is being re-initialized, just send it. CMD52
2080 * should be ignored by SD/eMMC cards.
2082 sdio_reset(host);
2083 mmc_go_idle(host);
2085 mmc_send_if_cond(host, host->ocr_avail);
2087 /* Order's important: probe SDIO, then SD, then MMC */
2088 if (!mmc_attach_sdio(host))
2089 return 0;
2090 if (!mmc_attach_sd(host))
2091 return 0;
2092 if (!mmc_attach_mmc(host))
2093 return 0;
2095 mmc_power_off(host);
2096 return -EIO;
2099 int _mmc_detect_card_removed(struct mmc_host *host)
2101 int ret;
2103 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2104 return 0;
2106 if (!host->card || mmc_card_removed(host->card))
2107 return 1;
2109 ret = host->bus_ops->alive(host);
2110 if (ret) {
2111 mmc_card_set_removed(host->card);
2112 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2115 return ret;
2118 int mmc_detect_card_removed(struct mmc_host *host)
2120 struct mmc_card *card = host->card;
2122 WARN_ON(!host->claimed);
2124 * The card will be considered unchanged unless we have been asked to
2125 * detect a change or host requires polling to provide card detection.
2127 if (card && !host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2128 return mmc_card_removed(card);
2130 host->detect_change = 0;
2132 return _mmc_detect_card_removed(host);
2134 EXPORT_SYMBOL(mmc_detect_card_removed);
2136 void mmc_rescan(struct work_struct *work)
2138 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
2139 struct mmc_host *host =
2140 container_of(work, struct mmc_host, detect.work);
2141 int i;
2143 if (host->rescan_disable)
2144 return;
2146 mmc_bus_get(host);
2149 * if there is a _removable_ card registered, check whether it is
2150 * still present
2152 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
2153 && !(host->caps & MMC_CAP_NONREMOVABLE))
2154 host->bus_ops->detect(host);
2156 host->detect_change = 0;
2159 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2160 * the card is no longer present.
2162 mmc_bus_put(host);
2163 mmc_bus_get(host);
2165 /* if there still is a card present, stop here */
2166 if (host->bus_ops != NULL) {
2167 mmc_bus_put(host);
2168 goto out;
2172 * Only we can add a new handler, so it's safe to
2173 * release the lock here.
2175 mmc_bus_put(host);
2177 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
2178 goto out;
2180 mmc_claim_host(host);
2181 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
2182 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2183 break;
2184 if (freqs[i] <= host->f_min)
2185 break;
2187 mmc_release_host(host);
2189 out:
2190 if (host->caps & MMC_CAP_NEEDS_POLL)
2191 mmc_schedule_delayed_work(&host->detect, HZ);
2194 void mmc_start_host(struct mmc_host *host)
2196 mmc_power_off(host);
2197 mmc_detect_change(host, 0);
2200 void mmc_stop_host(struct mmc_host *host)
2202 #ifdef CONFIG_MMC_DEBUG
2203 unsigned long flags;
2204 spin_lock_irqsave(&host->lock, flags);
2205 host->removed = 1;
2206 spin_unlock_irqrestore(&host->lock, flags);
2207 #endif
2209 if (host->caps & MMC_CAP_DISABLE)
2210 cancel_delayed_work(&host->disable);
2211 cancel_delayed_work_sync(&host->detect);
2212 mmc_flush_scheduled_work();
2214 /* clear pm flags now and let card drivers set them as needed */
2215 host->pm_flags = 0;
2217 mmc_bus_get(host);
2218 if (host->bus_ops && !host->bus_dead) {
2219 /* Calling bus_ops->remove() with a claimed host can deadlock */
2220 if (host->bus_ops->remove)
2221 host->bus_ops->remove(host);
2223 mmc_claim_host(host);
2224 mmc_detach_bus(host);
2225 mmc_power_off(host);
2226 mmc_release_host(host);
2227 mmc_bus_put(host);
2228 return;
2230 mmc_bus_put(host);
2232 BUG_ON(host->card);
2234 mmc_power_off(host);
2237 int mmc_power_save_host(struct mmc_host *host)
2239 int ret = 0;
2241 #ifdef CONFIG_MMC_DEBUG
2242 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2243 #endif
2245 mmc_bus_get(host);
2247 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2248 mmc_bus_put(host);
2249 return -EINVAL;
2252 if (host->bus_ops->power_save)
2253 ret = host->bus_ops->power_save(host);
2255 mmc_bus_put(host);
2257 mmc_power_off(host);
2259 return ret;
2261 EXPORT_SYMBOL(mmc_power_save_host);
2263 int mmc_power_restore_host(struct mmc_host *host)
2265 int ret;
2267 #ifdef CONFIG_MMC_DEBUG
2268 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2269 #endif
2271 mmc_bus_get(host);
2273 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2274 mmc_bus_put(host);
2275 return -EINVAL;
2278 mmc_power_up(host);
2279 ret = host->bus_ops->power_restore(host);
2281 mmc_bus_put(host);
2283 return ret;
2285 EXPORT_SYMBOL(mmc_power_restore_host);
2287 int mmc_card_awake(struct mmc_host *host)
2289 int err = -ENOSYS;
2291 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2292 return 0;
2294 mmc_bus_get(host);
2296 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2297 err = host->bus_ops->awake(host);
2299 mmc_bus_put(host);
2301 return err;
2303 EXPORT_SYMBOL(mmc_card_awake);
2305 int mmc_card_sleep(struct mmc_host *host)
2307 int err = -ENOSYS;
2309 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2310 return 0;
2312 mmc_bus_get(host);
2314 if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
2315 err = host->bus_ops->sleep(host);
2317 mmc_bus_put(host);
2319 return err;
2321 EXPORT_SYMBOL(mmc_card_sleep);
2323 int mmc_card_can_sleep(struct mmc_host *host)
2325 struct mmc_card *card = host->card;
2327 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2328 return 1;
2329 return 0;
2331 EXPORT_SYMBOL(mmc_card_can_sleep);
2334 * Flush the cache to the non-volatile storage.
2336 int mmc_flush_cache(struct mmc_card *card)
2338 struct mmc_host *host = card->host;
2339 int err = 0;
2341 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2342 return err;
2344 if (mmc_card_mmc(card) &&
2345 (card->ext_csd.cache_size > 0) &&
2346 (card->ext_csd.cache_ctrl & 1)) {
2347 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2348 EXT_CSD_FLUSH_CACHE, 1, 0);
2349 if (err)
2350 pr_err("%s: cache flush error %d\n",
2351 mmc_hostname(card->host), err);
2354 return err;
2356 EXPORT_SYMBOL(mmc_flush_cache);
2359 * Turn the cache ON/OFF.
2360 * Turning the cache OFF shall trigger flushing of the data
2361 * to the non-volatile storage.
2363 int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2365 struct mmc_card *card = host->card;
2366 unsigned int timeout;
2367 int err = 0;
2369 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2370 mmc_card_is_removable(host))
2371 return err;
2373 if (card && mmc_card_mmc(card) &&
2374 (card->ext_csd.cache_size > 0)) {
2375 enable = !!enable;
2377 if (card->ext_csd.cache_ctrl ^ enable) {
2378 timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
2379 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2380 EXT_CSD_CACHE_CTRL, enable, timeout);
2381 if (err)
2382 pr_err("%s: cache %s error %d\n",
2383 mmc_hostname(card->host),
2384 enable ? "on" : "off",
2385 err);
2386 else
2387 card->ext_csd.cache_ctrl = enable;
2391 return err;
2393 EXPORT_SYMBOL(mmc_cache_ctrl);
2395 #ifdef CONFIG_PM
2398 * mmc_suspend_host - suspend a host
2399 * @host: mmc host
2401 int mmc_suspend_host(struct mmc_host *host)
2403 int err = 0;
2405 if (host->caps & MMC_CAP_DISABLE)
2406 cancel_delayed_work(&host->disable);
2407 cancel_delayed_work(&host->detect);
2408 mmc_flush_scheduled_work();
2409 if (mmc_try_claim_host(host)) {
2410 err = mmc_cache_ctrl(host, 0);
2411 mmc_do_release_host(host);
2412 } else {
2413 err = -EBUSY;
2416 if (err)
2417 goto out;
2419 mmc_bus_get(host);
2420 if (host->bus_ops && !host->bus_dead) {
2423 * A long response time is not acceptable for device drivers
2424 * when doing suspend. Prevent mmc_claim_host in the suspend
2425 * sequence, to potentially wait "forever" by trying to
2426 * pre-claim the host.
2428 if (mmc_try_claim_host(host)) {
2429 if (host->bus_ops->suspend) {
2430 err = host->bus_ops->suspend(host);
2432 mmc_do_release_host(host);
2434 if (err == -ENOSYS || !host->bus_ops->resume) {
2436 * We simply "remove" the card in this case.
2437 * It will be redetected on resume. (Calling
2438 * bus_ops->remove() with a claimed host can
2439 * deadlock.)
2441 if (host->bus_ops->remove)
2442 host->bus_ops->remove(host);
2443 mmc_claim_host(host);
2444 mmc_detach_bus(host);
2445 mmc_power_off(host);
2446 mmc_release_host(host);
2447 host->pm_flags = 0;
2448 err = 0;
2450 } else {
2451 err = -EBUSY;
2454 mmc_bus_put(host);
2456 if (!err && !mmc_card_keep_power(host))
2457 mmc_power_off(host);
2459 out:
2460 return err;
2463 EXPORT_SYMBOL(mmc_suspend_host);
2466 * mmc_resume_host - resume a previously suspended host
2467 * @host: mmc host
2469 int mmc_resume_host(struct mmc_host *host)
2471 int err = 0;
2473 mmc_bus_get(host);
2474 if (host->bus_ops && !host->bus_dead) {
2475 if (!mmc_card_keep_power(host)) {
2476 mmc_power_up(host);
2477 mmc_select_voltage(host, host->ocr);
2479 * Tell runtime PM core we just powered up the card,
2480 * since it still believes the card is powered off.
2481 * Note that currently runtime PM is only enabled
2482 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2484 if (mmc_card_sdio(host->card) &&
2485 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2486 pm_runtime_disable(&host->card->dev);
2487 pm_runtime_set_active(&host->card->dev);
2488 pm_runtime_enable(&host->card->dev);
2491 BUG_ON(!host->bus_ops->resume);
2492 err = host->bus_ops->resume(host);
2493 if (err) {
2494 pr_warning("%s: error %d during resume "
2495 "(card was removed?)\n",
2496 mmc_hostname(host), err);
2497 err = 0;
2500 host->pm_flags &= ~MMC_PM_KEEP_POWER;
2501 mmc_bus_put(host);
2503 return err;
2505 EXPORT_SYMBOL(mmc_resume_host);
2507 /* Do the card removal on suspend if card is assumed removeable
2508 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2509 to sync the card.
2511 int mmc_pm_notify(struct notifier_block *notify_block,
2512 unsigned long mode, void *unused)
2514 struct mmc_host *host = container_of(
2515 notify_block, struct mmc_host, pm_notify);
2516 unsigned long flags;
2519 switch (mode) {
2520 case PM_HIBERNATION_PREPARE:
2521 case PM_SUSPEND_PREPARE:
2523 spin_lock_irqsave(&host->lock, flags);
2524 host->rescan_disable = 1;
2525 host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
2526 spin_unlock_irqrestore(&host->lock, flags);
2527 cancel_delayed_work_sync(&host->detect);
2529 if (!host->bus_ops || host->bus_ops->suspend)
2530 break;
2532 /* Calling bus_ops->remove() with a claimed host can deadlock */
2533 if (host->bus_ops->remove)
2534 host->bus_ops->remove(host);
2536 mmc_claim_host(host);
2537 mmc_detach_bus(host);
2538 mmc_power_off(host);
2539 mmc_release_host(host);
2540 host->pm_flags = 0;
2541 break;
2543 case PM_POST_SUSPEND:
2544 case PM_POST_HIBERNATION:
2545 case PM_POST_RESTORE:
2547 spin_lock_irqsave(&host->lock, flags);
2548 host->rescan_disable = 0;
2549 host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
2550 spin_unlock_irqrestore(&host->lock, flags);
2551 mmc_detect_change(host, 0);
2555 return 0;
2557 #endif
2559 static int __init mmc_init(void)
2561 int ret;
2563 workqueue = alloc_ordered_workqueue("kmmcd", 0);
2564 if (!workqueue)
2565 return -ENOMEM;
2567 ret = mmc_register_bus();
2568 if (ret)
2569 goto destroy_workqueue;
2571 ret = mmc_register_host_class();
2572 if (ret)
2573 goto unregister_bus;
2575 ret = sdio_register_bus();
2576 if (ret)
2577 goto unregister_host_class;
2579 return 0;
2581 unregister_host_class:
2582 mmc_unregister_host_class();
2583 unregister_bus:
2584 mmc_unregister_bus();
2585 destroy_workqueue:
2586 destroy_workqueue(workqueue);
2588 return ret;
2591 static void __exit mmc_exit(void)
2593 sdio_unregister_bus();
2594 mmc_unregister_host_class();
2595 mmc_unregister_bus();
2596 destroy_workqueue(workqueue);
2599 subsys_initcall(mmc_init);
2600 module_exit(mmc_exit);
2602 MODULE_LICENSE("GPL");