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 static int mmc_schedule_delayed_work(struct delayed_work
*work
,
47 return queue_delayed_work(workqueue
, work
, delay
);
51 * Internal function. Flush all scheduled work from the MMC work queue.
53 static void mmc_flush_scheduled_work(void)
55 flush_workqueue(workqueue
);
59 * mmc_request_done - finish processing an MMC request
60 * @host: MMC host which completed request
61 * @mrq: MMC request which request
63 * MMC drivers should call this function when they have completed
64 * their processing of a request.
66 void mmc_request_done(struct mmc_host
*host
, struct mmc_request
*mrq
)
68 struct mmc_command
*cmd
= mrq
->cmd
;
71 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
72 mmc_hostname(host
), cmd
->opcode
, err
,
73 mrq
->data
? mrq
->data
->error
: 0,
74 mrq
->stop
? mrq
->stop
->error
: 0,
75 cmd
->resp
[0], cmd
->resp
[1], cmd
->resp
[2], cmd
->resp
[3]);
77 if (err
&& cmd
->retries
) {
80 host
->ops
->request(host
, mrq
);
81 } else if (mrq
->done
) {
86 EXPORT_SYMBOL(mmc_request_done
);
89 * mmc_start_request - start a command on a host
90 * @host: MMC host to start command on
91 * @mrq: MMC request to start
93 * Queue a command on the specified host. We expect the
94 * caller to be holding the host lock with interrupts disabled.
97 mmc_start_request(struct mmc_host
*host
, struct mmc_request
*mrq
)
99 #ifdef CONFIG_MMC_DEBUG
103 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
104 mmc_hostname(host
), mrq
->cmd
->opcode
,
105 mrq
->cmd
->arg
, mrq
->cmd
->flags
);
107 WARN_ON(!host
->claimed
);
112 BUG_ON(mrq
->data
->blksz
> host
->max_blk_size
);
113 BUG_ON(mrq
->data
->blocks
> host
->max_blk_count
);
114 BUG_ON(mrq
->data
->blocks
* mrq
->data
->blksz
>
117 #ifdef CONFIG_MMC_DEBUG
119 for (i
= 0;i
< mrq
->data
->sg_len
;i
++)
120 sz
+= mrq
->data
->sg
[i
].length
;
121 BUG_ON(sz
!= mrq
->data
->blocks
* mrq
->data
->blksz
);
124 mrq
->cmd
->data
= mrq
->data
;
125 mrq
->data
->error
= 0;
126 mrq
->data
->mrq
= mrq
;
128 mrq
->data
->stop
= mrq
->stop
;
129 mrq
->stop
->error
= 0;
130 mrq
->stop
->mrq
= mrq
;
133 host
->ops
->request(host
, mrq
);
136 EXPORT_SYMBOL(mmc_start_request
);
138 static void mmc_wait_done(struct mmc_request
*mrq
)
140 complete(mrq
->done_data
);
143 int mmc_wait_for_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
145 DECLARE_COMPLETION_ONSTACK(complete
);
147 mrq
->done_data
= &complete
;
148 mrq
->done
= mmc_wait_done
;
150 mmc_start_request(host
, mrq
);
152 wait_for_completion(&complete
);
157 EXPORT_SYMBOL(mmc_wait_for_req
);
160 * mmc_wait_for_cmd - start a command and wait for completion
161 * @host: MMC host to start command
162 * @cmd: MMC command to start
163 * @retries: maximum number of retries
165 * Start a new MMC command for a host, and wait for the command
166 * to complete. Return any error that occurred while the command
167 * was executing. Do not attempt to parse the response.
169 int mmc_wait_for_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
, int retries
)
171 struct mmc_request mrq
;
173 BUG_ON(!host
->claimed
);
175 memset(&mrq
, 0, sizeof(struct mmc_request
));
177 memset(cmd
->resp
, 0, sizeof(cmd
->resp
));
178 cmd
->retries
= retries
;
183 mmc_wait_for_req(host
, &mrq
);
188 EXPORT_SYMBOL(mmc_wait_for_cmd
);
191 * mmc_set_data_timeout - set the timeout for a data command
192 * @data: data phase for command
193 * @card: the MMC card associated with the data transfer
194 * @write: flag to differentiate reads from writes
196 void mmc_set_data_timeout(struct mmc_data
*data
, const struct mmc_card
*card
,
202 * SD cards use a 100 multiplier rather than 10
204 mult
= mmc_card_sd(card
) ? 100 : 10;
207 * Scale up the multiplier (and therefore the timeout) by
208 * the r2w factor for writes.
211 mult
<<= card
->csd
.r2w_factor
;
213 data
->timeout_ns
= card
->csd
.tacc_ns
* mult
;
214 data
->timeout_clks
= card
->csd
.tacc_clks
* mult
;
217 * SD cards also have an upper limit on the timeout.
219 if (mmc_card_sd(card
)) {
220 unsigned int timeout_us
, limit_us
;
222 timeout_us
= data
->timeout_ns
/ 1000;
223 timeout_us
+= data
->timeout_clks
* 1000 /
224 (card
->host
->ios
.clock
/ 1000);
232 * SDHC cards always use these fixed values.
234 if (timeout_us
> limit_us
|| mmc_card_blockaddr(card
)) {
235 data
->timeout_ns
= limit_us
* 1000;
236 data
->timeout_clks
= 0;
240 EXPORT_SYMBOL(mmc_set_data_timeout
);
243 * __mmc_claim_host - exclusively claim a host
244 * @host: mmc host to claim
245 * @card: mmc card to claim host for
247 * Claim a host for a set of operations. If a valid card
248 * is passed and this wasn't the last card selected, select
249 * the card before returning.
251 * Note: you should use mmc_card_claim_host or mmc_claim_host.
253 void mmc_claim_host(struct mmc_host
*host
)
255 DECLARE_WAITQUEUE(wait
, current
);
258 add_wait_queue(&host
->wq
, &wait
);
259 spin_lock_irqsave(&host
->lock
, flags
);
261 set_current_state(TASK_UNINTERRUPTIBLE
);
264 spin_unlock_irqrestore(&host
->lock
, flags
);
266 spin_lock_irqsave(&host
->lock
, flags
);
268 set_current_state(TASK_RUNNING
);
270 spin_unlock_irqrestore(&host
->lock
, flags
);
271 remove_wait_queue(&host
->wq
, &wait
);
274 EXPORT_SYMBOL(mmc_claim_host
);
277 * mmc_release_host - release a host
278 * @host: mmc host to release
280 * Release a MMC host, allowing others to claim the host
281 * for their operations.
283 void mmc_release_host(struct mmc_host
*host
)
287 BUG_ON(!host
->claimed
);
289 spin_lock_irqsave(&host
->lock
, flags
);
291 spin_unlock_irqrestore(&host
->lock
, flags
);
296 EXPORT_SYMBOL(mmc_release_host
);
299 * Internal function that does the actual ios call to the host driver,
300 * optionally printing some debug output.
302 static inline void mmc_set_ios(struct mmc_host
*host
)
304 struct mmc_ios
*ios
= &host
->ios
;
306 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
307 "width %u timing %u\n",
308 mmc_hostname(host
), ios
->clock
, ios
->bus_mode
,
309 ios
->power_mode
, ios
->chip_select
, ios
->vdd
,
310 ios
->bus_width
, ios
->timing
);
312 host
->ops
->set_ios(host
, ios
);
316 * Control chip select pin on a host.
318 void mmc_set_chip_select(struct mmc_host
*host
, int mode
)
320 host
->ios
.chip_select
= mode
;
325 * Sets the host clock to the highest possible frequency that
328 void mmc_set_clock(struct mmc_host
*host
, unsigned int hz
)
330 WARN_ON(hz
< host
->f_min
);
332 if (hz
> host
->f_max
)
335 host
->ios
.clock
= hz
;
340 * Change the bus mode (open drain/push-pull) of a host.
342 void mmc_set_bus_mode(struct mmc_host
*host
, unsigned int mode
)
344 host
->ios
.bus_mode
= mode
;
349 * Change data bus width of a host.
351 void mmc_set_bus_width(struct mmc_host
*host
, unsigned int width
)
353 host
->ios
.bus_width
= width
;
358 * Mask off any voltages we don't support and select
361 u32
mmc_select_voltage(struct mmc_host
*host
, u32 ocr
)
365 ocr
&= host
->ocr_avail
;
383 * Select timing parameters for host.
385 void mmc_set_timing(struct mmc_host
*host
, unsigned int timing
)
387 host
->ios
.timing
= timing
;
392 * Apply power to the MMC stack. This is a two-stage process.
393 * First, we enable power to the card without the clock running.
394 * We then wait a bit for the power to stabilise. Finally,
395 * enable the bus drivers and clock to the card.
397 * We must _NOT_ enable the clock prior to power stablising.
399 * If a host does all the power sequencing itself, ignore the
400 * initial MMC_POWER_UP stage.
402 static void mmc_power_up(struct mmc_host
*host
)
404 int bit
= fls(host
->ocr_avail
) - 1;
407 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
408 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
409 host
->ios
.power_mode
= MMC_POWER_UP
;
410 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
411 host
->ios
.timing
= MMC_TIMING_LEGACY
;
416 host
->ios
.clock
= host
->f_min
;
417 host
->ios
.power_mode
= MMC_POWER_ON
;
423 static void mmc_power_off(struct mmc_host
*host
)
427 host
->ios
.bus_mode
= MMC_BUSMODE_OPENDRAIN
;
428 host
->ios
.chip_select
= MMC_CS_DONTCARE
;
429 host
->ios
.power_mode
= MMC_POWER_OFF
;
430 host
->ios
.bus_width
= MMC_BUS_WIDTH_1
;
431 host
->ios
.timing
= MMC_TIMING_LEGACY
;
436 * Assign a mmc bus handler to a host. Only one bus handler may control a
437 * host at any given time.
439 void mmc_attach_bus(struct mmc_host
*host
, const struct mmc_bus_ops
*ops
)
446 BUG_ON(!host
->claimed
);
448 spin_lock_irqsave(&host
->lock
, flags
);
450 BUG_ON(host
->bus_ops
);
451 BUG_ON(host
->bus_refs
);
457 spin_unlock_irqrestore(&host
->lock
, flags
);
461 * Remove the current bus handler from a host. Assumes that there are
462 * no interesting cards left, so the bus is powered down.
464 void mmc_detach_bus(struct mmc_host
*host
)
470 BUG_ON(!host
->claimed
);
471 BUG_ON(!host
->bus_ops
);
473 spin_lock_irqsave(&host
->lock
, flags
);
477 spin_unlock_irqrestore(&host
->lock
, flags
);
485 * Cleanup when the last reference to the bus operator is dropped.
487 void __mmc_release_bus(struct mmc_host
*host
)
490 BUG_ON(host
->bus_refs
);
491 BUG_ON(!host
->bus_dead
);
493 host
->bus_ops
= NULL
;
497 * mmc_detect_change - process change of state on a MMC socket
498 * @host: host which changed state.
499 * @delay: optional delay to wait before detection (jiffies)
501 * All we know is that card(s) have been inserted or removed
502 * from the socket(s). We don't know which socket or cards.
504 void mmc_detect_change(struct mmc_host
*host
, unsigned long delay
)
506 #ifdef CONFIG_MMC_DEBUG
508 spin_lock_irqsave(&host
->lock
, flags
);
509 BUG_ON(host
->removed
);
510 spin_unlock_irqrestore(&host
->lock
, flags
);
513 mmc_schedule_delayed_work(&host
->detect
, delay
);
516 EXPORT_SYMBOL(mmc_detect_change
);
519 void mmc_rescan(struct work_struct
*work
)
521 struct mmc_host
*host
=
522 container_of(work
, struct mmc_host
, detect
.work
);
528 if (host
->bus_ops
== NULL
) {
530 * Only we can add a new handler, so it's safe to
531 * release the lock here.
535 mmc_claim_host(host
);
540 mmc_send_if_cond(host
, host
->ocr_avail
);
542 err
= mmc_send_app_op_cond(host
, 0, &ocr
);
543 if (err
== MMC_ERR_NONE
) {
544 if (mmc_attach_sd(host
, ocr
))
548 * If we fail to detect any SD cards then try
549 * searching for MMC cards.
551 err
= mmc_send_op_cond(host
, 0, &ocr
);
552 if (err
== MMC_ERR_NONE
) {
553 if (mmc_attach_mmc(host
, ocr
))
557 mmc_release_host(host
);
561 if (host
->bus_ops
->detect
&& !host
->bus_dead
)
562 host
->bus_ops
->detect(host
);
568 void mmc_start_host(struct mmc_host
*host
)
571 mmc_detect_change(host
, 0);
574 void mmc_stop_host(struct mmc_host
*host
)
576 #ifdef CONFIG_MMC_DEBUG
578 spin_lock_irqsave(&host
->lock
, flags
);
580 spin_unlock_irqrestore(&host
->lock
, flags
);
583 mmc_flush_scheduled_work();
586 if (host
->bus_ops
&& !host
->bus_dead
) {
587 if (host
->bus_ops
->remove
)
588 host
->bus_ops
->remove(host
);
590 mmc_claim_host(host
);
591 mmc_detach_bus(host
);
592 mmc_release_host(host
);
604 * mmc_suspend_host - suspend a host
606 * @state: suspend mode (PM_SUSPEND_xxx)
608 int mmc_suspend_host(struct mmc_host
*host
, pm_message_t state
)
610 mmc_flush_scheduled_work();
613 if (host
->bus_ops
&& !host
->bus_dead
) {
614 if (host
->bus_ops
->suspend
)
615 host
->bus_ops
->suspend(host
);
616 if (!host
->bus_ops
->resume
) {
617 if (host
->bus_ops
->remove
)
618 host
->bus_ops
->remove(host
);
620 mmc_claim_host(host
);
621 mmc_detach_bus(host
);
622 mmc_release_host(host
);
632 EXPORT_SYMBOL(mmc_suspend_host
);
635 * mmc_resume_host - resume a previously suspended host
638 int mmc_resume_host(struct mmc_host
*host
)
641 if (host
->bus_ops
&& !host
->bus_dead
) {
643 BUG_ON(!host
->bus_ops
->resume
);
644 host
->bus_ops
->resume(host
);
649 * We add a slight delay here so that resume can progress
652 mmc_detect_change(host
, 1);
657 EXPORT_SYMBOL(mmc_resume_host
);
661 static int __init
mmc_init(void)
665 workqueue
= create_singlethread_workqueue("kmmcd");
669 ret
= mmc_register_bus();
671 ret
= mmc_register_host_class();
673 mmc_unregister_bus();
678 static void __exit
mmc_exit(void)
680 mmc_unregister_host_class();
681 mmc_unregister_bus();
682 destroy_workqueue(workqueue
);
685 module_init(mmc_init
);
686 module_exit(mmc_exit
);
688 MODULE_LICENSE("GPL");
This page took 0.051202 seconds and 6 git commands to generate.