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 struct mmc_context_info
*context_info
= &mrq
->host
->context_info
;
357 context_info
->is_done_rcv
= true;
358 wake_up_interruptible(&context_info
->wait
);
361 static void mmc_wait_done(struct mmc_request
*mrq
)
363 complete(&mrq
->completion
);
367 *__mmc_start_data_req() - starts data request
368 * @host: MMC host to start the request
369 * @mrq: data request to start
371 * Sets the done callback to be called when request is completed by the card.
372 * Starts data mmc request execution
374 static int __mmc_start_data_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
376 mrq
->done
= mmc_wait_data_done
;
378 if (mmc_card_removed(host
->card
)) {
379 mrq
->cmd
->error
= -ENOMEDIUM
;
380 mmc_wait_data_done(mrq
);
383 mmc_start_request(host
, mrq
);
388 static int __mmc_start_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
390 init_completion(&mrq
->completion
);
391 mrq
->done
= mmc_wait_done
;
392 if (mmc_card_removed(host
->card
)) {
393 mrq
->cmd
->error
= -ENOMEDIUM
;
394 complete(&mrq
->completion
);
397 mmc_start_request(host
, mrq
);
402 * mmc_wait_for_data_req_done() - wait for request completed
403 * @host: MMC host to prepare the command.
404 * @mrq: MMC request to wait for
406 * Blocks MMC context till host controller will ack end of data request
407 * execution or new request notification arrives from the block layer.
408 * Handles command retries.
410 * Returns enum mmc_blk_status after checking errors.
412 static int mmc_wait_for_data_req_done(struct mmc_host
*host
,
413 struct mmc_request
*mrq
,
414 struct mmc_async_req
*next_req
)
416 struct mmc_command
*cmd
;
417 struct mmc_context_info
*context_info
= &host
->context_info
;
422 wait_event_interruptible(context_info
->wait
,
423 (context_info
->is_done_rcv
||
424 context_info
->is_new_req
));
425 spin_lock_irqsave(&context_info
->lock
, flags
);
426 context_info
->is_waiting_last_req
= false;
427 spin_unlock_irqrestore(&context_info
->lock
, flags
);
428 if (context_info
->is_done_rcv
) {
429 context_info
->is_done_rcv
= false;
430 context_info
->is_new_req
= false;
432 if (!cmd
->error
|| !cmd
->retries
||
433 mmc_card_removed(host
->card
)) {
434 err
= host
->areq
->err_check(host
->card
,
436 break; /* return err */
438 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
440 cmd
->opcode
, cmd
->error
);
443 host
->ops
->request(host
, mrq
);
444 continue; /* wait for done/new event again */
446 } else if (context_info
->is_new_req
) {
447 context_info
->is_new_req
= false;
449 err
= MMC_BLK_NEW_REQUEST
;
450 break; /* return err */
457 static void mmc_wait_for_req_done(struct mmc_host
*host
,
458 struct mmc_request
*mrq
)
461 struct scatterlist
*sg
;
468 struct mmc_command
*cmd
;
471 if(!wait_for_completion_timeout(&mrq
->completion
,DAT_TIMEOUT
)){
472 printk(KERN_ERR
"MSDC wait request timeout CMD<%d>ARG<0x%x>\n",mrq
->cmd
->opcode
,mrq
->cmd
->arg
);
474 host
->ops
->dma_error_reset(host
);
478 mrq
->data
->error
= (unsigned int)-ETIMEDOUT
;
481 printk(KERN_ERR
"MSDC wait request timeout DAT<%d>\n",(mrq
->data
->blocks
) * (mrq
->data
->blksz
));
485 if ((mrq
->cmd
->arg
== 0) && (mrq
->data
) &&
486 ((mrq
->cmd
->opcode
== 17)||(mrq
->cmd
->opcode
== 18))){
487 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
);
489 num
= mrq
->data
->sg_len
;
492 left
= sg_dma_len(sg
);
495 printk("====left: %d\n===\n", left
);
496 for (i
= 0; i
<= left
/4; i
++){
497 printk("0x%x ", *(ptr
+ i
));
502 //page = sg_to_page(sg);
505 //paddr = page_to_phys(page);
514 if (!cmd
->error
|| !cmd
->retries
||
515 mmc_card_removed(host
->card
))
518 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
519 mmc_hostname(host
), cmd
->opcode
, cmd
->error
);
522 host
->ops
->request(host
, mrq
);
527 * mmc_pre_req - Prepare for a new request
528 * @host: MMC host to prepare command
529 * @mrq: MMC request to prepare for
530 * @is_first_req: true if there is no previous started request
531 * that may run in parellel to this call, otherwise false
533 * mmc_pre_req() is called in prior to mmc_start_req() to let
534 * host prepare for the new request. Preparation of a request may be
535 * performed while another request is running on the host.
537 static void mmc_pre_req(struct mmc_host
*host
, struct mmc_request
*mrq
,
540 if (host
->ops
->pre_req
) {
541 mmc_host_clk_hold(host
);
542 host
->ops
->pre_req(host
, mrq
, is_first_req
);
543 mmc_host_clk_release(host
);
548 * mmc_post_req - Post process a completed request
549 * @host: MMC host to post process command
550 * @mrq: MMC request to post process for
551 * @err: Error, if non zero, clean up any resources made in pre_req
553 * Let the host post process a completed request. Post processing of
554 * a request may be performed while another reuqest is running.
556 static void mmc_post_req(struct mmc_host
*host
, struct mmc_request
*mrq
,
559 if (host
->ops
->post_req
) {
560 mmc_host_clk_hold(host
);
561 host
->ops
->post_req(host
, mrq
, err
);
562 mmc_host_clk_release(host
);
567 * mmc_start_req - start a non-blocking request
568 * @host: MMC host to start command
569 * @areq: async request to start
570 * @error: out parameter returns 0 for success, otherwise non zero
572 * Start a new MMC custom command request for a host.
573 * If there is on ongoing async request wait for completion
574 * of that request and start the new one and return.
575 * Does not wait for the new request to complete.
577 * Returns the completed request, NULL in case of none completed.
578 * Wait for the an ongoing request (previoulsy started) to complete and
579 * return the completed request. If there is no ongoing request, NULL
580 * is returned without waiting. NULL is not an error condition.
583 #if defined(FEATURE_STORAGE_PERF_INDEX)
584 extern bool start_async_req
[];
585 extern unsigned long long start_async_req_time
[];
586 extern unsigned int find_mmcqd_index(void);
587 extern unsigned long long mmcqd_t_usage_wr
[];
588 extern unsigned long long mmcqd_t_usage_rd
[];
589 extern unsigned int mmcqd_rq_size_wr
[];
590 extern unsigned int mmcqd_rq_size_rd
[];
591 extern unsigned int mmcqd_rq_count
[];
592 extern unsigned int mmcqd_wr_rq_count
[];
593 extern unsigned int mmcqd_rd_rq_count
[];
596 struct mmc_async_req
*mmc_start_req(struct mmc_host
*host
,
597 struct mmc_async_req
*areq
, int *error
)
601 #if defined(FEATURE_STORAGE_PERF_INDEX)
602 unsigned long long time1
= 0;
603 unsigned int idx
= 0;
605 struct mmc_async_req
*data
= host
->areq
;
609 met_mmc_continue_req_end(host
, host
->areq
);
613 /* Prepare a new request */
615 met_mmc_insert(host
, areq
);
617 mmc_pre_req(host
, areq
->mrq
, !host
->areq
);
619 met_mmc_dma_map(host
, areq
);
623 err
= mmc_wait_for_data_req_done(host
, host
->areq
->mrq
, areq
);
624 if (err
== MMC_BLK_NEW_REQUEST
) {
628 * The previous request was not completed,
633 host
->ops
->send_stop(host
,host
->areq
->mrq
); //add for MTK msdc host <Yuchi Xu>
635 host
->ops
->tuning(host
, host
->areq
->mrq
); //add for MTK msdc host <Yuchi Xu>
636 }while(host
->ops
->check_written_data(host
,host
->areq
->mrq
));
638 #if defined(FEATURE_STORAGE_PERF_INDEX)
639 time1
= sched_clock();
641 idx
= find_mmcqd_index();
642 if (start_async_req
[idx
] == 1)
644 //idx = find_mmcqd_index();
645 mmcqd_rq_count
[idx
]++;
647 if(host
->areq
->mrq
->data
->flags
== MMC_DATA_WRITE
)
649 mmcqd_wr_rq_count
[idx
]++;
650 mmcqd_rq_size_wr
[idx
] += ((host
->areq
->mrq
->data
->blocks
) * (host
->areq
->mrq
->data
->blksz
));
651 mmcqd_t_usage_wr
[idx
] += time1
- start_async_req_time
[idx
];
653 else if (host
->areq
->mrq
->data
->flags
== MMC_DATA_READ
)
655 mmcqd_rd_rq_count
[idx
]++;
656 mmcqd_rq_size_rd
[idx
] += ((host
->areq
->mrq
->data
->blocks
) * (host
->areq
->mrq
->data
->blksz
));
657 mmcqd_t_usage_rd
[idx
] += time1
- start_async_req_time
[idx
];
660 start_async_req
[idx
] = 0;
664 err
= host
->areq
->err_check(host
->card
, host
->areq
);
667 * Check BKOPS urgency for each R1 response
669 if (host
->card
&& mmc_card_mmc(host
->card
) &&
670 ((mmc_resp_type(host
->areq
->mrq
->cmd
) == MMC_RSP_R1
) ||
671 (mmc_resp_type(host
->areq
->mrq
->cmd
) == MMC_RSP_R1B
)) &&
672 (host
->areq
->mrq
->cmd
->resp
[0] & R1_EXCEPTION_EVENT
))
673 mmc_start_bkops(host
->card
, true);
675 met_mmc_complete(host
, host
->areq
);
679 trace_mmc_blk_rw_start(areq
->mrq
->cmd
->opcode
,
682 start_err
= __mmc_start_data_req(host
, areq
->mrq
);
683 #if defined(FEATURE_STORAGE_PERF_INDEX)
684 start_async_req
[idx
] = 1;
685 start_async_req_time
[idx
] = sched_clock();
691 met_mmc_dma_unmap_start(host
, host
->areq
);
693 mmc_post_req(host
, host
->areq
->mrq
, 0);
695 met_mmc_dma_unmap_stop(host
, host
->areq
);
698 /* Cancel a prepared request if it was not started. */
699 if ((err
|| start_err
) && areq
)
700 mmc_post_req(host
, areq
->mrq
, -EINVAL
);
711 EXPORT_SYMBOL(mmc_start_req
);
714 * mmc_wait_for_req - start a request and wait for completion
715 * @host: MMC host to start command
716 * @mrq: MMC request to start
718 * Start a new MMC custom command request for a host, and wait
719 * for the command to complete. Does not attempt to parse the
722 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
724 __mmc_start_req(host
, mrq
);
725 mmc_wait_for_req_done(host
, mrq
);
727 EXPORT_SYMBOL(mmc_wait_for_req
);
730 * mmc_interrupt_hpi - Issue for High priority Interrupt
731 * @card: the MMC card associated with the HPI transfer
733 * Issued High Priority Interrupt, and check for card status
734 * until out-of prg-state.
736 #ifdef MMC_ENABLED_EMPTY_QUEUE_FLUSH
738 int mmc_interrupt_hpi_delay(struct mmc_card
*card
, u32 delay
)
741 u32 status
, delay_count
= 0;
742 unsigned long prg_wait
;
746 if (!card
->ext_csd
.hpi_en
) {
747 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card
->host
));
751 mmc_claim_host(card
->host
);
754 err
= mmc_send_status(card
, &status
);
756 pr_err("%s: Get card status fail\n", mmc_hostname(card
->host
));
759 if(R1_CURRENT_STATE(status
) == R1_STATE_PRG
)
763 }while (++delay_count
< delay
);
765 switch (R1_CURRENT_STATE(status
)) {
771 * In idle and transfer states, HPI is not needed and the caller
772 * can issue the next intended command immediately
774 pr_err("[%s]: %s: card release busy status before stopping by HPI, wait %dms\n", __func__
, mmc_hostname(card
->host
), delay_count
);
777 pr_err("[%s]: %s: do HPI to stop flush ops, wait %dms.\n", __func__
, mmc_hostname(card
->host
), delay_count
);
780 /* In all other states, it's illegal to issue HPI */
781 pr_err("%s: HPI cannot be sent. Card state=%d\n",
782 mmc_hostname(card
->host
), R1_CURRENT_STATE(status
));
787 err
= mmc_send_hpi_cmd(card
, &status
);
791 prg_wait
= jiffies
+ msecs_to_jiffies(card
->ext_csd
.out_of_int_time
);
793 err
= mmc_send_status(card
, &status
);
795 if (!err
&& R1_CURRENT_STATE(status
) == R1_STATE_TRAN
)
797 if (time_after(jiffies
, prg_wait
))
802 mmc_release_host(card
->host
);
806 int mmc_interrupt_hpi(struct mmc_card
*card
)
808 return mmc_interrupt_hpi_delay(card
, 0);
812 int mmc_interrupt_hpi(struct mmc_card
*card
)
816 unsigned long prg_wait
;
820 if (!card
->ext_csd
.hpi_en
) {
821 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card
->host
));
825 mmc_claim_host(card
->host
);
826 err
= mmc_send_status(card
, &status
);
828 pr_err("%s: Get card status fail\n", mmc_hostname(card
->host
));
832 switch (R1_CURRENT_STATE(status
)) {
838 * In idle and transfer states, HPI is not needed and the caller
839 * can issue the next intended command immediately
845 /* In all other states, it's illegal to issue HPI */
846 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
847 mmc_hostname(card
->host
), R1_CURRENT_STATE(status
));
852 err
= mmc_send_hpi_cmd(card
, &status
);
856 prg_wait
= jiffies
+ msecs_to_jiffies(card
->ext_csd
.out_of_int_time
);
858 err
= mmc_send_status(card
, &status
);
860 if (!err
&& R1_CURRENT_STATE(status
) == R1_STATE_TRAN
)
862 if (time_after(jiffies
, prg_wait
))
867 mmc_release_host(card
->host
);
871 EXPORT_SYMBOL(mmc_interrupt_hpi
);
873 #ifdef MMC_ENABLED_EMPTY_QUEUE_FLUSH
875 * mmc_start_flush - start FLUSH OPS for supported cards
876 * @card: MMC card to start FLUSH OPS
877 * @form_exception: A flag to indicate if this function was
878 * called due to an exception raised by the card
880 * Start cache flush operations whenever requested.
881 * When the urgent FLUSH OPS bit is set in a R1 command response
882 * then cache flush operations should be started immediately.
884 void mmc_start_flush(struct mmc_card
*card
)
887 bool use_busy_signal
;
889 if (!card
|| mmc_card_doing_flush(card
)) {
890 printk("[%s]: !card, or it is doing flush now\n", __func__
);
894 mmc_claim_host(card
->host
);
896 if (card
->host
->flush_info
.cancel_delayed_work
) {
897 printk("[%s]: %s: cancel_delayed_work flush ops was set, exit.\n", __func__
, mmc_hostname(card
->host
));
898 card
->host
->flush_info
.cancel_delayed_work
= false;
902 if (!card
->ext_csd
.cache_ctrl
) /* No operations required */
905 //printk("[%s]: mmc_start_flush_doing.\n", __func__);
906 mmc_card_set_doing_flush(card
);
907 err
= mmc_flush_cache(card
);
909 pr_warn("%s: Error %d starting flush ops\n",
910 mmc_hostname(card
->host
), err
);
915 mmc_release_host(card
->host
);
917 EXPORT_SYMBOL(mmc_start_flush
);
920 * mmc_start_idle_time_flush() - check if a non urgent FLUSH OPs is needed
921 * @work: The idle time FLUSH OPs work
923 void mmc_start_idle_time_flush(struct work_struct
*work
)
925 struct mmc_host
*host
= container_of(work
, struct mmc_host
,
926 flush_info
.idle_time_dw
.work
);
928 printk("mmc_start_idle_time_flush...\n");
930 mmc_start_flush(host
->card
);
932 EXPORT_SYMBOL(mmc_start_idle_time_flush
);
935 * mmc_start_delayed_flush() - Start a delayed work to check for the need of non urgent FLUSH OPs
936 * @card: MMC card to start FLUSH OPS
938 void mmc_start_delayed_flush(struct mmc_card
*card
)
940 if (!card
->ext_csd
.cache_ctrl
|| mmc_card_doing_flush(card
)) {
941 printk("queueing delayed_flush_work return.\n");
945 // printk("%s: %s: queueing delayed_flush_work.\n", __func__, mmc_hostname(card->host));
948 * cancel_delayed_flush_work will prevent a race condition between
949 * fetching a request by the queue_thread and the delayed work
951 card
->host
->flush_info
.cancel_delayed_work
= false;
952 queue_delayed_work(card
->host
->flush_info
.wq
, &card
->host
->flush_info
.idle_time_dw
,
953 msecs_to_jiffies(card
->host
->flush_info
.time_to_start_flush_ms
));
955 EXPORT_SYMBOL(mmc_start_delayed_flush
);
958 * mmc_stop_flush - stop ongoing flush
959 * @card: MMC card to check flush
961 * Send HPI command to stop ongoing cache flush operations to
962 * allow rapid servicing of foreground operations, e.g. read/
963 * writes. Wait until the card comes out of the programming state
964 * to avoid errors in servicing read/write requests.
966 int mmc_stop_flush(struct mmc_card
*card
)
968 int err
= 0, count
= 0;
973 * Notify the delayed work to be cancelled, in case it was already
974 * removed from the queue, but was not started yet
976 card
->host
->flush_info
.cancel_delayed_work
= true;
978 if (delayed_work_pending(&card
->host
->flush_info
.idle_time_dw
)) {
979 cancel_delayed_work_sync(&card
->host
->flush_info
.idle_time_dw
);
982 err
= mmc_interrupt_hpi_delay(card
, MMC_CACHE_MAX_TIME_OUT
);
985 * If err is EINVAL, we can't issue an HPI.
986 * It should complete the FLUSH OPS.
988 if (!err
|| (err
== -EINVAL
)) {
989 mmc_card_clr_doing_flush(card
);
995 EXPORT_SYMBOL(mmc_stop_flush
);
999 * mmc_wait_for_cmd - start a command and wait for completion
1000 * @host: MMC host to start command
1001 * @cmd: MMC command to start
1002 * @retries: maximum number of retries
1004 * Start a new MMC command for a host, and wait for the command
1005 * to complete. Return any error that occurred while the command
1006 * was executing. Do not attempt to parse the response.
1008 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
1010 struct mmc_request mrq
= {NULL
};
1012 WARN_ON(!host
->claimed
);
1014 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
1015 cmd
->retries
= retries
;
1020 mmc_wait_for_req(host
, &mrq
);
1025 EXPORT_SYMBOL(mmc_wait_for_cmd
);
1028 * mmc_stop_bkops - stop ongoing BKOPS
1029 * @card: MMC card to check BKOPS
1031 * Send HPI command to stop ongoing background operations to
1032 * allow rapid servicing of foreground operations, e.g. read/
1033 * writes. Wait until the card comes out of the programming state
1034 * to avoid errors in servicing read/write requests.
1036 int mmc_stop_bkops(struct mmc_card
*card
)
1041 err
= mmc_interrupt_hpi(card
);
1044 * If err is EINVAL, we can't issue an HPI.
1045 * It should complete the BKOPS.
1047 if (!err
|| (err
== -EINVAL
)) {
1048 mmc_card_clr_doing_bkops(card
);
1054 EXPORT_SYMBOL(mmc_stop_bkops
);
1056 int mmc_read_bkops_status(struct mmc_card
*card
)
1062 * In future work, we should consider storing the entire ext_csd.
1064 ext_csd
= kmalloc(512, GFP_KERNEL
);
1066 pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
1067 mmc_hostname(card
->host
));
1071 mmc_claim_host(card
->host
);
1072 err
= mmc_send_ext_csd(card
, ext_csd
);
1073 mmc_release_host(card
->host
);
1077 card
->ext_csd
.raw_bkops_status
= ext_csd
[EXT_CSD_BKOPS_STATUS
];
1078 card
->ext_csd
.raw_exception_status
= ext_csd
[EXT_CSD_EXP_EVENTS_STATUS
];
1083 EXPORT_SYMBOL(mmc_read_bkops_status
);
1086 * mmc_set_data_timeout - set the timeout for a data command
1087 * @data: data phase for command
1088 * @card: the MMC card associated with the data transfer
1090 * Computes the data timeout parameters according to the
1091 * correct algorithm given the card type.
1093 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
)
1098 * SDIO cards only define an upper 1 s limit on access.
1100 if (mmc_card_sdio(card
)) {
1101 data
->timeout_ns
= 1000000000;
1102 data
->timeout_clks
= 0;
1107 * SD cards use a 100 multiplier rather than 10
1109 mult
= mmc_card_sd(card
) ? 100 : 10;
1112 * Scale up the multiplier (and therefore the timeout) by
1113 * the r2w factor for writes.
1115 if (data
->flags
& MMC_DATA_WRITE
)
1116 mult
<<= card
->csd
.r2w_factor
;
1118 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
1119 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
1122 * SD cards also have an upper limit on the timeout.
1124 if (mmc_card_sd(card
)) {
1125 unsigned int timeout_us
, limit_us
;
1127 timeout_us
= data
->timeout_ns
/ 1000;
1128 if (mmc_host_clk_rate(card
->host
))
1129 timeout_us
+= data
->timeout_clks
* 1000 /
1130 (mmc_host_clk_rate(card
->host
) / 1000);
1132 if (data
->flags
& MMC_DATA_WRITE
)
1134 * The MMC spec "It is strongly recommended
1135 * for hosts to implement more than 500ms
1136 * timeout value even if the card indicates
1137 * the 250ms maximum busy length." Even the
1138 * previous value of 300ms is known to be
1139 * insufficient for some cards.
1146 * SDHC cards always use these fixed values.
1148 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
1149 data
->timeout_ns
= limit_us
* 1000;
1150 data
->timeout_clks
= 0;
1155 * Some cards require longer data read timeout than indicated in CSD.
1156 * Address this by setting the read timeout to a "reasonably high"
1157 * value. For the cards tested, 300ms has proven enough. If necessary,
1158 * this value can be increased if other problematic cards require this.
1160 if (mmc_card_long_read_time(card
) && data
->flags
& MMC_DATA_READ
) {
1161 data
->timeout_ns
= 300000000;
1162 data
->timeout_clks
= 0;
1166 * Some cards need very high timeouts if driven in SPI mode.
1167 * The worst observed timeout was 900ms after writing a
1168 * continuous stream of data until the internal logic
1171 if (mmc_host_is_spi(card
->host
)) {
1172 if (data
->flags
& MMC_DATA_WRITE
) {
1173 if (data
->timeout_ns
< 1000000000)
1174 data
->timeout_ns
= 1000000000; /* 1s */
1176 if (data
->timeout_ns
< 100000000)
1177 data
->timeout_ns
= 100000000; /* 100ms */
1181 EXPORT_SYMBOL(mmc_set_data_timeout
);
1184 * mmc_align_data_size - pads a transfer size to a more optimal value
1185 * @card: the MMC card associated with the data transfer
1186 * @sz: original transfer size
1188 * Pads the original data size with a number of extra bytes in
1189 * order to avoid controller bugs and/or performance hits
1190 * (e.g. some controllers revert to PIO for certain sizes).
1192 * Returns the improved size, which might be unmodified.
1194 * Note that this function is only relevant when issuing a
1195 * single scatter gather entry.
1197 unsigned int mmc_align_data_size(struct mmc_card
*card
, unsigned int sz
)
1200 * FIXME: We don't have a system for the controller to tell
1201 * the core about its problems yet, so for now we just 32-bit
1204 sz
= ((sz
+ 3) / 4) * 4;
1208 EXPORT_SYMBOL(mmc_align_data_size
);
1211 * __mmc_claim_host - exclusively claim a host
1212 * @host: mmc host to claim
1213 * @abort: whether or not the operation should be aborted
1215 * Claim a host for a set of operations. If @abort is non null and
1216 * dereference a non-zero value then this will return prematurely with
1217 * that non-zero value without acquiring the lock. Returns zero
1218 * with the lock held otherwise.
1220 int __mmc_claim_host(struct mmc_host
*host
, atomic_t
*abort
)
1222 DECLARE_WAITQUEUE(wait
, current
);
1223 unsigned long flags
;
1228 add_wait_queue(&host
->wq
, &wait
);
1229 spin_lock_irqsave(&host
->lock
, flags
);
1231 set_current_state(TASK_UNINTERRUPTIBLE
);
1232 stop
= abort
? atomic_read(abort
) : 0;
1233 if (stop
|| !host
->claimed
|| host
->claimer
== current
)
1235 spin_unlock_irqrestore(&host
->lock
, flags
);
1237 spin_lock_irqsave(&host
->lock
, flags
);
1239 set_current_state(TASK_RUNNING
);
1242 host
->claimer
= current
;
1243 host
->claim_cnt
+= 1;
1246 spin_unlock_irqrestore(&host
->lock
, flags
);
1247 remove_wait_queue(&host
->wq
, &wait
);
1248 if (host
->ops
->enable
&& !stop
&& host
->claim_cnt
== 1)
1249 host
->ops
->enable(host
);
1253 EXPORT_SYMBOL(__mmc_claim_host
);
1256 * mmc_try_claim_host - try exclusively to claim a host
1257 * @host: mmc host to claim
1259 * Returns %1 if the host is claimed, %0 otherwise.
1261 int mmc_try_claim_host(struct mmc_host
*host
)
1263 int claimed_host
= 0;
1264 unsigned long flags
;
1266 spin_lock_irqsave(&host
->lock
, flags
);
1267 if (!host
->claimed
|| host
->claimer
== current
) {
1269 host
->claimer
= current
;
1270 host
->claim_cnt
+= 1;
1273 spin_unlock_irqrestore(&host
->lock
, flags
);
1274 if (host
->ops
->enable
&& claimed_host
&& host
->claim_cnt
== 1)
1275 host
->ops
->enable(host
);
1276 return claimed_host
;
1278 EXPORT_SYMBOL(mmc_try_claim_host
);
1281 * mmc_release_host - release a host
1282 * @host: mmc host to release
1284 * Release a MMC host, allowing others to claim the host
1285 * for their operations.
1287 void mmc_release_host(struct mmc_host
*host
)
1289 unsigned long flags
;
1291 WARN_ON(!host
->claimed
);
1293 if (host
->ops
->disable
&& host
->claim_cnt
== 1)
1294 host
->ops
->disable(host
);
1296 spin_lock_irqsave(&host
->lock
, flags
);
1297 if (--host
->claim_cnt
) {
1298 /* Release for nested claim */
1299 spin_unlock_irqrestore(&host
->lock
, flags
);
1302 host
->claimer
= NULL
;
1303 spin_unlock_irqrestore(&host
->lock
, flags
);
1307 EXPORT_SYMBOL(mmc_release_host
);
1310 * Internal function that does the actual ios call to the host driver,
1311 * optionally printing some debug output.
1313 static inline void mmc_set_ios(struct mmc_host
*host
)
1315 struct mmc_ios
*ios
= &host
->ios
;
1317 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1318 "width %u timing %u\n",
1319 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
1320 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
1321 ios
->bus_width
, ios
->timing
);
1324 mmc_set_ungated(host
);
1325 host
->ops
->set_ios(host
, ios
);
1329 * Control chip select pin on a host.
1331 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
1333 mmc_host_clk_hold(host
);
1334 host
->ios
.chip_select
= mode
;
1336 mmc_host_clk_release(host
);
1340 * Sets the host clock to the highest possible frequency that
1343 static void __mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
1345 WARN_ON(hz
< host
->f_min
);
1347 if (hz
> host
->f_max
)
1350 host
->ios
.clock
= hz
;
1354 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
1356 mmc_host_clk_hold(host
);
1357 __mmc_set_clock(host
, hz
);
1358 mmc_host_clk_release(host
);
1361 #ifdef CONFIG_MMC_CLKGATE
1363 * This gates the clock by setting it to 0 Hz.
1365 void mmc_gate_clock(struct mmc_host
*host
)
1367 unsigned long flags
;
1369 spin_lock_irqsave(&host
->clk_lock
, flags
);
1370 host
->clk_old
= host
->ios
.clock
;
1371 host
->ios
.clock
= 0;
1372 host
->clk_gated
= true;
1373 spin_unlock_irqrestore(&host
->clk_lock
, flags
);
1378 * This restores the clock from gating by using the cached
1381 void mmc_ungate_clock(struct mmc_host
*host
)
1384 * We should previously have gated the clock, so the clock shall
1385 * be 0 here! The clock may however be 0 during initialization,
1386 * when some request operations are performed before setting
1387 * the frequency. When ungate is requested in that situation
1388 * we just ignore the call.
1390 if (host
->clk_old
) {
1391 BUG_ON(host
->ios
.clock
);
1392 /* This call will also set host->clk_gated to false */
1393 __mmc_set_clock(host
, host
->clk_old
);
1397 void mmc_set_ungated(struct mmc_host
*host
)
1399 unsigned long flags
;
1402 * We've been given a new frequency while the clock is gated,
1403 * so make sure we regard this as ungating it.
1405 spin_lock_irqsave(&host
->clk_lock
, flags
);
1406 host
->clk_gated
= false;
1407 spin_unlock_irqrestore(&host
->clk_lock
, flags
);
1411 void mmc_set_ungated(struct mmc_host
*host
)
1417 * Change the bus mode (open drain/push-pull) of a host.
1419 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
1421 mmc_host_clk_hold(host
);
1422 host
->ios
.bus_mode
= mode
;
1424 mmc_host_clk_release(host
);
1428 * Change data bus width of a host.
1430 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
1432 mmc_host_clk_hold(host
);
1433 host
->ios
.bus_width
= width
;
1435 mmc_host_clk_release(host
);
1439 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1440 * @vdd: voltage (mV)
1441 * @low_bits: prefer low bits in boundary cases
1443 * This function returns the OCR bit number according to the provided @vdd
1444 * value. If conversion is not possible a negative errno value returned.
1446 * Depending on the @low_bits flag the function prefers low or high OCR bits
1447 * on boundary voltages. For example,
1448 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1449 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1451 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1453 static int mmc_vdd_to_ocrbitnum(int vdd
, bool low_bits
)
1455 const int max_bit
= ilog2(MMC_VDD_35_36
);
1458 if (vdd
< 1650 || vdd
> 3600)
1461 if (vdd
>= 1650 && vdd
<= 1950)
1462 return ilog2(MMC_VDD_165_195
);
1467 /* Base 2000 mV, step 100 mV, bit's base 8. */
1468 bit
= (vdd
- 2000) / 100 + 8;
1475 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1476 * @vdd_min: minimum voltage value (mV)
1477 * @vdd_max: maximum voltage value (mV)
1479 * This function returns the OCR mask bits according to the provided @vdd_min
1480 * and @vdd_max values. If conversion is not possible the function returns 0.
1482 * Notes wrt boundary cases:
1483 * This function sets the OCR bits for all boundary voltages, for example
1484 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1485 * MMC_VDD_34_35 mask.
1487 u32
mmc_vddrange_to_ocrmask(int vdd_min
, int vdd_max
)
1491 if (vdd_max
< vdd_min
)
1494 /* Prefer high bits for the boundary vdd_max values. */
1495 vdd_max
= mmc_vdd_to_ocrbitnum(vdd_max
, false);
1499 /* Prefer low bits for the boundary vdd_min values. */
1500 vdd_min
= mmc_vdd_to_ocrbitnum(vdd_min
, true);
1504 /* Fill the mask, from max bit to min bit. */
1505 while (vdd_max
>= vdd_min
)
1506 mask
|= 1 << vdd_max
--;
1510 EXPORT_SYMBOL(mmc_vddrange_to_ocrmask
);
1512 #ifdef CONFIG_REGULATOR
1515 * mmc_regulator_get_ocrmask - return mask of supported voltages
1516 * @supply: regulator to use
1518 * This returns either a negative errno, or a mask of voltages that
1519 * can be provided to MMC/SD/SDIO devices using the specified voltage
1520 * regulator. This would normally be called before registering the
1523 int mmc_regulator_get_ocrmask(struct regulator
*supply
)
1529 count
= regulator_count_voltages(supply
);
1533 for (i
= 0; i
< count
; i
++) {
1537 vdd_uV
= regulator_list_voltage(supply
, i
);
1541 vdd_mV
= vdd_uV
/ 1000;
1542 result
|= mmc_vddrange_to_ocrmask(vdd_mV
, vdd_mV
);
1547 EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask
);
1550 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
1551 * @mmc: the host to regulate
1552 * @supply: regulator to use
1553 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
1555 * Returns zero on success, else negative errno.
1557 * MMC host drivers may use this to enable or disable a regulator using
1558 * a particular supply voltage. This would normally be called from the
1561 int mmc_regulator_set_ocr(struct mmc_host
*mmc
,
1562 struct regulator
*supply
,
1563 unsigned short vdd_bit
)
1573 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1574 * bits this regulator doesn't quite support ... don't
1575 * be too picky, most cards and regulators are OK with
1576 * a 0.1V range goof (it's a small error percentage).
1578 tmp
= vdd_bit
- ilog2(MMC_VDD_165_195
);
1580 min_uV
= 1650 * 1000;
1581 max_uV
= 1950 * 1000;
1583 min_uV
= 1900 * 1000 + tmp
* 100 * 1000;
1584 max_uV
= min_uV
+ 100 * 1000;
1588 * If we're using a fixed/static regulator, don't call
1589 * regulator_set_voltage; it would fail.
1591 voltage
= regulator_get_voltage(supply
);
1593 if (!regulator_can_change_voltage(supply
))
1594 min_uV
= max_uV
= voltage
;
1598 else if (voltage
< min_uV
|| voltage
> max_uV
)
1599 result
= regulator_set_voltage(supply
, min_uV
, max_uV
);
1603 if (result
== 0 && !mmc
->regulator_enabled
) {
1604 result
= regulator_enable(supply
);
1606 mmc
->regulator_enabled
= true;
1608 } else if (mmc
->regulator_enabled
) {
1609 result
= regulator_disable(supply
);
1611 mmc
->regulator_enabled
= false;
1615 dev_err(mmc_dev(mmc
),
1616 "could not set regulator OCR (%d)\n", result
);
1619 EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr
);
1621 int mmc_regulator_get_supply(struct mmc_host
*mmc
)
1623 struct device
*dev
= mmc_dev(mmc
);
1624 struct regulator
*supply
;
1627 supply
= devm_regulator_get(dev
, "vmmc");
1628 mmc
->supply
.vmmc
= supply
;
1629 mmc
->supply
.vqmmc
= devm_regulator_get(dev
, "vqmmc");
1632 return PTR_ERR(supply
);
1634 ret
= mmc_regulator_get_ocrmask(supply
);
1636 mmc
->ocr_avail
= ret
;
1638 dev_warn(mmc_dev(mmc
), "Failed getting OCR mask: %d\n", ret
);
1642 EXPORT_SYMBOL_GPL(mmc_regulator_get_supply
);
1644 #endif /* CONFIG_REGULATOR */
1647 * Mask off any voltages we don't support and select
1648 * the lowest voltage
1650 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
1654 ocr
&= host
->ocr_avail
;
1662 mmc_host_clk_hold(host
);
1663 host
->ios
.vdd
= bit
;
1665 mmc_host_clk_release(host
);
1667 pr_warning("%s: host doesn't support card's voltages\n",
1668 mmc_hostname(host
));
1675 int __mmc_set_signal_voltage(struct mmc_host
*host
, int signal_voltage
)
1678 int old_signal_voltage
= host
->ios
.signal_voltage
;
1680 host
->ios
.signal_voltage
= signal_voltage
;
1681 if (host
->ops
->start_signal_voltage_switch
) {
1682 mmc_host_clk_hold(host
);
1683 err
= host
->ops
->start_signal_voltage_switch(host
, &host
->ios
);
1684 mmc_host_clk_release(host
);
1688 host
->ios
.signal_voltage
= old_signal_voltage
;
1694 int mmc_set_signal_voltage(struct mmc_host
*host
, int signal_voltage
)
1696 struct mmc_command cmd
= {0};
1703 * Send CMD11 only if the request is to switch the card to
1706 if (signal_voltage
== MMC_SIGNAL_VOLTAGE_330
)
1707 return __mmc_set_signal_voltage(host
, signal_voltage
);
1710 * If we cannot switch voltages, return failure so the caller
1711 * can continue without UHS mode
1713 if (!host
->ops
->start_signal_voltage_switch
)
1715 if (!host
->ops
->card_busy
)
1716 pr_warning("%s: cannot verify signal voltage switch\n",
1717 mmc_hostname(host
));
1719 cmd
.opcode
= SD_SWITCH_VOLTAGE
;
1721 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1723 err
= mmc_wait_for_cmd(host
, &cmd
, 0);
1727 if (!mmc_host_is_spi(host
) && (cmd
.resp
[0] & R1_ERROR
))
1730 mmc_host_clk_hold(host
);
1732 * The card should drive cmd and dat[0:3] low immediately
1733 * after the response of cmd11, but wait 1 ms to be sure
1736 if (host
->ops
->card_busy
&& !host
->ops
->card_busy(host
)) {
1741 * During a signal voltage level switch, the clock must be gated
1742 * for 5 ms according to the SD spec
1744 clock
= host
->ios
.clock
;
1745 host
->ios
.clock
= 0;
1748 if (__mmc_set_signal_voltage(host
, signal_voltage
)) {
1750 * Voltages may not have been switched, but we've already
1751 * sent CMD11, so a power cycle is required anyway
1757 /* Keep clock gated for at least 5 ms */
1759 host
->ios
.clock
= clock
;
1762 /* Wait for at least 1 ms according to spec */
1766 * Failure to switch is indicated by the card holding
1769 if (host
->ops
->card_busy
&& host
->ops
->card_busy(host
))
1774 pr_debug("%s: Signal voltage switch failed, "
1775 "power cycling card\n", mmc_hostname(host
));
1776 mmc_power_cycle(host
);
1779 mmc_host_clk_release(host
);
1785 * Select timing parameters for host.
1787 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
1789 mmc_host_clk_hold(host
);
1790 host
->ios
.timing
= timing
;
1792 mmc_host_clk_release(host
);
1796 * Select appropriate driver type for host.
1798 void mmc_set_driver_type(struct mmc_host
*host
, unsigned int drv_type
)
1800 mmc_host_clk_hold(host
);
1801 host
->ios
.drv_type
= drv_type
;
1803 mmc_host_clk_release(host
);
1807 * Apply power to the MMC stack. This is a two-stage process.
1808 * First, we enable power to the card without the clock running.
1809 * We then wait a bit for the power to stabilise. Finally,
1810 * enable the bus drivers and clock to the card.
1812 * We must _NOT_ enable the clock prior to power stablising.
1814 * If a host does all the power sequencing itself, ignore the
1815 * initial MMC_POWER_UP stage.
1817 static void mmc_power_up(struct mmc_host
*host
)
1821 mmc_host_clk_hold(host
);
1823 /* If ocr is set, we use it */
1825 bit
= ffs(host
->ocr
) - 1;
1827 bit
= fls(host
->ocr_avail
) - 1;
1829 host
->ios
.vdd
= bit
;
1830 if (mmc_host_is_spi(host
))
1831 host
->ios
.chip_select
= MMC_CS_HIGH
;
1833 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
1834 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
1835 host
->ios
.power_mode
= MMC_POWER_UP
;
1836 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
1837 host
->ios
.timing
= MMC_TIMING_LEGACY
;
1840 /* Set signal voltage to 3.3V */
1841 __mmc_set_signal_voltage(host
, MMC_SIGNAL_VOLTAGE_330
);
1844 * This delay should be sufficient to allow the power supply
1845 * to reach the minimum voltage.
1849 host
->ios
.clock
= host
->f_init
;
1851 host
->ios
.power_mode
= MMC_POWER_ON
;
1855 * This delay must be at least 74 clock sizes, or 1 ms, or the
1856 * time required to reach a stable voltage.
1860 mmc_host_clk_release(host
);
1863 void mmc_power_off(struct mmc_host
*host
)
1865 mmc_host_clk_hold(host
);
1867 host
->ios
.clock
= 0;
1870 #ifdef CONFIG_MTK_EMMC_CACHE
1871 if (host
->card
&& (mmc_card_mmc(host
->card
)) && (host
->card
->ext_csd
.cache_ctrl
& 0x1)) {
1872 if (mmc_cache_ctrl(host
, 0)) {
1873 pr_err("%s: failed to disable cache\n", mmc_hostname(host
));
1880 * Reset ocr mask to be the highest possible voltage supported for
1881 * this mmc host. This value will be used at next power up.
1883 host
->ocr
= 1 << (fls(host
->ocr_avail
) - 1);
1885 if (!mmc_host_is_spi(host
)) {
1886 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
1887 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
1889 host
->ios
.power_mode
= MMC_POWER_OFF
;
1890 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
1891 host
->ios
.timing
= MMC_TIMING_LEGACY
;
1895 * Some configurations, such as the 802.11 SDIO card in the OLPC
1896 * XO-1.5, require a short delay after poweroff before the card
1897 * can be successfully turned on again.
1901 mmc_host_clk_release(host
);
1904 void mmc_power_cycle(struct mmc_host
*host
)
1906 mmc_power_off(host
);
1907 /* Wait at least 1 ms according to SD spec */
1913 * Cleanup when the last reference to the bus operator is dropped.
1915 static void __mmc_release_bus(struct mmc_host
*host
)
1918 BUG_ON(host
->bus_refs
);
1919 BUG_ON(!host
->bus_dead
);
1921 host
->bus_ops
= NULL
;
1925 * Increase reference count of bus operator
1927 static inline void mmc_bus_get(struct mmc_host
*host
)
1929 unsigned long flags
;
1931 spin_lock_irqsave(&host
->lock
, flags
);
1933 spin_unlock_irqrestore(&host
->lock
, flags
);
1937 * Decrease reference count of bus operator and free it if
1938 * it is the last reference.
1940 static inline void mmc_bus_put(struct mmc_host
*host
)
1942 unsigned long flags
;
1944 spin_lock_irqsave(&host
->lock
, flags
);
1946 if ((host
->bus_refs
== 0) && host
->bus_ops
)
1947 __mmc_release_bus(host
);
1948 spin_unlock_irqrestore(&host
->lock
, flags
);
1951 int mmc_resume_bus(struct mmc_host
*host
)
1953 unsigned long flags
;
1955 if (!mmc_bus_needs_resume(host
))
1958 printk("%s: Starting deferred resume\n", mmc_hostname(host
));
1959 spin_lock_irqsave(&host
->lock
, flags
);
1960 host
->bus_resume_flags
&= ~MMC_BUSRESUME_NEEDS_RESUME
;
1961 host
->rescan_disable
= 0;
1962 spin_unlock_irqrestore(&host
->lock
, flags
);
1965 if (host
->bus_ops
&& !host
->bus_dead
) {
1967 BUG_ON(!host
->bus_ops
->resume
);
1968 host
->bus_ops
->resume(host
);
1971 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
1972 host
->bus_ops
->detect(host
);
1975 printk("%s: Deferred resume completed\n", mmc_hostname(host
));
1979 EXPORT_SYMBOL(mmc_resume_bus
);
1982 * Assign a mmc bus handler to a host. Only one bus handler may control a
1983 * host at any given time.
1985 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
1987 unsigned long flags
;
1992 WARN_ON(!host
->claimed
);
1994 spin_lock_irqsave(&host
->lock
, flags
);
1996 BUG_ON(host
->bus_ops
);
1997 BUG_ON(host
->bus_refs
);
1999 host
->bus_ops
= ops
;
2003 spin_unlock_irqrestore(&host
->lock
, flags
);
2007 * Remove the current bus handler from a host.
2009 void mmc_detach_bus(struct mmc_host
*host
)
2011 unsigned long flags
;
2015 WARN_ON(!host
->claimed
);
2016 WARN_ON(!host
->bus_ops
);
2018 spin_lock_irqsave(&host
->lock
, flags
);
2022 spin_unlock_irqrestore(&host
->lock
, flags
);
2028 * mmc_detect_change - process change of state on a MMC socket
2029 * @host: host which changed state.
2030 * @delay: optional delay to wait before detection (jiffies)
2032 * MMC drivers should call this when they detect a card has been
2033 * inserted or removed. The MMC layer will confirm that any
2034 * present card is still functional, and initialize any newly
2037 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
2040 #ifdef CONFIG_MMC_DEBUG
2041 unsigned long flags
;
2042 spin_lock_irqsave(&host
->lock
, flags
);
2043 WARN_ON(host
->removed
);
2044 spin_unlock_irqrestore(&host
->lock
, flags
);
2046 host
->detect_change
= 1;
2048 wake_lock(&host
->detect_wake_lock
);
2049 ret
= mmc_schedule_delayed_work(&host
->detect
, delay
);
2050 printk(KERN_INFO
"msdc: %d,mmc_schedule_delayed_work ret= %d\n",host
->index
,ret
);
2053 EXPORT_SYMBOL(mmc_detect_change
);
2055 void mmc_init_erase(struct mmc_card
*card
)
2059 if (is_power_of_2(card
->erase_size
))
2060 card
->erase_shift
= ffs(card
->erase_size
) - 1;
2062 card
->erase_shift
= 0;
2065 * It is possible to erase an arbitrarily large area of an SD or MMC
2066 * card. That is not desirable because it can take a long time
2067 * (minutes) potentially delaying more important I/O, and also the
2068 * timeout calculations become increasingly hugely over-estimated.
2069 * Consequently, 'pref_erase' is defined as a guide to limit erases
2070 * to that size and alignment.
2072 * For SD cards that define Allocation Unit size, limit erases to one
2073 * Allocation Unit at a time. For MMC cards that define High Capacity
2074 * Erase Size, whether it is switched on or not, limit to that size.
2075 * Otherwise just have a stab at a good value. For modern cards it
2076 * will end up being 4MiB. Note that if the value is too small, it
2077 * can end up taking longer to erase.
2079 if (mmc_card_sd(card
) && card
->ssr
.au
) {
2080 card
->pref_erase
= card
->ssr
.au
;
2081 card
->erase_shift
= ffs(card
->ssr
.au
) - 1;
2082 } else if (card
->ext_csd
.hc_erase_size
) {
2083 card
->pref_erase
= card
->ext_csd
.hc_erase_size
;
2085 sz
= (card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9)) >> 11;
2087 card
->pref_erase
= 512 * 1024 / 512;
2089 card
->pref_erase
= 1024 * 1024 / 512;
2091 card
->pref_erase
= 2 * 1024 * 1024 / 512;
2093 card
->pref_erase
= 4 * 1024 * 1024 / 512;
2094 if (card
->pref_erase
< card
->erase_size
)
2095 card
->pref_erase
= card
->erase_size
;
2097 sz
= card
->pref_erase
% card
->erase_size
;
2099 card
->pref_erase
+= card
->erase_size
- sz
;
2104 static unsigned int mmc_mmc_erase_timeout(struct mmc_card
*card
,
2105 unsigned int arg
, unsigned int qty
)
2107 unsigned int erase_timeout
;
2109 if (arg
== MMC_DISCARD_ARG
||
2110 (arg
== MMC_TRIM_ARG
&& card
->ext_csd
.rev
>= 6)) {
2111 erase_timeout
= card
->ext_csd
.trim_timeout
;
2112 } else if (card
->ext_csd
.erase_group_def
& 1) {
2113 /* High Capacity Erase Group Size uses HC timeouts */
2114 if (arg
== MMC_TRIM_ARG
)
2115 erase_timeout
= card
->ext_csd
.trim_timeout
;
2117 erase_timeout
= card
->ext_csd
.hc_erase_timeout
;
2119 /* CSD Erase Group Size uses write timeout */
2120 unsigned int mult
= (10 << card
->csd
.r2w_factor
);
2121 unsigned int timeout_clks
= card
->csd
.tacc_clks
* mult
;
2122 unsigned int timeout_us
;
2124 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
2125 if (card
->csd
.tacc_ns
< 1000000)
2126 timeout_us
= (card
->csd
.tacc_ns
* mult
) / 1000;
2128 timeout_us
= (card
->csd
.tacc_ns
/ 1000) * mult
;
2131 * ios.clock is only a target. The real clock rate might be
2132 * less but not that much less, so fudge it by multiplying by 2.
2135 timeout_us
+= (timeout_clks
* 1000) /
2136 (mmc_host_clk_rate(card
->host
) / 1000);
2138 erase_timeout
= timeout_us
/ 1000;
2141 * Theoretically, the calculation could underflow so round up
2142 * to 1ms in that case.
2148 /* Multiplier for secure operations */
2149 if (arg
& MMC_SECURE_ARGS
) {
2150 if (arg
== MMC_SECURE_ERASE_ARG
)
2151 erase_timeout
*= card
->ext_csd
.sec_erase_mult
;
2153 erase_timeout
*= card
->ext_csd
.sec_trim_mult
;
2156 erase_timeout
*= qty
;
2159 * Ensure at least a 1 second timeout for SPI as per
2160 * 'mmc_set_data_timeout()'
2162 if (mmc_host_is_spi(card
->host
) && erase_timeout
< 1000)
2163 erase_timeout
= 1000;
2165 return erase_timeout
;
2168 static unsigned int mmc_sd_erase_timeout(struct mmc_card
*card
,
2172 unsigned int erase_timeout
;
2174 if (card
->ssr
.erase_timeout
) {
2175 /* Erase timeout specified in SD Status Register (SSR) */
2176 erase_timeout
= card
->ssr
.erase_timeout
* qty
+
2177 card
->ssr
.erase_offset
;
2180 * Erase timeout not specified in SD Status Register (SSR) so
2181 * use 250ms per write block.
2183 erase_timeout
= 250 * qty
;
2186 /* Must not be less than 1 second */
2187 if (erase_timeout
< 1000)
2188 erase_timeout
= 1000;
2190 return erase_timeout
;
2193 static unsigned int mmc_erase_timeout(struct mmc_card
*card
,
2197 if (mmc_card_sd(card
))
2198 return mmc_sd_erase_timeout(card
, arg
, qty
);
2200 return mmc_mmc_erase_timeout(card
, arg
, qty
);
2203 static int mmc_do_erase(struct mmc_card
*card
, unsigned int from
,
2204 unsigned int to
, unsigned int arg
)
2206 struct mmc_command cmd
= {0};
2207 unsigned int qty
= 0;
2208 unsigned long timeout
;
2209 unsigned int fr
, nr
;
2214 trace_mmc_blk_erase_start(arg
, fr
, nr
);
2217 * qty is used to calculate the erase timeout which depends on how many
2218 * erase groups (or allocation units in SD terminology) are affected.
2219 * We count erasing part of an erase group as one erase group.
2220 * For SD, the allocation units are always a power of 2. For MMC, the
2221 * erase group size is almost certainly also power of 2, but it does not
2222 * seem to insist on that in the JEDEC standard, so we fall back to
2223 * division in that case. SD may not specify an allocation unit size,
2224 * in which case the timeout is based on the number of write blocks.
2226 * Note that the timeout for secure trim 2 will only be correct if the
2227 * number of erase groups specified is the same as the total of all
2228 * preceding secure trim 1 commands. Since the power may have been
2229 * lost since the secure trim 1 commands occurred, it is generally
2230 * impossible to calculate the secure trim 2 timeout correctly.
2232 if (card
->erase_shift
)
2233 qty
+= ((to
>> card
->erase_shift
) -
2234 (from
>> card
->erase_shift
)) + 1;
2235 else if (mmc_card_sd(card
))
2236 qty
+= to
- from
+ 1;
2238 qty
+= ((to
/ card
->erase_size
) -
2239 (from
/ card
->erase_size
)) + 1;
2241 if (!mmc_card_blockaddr(card
)) {
2246 if (mmc_card_sd(card
))
2247 cmd
.opcode
= SD_ERASE_WR_BLK_START
;
2249 cmd
.opcode
= MMC_ERASE_GROUP_START
;
2251 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2252 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2254 pr_err("mmc_erase: group start error %d, "
2255 "status %#x\n", err
, cmd
.resp
[0]);
2260 memset(&cmd
, 0, sizeof(struct mmc_command
));
2261 if (mmc_card_sd(card
))
2262 cmd
.opcode
= SD_ERASE_WR_BLK_END
;
2264 cmd
.opcode
= MMC_ERASE_GROUP_END
;
2266 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2267 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2269 pr_err("mmc_erase: group end error %d, status %#x\n",
2275 memset(&cmd
, 0, sizeof(struct mmc_command
));
2276 cmd
.opcode
= MMC_ERASE
;
2278 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
2279 cmd
.cmd_timeout_ms
= mmc_erase_timeout(card
, arg
, qty
);
2280 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2282 pr_err("mmc_erase: erase error %d, status %#x\n",
2288 if (mmc_host_is_spi(card
->host
))
2291 timeout
= jiffies
+ msecs_to_jiffies(MMC_CORE_TIMEOUT_MS
);
2293 memset(&cmd
, 0, sizeof(struct mmc_command
));
2294 cmd
.opcode
= MMC_SEND_STATUS
;
2295 cmd
.arg
= card
->rca
<< 16;
2296 cmd
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
2297 /* Do not retry else we can't see errors */
2298 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2299 if (err
|| (cmd
.resp
[0] & 0xFDF92000)) {
2300 pr_err("error %d requesting status %#x\n",
2306 /* Timeout if the device never becomes ready for data and
2307 * never leaves the program state.
2309 if (time_after(jiffies
, timeout
)) {
2310 pr_err("%s: Card stuck in programming state! %s\n",
2311 mmc_hostname(card
->host
), __func__
);
2316 } while (!(cmd
.resp
[0] & R1_READY_FOR_DATA
) ||
2317 (R1_CURRENT_STATE(cmd
.resp
[0]) == R1_STATE_PRG
));
2320 trace_mmc_blk_erase_end(arg
, fr
, nr
);
2325 * mmc_erase - erase sectors.
2326 * @card: card to erase
2327 * @from: first sector to erase
2328 * @nr: number of sectors to erase
2329 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2331 * Caller must claim host before calling this function.
2333 int mmc_erase(struct mmc_card
*card
, unsigned int from
, unsigned int nr
,
2336 unsigned int rem
, to
= from
+ nr
;
2338 if (!(card
->host
->caps
& MMC_CAP_ERASE
) ||
2339 !(card
->csd
.cmdclass
& CCC_ERASE
))
2342 if (!card
->erase_size
)
2345 if (mmc_card_sd(card
) && arg
!= MMC_ERASE_ARG
)
2348 if ((arg
& MMC_SECURE_ARGS
) &&
2349 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
))
2352 if ((arg
& MMC_TRIM_ARGS
) &&
2353 !(card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
))
2356 if (arg
== MMC_SECURE_ERASE_ARG
) {
2357 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
2361 if (arg
== MMC_ERASE_ARG
) {
2362 rem
= from
% card
->erase_size
;
2364 rem
= card
->erase_size
- rem
;
2371 rem
= nr
% card
->erase_size
;
2384 /* 'from' and 'to' are inclusive */
2387 return mmc_do_erase(card
, from
, to
, arg
);
2389 EXPORT_SYMBOL(mmc_erase
);
2391 int mmc_can_erase(struct mmc_card
*card
)
2393 if ((card
->host
->caps
& MMC_CAP_ERASE
) &&
2394 (card
->csd
.cmdclass
& CCC_ERASE
) && card
->erase_size
)
2398 EXPORT_SYMBOL(mmc_can_erase
);
2400 int mmc_can_trim(struct mmc_card
*card
)
2402 if ((card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_GB_CL_EN
) &&
2403 !(card
->quirks
& MMC_QUIRK_TRIM_UNSTABLE
) &&
2404 !(card
->quirks
& MMC_QUIRK_KSI_V03_SKIP_TRIM
))
2406 //printk(KERN_ERR "[%s]: quirks=0x%x, MMC_QUIRK_TRIM_UNSTABLE=0x%x\n", __func__, card->quirks, MMC_QUIRK_TRIM_UNSTABLE);
2407 //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);
2410 EXPORT_SYMBOL(mmc_can_trim
);
2412 int mmc_can_discard(struct mmc_card
*card
)
2415 * As there's no way to detect the discard support bit at v4.5
2416 * use the s/w feature support filed.
2418 if (card
->ext_csd
.feature_support
& MMC_DISCARD_FEATURE
)
2422 EXPORT_SYMBOL(mmc_can_discard
);
2424 int mmc_can_sanitize(struct mmc_card
*card
)
2426 if (!mmc_can_trim(card
) && !mmc_can_erase(card
))
2428 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_SANITIZE
)
2432 EXPORT_SYMBOL(mmc_can_sanitize
);
2434 int mmc_can_secure_erase_trim(struct mmc_card
*card
)
2436 if (card
->ext_csd
.sec_feature_support
& EXT_CSD_SEC_ER_EN
)
2440 EXPORT_SYMBOL(mmc_can_secure_erase_trim
);
2442 int mmc_erase_group_aligned(struct mmc_card
*card
, unsigned int from
,
2445 if (!card
->erase_size
)
2447 if (from
% card
->erase_size
|| nr
% card
->erase_size
)
2451 EXPORT_SYMBOL(mmc_erase_group_aligned
);
2453 static unsigned int mmc_do_calc_max_discard(struct mmc_card
*card
,
2456 struct mmc_host
*host
= card
->host
;
2457 unsigned int max_discard
, x
, y
, qty
= 0, max_qty
, timeout
;
2458 unsigned int last_timeout
= 0;
2460 if (card
->erase_shift
)
2461 max_qty
= UINT_MAX
>> card
->erase_shift
;
2462 else if (mmc_card_sd(card
))
2465 max_qty
= UINT_MAX
/ card
->erase_size
;
2467 /* Find the largest qty with an OK timeout */
2470 for (x
= 1; x
&& x
<= max_qty
&& max_qty
- x
>= qty
; x
<<= 1) {
2471 timeout
= mmc_erase_timeout(card
, arg
, qty
+ x
);
2472 if (timeout
> host
->max_discard_to
)
2474 if (timeout
< last_timeout
)
2476 last_timeout
= timeout
;
2488 /* Convert qty to sectors */
2489 if (card
->erase_shift
)
2490 max_discard
= --qty
<< card
->erase_shift
;
2491 else if (mmc_card_sd(card
))
2494 max_discard
= --qty
* card
->erase_size
;
2499 unsigned int mmc_calc_max_discard(struct mmc_card
*card
)
2501 struct mmc_host
*host
= card
->host
;
2502 unsigned int max_discard
, max_trim
;
2504 if (!host
->max_discard_to
)
2508 * Without erase_group_def set, MMC erase timeout depends on clock
2509 * frequence which can change. In that case, the best choice is
2510 * just the preferred erase size.
2512 if (mmc_card_mmc(card
) && !(card
->ext_csd
.erase_group_def
& 1))
2513 return card
->pref_erase
;
2515 max_discard
= mmc_do_calc_max_discard(card
, MMC_ERASE_ARG
);
2516 if (mmc_can_trim(card
)) {
2517 max_trim
= mmc_do_calc_max_discard(card
, MMC_TRIM_ARG
);
2518 if (max_trim
< max_discard
)
2519 max_discard
= max_trim
;
2520 } else if (max_discard
< card
->erase_size
) {
2523 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2524 mmc_hostname(host
), max_discard
, host
->max_discard_to
);
2527 EXPORT_SYMBOL(mmc_calc_max_discard
);
2529 int mmc_set_blocklen(struct mmc_card
*card
, unsigned int blocklen
)
2531 struct mmc_command cmd
= {0};
2533 if (mmc_card_blockaddr(card
) || mmc_card_ddr_mode(card
))
2536 cmd
.opcode
= MMC_SET_BLOCKLEN
;
2538 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2539 return mmc_wait_for_cmd(card
->host
, &cmd
, 5);
2541 EXPORT_SYMBOL(mmc_set_blocklen
);
2543 int mmc_set_blockcount(struct mmc_card
*card
, unsigned int blockcount
,
2546 struct mmc_command cmd
= {0};
2548 cmd
.opcode
= MMC_SET_BLOCK_COUNT
;
2549 cmd
.arg
= blockcount
& 0x0000FFFF;
2552 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
2553 return mmc_wait_for_cmd(card
->host
, &cmd
, 5);
2555 EXPORT_SYMBOL(mmc_set_blockcount
);
2557 static void mmc_hw_reset_for_init(struct mmc_host
*host
)
2559 if (!(host
->caps
& MMC_CAP_HW_RESET
) || !host
->ops
->hw_reset
)
2561 mmc_host_clk_hold(host
);
2562 host
->ops
->hw_reset(host
);
2563 mmc_host_clk_release(host
);
2566 int mmc_can_reset(struct mmc_card
*card
)
2570 if (!mmc_card_mmc(card
))
2572 rst_n_function
= card
->ext_csd
.rst_n_function
;
2573 if ((rst_n_function
& EXT_CSD_RST_N_EN_MASK
) != EXT_CSD_RST_N_ENABLED
)
2577 EXPORT_SYMBOL(mmc_can_reset
);
2579 static int mmc_do_hw_reset(struct mmc_host
*host
, int check
)
2581 struct mmc_card
*card
= host
->card
;
2583 if (!host
->bus_ops
->power_restore
)
2586 if (!(host
->caps
& MMC_CAP_HW_RESET
) || !host
->ops
->hw_reset
)
2592 if (!mmc_can_reset(card
))
2595 mmc_host_clk_hold(host
);
2596 mmc_set_clock(host
, host
->f_init
);
2598 host
->ops
->hw_reset(host
);
2600 /* If the reset has happened, then a status command will fail */
2602 struct mmc_command cmd
= {0};
2605 cmd
.opcode
= MMC_SEND_STATUS
;
2606 if (!mmc_host_is_spi(card
->host
))
2607 cmd
.arg
= card
->rca
<< 16;
2608 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
2609 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
2611 mmc_host_clk_release(host
);
2616 host
->card
->state
&= ~(MMC_STATE_HIGHSPEED
| MMC_STATE_HIGHSPEED_DDR
);
2617 if (mmc_host_is_spi(host
)) {
2618 host
->ios
.chip_select
= MMC_CS_HIGH
;
2619 host
->ios
.bus_mode
= MMC_BUSMODE_PUSHPULL
;
2621 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
2622 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
2624 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
2625 host
->ios
.timing
= MMC_TIMING_LEGACY
;
2628 mmc_host_clk_release(host
);
2630 return host
->bus_ops
->power_restore(host
);
2633 int mmc_hw_reset(struct mmc_host
*host
)
2635 return mmc_do_hw_reset(host
, 0);
2637 EXPORT_SYMBOL(mmc_hw_reset
);
2639 int mmc_hw_reset_check(struct mmc_host
*host
)
2641 return mmc_do_hw_reset(host
, 1);
2643 EXPORT_SYMBOL(mmc_hw_reset_check
);
2645 static int mmc_rescan_try_freq(struct mmc_host
*host
, unsigned freq
)
2647 host
->f_init
= freq
;
2649 #ifdef CONFIG_MMC_DEBUG
2650 pr_info("%s: %s: trying to init card at %u Hz\n",
2651 mmc_hostname(host
), __func__
, host
->f_init
);
2656 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2657 * do a hardware reset if possible.
2659 mmc_hw_reset_for_init(host
);
2662 * sdio_reset sends CMD52 to reset card. Since we do not know
2663 * if the card is being re-initialized, just send it. CMD52
2664 * should be ignored by SD/eMMC cards.
2669 mmc_send_if_cond(host
, host
->ocr_avail
);
2671 /* Order's important: probe SDIO, then SD, then MMC */
2672 if (!mmc_attach_sdio(host
))
2674 if (!mmc_attach_sd(host
))
2676 if (!mmc_attach_mmc(host
))
2679 mmc_power_off(host
);
2683 int _mmc_detect_card_removed(struct mmc_host
*host
)
2687 if ((host
->caps
& MMC_CAP_NONREMOVABLE
) || !host
->bus_ops
->alive
)
2690 if (!host
->card
|| mmc_card_removed(host
->card
))
2693 ret
= host
->bus_ops
->alive(host
);
2696 * Card detect status and alive check may be out of sync if card is
2697 * removed slowly, when card detect switch changes while card/slot
2698 * pads are still contacted in hardware (refer to "SD Card Mechanical
2699 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2700 * detect work 200ms later for this case.
2702 if (!ret
&& host
->ops
->get_cd
&& !host
->ops
->get_cd(host
)) {
2703 mmc_detect_change(host
, msecs_to_jiffies(200));
2704 pr_debug("%s: card removed too slowly\n", mmc_hostname(host
));
2708 mmc_card_set_removed(host
->card
);
2709 pr_debug("%s: card remove detected\n", mmc_hostname(host
));
2715 int mmc_detect_card_removed(struct mmc_host
*host
)
2717 struct mmc_card
*card
= host
->card
;
2720 WARN_ON(!host
->claimed
);
2725 ret
= mmc_card_removed(card
);
2727 * The card will be considered unchanged unless we have been asked to
2728 * detect a change or host requires polling to provide card detection.
2730 if (!host
->detect_change
&& !(host
->caps
& MMC_CAP_NEEDS_POLL
) &&
2731 !(host
->caps2
& MMC_CAP2_DETECT_ON_ERR
))
2734 host
->detect_change
= 0;
2736 ret
= _mmc_detect_card_removed(host
);
2737 if (ret
&& (host
->caps2
& MMC_CAP2_DETECT_ON_ERR
)) {
2739 * Schedule a detect work as soon as possible to let a
2740 * rescan handle the card removal.
2742 cancel_delayed_work(&host
->detect
);
2743 mmc_detect_change(host
, 0);
2749 EXPORT_SYMBOL(mmc_detect_card_removed
);
2751 void mmc_rescan(struct work_struct
*work
)
2753 struct mmc_host
*host
=
2754 container_of(work
, struct mmc_host
, detect
.work
);
2756 bool extend_wakelock
= false;
2758 if (host
->rescan_disable
)
2761 /* If there is a non-removable card registered, only scan once */
2762 // [FIXME] VIA marks it wrong
2763 /* if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered){
2764 if (extend_wakelock)
2765 wake_lock_timeout(&host->detect_wake_lock, HZ / 2);
2767 wake_unlock(&host->detect_wake_lock);
2775 * if there is a _removable_ card registered, check whether it is
2778 if (host
->bus_ops
&& host
->bus_ops
->detect
&& !host
->bus_dead
2779 && !(host
->caps
& MMC_CAP_NONREMOVABLE
))
2780 host
->bus_ops
->detect(host
);
2782 host
->detect_change
= 0;
2784 /* If the card was removed the bus will be marked
2785 * as dead - extend the wakelock so userspace
2788 extend_wakelock
= 1;
2791 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2792 * the card is no longer present.
2797 /* if there still is a card present, stop here */
2798 if (host
->bus_ops
!= NULL
) {
2804 * Only we can add a new handler, so it's safe to
2805 * release the lock here.
2809 if (host
->ops
->get_cd
&& host
->ops
->get_cd(host
) == 0) {
2810 mmc_claim_host(host
);
2811 mmc_power_off(host
);
2812 mmc_release_host(host
);
2816 mmc_claim_host(host
);
2817 for (i
= 0; i
< ARRAY_SIZE(freqs
); i
++) {
2818 if (!mmc_rescan_try_freq(host
, max(freqs
[i
], host
->f_min
))) {
2819 extend_wakelock
= true;
2822 if (freqs
[i
] <= host
->f_min
)
2825 mmc_release_host(host
);
2826 host
->rescan_entered
= 1;
2829 if (extend_wakelock
)
2830 wake_lock_timeout(&host
->detect_wake_lock
, HZ
/ 2);
2832 wake_unlock(&host
->detect_wake_lock
);
2833 if (host
->caps
& MMC_CAP_NEEDS_POLL
) {
2834 wake_lock(&host
->detect_wake_lock
);
2835 mmc_schedule_delayed_work(&host
->detect
, HZ
);
2839 void mmc_start_host(struct mmc_host
*host
)
2841 host
->f_init
= max(freqs
[0], host
->f_min
);
2842 host
->rescan_disable
= 0;
2843 if (host
->caps2
& MMC_CAP2_NO_PRESCAN_POWERUP
)
2844 mmc_power_off(host
);
2847 mmc_detect_change(host
, 0);
2850 void mmc_stop_host(struct mmc_host
*host
)
2852 #ifdef CONFIG_MMC_DEBUG
2853 unsigned long flags
;
2854 spin_lock_irqsave(&host
->lock
, flags
);
2856 spin_unlock_irqrestore(&host
->lock
, flags
);
2859 host
->rescan_disable
= 1;
2860 if (cancel_delayed_work_sync(&host
->detect
))
2861 wake_unlock(&host
->detect_wake_lock
);
2862 mmc_flush_scheduled_work();
2864 /* clear pm flags now and let card drivers set them as needed */
2868 if (host
->bus_ops
&& !host
->bus_dead
) {
2869 /* Calling bus_ops->remove() with a claimed host can deadlock */
2870 if (host
->bus_ops
->remove
)
2871 host
->bus_ops
->remove(host
);
2873 mmc_claim_host(host
);
2874 mmc_detach_bus(host
);
2875 mmc_power_off(host
);
2876 mmc_release_host(host
);
2884 mmc_power_off(host
);
2887 int mmc_power_save_host(struct mmc_host
*host
)
2891 #ifdef CONFIG_MMC_DEBUG
2892 pr_info("%s: %s: powering down\n", mmc_hostname(host
), __func__
);
2897 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
2902 if (host
->bus_ops
->power_save
)
2903 ret
= host
->bus_ops
->power_save(host
);
2907 mmc_power_off(host
);
2911 EXPORT_SYMBOL(mmc_power_save_host
);
2913 int mmc_power_restore_host(struct mmc_host
*host
)
2917 #ifdef CONFIG_MMC_DEBUG
2918 pr_info("%s: %s: powering up\n", mmc_hostname(host
), __func__
);
2923 if (!host
->bus_ops
|| host
->bus_dead
|| !host
->bus_ops
->power_restore
) {
2929 ret
= host
->bus_ops
->power_restore(host
);
2935 EXPORT_SYMBOL(mmc_power_restore_host
);
2937 int mmc_card_awake(struct mmc_host
*host
)
2941 if (host
->caps2
& MMC_CAP2_NO_SLEEP_CMD
)
2946 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->awake
)
2947 err
= host
->bus_ops
->awake(host
);
2953 EXPORT_SYMBOL(mmc_card_awake
);
2955 int mmc_card_sleep(struct mmc_host
*host
)
2959 if (host
->caps2
& MMC_CAP2_NO_SLEEP_CMD
)
2964 if (host
->bus_ops
&& !host
->bus_dead
&& host
->bus_ops
->sleep
)
2965 err
= host
->bus_ops
->sleep(host
);
2971 EXPORT_SYMBOL(mmc_card_sleep
);
2973 int mmc_card_can_sleep(struct mmc_host
*host
)
2975 struct mmc_card
*card
= host
->card
;
2977 if (card
&& mmc_card_mmc(card
) && card
->ext_csd
.rev
>= 3)
2981 EXPORT_SYMBOL(mmc_card_can_sleep
);
2984 * Flush the cache to the non-volatile storage.
2986 int mmc_flush_cache(struct mmc_card
*card
)
2988 struct mmc_host
*host
= card
->host
;
2991 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
))
2994 if (mmc_card_mmc(card
) &&
2995 (card
->ext_csd
.cache_size
> 0) &&
2996 (card
->ext_csd
.cache_ctrl
& 1)) {
2997 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
2998 EXT_CSD_FLUSH_CACHE
, 1, 0);
3000 pr_err("%s: cache flush error %d\n",
3001 mmc_hostname(card
->host
), err
);
3006 EXPORT_SYMBOL(mmc_flush_cache
);
3009 * Turn the cache ON/OFF.
3010 * Turning the cache OFF shall trigger flushing of the data
3011 * to the non-volatile storage.
3012 * This function should be called with host claimed
3014 int mmc_cache_ctrl(struct mmc_host
*host
, u8 enable
)
3016 struct mmc_card
*card
= host
->card
;
3017 unsigned int timeout
;
3020 #ifdef CONFIG_MTK_EMMC_CACHE
3021 //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);
3022 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
) ||
3023 mmc_card_is_removable(host
) ||
3024 (card
->quirks
& MMC_QUIRK_DISABLE_CACHE
))
3027 if (!(host
->caps2
& MMC_CAP2_CACHE_CTRL
) ||
3028 mmc_card_is_removable(host
))
3032 if (card
&& mmc_card_mmc(card
) &&
3033 (card
->ext_csd
.cache_size
> 0)) {
3036 if (card
->ext_csd
.cache_ctrl
^ enable
) {
3037 timeout
= enable
? card
->ext_csd
.generic_cmd6_time
: 0;
3038 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
3039 EXT_CSD_CACHE_CTRL
, enable
, timeout
);
3041 pr_err("%s: cache %s error %d\n",
3042 mmc_hostname(card
->host
),
3043 enable
? "on" : "off",
3046 card
->ext_csd
.cache_ctrl
= enable
;
3052 EXPORT_SYMBOL(mmc_cache_ctrl
);
3057 * mmc_suspend_host - suspend a host
3060 int mmc_suspend_host(struct mmc_host
*host
)
3064 if (mmc_bus_needs_resume(host
))
3067 if (cancel_delayed_work(&host
->detect
))
3068 wake_unlock(&host
->detect_wake_lock
);
3069 mmc_flush_scheduled_work();
3072 if (host
->bus_ops
&& !host
->bus_dead
) {
3073 if (host
->bus_ops
->suspend
) {
3074 if (mmc_card_doing_bkops(host
->card
)) {
3075 err
= mmc_stop_bkops(host
->card
);
3079 err
= host
->bus_ops
->suspend(host
);
3082 if (err
== -ENOSYS
|| !host
->bus_ops
->resume
) {
3084 * We simply "remove" the card in this case.
3085 * It will be redetected on resume. (Calling
3086 * bus_ops->remove() with a claimed host can
3089 if (host
->bus_ops
->remove
)
3090 host
->bus_ops
->remove(host
);
3091 mmc_claim_host(host
);
3092 mmc_detach_bus(host
);
3093 mmc_power_off(host
);
3094 mmc_release_host(host
);
3101 if (!err
&& !mmc_card_keep_power(host
))
3102 mmc_power_off(host
);
3108 EXPORT_SYMBOL(mmc_suspend_host
);
3111 * mmc_resume_host - resume a previously suspended host
3114 int mmc_resume_host(struct mmc_host
*host
)
3119 if (mmc_bus_manual_resume(host
)) {
3120 host
->bus_resume_flags
|= MMC_BUSRESUME_NEEDS_RESUME
;
3125 if (host
->bus_ops
&& !host
->bus_dead
) {
3126 if (!mmc_card_keep_power(host
)) {
3128 mmc_select_voltage(host
, host
->ocr
);
3130 * Tell runtime PM core we just powered up the card,
3131 * since it still believes the card is powered off.
3132 * Note that currently runtime PM is only enabled
3133 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
3135 if (mmc_card_sdio(host
->card
) &&
3136 (host
->caps
& MMC_CAP_POWER_OFF_CARD
)) {
3137 pm_runtime_disable(&host
->card
->dev
);
3138 pm_runtime_set_active(&host
->card
->dev
);
3139 pm_runtime_enable(&host
->card
->dev
);
3142 BUG_ON(!host
->bus_ops
->resume
);
3143 err
= host
->bus_ops
->resume(host
);
3145 pr_warning("%s: error %d during resume "
3146 "(card was removed?)\n",
3147 mmc_hostname(host
), err
);
3149 mmc_card_set_removed(host
->card
);
3150 pr_warning("%s: card resume fail and remove\n", mmc_hostname(host
));
3155 host
->pm_flags
&= ~MMC_PM_KEEP_POWER
;
3160 EXPORT_SYMBOL(mmc_resume_host
);
3162 /* Do the card removal on suspend if card is assumed removeable
3163 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
3166 int mmc_pm_notify(struct notifier_block
*notify_block
,
3167 unsigned long mode
, void *unused
)
3169 struct mmc_host
*host
= container_of(
3170 notify_block
, struct mmc_host
, pm_notify
);
3171 unsigned long flags
;
3173 #ifdef CONFIG_MTK_HIBERNATION
3174 unsigned long wait_time
= 0;
3178 case PM_HIBERNATION_PREPARE
:
3179 case PM_SUSPEND_PREPARE
:
3180 if (host
->card
&& mmc_card_mmc(host
->card
) &&
3181 mmc_card_doing_bkops(host
->card
)) {
3182 err
= mmc_stop_bkops(host
->card
);
3184 pr_err("%s: didn't stop bkops\n",
3185 mmc_hostname(host
));
3188 mmc_card_clr_doing_bkops(host
->card
);
3191 spin_lock_irqsave(&host
->lock
, flags
);
3192 if (mmc_bus_needs_resume(host
)) {
3193 spin_unlock_irqrestore(&host
->lock
, flags
);
3196 host
->rescan_disable
= 1;
3197 spin_unlock_irqrestore(&host
->lock
, flags
);
3198 if (cancel_delayed_work_sync(&host
->detect
))
3199 wake_unlock(&host
->detect_wake_lock
);
3201 if (!host
->bus_ops
|| host
->bus_ops
->suspend
)
3204 /* Calling bus_ops->remove() with a claimed host can deadlock */
3205 if (host
->bus_ops
->remove
)
3206 host
->bus_ops
->remove(host
);
3208 mmc_claim_host(host
);
3209 mmc_detach_bus(host
);
3210 mmc_power_off(host
);
3211 mmc_release_host(host
);
3215 #ifdef CONFIG_MTK_HIBERNATION
3216 case PM_RESTORE_PREPARE
:
3217 /* For hibernation boot-up, mmc rescan job MUST finish before entering hiberation restore flow.
3218 Or mmc rescan may call submit_bio(), which will induce BUG_ON() in submit_bio() !!
3220 if (!host
->card
|| !mmc_card_present(host
->card
)) {
3221 pr_warn("[%s] %s card is not present.\n", __func__
, mmc_hostname(host
));
3224 while (wait_time
< MMC_PM_RESTORE_WAIT_MS
) {
3225 if (host
->rescan_disable
|| host
->rescan_entered
) {
3226 pr_warn("[%s] %s (%d/%d) rescan done.\n", __func__
,
3227 mmc_hostname(host
), host
->rescan_disable
, host
->rescan_entered
);
3233 if (unlikely(wait_time
>= MMC_PM_RESTORE_WAIT_MS
)) {
3234 pr_warn("[%s] %s (%d/%d) rescan timeout !!\n", __func__
,
3235 mmc_hostname(host
), host
->rescan_disable
, host
->rescan_entered
);
3236 return notifier_from_errno(-EIO
);
3240 #endif /* CONFIG_MTK_HIBERNATION */
3242 case PM_POST_SUSPEND
:
3243 case PM_POST_HIBERNATION
:
3244 case PM_POST_RESTORE
:
3246 spin_lock_irqsave(&host
->lock
, flags
);
3247 if (mmc_bus_manual_resume(host
)) {
3248 spin_unlock_irqrestore(&host
->lock
, flags
);
3251 host
->rescan_disable
= 0;
3252 spin_unlock_irqrestore(&host
->lock
, flags
);
3253 mmc_detect_change(host
, 0);
3262 * mmc_init_context_info() - init synchronization context
3265 * Init struct context_info needed to implement asynchronous
3266 * request mechanism, used by mmc core, host driver and mmc requests
3269 void mmc_init_context_info(struct mmc_host
*host
)
3271 spin_lock_init(&host
->context_info
.lock
);
3272 host
->context_info
.is_new_req
= false;
3273 host
->context_info
.is_done_rcv
= false;
3274 host
->context_info
.is_waiting_last_req
= false;
3275 init_waitqueue_head(&host
->context_info
.wait
);
3278 #ifdef CONFIG_MMC_EMBEDDED_SDIO
3279 void mmc_set_embedded_sdio_data(struct mmc_host
*host
,
3280 struct sdio_cis
*cis
,
3281 struct sdio_cccr
*cccr
,
3282 struct sdio_embedded_func
*funcs
,
3285 host
->embedded_sdio_data
.cis
= cis
;
3286 host
->embedded_sdio_data
.cccr
= cccr
;
3287 host
->embedded_sdio_data
.funcs
= funcs
;
3288 host
->embedded_sdio_data
.num_funcs
= num_funcs
;
3291 EXPORT_SYMBOL(mmc_set_embedded_sdio_data
);
3294 static int __init
mmc_init(void)
3298 workqueue
= alloc_ordered_workqueue("kmmcd", 0);
3302 ret
= mmc_register_bus();
3304 goto destroy_workqueue
;
3306 ret
= mmc_register_host_class();
3308 goto unregister_bus
;
3310 ret
= sdio_register_bus();
3312 goto unregister_host_class
;
3316 unregister_host_class
:
3317 mmc_unregister_host_class();
3319 mmc_unregister_bus();
3321 destroy_workqueue(workqueue
);
3326 static void __exit
mmc_exit(void)
3328 sdio_unregister_bus();
3329 mmc_unregister_host_class();
3330 mmc_unregister_bus();
3331 destroy_workqueue(workqueue
);
3334 subsys_initcall(mmc_init
);
3335 module_exit(mmc_exit
);
3337 MODULE_LICENSE("GPL");