mmc: mmc_set_data_timeout() parameter write is redundant
[linux-2.6/kvm.git] / drivers / mmc / core / core.c
blob51e611f2f33d302e8c552bcd4f653a4e830005e2
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 static int mmc_schedule_delayed_work(struct delayed_work *work,
45 unsigned long delay)
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);
58 /**
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;
69 int err = cmd->error;
71 if (err && cmd->retries) {
72 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
73 mmc_hostname(host), cmd->opcode, err);
75 cmd->retries--;
76 cmd->error = 0;
77 host->ops->request(host, mrq);
78 } else {
79 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
80 mmc_hostname(host), cmd->opcode, err,
81 cmd->resp[0], cmd->resp[1],
82 cmd->resp[2], cmd->resp[3]);
84 if (mrq->data) {
85 pr_debug("%s: %d bytes transferred: %d\n",
86 mmc_hostname(host),
87 mrq->data->bytes_xfered, mrq->data->error);
90 if (mrq->stop) {
91 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
92 mmc_hostname(host), mrq->stop->opcode,
93 mrq->stop->error,
94 mrq->stop->resp[0], mrq->stop->resp[1],
95 mrq->stop->resp[2], mrq->stop->resp[3]);
98 if (mrq->done)
99 mrq->done(mrq);
103 EXPORT_SYMBOL(mmc_request_done);
105 static void
106 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
108 #ifdef CONFIG_MMC_DEBUG
109 unsigned int i, sz;
110 #endif
112 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
113 mmc_hostname(host), mrq->cmd->opcode,
114 mrq->cmd->arg, mrq->cmd->flags);
116 if (mrq->data) {
117 pr_debug("%s: blksz %d blocks %d flags %08x "
118 "tsac %d ms nsac %d\n",
119 mmc_hostname(host), mrq->data->blksz,
120 mrq->data->blocks, mrq->data->flags,
121 mrq->data->timeout_ns / 10000000,
122 mrq->data->timeout_clks);
125 if (mrq->stop) {
126 pr_debug("%s: CMD%u arg %08x flags %08x\n",
127 mmc_hostname(host), mrq->stop->opcode,
128 mrq->stop->arg, mrq->stop->flags);
131 WARN_ON(!host->claimed);
133 mrq->cmd->error = 0;
134 mrq->cmd->mrq = mrq;
135 if (mrq->data) {
136 BUG_ON(mrq->data->blksz > host->max_blk_size);
137 BUG_ON(mrq->data->blocks > host->max_blk_count);
138 BUG_ON(mrq->data->blocks * mrq->data->blksz >
139 host->max_req_size);
141 #ifdef CONFIG_MMC_DEBUG
142 sz = 0;
143 for (i = 0;i < mrq->data->sg_len;i++)
144 sz += mrq->data->sg[i].length;
145 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
146 #endif
148 mrq->cmd->data = mrq->data;
149 mrq->data->error = 0;
150 mrq->data->mrq = mrq;
151 if (mrq->stop) {
152 mrq->data->stop = mrq->stop;
153 mrq->stop->error = 0;
154 mrq->stop->mrq = mrq;
157 host->ops->request(host, mrq);
160 static void mmc_wait_done(struct mmc_request *mrq)
162 complete(mrq->done_data);
166 * mmc_wait_for_req - start a request and wait for completion
167 * @host: MMC host to start command
168 * @mrq: MMC request to start
170 * Start a new MMC custom command request for a host, and wait
171 * for the command to complete. Does not attempt to parse the
172 * response.
174 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
176 DECLARE_COMPLETION_ONSTACK(complete);
178 mrq->done_data = &complete;
179 mrq->done = mmc_wait_done;
181 mmc_start_request(host, mrq);
183 wait_for_completion(&complete);
186 EXPORT_SYMBOL(mmc_wait_for_req);
189 * mmc_wait_for_cmd - start a command and wait for completion
190 * @host: MMC host to start command
191 * @cmd: MMC command to start
192 * @retries: maximum number of retries
194 * Start a new MMC command for a host, and wait for the command
195 * to complete. Return any error that occurred while the command
196 * was executing. Do not attempt to parse the response.
198 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
200 struct mmc_request mrq;
202 BUG_ON(!host->claimed);
204 memset(&mrq, 0, sizeof(struct mmc_request));
206 memset(cmd->resp, 0, sizeof(cmd->resp));
207 cmd->retries = retries;
209 mrq.cmd = cmd;
210 cmd->data = NULL;
212 mmc_wait_for_req(host, &mrq);
214 return cmd->error;
217 EXPORT_SYMBOL(mmc_wait_for_cmd);
220 * mmc_set_data_timeout - set the timeout for a data command
221 * @data: data phase for command
222 * @card: the MMC card associated with the data transfer
224 * Computes the data timeout parameters according to the
225 * correct algorithm given the card type.
227 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
229 unsigned int mult;
232 * SD cards use a 100 multiplier rather than 10
234 mult = mmc_card_sd(card) ? 100 : 10;
237 * Scale up the multiplier (and therefore the timeout) by
238 * the r2w factor for writes.
240 if (data->flags & MMC_DATA_WRITE)
241 mult <<= card->csd.r2w_factor;
243 data->timeout_ns = card->csd.tacc_ns * mult;
244 data->timeout_clks = card->csd.tacc_clks * mult;
247 * SD cards also have an upper limit on the timeout.
249 if (mmc_card_sd(card)) {
250 unsigned int timeout_us, limit_us;
252 timeout_us = data->timeout_ns / 1000;
253 timeout_us += data->timeout_clks * 1000 /
254 (card->host->ios.clock / 1000);
256 if (data->flags & MMC_DATA_WRITE)
257 limit_us = 250000;
258 else
259 limit_us = 100000;
262 * SDHC cards always use these fixed values.
264 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
265 data->timeout_ns = limit_us * 1000;
266 data->timeout_clks = 0;
270 EXPORT_SYMBOL(mmc_set_data_timeout);
273 * mmc_claim_host - exclusively claim a host
274 * @host: mmc host to claim
276 * Claim a host for a set of operations.
278 void mmc_claim_host(struct mmc_host *host)
280 DECLARE_WAITQUEUE(wait, current);
281 unsigned long flags;
283 might_sleep();
285 add_wait_queue(&host->wq, &wait);
286 spin_lock_irqsave(&host->lock, flags);
287 while (1) {
288 set_current_state(TASK_UNINTERRUPTIBLE);
289 if (!host->claimed)
290 break;
291 spin_unlock_irqrestore(&host->lock, flags);
292 schedule();
293 spin_lock_irqsave(&host->lock, flags);
295 set_current_state(TASK_RUNNING);
296 host->claimed = 1;
297 spin_unlock_irqrestore(&host->lock, flags);
298 remove_wait_queue(&host->wq, &wait);
301 EXPORT_SYMBOL(mmc_claim_host);
304 * mmc_release_host - release a host
305 * @host: mmc host to release
307 * Release a MMC host, allowing others to claim the host
308 * for their operations.
310 void mmc_release_host(struct mmc_host *host)
312 unsigned long flags;
314 BUG_ON(!host->claimed);
316 spin_lock_irqsave(&host->lock, flags);
317 host->claimed = 0;
318 spin_unlock_irqrestore(&host->lock, flags);
320 wake_up(&host->wq);
323 EXPORT_SYMBOL(mmc_release_host);
326 * Internal function that does the actual ios call to the host driver,
327 * optionally printing some debug output.
329 static inline void mmc_set_ios(struct mmc_host *host)
331 struct mmc_ios *ios = &host->ios;
333 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
334 "width %u timing %u\n",
335 mmc_hostname(host), ios->clock, ios->bus_mode,
336 ios->power_mode, ios->chip_select, ios->vdd,
337 ios->bus_width, ios->timing);
339 host->ops->set_ios(host, ios);
343 * Control chip select pin on a host.
345 void mmc_set_chip_select(struct mmc_host *host, int mode)
347 host->ios.chip_select = mode;
348 mmc_set_ios(host);
352 * Sets the host clock to the highest possible frequency that
353 * is below "hz".
355 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
357 WARN_ON(hz < host->f_min);
359 if (hz > host->f_max)
360 hz = host->f_max;
362 host->ios.clock = hz;
363 mmc_set_ios(host);
367 * Change the bus mode (open drain/push-pull) of a host.
369 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
371 host->ios.bus_mode = mode;
372 mmc_set_ios(host);
376 * Change data bus width of a host.
378 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
380 host->ios.bus_width = width;
381 mmc_set_ios(host);
385 * Mask off any voltages we don't support and select
386 * the lowest voltage
388 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
390 int bit;
392 ocr &= host->ocr_avail;
394 bit = ffs(ocr);
395 if (bit) {
396 bit -= 1;
398 ocr &= 3 << bit;
400 host->ios.vdd = bit;
401 mmc_set_ios(host);
402 } else {
403 ocr = 0;
406 return ocr;
410 * Select timing parameters for host.
412 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
414 host->ios.timing = timing;
415 mmc_set_ios(host);
419 * Apply power to the MMC stack. This is a two-stage process.
420 * First, we enable power to the card without the clock running.
421 * We then wait a bit for the power to stabilise. Finally,
422 * enable the bus drivers and clock to the card.
424 * We must _NOT_ enable the clock prior to power stablising.
426 * If a host does all the power sequencing itself, ignore the
427 * initial MMC_POWER_UP stage.
429 static void mmc_power_up(struct mmc_host *host)
431 int bit = fls(host->ocr_avail) - 1;
433 host->ios.vdd = bit;
434 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
435 host->ios.chip_select = MMC_CS_DONTCARE;
436 host->ios.power_mode = MMC_POWER_UP;
437 host->ios.bus_width = MMC_BUS_WIDTH_1;
438 host->ios.timing = MMC_TIMING_LEGACY;
439 mmc_set_ios(host);
441 mmc_delay(1);
443 host->ios.clock = host->f_min;
444 host->ios.power_mode = MMC_POWER_ON;
445 mmc_set_ios(host);
447 mmc_delay(2);
450 static void mmc_power_off(struct mmc_host *host)
452 host->ios.clock = 0;
453 host->ios.vdd = 0;
454 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
455 host->ios.chip_select = MMC_CS_DONTCARE;
456 host->ios.power_mode = MMC_POWER_OFF;
457 host->ios.bus_width = MMC_BUS_WIDTH_1;
458 host->ios.timing = MMC_TIMING_LEGACY;
459 mmc_set_ios(host);
463 * Cleanup when the last reference to the bus operator is dropped.
465 void __mmc_release_bus(struct mmc_host *host)
467 BUG_ON(!host);
468 BUG_ON(host->bus_refs);
469 BUG_ON(!host->bus_dead);
471 host->bus_ops = NULL;
475 * Increase reference count of bus operator
477 static inline void mmc_bus_get(struct mmc_host *host)
479 unsigned long flags;
481 spin_lock_irqsave(&host->lock, flags);
482 host->bus_refs++;
483 spin_unlock_irqrestore(&host->lock, flags);
487 * Decrease reference count of bus operator and free it if
488 * it is the last reference.
490 static inline void mmc_bus_put(struct mmc_host *host)
492 unsigned long flags;
494 spin_lock_irqsave(&host->lock, flags);
495 host->bus_refs--;
496 if ((host->bus_refs == 0) && host->bus_ops)
497 __mmc_release_bus(host);
498 spin_unlock_irqrestore(&host->lock, flags);
502 * Assign a mmc bus handler to a host. Only one bus handler may control a
503 * host at any given time.
505 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
507 unsigned long flags;
509 BUG_ON(!host);
510 BUG_ON(!ops);
512 BUG_ON(!host->claimed);
514 spin_lock_irqsave(&host->lock, flags);
516 BUG_ON(host->bus_ops);
517 BUG_ON(host->bus_refs);
519 host->bus_ops = ops;
520 host->bus_refs = 1;
521 host->bus_dead = 0;
523 spin_unlock_irqrestore(&host->lock, flags);
527 * Remove the current bus handler from a host. Assumes that there are
528 * no interesting cards left, so the bus is powered down.
530 void mmc_detach_bus(struct mmc_host *host)
532 unsigned long flags;
534 BUG_ON(!host);
536 BUG_ON(!host->claimed);
537 BUG_ON(!host->bus_ops);
539 spin_lock_irqsave(&host->lock, flags);
541 host->bus_dead = 1;
543 spin_unlock_irqrestore(&host->lock, flags);
545 mmc_power_off(host);
547 mmc_bus_put(host);
551 * mmc_detect_change - process change of state on a MMC socket
552 * @host: host which changed state.
553 * @delay: optional delay to wait before detection (jiffies)
555 * MMC drivers should call this when they detect a card has been
556 * inserted or removed. The MMC layer will confirm that any
557 * present card is still functional, and initialize any newly
558 * inserted.
560 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
562 #ifdef CONFIG_MMC_DEBUG
563 unsigned long flags;
564 spin_lock_irqsave(&host->lock, flags);
565 BUG_ON(host->removed);
566 spin_unlock_irqrestore(&host->lock, flags);
567 #endif
569 mmc_schedule_delayed_work(&host->detect, delay);
572 EXPORT_SYMBOL(mmc_detect_change);
575 void mmc_rescan(struct work_struct *work)
577 struct mmc_host *host =
578 container_of(work, struct mmc_host, detect.work);
579 u32 ocr;
580 int err;
582 mmc_bus_get(host);
584 if (host->bus_ops == NULL) {
586 * Only we can add a new handler, so it's safe to
587 * release the lock here.
589 mmc_bus_put(host);
591 mmc_claim_host(host);
593 mmc_power_up(host);
594 mmc_go_idle(host);
596 mmc_send_if_cond(host, host->ocr_avail);
598 err = mmc_send_app_op_cond(host, 0, &ocr);
599 if (!err) {
600 if (mmc_attach_sd(host, ocr))
601 mmc_power_off(host);
602 } else {
604 * If we fail to detect any SD cards then try
605 * searching for MMC cards.
607 err = mmc_send_op_cond(host, 0, &ocr);
608 if (!err) {
609 if (mmc_attach_mmc(host, ocr))
610 mmc_power_off(host);
611 } else {
612 mmc_power_off(host);
613 mmc_release_host(host);
616 } else {
617 if (host->bus_ops->detect && !host->bus_dead)
618 host->bus_ops->detect(host);
620 mmc_bus_put(host);
624 void mmc_start_host(struct mmc_host *host)
626 mmc_power_off(host);
627 mmc_detect_change(host, 0);
630 void mmc_stop_host(struct mmc_host *host)
632 #ifdef CONFIG_MMC_DEBUG
633 unsigned long flags;
634 spin_lock_irqsave(&host->lock, flags);
635 host->removed = 1;
636 spin_unlock_irqrestore(&host->lock, flags);
637 #endif
639 mmc_flush_scheduled_work();
641 mmc_bus_get(host);
642 if (host->bus_ops && !host->bus_dead) {
643 if (host->bus_ops->remove)
644 host->bus_ops->remove(host);
646 mmc_claim_host(host);
647 mmc_detach_bus(host);
648 mmc_release_host(host);
650 mmc_bus_put(host);
652 BUG_ON(host->card);
654 mmc_power_off(host);
657 #ifdef CONFIG_PM
660 * mmc_suspend_host - suspend a host
661 * @host: mmc host
662 * @state: suspend mode (PM_SUSPEND_xxx)
664 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
666 mmc_flush_scheduled_work();
668 mmc_bus_get(host);
669 if (host->bus_ops && !host->bus_dead) {
670 if (host->bus_ops->suspend)
671 host->bus_ops->suspend(host);
672 if (!host->bus_ops->resume) {
673 if (host->bus_ops->remove)
674 host->bus_ops->remove(host);
676 mmc_claim_host(host);
677 mmc_detach_bus(host);
678 mmc_release_host(host);
681 mmc_bus_put(host);
683 mmc_power_off(host);
685 return 0;
688 EXPORT_SYMBOL(mmc_suspend_host);
691 * mmc_resume_host - resume a previously suspended host
692 * @host: mmc host
694 int mmc_resume_host(struct mmc_host *host)
696 mmc_bus_get(host);
697 if (host->bus_ops && !host->bus_dead) {
698 mmc_power_up(host);
699 BUG_ON(!host->bus_ops->resume);
700 host->bus_ops->resume(host);
702 mmc_bus_put(host);
705 * We add a slight delay here so that resume can progress
706 * in parallel.
708 mmc_detect_change(host, 1);
710 return 0;
713 EXPORT_SYMBOL(mmc_resume_host);
715 #endif
717 static int __init mmc_init(void)
719 int ret;
721 workqueue = create_singlethread_workqueue("kmmcd");
722 if (!workqueue)
723 return -ENOMEM;
725 ret = mmc_register_bus();
726 if (ret == 0) {
727 ret = mmc_register_host_class();
728 if (ret)
729 mmc_unregister_bus();
731 return ret;
734 static void __exit mmc_exit(void)
736 mmc_unregister_host_class();
737 mmc_unregister_bus();
738 destroy_workqueue(workqueue);
741 module_init(mmc_init);
742 module_exit(mmc_exit);
744 MODULE_LICENSE("GPL");