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>
38 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
39 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
40 extern int mmc_attach_sdio(struct mmc_host *host, u32 ocr);
42 static struct workqueue_struct *workqueue;
45 * Internal function. Schedule delayed work in the MMC work queue.
47 static int mmc_schedule_delayed_work(struct delayed_work *work,
50 return queue_delayed_work(workqueue, work, delay);
54 * Internal function. Flush all scheduled work from the MMC work queue.
56 static void mmc_flush_scheduled_work(void)
58 flush_workqueue(workqueue);
62 * mmc_request_done - finish processing an MMC request
63 * @host: MMC host which completed request
64 * @mrq: MMC request which request
66 * MMC drivers should call this function when they have completed
67 * their processing of a request.
69 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
71 struct mmc_command *cmd = mrq->cmd;
74 if (err && cmd->retries) {
75 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
76 mmc_hostname(host), cmd->opcode, err);
80 host->ops->request(host, mrq);
82 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
83 mmc_hostname(host), cmd->opcode, err,
84 cmd->resp[0], cmd->resp[1],
85 cmd->resp[2], cmd->resp[3]);
88 pr_debug("%s: %d bytes transferred: %d\n",
90 mrq->data->bytes_xfered, mrq->data->error);
94 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
95 mmc_hostname(host), mrq->stop->opcode,
97 mrq->stop->resp[0], mrq->stop->resp[1],
98 mrq->stop->resp[2], mrq->stop->resp[3]);
106 EXPORT_SYMBOL(mmc_request_done);
109 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
111 #ifdef CONFIG_MMC_DEBUG
115 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
116 mmc_hostname(host), mrq->cmd->opcode,
117 mrq->cmd->arg, mrq->cmd->flags);
120 pr_debug("%s: blksz %d blocks %d flags %08x "
121 "tsac %d ms nsac %d\n",
122 mmc_hostname(host), mrq->data->blksz,
123 mrq->data->blocks, mrq->data->flags,
124 mrq->data->timeout_ns / 10000000,
125 mrq->data->timeout_clks);
129 pr_debug("%s: CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->stop->opcode,
131 mrq->stop->arg, mrq->stop->flags);
134 WARN_ON(!host->claimed);
139 BUG_ON(mrq->data->blksz > host->max_blk_size);
140 BUG_ON(mrq->data->blocks > host->max_blk_count);
141 BUG_ON(mrq->data->blocks * mrq->data->blksz >
144 #ifdef CONFIG_MMC_DEBUG
146 for (i = 0;i < mrq->data->sg_len;i++)
147 sz += mrq->data->sg[i].length;
148 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
151 mrq->cmd->data = mrq->data;
152 mrq->data->error = 0;
153 mrq->data->mrq = mrq;
155 mrq->data->stop = mrq->stop;
156 mrq->stop->error = 0;
157 mrq->stop->mrq = mrq;
160 host->ops->request(host, mrq);
163 static void mmc_wait_done(struct mmc_request *mrq)
165 complete(mrq->done_data);
169 * mmc_wait_for_req - start a request and wait for completion
170 * @host: MMC host to start command
171 * @mrq: MMC request to start
173 * Start a new MMC custom command request for a host, and wait
174 * for the command to complete. Does not attempt to parse the
177 void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
179 DECLARE_COMPLETION_ONSTACK(complete);
181 mrq->done_data = &complete;
182 mrq->done = mmc_wait_done;
184 mmc_start_request(host, mrq);
186 wait_for_completion(&complete);
189 EXPORT_SYMBOL(mmc_wait_for_req);
192 * mmc_wait_for_cmd - start a command and wait for completion
193 * @host: MMC host to start command
194 * @cmd: MMC command to start
195 * @retries: maximum number of retries
197 * Start a new MMC command for a host, and wait for the command
198 * to complete. Return any error that occurred while the command
199 * was executing. Do not attempt to parse the response.
201 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
203 struct mmc_request mrq;
205 BUG_ON(!host->claimed);
207 memset(&mrq, 0, sizeof(struct mmc_request));
209 memset(cmd->resp, 0, sizeof(cmd->resp));
210 cmd->retries = retries;
215 mmc_wait_for_req(host, &mrq);
220 EXPORT_SYMBOL(mmc_wait_for_cmd);
223 * mmc_set_data_timeout - set the timeout for a data command
224 * @data: data phase for command
225 * @card: the MMC card associated with the data transfer
227 * Computes the data timeout parameters according to the
228 * correct algorithm given the card type.
230 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
235 * SD cards use a 100 multiplier rather than 10
237 mult = mmc_card_sd(card) ? 100 : 10;
240 * Scale up the multiplier (and therefore the timeout) by
241 * the r2w factor for writes.
243 if (data->flags & MMC_DATA_WRITE)
244 mult <<= card->csd.r2w_factor;
246 data->timeout_ns = card->csd.tacc_ns * mult;
247 data->timeout_clks = card->csd.tacc_clks * mult;
250 * SD cards also have an upper limit on the timeout.
252 if (mmc_card_sd(card)) {
253 unsigned int timeout_us, limit_us;
255 timeout_us = data->timeout_ns / 1000;
256 timeout_us += data->timeout_clks * 1000 /
257 (card->host->ios.clock / 1000);
259 if (data->flags & MMC_DATA_WRITE)
265 * SDHC cards always use these fixed values.
267 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
268 data->timeout_ns = limit_us * 1000;
269 data->timeout_clks = 0;
273 EXPORT_SYMBOL(mmc_set_data_timeout);
276 * mmc_claim_host - exclusively claim a host
277 * @host: mmc host to claim
279 * Claim a host for a set of operations.
281 void mmc_claim_host(struct mmc_host *host)
283 DECLARE_WAITQUEUE(wait, current);
288 add_wait_queue(&host->wq, &wait);
289 spin_lock_irqsave(&host->lock, flags);
291 set_current_state(TASK_UNINTERRUPTIBLE);
294 spin_unlock_irqrestore(&host->lock, flags);
296 spin_lock_irqsave(&host->lock, flags);
298 set_current_state(TASK_RUNNING);
300 spin_unlock_irqrestore(&host->lock, flags);
301 remove_wait_queue(&host->wq, &wait);
304 EXPORT_SYMBOL(mmc_claim_host);
307 * mmc_release_host - release a host
308 * @host: mmc host to release
310 * Release a MMC host, allowing others to claim the host
311 * for their operations.
313 void mmc_release_host(struct mmc_host *host)
317 BUG_ON(!host->claimed);
319 spin_lock_irqsave(&host->lock, flags);
321 spin_unlock_irqrestore(&host->lock, flags);
326 EXPORT_SYMBOL(mmc_release_host);
329 * Internal function that does the actual ios call to the host driver,
330 * optionally printing some debug output.
332 static inline void mmc_set_ios(struct mmc_host *host)
334 struct mmc_ios *ios = &host->ios;
336 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
337 "width %u timing %u\n",
338 mmc_hostname(host), ios->clock, ios->bus_mode,
339 ios->power_mode, ios->chip_select, ios->vdd,
340 ios->bus_width, ios->timing);
342 host->ops->set_ios(host, ios);
346 * Control chip select pin on a host.
348 void mmc_set_chip_select(struct mmc_host *host, int mode)
350 host->ios.chip_select = mode;
355 * Sets the host clock to the highest possible frequency that
358 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
360 WARN_ON(hz < host->f_min);
362 if (hz > host->f_max)
365 host->ios.clock = hz;
370 * Change the bus mode (open drain/push-pull) of a host.
372 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
374 host->ios.bus_mode = mode;
379 * Change data bus width of a host.
381 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
383 host->ios.bus_width = width;
388 * Mask off any voltages we don't support and select
391 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
395 ocr &= host->ocr_avail;
413 * Select timing parameters for host.
415 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
417 host->ios.timing = timing;
422 * Apply power to the MMC stack. This is a two-stage process.
423 * First, we enable power to the card without the clock running.
424 * We then wait a bit for the power to stabilise. Finally,
425 * enable the bus drivers and clock to the card.
427 * We must _NOT_ enable the clock prior to power stablising.
429 * If a host does all the power sequencing itself, ignore the
430 * initial MMC_POWER_UP stage.
432 static void mmc_power_up(struct mmc_host *host)
434 int bit = fls(host->ocr_avail) - 1;
437 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
438 host->ios.chip_select = MMC_CS_DONTCARE;
439 host->ios.power_mode = MMC_POWER_UP;
440 host->ios.bus_width = MMC_BUS_WIDTH_1;
441 host->ios.timing = MMC_TIMING_LEGACY;
446 host->ios.clock = host->f_min;
447 host->ios.power_mode = MMC_POWER_ON;
453 static void mmc_power_off(struct mmc_host *host)
457 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
458 host->ios.chip_select = MMC_CS_DONTCARE;
459 host->ios.power_mode = MMC_POWER_OFF;
460 host->ios.bus_width = MMC_BUS_WIDTH_1;
461 host->ios.timing = MMC_TIMING_LEGACY;
466 * Cleanup when the last reference to the bus operator is dropped.
468 void __mmc_release_bus(struct mmc_host *host)
471 BUG_ON(host->bus_refs);
472 BUG_ON(!host->bus_dead);
474 host->bus_ops = NULL;
478 * Increase reference count of bus operator
480 static inline void mmc_bus_get(struct mmc_host *host)
484 spin_lock_irqsave(&host->lock, flags);
486 spin_unlock_irqrestore(&host->lock, flags);
490 * Decrease reference count of bus operator and free it if
491 * it is the last reference.
493 static inline void mmc_bus_put(struct mmc_host *host)
497 spin_lock_irqsave(&host->lock, flags);
499 if ((host->bus_refs == 0) && host->bus_ops)
500 __mmc_release_bus(host);
501 spin_unlock_irqrestore(&host->lock, flags);
505 * Assign a mmc bus handler to a host. Only one bus handler may control a
506 * host at any given time.
508 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
515 BUG_ON(!host->claimed);
517 spin_lock_irqsave(&host->lock, flags);
519 BUG_ON(host->bus_ops);
520 BUG_ON(host->bus_refs);
526 spin_unlock_irqrestore(&host->lock, flags);
530 * Remove the current bus handler from a host. Assumes that there are
531 * no interesting cards left, so the bus is powered down.
533 void mmc_detach_bus(struct mmc_host *host)
539 BUG_ON(!host->claimed);
540 BUG_ON(!host->bus_ops);
542 spin_lock_irqsave(&host->lock, flags);
546 spin_unlock_irqrestore(&host->lock, flags);
554 * mmc_detect_change - process change of state on a MMC socket
555 * @host: host which changed state.
556 * @delay: optional delay to wait before detection (jiffies)
558 * MMC drivers should call this when they detect a card has been
559 * inserted or removed. The MMC layer will confirm that any
560 * present card is still functional, and initialize any newly
563 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
565 #ifdef CONFIG_MMC_DEBUG
567 spin_lock_irqsave(&host->lock, flags);
568 BUG_ON(host->removed);
569 spin_unlock_irqrestore(&host->lock, flags);
572 mmc_schedule_delayed_work(&host->detect, delay);
575 EXPORT_SYMBOL(mmc_detect_change);
578 void mmc_rescan(struct work_struct *work)
580 struct mmc_host *host =
581 container_of(work, struct mmc_host, detect.work);
587 if (host->bus_ops == NULL) {
589 * Only we can add a new handler, so it's safe to
590 * release the lock here.
594 mmc_claim_host(host);
599 mmc_send_if_cond(host, host->ocr_avail);
602 * First we search for SDIO...
604 err = mmc_send_io_op_cond(host, 0, &ocr);
606 if (mmc_attach_sdio(host, ocr))
612 * ...then normal SD...
614 err = mmc_send_app_op_cond(host, 0, &ocr);
616 if (mmc_attach_sd(host, ocr))
622 * ...and finally MMC.
624 err = mmc_send_op_cond(host, 0, &ocr);
626 if (mmc_attach_mmc(host, ocr))
631 mmc_release_host(host);
634 if (host->bus_ops->detect && !host->bus_dead)
635 host->bus_ops->detect(host);
641 void mmc_start_host(struct mmc_host *host)
644 mmc_detect_change(host, 0);
647 void mmc_stop_host(struct mmc_host *host)
649 #ifdef CONFIG_MMC_DEBUG
651 spin_lock_irqsave(&host->lock, flags);
653 spin_unlock_irqrestore(&host->lock, flags);
656 mmc_flush_scheduled_work();
659 if (host->bus_ops && !host->bus_dead) {
660 if (host->bus_ops->remove)
661 host->bus_ops->remove(host);
663 mmc_claim_host(host);
664 mmc_detach_bus(host);
665 mmc_release_host(host);
677 * mmc_suspend_host - suspend a host
679 * @state: suspend mode (PM_SUSPEND_xxx)
681 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
683 mmc_flush_scheduled_work();
686 if (host->bus_ops && !host->bus_dead) {
687 if (host->bus_ops->suspend)
688 host->bus_ops->suspend(host);
689 if (!host->bus_ops->resume) {
690 if (host->bus_ops->remove)
691 host->bus_ops->remove(host);
693 mmc_claim_host(host);
694 mmc_detach_bus(host);
695 mmc_release_host(host);
705 EXPORT_SYMBOL(mmc_suspend_host);
708 * mmc_resume_host - resume a previously suspended host
711 int mmc_resume_host(struct mmc_host *host)
714 if (host->bus_ops && !host->bus_dead) {
716 BUG_ON(!host->bus_ops->resume);
717 host->bus_ops->resume(host);
722 * We add a slight delay here so that resume can progress
725 mmc_detect_change(host, 1);
730 EXPORT_SYMBOL(mmc_resume_host);
734 static int __init mmc_init(void)
738 workqueue = create_singlethread_workqueue("kmmcd");
742 ret = mmc_register_bus();
744 goto destroy_workqueue;
746 ret = mmc_register_host_class();
750 ret = sdio_register_bus();
752 goto unregister_host_class;
756 unregister_host_class:
757 mmc_unregister_host_class();
759 mmc_unregister_bus();
761 destroy_workqueue(workqueue);
766 static void __exit mmc_exit(void)
768 sdio_unregister_bus();
769 mmc_unregister_host_class();
770 mmc_unregister_bus();
771 destroy_workqueue(workqueue);
774 subsys_initcall(mmc_init);
775 module_exit(mmc_exit);
777 MODULE_LICENSE("GPL");