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 if (err && cmd->retries) {
72 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
73 mmc_hostname(host), cmd->opcode, err);
77 host->ops->request(host, mrq);
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]);
85 pr_debug("%s: %d bytes transferred: %d\n",
87 mrq->data->bytes_xfered, mrq->data->error);
91 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
92 mmc_hostname(host), mrq->stop->opcode,
94 mrq->stop->resp[0], mrq->stop->resp[1],
95 mrq->stop->resp[2], mrq->stop->resp[3]);
103 EXPORT_SYMBOL(mmc_request_done);
106 * mmc_start_request - start a command on a host
107 * @host: MMC host to start command on
108 * @mrq: MMC request to start
110 * Queue a command on the specified host. We expect the
111 * caller to be holding the host lock with interrupts disabled.
114 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
116 #ifdef CONFIG_MMC_DEBUG
120 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
121 mmc_hostname(host), mrq->cmd->opcode,
122 mrq->cmd->arg, mrq->cmd->flags);
125 pr_debug("%s: blksz %d blocks %d flags %08x "
126 "tsac %d ms nsac %d\n",
127 mmc_hostname(host), mrq->data->blksz,
128 mrq->data->blocks, mrq->data->flags,
129 mrq->data->timeout_ns / 10000000,
130 mrq->data->timeout_clks);
134 pr_debug("%s: CMD%u arg %08x flags %08x\n",
135 mmc_hostname(host), mrq->stop->opcode,
136 mrq->stop->arg, mrq->stop->flags);
139 WARN_ON(!host->claimed);
144 BUG_ON(mrq->data->blksz > host->max_blk_size);
145 BUG_ON(mrq->data->blocks > host->max_blk_count);
146 BUG_ON(mrq->data->blocks * mrq->data->blksz >
149 #ifdef CONFIG_MMC_DEBUG
151 for (i = 0;i < mrq->data->sg_len;i++)
152 sz += mrq->data->sg[i].length;
153 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
156 mrq->cmd->data = mrq->data;
157 mrq->data->error = 0;
158 mrq->data->mrq = mrq;
160 mrq->data->stop = mrq->stop;
161 mrq->stop->error = 0;
162 mrq->stop->mrq = mrq;
165 host->ops->request(host, mrq);
168 EXPORT_SYMBOL(mmc_start_request);
170 static void mmc_wait_done(struct mmc_request *mrq)
172 complete(mrq->done_data);
176 * mmc_wait_for_req - start a request and wait for completion
177 * @host: MMC host to start command
178 * @mrq: MMC request to start
180 * Start a new MMC custom command request for a host, and wait
181 * for the command to complete. Does not attempt to parse the
184 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
186 DECLARE_COMPLETION_ONSTACK(complete);
188 mrq->done_data = &complete;
189 mrq->done = mmc_wait_done;
191 mmc_start_request(host, mrq);
193 wait_for_completion(&complete);
196 EXPORT_SYMBOL(mmc_wait_for_req);
199 * mmc_wait_for_cmd - start a command and wait for completion
200 * @host: MMC host to start command
201 * @cmd: MMC command to start
202 * @retries: maximum number of retries
204 * Start a new MMC command for a host, and wait for the command
205 * to complete. Return any error that occurred while the command
206 * was executing. Do not attempt to parse the response.
208 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
210 struct mmc_request mrq;
212 BUG_ON(!host->claimed);
214 memset(&mrq, 0, sizeof(struct mmc_request));
216 memset(cmd->resp, 0, sizeof(cmd->resp));
217 cmd->retries = retries;
222 mmc_wait_for_req(host, &mrq);
227 EXPORT_SYMBOL(mmc_wait_for_cmd);
230 * mmc_set_data_timeout - set the timeout for a data command
231 * @data: data phase for command
232 * @card: the MMC card associated with the data transfer
233 * @write: flag to differentiate reads from writes
235 * Computes the data timeout parameters according to the
236 * correct algorithm given the card type.
238 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
244 * SD cards use a 100 multiplier rather than 10
246 mult = mmc_card_sd(card) ? 100 : 10;
249 * Scale up the multiplier (and therefore the timeout) by
250 * the r2w factor for writes.
253 mult <<= card->csd.r2w_factor;
255 data->timeout_ns = card->csd.tacc_ns * mult;
256 data->timeout_clks = card->csd.tacc_clks * mult;
259 * SD cards also have an upper limit on the timeout.
261 if (mmc_card_sd(card)) {
262 unsigned int timeout_us, limit_us;
264 timeout_us = data->timeout_ns / 1000;
265 timeout_us += data->timeout_clks * 1000 /
266 (card->host->ios.clock / 1000);
274 * SDHC cards always use these fixed values.
276 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
277 data->timeout_ns = limit_us * 1000;
278 data->timeout_clks = 0;
282 EXPORT_SYMBOL(mmc_set_data_timeout);
285 * mmc_claim_host - exclusively claim a host
286 * @host: mmc host to claim
288 * Claim a host for a set of operations.
290 void mmc_claim_host(struct mmc_host *host)
292 DECLARE_WAITQUEUE(wait, current);
297 add_wait_queue(&host->wq, &wait);
298 spin_lock_irqsave(&host->lock, flags);
300 set_current_state(TASK_UNINTERRUPTIBLE);
303 spin_unlock_irqrestore(&host->lock, flags);
305 spin_lock_irqsave(&host->lock, flags);
307 set_current_state(TASK_RUNNING);
309 spin_unlock_irqrestore(&host->lock, flags);
310 remove_wait_queue(&host->wq, &wait);
313 EXPORT_SYMBOL(mmc_claim_host);
316 * mmc_release_host - release a host
317 * @host: mmc host to release
319 * Release a MMC host, allowing others to claim the host
320 * for their operations.
322 void mmc_release_host(struct mmc_host *host)
326 BUG_ON(!host->claimed);
328 spin_lock_irqsave(&host->lock, flags);
330 spin_unlock_irqrestore(&host->lock, flags);
335 EXPORT_SYMBOL(mmc_release_host);
338 * Internal function that does the actual ios call to the host driver,
339 * optionally printing some debug output.
341 static inline void mmc_set_ios(struct mmc_host *host)
343 struct mmc_ios *ios = &host->ios;
345 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
346 "width %u timing %u\n",
347 mmc_hostname(host), ios->clock, ios->bus_mode,
348 ios->power_mode, ios->chip_select, ios->vdd,
349 ios->bus_width, ios->timing);
351 host->ops->set_ios(host, ios);
355 * Control chip select pin on a host.
357 void mmc_set_chip_select(struct mmc_host *host, int mode)
359 host->ios.chip_select = mode;
364 * Sets the host clock to the highest possible frequency that
367 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
369 WARN_ON(hz < host->f_min);
371 if (hz > host->f_max)
374 host->ios.clock = hz;
379 * Change the bus mode (open drain/push-pull) of a host.
381 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
383 host->ios.bus_mode = mode;
388 * Change data bus width of a host.
390 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
392 host->ios.bus_width = width;
397 * Mask off any voltages we don't support and select
400 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
404 ocr &= host->ocr_avail;
422 * Select timing parameters for host.
424 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
426 host->ios.timing = timing;
431 * Apply power to the MMC stack. This is a two-stage process.
432 * First, we enable power to the card without the clock running.
433 * We then wait a bit for the power to stabilise. Finally,
434 * enable the bus drivers and clock to the card.
436 * We must _NOT_ enable the clock prior to power stablising.
438 * If a host does all the power sequencing itself, ignore the
439 * initial MMC_POWER_UP stage.
441 static void mmc_power_up(struct mmc_host *host)
443 int bit = fls(host->ocr_avail) - 1;
446 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
447 host->ios.chip_select = MMC_CS_DONTCARE;
448 host->ios.power_mode = MMC_POWER_UP;
449 host->ios.bus_width = MMC_BUS_WIDTH_1;
450 host->ios.timing = MMC_TIMING_LEGACY;
455 host->ios.clock = host->f_min;
456 host->ios.power_mode = MMC_POWER_ON;
462 static void mmc_power_off(struct mmc_host *host)
466 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
467 host->ios.chip_select = MMC_CS_DONTCARE;
468 host->ios.power_mode = MMC_POWER_OFF;
469 host->ios.bus_width = MMC_BUS_WIDTH_1;
470 host->ios.timing = MMC_TIMING_LEGACY;
475 * Assign a mmc bus handler to a host. Only one bus handler may control a
476 * host at any given time.
478 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
485 BUG_ON(!host->claimed);
487 spin_lock_irqsave(&host->lock, flags);
489 BUG_ON(host->bus_ops);
490 BUG_ON(host->bus_refs);
496 spin_unlock_irqrestore(&host->lock, flags);
500 * Remove the current bus handler from a host. Assumes that there are
501 * no interesting cards left, so the bus is powered down.
503 void mmc_detach_bus(struct mmc_host *host)
509 BUG_ON(!host->claimed);
510 BUG_ON(!host->bus_ops);
512 spin_lock_irqsave(&host->lock, flags);
516 spin_unlock_irqrestore(&host->lock, flags);
524 * Cleanup when the last reference to the bus operator is dropped.
526 void __mmc_release_bus(struct mmc_host *host)
529 BUG_ON(host->bus_refs);
530 BUG_ON(!host->bus_dead);
532 host->bus_ops = NULL;
536 * mmc_detect_change - process change of state on a MMC socket
537 * @host: host which changed state.
538 * @delay: optional delay to wait before detection (jiffies)
540 * MMC drivers should call this when they detect a card has been
541 * inserted or removed. The MMC layer will confirm that any
542 * present card is still functional, and initialize any newly
545 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
547 #ifdef CONFIG_MMC_DEBUG
549 spin_lock_irqsave(&host->lock, flags);
550 BUG_ON(host->removed);
551 spin_unlock_irqrestore(&host->lock, flags);
554 mmc_schedule_delayed_work(&host->detect, delay);
557 EXPORT_SYMBOL(mmc_detect_change);
560 void mmc_rescan(struct work_struct *work)
562 struct mmc_host *host =
563 container_of(work, struct mmc_host, detect.work);
569 if (host->bus_ops == NULL) {
571 * Only we can add a new handler, so it's safe to
572 * release the lock here.
576 mmc_claim_host(host);
581 mmc_send_if_cond(host, host->ocr_avail);
583 err = mmc_send_app_op_cond(host, 0, &ocr);
584 if (err == MMC_ERR_NONE) {
585 if (mmc_attach_sd(host, ocr))
589 * If we fail to detect any SD cards then try
590 * searching for MMC cards.
592 err = mmc_send_op_cond(host, 0, &ocr);
593 if (err == MMC_ERR_NONE) {
594 if (mmc_attach_mmc(host, ocr))
598 mmc_release_host(host);
602 if (host->bus_ops->detect && !host->bus_dead)
603 host->bus_ops->detect(host);
609 void mmc_start_host(struct mmc_host *host)
612 mmc_detect_change(host, 0);
615 void mmc_stop_host(struct mmc_host *host)
617 #ifdef CONFIG_MMC_DEBUG
619 spin_lock_irqsave(&host->lock, flags);
621 spin_unlock_irqrestore(&host->lock, flags);
624 mmc_flush_scheduled_work();
627 if (host->bus_ops && !host->bus_dead) {
628 if (host->bus_ops->remove)
629 host->bus_ops->remove(host);
631 mmc_claim_host(host);
632 mmc_detach_bus(host);
633 mmc_release_host(host);
645 * mmc_suspend_host - suspend a host
647 * @state: suspend mode (PM_SUSPEND_xxx)
649 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
651 mmc_flush_scheduled_work();
654 if (host->bus_ops && !host->bus_dead) {
655 if (host->bus_ops->suspend)
656 host->bus_ops->suspend(host);
657 if (!host->bus_ops->resume) {
658 if (host->bus_ops->remove)
659 host->bus_ops->remove(host);
661 mmc_claim_host(host);
662 mmc_detach_bus(host);
663 mmc_release_host(host);
673 EXPORT_SYMBOL(mmc_suspend_host);
676 * mmc_resume_host - resume a previously suspended host
679 int mmc_resume_host(struct mmc_host *host)
682 if (host->bus_ops && !host->bus_dead) {
684 BUG_ON(!host->bus_ops->resume);
685 host->bus_ops->resume(host);
690 * We add a slight delay here so that resume can progress
693 mmc_detect_change(host, 1);
698 EXPORT_SYMBOL(mmc_resume_host);
702 static int __init mmc_init(void)
706 workqueue = create_singlethread_workqueue("kmmcd");
710 ret = mmc_register_bus();
712 ret = mmc_register_host_class();
714 mmc_unregister_bus();
719 static void __exit mmc_exit(void)
721 mmc_unregister_host_class();
722 mmc_unregister_bus();
723 destroy_workqueue(workqueue);
726 module_init(mmc_init);
727 module_exit(mmc_exit);
729 MODULE_LICENSE("GPL");