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>
37 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
38 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
39 extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
41 static struct workqueue_struct *workqueue;
44 * Internal function. Schedule delayed work in the MMC work queue.
46 static int mmc_schedule_delayed_work(struct delayed_work *work,
49 return queue_delayed_work(workqueue, work, delay);
53 * Internal function. Flush all scheduled work from the MMC work queue.
55 static void mmc_flush_scheduled_work(void)
57 flush_workqueue(workqueue);
61 * mmc_request_done - finish processing an MMC request
62 * @host: MMC host which completed request
63 * @mrq: MMC request which request
65 * MMC drivers should call this function when they have completed
66 * their processing of a request.
68 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
70 struct mmc_command *cmd = mrq->cmd;
73 if (err && cmd->retries) {
74 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
75 mmc_hostname(host), cmd->opcode, err);
79 host->ops->request(host, mrq);
81 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
82 mmc_hostname(host), cmd->opcode, err,
83 cmd->resp[0], cmd->resp[1],
84 cmd->resp[2], cmd->resp[3]);
87 pr_debug("%s: %d bytes transferred: %d\n",
89 mrq->data->bytes_xfered, mrq->data->error);
93 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
94 mmc_hostname(host), mrq->stop->opcode,
96 mrq->stop->resp[0], mrq->stop->resp[1],
97 mrq->stop->resp[2], mrq->stop->resp[3]);
105 EXPORT_SYMBOL(mmc_request_done);
108 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
110 #ifdef CONFIG_MMC_DEBUG
114 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
115 mmc_hostname(host), mrq->cmd->opcode,
116 mrq->cmd->arg, mrq->cmd->flags);
119 pr_debug("%s: blksz %d blocks %d flags %08x "
120 "tsac %d ms nsac %d\n",
121 mmc_hostname(host), mrq->data->blksz,
122 mrq->data->blocks, mrq->data->flags,
123 mrq->data->timeout_ns / 10000000,
124 mrq->data->timeout_clks);
128 pr_debug("%s: CMD%u arg %08x flags %08x\n",
129 mmc_hostname(host), mrq->stop->opcode,
130 mrq->stop->arg, mrq->stop->flags);
133 WARN_ON(!host->claimed);
138 BUG_ON(mrq->data->blksz > host->max_blk_size);
139 BUG_ON(mrq->data->blocks > host->max_blk_count);
140 BUG_ON(mrq->data->blocks * mrq->data->blksz >
143 #ifdef CONFIG_MMC_DEBUG
145 for (i = 0;i < mrq->data->sg_len;i++)
146 sz += mrq->data->sg[i].length;
147 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
150 mrq->cmd->data = mrq->data;
151 mrq->data->error = 0;
152 mrq->data->mrq = mrq;
154 mrq->data->stop = mrq->stop;
155 mrq->stop->error = 0;
156 mrq->stop->mrq = mrq;
159 host->ops->request(host, mrq);
162 static void mmc_wait_done(struct mmc_request *mrq)
164 complete(mrq->done_data);
168 * mmc_wait_for_req - start a request and wait for completion
169 * @host: MMC host to start command
170 * @mrq: MMC request to start
172 * Start a new MMC custom command request for a host, and wait
173 * for the command to complete. Does not attempt to parse the
176 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
178 DECLARE_COMPLETION_ONSTACK(complete);
180 mrq->done_data = &complete;
181 mrq->done = mmc_wait_done;
183 mmc_start_request(host, mrq);
185 wait_for_completion(&complete);
188 EXPORT_SYMBOL(mmc_wait_for_req);
191 * mmc_wait_for_cmd - start a command and wait for completion
192 * @host: MMC host to start command
193 * @cmd: MMC command to start
194 * @retries: maximum number of retries
196 * Start a new MMC command for a host, and wait for the command
197 * to complete. Return any error that occurred while the command
198 * was executing. Do not attempt to parse the response.
200 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
202 struct mmc_request mrq;
204 BUG_ON(!host->claimed);
206 memset(&mrq, 0, sizeof(struct mmc_request));
208 memset(cmd->resp, 0, sizeof(cmd->resp));
209 cmd->retries = retries;
214 mmc_wait_for_req(host, &mrq);
219 EXPORT_SYMBOL(mmc_wait_for_cmd);
222 * mmc_set_data_timeout - set the timeout for a data command
223 * @data: data phase for command
224 * @card: the MMC card associated with the data transfer
226 * Computes the data timeout parameters according to the
227 * correct algorithm given the card type.
229 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
234 * SD cards use a 100 multiplier rather than 10
236 mult = mmc_card_sd(card) ? 100 : 10;
239 * Scale up the multiplier (and therefore the timeout) by
240 * the r2w factor for writes.
242 if (data->flags & MMC_DATA_WRITE)
243 mult <<= card->csd.r2w_factor;
245 data->timeout_ns = card->csd.tacc_ns * mult;
246 data->timeout_clks = card->csd.tacc_clks * mult;
249 * SD cards also have an upper limit on the timeout.
251 if (mmc_card_sd(card)) {
252 unsigned int timeout_us, limit_us;
254 timeout_us = data->timeout_ns / 1000;
255 timeout_us += data->timeout_clks * 1000 /
256 (card->host->ios.clock / 1000);
258 if (data->flags & MMC_DATA_WRITE)
264 * SDHC cards always use these fixed values.
266 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
267 data->timeout_ns = limit_us * 1000;
268 data->timeout_clks = 0;
272 EXPORT_SYMBOL(mmc_set_data_timeout);
275 * mmc_claim_host - exclusively claim a host
276 * @host: mmc host to claim
278 * Claim a host for a set of operations.
280 void mmc_claim_host(struct mmc_host *host)
282 DECLARE_WAITQUEUE(wait, current);
287 add_wait_queue(&host->wq, &wait);
288 spin_lock_irqsave(&host->lock, flags);
290 set_current_state(TASK_UNINTERRUPTIBLE);
293 spin_unlock_irqrestore(&host->lock, flags);
295 spin_lock_irqsave(&host->lock, flags);
297 set_current_state(TASK_RUNNING);
299 spin_unlock_irqrestore(&host->lock, flags);
300 remove_wait_queue(&host->wq, &wait);
303 EXPORT_SYMBOL(mmc_claim_host);
306 * mmc_release_host - release a host
307 * @host: mmc host to release
309 * Release a MMC host, allowing others to claim the host
310 * for their operations.
312 void mmc_release_host(struct mmc_host *host)
316 BUG_ON(!host->claimed);
318 spin_lock_irqsave(&host->lock, flags);
320 spin_unlock_irqrestore(&host->lock, flags);
325 EXPORT_SYMBOL(mmc_release_host);
328 * Internal function that does the actual ios call to the host driver,
329 * optionally printing some debug output.
331 static inline void mmc_set_ios(struct mmc_host *host)
333 struct mmc_ios *ios = &host->ios;
335 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
336 "width %u timing %u\n",
337 mmc_hostname(host), ios->clock, ios->bus_mode,
338 ios->power_mode, ios->chip_select, ios->vdd,
339 ios->bus_width, ios->timing);
341 host->ops->set_ios(host, ios);
345 * Control chip select pin on a host.
347 void mmc_set_chip_select(struct mmc_host *host, int mode)
349 host->ios.chip_select = mode;
354 * Sets the host clock to the highest possible frequency that
357 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
359 WARN_ON(hz < host->f_min);
361 if (hz > host->f_max)
364 host->ios.clock = hz;
369 * Change the bus mode (open drain/push-pull) of a host.
371 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
373 host->ios.bus_mode = mode;
378 * Change data bus width of a host.
380 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
382 host->ios.bus_width = width;
387 * Mask off any voltages we don't support and select
390 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
394 ocr &= host->ocr_avail;
412 * Select timing parameters for host.
414 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
416 host->ios.timing = timing;
421 * Apply power to the MMC stack. This is a two-stage process.
422 * First, we enable power to the card without the clock running.
423 * We then wait a bit for the power to stabilise. Finally,
424 * enable the bus drivers and clock to the card.
426 * We must _NOT_ enable the clock prior to power stablising.
428 * If a host does all the power sequencing itself, ignore the
429 * initial MMC_POWER_UP stage.
431 static void mmc_power_up(struct mmc_host *host)
433 int bit = fls(host->ocr_avail) - 1;
436 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
437 host->ios.chip_select = MMC_CS_DONTCARE;
438 host->ios.power_mode = MMC_POWER_UP;
439 host->ios.bus_width = MMC_BUS_WIDTH_1;
440 host->ios.timing = MMC_TIMING_LEGACY;
445 host->ios.clock = host->f_min;
446 host->ios.power_mode = MMC_POWER_ON;
452 static void mmc_power_off(struct mmc_host *host)
456 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
457 host->ios.chip_select = MMC_CS_DONTCARE;
458 host->ios.power_mode = MMC_POWER_OFF;
459 host->ios.bus_width = MMC_BUS_WIDTH_1;
460 host->ios.timing = MMC_TIMING_LEGACY;
465 * Cleanup when the last reference to the bus operator is dropped.
467 void __mmc_release_bus(struct mmc_host *host)
470 BUG_ON(host->bus_refs);
471 BUG_ON(!host->bus_dead);
473 host->bus_ops = NULL;
477 * Increase reference count of bus operator
479 static inline void mmc_bus_get(struct mmc_host *host)
483 spin_lock_irqsave(&host->lock, flags);
485 spin_unlock_irqrestore(&host->lock, flags);
489 * Decrease reference count of bus operator and free it if
490 * it is the last reference.
492 static inline void mmc_bus_put(struct mmc_host *host)
496 spin_lock_irqsave(&host->lock, flags);
498 if ((host->bus_refs == 0) && host->bus_ops)
499 __mmc_release_bus(host);
500 spin_unlock_irqrestore(&host->lock, flags);
504 * Assign a mmc bus handler to a host. Only one bus handler may control a
505 * host at any given time.
507 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
514 BUG_ON(!host->claimed);
516 spin_lock_irqsave(&host->lock, flags);
518 BUG_ON(host->bus_ops);
519 BUG_ON(host->bus_refs);
525 spin_unlock_irqrestore(&host->lock, flags);
529 * Remove the current bus handler from a host. Assumes that there are
530 * no interesting cards left, so the bus is powered down.
532 void mmc_detach_bus(struct mmc_host *host)
538 BUG_ON(!host->claimed);
539 BUG_ON(!host->bus_ops);
541 spin_lock_irqsave(&host->lock, flags);
545 spin_unlock_irqrestore(&host->lock, flags);
553 * mmc_detect_change - process change of state on a MMC socket
554 * @host: host which changed state.
555 * @delay: optional delay to wait before detection (jiffies)
557 * MMC drivers should call this when they detect a card has been
558 * inserted or removed. The MMC layer will confirm that any
559 * present card is still functional, and initialize any newly
562 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
564 #ifdef CONFIG_MMC_DEBUG
566 spin_lock_irqsave(&host->lock, flags);
567 BUG_ON(host->removed);
568 spin_unlock_irqrestore(&host->lock, flags);
571 mmc_schedule_delayed_work(&host->detect, delay);
574 EXPORT_SYMBOL(mmc_detect_change);
577 void mmc_rescan(struct work_struct *work)
579 struct mmc_host *host =
580 container_of(work, struct mmc_host, detect.work);
586 if (host->bus_ops == NULL) {
588 * Only we can add a new handler, so it's safe to
589 * release the lock here.
593 mmc_claim_host(host);
598 mmc_send_if_cond(host, host->ocr_avail);
601 * First we search for SDIO...
603 err = mmc_send_io_op_cond(host, 0, &ocr);
605 if (mmc_attach_sdio(host, ocr))
611 * ...then normal SD...
613 err = mmc_send_app_op_cond(host, 0, &ocr);
615 if (mmc_attach_sd(host, ocr))
621 * ...and finally MMC.
623 err = mmc_send_op_cond(host, 0, &ocr);
625 if (mmc_attach_mmc(host, ocr))
630 mmc_release_host(host);
633 if (host->bus_ops->detect && !host->bus_dead)
634 host->bus_ops->detect(host);
640 void mmc_start_host(struct mmc_host *host)
643 mmc_detect_change(host, 0);
646 void mmc_stop_host(struct mmc_host *host)
648 #ifdef CONFIG_MMC_DEBUG
650 spin_lock_irqsave(&host->lock, flags);
652 spin_unlock_irqrestore(&host->lock, flags);
655 mmc_flush_scheduled_work();
658 if (host->bus_ops && !host->bus_dead) {
659 if (host->bus_ops->remove)
660 host->bus_ops->remove(host);
662 mmc_claim_host(host);
663 mmc_detach_bus(host);
664 mmc_release_host(host);
676 * mmc_suspend_host - suspend a host
678 * @state: suspend mode (PM_SUSPEND_xxx)
680 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
682 mmc_flush_scheduled_work();
685 if (host->bus_ops && !host->bus_dead) {
686 if (host->bus_ops->suspend)
687 host->bus_ops->suspend(host);
688 if (!host->bus_ops->resume) {
689 if (host->bus_ops->remove)
690 host->bus_ops->remove(host);
692 mmc_claim_host(host);
693 mmc_detach_bus(host);
694 mmc_release_host(host);
704 EXPORT_SYMBOL(mmc_suspend_host);
707 * mmc_resume_host - resume a previously suspended host
710 int mmc_resume_host(struct mmc_host *host)
713 if (host->bus_ops && !host->bus_dead) {
715 BUG_ON(!host->bus_ops->resume);
716 host->bus_ops->resume(host);
721 * We add a slight delay here so that resume can progress
724 mmc_detect_change(host, 1);
729 EXPORT_SYMBOL(mmc_resume_host);
733 static int __init mmc_init(void)
737 workqueue = create_singlethread_workqueue("kmmcd");
741 ret = mmc_register_bus();
743 ret = mmc_register_host_class();
745 mmc_unregister_bus();
750 static void __exit mmc_exit(void)
752 mmc_unregister_host_class();
753 mmc_unregister_bus();
754 destroy_workqueue(workqueue);
757 module_init(mmc_init);
758 module_exit(mmc_exit);
760 MODULE_LICENSE("GPL");