blob: d08968470c41beb063f35b78cd65bf3f14ccf703 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossmanaaac1b42007-02-28 15:33:10 +01002 * linux/drivers/mmc/core/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
Pierre Ossman5b4fd9a2005-09-06 15:18:56 -07005 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
Pierre Ossmanb8558852007-01-03 19:47:29 +01006 * Copyright (C) 2005-2007 Pierre Ossman, All Rights Reserved.
Philip Langdalebce40a32006-10-21 12:35:02 +02007 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Pierre Ossmanb57c43a2005-09-06 15:18:53 -070021#include <asm/scatterlist.h>
22#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/mmc/card.h>
25#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010026#include <linux/mmc/mmc.h>
27#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010029#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020030#include "bus.h"
31#include "host.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010032
33#include "mmc_ops.h"
34#include "sd_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Pierre Ossman7ea239d2006-12-31 00:11:32 +010036extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr);
37extern int mmc_attach_sd(struct mmc_host *host, u32 ocr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Pierre Ossmanffce2e72007-05-19 14:32:22 +020039static struct workqueue_struct *workqueue;
40
41/*
42 * Internal function. Schedule delayed work in the MMC work queue.
43 */
44static int mmc_schedule_delayed_work(struct delayed_work *work,
45 unsigned long delay)
46{
47 return queue_delayed_work(workqueue, work, delay);
48}
49
50/*
51 * Internal function. Flush all scheduled work from the MMC work queue.
52 */
53static void mmc_flush_scheduled_work(void)
54{
55 flush_workqueue(workqueue);
56}
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/**
Russell Kingfe10c6a2006-05-04 13:51:45 +010059 * mmc_request_done - finish processing an MMC request
60 * @host: MMC host which completed request
61 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010064 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 */
66void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
67{
68 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010069 int err = cmd->error;
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 if (err && cmd->retries) {
Pierre Ossmane4d21702007-07-24 21:46:49 +020072 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
73 mmc_hostname(host), cmd->opcode, err);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 cmd->retries--;
76 cmd->error = 0;
77 host->ops->request(host, mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +020078 } 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]);
83
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);
88 }
89
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]);
96 }
97
98 if (mrq->done)
99 mrq->done(mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
101}
102
103EXPORT_SYMBOL(mmc_request_done);
104
105/**
106 * mmc_start_request - start a command on a host
107 * @host: MMC host to start command on
108 * @mrq: MMC request to start
109 *
110 * Queue a command on the specified host. We expect the
111 * caller to be holding the host lock with interrupts disabled.
112 */
113void
114mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
115{
Pierre Ossman976d9272007-04-13 22:47:01 +0200116#ifdef CONFIG_MMC_DEBUG
117 unsigned int i, sz;
118#endif
119
Russell King920e70c2006-05-04 18:22:51 +0100120 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Pierre Ossmane4d21702007-07-24 21:46:49 +0200124 if (mrq->data) {
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);
131 }
132
133 if (mrq->stop) {
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);
137 }
138
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100139 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 mrq->cmd->error = 0;
142 mrq->cmd->mrq = mrq;
143 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100144 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100145 BUG_ON(mrq->data->blocks > host->max_blk_count);
146 BUG_ON(mrq->data->blocks * mrq->data->blksz >
147 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100148
Pierre Ossman976d9272007-04-13 22:47:01 +0200149#ifdef CONFIG_MMC_DEBUG
150 sz = 0;
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);
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 mrq->cmd->data = mrq->data;
157 mrq->data->error = 0;
158 mrq->data->mrq = mrq;
159 if (mrq->stop) {
160 mrq->data->stop = mrq->stop;
161 mrq->stop->error = 0;
162 mrq->stop->mrq = mrq;
163 }
164 }
165 host->ops->request(host, mrq);
166}
167
168EXPORT_SYMBOL(mmc_start_request);
169
170static void mmc_wait_done(struct mmc_request *mrq)
171{
172 complete(mrq->done_data);
173}
174
Pierre Ossman67a61c42007-07-11 20:22:11 +0200175/**
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
179 *
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
182 * response.
183 */
184void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700186 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 mrq->done_data = &complete;
189 mrq->done = mmc_wait_done;
190
191 mmc_start_request(host, mrq);
192
193 wait_for_completion(&complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196EXPORT_SYMBOL(mmc_wait_for_req);
197
198/**
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
203 *
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.
207 */
208int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
209{
210 struct mmc_request mrq;
211
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100212 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 memset(&mrq, 0, sizeof(struct mmc_request));
215
216 memset(cmd->resp, 0, sizeof(cmd->resp));
217 cmd->retries = retries;
218
219 mrq.cmd = cmd;
220 cmd->data = NULL;
221
222 mmc_wait_for_req(host, &mrq);
223
224 return cmd->error;
225}
226
227EXPORT_SYMBOL(mmc_wait_for_cmd);
228
Pierre Ossman335eadf2005-09-06 15:18:50 -0700229/**
Russell Kingd773d722006-09-07 15:57:12 +0100230 * 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
Pierre Ossman67a61c42007-07-11 20:22:11 +0200234 *
235 * Computes the data timeout parameters according to the
236 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100237 */
238void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
239 int write)
240{
241 unsigned int mult;
242
243 /*
244 * SD cards use a 100 multiplier rather than 10
245 */
246 mult = mmc_card_sd(card) ? 100 : 10;
247
248 /*
249 * Scale up the multiplier (and therefore the timeout) by
250 * the r2w factor for writes.
251 */
252 if (write)
253 mult <<= card->csd.r2w_factor;
254
255 data->timeout_ns = card->csd.tacc_ns * mult;
256 data->timeout_clks = card->csd.tacc_clks * mult;
257
258 /*
259 * SD cards also have an upper limit on the timeout.
260 */
261 if (mmc_card_sd(card)) {
262 unsigned int timeout_us, limit_us;
263
264 timeout_us = data->timeout_ns / 1000;
265 timeout_us += data->timeout_clks * 1000 /
266 (card->host->ios.clock / 1000);
267
268 if (write)
269 limit_us = 250000;
270 else
271 limit_us = 100000;
272
Philip Langdalefba68bd2007-01-04 06:57:32 -0800273 /*
274 * SDHC cards always use these fixed values.
275 */
276 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100277 data->timeout_ns = limit_us * 1000;
278 data->timeout_clks = 0;
279 }
280 }
281}
282EXPORT_SYMBOL(mmc_set_data_timeout);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/**
Pierre Ossman67a61c42007-07-11 20:22:11 +0200285 * mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 * @host: mmc host to claim
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 *
Pierre Ossman67a61c42007-07-11 20:22:11 +0200288 * Claim a host for a set of operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
Pierre Ossmanb8558852007-01-03 19:47:29 +0100290void mmc_claim_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291{
292 DECLARE_WAITQUEUE(wait, current);
293 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200295 might_sleep();
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 add_wait_queue(&host->wq, &wait);
298 spin_lock_irqsave(&host->lock, flags);
299 while (1) {
300 set_current_state(TASK_UNINTERRUPTIBLE);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100301 if (!host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 break;
303 spin_unlock_irqrestore(&host->lock, flags);
304 schedule();
305 spin_lock_irqsave(&host->lock, flags);
306 }
307 set_current_state(TASK_RUNNING);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100308 host->claimed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 spin_unlock_irqrestore(&host->lock, flags);
310 remove_wait_queue(&host->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Pierre Ossmanb8558852007-01-03 19:47:29 +0100313EXPORT_SYMBOL(mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/**
316 * mmc_release_host - release a host
317 * @host: mmc host to release
318 *
319 * Release a MMC host, allowing others to claim the host
320 * for their operations.
321 */
322void mmc_release_host(struct mmc_host *host)
323{
324 unsigned long flags;
325
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100326 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100329 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 spin_unlock_irqrestore(&host->lock, flags);
331
332 wake_up(&host->wq);
333}
334
335EXPORT_SYMBOL(mmc_release_host);
336
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100337/*
338 * Internal function that does the actual ios call to the host driver,
339 * optionally printing some debug output.
340 */
Russell King920e70c2006-05-04 18:22:51 +0100341static inline void mmc_set_ios(struct mmc_host *host)
342{
343 struct mmc_ios *ios = &host->ios;
344
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100345 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
346 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100347 mmc_hostname(host), ios->clock, ios->bus_mode,
348 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100349 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800350
Russell King920e70c2006-05-04 18:22:51 +0100351 host->ops->set_ios(host, ios);
352}
353
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100354/*
355 * Control chip select pin on a host.
356 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100357void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700358{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100359 host->ios.chip_select = mode;
360 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100364 * Sets the host clock to the highest possible frequency that
365 * is below "hz".
366 */
367void mmc_set_clock(struct mmc_host *host, unsigned int hz)
368{
369 WARN_ON(hz < host->f_min);
370
371 if (hz > host->f_max)
372 hz = host->f_max;
373
374 host->ios.clock = hz;
375 mmc_set_ios(host);
376}
377
378/*
379 * Change the bus mode (open drain/push-pull) of a host.
380 */
381void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
382{
383 host->ios.bus_mode = mode;
384 mmc_set_ios(host);
385}
386
387/*
388 * Change data bus width of a host.
389 */
390void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
391{
392 host->ios.bus_width = width;
393 mmc_set_ios(host);
394}
395
396/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 * Mask off any voltages we don't support and select
398 * the lowest voltage
399 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100400u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 int bit;
403
404 ocr &= host->ocr_avail;
405
406 bit = ffs(ocr);
407 if (bit) {
408 bit -= 1;
409
Timo Teras63ef7312006-11-02 19:43:27 +0100410 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100413 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 } else {
415 ocr = 0;
416 }
417
418 return ocr;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100422 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100424void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100426 host->ios.timing = timing;
427 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700428}
429
430/*
Russell King45f82452005-12-14 14:57:35 +0000431 * 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.
435 *
436 * We must _NOT_ enable the clock prior to power stablising.
437 *
438 * If a host does all the power sequencing itself, ignore the
439 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 */
441static void mmc_power_up(struct mmc_host *host)
442{
443 int bit = fls(host->ocr_avail) - 1;
444
445 host->ios.vdd = bit;
446 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100447 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700449 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100450 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100451 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 mmc_delay(1);
454
455 host->ios.clock = host->f_min;
456 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100457 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 mmc_delay(2);
460}
461
462static void mmc_power_off(struct mmc_host *host)
463{
464 host->ios.clock = 0;
465 host->ios.vdd = 0;
466 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100467 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700469 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100470 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100471 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100475 * Assign a mmc bus handler to a host. Only one bus handler may control a
476 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100478void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100480 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100482 BUG_ON(!host);
483 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100484
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100485 BUG_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100486
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100487 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100488
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100489 BUG_ON(host->bus_ops);
490 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100491
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100492 host->bus_ops = ops;
493 host->bus_refs = 1;
494 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100496 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100500 * Remove the current bus handler from a host. Assumes that there are
501 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100503void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100505 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100507 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100509 BUG_ON(!host->claimed);
510 BUG_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100512 spin_lock_irqsave(&host->lock, flags);
513
514 host->bus_dead = 1;
515
516 spin_unlock_irqrestore(&host->lock, flags);
517
518 mmc_power_off(host);
519
520 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100523/*
524 * Cleanup when the last reference to the bus operator is dropped.
525 */
526void __mmc_release_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100528 BUG_ON(!host);
529 BUG_ON(host->bus_refs);
530 BUG_ON(!host->bus_dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100532 host->bus_ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535/**
536 * mmc_detect_change - process change of state on a MMC socket
537 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +0100538 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
Pierre Ossman67a61c42007-07-11 20:22:11 +0200540 * 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
543 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
Richard Purdie8dc00332005-09-08 17:53:01 +0100545void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100547#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200548 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -0700549 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100550 BUG_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -0700551 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100552#endif
553
David Howellsc4028952006-11-22 14:57:56 +0000554 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
556
557EXPORT_SYMBOL(mmc_detect_change);
558
559
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200560void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
David Howellsc4028952006-11-22 14:57:56 +0000562 struct mmc_host *host =
563 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100564 u32 ocr;
565 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100567 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100569 if (host->bus_ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100571 * Only we can add a new handler, so it's safe to
572 * release the lock here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100574 mmc_bus_put(host);
575
576 mmc_claim_host(host);
577
578 mmc_power_up(host);
579 mmc_go_idle(host);
580
581 mmc_send_if_cond(host, host->ocr_avail);
582
583 err = mmc_send_app_op_cond(host, 0, &ocr);
584 if (err == MMC_ERR_NONE) {
585 if (mmc_attach_sd(host, ocr))
586 mmc_power_off(host);
587 } else {
588 /*
589 * If we fail to detect any SD cards then try
590 * searching for MMC cards.
591 */
592 err = mmc_send_op_cond(host, 0, &ocr);
593 if (err == MMC_ERR_NONE) {
594 if (mmc_attach_mmc(host, ocr))
595 mmc_power_off(host);
596 } else {
597 mmc_power_off(host);
598 mmc_release_host(host);
599 }
600 }
601 } else {
602 if (host->bus_ops->detect && !host->bus_dead)
603 host->bus_ops->detect(host);
604
605 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607}
608
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200609void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200611 mmc_power_off(host);
612 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200615void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100617#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200618 unsigned long flags;
619 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100620 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200621 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100622#endif
623
624 mmc_flush_scheduled_work();
625
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100626 mmc_bus_get(host);
627 if (host->bus_ops && !host->bus_dead) {
628 if (host->bus_ops->remove)
629 host->bus_ops->remove(host);
630
631 mmc_claim_host(host);
632 mmc_detach_bus(host);
633 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100635 mmc_bus_put(host);
636
637 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
639 mmc_power_off(host);
640}
641
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642#ifdef CONFIG_PM
643
644/**
645 * mmc_suspend_host - suspend a host
646 * @host: mmc host
647 * @state: suspend mode (PM_SUSPEND_xxx)
648 */
Pavel Macheke5378ca2005-04-16 15:25:29 -0700649int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200651 mmc_flush_scheduled_work();
652
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100653 mmc_bus_get(host);
654 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200655 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);
660
661 mmc_claim_host(host);
662 mmc_detach_bus(host);
663 mmc_release_host(host);
664 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200665 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100666 mmc_bus_put(host);
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 return 0;
671}
672
673EXPORT_SYMBOL(mmc_suspend_host);
674
675/**
676 * mmc_resume_host - resume a previously suspended host
677 * @host: mmc host
678 */
679int mmc_resume_host(struct mmc_host *host)
680{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200681 mmc_bus_get(host);
682 if (host->bus_ops && !host->bus_dead) {
683 mmc_power_up(host);
684 BUG_ON(!host->bus_ops->resume);
685 host->bus_ops->resume(host);
686 }
687 mmc_bus_put(host);
688
689 /*
690 * We add a slight delay here so that resume can progress
691 * in parallel.
692 */
693 mmc_detect_change(host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 return 0;
696}
697
698EXPORT_SYMBOL(mmc_resume_host);
699
700#endif
701
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200702static int __init mmc_init(void)
703{
704 int ret;
705
706 workqueue = create_singlethread_workqueue("kmmcd");
707 if (!workqueue)
708 return -ENOMEM;
709
710 ret = mmc_register_bus();
711 if (ret == 0) {
712 ret = mmc_register_host_class();
713 if (ret)
714 mmc_unregister_bus();
715 }
716 return ret;
717}
718
719static void __exit mmc_exit(void)
720{
721 mmc_unregister_host_class();
722 mmc_unregister_bus();
723 destroy_workqueue(workqueue);
724}
725
726module_init(mmc_init);
727module_exit(mmc_exit);
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729MODULE_LICENSE("GPL");