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>
29 #include <linux/slab.h>
30 #include <linux/wakelock.h>
32 #include <trace/events/mmc.h>
34 #include <linux/mmc/card.h>
35 #include <linux/mmc/host.h>
36 #include <linux/mmc/mmc.h>
37 #include <linux/mmc/sd.h>
39 #define FEATURE_STORAGE_PERF_INDEX
41 #ifdef USER_BUILD_KERNEL
42 #undef FEATURE_STORAGE_PERF_INDEX
54 #define MET_USER_EVENT_SUPPORT
55 #include <linux/met_drv.h>
56 extern void met_mmc_insert(struct mmc_host
*host
, struct mmc_async_req
*areq
);
57 extern void met_mmc_dma_map(struct mmc_host
*host
, struct mmc_async_req
*areq
);
58 extern void met_mmc_wait_xfr(struct mmc_host
*host
, struct mmc_async_req
*areq
);
59 extern void met_mmc_complete(struct mmc_host
*host
, struct mmc_async_req
*areq
);
60 extern void met_mmc_dma_unmap_start(struct mmc_host
*host
, struct mmc_async_req
*areq
);
61 extern void met_mmc_dma_unmap_stop(struct mmc_host
*host
, struct mmc_async_req
*areq
);
62 extern void met_mmc_continue_req_end(struct mmc_host
*host
, struct mmc_async_req
*areq
);
64 #define DAT_TIMEOUT (HZ * 5)
65 /* If the device is not responding */
66 #define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
69 * Background operations can take a long time, depending on the housekeeping
70 * operations the card has to perform.
72 #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */
74 #ifdef CONFIG_MTK_HIBERNATION
75 #define MMC_PM_RESTORE_WAIT_MS (5000) /* PM_RESTORE check mmc_rescan finish at most wait 5s */
78 static struct workqueue_struct
*workqueue
;
79 static const unsigned freqs
[] = { 300000, 260000, 200000, 100000 };
82 * Enabling software CRCs on the data blocks can be a significant (30%)
83 * performance cost, and for other reasons may not always be desired.
84 * So we allow it it to be disabled.
87 module_param(use_spi_crc
, bool, 0);
90 * We normally treat cards as removed during suspend if they are not
91 * known to be on a non-removable bus, to avoid the risk of writing
92 * back data to a different card after resume. Allow this to be
93 * overridden if necessary.
95 #ifdef CONFIG_MMC_UNSAFE_RESUME
96 bool mmc_assume_removable
;
98 bool mmc_assume_removable
= 1;
100 EXPORT_SYMBOL(mmc_assume_removable
);
101 module_param_named(removable
, mmc_assume_removable
, bool, 0644);
104 "MMC/SD cards are removable and may be removed during suspend");
107 * Internal function. Schedule delayed work in the MMC work queue.
109 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
112 return queue_delayed_work(workqueue
, work
, delay
);
116 * Internal function. Flush all scheduled work from the MMC work queue.
118 static void mmc_flush_scheduled_work(void)
120 flush_workqueue(workqueue
);
123 #ifdef CONFIG_FAIL_MMC_REQUEST
126 * Internal function. Inject random data errors.
127 * If mmc_data is NULL no errors are injected.
129 static void mmc_should_fail_request(struct mmc_host
*host
,
130 struct mmc_request
*mrq
)
132 struct mmc_command
*cmd
= mrq
->cmd
;
133 struct mmc_data
*data
= mrq
->data
;
134 static const int data_errors
[] = {
143 if (cmd
->error
|| data
->error
||
144 !should_fail(&host
->fail_mmc_request
, data
->blksz
* data
->blocks
))
147 data
->error
= data_errors
[prandom_u32() % ARRAY_SIZE(data_errors
)];
148 data
->bytes_xfered
= (prandom_u32() % (data
->bytes_xfered
>> 9)) << 9;
151 #else /* CONFIG_FAIL_MMC_REQUEST */
153 static inline void mmc_should_fail_request(struct mmc_host
*host
,
154 struct mmc_request
*mrq
)
158 #endif /* CONFIG_FAIL_MMC_REQUEST */
161 * mmc_request_done - finish processing an MMC request
162 * @host: MMC host which completed request
163 * @mrq: MMC request which request
165 * MMC drivers should call this function when they have completed
166 * their processing of a request.
168 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
170 struct mmc_command
*cmd
= mrq
->cmd
;
171 int err
= cmd
->error
;
173 if (err
&& cmd
->retries
&& mmc_host_is_spi(host
)) {
174 if (cmd
->resp
[0] & R1_SPI_ILLEGAL_COMMAND
)
178 if (err
&& cmd
->retries
&& !mmc_card_removed(host
->card
)) {
180 * Request starter must handle retries - see
181 * mmc_wait_for_req_done().
186 mmc_should_fail_request(host
, mrq
);
188 led_trigger_event(host
->led
, LED_OFF
);
190 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
191 mmc_hostname(host
), cmd
->opcode
, err
,
192 cmd
->resp
[0], cmd
->resp
[1],
193 cmd
->resp
[2], cmd
->resp
[3]);
196 pr_debug("%s: %d bytes transferred: %d\n",
198 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
199 trace_mmc_blk_rw_end(cmd
->opcode
, cmd
->arg
, mrq
->data
);
203 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
204 mmc_hostname(host
), mrq
->stop
->opcode
,
206 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
207 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
213 mmc_host_clk_release(host
);
217 EXPORT_SYMBOL(mmc_request_done
);
220 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
222 #ifdef CONFIG_MMC_DEBUG
224 struct scatterlist
*sg
;
228 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
229 mmc_hostname(host
), mrq
->sbc
->opcode
,
230 mrq
->sbc
->arg
, mrq
->sbc
->flags
);
233 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
234 mmc_hostname(host
), mrq
->cmd
->opcode
,
235 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
238 pr_debug("%s: blksz %d blocks %d flags %08x "
239 "tsac %d ms nsac %d\n",
240 mmc_hostname(host
), mrq
->data
->blksz
,
241 mrq
->data
->blocks
, mrq
->data
->flags
,
242 mrq
->data
->timeout_ns
/ 1000000,
243 mrq
->data
->timeout_clks
);
247 pr_debug("%s: CMD%u arg %08x flags %08x\n",
248 mmc_hostname(host
), mrq
->stop
->opcode
,
249 mrq
->stop
->arg
, mrq
->stop
->flags
);
252 WARN_ON(!host
->claimed
);
257 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
258 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
259 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
262 #ifdef CONFIG_MMC_DEBUG
264 for_each_sg(mrq
->data
->sg
, sg
, mrq
->data
->sg_len
, i
)
266 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
269 mrq
->cmd
->data
= mrq
->data
;
270 mrq
->data
->error
= 0;
271 mrq
->data
->mrq
= mrq
;
273 mrq
->data
->stop
= mrq
->stop
;
274 mrq
->stop
->error
= 0;
275 mrq
->stop
->mrq
= mrq
;
278 mmc_host_clk_hold(host
);
279 led_trigger_event(host
->led
, LED_FULL
);
280 host
->ops
->request(host
, mrq
);
284 * mmc_start_bkops - start BKOPS for supported cards
285 * @card: MMC card to start BKOPS
286 * @form_exception: A flag to indicate if this function was
287 * called due to an exception raised by the card
289 * Start background operations whenever requested.
290 * When the urgent BKOPS bit is set in a R1 command response
291 * then background operations should be started immediately.
293 void mmc_start_bkops(struct mmc_card
*card
, bool from_exception
)
297 bool use_busy_signal
;
301 if (!card
->ext_csd
.bkops_en
|| mmc_card_doing_bkops(card
))
304 err
= mmc_read_bkops_status(card
);
306 pr_err("%s: Failed to read bkops status: %d\n",
307 mmc_hostname(card
->host
), err
);
311 if (!card
->ext_csd
.raw_bkops_status
)
314 if (card
->ext_csd
.raw_bkops_status
< EXT_CSD_BKOPS_LEVEL_2
&&
318 mmc_claim_host(card
->host
);
319 if (card
->ext_csd
.raw_bkops_status
>= EXT_CSD_BKOPS_LEVEL_2
) {
320 timeout
= MMC_BKOPS_MAX_TIMEOUT
;
321 use_busy_signal
= true;
324 use_busy_signal
= false;
327 err
= __mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
328 EXT_CSD_BKOPS_START
, 1, timeout
, use_busy_signal
);
330 pr_warn("%s: Error %d starting bkops\n",
331 mmc_hostname(card
->host
), err
);
336 * For urgent bkops status (LEVEL_2 and more)
337 * bkops executed synchronously, otherwise
338 * the operation is in progress
340 if (!use_busy_signal
)
341 mmc_card_set_doing_bkops(card
);
343 mmc_release_host(card
->host
);
345 EXPORT_SYMBOL(mmc_start_bkops
);
348 * mmc_wait_data_done() - done callback for data request
349 * @mrq: done data request
351 * Wakes up mmc context, passed as a callback to host controller driver
353 static void mmc_wait_data_done(struct mmc_request
*mrq
)
355 mrq
->host
->context_info
.is_done_rcv
= true;
356 wake_up_interruptible(&mrq
->host
->context_info
.wait
);
359 static void mmc_wait_done(struct mmc_request
*mrq
)
361 complete(&mrq
->completion
);
365 *__mmc_start_data_req() - starts data request
366 * @host: MMC host to start the request
367 * @mrq: data request to start
369 * Sets the done callback to be called when request is completed by the card.
370 * Starts data mmc request execution
372 static int __mmc_start_data_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
374 mrq
->done
= mmc_wait_data_done
;
376 if (mmc_card_removed(host
->card
)) {
377 mrq
->cmd
->error
= -ENOMEDIUM
;
378 mmc_wait_data_done(mrq
);
381 mmc_start_request(host
, mrq
);
386 static int __mmc_start_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
388 init_completion(&mrq
->completion
);
389 mrq
->done
= mmc_wait_done
;
390 if (mmc_card_removed(host
->card
)) {
391 mrq
->cmd
->error
= -ENOMEDIUM
;
392 complete(&mrq
->completion
);
395 mmc_start_request(host
, mrq
);
400 * mmc_wait_for_data_req_done() - wait for request completed
401 * @host: MMC host to prepare the command.
402 * @mrq: MMC request to wait for
404 * Blocks MMC context till host controller will ack end of data request
405 * execution or new request notification arrives from the block layer.
406 * Handles command retries.
408 * Returns enum mmc_blk_status after checking errors.
410 static int mmc_wait_for_data_req_done(struct mmc_host
*host
,
411 struct mmc_request
*mrq
,
412 struct mmc_async_req
*next_req
)
414 struct mmc_command
*cmd
;
415 struct mmc_context_info
*context_info
= &host
->context_info
;
420 wait_event_interruptible(context_info
->wait
,
421 (context_info
->is_done_rcv
||
422 context_info
->is_new_req
));
423 spin_lock_irqsave(&context_info
->lock
, flags
);
424 context_info
->is_waiting_last_req
= false;
425 spin_unlock_irqrestore(&context_info
->lock
, flags
);
426 if (context_info
->is_done_rcv
) {
427 context_info
->is_done_rcv
= false;
428 context_info
->is_new_req
= false;
430 if (!cmd
->error
|| !cmd
->retries
||
431 mmc_card_removed(host
->card
)) {
432 err
= host
->areq
->err_check(host
->card
,
434 break; /* return err */
436 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
438 cmd
->opcode
, cmd
->error
);
441 host
->ops
->request(host
, mrq
);
442 continue; /* wait for done/new event again */
444 } else if (context_info
->is_new_req
) {
445 context_info
->is_new_req
= false;
447 err
= MMC_BLK_NEW_REQUEST
;
448 break; /* return err */
455 static void mmc_wait_for_req_done(struct mmc_host
*host
,
456 struct mmc_request
*mrq
)
459 struct scatterlist
*sg
;
466 struct mmc_command
*cmd
;
469 if(!wait_for_completion_timeout(&mrq
->completion
,DAT_TIMEOUT
)){
470 printk(KERN_ERR
"MSDC wait request timeout CMD<%d>ARG<0x%x>\n",mrq
->cmd
->opcode
,mrq
->cmd
->arg
);
472 host
->ops
->dma_error_reset(host
);
476 mrq
->data
->error
= (unsigned int)-ETIMEDOUT
;
479 printk(KERN_ERR
"MSDC wait request timeout DAT<%d>\n",(mrq
->data
->blocks
) * (mrq
->data
->blksz
));
483 if ((mrq
->cmd
->arg
== 0) && (mrq
->data
) &&
484 ((mrq
->cmd
->opcode
== 17)||(mrq
->cmd
->opcode
== 18))){
485 printk("read MBR cmd%d: blocks %d arg %08x, sg_len = %d\n", mrq
->cmd
->opcode
, mrq
->data
->blocks
, mrq
->cmd
->arg
, mrq
->data
->sg_len
);
487 num
= mrq
->data
->sg_len
;
490 left
= sg_dma_len(sg
);
493 printk("====left: %d\n===\n", left
);
494 for (i
= 0; i
<= left
/4; i
++){
495 printk("0x%x ", *(ptr
+ i
));
500 //page = sg_to_page(sg);
503 //paddr = page_to_phys(page);
512 if (!cmd
->error
|| !cmd
->retries
||
513 mmc_card_removed(host
->card
))
516 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
517 mmc_hostname(host
), cmd
->opcode
, cmd
->error
);
520 host
->ops
->request(host
, mrq
);
525 * mmc_pre_req - Prepare for a new request
526 * @host: MMC host to prepare command
527 * @mrq: MMC request to prepare for
528 * @is_first_req: true if there is no previous started request
529 * that may run in parellel to this call, otherwise false
531 * mmc_pre_req() is called in prior to mmc_start_req() to let
532 * host prepare for the new request. Preparation of a request may be
533 * performed while another request is running on the host.
535 static void mmc_pre_req(struct mmc_host
*host
, struct mmc_request
*mrq
,
538 if (host
->ops
->pre_req
) {
539 mmc_host_clk_hold(host
);
540 host
->ops
->pre_req(host
, mrq
, is_first_req
);
541 mmc_host_clk_release(host
);
546 * mmc_post_req - Post process a completed request
547 * @host: MMC host to post process command
548 * @mrq: MMC request to post process for
549 * @err: Error, if non zero, clean up any resources made in pre_req
551 * Let the host post process a completed request. Post processing of
552 * a request may be performed while another reuqest is running.
554 static void mmc_post_req(struct mmc_host
*host
, struct mmc_request
*mrq
,
557 if (host
->ops
->post_req
) {
558 mmc_host_clk_hold(host
);
559 host
->ops
->post_req(host
, mrq
, err
);
560 mmc_host_clk_release(host
);
565 * mmc_start_req - start a non-blocking request
566 * @host: MMC host to start command
567 * @areq: async request to start
568 * @error: out parameter returns 0 for success, otherwise non zero
570 * Start a new MMC custom command request for a host.
571 * If there is on ongoing async request wait for completion
572 * of that request and start the new one and return.
573 * Does not wait for the new request to complete.
575 * Returns the completed request, NULL in case of none completed.
576 * Wait for the an ongoing request (previoulsy started) to complete and
577 * return the completed request. If there is no ongoing request, NULL
578 * is returned without waiting. NULL is not an error condition.
581 #if defined(FEATURE_STORAGE_PERF_INDEX)
582 extern bool start_async_req
[];
583 extern unsigned long long start_async_req_time
[];
584 extern unsigned int find_mmcqd_index(void);
585 extern unsigned long long mmcqd_t_usage_wr
[];
586 extern unsigned long long mmcqd_t_usage_rd
[];
587 extern unsigned int mmcqd_rq_size_wr
[];
588 extern unsigned int mmcqd_rq_size_rd
[];
589 extern unsigned int mmcqd_rq_count
[];
590 extern unsigned int mmcqd_wr_rq_count
[];
591 extern unsigned int mmcqd_rd_rq_count
[];
594 struct mmc_async_req
*mmc_start_req(struct mmc_host
*host
,
595 struct mmc_async_req
*areq
, int *error
)
599 #if defined(FEATURE_STORAGE_PERF_INDEX)
600 unsigned long long time1
= 0;
601 unsigned int idx
= 0;
603 struct mmc_async_req
*data
= host
->areq
;
607 met_mmc_continue_req_end(host
, host
->areq
);
611 /* Prepare a new request */
613 met_mmc_insert(host
, areq
);
615 mmc_pre_req(host
, areq
->mrq
, !host
->areq
);
617 met_mmc_dma_map(host
, areq
);
621 err
= mmc_wait_for_data_req_done(host
, host
->areq
->mrq
, areq
);
622 if (err
== MMC_BLK_NEW_REQUEST
) {
626 * The previous request was not completed,
631 host
->ops
->send_stop(host
,host
->areq
->mrq
); //add for MTK msdc host <Yuchi Xu>
633 host
->ops
->tuning(host
, host
->areq
->mrq
); //add for MTK msdc host <Yuchi Xu>
634 }while(host
->ops
->check_written_data(host
,host
->areq
->mrq
));
636 #if defined(FEATURE_STORAGE_PERF_INDEX)
637 time1
= sched_clock();
639 idx
= find_mmcqd_index();
640 if (start_async_req
[idx
] == 1)
642 //idx = find_mmcqd_index();
643 mmcqd_rq_count
[idx
]++;
645 if(host
->areq
->mrq
->data
->flags
== MMC_DATA_WRITE
)
647 mmcqd_wr_rq_count
[idx
]++;
648 mmcqd_rq_size_wr
[idx
] += ((host
->areq
->mrq
->data
->blocks
) * (host
->areq
->mrq
->data
->blksz
));
649 mmcqd_t_usage_wr
[idx
] += time1
- start_async_req_time
[idx
];
651 else if (host
->areq
->mrq
->data
->flags
== MMC_DATA_READ
)
653 mmcqd_rd_rq_count
[idx
]++;
654 mmcqd_rq_size_rd
[idx
] += ((host
->areq
->mrq
->data
->blocks
) * (host
->areq
->mrq
->data
->blksz
));
655 mmcqd_t_usage_rd
[idx
] += time1
- start_async_req_time
[idx
];
658 start_async_req
[idx
] = 0;
662 err
= host
->areq
->err_check(host
->card
, host
->areq
);
665 * Check BKOPS urgency for each R1 response
667 if (host
->card
&& mmc_card_mmc(host
->card
) &&
668 ((mmc_resp_type(host
->areq
->mrq
->cmd
) == MMC_RSP_R1
) ||
669 (mmc_resp_type(host
->areq
->mrq
->cmd
) == MMC_RSP_R1B
)) &&
670 (host
->areq
->mrq
->cmd
->resp
[0] & R1_EXCEPTION_EVENT
))
671 mmc_start_bkops(host
->card
, true);
673 met_mmc_complete(host
, host
->areq
);
677 trace_mmc_blk_rw_start(areq
->mrq
->cmd
->opcode
,
680 start_err
= __mmc_start_data_req(host
, areq
->mrq
);
681 #if defined(FEATURE_STORAGE_PERF_INDEX)
682 start_async_req
[idx
] = 1;
683 start_async_req_time
[idx
] = sched_clock();
689 met_mmc_dma_unmap_start(host
, host
->areq
);
691 mmc_post_req(host
, host
->areq
->mrq
, 0);
693 met_mmc_dma_unmap_stop(host
, host
->areq
);
696 /* Cancel a prepared request if it was not started. */
697 if ((err
|| start_err
) && areq
)
698 mmc_post_req(host
, areq
->mrq
, -EINVAL
);
709 EXPORT_SYMBOL(mmc_start_req
);
712 * mmc_wait_for_req - start a request and wait for completion
713 * @host: MMC host to start command
714 * @mrq: MMC request to start
716 * Start a new MMC custom command request for a host, and wait
717 * for the command to complete. Does not attempt to parse the
720 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
722 __mmc_start_req(host
, mrq
);
723 mmc_wait_for_req_done(host
, mrq
);
725 EXPORT_SYMBOL(mmc_wait_for_req
);
728 * mmc_interrupt_hpi - Issue for High priority Interrupt
729 * @card: the MMC card associated with the HPI transfer
731 * Issued High Priority Interrupt, and check for card status
732 * until out-of prg-state.
734 #ifdef MMC_ENABLED_EMPTY_QUEUE_FLUSH
736 int mmc_interrupt_hpi_delay(struct mmc_card
*card
, u32 delay
)
739 u32 status
, delay_count
= 0;
740 unsigned long prg_wait
;
744 if (!card
->ext_csd
.hpi_en
) {
745 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card
->host
));
749 mmc_claim_host(card
->host
);
752 err
= mmc_send_status(card
, &status
);
754 pr_err("%s: Get card status fail\n", mmc_hostname(card
->host
));
757 if(R1_CURRENT_STATE(status
) == R1_STATE_PRG
)
761 }while (++delay_count
< delay
);
763 switch (R1_CURRENT_STATE(status
)) {
769 * In idle and transfer states, HPI is not needed and the caller
770 * can issue the next intended command immediately
772 pr_err("[%s]: %s: card release busy status before stopping by HPI, wait %dms\n", __func__
, mmc_hostname(card
->host
), delay_count
);
775 pr_err("[%s]: %s: do HPI to stop flush ops, wait %dms.\n", __func__
, mmc_hostname(card
->host
), delay_count
);
778 /* In all other states, it's illegal to issue HPI */
779 pr_err("%s: HPI cannot be sent. Card state=%d\n",
780 mmc_hostname(card
->host
), R1_CURRENT_STATE(status
));
785 err
= mmc_send_hpi_cmd(card
, &status
);
789 prg_wait
= jiffies
+ msecs_to_jiffies(card
->ext_csd
.out_of_int_time
);
791 err
= mmc_send_status(card
, &status
);
793 if (!err
&& R1_CURRENT_STATE(status
) == R1_STATE_TRAN
)
795 if (time_after(jiffies
, prg_wait
))
800 mmc_release_host(card
->host
);
804 int mmc_interrupt_hpi(struct mmc_card
*card
)
806 return mmc_interrupt_hpi_delay(card
, 0);
810 int mmc_interrupt_hpi(struct mmc_card
*card
)
814 unsigned long prg_wait
;
818 if (!card
->ext_csd
.hpi_en
) {
819 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card
->host
));
823 mmc_claim_host(card
->host
);
824 err
= mmc_send_status(card
, &status
);
826 pr_err("%s: Get card status fail\n", mmc_hostname(card
->host
));
830 switch (R1_CURRENT_STATE(status
)) {
836 * In idle and transfer states, HPI is not needed and the caller
837 * can issue the next intended command immediately
843 /* In all other states, it's illegal to issue HPI */
844 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
845 mmc_hostname(card
->host
), R1_CURRENT_STATE(status
));
850 err
= mmc_send_hpi_cmd(card
, &status
);
854 prg_wait
= jiffies
+ msecs_to_jiffies(card
->ext_csd
.out_of_int_time
);
856 err
= mmc_send_status(card
, &status
);
858 if (!err
&& R1_CURRENT_STATE(status
) == R1_STATE_TRAN
)
860 if (time_after(jiffies
, prg_wait
))
865 mmc_release_host(card
->host
);
869 EXPORT_SYMBOL(mmc_interrupt_hpi
);
871 #ifdef MMC_ENABLED_EMPTY_QUEUE_FLUSH
873 * mmc_start_flush - start FLUSH OPS for supported cards
874 * @card: MMC card to start FLUSH OPS
875 * @form_exception: A flag to indicate if this function was
876 * called due to an exception raised by the card
878 * Start cache flush operations whenever requested.
879 * When the urgent FLUSH OPS bit is set in a R1 command response
880 * then cache flush operations should be started immediately.
882 void mmc_start_flush(struct mmc_card
*card
)
885 bool use_busy_signal
;
887 if (!card
|| mmc_card_doing_flush(card
)) {
888 printk("[%s]: !card, or it is doing flush now\n", __func__
);
892 mmc_claim_host(card
->host
);
894 if (card
->host
->flush_info
.cancel_delayed_work
) {
895 printk("[%s]: %s: cancel_delayed_work flush ops was set, exit.\n", __func__
, mmc_hostname(card
->host
));
896 card
->host
->flush_info
.cancel_delayed_work
= false;
900 if (!card
->ext_csd
.cache_ctrl
) /* No operations required */
903 //printk("[%s]: mmc_start_flush_doing.\n", __func__);
904 mmc_card_set_doing_flush(card
);
905 err
= mmc_flush_cache(card
);
907 pr_warn("%s: Error %d starting flush ops\n",
908 mmc_hostname(card
->host
), err
);
913 mmc_release_host(card
->host
);
915 EXPORT_SYMBOL(mmc_start_flush
);
918 * mmc_start_idle_time_flush() - check if a non urgent FLUSH OPs is needed
919 * @work: The idle time FLUSH OPs work
921 void mmc_start_idle_time_flush(struct work_struct
*work
)
923 struct mmc_host
*host
= container_of(work
, struct mmc_host
,
924 flush_info
.idle_time_dw
.work
);
926 printk("mmc_start_idle_time_flush...\n");
928 mmc_start_flush(host
->card
);
930 EXPORT_SYMBOL(mmc_start_idle_time_flush
);
933 * mmc_start_delayed_flush() - Start a delayed work to check for the need of non urgent FLUSH OPs
934 * @card: MMC card to start FLUSH OPS
936 void mmc_start_delayed_flush(struct mmc_card
*card
)
938 if (!card
->ext_csd
.cache_ctrl
|| mmc_card_doing_flush(card
)) {
939 printk("queueing delayed_flush_work return.\n");
943 // printk("%s: %s: queueing delayed_flush_work.\n", __func__, mmc_hostname(card->host));
946 * cancel_delayed_flush_work will prevent a race condition between
947 * fetching a request by the queue_thread and the delayed work
949 card
->host
->flush_info
.cancel_delayed_work
= false;
950 queue_delayed_work(card
->host
->flush_info
.wq
, &card
->host
->flush_info
.idle_time_dw
,
951 msecs_to_jiffies(card
->host
->flush_info
.time_to_start_flush_ms
));
953 EXPORT_SYMBOL(mmc_start_delayed_flush
);
956 * mmc_stop_flush - stop ongoing flush
957 * @card: MMC card to check flush
959 * Send HPI command to stop ongoing cache flush operations to
960 * allow rapid servicing of foreground operations, e.g. read/
961 * writes. Wait until the card comes out of the programming state
962 * to avoid errors in servicing read/write requests.
964 int mmc_stop_flush(struct mmc_card
*card
)
966 int err
= 0, count
= 0;
971 * Notify the delayed work to be cancelled, in case it was already
972 * removed from the queue, but was not started yet
974 card
->host
->flush_info
.cancel_delayed_work
= true;
976 if (delayed_work_pending(&card
->host
->flush_info
.idle_time_dw
)) {
977 cancel_delayed_work_sync(&card
->host
->flush_info
.idle_time_dw
);
980 err
= mmc_interrupt_hpi_delay(card
, MMC_CACHE_MAX_TIME_OUT
);
983 * If err is EINVAL, we can't issue an HPI.
984 * It should complete the FLUSH OPS.
986 if (!err
|| (err
== -EINVAL
)) {
987 mmc_card_clr_doing_flush(card
);
993 EXPORT_SYMBOL(mmc_stop_flush
);
997 * mmc_wait_for_cmd - start a command and wait for completion
998 * @host: MMC host to start command
999 * @cmd: MMC command to start
1000 * @retries: maximum number of retries
1002 * Start a new MMC command for a host, and wait for the command
1003 * to complete. Return any error that occurred while the command
1004 * was executing. Do not attempt to parse the response.
1006 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
1008 struct mmc_request mrq
= {NULL
};
1010 WARN_ON(!host
->claimed
);
1012 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
1013 cmd
->retries
= retries
;
1018 mmc_wait_for_req(host
, &mrq
);
1023 EXPORT_SYMBOL(mmc_wait_for_cmd
);
1026 * mmc_stop_bkops - stop ongoing BKOPS
1027 * @card: MMC card to check BKOPS
1029 * Send HPI command to stop ongoing background operations to
1030 * allow rapid servicing of foreground operations, e.g. read/
1031 * writes. Wait until the card comes out of the programming state
1032 * to avoid errors in servicing read/write requests.
1034 int mmc_stop_bkops(struct mmc_card
*card
)
1039 err
= mmc_interrupt_hpi(card
);
1042 * If err is EINVAL, we can't issue an HPI.
1043 * It should complete the BKOPS.
1045 if (!err
|| (err
== -EINVAL
)) {
1046 mmc_card_clr_doing_bkops(card
);
1052 EXPORT_SYMBOL(mmc_stop_bkops
);
1054 int mmc_read_bkops_status(struct mmc_card
*card
)
1060 * In future work, we should consider storing the entire ext_csd.
1062 ext_csd
= kmalloc(512, GFP_KERNEL
);
1064 pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
1065 mmc_hostname(card
->host
));
1069 mmc_claim_host(card
->host
);
1070 err
= mmc_send_ext_csd(card
, ext_csd
);
1071 mmc_release_host(card
->host
);
1075 card
->ext_csd
.raw_bkops_status
= ext_csd
[EXT_CSD_BKOPS_STATUS
];
1076 card
->ext_csd
.raw_exception_status
= ext_csd
[EXT_CSD_EXP_EVENTS_STATUS
];
1081 EXPORT_SYMBOL(mmc_read_bkops_status
);
1084 * mmc_set_data_timeout - set the timeout for a data command
1085 * @data: data phase for command
1086 * @card: the MMC card associated with the data transfer
1088 * Computes the data timeout parameters according to the
1089 * correct algorithm given the card type.
1091 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
1096 * SDIO cards only define an upper 1 s limit on access.
1098 if (mmc_card_sdio(card
)) {
1099 data
->timeout_ns
= 1000000000;
1100 data
->timeout_clks
= 0;
1105 * SD cards use a 100 multiplier rather than 10
1107 mult
= mmc_card_sd(card
) ? 100 : 10;
1110 * Scale up the multiplier (and therefore the timeout) by
1111 * the r2w factor for writes.
1113 if (data
->flags
& MMC_DATA_WRITE
)
1114 mult
<<= card
->csd
.r2w_factor
;
1116 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
1117 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
1120 * SD cards also have an upper limit on the timeout.
1122 if (mmc_card_sd(card
)) {
1123 unsigned int timeout_us
, limit_us
;
1125 timeout_us
= data
->timeout_ns
/ 1000;
1126 if (mmc_host_clk_rate(card
->host
))
1127 timeout_us
+= data
->timeout_clks
* 1000 /
1128 (mmc_host_clk_rate(card
->host
) / 1000);
1130 if (data
->flags
& MMC_DATA_WRITE
)
1132 * The MMC spec "It is strongly recommended
1133 * for hosts to implement more than 500ms
1134 * timeout value even if the card indicates
1135 * the 250ms maximum busy length." Even the
1136 * previous value of 300ms is known to be
1137 * insufficient for some cards.
1144 * SDHC cards always use these fixed values.
1146 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
1147 data
->timeout_ns
= limit_us
* 1000;
1148 data
->timeout_clks
= 0;
1153 * Some cards require longer data read timeout than indicated in CSD.
1154 * Address this by setting the read timeout to a "reasonably high"
1155 * value. For the cards tested, 300ms has proven enough. If necessary,
1156 * this value can be increased if other problematic cards require this.
1158 if (mmc_card_long_read_time(card
) && data
->flags
& MMC_DATA_READ
) {
1159 data
->timeout_ns
= 300000000;
1160 data
->timeout_clks
= 0;
1164 * Some cards need very high timeouts if driven in SPI mode.
1165 * The worst observed timeout was 900ms after writing a
1166 * continuous stream of data until the internal logic
1169 if (mmc_host_is_spi(card
->host
)) {
1170 if (data
->flags
& MMC_DATA_WRITE
) {
1171 if (data
->timeout_ns
< 1000000000)
1172 data
->timeout_ns
= 1000000000; /* 1s */
1174 if (data
->timeout_ns
< 100000000)
1175 data
->timeout_ns
= 100000000; /* 100ms */
1179 EXPORT_SYMBOL(mmc_set_data_timeout
);
1182 * mmc_align_data_size - pads a transfer size to a more optimal value
1183 * @card: the MMC card associated with the data transfer
1184 * @sz: original transfer size
1186 * Pads the original data size with a number of extra bytes in
1187 * order to avoid controller bugs and/or performance hits
1188 * (e.g. some controllers revert to PIO for certain sizes).
1190 * Returns the improved size, which might be unmodified.
1192 * Note that this function is only relevant when issuing a
1193 * single scatter gather entry.
1195 unsigned int mmc_align_data_size(struct mmc_card
*card
, unsigned int sz
)
1198 * FIXME: We don't have a system for the controller to tell
1199 * the core about its problems yet, so for now we just 32-bit
1202 sz
= ((sz
+ 3) / 4) * 4;
1206 EXPORT_SYMBOL(mmc_align_data_size
);
1209 * __mmc_claim_host - exclusively claim a host
1210 * @host: mmc host to claim
1211 * @abort: whether or not the operation should be aborted
1213 * Claim a host for a set of operations. If @abort is non null and
1214 * dereference a non-zero value then this will return prematurely with
1215 * that non-zero value without acquiring the lock. Returns zero
1216 * with the lock held otherwise.
1218 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
1220 DECLARE_WAITQUEUE(wait
, current
);
1221 unsigned long flags
;
1226 add_wait_queue(&host
->wq
, &wait
);
1227 spin_lock_irqsave(&host
->lock
, flags
);
1229 set_current_state(TASK_UNINTERRUPTIBLE
);
1230 stop
= abort
? atomic_read(abort
) : 0;
1231 if (stop
|| !host
->claimed
|| host
->claimer
== current
)
1233 spin_unlock_irqrestore(&host
->lock
, flags
);
1235 spin_lock_irqsave(&host
->lock
, flags
);
1237 set_current_state(TASK_RUNNING
);
1240 host
->claimer
= current
;
1241 host
->claim_cnt
+= 1;
1244 spin_unlock_irqrestore(&host
->lock
, flags
);
1245 remove_wait_queue(&host
->wq
, &wait
);
1246 if (host
->ops
->enable
&& !stop
&& host
->claim_cnt
== 1)
1247 host
->ops
->enable(host
);
1251 EXPORT_SYMBOL(__mmc_claim_host
);
1254 * mmc_try_claim_host - try exclusively to claim a host
1255 * @host: mmc host to claim
1257 * Returns %1 if the host is claimed, %0 otherwise.
1259 int mmc_try_claim_host(struct mmc_host
*host
)
1261 int claimed_host
= 0;
1262 unsigned long flags
;
1264 spin_lock_irqsave(&host
->lock
, flags
);
1265 if (!host
->claimed
|| host
->claimer
== current
) {
1267 host
->claimer
= current
;
1268 host
->claim_cnt
+= 1;
1271 spin_unlock_irqrestore(&host
->lock
, flags
);
1272 if (host
->ops
->enable
&& claimed_host
&& host
->claim_cnt
== 1)
1273 host
->ops
->enable(host
);
1274 return claimed_host
;
1276 EXPORT_SYMBOL(mmc_try_claim_host
);
1279 * mmc_release_host - release a host
1280 * @host: mmc host to release
1282 * Release a MMC host, allowing others to claim the host
1283 * for their operations.
1285 void mmc_release_host(struct mmc_host
*host
)
1287 unsigned long flags
;
1289 WARN_ON(!host
->claimed
);
1291 if (host
->ops
->disable
&& host
->claim_cnt
== 1)
1292 host
->ops
->disable(host
);
1294 spin_lock_irqsave(&host
->lock
, flags
);
1295 if (--host
->claim_cnt
) {
1296 /* Release for nested claim */
1297 spin_unlock_irqrestore(&host
->lock
, flags
);
1300 host
->claimer
= NULL
;
1301 spin_unlock_irqrestore(&host
->lock
, flags
);
1305 EXPORT_SYMBOL(mmc_release_host
);
1308 * Internal function that does the actual ios call to the host driver,
1309 * optionally printing some debug output.
1311 static inline void mmc_set_ios(struct mmc_host
*host
)
1313 struct mmc_ios
*ios
= &host
->ios
;
1315 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1316 "width %u timing %u\n",
1317 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
1318 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
1319 ios
->bus_width
, ios
->timing
);
1322 mmc_set_ungated(host
);
1323 host
->ops
->set_ios(host
, ios
);
1327 * Control chip select pin on a host.
1329 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
1331 mmc_host_clk_hold(host
);
1332 host
->ios
.chip_select
= mode
;
1334 mmc_host_clk_release(host
);
1338 * Sets the host clock to the highest possible frequency that
1341 static void __mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
1343 WARN_ON(hz
< host
->f_min
);
1345 if (hz
> host
->f_max
)
1348 host
->ios
.clock
= hz
;
1352 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
1354 mmc_host_clk_hold(host
);
1355 __mmc_set_clock(host
, hz
);
1356 mmc_host_clk_release(host
);
1359 #ifdef CONFIG_MMC_CLKGATE
1361 * This gates the clock by setting it to 0 Hz.
1363 void mmc_gate_clock(struct mmc_host
*host
)
1365 unsigned long flags
;
1367 spin_lock_irqsave(&host
->clk_lock
, flags
);
1368 host
->clk_old
= host
->ios
.clock
;
1369 host
->ios
.clock
= 0;
1370 host
->clk_gated
= true;
1371 spin_unlock_irqrestore(&host
->clk_lock
, flags
);
1376 * This restores the clock from gating by using the cached
1379 void mmc_ungate_clock(struct mmc_host
*host
)
1382 * We should previously have gated the clock, so the clock shall
1383 * be 0 here! The clock may however be 0 during initialization,
1384 * when some request operations are performed before setting
1385 * the frequency. When ungate is requested in that situation
1386 * we just ignore the call.
1388 if (host
->clk_old
) {
1389 BUG_ON(host
->ios
.clock
);
1390 /* This call will also set host->clk_gated to false */
1391 __mmc_set_clock(host
, host
->clk_old
);
1395 void mmc_set_ungated(struct mmc_host
*host
)
1397 unsigned long flags
;
1400 * We've been given a new frequency while the clock is gated,
1401 * so make sure we regard this as ungating it.
1403 spin_lock_irqsave(&host
->clk_lock
, flags
);
1404 host
->clk_gated
= false;
1405 spin_unlock_irqrestore(&host
->clk_lock
, flags
);
1409 void mmc_set_ungated(struct mmc_host
*host
)
1415 * Change the bus mode (open drain/push-pull) of a host.
1417 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
1419 mmc_host_clk_hold(host
);
1420 host
->ios
.bus_mode
= mode
;
1422 mmc_host_clk_release(host
);
1426 * Change data bus width of a host.
1428 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
1430 mmc_host_clk_hold(host
);
1431 host
->ios
.bus_width
= width
;
1433 mmc_host_clk_release(host
);
1437 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1438 * @vdd: voltage (mV)
1439 * @low_bits: prefer low bits in boundary cases
1441 * This function returns the OCR bit number according to the provided @vdd
1442 * value. If conversion is not possible a negative errno value returned.
1444 * Depending on the @low_bits flag the function prefers low or high OCR bits
1445 * on boundary voltages. For example,
1446 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1447 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1449 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1451 static int mmc_vdd_to_ocrbitnum(int vdd
, bool low_bits
)
1453 const int max_bit
= ilog2(MMC_VDD_35_36
);
1456 if (vdd
< 1650 || vdd
> 3600)
1459 if (vdd
>= 1650 && vdd
<= 1950)
1460 return ilog2(MMC_VDD_165_195
);
1465 /* Base 2000 mV, step 100 mV, bit's base 8. */
1466 bit
= (vdd
- 2000) / 100 + 8;
1473 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1474 * @vdd_min: minimum voltage value (mV)
1475 * @vdd_max: maximum voltage value (mV)
1477 * This function returns the OCR mask bits according to the provided @vdd_min
1478 * and @vdd_max values. If conversion is not possible the function returns 0.
1480 * Notes wrt boundary cases:
1481 * This function sets the OCR bits for all boundary voltages, for example
1482 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1483 * MMC_VDD_34_35 mask.
1485 u32
mmc_vddrange_to_ocrmask(int vdd_min
, int vdd_max
)
1489 if (vdd_max
< vdd_min
)
1492 /* Prefer high bits for the boundary vdd_max values. */
1493 vdd_max
= mmc_vdd_to_ocrbitnum(vdd_max
, false);
1497 /* Prefer low bits for the boundary vdd_min values. */
1498 vdd_min
= mmc_vdd_to_ocrbitnum(vdd_min
, true);
1502 /* Fill the mask, from max bit to min bit. */
1503 while (vdd_max
>= vdd_min
)
1504 mask
|= 1 << vdd_max
--;
1508 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask
);
1510 #ifdef CONFIG_REGULATOR
1513 * mmc_regulator_get_ocrmask - return mask of supported voltages
1514 * @supply: regulator to use
1516 * This returns either a negative errno, or a mask of voltages that
1517 * can be provided to MMC/SD/SDIO devices using the specified voltage
1518 * regulator. This would normally be called before registering the
1521 int mmc_regulator_get_ocrmask(struct regulator
*supply
)
1527 count
= regulator_count_voltages(supply
);
1531 for (i
= 0; i
< count
; i
++) {
1535 vdd_uV
= regulator_list_voltage(supply
, i
);
1539 vdd_mV
= vdd_uV
/ 1000;
1540 result
|= mmc_vddrange_to_ocrmask(vdd_mV
, vdd_mV
);
1545 EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask
);
1548 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1549 * @mmc: the host to regulate
1550 * @supply: regulator to use
1551 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1553 * Returns zero on success, else negative errno.
1555 * MMC host drivers may use this to enable or disable a regulator using
1556 * a particular supply voltage. This would normally be called from the
1559 int mmc_regulator_set_ocr(struct mmc_host
*mmc
,
1560 struct regulator
*supply
,
1561 unsigned short vdd_bit
)
1571 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1572 * bits this regulator doesn't quite support ... don't
1573 * be too picky, most cards and regulators are OK with
1574 * a 0.1V range goof (it's a small error percentage).
1576 tmp
= vdd_bit
- ilog2(MMC_VDD_165_195
);
1578 min_uV
= 1650 * 1000;
1579 max_uV
= 1950 * 1000;
1581 min_uV
= 1900 * 1000 + tmp
* 100 * 1000;
1582 max_uV
= min_uV
+ 100 * 1000;
1586 * If we're using a fixed/static regulator, don't call
1587 * regulator_set_voltage; it would fail.
1589 voltage
= regulator_get_voltage(supply
);
1591 if (!regulator_can_change_voltage(supply
))
1592 min_uV
= max_uV
= voltage
;
1596 else if (voltage
< min_uV
|| voltage
> max_uV
)
1597 result
= regulator_set_voltage(supply
, min_uV
, max_uV
);
1601 if (result
== 0 && !mmc
->regulator_enabled
) {
1602 result
= regulator_enable(supply
);
1604 mmc
->regulator_enabled
= true;
1606 } else if (mmc
->regulator_enabled
) {
1607 result
= regulator_disable(supply
);
1609 mmc
->regulator_enabled
= false;
1613 dev_err(mmc_dev(mmc
),
1614 "could not set regulator OCR (%d)\n", result
);
1617 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr
);
1619 int mmc_regulator_get_supply(struct mmc_host
*mmc
)
1621 struct device
*dev
= mmc_dev(mmc
);
1622 struct regulator
*supply
;
1625 supply
= devm_regulator_get(dev
, "vmmc");
1626 mmc
->supply
.vmmc
= supply
;
1627 mmc
->supply
.vqmmc
= devm_regulator_get(dev
, "vqmmc");
1630 return PTR_ERR(supply
);
1632 ret
= mmc_regulator_get_ocrmask(supply
);
1634 mmc
->ocr_avail
= ret
;
1636 dev_warn(mmc_dev(mmc
), "Failed getting OCR mask: %d\n", ret
);
1640 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply
);
1642 #endif /* CONFIG_REGULATOR */
1645 * Mask off any voltages we don't support and select
1646 * the lowest voltage
1648 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
1652 ocr
&= host
->ocr_avail
;
1660 mmc_host_clk_hold(host
);
1661 host
->ios
.vdd
= bit
;
1663 mmc_host_clk_release(host
);
1665 pr_warning("%s: host doesn't support card's voltages\n",
1666 mmc_hostname(host
));
1673 int __mmc_set_signal_voltage(struct mmc_host
*host
, int signal_voltage
)
1676 int old_signal_voltage
= host
->ios
.signal_voltage
;
1678 host
->ios
.signal_voltage
= signal_voltage
;
1679 if (host
->ops
->start_signal_voltage_switch
) {
1680 mmc_host_clk_hold(host
);
1681 err
= host
->ops
->start_signal_voltage_switch(host
, &host
->ios
);
1682 mmc_host_clk_release(host
);
1686 host
->ios
.signal_voltage
= old_signal_voltage
;
1692 int mmc_set_signal_voltage(struct mmc_host
*host
, int signal_voltage
)
1694 struct mmc_command cmd
= {0};
1701 * Send CMD11 only if the request is to switch the card to
1704 if (signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1705 return __mmc_set_signal_voltage(host
, signal_voltage
);
1708 * If we cannot switch voltages, return failure so the caller
1709 * can continue without UHS mode
1711 if (!host
->ops
->start_signal_voltage_switch
)
1713 if (!host
->ops
->card_busy
)
1714 pr_warning("%s: cannot verify signal voltage switch\n",
1715 mmc_hostname(host
));
1717 cmd
.opcode
= SD_SWITCH_VOLTAGE
;
1719 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1721 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
1725 if (!mmc_host_is_spi(host
) && (cmd
.resp
[0] & R1_ERROR
))
1728 mmc_host_clk_hold(host
);
1730 * The card should drive cmd and dat[0:3] low immediately
1731 * after the response of cmd11, but wait 1 ms to be sure
1734 if (host
->ops
->card_busy
&& !host
->ops
->card_busy(host
)) {
1739 * During a signal voltage level switch, the clock must be gated
1740 * for 5 ms according to the SD spec
1742 clock
= host
->ios
.clock
;
1743 host
->ios
.clock
= 0;
1746 if (__mmc_set_signal_voltage(host
, signal_voltage
)) {
1748 * Voltages may not have been switched, but we've already
1749 * sent CMD11, so a power cycle is required anyway
1755 /* Keep clock gated for at least 5 ms */
1757 host
->ios
.clock
= clock
;
1760 /* Wait for at least 1 ms according to spec */
1764 * Failure to switch is indicated by the card holding
1767 if (host
->ops
->card_busy
&& host
->ops
->card_busy(host
))
1772 pr_debug("%s: Signal voltage switch failed, "
1773 "power cycling card\n", mmc_hostname(host
));
1774 mmc_power_cycle(host
);
1777 mmc_host_clk_release(host
);
1783 * Select timing parameters for host.
1785 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
1787 mmc_host_clk_hold(host
);
1788 host
->ios
.timing
= timing
;
1790 mmc_host_clk_release(host
);
1794 * Select appropriate driver type for host.
1796 void mmc_set_driver_type(struct mmc_host
*host
, unsigned int drv_type
)
1798 mmc_host_clk_hold(host
);
1799 host
->ios
.drv_type
= drv_type
;
1801 mmc_host_clk_release(host
);
1805 * Apply power to the MMC stack. This is a two-stage process.
1806 * First, we enable power to the card without the clock running.
1807 * We then wait a bit for the power to stabilise. Finally,
1808 * enable the bus drivers and clock to the card.
1810 * We must _NOT_ enable the clock prior to power stablising.
1812 * If a host does all the power sequencing itself, ignore the
1813 * initial MMC_POWER_UP stage.
1815 static void mmc_power_up(struct mmc_host
*host
)
1819 mmc_host_clk_hold(host
);
1821 /* If ocr is set, we use it */
1823 bit
= ffs(host
->ocr
) - 1;
1825 bit
= fls(host
->ocr_avail
) - 1;
1827 host
->ios
.vdd
= bit
;
1828 if (mmc_host_is_spi(host
))
1829 host
->ios
.chip_select
= MMC_CS_HIGH
;
1831 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
1832 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
1833 host
->ios
.power_mode
= MMC_POWER_UP
;
1834 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
1835 host
->ios
.timing
= MMC_TIMING_LEGACY
;
1838 /* Set signal voltage to 3.3V */
1839 __mmc_set_signal_voltage(host
, MMC_SIGNAL_VOLTAGE_330
);
1842 * This delay should be sufficient to allow the power supply
1843 * to reach the minimum voltage.
1847 host
->ios
.clock
= host
->f_init
;
1849 host
->ios
.power_mode
= MMC_POWER_ON
;
1853 * This delay must be at least 74 clock sizes, or 1 ms, or the
1854 * time required to reach a stable voltage.
1858 mmc_host_clk_release(host
);
1861 void mmc_power_off(struct mmc_host
*host
)
1863 mmc_host_clk_hold(host
);
1865 host
->ios
.clock
= 0;
1868 #ifdef CONFIG_MTK_EMMC_CACHE
1869 if (host
->card
&& (mmc_card_mmc(host
->card
)) && (host
->card
->ext_csd
.cache_ctrl
& 0x1)) {
1870 if (mmc_cache_ctrl(host
, 0)) {
1871 pr_err("%s: failed to disable cache\n", mmc_hostname(host
));
1878 * Reset ocr mask to be the highest possible voltage supported for
1879 * this mmc host. This value will be used at next power up.
1881 host
->ocr
= 1 << (fls(host
->ocr_avail
) - 1);
1883 if (!mmc_host_is_spi(host
)) {
1884 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
1885 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
1887 host
->ios
.power_mode
= MMC_POWER_OFF
;
1888 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
1889 host
->ios
.timing
= MMC_TIMING_LEGACY
;
1893 * Some configurations, such as the 802.11 SDIO card in the OLPC
1894 * XO-1.5, require a short delay after poweroff before the card
1895 * can be successfully turned on again.
1899 mmc_host_clk_release(host
);
1902 void mmc_power_cycle(struct mmc_host
*host
)
1904 mmc_power_off(host
);
1905 /* Wait at least 1 ms according to SD spec */
1911 * Cleanup when the last reference to the bus operator is dropped.
1913 static void __mmc_release_bus(struct mmc_host
*host
)
1916 BUG_ON(host
->bus_refs
);
1917 BUG_ON(!host
->bus_dead
);
1919 host
->bus_ops
= NULL
;
1923 * Increase reference count of bus operator
1925 static inline void mmc_bus_get(struct mmc_host
*host
)
1927 unsigned long flags
;
1929 spin_lock_irqsave(&host
->lock
, flags
);
1931 spin_unlock_irqrestore(&host
->lock
, flags
);
1935 * Decrease reference count of bus operator and free it if
1936 * it is the last reference.
1938 static inline void mmc_bus_put(struct mmc_host
*host
)
1940 unsigned long flags
;
1942 spin_lock_irqsave(&host
->lock
, flags
);
1944 if ((host
->bus_refs
== 0) && host
->bus_ops
)
1945 __mmc_release_bus(host
);
1946 spin_unlock_irqrestore(&host
->lock
, flags
);
1949 int mmc_resume_bus(struct mmc_host
*host
)
1951 unsigned long flags
;
1953 if (!mmc_bus_needs_resume(host
))
1956 printk("%s: Starting deferred resume\n", mmc_hostname(host
));
1957 spin_lock_irqsave(&host
->lock
, flags
);
1958 host
->bus_resume_flags
&= ~MMC_BUSRESUME_NEEDS_RESUME
;
1959 host
->rescan_disable
= 0;
1960 spin_unlock_irqrestore(&host
->lock
, flags
);
1963 if (host
->bus_ops
&& !host
->bus_dead
) {
1965 BUG_ON(!host
->bus_ops
->resume
);
1966 host
->bus_ops
->resume(host
);
1969 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
1970 host
->bus_ops
->detect(host
);
1973 printk("%s: Deferred resume completed\n", mmc_hostname(host
));
1977 EXPORT_SYMBOL(mmc_resume_bus
);
1980 * Assign a mmc bus handler to a host. Only one bus handler may control a
1981 * host at any given time.
1983 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
1985 unsigned long flags
;
1990 WARN_ON(!host
->claimed
);
1992 spin_lock_irqsave(&host
->lock
, flags
);
1994 BUG_ON(host
->bus_ops
);
1995 BUG_ON(host
->bus_refs
);
1997 host
->bus_ops
= ops
;
2001 spin_unlock_irqrestore(&host
->lock
, flags
);
2005 * Remove the current bus handler from a host.
2007 void mmc_detach_bus(struct mmc_host
*host
)
2009 unsigned long flags
;
2013 WARN_ON(!host
->claimed
);
2014 WARN_ON(!host
->bus_ops
);
2016 spin_lock_irqsave(&host
->lock
, flags
);
2020 spin_unlock_irqrestore(&host
->lock
, flags
);
2026 * mmc_detect_change - process change of state on a MMC socket
2027 * @host: host which changed state.
2028 * @delay: optional delay to wait before detection (jiffies)
2030 * MMC drivers should call this when they detect a card has been
2031 * inserted or removed. The MMC layer will confirm that any
2032 * present card is still functional, and initialize any newly
2035 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
2038 #ifdef CONFIG_MMC_DEBUG
2039 unsigned long flags
;
2040 spin_lock_irqsave(&host
->lock
, flags
);
2041 WARN_ON(host
->removed
);
2042 spin_unlock_irqrestore(&host
->lock
, flags
);
2044 host
->detect_change
= 1;
2046 wake_lock(&host
->detect_wake_lock
);
2047 ret
= mmc_schedule_delayed_work(&host
->detect
, delay
);
2048 printk(KERN_INFO
"msdc: %d,mmc_schedule_delayed_work ret= %d\n",host
->index
,ret
);
2051 EXPORT_SYMBOL(mmc_detect_change
);
2053 void mmc_init_erase(struct mmc_card
*card
)
2057 if (is_power_of_2(card
->erase_size
))
2058 card
->erase_shift
= ffs(card
->erase_size
) - 1;
2060 card
->erase_shift
= 0;
2063 * It is possible to erase an arbitrarily large area of an SD or MMC
2064 * card. That is not desirable because it can take a long time
2065 * (minutes) potentially delaying more important I/O, and also the
2066 * timeout calculations become increasingly hugely over-estimated.
2067 * Consequently, 'pref_erase' is defined as a guide to limit erases
2068 * to that size and alignment.
2070 * For SD cards that define Allocation Unit size, limit erases to one
2071 * Allocation Unit at a time. For MMC cards that define High Capacity
2072 * Erase Size, whether it is switched on or not, limit to that size.
2073 * Otherwise just have a stab at a good value. For modern cards it
2074 * will end up being 4MiB. Note that if the value is too small, it
2075 * can end up taking longer to erase.
2077 if (mmc_card_sd(card
) && card
->ssr
.au
) {
2078 card
->pref_erase
= card
->ssr
.au
;
2079 card
->erase_shift
= ffs(card
->ssr
.au
) - 1;
2080 } else if (card
->ext_csd
.hc_erase_size
) {
2081 card
->pref_erase
= card
->ext_csd
.hc_erase_size
;
2083 sz
= (card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9)) >> 11;
2085 card
->pref_erase
= 512 * 1024 / 512;
2087 card
->pref_erase
= 1024 * 1024 / 512;
2089 card
->pref_erase
= 2 * 1024 * 1024 / 512;
2091 card
->pref_erase
= 4 * 1024 * 1024 / 512;
2092 if (card
->pref_erase
< card
->erase_size
)
2093 card
->pref_erase
= card
->erase_size
;
2095 sz
= card
->pref_erase
% card
->erase_size
;
2097 card
->pref_erase
+= card
->erase_size
- sz
;
2102 static unsigned int mmc_mmc_erase_timeout(struct mmc_card
*card
,
2103 unsigned int arg
, unsigned int qty
)
2105 unsigned int erase_timeout
;
2107 if (arg
== MMC_DISCARD_ARG
||
2108 (arg
== MMC_TRIM_ARG
&& card
->ext_csd
.rev
>= 6)) {
2109 erase_timeout
= card
->ext_csd
.trim_timeout
;
2110 } else if (card
->ext_csd
.erase_group_def
& 1) {
2111 /* High Capacity Erase Group Size uses HC timeouts */
2112 if (arg
== MMC_TRIM_ARG
)
2113 erase_timeout
= card
->ext_csd
.trim_timeout
;
2115 erase_timeout
= card
->ext_csd
.hc_erase_timeout
;
2117 /* CSD Erase Group Size uses write timeout */
2118 unsigned int mult
= (10 << card
->csd
.r2w_factor
);
2119 unsigned int timeout_clks
= card
->csd
.tacc_clks
* mult
;
2120 unsigned int timeout_us
;
2122 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
2123 if (card
->csd
.tacc_ns
< 1000000)
2124 timeout_us
= (card
->csd
.tacc_ns
* mult
) / 1000;
2126 timeout_us
= (card
->csd
.tacc_ns
/ 1000) * mult
;
2129 * ios.clock is only a target. The real clock rate might be
2130 * less but not that much less, so fudge it by multiplying by 2.
2133 timeout_us
+= (timeout_clks
* 1000) /
2134 (mmc_host_clk_rate(card
->host
) / 1000);
2136 erase_timeout
= timeout_us
/ 1000;
2139 * Theoretically, the calculation could underflow so round up
2140 * to 1ms in that case.
2146 /* Multiplier for secure operations */
2147 if (arg
& MMC_SECURE_ARGS
) {
2148 if (arg
== MMC_SECURE_ERASE_ARG
)
2149 erase_timeout
*= card
->ext_csd
.sec_erase_mult
;
2151 erase_timeout
*= card
->ext_csd
.sec_trim_mult
;
2154 erase_timeout
*= qty
;
2157 * Ensure at least a 1 second timeout for SPI as per
2158 * 'mmc_set_data_timeout()'
2160 if (mmc_host_is_spi(card
->host
) && erase_timeout
< 1000)
2161 erase_timeout
= 1000;
2163 return erase_timeout
;
2166 static unsigned int mmc_sd_erase_timeout(struct mmc_card
*card
,
2170 unsigned int erase_timeout
;
2172 if (card
->ssr
.erase_timeout
) {
2173 /* Erase timeout specified in SD Status Register (SSR) */
2174 erase_timeout
= card
->ssr
.erase_timeout
* qty
+
2175 card
->ssr
.erase_offset
;
2178 * Erase timeout not specified in SD Status Register (SSR) so
2179 * use 250ms per write block.
2181 erase_timeout
= 250 * qty
;
2184 /* Must not be less than 1 second */
2185 if (erase_timeout
< 1000)
2186 erase_timeout
= 1000;
2188 return erase_timeout
;
2191 static unsigned int mmc_erase_timeout(struct mmc_card
*card
,
2195 if (mmc_card_sd(card
))
2196 return mmc_sd_erase_timeout(card
, arg
, qty
);
2198 return mmc_mmc_erase_timeout(card
, arg
, qty
);
2201 static int mmc_do_erase(struct mmc_card
*card
, unsigned int from
,
2202 unsigned int to
, unsigned int arg
)
2204 struct mmc_command cmd
= {0};
2205 unsigned int qty
= 0;
2206 unsigned long timeout
;
2207 unsigned int fr
, nr
;
2212 trace_mmc_blk_erase_start(arg
, fr
, nr
);
2215 * qty is used to calculate the erase timeout which depends on how many
2216 * erase groups (or allocation units in SD terminology) are affected.
2217 * We count erasing part of an erase group as one erase group.
2218 * For SD, the allocation units are always a power of 2. For MMC, the
2219 * erase group size is almost certainly also power of 2, but it does not
2220 * seem to insist on that in the JEDEC standard, so we fall back to
2221 * division in that case. SD may not specify an allocation unit size,
2222 * in which case the timeout is based on the number of write blocks.
2224 * Note that the timeout for secure trim 2 will only be correct if the
2225 * number of erase groups specified is the same as the total of all
2226 * preceding secure trim 1 commands. Since the power may have been
2227 * lost since the secure trim 1 commands occurred, it is generally
2228 * impossible to calculate the secure trim 2 timeout correctly.
2230 if (card
->erase_shift
)
2231 qty
+= ((to
>> card
->erase_shift
) -
2232 (from
>> card
->erase_shift
)) + 1;
2233 else if (mmc_card_sd(card
))
2234 qty
+= to
- from
+ 1;
2236 qty
+= ((to
/ card
->erase_size
) -
2237 (from
/ card
->erase_size
)) + 1;
2239 if (!mmc_card_blockaddr(card
)) {
2244 if (mmc_card_sd(card
))
2245 cmd
.opcode
= SD_ERASE_WR_BLK_START
;
2247 cmd
.opcode
= MMC_ERASE_GROUP_START
;
2249 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2250 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2252 pr_err("mmc_erase: group start error %d, "
2253 "status %#x\n", err
, cmd
.resp
[0]);
2258 memset(&cmd
, 0, sizeof(struct mmc_command
));
2259 if (mmc_card_sd(card
))
2260 cmd
.opcode
= SD_ERASE_WR_BLK_END
;
2262 cmd
.opcode
= MMC_ERASE_GROUP_END
;
2264 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2265 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2267 pr_err("mmc_erase: group end error %d, status %#x\n",
2273 memset(&cmd
, 0, sizeof(struct mmc_command
));
2274 cmd
.opcode
= MMC_ERASE
;
2276 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
2277 cmd
.cmd_timeout_ms
= mmc_erase_timeout(card
, arg
, qty
);
2278 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2280 pr_err("mmc_erase: erase error %d, status %#x\n",
2286 if (mmc_host_is_spi(card
->host
))
2289 timeout
= jiffies
+ msecs_to_jiffies(MMC_CORE_TIMEOUT_MS
);
2291 memset(&cmd
, 0, sizeof(struct mmc_command
));
2292 cmd
.opcode
= MMC_SEND_STATUS
;
2293 cmd
.arg
= card
->rca
<< 16;
2294 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
2295 /* Do not retry else we can't see errors */
2296 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2297 if (err
|| (cmd
.resp
[0] & 0xFDF92000)) {
2298 pr_err("error %d requesting status %#x\n",
2304 /* Timeout if the device never becomes ready for data and
2305 * never leaves the program state.
2307 if (time_after(jiffies
, timeout
)) {
2308 pr_err("%s: Card stuck in programming state! %s\n",
2309 mmc_hostname(card
->host
), __func__
);
2314 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
) ||
2315 (R1_CURRENT_STATE(cmd
.resp
[0]) == R1_STATE_PRG
));
2318 trace_mmc_blk_erase_end(arg
, fr
, nr
);
2323 * mmc_erase - erase sectors.
2324 * @card: card to erase
2325 * @from: first sector to erase
2326 * @nr: number of sectors to erase
2327 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2329 * Caller must claim host before calling this function.
2331 int mmc_erase(struct mmc_card
*card
, unsigned int from
, unsigned int nr
,
2334 unsigned int rem
, to
= from
+ nr
;
2336 if (!(card
->host
->caps
& MMC_CAP_ERASE
) ||
2337 !(card
->csd
.cmdclass
& CCC_ERASE
))
2340 if (!card
->erase_size
)
2343 if (mmc_card_sd(card
) && arg
!= MMC_ERASE_ARG
)
2346 if ((arg
& MMC_SECURE_ARGS
) &&
2347 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
))
2350 if ((arg
& MMC_TRIM_ARGS
) &&
2351 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
))
2354 if (arg
== MMC_SECURE_ERASE_ARG
) {
2355 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
2359 if (arg
== MMC_ERASE_ARG
) {
2360 rem
= from
% card
->erase_size
;
2362 rem
= card
->erase_size
- rem
;
2369 rem
= nr
% card
->erase_size
;
2382 /* 'from' and 'to' are inclusive */
2385 return mmc_do_erase(card
, from
, to
, arg
);
2387 EXPORT_SYMBOL(mmc_erase
);
2389 int mmc_can_erase(struct mmc_card
*card
)
2391 if ((card
->host
->caps
& MMC_CAP_ERASE
) &&
2392 (card
->csd
.cmdclass
& CCC_ERASE
) && card
->erase_size
)
2396 EXPORT_SYMBOL(mmc_can_erase
);
2398 int mmc_can_trim(struct mmc_card
*card
)
2400 if ((card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
) &&
2401 !(card
->quirks
& MMC_QUIRK_TRIM_UNSTABLE
) &&
2402 !(card
->quirks
& MMC_QUIRK_KSI_V03_SKIP_TRIM
))
2404 //printk(KERN_ERR "[%s]: quirks=0x%x, MMC_QUIRK_TRIM_UNSTABLE=0x%x\n", __func__, card->quirks, MMC_QUIRK_TRIM_UNSTABLE);
2405 //printk(KERN_ERR "[%s]: quirks=0x%x, MMC_QUIRK_KSI_V03_SKIP_TRIM=0x%x\n", __func__, card->quirks, MMC_QUIRK_KSI_V03_SKIP_TRIM);
2408 EXPORT_SYMBOL(mmc_can_trim
);
2410 int mmc_can_discard(struct mmc_card
*card
)
2413 * As there's no way to detect the discard support bit at v4.5
2414 * use the s/w feature support filed.
2416 if (card
->ext_csd
.feature_support
& MMC_DISCARD_FEATURE
)
2420 EXPORT_SYMBOL(mmc_can_discard
);
2422 int mmc_can_sanitize(struct mmc_card
*card
)
2424 if (!mmc_can_trim(card
) && !mmc_can_erase(card
))
2426 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_SANITIZE
)
2430 EXPORT_SYMBOL(mmc_can_sanitize
);
2432 int mmc_can_secure_erase_trim(struct mmc_card
*card
)
2434 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
)
2438 EXPORT_SYMBOL(mmc_can_secure_erase_trim
);
2440 int mmc_erase_group_aligned(struct mmc_card
*card
, unsigned int from
,
2443 if (!card
->erase_size
)
2445 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
2449 EXPORT_SYMBOL(mmc_erase_group_aligned
);
2451 static unsigned int mmc_do_calc_max_discard(struct mmc_card
*card
,
2454 struct mmc_host
*host
= card
->host
;
2455 unsigned int max_discard
, x
, y
, qty
= 0, max_qty
, timeout
;
2456 unsigned int last_timeout
= 0;
2458 if (card
->erase_shift
)
2459 max_qty
= UINT_MAX
>> card
->erase_shift
;
2460 else if (mmc_card_sd(card
))
2463 max_qty
= UINT_MAX
/ card
->erase_size
;
2465 /* Find the largest qty with an OK timeout */
2468 for (x
= 1; x
&& x
<= max_qty
&& max_qty
- x
>= qty
; x
<<= 1) {
2469 timeout
= mmc_erase_timeout(card
, arg
, qty
+ x
);
2470 if (timeout
> host
->max_discard_to
)
2472 if (timeout
< last_timeout
)
2474 last_timeout
= timeout
;
2486 /* Convert qty to sectors */
2487 if (card
->erase_shift
)
2488 max_discard
= --qty
<< card
->erase_shift
;
2489 else if (mmc_card_sd(card
))
2492 max_discard
= --qty
* card
->erase_size
;
2497 unsigned int mmc_calc_max_discard(struct mmc_card
*card
)
2499 struct mmc_host
*host
= card
->host
;
2500 unsigned int max_discard
, max_trim
;
2502 if (!host
->max_discard_to
)
2506 * Without erase_group_def set, MMC erase timeout depends on clock
2507 * frequence which can change. In that case, the best choice is
2508 * just the preferred erase size.
2510 if (mmc_card_mmc(card
) && !(card
->ext_csd
.erase_group_def
& 1))
2511 return card
->pref_erase
;
2513 max_discard
= mmc_do_calc_max_discard(card
, MMC_ERASE_ARG
);
2514 if (mmc_can_trim(card
)) {
2515 max_trim
= mmc_do_calc_max_discard(card
, MMC_TRIM_ARG
);
2516 if (max_trim
< max_discard
)
2517 max_discard
= max_trim
;
2518 } else if (max_discard
< card
->erase_size
) {
2521 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2522 mmc_hostname(host
), max_discard
, host
->max_discard_to
);
2525 EXPORT_SYMBOL(mmc_calc_max_discard
);
2527 int mmc_set_blocklen(struct mmc_card
*card
, unsigned int blocklen
)
2529 struct mmc_command cmd
= {0};
2531 if (mmc_card_blockaddr(card
) || mmc_card_ddr_mode(card
))
2534 cmd
.opcode
= MMC_SET_BLOCKLEN
;
2536 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2537 return mmc_wait_for_cmd(card
->host
, &cmd
, 5);
2539 EXPORT_SYMBOL(mmc_set_blocklen
);
2541 int mmc_set_blockcount(struct mmc_card
*card
, unsigned int blockcount
,
2544 struct mmc_command cmd
= {0};
2546 cmd
.opcode
= MMC_SET_BLOCK_COUNT
;
2547 cmd
.arg
= blockcount
& 0x0000FFFF;
2550 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2551 return mmc_wait_for_cmd(card
->host
, &cmd
, 5);
2553 EXPORT_SYMBOL(mmc_set_blockcount
);
2555 static void mmc_hw_reset_for_init(struct mmc_host
*host
)
2557 if (!(host
->caps
& MMC_CAP_HW_RESET
) || !host
->ops
->hw_reset
)
2559 mmc_host_clk_hold(host
);
2560 host
->ops
->hw_reset(host
);
2561 mmc_host_clk_release(host
);
2564 int mmc_can_reset(struct mmc_card
*card
)
2568 if (!mmc_card_mmc(card
))
2570 rst_n_function
= card
->ext_csd
.rst_n_function
;
2571 if ((rst_n_function
& EXT_CSD_RST_N_EN_MASK
) != EXT_CSD_RST_N_ENABLED
)
2575 EXPORT_SYMBOL(mmc_can_reset
);
2577 static int mmc_do_hw_reset(struct mmc_host
*host
, int check
)
2579 struct mmc_card
*card
= host
->card
;
2581 if (!host
->bus_ops
->power_restore
)
2584 if (!(host
->caps
& MMC_CAP_HW_RESET
) || !host
->ops
->hw_reset
)
2590 if (!mmc_can_reset(card
))
2593 mmc_host_clk_hold(host
);
2594 mmc_set_clock(host
, host
->f_init
);
2596 host
->ops
->hw_reset(host
);
2598 /* If the reset has happened, then a status command will fail */
2600 struct mmc_command cmd
= {0};
2603 cmd
.opcode
= MMC_SEND_STATUS
;
2604 if (!mmc_host_is_spi(card
->host
))
2605 cmd
.arg
= card
->rca
<< 16;
2606 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
2607 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2609 mmc_host_clk_release(host
);
2614 host
->card
->state
&= ~(MMC_STATE_HIGHSPEED
| MMC_STATE_HIGHSPEED_DDR
);
2615 if (mmc_host_is_spi(host
)) {
2616 host
->ios
.chip_select
= MMC_CS_HIGH
;
2617 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
2619 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
2620 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
2622 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
2623 host
->ios
.timing
= MMC_TIMING_LEGACY
;
2626 mmc_host_clk_release(host
);
2628 return host
->bus_ops
->power_restore(host
);
2631 int mmc_hw_reset(struct mmc_host
*host
)
2633 return mmc_do_hw_reset(host
, 0);
2635 EXPORT_SYMBOL(mmc_hw_reset
);
2637 int mmc_hw_reset_check(struct mmc_host
*host
)
2639 return mmc_do_hw_reset(host
, 1);
2641 EXPORT_SYMBOL(mmc_hw_reset_check
);
2643 static int mmc_rescan_try_freq(struct mmc_host
*host
, unsigned freq
)
2645 host
->f_init
= freq
;
2647 #ifdef CONFIG_MMC_DEBUG
2648 pr_info("%s: %s: trying to init card at %u Hz\n",
2649 mmc_hostname(host
), __func__
, host
->f_init
);
2654 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2655 * do a hardware reset if possible.
2657 mmc_hw_reset_for_init(host
);
2660 * sdio_reset sends CMD52 to reset card. Since we do not know
2661 * if the card is being re-initialized, just send it. CMD52
2662 * should be ignored by SD/eMMC cards.
2667 mmc_send_if_cond(host
, host
->ocr_avail
);
2669 /* Order's important: probe SDIO, then SD, then MMC */
2670 if (!mmc_attach_sdio(host
))
2672 if (!mmc_attach_sd(host
))
2674 if (!mmc_attach_mmc(host
))
2677 mmc_power_off(host
);
2681 int _mmc_detect_card_removed(struct mmc_host
*host
)
2685 if ((host
->caps
& MMC_CAP_NONREMOVABLE
) || !host
->bus_ops
->alive
)
2688 if (!host
->card
|| mmc_card_removed(host
->card
))
2691 ret
= host
->bus_ops
->alive(host
);
2694 * Card detect status and alive check may be out of sync if card is
2695 * removed slowly, when card detect switch changes while card/slot
2696 * pads are still contacted in hardware (refer to "SD Card Mechanical
2697 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2698 * detect work 200ms later for this case.
2700 if (!ret
&& host
->ops
->get_cd
&& !host
->ops
->get_cd(host
)) {
2701 mmc_detect_change(host
, msecs_to_jiffies(200));
2702 pr_debug("%s: card removed too slowly\n", mmc_hostname(host
));
2706 mmc_card_set_removed(host
->card
);
2707 pr_debug("%s: card remove detected\n", mmc_hostname(host
));
2713 int mmc_detect_card_removed(struct mmc_host
*host
)
2715 struct mmc_card
*card
= host
->card
;
2718 WARN_ON(!host
->claimed
);
2723 ret
= mmc_card_removed(card
);
2725 * The card will be considered unchanged unless we have been asked to
2726 * detect a change or host requires polling to provide card detection.
2728 if (!host
->detect_change
&& !(host
->caps
& MMC_CAP_NEEDS_POLL
) &&
2729 !(host
->caps2
& MMC_CAP2_DETECT_ON_ERR
))
2732 host
->detect_change
= 0;
2734 ret
= _mmc_detect_card_removed(host
);
2735 if (ret
&& (host
->caps2
& MMC_CAP2_DETECT_ON_ERR
)) {
2737 * Schedule a detect work as soon as possible to let a
2738 * rescan handle the card removal.
2740 cancel_delayed_work(&host
->detect
);
2741 mmc_detect_change(host
, 0);
2747 EXPORT_SYMBOL(mmc_detect_card_removed
);
2749 void mmc_rescan(struct work_struct
*work
)
2751 struct mmc_host
*host
=
2752 container_of(work
, struct mmc_host
, detect
.work
);
2754 bool extend_wakelock
= false;
2756 if (host
->rescan_disable
)
2759 /* If there is a non-removable card registered, only scan once */
2760 // [FIXME] VIA marks it wrong
2761 /* if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered){
2762 if (extend_wakelock)
2763 wake_lock_timeout(&host->detect_wake_lock, HZ / 2);
2765 wake_unlock(&host->detect_wake_lock);
2773 * if there is a _removable_ card registered, check whether it is
2776 if (host
->bus_ops
&& host
->bus_ops
->detect
&& !host
->bus_dead
2777 && !(host
->caps
& MMC_CAP_NONREMOVABLE
))
2778 host
->bus_ops
->detect(host
);
2780 host
->detect_change
= 0;
2782 /* If the card was removed the bus will be marked
2783 * as dead - extend the wakelock so userspace
2786 extend_wakelock
= 1;
2789 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2790 * the card is no longer present.
2795 /* if there still is a card present, stop here */
2796 if (host
->bus_ops
!= NULL
) {
2802 * Only we can add a new handler, so it's safe to
2803 * release the lock here.
2807 if (host
->ops
->get_cd
&& host
->ops
->get_cd(host
) == 0) {
2808 mmc_claim_host(host
);
2809 mmc_power_off(host
);
2810 mmc_release_host(host
);
2814 mmc_claim_host(host
);
2815 for (i
= 0; i
< ARRAY_SIZE(freqs
); i
++) {
2816 if (!mmc_rescan_try_freq(host
, max(freqs
[i
], host
->f_min
))) {
2817 extend_wakelock
= true;
2820 if (freqs
[i
] <= host
->f_min
)
2823 mmc_release_host(host
);
2824 host
->rescan_entered
= 1;
2827 if (extend_wakelock
)
2828 wake_lock_timeout(&host
->detect_wake_lock
, HZ
/ 2);
2830 wake_unlock(&host
->detect_wake_lock
);
2831 if (host
->caps
& MMC_CAP_NEEDS_POLL
) {
2832 wake_lock(&host
->detect_wake_lock
);
2833 mmc_schedule_delayed_work(&host
->detect
, HZ
);
2837 void mmc_start_host(struct mmc_host
*host
)
2839 host
->f_init
= max(freqs
[0], host
->f_min
);
2840 host
->rescan_disable
= 0;
2841 if (host
->caps2
& MMC_CAP2_NO_PRESCAN_POWERUP
)
2842 mmc_power_off(host
);
2845 mmc_detect_change(host
, 0);
2848 void mmc_stop_host(struct mmc_host
*host
)
2850 #ifdef CONFIG_MMC_DEBUG
2851 unsigned long flags
;
2852 spin_lock_irqsave(&host
->lock
, flags
);
2854 spin_unlock_irqrestore(&host
->lock
, flags
);
2857 host
->rescan_disable
= 1;
2858 if (cancel_delayed_work_sync(&host
->detect
))
2859 wake_unlock(&host
->detect_wake_lock
);
2860 mmc_flush_scheduled_work();
2862 /* clear pm flags now and let card drivers set them as needed */
2866 if (host
->bus_ops
&& !host
->bus_dead
) {
2867 /* Calling bus_ops->remove() with a claimed host can deadlock */
2868 if (host
->bus_ops
->remove
)
2869 host
->bus_ops
->remove(host
);
2871 mmc_claim_host(host
);
2872 mmc_detach_bus(host
);
2873 mmc_power_off(host
);
2874 mmc_release_host(host
);
2882 mmc_power_off(host
);
2885 int mmc_power_save_host(struct mmc_host
*host
)
2889 #ifdef CONFIG_MMC_DEBUG
2890 pr_info("%s: %s: powering down\n", mmc_hostname(host
), __func__
);
2895 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
2900 if (host
->bus_ops
->power_save
)
2901 ret
= host
->bus_ops
->power_save(host
);
2905 mmc_power_off(host
);
2909 EXPORT_SYMBOL(mmc_power_save_host
);
2911 int mmc_power_restore_host(struct mmc_host
*host
)
2915 #ifdef CONFIG_MMC_DEBUG
2916 pr_info("%s: %s: powering up\n", mmc_hostname(host
), __func__
);
2921 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
2927 ret
= host
->bus_ops
->power_restore(host
);
2933 EXPORT_SYMBOL(mmc_power_restore_host
);
2935 int mmc_card_awake(struct mmc_host
*host
)
2939 if (host
->caps2
& MMC_CAP2_NO_SLEEP_CMD
)
2944 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->awake
)
2945 err
= host
->bus_ops
->awake(host
);
2951 EXPORT_SYMBOL(mmc_card_awake
);
2953 int mmc_card_sleep(struct mmc_host
*host
)
2957 if (host
->caps2
& MMC_CAP2_NO_SLEEP_CMD
)
2962 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->sleep
)
2963 err
= host
->bus_ops
->sleep(host
);
2969 EXPORT_SYMBOL(mmc_card_sleep
);
2971 int mmc_card_can_sleep(struct mmc_host
*host
)
2973 struct mmc_card
*card
= host
->card
;
2975 if (card
&& mmc_card_mmc(card
) && card
->ext_csd
.rev
>= 3)
2979 EXPORT_SYMBOL(mmc_card_can_sleep
);
2982 * Flush the cache to the non-volatile storage.
2984 int mmc_flush_cache(struct mmc_card
*card
)
2986 struct mmc_host
*host
= card
->host
;
2989 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
))
2992 if (mmc_card_mmc(card
) &&
2993 (card
->ext_csd
.cache_size
> 0) &&
2994 (card
->ext_csd
.cache_ctrl
& 1)) {
2995 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
2996 EXT_CSD_FLUSH_CACHE
, 1, 0);
2998 pr_err("%s: cache flush error %d\n",
2999 mmc_hostname(card
->host
), err
);
3004 EXPORT_SYMBOL(mmc_flush_cache
);
3007 * Turn the cache ON/OFF.
3008 * Turning the cache OFF shall trigger flushing of the data
3009 * to the non-volatile storage.
3010 * This function should be called with host claimed
3012 int mmc_cache_ctrl(struct mmc_host
*host
, u8 enable
)
3014 struct mmc_card
*card
= host
->card
;
3015 unsigned int timeout
;
3018 #ifdef CONFIG_MTK_EMMC_CACHE
3019 //printk("[%s]: enable=%d, caps=0x%x, CACHE_FLAG=0x%x, quirks=0x%x, CACHE_QUIRK=0x%x\n", __func__, enable, host->caps2, MMC_CAP2_CACHE_CTRL, card->quirks, MMC_QUIRK_DISABLE_CACHE);
3020 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
) ||
3021 mmc_card_is_removable(host
) ||
3022 (card
->quirks
& MMC_QUIRK_DISABLE_CACHE
))
3025 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
) ||
3026 mmc_card_is_removable(host
))
3030 if (card
&& mmc_card_mmc(card
) &&
3031 (card
->ext_csd
.cache_size
> 0)) {
3034 if (card
->ext_csd
.cache_ctrl
^ enable
) {
3035 timeout
= enable
? card
->ext_csd
.generic_cmd6_time
: 0;
3036 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
3037 EXT_CSD_CACHE_CTRL
, enable
, timeout
);
3039 pr_err("%s: cache %s error %d\n",
3040 mmc_hostname(card
->host
),
3041 enable
? "on" : "off",
3044 card
->ext_csd
.cache_ctrl
= enable
;
3050 EXPORT_SYMBOL(mmc_cache_ctrl
);
3055 * mmc_suspend_host - suspend a host
3058 int mmc_suspend_host(struct mmc_host
*host
)
3062 if (mmc_bus_needs_resume(host
))
3065 if (cancel_delayed_work(&host
->detect
))
3066 wake_unlock(&host
->detect_wake_lock
);
3067 mmc_flush_scheduled_work();
3070 if (host
->bus_ops
&& !host
->bus_dead
) {
3071 if (host
->bus_ops
->suspend
) {
3072 if (mmc_card_doing_bkops(host
->card
)) {
3073 err
= mmc_stop_bkops(host
->card
);
3077 err
= host
->bus_ops
->suspend(host
);
3080 if (err
== -ENOSYS
|| !host
->bus_ops
->resume
) {
3082 * We simply "remove" the card in this case.
3083 * It will be redetected on resume. (Calling
3084 * bus_ops->remove() with a claimed host can
3087 if (host
->bus_ops
->remove
)
3088 host
->bus_ops
->remove(host
);
3089 mmc_claim_host(host
);
3090 mmc_detach_bus(host
);
3091 mmc_power_off(host
);
3092 mmc_release_host(host
);
3099 if (!err
&& !mmc_card_keep_power(host
))
3100 mmc_power_off(host
);
3106 EXPORT_SYMBOL(mmc_suspend_host
);
3109 * mmc_resume_host - resume a previously suspended host
3112 int mmc_resume_host(struct mmc_host
*host
)
3117 if (mmc_bus_manual_resume(host
)) {
3118 host
->bus_resume_flags
|= MMC_BUSRESUME_NEEDS_RESUME
;
3123 if (host
->bus_ops
&& !host
->bus_dead
) {
3124 if (!mmc_card_keep_power(host
)) {
3126 mmc_select_voltage(host
, host
->ocr
);
3128 * Tell runtime PM core we just powered up the card,
3129 * since it still believes the card is powered off.
3130 * Note that currently runtime PM is only enabled
3131 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
3133 if (mmc_card_sdio(host
->card
) &&
3134 (host
->caps
& MMC_CAP_POWER_OFF_CARD
)) {
3135 pm_runtime_disable(&host
->card
->dev
);
3136 pm_runtime_set_active(&host
->card
->dev
);
3137 pm_runtime_enable(&host
->card
->dev
);
3140 BUG_ON(!host
->bus_ops
->resume
);
3141 err
= host
->bus_ops
->resume(host
);
3143 pr_warning("%s: error %d during resume "
3144 "(card was removed?)\n",
3145 mmc_hostname(host
), err
);
3147 mmc_card_set_removed(host
->card
);
3148 pr_warning("%s: card resume fail and remove\n", mmc_hostname(host
));
3153 host
->pm_flags
&= ~MMC_PM_KEEP_POWER
;
3158 EXPORT_SYMBOL(mmc_resume_host
);
3160 /* Do the card removal on suspend if card is assumed removeable
3161 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
3164 int mmc_pm_notify(struct notifier_block
*notify_block
,
3165 unsigned long mode
, void *unused
)
3167 struct mmc_host
*host
= container_of(
3168 notify_block
, struct mmc_host
, pm_notify
);
3169 unsigned long flags
;
3171 #ifdef CONFIG_MTK_HIBERNATION
3172 unsigned long wait_time
= 0;
3176 case PM_HIBERNATION_PREPARE
:
3177 case PM_SUSPEND_PREPARE
:
3178 if (host
->card
&& mmc_card_mmc(host
->card
) &&
3179 mmc_card_doing_bkops(host
->card
)) {
3180 err
= mmc_stop_bkops(host
->card
);
3182 pr_err("%s: didn't stop bkops\n",
3183 mmc_hostname(host
));
3186 mmc_card_clr_doing_bkops(host
->card
);
3189 spin_lock_irqsave(&host
->lock
, flags
);
3190 if (mmc_bus_needs_resume(host
)) {
3191 spin_unlock_irqrestore(&host
->lock
, flags
);
3194 host
->rescan_disable
= 1;
3195 spin_unlock_irqrestore(&host
->lock
, flags
);
3196 if (cancel_delayed_work_sync(&host
->detect
))
3197 wake_unlock(&host
->detect_wake_lock
);
3199 if (!host
->bus_ops
|| host
->bus_ops
->suspend
)
3202 /* Calling bus_ops->remove() with a claimed host can deadlock */
3203 if (host
->bus_ops
->remove
)
3204 host
->bus_ops
->remove(host
);
3206 mmc_claim_host(host
);
3207 mmc_detach_bus(host
);
3208 mmc_power_off(host
);
3209 mmc_release_host(host
);
3213 #ifdef CONFIG_MTK_HIBERNATION
3214 case PM_RESTORE_PREPARE
:
3215 /* For hibernation boot-up, mmc rescan job MUST finish before entering hiberation restore flow.
3216 Or mmc rescan may call submit_bio(), which will induce BUG_ON() in submit_bio() !!
3218 if (!host
->card
|| !mmc_card_present(host
->card
)) {
3219 pr_warn("[%s] %s card is not present.\n", __func__
, mmc_hostname(host
));
3222 while (wait_time
< MMC_PM_RESTORE_WAIT_MS
) {
3223 if (host
->rescan_disable
|| host
->rescan_entered
) {
3224 pr_warn("[%s] %s (%d/%d) rescan done.\n", __func__
,
3225 mmc_hostname(host
), host
->rescan_disable
, host
->rescan_entered
);
3231 if (unlikely(wait_time
>= MMC_PM_RESTORE_WAIT_MS
)) {
3232 pr_warn("[%s] %s (%d/%d) rescan timeout !!\n", __func__
,
3233 mmc_hostname(host
), host
->rescan_disable
, host
->rescan_entered
);
3234 return notifier_from_errno(-EIO
);
3238 #endif /* CONFIG_MTK_HIBERNATION */
3240 case PM_POST_SUSPEND
:
3241 case PM_POST_HIBERNATION
:
3242 case PM_POST_RESTORE
:
3244 spin_lock_irqsave(&host
->lock
, flags
);
3245 if (mmc_bus_manual_resume(host
)) {
3246 spin_unlock_irqrestore(&host
->lock
, flags
);
3249 host
->rescan_disable
= 0;
3250 spin_unlock_irqrestore(&host
->lock
, flags
);
3251 mmc_detect_change(host
, 0);
3260 * mmc_init_context_info() - init synchronization context
3263 * Init struct context_info needed to implement asynchronous
3264 * request mechanism, used by mmc core, host driver and mmc requests
3267 void mmc_init_context_info(struct mmc_host
*host
)
3269 spin_lock_init(&host
->context_info
.lock
);
3270 host
->context_info
.is_new_req
= false;
3271 host
->context_info
.is_done_rcv
= false;
3272 host
->context_info
.is_waiting_last_req
= false;
3273 init_waitqueue_head(&host
->context_info
.wait
);
3276 #ifdef CONFIG_MMC_EMBEDDED_SDIO
3277 void mmc_set_embedded_sdio_data(struct mmc_host
*host
,
3278 struct sdio_cis
*cis
,
3279 struct sdio_cccr
*cccr
,
3280 struct sdio_embedded_func
*funcs
,
3283 host
->embedded_sdio_data
.cis
= cis
;
3284 host
->embedded_sdio_data
.cccr
= cccr
;
3285 host
->embedded_sdio_data
.funcs
= funcs
;
3286 host
->embedded_sdio_data
.num_funcs
= num_funcs
;
3289 EXPORT_SYMBOL(mmc_set_embedded_sdio_data
);
3292 static int __init
mmc_init(void)
3296 workqueue
= alloc_ordered_workqueue("kmmcd", 0);
3300 ret
= mmc_register_bus();
3302 goto destroy_workqueue
;
3304 ret
= mmc_register_host_class();
3306 goto unregister_bus
;
3308 ret
= sdio_register_bus();
3310 goto unregister_host_class
;
3314 unregister_host_class
:
3315 mmc_unregister_host_class();
3317 mmc_unregister_bus();
3319 destroy_workqueue(workqueue
);
3324 static void __exit
mmc_exit(void)
3326 sdio_unregister_bus();
3327 mmc_unregister_host_class();
3328 mmc_unregister_bus();
3329 destroy_workqueue(workqueue
);
3332 subsys_initcall(mmc_init
);
3333 module_exit(mmc_exit
);
3335 MODULE_LICENSE("GPL");