blob: ede5d1e2e20dfe4626ef3814dae5e886c8374372 [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 Ossmanaf8350c2007-09-24 07:15:48 +020021#include <linux/leds.h>
Pierre Ossmanb57c43a2005-09-06 15:18:53 -070022#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 Ossmane29a7d72007-05-26 13:48:18 +020032#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010033
34#include "mmc_ops.h"
35#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020036#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Pierre Ossmanffce2e72007-05-19 14:32:22 +020038static struct workqueue_struct *workqueue;
39
40/*
David Brownellaf517152007-08-08 09:11:32 -070041 * Enabling software CRCs on the data blocks can be a significant (30%)
42 * performance cost, and for other reasons may not always be desired.
43 * So we allow it it to be disabled.
44 */
45int use_spi_crc = 1;
46module_param(use_spi_crc, bool, 0);
47
48/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020049 * Internal function. Schedule delayed work in the MMC work queue.
50 */
51static int mmc_schedule_delayed_work(struct delayed_work *work,
52 unsigned long delay)
53{
54 return queue_delayed_work(workqueue, work, delay);
55}
56
57/*
58 * Internal function. Flush all scheduled work from the MMC work queue.
59 */
60static void mmc_flush_scheduled_work(void)
61{
62 flush_workqueue(workqueue);
63}
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065/**
Russell Kingfe10c6a2006-05-04 13:51:45 +010066 * mmc_request_done - finish processing an MMC request
67 * @host: MMC host which completed request
68 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 *
70 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010071 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 */
73void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
74{
75 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010076 int err = cmd->error;
77
David Brownellaf517152007-08-08 09:11:32 -070078 if (err && cmd->retries && mmc_host_is_spi(host)) {
79 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
80 cmd->retries = 0;
81 }
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 if (err && cmd->retries) {
Pierre Ossmane4d21702007-07-24 21:46:49 +020084 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
85 mmc_hostname(host), cmd->opcode, err);
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 cmd->retries--;
88 cmd->error = 0;
89 host->ops->request(host, mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +020090 } else {
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020091 led_trigger_event(host->led, LED_OFF);
92
Pierre Ossmane4d21702007-07-24 21:46:49 +020093 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
94 mmc_hostname(host), cmd->opcode, err,
95 cmd->resp[0], cmd->resp[1],
96 cmd->resp[2], cmd->resp[3]);
97
98 if (mrq->data) {
99 pr_debug("%s: %d bytes transferred: %d\n",
100 mmc_hostname(host),
101 mrq->data->bytes_xfered, mrq->data->error);
102 }
103
104 if (mrq->stop) {
105 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
106 mmc_hostname(host), mrq->stop->opcode,
107 mrq->stop->error,
108 mrq->stop->resp[0], mrq->stop->resp[1],
109 mrq->stop->resp[2], mrq->stop->resp[3]);
110 }
111
112 if (mrq->done)
113 mrq->done(mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115}
116
117EXPORT_SYMBOL(mmc_request_done);
118
Adrian Bunk39361852007-07-25 00:40:58 +0200119static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
121{
Pierre Ossman976d9272007-04-13 22:47:01 +0200122#ifdef CONFIG_MMC_DEBUG
123 unsigned int i, sz;
124#endif
125
Russell King920e70c2006-05-04 18:22:51 +0100126 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
127 mmc_hostname(host), mrq->cmd->opcode,
128 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Pierre Ossmane4d21702007-07-24 21:46:49 +0200130 if (mrq->data) {
131 pr_debug("%s: blksz %d blocks %d flags %08x "
132 "tsac %d ms nsac %d\n",
133 mmc_hostname(host), mrq->data->blksz,
134 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200135 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200136 mrq->data->timeout_clks);
137 }
138
139 if (mrq->stop) {
140 pr_debug("%s: CMD%u arg %08x flags %08x\n",
141 mmc_hostname(host), mrq->stop->opcode,
142 mrq->stop->arg, mrq->stop->flags);
143 }
144
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100145 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200147 led_trigger_event(host->led, LED_FULL);
148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 mrq->cmd->error = 0;
150 mrq->cmd->mrq = mrq;
151 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100152 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100153 BUG_ON(mrq->data->blocks > host->max_blk_count);
154 BUG_ON(mrq->data->blocks * mrq->data->blksz >
155 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100156
Pierre Ossman976d9272007-04-13 22:47:01 +0200157#ifdef CONFIG_MMC_DEBUG
158 sz = 0;
159 for (i = 0;i < mrq->data->sg_len;i++)
160 sz += mrq->data->sg[i].length;
161 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
162#endif
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 mrq->cmd->data = mrq->data;
165 mrq->data->error = 0;
166 mrq->data->mrq = mrq;
167 if (mrq->stop) {
168 mrq->data->stop = mrq->stop;
169 mrq->stop->error = 0;
170 mrq->stop->mrq = mrq;
171 }
172 }
173 host->ops->request(host, mrq);
174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static void mmc_wait_done(struct mmc_request *mrq)
177{
178 complete(mrq->done_data);
179}
180
Pierre Ossman67a61c42007-07-11 20:22:11 +0200181/**
182 * mmc_wait_for_req - start a request and wait for completion
183 * @host: MMC host to start command
184 * @mrq: MMC request to start
185 *
186 * Start a new MMC custom command request for a host, and wait
187 * for the command to complete. Does not attempt to parse the
188 * response.
189 */
190void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700192 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
194 mrq->done_data = &complete;
195 mrq->done = mmc_wait_done;
196
197 mmc_start_request(host, mrq);
198
199 wait_for_completion(&complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
202EXPORT_SYMBOL(mmc_wait_for_req);
203
204/**
205 * mmc_wait_for_cmd - start a command and wait for completion
206 * @host: MMC host to start command
207 * @cmd: MMC command to start
208 * @retries: maximum number of retries
209 *
210 * Start a new MMC command for a host, and wait for the command
211 * to complete. Return any error that occurred while the command
212 * was executing. Do not attempt to parse the response.
213 */
214int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
215{
216 struct mmc_request mrq;
217
Pierre Ossmand84075c82007-08-09 13:23:56 +0200218 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 memset(&mrq, 0, sizeof(struct mmc_request));
221
222 memset(cmd->resp, 0, sizeof(cmd->resp));
223 cmd->retries = retries;
224
225 mrq.cmd = cmd;
226 cmd->data = NULL;
227
228 mmc_wait_for_req(host, &mrq);
229
230 return cmd->error;
231}
232
233EXPORT_SYMBOL(mmc_wait_for_cmd);
234
Pierre Ossman335eadf2005-09-06 15:18:50 -0700235/**
Russell Kingd773d722006-09-07 15:57:12 +0100236 * mmc_set_data_timeout - set the timeout for a data command
237 * @data: data phase for command
238 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200239 *
240 * Computes the data timeout parameters according to the
241 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100242 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200243void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100244{
245 unsigned int mult;
246
247 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200248 * SDIO cards only define an upper 1 s limit on access.
249 */
250 if (mmc_card_sdio(card)) {
251 data->timeout_ns = 1000000000;
252 data->timeout_clks = 0;
253 return;
254 }
255
256 /*
Russell Kingd773d722006-09-07 15:57:12 +0100257 * SD cards use a 100 multiplier rather than 10
258 */
259 mult = mmc_card_sd(card) ? 100 : 10;
260
261 /*
262 * Scale up the multiplier (and therefore the timeout) by
263 * the r2w factor for writes.
264 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200265 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100266 mult <<= card->csd.r2w_factor;
267
268 data->timeout_ns = card->csd.tacc_ns * mult;
269 data->timeout_clks = card->csd.tacc_clks * mult;
270
271 /*
272 * SD cards also have an upper limit on the timeout.
273 */
274 if (mmc_card_sd(card)) {
275 unsigned int timeout_us, limit_us;
276
277 timeout_us = data->timeout_ns / 1000;
278 timeout_us += data->timeout_clks * 1000 /
279 (card->host->ios.clock / 1000);
280
Pierre Ossmanb146d262007-07-24 19:16:54 +0200281 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100282 limit_us = 250000;
283 else
284 limit_us = 100000;
285
Philip Langdalefba68bd2007-01-04 06:57:32 -0800286 /*
287 * SDHC cards always use these fixed values.
288 */
289 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100290 data->timeout_ns = limit_us * 1000;
291 data->timeout_clks = 0;
292 }
293 }
294}
295EXPORT_SYMBOL(mmc_set_data_timeout);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200298 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200300 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200302 * Claim a host for a set of operations. If @abort is non null and
303 * dereference a non-zero value then this will return prematurely with
304 * that non-zero value without acquiring the lock. Returns zero
305 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200307int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 DECLARE_WAITQUEUE(wait, current);
310 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200311 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200313 might_sleep();
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 add_wait_queue(&host->wq, &wait);
316 spin_lock_irqsave(&host->lock, flags);
317 while (1) {
318 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200319 stop = abort ? atomic_read(abort) : 0;
320 if (stop || !host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 spin_unlock_irqrestore(&host->lock, flags);
323 schedule();
324 spin_lock_irqsave(&host->lock, flags);
325 }
326 set_current_state(TASK_RUNNING);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200327 if (!stop)
328 host->claimed = 1;
329 else
330 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 spin_unlock_irqrestore(&host->lock, flags);
332 remove_wait_queue(&host->wq, &wait);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200333 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
Nicolas Pitre2342f332007-06-30 16:21:52 +0200336EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
338/**
339 * mmc_release_host - release a host
340 * @host: mmc host to release
341 *
342 * Release a MMC host, allowing others to claim the host
343 * for their operations.
344 */
345void mmc_release_host(struct mmc_host *host)
346{
347 unsigned long flags;
348
Pierre Ossmand84075c82007-08-09 13:23:56 +0200349 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100352 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 spin_unlock_irqrestore(&host->lock, flags);
354
355 wake_up(&host->wq);
356}
357
358EXPORT_SYMBOL(mmc_release_host);
359
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100360/*
361 * Internal function that does the actual ios call to the host driver,
362 * optionally printing some debug output.
363 */
Russell King920e70c2006-05-04 18:22:51 +0100364static inline void mmc_set_ios(struct mmc_host *host)
365{
366 struct mmc_ios *ios = &host->ios;
367
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100368 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
369 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100370 mmc_hostname(host), ios->clock, ios->bus_mode,
371 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100372 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800373
Russell King920e70c2006-05-04 18:22:51 +0100374 host->ops->set_ios(host, ios);
375}
376
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100377/*
378 * Control chip select pin on a host.
379 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100380void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700381{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100382 host->ios.chip_select = mode;
383 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
386/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100387 * Sets the host clock to the highest possible frequency that
388 * is below "hz".
389 */
390void mmc_set_clock(struct mmc_host *host, unsigned int hz)
391{
392 WARN_ON(hz < host->f_min);
393
394 if (hz > host->f_max)
395 hz = host->f_max;
396
397 host->ios.clock = hz;
398 mmc_set_ios(host);
399}
400
401/*
402 * Change the bus mode (open drain/push-pull) of a host.
403 */
404void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
405{
406 host->ios.bus_mode = mode;
407 mmc_set_ios(host);
408}
409
410/*
411 * Change data bus width of a host.
412 */
413void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
414{
415 host->ios.bus_width = width;
416 mmc_set_ios(host);
417}
418
419/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 * Mask off any voltages we don't support and select
421 * the lowest voltage
422 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100423u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{
425 int bit;
426
427 ocr &= host->ocr_avail;
428
429 bit = ffs(ocr);
430 if (bit) {
431 bit -= 1;
432
Timo Teras63ef7312006-11-02 19:43:27 +0100433 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100436 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 } else {
438 ocr = 0;
439 }
440
441 return ocr;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100445 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100447void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100449 host->ios.timing = timing;
450 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700451}
452
453/*
Russell King45f82452005-12-14 14:57:35 +0000454 * Apply power to the MMC stack. This is a two-stage process.
455 * First, we enable power to the card without the clock running.
456 * We then wait a bit for the power to stabilise. Finally,
457 * enable the bus drivers and clock to the card.
458 *
459 * We must _NOT_ enable the clock prior to power stablising.
460 *
461 * If a host does all the power sequencing itself, ignore the
462 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 */
464static void mmc_power_up(struct mmc_host *host)
465{
466 int bit = fls(host->ocr_avail) - 1;
467
468 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -0700469 if (mmc_host_is_spi(host)) {
470 host->ios.chip_select = MMC_CS_HIGH;
471 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
472 } else {
473 host->ios.chip_select = MMC_CS_DONTCARE;
474 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700477 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100478 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100479 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200481 /*
482 * This delay should be sufficient to allow the power supply
483 * to reach the minimum voltage.
484 */
485 mmc_delay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 host->ios.clock = host->f_min;
488 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100489 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200491 /*
492 * This delay must be at least 74 clock sizes, or 1 ms, or the
493 * time required to reach a stable voltage.
494 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 mmc_delay(2);
496}
497
498static void mmc_power_off(struct mmc_host *host)
499{
500 host->ios.clock = 0;
501 host->ios.vdd = 0;
David Brownellaf517152007-08-08 09:11:32 -0700502 if (!mmc_host_is_spi(host)) {
503 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
504 host->ios.chip_select = MMC_CS_DONTCARE;
505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700507 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100508 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100509 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512/*
Adrian Bunk39361852007-07-25 00:40:58 +0200513 * Cleanup when the last reference to the bus operator is dropped.
514 */
Adrian Bunk261172f2008-04-13 21:15:47 +0300515static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +0200516{
517 BUG_ON(!host);
518 BUG_ON(host->bus_refs);
519 BUG_ON(!host->bus_dead);
520
521 host->bus_ops = NULL;
522}
523
524/*
525 * Increase reference count of bus operator
526 */
527static inline void mmc_bus_get(struct mmc_host *host)
528{
529 unsigned long flags;
530
531 spin_lock_irqsave(&host->lock, flags);
532 host->bus_refs++;
533 spin_unlock_irqrestore(&host->lock, flags);
534}
535
536/*
537 * Decrease reference count of bus operator and free it if
538 * it is the last reference.
539 */
540static inline void mmc_bus_put(struct mmc_host *host)
541{
542 unsigned long flags;
543
544 spin_lock_irqsave(&host->lock, flags);
545 host->bus_refs--;
546 if ((host->bus_refs == 0) && host->bus_ops)
547 __mmc_release_bus(host);
548 spin_unlock_irqrestore(&host->lock, flags);
549}
550
551/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100552 * Assign a mmc bus handler to a host. Only one bus handler may control a
553 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100555void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100557 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100559 BUG_ON(!host);
560 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100561
Pierre Ossmand84075c82007-08-09 13:23:56 +0200562 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100563
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100564 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100565
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100566 BUG_ON(host->bus_ops);
567 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100568
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100569 host->bus_ops = ops;
570 host->bus_refs = 1;
571 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100573 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
576/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100577 * Remove the current bus handler from a host. Assumes that there are
578 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100580void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100582 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100584 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Pierre Ossmand84075c82007-08-09 13:23:56 +0200586 WARN_ON(!host->claimed);
587 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100589 spin_lock_irqsave(&host->lock, flags);
590
591 host->bus_dead = 1;
592
593 spin_unlock_irqrestore(&host->lock, flags);
594
595 mmc_power_off(host);
596
597 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600/**
601 * mmc_detect_change - process change of state on a MMC socket
602 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +0100603 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 *
Pierre Ossman67a61c42007-07-11 20:22:11 +0200605 * MMC drivers should call this when they detect a card has been
606 * inserted or removed. The MMC layer will confirm that any
607 * present card is still functional, and initialize any newly
608 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 */
Richard Purdie8dc00332005-09-08 17:53:01 +0100610void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100612#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200613 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -0700614 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200615 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -0700616 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100617#endif
618
David Howellsc4028952006-11-22 14:57:56 +0000619 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620}
621
622EXPORT_SYMBOL(mmc_detect_change);
623
624
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200625void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
David Howellsc4028952006-11-22 14:57:56 +0000627 struct mmc_host *host =
628 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100629 u32 ocr;
630 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100632 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100634 if (host->bus_ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100636 * Only we can add a new handler, so it's safe to
637 * release the lock here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100639 mmc_bus_put(host);
640
Anton Vorontsov28f52482008-06-17 18:17:15 +0400641 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
642 goto out;
643
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100644 mmc_claim_host(host);
645
646 mmc_power_up(host);
647 mmc_go_idle(host);
648
649 mmc_send_if_cond(host, host->ocr_avail);
650
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200651 /*
652 * First we search for SDIO...
653 */
654 err = mmc_send_io_op_cond(host, 0, &ocr);
655 if (!err) {
656 if (mmc_attach_sdio(host, ocr))
657 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400658 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200659 }
660
661 /*
662 * ...then normal SD...
663 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100664 err = mmc_send_app_op_cond(host, 0, &ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200665 if (!err) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100666 if (mmc_attach_sd(host, ocr))
667 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400668 goto out;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100669 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200670
671 /*
672 * ...and finally MMC.
673 */
674 err = mmc_send_op_cond(host, 0, &ocr);
675 if (!err) {
676 if (mmc_attach_mmc(host, ocr))
677 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400678 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200679 }
680
681 mmc_release_host(host);
682 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100683 } else {
684 if (host->bus_ops->detect && !host->bus_dead)
685 host->bus_ops->detect(host);
686
687 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 }
Anton Vorontsov28f52482008-06-17 18:17:15 +0400689out:
690 if (host->caps & MMC_CAP_NEEDS_POLL)
691 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
693
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200694void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200696 mmc_power_off(host);
697 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698}
699
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200700void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100702#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200703 unsigned long flags;
704 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100705 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200706 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100707#endif
708
709 mmc_flush_scheduled_work();
710
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100711 mmc_bus_get(host);
712 if (host->bus_ops && !host->bus_dead) {
713 if (host->bus_ops->remove)
714 host->bus_ops->remove(host);
715
716 mmc_claim_host(host);
717 mmc_detach_bus(host);
718 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100720 mmc_bus_put(host);
721
722 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 mmc_power_off(host);
725}
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727#ifdef CONFIG_PM
728
729/**
730 * mmc_suspend_host - suspend a host
731 * @host: mmc host
732 * @state: suspend mode (PM_SUSPEND_xxx)
733 */
Pavel Macheke5378ca2005-04-16 15:25:29 -0700734int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200736 mmc_flush_scheduled_work();
737
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100738 mmc_bus_get(host);
739 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200740 if (host->bus_ops->suspend)
741 host->bus_ops->suspend(host);
742 if (!host->bus_ops->resume) {
743 if (host->bus_ops->remove)
744 host->bus_ops->remove(host);
745
746 mmc_claim_host(host);
747 mmc_detach_bus(host);
748 mmc_release_host(host);
749 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200750 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100751 mmc_bus_put(host);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 return 0;
756}
757
758EXPORT_SYMBOL(mmc_suspend_host);
759
760/**
761 * mmc_resume_host - resume a previously suspended host
762 * @host: mmc host
763 */
764int mmc_resume_host(struct mmc_host *host)
765{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200766 mmc_bus_get(host);
767 if (host->bus_ops && !host->bus_dead) {
768 mmc_power_up(host);
769 BUG_ON(!host->bus_ops->resume);
770 host->bus_ops->resume(host);
771 }
772 mmc_bus_put(host);
773
774 /*
775 * We add a slight delay here so that resume can progress
776 * in parallel.
777 */
778 mmc_detect_change(host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 return 0;
781}
782
783EXPORT_SYMBOL(mmc_resume_host);
784
785#endif
786
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200787static int __init mmc_init(void)
788{
789 int ret;
790
791 workqueue = create_singlethread_workqueue("kmmcd");
792 if (!workqueue)
793 return -ENOMEM;
794
795 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200796 if (ret)
797 goto destroy_workqueue;
798
799 ret = mmc_register_host_class();
800 if (ret)
801 goto unregister_bus;
802
803 ret = sdio_register_bus();
804 if (ret)
805 goto unregister_host_class;
806
807 return 0;
808
809unregister_host_class:
810 mmc_unregister_host_class();
811unregister_bus:
812 mmc_unregister_bus();
813destroy_workqueue:
814 destroy_workqueue(workqueue);
815
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200816 return ret;
817}
818
819static void __exit mmc_exit(void)
820{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200821 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200822 mmc_unregister_host_class();
823 mmc_unregister_bus();
824 destroy_workqueue(workqueue);
825}
826
Nicolas Pitre26074962007-06-16 02:07:53 -0400827subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200828module_exit(mmc_exit);
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830MODULE_LICENSE("GPL");