MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / mmc / core / core.c
blobbc0a8e7a1a83b1165b750e009d204f028ed4e653
1 /*
2 * linux/drivers/mmc/core/core.c
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
6 * Copyright (C) 2005-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>
29 #include "core.h"
30 #include "bus.h"
31 #include "host.h"
33 #include "mmc_ops.h"
34 #include "sd_ops.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,
46 unsigned long delay)
47 #else
48 static int mmc_schedule_delayed_work(struct work_struct *work,
49 unsigned long delay)
50 #endif
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);
63 /**
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;
74 int err = cmd->error;
76 if (err && cmd->retries) {
77 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
78 mmc_hostname(host), cmd->opcode, err);
80 cmd->retries--;
81 cmd->error = 0;
82 host->ops->request(host, mrq);
83 } else {
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]);
89 if (mrq->data) {
90 pr_debug("%s: %d bytes transferred: %d\n",
91 mmc_hostname(host),
92 mrq->data->bytes_xfered, mrq->data->error);
95 if (mrq->stop) {
96 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
97 mmc_hostname(host), mrq->stop->opcode,
98 mrq->stop->error,
99 mrq->stop->resp[0], mrq->stop->resp[1],
100 mrq->stop->resp[2], mrq->stop->resp[3]);
103 if (mrq->done)
104 mrq->done(mrq);
108 EXPORT_SYMBOL(mmc_request_done);
110 static void
111 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
113 #ifdef CONFIG_MMC_DEBUG
114 unsigned int i, sz;
115 #endif
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);
121 if (mrq->data) {
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);
130 if (mrq->stop) {
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);
138 mrq->cmd->error = 0;
139 mrq->cmd->mrq = mrq;
140 if (mrq->data) {
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 >
144 host->max_req_size);
146 #ifdef CONFIG_MMC_DEBUG
147 sz = 0;
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);
151 #endif
153 mrq->cmd->data = mrq->data;
154 mrq->data->error = 0;
155 mrq->data->mrq = mrq;
156 if (mrq->stop) {
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
177 * response.
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;
214 mrq.cmd = cmd;
215 cmd->data = NULL;
217 mmc_wait_for_req(host, &mrq);
219 return cmd->error;
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,
234 int write)
236 unsigned int mult;
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.
247 if (write)
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);
263 if (write)
264 limit_us = 250000;
265 else
266 limit_us = 100000;
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);
288 unsigned long flags;
290 might_sleep();
292 add_wait_queue(&host->wq, &wait);
293 spin_lock_irqsave(&host->lock, flags);
294 while (1) {
295 set_current_state(TASK_UNINTERRUPTIBLE);
296 if (!host->claimed)
297 break;
298 spin_unlock_irqrestore(&host->lock, flags);
299 schedule();
300 spin_lock_irqsave(&host->lock, flags);
302 set_current_state(TASK_RUNNING);
303 host->claimed = 1;
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)
319 unsigned long flags;
321 BUG_ON(!host->claimed);
323 spin_lock_irqsave(&host->lock, flags);
324 host->claimed = 0;
325 spin_unlock_irqrestore(&host->lock, flags);
327 wake_up(&host->wq);
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;
355 mmc_set_ios(host);
359 * Sets the host clock to the highest possible frequency that
360 * is below "hz".
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)
367 hz = host->f_max;
369 host->ios.clock = hz;
370 mmc_set_ios(host);
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;
379 mmc_set_ios(host);
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;
388 mmc_set_ios(host);
392 * Mask off any voltages we don't support and select
393 * the lowest voltage
395 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
397 int bit;
399 ocr &= host->ocr_avail;
401 bit = ffs(ocr);
402 if (bit) {
403 bit -= 1;
405 ocr &= 3 << bit;
407 host->ios.vdd = bit;
408 mmc_set_ios(host);
409 } else {
410 ocr = 0;
413 return ocr;
417 * Select timing parameters for host.
419 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
421 host->ios.timing = timing;
422 mmc_set_ios(host);
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;
440 host->ios.vdd = bit;
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;
446 mmc_set_ios(host);
448 mmc_delay(1);
450 host->ios.clock = host->f_min;
451 host->ios.power_mode = MMC_POWER_ON;
452 mmc_set_ios(host);
454 mmc_delay(2);
457 static void mmc_power_off(struct mmc_host *host)
459 host->ios.clock = 0;
460 host->ios.vdd = 0;
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;
466 mmc_set_ios(host);
470 * Cleanup when the last reference to the bus operator is dropped.
472 void __mmc_release_bus(struct mmc_host *host)
474 BUG_ON(!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)
486 unsigned long flags;
488 spin_lock_irqsave(&host->lock, flags);
489 host->bus_refs++;
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)
499 unsigned long flags;
501 spin_lock_irqsave(&host->lock, flags);
502 host->bus_refs--;
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)
515 unsigned long flags;
517 BUG_ON(!host);
518 BUG_ON(!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);
527 host->bus_ops = ops;
528 host->bus_refs = 1;
529 host->bus_dead = 0;
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)
540 unsigned long flags;
542 BUG_ON(!host);
544 BUG_ON(!host->claimed);
545 BUG_ON(!host->bus_ops);
547 spin_lock_irqsave(&host->lock, flags);
549 host->bus_dead = 1;
551 spin_unlock_irqrestore(&host->lock, flags);
553 mmc_power_off(host);
555 mmc_bus_put(host);
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
566 * inserted.
568 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
570 #ifdef CONFIG_MMC_DEBUG
571 unsigned long flags;
572 spin_lock_irqsave(&host->lock, flags);
573 BUG_ON(host->removed);
574 spin_unlock_irqrestore(&host->lock, flags);
575 #endif
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)
585 #else
586 void mmc_rescan(void *data)
587 #endif
589 #if 0 // mask by Victor Yu. 12-02-2008
590 struct mmc_host *host =
591 container_of(work, struct mmc_host, detect.work);
592 #else
593 struct mmc_host *host = data;
594 #endif
595 u32 ocr;
596 int err;
598 #if 1 // add by Victor Yu. 12-03-2008
599 if ( host == NULL )
600 return;
601 #endif
602 mmc_bus_get(host);
604 if (host->bus_ops == NULL) {
606 * Only we can add a new handler, so it's safe to
607 * release the lock here.
609 mmc_bus_put(host);
611 mmc_claim_host(host);
613 mmc_power_up(host);
614 mmc_go_idle(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)) {
621 mmc_power_off(host);
623 } else {
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)) {
631 mmc_power_off(host);
633 } else {
634 mmc_power_off(host);
635 mmc_release_host(host);
638 } else {
639 if (host->bus_ops->detect && !host->bus_dead) {
640 host->bus_ops->detect(host);
643 mmc_bus_put(host);
647 void mmc_start_host(struct mmc_host *host)
649 mmc_power_off(host);
650 mmc_detect_change(host, 0);
653 void mmc_stop_host(struct mmc_host *host)
655 #ifdef CONFIG_MMC_DEBUG
656 unsigned long flags;
657 spin_lock_irqsave(&host->lock, flags);
658 host->removed = 1;
659 spin_unlock_irqrestore(&host->lock, flags);
660 #endif
662 mmc_flush_scheduled_work();
664 mmc_bus_get(host);
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);
673 mmc_bus_put(host);
675 BUG_ON(host->card);
677 mmc_power_off(host);
680 #ifdef CONFIG_PM
683 * mmc_suspend_host - suspend a host
684 * @host: mmc 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();
691 mmc_bus_get(host);
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);
704 mmc_bus_put(host);
706 mmc_power_off(host);
708 return 0;
711 EXPORT_SYMBOL(mmc_suspend_host);
714 * mmc_resume_host - resume a previously suspended host
715 * @host: mmc host
717 int mmc_resume_host(struct mmc_host *host)
719 mmc_bus_get(host);
720 if (host->bus_ops && !host->bus_dead) {
721 mmc_power_up(host);
722 BUG_ON(!host->bus_ops->resume);
723 host->bus_ops->resume(host);
725 mmc_bus_put(host);
728 * We add a slight delay here so that resume can progress
729 * in parallel.
731 mmc_detect_change(host, 1);
733 return 0;
736 EXPORT_SYMBOL(mmc_resume_host);
738 #endif
740 static int __init mmc_init(void)
742 int ret;
744 workqueue = create_singlethread_workqueue("kmmcd");
745 if (!workqueue)
746 return -ENOMEM;
748 ret = mmc_register_bus();
749 if (ret == 0) {
750 ret = mmc_register_host_class();
751 if (ret)
752 mmc_unregister_bus();
754 return ret;
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");