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-2007 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 <asm/scatterlist.h>
22 #include <linux/scatterlist.h>
24 #include <linux/mmc/card.h>
25 #include <linux/mmc/host.h>
26 #include <linux/mmc/mmc.h>
27 #include <linux/mmc/sd.h>
36 extern int mmc_attach_mmc(struct mmc_host
*host
, u32 ocr
);
37 extern int mmc_attach_sd(struct mmc_host
*host
, u32 ocr
);
39 static struct workqueue_struct
*workqueue
;
42 * Internal function. Schedule delayed work in the MMC work queue.
44 #if 0 // mask by Victor Yu. 12-02-2008
45 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
48 static int mmc_schedule_delayed_work(struct work_struct
*work
,
52 return queue_delayed_work(workqueue
, work
, delay
);
56 * Internal function. Flush all scheduled work from the MMC work queue.
58 static void mmc_flush_scheduled_work(void)
60 flush_workqueue(workqueue
);
64 * mmc_request_done - finish processing an MMC request
65 * @host: MMC host which completed request
66 * @mrq: MMC request which request
68 * MMC drivers should call this function when they have completed
69 * their processing of a request.
71 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
73 struct mmc_command
*cmd
= mrq
->cmd
;
76 if (err
&& cmd
->retries
) {
77 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
78 mmc_hostname(host
), cmd
->opcode
, err
);
82 host
->ops
->request(host
, mrq
);
84 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
85 mmc_hostname(host
), cmd
->opcode
, err
,
86 cmd
->resp
[0], cmd
->resp
[1],
87 cmd
->resp
[2], cmd
->resp
[3]);
90 pr_debug("%s: %d bytes transferred: %d\n",
92 mrq
->data
->bytes_xfered
, mrq
->data
->error
);
96 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
97 mmc_hostname(host
), mrq
->stop
->opcode
,
99 mrq
->stop
->resp
[0], mrq
->stop
->resp
[1],
100 mrq
->stop
->resp
[2], mrq
->stop
->resp
[3]);
108 EXPORT_SYMBOL(mmc_request_done
);
111 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
113 #ifdef CONFIG_MMC_DEBUG
117 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
118 mmc_hostname(host
), mrq
->cmd
->opcode
,
119 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
122 pr_debug("%s: blksz %d blocks %d flags %08x "
123 "tsac %d ms nsac %d\n",
124 mmc_hostname(host
), mrq
->data
->blksz
,
125 mrq
->data
->blocks
, mrq
->data
->flags
,
126 mrq
->data
->timeout_ns
/ 10000000,
127 mrq
->data
->timeout_clks
);
131 pr_debug("%s: CMD%u arg %08x flags %08x\n",
132 mmc_hostname(host
), mrq
->stop
->opcode
,
133 mrq
->stop
->arg
, mrq
->stop
->flags
);
136 WARN_ON(!host
->claimed
);
141 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
142 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
143 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
146 #ifdef CONFIG_MMC_DEBUG
148 for (i
= 0;i
< mrq
->data
->sg_len
;i
++)
149 sz
+= mrq
->data
->sg
[i
].length
;
150 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
153 mrq
->cmd
->data
= mrq
->data
;
154 mrq
->data
->error
= 0;
155 mrq
->data
->mrq
= mrq
;
157 mrq
->data
->stop
= mrq
->stop
;
158 mrq
->stop
->error
= 0;
159 mrq
->stop
->mrq
= mrq
;
162 host
->ops
->request(host
, mrq
);
165 static void mmc_wait_done(struct mmc_request
*mrq
)
167 complete(mrq
->done_data
);
171 * mmc_wait_for_req - start a request and wait for completion
172 * @host: MMC host to start command
173 * @mrq: MMC request to start
175 * Start a new MMC custom command request for a host, and wait
176 * for the command to complete. Does not attempt to parse the
179 void mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
181 DECLARE_COMPLETION
/*_ONSTACK*/(complete
);
183 mrq
->done_data
= &complete
;
184 mrq
->done
= mmc_wait_done
;
186 mmc_start_request(host
, mrq
);
188 wait_for_completion(&complete
);
191 EXPORT_SYMBOL(mmc_wait_for_req
);
194 * mmc_wait_for_cmd - start a command and wait for completion
195 * @host: MMC host to start command
196 * @cmd: MMC command to start
197 * @retries: maximum number of retries
199 * Start a new MMC command for a host, and wait for the command
200 * to complete. Return any error that occurred while the command
201 * was executing. Do not attempt to parse the response.
203 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
205 struct mmc_request mrq
;
207 BUG_ON(!host
->claimed
);
209 memset(&mrq
, 0, sizeof(struct mmc_request
));
211 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
212 cmd
->retries
= retries
;
217 mmc_wait_for_req(host
, &mrq
);
222 EXPORT_SYMBOL(mmc_wait_for_cmd
);
225 * mmc_set_data_timeout - set the timeout for a data command
226 * @data: data phase for command
227 * @card: the MMC card associated with the data transfer
228 * @write: flag to differentiate reads from writes
230 * Computes the data timeout parameters according to the
231 * correct algorithm given the card type.
233 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
,
239 * SD cards use a 100 multiplier rather than 10
241 mult
= mmc_card_sd(card
) ? 100 : 10;
244 * Scale up the multiplier (and therefore the timeout) by
245 * the r2w factor for writes.
248 mult
<<= card
->csd
.r2w_factor
;
250 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
251 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
254 * SD cards also have an upper limit on the timeout.
256 if (mmc_card_sd(card
)) {
257 unsigned int timeout_us
, limit_us
;
259 timeout_us
= data
->timeout_ns
/ 1000;
260 timeout_us
+= data
->timeout_clks
* 1000 /
261 (card
->host
->ios
.clock
/ 1000);
269 * SDHC cards always use these fixed values.
271 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
272 data
->timeout_ns
= limit_us
* 1000;
273 data
->timeout_clks
= 0;
277 EXPORT_SYMBOL(mmc_set_data_timeout
);
280 * mmc_claim_host - exclusively claim a host
281 * @host: mmc host to claim
283 * Claim a host for a set of operations.
285 void mmc_claim_host(struct mmc_host
*host
)
287 DECLARE_WAITQUEUE(wait
, current
);
292 add_wait_queue(&host
->wq
, &wait
);
293 spin_lock_irqsave(&host
->lock
, flags
);
295 set_current_state(TASK_UNINTERRUPTIBLE
);
298 spin_unlock_irqrestore(&host
->lock
, flags
);
300 spin_lock_irqsave(&host
->lock
, flags
);
302 set_current_state(TASK_RUNNING
);
304 spin_unlock_irqrestore(&host
->lock
, flags
);
305 remove_wait_queue(&host
->wq
, &wait
);
308 EXPORT_SYMBOL(mmc_claim_host
);
311 * mmc_release_host - release a host
312 * @host: mmc host to release
314 * Release a MMC host, allowing others to claim the host
315 * for their operations.
317 void mmc_release_host(struct mmc_host
*host
)
321 BUG_ON(!host
->claimed
);
323 spin_lock_irqsave(&host
->lock
, flags
);
325 spin_unlock_irqrestore(&host
->lock
, flags
);
330 EXPORT_SYMBOL(mmc_release_host
);
333 * Internal function that does the actual ios call to the host driver,
334 * optionally printing some debug output.
336 static inline void mmc_set_ios(struct mmc_host
*host
)
338 struct mmc_ios
*ios
= &host
->ios
;
340 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
341 "width %u timing %u\n",
342 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
343 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
344 ios
->bus_width
, ios
->timing
);
346 host
->ops
->set_ios(host
, ios
);
350 * Control chip select pin on a host.
352 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
354 host
->ios
.chip_select
= mode
;
359 * Sets the host clock to the highest possible frequency that
362 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
364 WARN_ON(hz
< host
->f_min
);
366 if (hz
> host
->f_max
)
369 host
->ios
.clock
= hz
;
374 * Change the bus mode (open drain/push-pull) of a host.
376 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
378 host
->ios
.bus_mode
= mode
;
383 * Change data bus width of a host.
385 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
387 host
->ios
.bus_width
= width
;
392 * Mask off any voltages we don't support and select
395 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
399 ocr
&= host
->ocr_avail
;
417 * Select timing parameters for host.
419 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
421 host
->ios
.timing
= timing
;
426 * Apply power to the MMC stack. This is a two-stage process.
427 * First, we enable power to the card without the clock running.
428 * We then wait a bit for the power to stabilise. Finally,
429 * enable the bus drivers and clock to the card.
431 * We must _NOT_ enable the clock prior to power stablising.
433 * If a host does all the power sequencing itself, ignore the
434 * initial MMC_POWER_UP stage.
436 static void mmc_power_up(struct mmc_host
*host
)
438 int bit
= fls(host
->ocr_avail
) - 1;
441 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
442 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
443 host
->ios
.power_mode
= MMC_POWER_UP
;
444 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
445 host
->ios
.timing
= MMC_TIMING_LEGACY
;
450 host
->ios
.clock
= host
->f_min
;
451 host
->ios
.power_mode
= MMC_POWER_ON
;
457 static void mmc_power_off(struct mmc_host
*host
)
461 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
462 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
463 host
->ios
.power_mode
= MMC_POWER_OFF
;
464 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
465 host
->ios
.timing
= MMC_TIMING_LEGACY
;
470 * Cleanup when the last reference to the bus operator is dropped.
472 void __mmc_release_bus(struct mmc_host
*host
)
475 BUG_ON(host
->bus_refs
);
476 BUG_ON(!host
->bus_dead
);
478 host
->bus_ops
= NULL
;
482 * Increase reference count of bus operator
484 static inline void mmc_bus_get(struct mmc_host
*host
)
488 spin_lock_irqsave(&host
->lock
, flags
);
490 spin_unlock_irqrestore(&host
->lock
, flags
);
494 * Decrease reference count of bus operator and free it if
495 * it is the last reference.
497 static inline void mmc_bus_put(struct mmc_host
*host
)
501 spin_lock_irqsave(&host
->lock
, flags
);
503 if ((host
->bus_refs
== 0) && host
->bus_ops
) {
504 __mmc_release_bus(host
);
506 spin_unlock_irqrestore(&host
->lock
, flags
);
510 * Assign a mmc bus handler to a host. Only one bus handler may control a
511 * host at any given time.
513 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
520 BUG_ON(!host
->claimed
);
522 spin_lock_irqsave(&host
->lock
, flags
);
524 BUG_ON(host
->bus_ops
);
525 BUG_ON(host
->bus_refs
);
531 spin_unlock_irqrestore(&host
->lock
, flags
);
535 * Remove the current bus handler from a host. Assumes that there are
536 * no interesting cards left, so the bus is powered down.
538 void mmc_detach_bus(struct mmc_host
*host
)
544 BUG_ON(!host
->claimed
);
545 BUG_ON(!host
->bus_ops
);
547 spin_lock_irqsave(&host
->lock
, flags
);
551 spin_unlock_irqrestore(&host
->lock
, flags
);
559 * mmc_detect_change - process change of state on a MMC socket
560 * @host: host which changed state.
561 * @delay: optional delay to wait before detection (jiffies)
563 * MMC drivers should call this when they detect a card has been
564 * inserted or removed. The MMC layer will confirm that any
565 * present card is still functional, and initialize any newly
568 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
570 #ifdef CONFIG_MMC_DEBUG
572 spin_lock_irqsave(&host
->lock
, flags
);
573 BUG_ON(host
->removed
);
574 spin_unlock_irqrestore(&host
->lock
, flags
);
577 mmc_schedule_delayed_work(&host
->detect
, delay
);
580 EXPORT_SYMBOL(mmc_detect_change
);
583 #if 0 // mask by Victor Yu. 12-02-2008
584 void mmc_rescan(struct work_struct
*work
)
586 void mmc_rescan(void *data
)
589 #if 0 // mask by Victor Yu. 12-02-2008
590 struct mmc_host
*host
=
591 container_of(work
, struct mmc_host
, detect
.work
);
593 struct mmc_host
*host
= data
;
598 #if 1 // add by Victor Yu. 12-03-2008
604 if (host
->bus_ops
== NULL
) {
606 * Only we can add a new handler, so it's safe to
607 * release the lock here.
611 mmc_claim_host(host
);
616 mmc_send_if_cond(host
, host
->ocr_avail
);
618 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
619 if (err
== MMC_ERR_NONE
) {
620 if (mmc_attach_sd(host
, ocr
)) {
625 * If we fail to detect any SD cards then try
626 * searching for MMC cards.
628 err
= mmc_send_op_cond(host
, 0, &ocr
);
629 if (err
== MMC_ERR_NONE
) {
630 if (mmc_attach_mmc(host
, ocr
)) {
635 mmc_release_host(host
);
639 if (host
->bus_ops
->detect
&& !host
->bus_dead
) {
640 host
->bus_ops
->detect(host
);
647 void mmc_start_host(struct mmc_host
*host
)
650 mmc_detect_change(host
, 0);
653 void mmc_stop_host(struct mmc_host
*host
)
655 #ifdef CONFIG_MMC_DEBUG
657 spin_lock_irqsave(&host
->lock
, flags
);
659 spin_unlock_irqrestore(&host
->lock
, flags
);
662 mmc_flush_scheduled_work();
665 if (host
->bus_ops
&& !host
->bus_dead
) {
666 if (host
->bus_ops
->remove
)
667 host
->bus_ops
->remove(host
);
669 mmc_claim_host(host
);
670 mmc_detach_bus(host
);
671 mmc_release_host(host
);
683 * mmc_suspend_host - suspend a host
685 * @state: suspend mode (PM_SUSPEND_xxx)
687 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
689 mmc_flush_scheduled_work();
692 if (host
->bus_ops
&& !host
->bus_dead
) {
693 if (host
->bus_ops
->suspend
)
694 host
->bus_ops
->suspend(host
);
695 if (!host
->bus_ops
->resume
) {
696 if (host
->bus_ops
->remove
)
697 host
->bus_ops
->remove(host
);
699 mmc_claim_host(host
);
700 mmc_detach_bus(host
);
701 mmc_release_host(host
);
711 EXPORT_SYMBOL(mmc_suspend_host
);
714 * mmc_resume_host - resume a previously suspended host
717 int mmc_resume_host(struct mmc_host
*host
)
720 if (host
->bus_ops
&& !host
->bus_dead
) {
722 BUG_ON(!host
->bus_ops
->resume
);
723 host
->bus_ops
->resume(host
);
728 * We add a slight delay here so that resume can progress
731 mmc_detect_change(host
, 1);
736 EXPORT_SYMBOL(mmc_resume_host
);
740 static int __init
mmc_init(void)
744 workqueue
= create_singlethread_workqueue("kmmcd");
748 ret
= mmc_register_bus();
750 ret
= mmc_register_host_class();
752 mmc_unregister_bus();
757 static void __exit
mmc_exit(void)
759 mmc_unregister_host_class();
760 mmc_unregister_bus();
761 destroy_workqueue(workqueue
);
764 module_init(mmc_init
);
765 module_exit(mmc_exit
);
767 MODULE_LICENSE("GPL");