blob: b5d8a6d90cca6dfdac015f2e6cac031786963067 [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
71 pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n",
72 mmc_hostname(host), cmd->opcode, err,
73 mrq->data ? mrq->data->error : 0,
74 mrq->stop ? mrq->stop->error : 0,
75 cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 if (err && cmd->retries) {
78 cmd->retries--;
79 cmd->error = 0;
80 host->ops->request(host, mrq);
81 } else if (mrq->done) {
82 mrq->done(mrq);
83 }
84}
85
86EXPORT_SYMBOL(mmc_request_done);
87
88/**
89 * mmc_start_request - start a command on a host
90 * @host: MMC host to start command on
91 * @mrq: MMC request to start
92 *
93 * Queue a command on the specified host. We expect the
94 * caller to be holding the host lock with interrupts disabled.
95 */
96void
97mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
98{
Pierre Ossman976d9272007-04-13 22:47:01 +020099#ifdef CONFIG_MMC_DEBUG
100 unsigned int i, sz;
101#endif
102
Russell King920e70c2006-05-04 18:22:51 +0100103 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
104 mmc_hostname(host), mrq->cmd->opcode,
105 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100107 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 mrq->cmd->error = 0;
110 mrq->cmd->mrq = mrq;
111 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100112 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100113 BUG_ON(mrq->data->blocks > host->max_blk_count);
114 BUG_ON(mrq->data->blocks * mrq->data->blksz >
115 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100116
Pierre Ossman976d9272007-04-13 22:47:01 +0200117#ifdef CONFIG_MMC_DEBUG
118 sz = 0;
119 for (i = 0;i < mrq->data->sg_len;i++)
120 sz += mrq->data->sg[i].length;
121 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
122#endif
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 mrq->cmd->data = mrq->data;
125 mrq->data->error = 0;
126 mrq->data->mrq = mrq;
127 if (mrq->stop) {
128 mrq->data->stop = mrq->stop;
129 mrq->stop->error = 0;
130 mrq->stop->mrq = mrq;
131 }
132 }
133 host->ops->request(host, mrq);
134}
135
136EXPORT_SYMBOL(mmc_start_request);
137
138static void mmc_wait_done(struct mmc_request *mrq)
139{
140 complete(mrq->done_data);
141}
142
143int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
144{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700145 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 mrq->done_data = &complete;
148 mrq->done = mmc_wait_done;
149
150 mmc_start_request(host, mrq);
151
152 wait_for_completion(&complete);
153
154 return 0;
155}
156
157EXPORT_SYMBOL(mmc_wait_for_req);
158
159/**
160 * mmc_wait_for_cmd - start a command and wait for completion
161 * @host: MMC host to start command
162 * @cmd: MMC command to start
163 * @retries: maximum number of retries
164 *
165 * Start a new MMC command for a host, and wait for the command
166 * to complete. Return any error that occurred while the command
167 * was executing. Do not attempt to parse the response.
168 */
169int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
170{
171 struct mmc_request mrq;
172
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100173 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 memset(&mrq, 0, sizeof(struct mmc_request));
176
177 memset(cmd->resp, 0, sizeof(cmd->resp));
178 cmd->retries = retries;
179
180 mrq.cmd = cmd;
181 cmd->data = NULL;
182
183 mmc_wait_for_req(host, &mrq);
184
185 return cmd->error;
186}
187
188EXPORT_SYMBOL(mmc_wait_for_cmd);
189
Pierre Ossman335eadf2005-09-06 15:18:50 -0700190/**
Russell Kingd773d722006-09-07 15:57:12 +0100191 * mmc_set_data_timeout - set the timeout for a data command
192 * @data: data phase for command
193 * @card: the MMC card associated with the data transfer
194 * @write: flag to differentiate reads from writes
195 */
196void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card,
197 int write)
198{
199 unsigned int mult;
200
201 /*
202 * SD cards use a 100 multiplier rather than 10
203 */
204 mult = mmc_card_sd(card) ? 100 : 10;
205
206 /*
207 * Scale up the multiplier (and therefore the timeout) by
208 * the r2w factor for writes.
209 */
210 if (write)
211 mult <<= card->csd.r2w_factor;
212
213 data->timeout_ns = card->csd.tacc_ns * mult;
214 data->timeout_clks = card->csd.tacc_clks * mult;
215
216 /*
217 * SD cards also have an upper limit on the timeout.
218 */
219 if (mmc_card_sd(card)) {
220 unsigned int timeout_us, limit_us;
221
222 timeout_us = data->timeout_ns / 1000;
223 timeout_us += data->timeout_clks * 1000 /
224 (card->host->ios.clock / 1000);
225
226 if (write)
227 limit_us = 250000;
228 else
229 limit_us = 100000;
230
Philip Langdalefba68bd2007-01-04 06:57:32 -0800231 /*
232 * SDHC cards always use these fixed values.
233 */
234 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100235 data->timeout_ns = limit_us * 1000;
236 data->timeout_clks = 0;
237 }
238 }
239}
240EXPORT_SYMBOL(mmc_set_data_timeout);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242/**
243 * __mmc_claim_host - exclusively claim a host
244 * @host: mmc host to claim
245 * @card: mmc card to claim host for
246 *
247 * Claim a host for a set of operations. If a valid card
248 * is passed and this wasn't the last card selected, select
249 * the card before returning.
250 *
251 * Note: you should use mmc_card_claim_host or mmc_claim_host.
252 */
Pierre Ossmanb8558852007-01-03 19:47:29 +0100253void mmc_claim_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 DECLARE_WAITQUEUE(wait, current);
256 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 add_wait_queue(&host->wq, &wait);
259 spin_lock_irqsave(&host->lock, flags);
260 while (1) {
261 set_current_state(TASK_UNINTERRUPTIBLE);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100262 if (!host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 break;
264 spin_unlock_irqrestore(&host->lock, flags);
265 schedule();
266 spin_lock_irqsave(&host->lock, flags);
267 }
268 set_current_state(TASK_RUNNING);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100269 host->claimed = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 spin_unlock_irqrestore(&host->lock, flags);
271 remove_wait_queue(&host->wq, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Pierre Ossmanb8558852007-01-03 19:47:29 +0100274EXPORT_SYMBOL(mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276/**
277 * mmc_release_host - release a host
278 * @host: mmc host to release
279 *
280 * Release a MMC host, allowing others to claim the host
281 * for their operations.
282 */
283void mmc_release_host(struct mmc_host *host)
284{
285 unsigned long flags;
286
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100287 BUG_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100290 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 spin_unlock_irqrestore(&host->lock, flags);
292
293 wake_up(&host->wq);
294}
295
296EXPORT_SYMBOL(mmc_release_host);
297
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100298/*
299 * Internal function that does the actual ios call to the host driver,
300 * optionally printing some debug output.
301 */
Russell King920e70c2006-05-04 18:22:51 +0100302static inline void mmc_set_ios(struct mmc_host *host)
303{
304 struct mmc_ios *ios = &host->ios;
305
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100306 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
307 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100308 mmc_hostname(host), ios->clock, ios->bus_mode,
309 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100310 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800311
Russell King920e70c2006-05-04 18:22:51 +0100312 host->ops->set_ios(host, ios);
313}
314
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100315/*
316 * Control chip select pin on a host.
317 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100318void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700319{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100320 host->ios.chip_select = mode;
321 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100325 * Sets the host clock to the highest possible frequency that
326 * is below "hz".
327 */
328void mmc_set_clock(struct mmc_host *host, unsigned int hz)
329{
330 WARN_ON(hz < host->f_min);
331
332 if (hz > host->f_max)
333 hz = host->f_max;
334
335 host->ios.clock = hz;
336 mmc_set_ios(host);
337}
338
339/*
340 * Change the bus mode (open drain/push-pull) of a host.
341 */
342void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
343{
344 host->ios.bus_mode = mode;
345 mmc_set_ios(host);
346}
347
348/*
349 * Change data bus width of a host.
350 */
351void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
352{
353 host->ios.bus_width = width;
354 mmc_set_ios(host);
355}
356
357/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * Mask off any voltages we don't support and select
359 * the lowest voltage
360 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100361u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int bit;
364
365 ocr &= host->ocr_avail;
366
367 bit = ffs(ocr);
368 if (bit) {
369 bit -= 1;
370
Timo Teras63ef7312006-11-02 19:43:27 +0100371 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
373 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100374 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 } else {
376 ocr = 0;
377 }
378
379 return ocr;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100383 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100385void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100387 host->ios.timing = timing;
388 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700389}
390
391/*
Russell King45f82452005-12-14 14:57:35 +0000392 * Apply power to the MMC stack. This is a two-stage process.
393 * First, we enable power to the card without the clock running.
394 * We then wait a bit for the power to stabilise. Finally,
395 * enable the bus drivers and clock to the card.
396 *
397 * We must _NOT_ enable the clock prior to power stablising.
398 *
399 * If a host does all the power sequencing itself, ignore the
400 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 */
402static void mmc_power_up(struct mmc_host *host)
403{
404 int bit = fls(host->ocr_avail) - 1;
405
406 host->ios.vdd = bit;
407 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100408 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700410 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100411 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100412 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 mmc_delay(1);
415
416 host->ios.clock = host->f_min;
417 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100418 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 mmc_delay(2);
421}
422
423static void mmc_power_off(struct mmc_host *host)
424{
425 host->ios.clock = 0;
426 host->ios.vdd = 0;
427 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
Pierre Ossman865e9f12005-09-03 16:45:02 +0100428 host->ios.chip_select = MMC_CS_DONTCARE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700430 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100431 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100432 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100436 * Assign a mmc bus handler to a host. Only one bus handler may control a
437 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100439void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100443 BUG_ON(!host);
444 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100445
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100446 BUG_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100447
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100448 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100449
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100450 BUG_ON(host->bus_ops);
451 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100452
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100453 host->bus_ops = ops;
454 host->bus_refs = 1;
455 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100457 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
460/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100461 * Remove the current bus handler from a host. Assumes that there are
462 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100464void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100466 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100468 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100470 BUG_ON(!host->claimed);
471 BUG_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100473 spin_lock_irqsave(&host->lock, flags);
474
475 host->bus_dead = 1;
476
477 spin_unlock_irqrestore(&host->lock, flags);
478
479 mmc_power_off(host);
480
481 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100484/*
485 * Cleanup when the last reference to the bus operator is dropped.
486 */
487void __mmc_release_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100489 BUG_ON(!host);
490 BUG_ON(host->bus_refs);
491 BUG_ON(!host->bus_dead);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100493 host->bus_ops = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496/**
497 * mmc_detect_change - process change of state on a MMC socket
498 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +0100499 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *
501 * All we know is that card(s) have been inserted or removed
502 * from the socket(s). We don't know which socket or cards.
503 */
Richard Purdie8dc00332005-09-08 17:53:01 +0100504void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100506#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200507 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -0700508 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100509 BUG_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -0700510 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100511#endif
512
David Howellsc4028952006-11-22 14:57:56 +0000513 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
516EXPORT_SYMBOL(mmc_detect_change);
517
518
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200519void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
David Howellsc4028952006-11-22 14:57:56 +0000521 struct mmc_host *host =
522 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100523 u32 ocr;
524 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100526 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100528 if (host->bus_ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100530 * Only we can add a new handler, so it's safe to
531 * release the lock here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100533 mmc_bus_put(host);
534
535 mmc_claim_host(host);
536
537 mmc_power_up(host);
538 mmc_go_idle(host);
539
540 mmc_send_if_cond(host, host->ocr_avail);
541
542 err = mmc_send_app_op_cond(host, 0, &ocr);
543 if (err == MMC_ERR_NONE) {
544 if (mmc_attach_sd(host, ocr))
545 mmc_power_off(host);
546 } else {
547 /*
548 * If we fail to detect any SD cards then try
549 * searching for MMC cards.
550 */
551 err = mmc_send_op_cond(host, 0, &ocr);
552 if (err == MMC_ERR_NONE) {
553 if (mmc_attach_mmc(host, ocr))
554 mmc_power_off(host);
555 } else {
556 mmc_power_off(host);
557 mmc_release_host(host);
558 }
559 }
560 } else {
561 if (host->bus_ops->detect && !host->bus_dead)
562 host->bus_ops->detect(host);
563
564 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200568void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200570 mmc_power_off(host);
571 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572}
573
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200574void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100576#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200577 unsigned long flags;
578 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100579 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200580 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100581#endif
582
583 mmc_flush_scheduled_work();
584
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100585 mmc_bus_get(host);
586 if (host->bus_ops && !host->bus_dead) {
587 if (host->bus_ops->remove)
588 host->bus_ops->remove(host);
589
590 mmc_claim_host(host);
591 mmc_detach_bus(host);
592 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100594 mmc_bus_put(host);
595
596 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598 mmc_power_off(host);
599}
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601#ifdef CONFIG_PM
602
603/**
604 * mmc_suspend_host - suspend a host
605 * @host: mmc host
606 * @state: suspend mode (PM_SUSPEND_xxx)
607 */
Pavel Macheke5378ca2005-04-16 15:25:29 -0700608int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200610 mmc_flush_scheduled_work();
611
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100612 mmc_bus_get(host);
613 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200614 if (host->bus_ops->suspend)
615 host->bus_ops->suspend(host);
616 if (!host->bus_ops->resume) {
617 if (host->bus_ops->remove)
618 host->bus_ops->remove(host);
619
620 mmc_claim_host(host);
621 mmc_detach_bus(host);
622 mmc_release_host(host);
623 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200624 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100625 mmc_bus_put(host);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 return 0;
630}
631
632EXPORT_SYMBOL(mmc_suspend_host);
633
634/**
635 * mmc_resume_host - resume a previously suspended host
636 * @host: mmc host
637 */
638int mmc_resume_host(struct mmc_host *host)
639{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200640 mmc_bus_get(host);
641 if (host->bus_ops && !host->bus_dead) {
642 mmc_power_up(host);
643 BUG_ON(!host->bus_ops->resume);
644 host->bus_ops->resume(host);
645 }
646 mmc_bus_put(host);
647
648 /*
649 * We add a slight delay here so that resume can progress
650 * in parallel.
651 */
652 mmc_detect_change(host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
654 return 0;
655}
656
657EXPORT_SYMBOL(mmc_resume_host);
658
659#endif
660
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200661static int __init mmc_init(void)
662{
663 int ret;
664
665 workqueue = create_singlethread_workqueue("kmmcd");
666 if (!workqueue)
667 return -ENOMEM;
668
669 ret = mmc_register_bus();
670 if (ret == 0) {
671 ret = mmc_register_host_class();
672 if (ret)
673 mmc_unregister_bus();
674 }
675 return ret;
676}
677
678static void __exit mmc_exit(void)
679{
680 mmc_unregister_host_class();
681 mmc_unregister_bus();
682 destroy_workqueue(workqueue);
683}
684
685module_init(mmc_init);
686module_exit(mmc_exit);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688MODULE_LICENSE("GPL");