mmc build fix
[firewire-audio.git] / drivers / mmc / core / core.c
blob7385acfa1dd939a29e77fc414f89be7b47bed576
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 "sysfs.h"
32 #include "mmc_ops.h"
33 #include "sd_ops.h"
35 extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
36 extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
38 /**
39 * mmc_request_done - finish processing an MMC request
40 * @host: MMC host which completed request
41 * @mrq: MMC request which request
43 * MMC drivers should call this function when they have completed
44 * their processing of a request.
46 void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
48 struct mmc_command *cmd = mrq->cmd;
49 int err = cmd->error;
51 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
52 mmc_hostname(host), cmd->opcode, err,
53 mrq->data ? mrq->data->error : 0,
54 mrq->stop ? mrq->stop->error : 0,
55 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
57 if (err && cmd->retries) {
58 cmd->retries--;
59 cmd->error = 0;
60 host->ops->request(host, mrq);
61 } else if (mrq->done) {
62 mrq->done(mrq);
66 EXPORT_SYMBOL(mmc_request_done);
68 /**
69 * mmc_start_request - start a command on a host
70 * @host: MMC host to start command on
71 * @mrq: MMC request to start
73 * Queue a command on the specified host. We expect the
74 * caller to be holding the host lock with interrupts disabled.
76 void
77 mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
79 #ifdef CONFIG_MMC_DEBUG
80 unsigned int i, sz;
81 #endif
83 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
84 mmc_hostname(host), mrq->cmd->opcode,
85 mrq->cmd->arg, mrq->cmd->flags);
87 WARN_ON(!host->claimed);
89 mrq->cmd->error = 0;
90 mrq->cmd->mrq = mrq;
91 if (mrq->data) {
92 BUG_ON(mrq->data->blksz > host->max_blk_size);
93 BUG_ON(mrq->data->blocks > host->max_blk_count);
94 BUG_ON(mrq->data->blocks * mrq->data->blksz >
95 host->max_req_size);
97 #ifdef CONFIG_MMC_DEBUG
98 sz = 0;
99 for (i = 0;i < mrq->data->sg_len;i++)
100 sz += mrq->data->sg[i].length;
101 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
102 #endif
104 mrq->cmd->data = mrq->data;
105 mrq->data->error = 0;
106 mrq->data->mrq = mrq;
107 if (mrq->stop) {
108 mrq->data->stop = mrq->stop;
109 mrq->stop->error = 0;
110 mrq->stop->mrq = mrq;
113 host->ops->request(host, mrq);
116 EXPORT_SYMBOL(mmc_start_request);
118 static void mmc_wait_done(struct mmc_request *mrq)
120 complete(mrq->done_data);
123 int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
125 DECLARE_COMPLETION_ONSTACK(complete);
127 mrq->done_data = &complete;
128 mrq->done = mmc_wait_done;
130 mmc_start_request(host, mrq);
132 wait_for_completion(&complete);
134 return 0;
137 EXPORT_SYMBOL(mmc_wait_for_req);
140 * mmc_wait_for_cmd - start a command and wait for completion
141 * @host: MMC host to start command
142 * @cmd: MMC command to start
143 * @retries: maximum number of retries
145 * Start a new MMC command for a host, and wait for the command
146 * to complete. Return any error that occurred while the command
147 * was executing. Do not attempt to parse the response.
149 int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
151 struct mmc_request mrq;
153 BUG_ON(!host->claimed);
155 memset(&mrq, 0, sizeof(struct mmc_request));
157 memset(cmd->resp, 0, sizeof(cmd->resp));
158 cmd->retries = retries;
160 mrq.cmd = cmd;
161 cmd->data = NULL;
163 mmc_wait_for_req(host, &mrq);
165 return cmd->error;
168 EXPORT_SYMBOL(mmc_wait_for_cmd);
171 * mmc_set_data_timeout - set the timeout for a data command
172 * @data: data phase for command
173 * @card: the MMC card associated with the data transfer
174 * @write: flag to differentiate reads from writes
176 void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
177 int write)
179 unsigned int mult;
182 * SD cards use a 100 multiplier rather than 10
184 mult = mmc_card_sd(card) ? 100 : 10;
187 * Scale up the multiplier (and therefore the timeout) by
188 * the r2w factor for writes.
190 if (write)
191 mult <<= card->csd.r2w_factor;
193 data->timeout_ns = card->csd.tacc_ns * mult;
194 data->timeout_clks = card->csd.tacc_clks * mult;
197 * SD cards also have an upper limit on the timeout.
199 if (mmc_card_sd(card)) {
200 unsigned int timeout_us, limit_us;
202 timeout_us = data->timeout_ns / 1000;
203 timeout_us += data->timeout_clks * 1000 /
204 (card->host->ios.clock / 1000);
206 if (write)
207 limit_us = 250000;
208 else
209 limit_us = 100000;
212 * SDHC cards always use these fixed values.
214 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
215 data->timeout_ns = limit_us * 1000;
216 data->timeout_clks = 0;
220 EXPORT_SYMBOL(mmc_set_data_timeout);
223 * __mmc_claim_host - exclusively claim a host
224 * @host: mmc host to claim
225 * @card: mmc card to claim host for
227 * Claim a host for a set of operations. If a valid card
228 * is passed and this wasn't the last card selected, select
229 * the card before returning.
231 * Note: you should use mmc_card_claim_host or mmc_claim_host.
233 void mmc_claim_host(struct mmc_host *host)
235 DECLARE_WAITQUEUE(wait, current);
236 unsigned long flags;
238 add_wait_queue(&host->wq, &wait);
239 spin_lock_irqsave(&host->lock, flags);
240 while (1) {
241 set_current_state(TASK_UNINTERRUPTIBLE);
242 if (!host->claimed)
243 break;
244 spin_unlock_irqrestore(&host->lock, flags);
245 schedule();
246 spin_lock_irqsave(&host->lock, flags);
248 set_current_state(TASK_RUNNING);
249 host->claimed = 1;
250 spin_unlock_irqrestore(&host->lock, flags);
251 remove_wait_queue(&host->wq, &wait);
254 EXPORT_SYMBOL(mmc_claim_host);
257 * mmc_release_host - release a host
258 * @host: mmc host to release
260 * Release a MMC host, allowing others to claim the host
261 * for their operations.
263 void mmc_release_host(struct mmc_host *host)
265 unsigned long flags;
267 BUG_ON(!host->claimed);
269 spin_lock_irqsave(&host->lock, flags);
270 host->claimed = 0;
271 spin_unlock_irqrestore(&host->lock, flags);
273 wake_up(&host->wq);
276 EXPORT_SYMBOL(mmc_release_host);
279 * Internal function that does the actual ios call to the host driver,
280 * optionally printing some debug output.
282 static inline void mmc_set_ios(struct mmc_host *host)
284 struct mmc_ios *ios = &host->ios;
286 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
287 "width %u timing %u\n",
288 mmc_hostname(host), ios->clock, ios->bus_mode,
289 ios->power_mode, ios->chip_select, ios->vdd,
290 ios->bus_width, ios->timing);
292 host->ops->set_ios(host, ios);
296 * Control chip select pin on a host.
298 void mmc_set_chip_select(struct mmc_host *host, int mode)
300 host->ios.chip_select = mode;
301 mmc_set_ios(host);
305 * Sets the host clock to the highest possible frequency that
306 * is below "hz".
308 void mmc_set_clock(struct mmc_host *host, unsigned int hz)
310 WARN_ON(hz < host->f_min);
312 if (hz > host->f_max)
313 hz = host->f_max;
315 host->ios.clock = hz;
316 mmc_set_ios(host);
320 * Change the bus mode (open drain/push-pull) of a host.
322 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
324 host->ios.bus_mode = mode;
325 mmc_set_ios(host);
329 * Change data bus width of a host.
331 void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
333 host->ios.bus_width = width;
334 mmc_set_ios(host);
338 * Mask off any voltages we don't support and select
339 * the lowest voltage
341 u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
343 int bit;
345 ocr &= host->ocr_avail;
347 bit = ffs(ocr);
348 if (bit) {
349 bit -= 1;
351 ocr &= 3 << bit;
353 host->ios.vdd = bit;
354 mmc_set_ios(host);
355 } else {
356 ocr = 0;
359 return ocr;
363 * Select timing parameters for host.
365 void mmc_set_timing(struct mmc_host *host, unsigned int timing)
367 host->ios.timing = timing;
368 mmc_set_ios(host);
372 * Allocate a new MMC card
374 struct mmc_card *mmc_alloc_card(struct mmc_host *host)
376 struct mmc_card *card;
378 card = kmalloc(sizeof(struct mmc_card), GFP_KERNEL);
379 if (!card)
380 return ERR_PTR(-ENOMEM);
382 mmc_init_card(card, host);
384 return card;
388 * Apply power to the MMC stack. This is a two-stage process.
389 * First, we enable power to the card without the clock running.
390 * We then wait a bit for the power to stabilise. Finally,
391 * enable the bus drivers and clock to the card.
393 * We must _NOT_ enable the clock prior to power stablising.
395 * If a host does all the power sequencing itself, ignore the
396 * initial MMC_POWER_UP stage.
398 static void mmc_power_up(struct mmc_host *host)
400 int bit = fls(host->ocr_avail) - 1;
402 host->ios.vdd = bit;
403 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
404 host->ios.chip_select = MMC_CS_DONTCARE;
405 host->ios.power_mode = MMC_POWER_UP;
406 host->ios.bus_width = MMC_BUS_WIDTH_1;
407 host->ios.timing = MMC_TIMING_LEGACY;
408 mmc_set_ios(host);
410 mmc_delay(1);
412 host->ios.clock = host->f_min;
413 host->ios.power_mode = MMC_POWER_ON;
414 mmc_set_ios(host);
416 mmc_delay(2);
419 static void mmc_power_off(struct mmc_host *host)
421 host->ios.clock = 0;
422 host->ios.vdd = 0;
423 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
424 host->ios.chip_select = MMC_CS_DONTCARE;
425 host->ios.power_mode = MMC_POWER_OFF;
426 host->ios.bus_width = MMC_BUS_WIDTH_1;
427 host->ios.timing = MMC_TIMING_LEGACY;
428 mmc_set_ios(host);
432 * Assign a mmc bus handler to a host. Only one bus handler may control a
433 * host at any given time.
435 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
437 unsigned long flags;
439 BUG_ON(!host);
440 BUG_ON(!ops);
442 BUG_ON(!host->claimed);
444 spin_lock_irqsave(&host->lock, flags);
446 BUG_ON(host->bus_ops);
447 BUG_ON(host->bus_refs);
449 host->bus_ops = ops;
450 host->bus_refs = 1;
451 host->bus_dead = 0;
453 spin_unlock_irqrestore(&host->lock, flags);
457 * Remove the current bus handler from a host. Assumes that there are
458 * no interesting cards left, so the bus is powered down.
460 void mmc_detach_bus(struct mmc_host *host)
462 unsigned long flags;
464 BUG_ON(!host);
466 BUG_ON(!host->claimed);
467 BUG_ON(!host->bus_ops);
469 spin_lock_irqsave(&host->lock, flags);
471 host->bus_dead = 1;
473 spin_unlock_irqrestore(&host->lock, flags);
475 mmc_power_off(host);
477 mmc_bus_put(host);
481 * Cleanup when the last reference to the bus operator is dropped.
483 void __mmc_release_bus(struct mmc_host *host)
485 BUG_ON(!host);
486 BUG_ON(host->bus_refs);
487 BUG_ON(!host->bus_dead);
489 host->bus_ops = NULL;
493 * mmc_detect_change - process change of state on a MMC socket
494 * @host: host which changed state.
495 * @delay: optional delay to wait before detection (jiffies)
497 * All we know is that card(s) have been inserted or removed
498 * from the socket(s). We don't know which socket or cards.
500 void mmc_detect_change(struct mmc_host *host, unsigned long delay)
502 #ifdef CONFIG_MMC_DEBUG
503 unsigned long flags;
504 spin_lock_irqsave(&host->lock, flags);
505 BUG_ON(host->removed);
506 spin_unlock_irqrestore(&host->lock, flags);
507 #endif
509 mmc_schedule_delayed_work(&host->detect, delay);
512 EXPORT_SYMBOL(mmc_detect_change);
515 static void mmc_rescan(struct work_struct *work)
517 struct mmc_host *host =
518 container_of(work, struct mmc_host, detect.work);
519 u32 ocr;
520 int err;
522 mmc_bus_get(host);
524 if (host->bus_ops == NULL) {
526 * Only we can add a new handler, so it's safe to
527 * release the lock here.
529 mmc_bus_put(host);
531 mmc_claim_host(host);
533 mmc_power_up(host);
534 mmc_go_idle(host);
536 mmc_send_if_cond(host, host->ocr_avail);
538 err = mmc_send_app_op_cond(host, 0, &ocr);
539 if (err == MMC_ERR_NONE) {
540 if (mmc_attach_sd(host, ocr))
541 mmc_power_off(host);
542 } else {
544 * If we fail to detect any SD cards then try
545 * searching for MMC cards.
547 err = mmc_send_op_cond(host, 0, &ocr);
548 if (err == MMC_ERR_NONE) {
549 if (mmc_attach_mmc(host, ocr))
550 mmc_power_off(host);
551 } else {
552 mmc_power_off(host);
553 mmc_release_host(host);
556 } else {
557 if (host->bus_ops->detect && !host->bus_dead)
558 host->bus_ops->detect(host);
560 mmc_bus_put(host);
566 * mmc_alloc_host - initialise the per-host structure.
567 * @extra: sizeof private data structure
568 * @dev: pointer to host device model structure
570 * Initialise the per-host structure.
572 struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
574 struct mmc_host *host;
576 host = mmc_alloc_host_sysfs(extra, dev);
577 if (host) {
578 spin_lock_init(&host->lock);
579 init_waitqueue_head(&host->wq);
580 INIT_DELAYED_WORK(&host->detect, mmc_rescan);
583 * By default, hosts do not support SGIO or large requests.
584 * They have to set these according to their abilities.
586 host->max_hw_segs = 1;
587 host->max_phys_segs = 1;
588 host->max_seg_size = PAGE_CACHE_SIZE;
590 host->max_req_size = PAGE_CACHE_SIZE;
591 host->max_blk_size = 512;
592 host->max_blk_count = PAGE_CACHE_SIZE / 512;
595 return host;
598 EXPORT_SYMBOL(mmc_alloc_host);
601 * mmc_add_host - initialise host hardware
602 * @host: mmc host
604 int mmc_add_host(struct mmc_host *host)
606 int ret;
608 ret = mmc_add_host_sysfs(host);
609 if (ret == 0) {
610 mmc_power_off(host);
611 mmc_detect_change(host, 0);
614 return ret;
617 EXPORT_SYMBOL(mmc_add_host);
620 * mmc_remove_host - remove host hardware
621 * @host: mmc host
623 * Unregister and remove all cards associated with this host,
624 * and power down the MMC bus.
626 void mmc_remove_host(struct mmc_host *host)
628 #ifdef CONFIG_MMC_DEBUG
629 unsigned long flags;
630 spin_lock_irqsave(&host->lock, flags);
631 host->removed = 1;
632 spin_unlock_irqrestore(&host->lock, flags);
633 #endif
635 mmc_flush_scheduled_work();
637 mmc_bus_get(host);
638 if (host->bus_ops && !host->bus_dead) {
639 if (host->bus_ops->remove)
640 host->bus_ops->remove(host);
642 mmc_claim_host(host);
643 mmc_detach_bus(host);
644 mmc_release_host(host);
646 mmc_bus_put(host);
648 BUG_ON(host->card);
650 mmc_power_off(host);
651 mmc_remove_host_sysfs(host);
654 EXPORT_SYMBOL(mmc_remove_host);
657 * mmc_free_host - free the host structure
658 * @host: mmc host
660 * Free the host once all references to it have been dropped.
662 void mmc_free_host(struct mmc_host *host)
664 mmc_free_host_sysfs(host);
667 EXPORT_SYMBOL(mmc_free_host);
669 #ifdef CONFIG_PM
672 * mmc_suspend_host - suspend a host
673 * @host: mmc host
674 * @state: suspend mode (PM_SUSPEND_xxx)
676 int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
678 mmc_flush_scheduled_work();
680 mmc_bus_get(host);
681 if (host->bus_ops && !host->bus_dead) {
682 if (host->bus_ops->suspend)
683 host->bus_ops->suspend(host);
684 if (!host->bus_ops->resume) {
685 if (host->bus_ops->remove)
686 host->bus_ops->remove(host);
688 mmc_claim_host(host);
689 mmc_detach_bus(host);
690 mmc_release_host(host);
693 mmc_bus_put(host);
695 mmc_power_off(host);
697 return 0;
700 EXPORT_SYMBOL(mmc_suspend_host);
703 * mmc_resume_host - resume a previously suspended host
704 * @host: mmc host
706 int mmc_resume_host(struct mmc_host *host)
708 mmc_bus_get(host);
709 if (host->bus_ops && !host->bus_dead) {
710 mmc_power_up(host);
711 BUG_ON(!host->bus_ops->resume);
712 host->bus_ops->resume(host);
714 mmc_bus_put(host);
717 * We add a slight delay here so that resume can progress
718 * in parallel.
720 mmc_detect_change(host, 1);
722 return 0;
725 EXPORT_SYMBOL(mmc_resume_host);
727 #endif
729 MODULE_LICENSE("GPL");