blob: 3ee5b8c3b5ce42fa205d3bbc2ee2d5e4f6420794 [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 Ossmanad3868b2008-06-28 12:52:45 +02006 * Copyright (C) 2005-2008 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/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200298 * mmc_align_data_size - pads a transfer size to a more optimal value
299 * @card: the MMC card associated with the data transfer
300 * @sz: original transfer size
301 *
302 * Pads the original data size with a number of extra bytes in
303 * order to avoid controller bugs and/or performance hits
304 * (e.g. some controllers revert to PIO for certain sizes).
305 *
306 * Returns the improved size, which might be unmodified.
307 *
308 * Note that this function is only relevant when issuing a
309 * single scatter gather entry.
310 */
311unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
312{
313 /*
314 * FIXME: We don't have a system for the controller to tell
315 * the core about its problems yet, so for now we just 32-bit
316 * align the size.
317 */
318 sz = ((sz + 3) / 4) * 4;
319
320 return sz;
321}
322EXPORT_SYMBOL(mmc_align_data_size);
323
324/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200325 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200327 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200329 * Claim a host for a set of operations. If @abort is non null and
330 * dereference a non-zero value then this will return prematurely with
331 * that non-zero value without acquiring the lock. Returns zero
332 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200334int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 DECLARE_WAITQUEUE(wait, current);
337 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200338 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200340 might_sleep();
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 add_wait_queue(&host->wq, &wait);
343 spin_lock_irqsave(&host->lock, flags);
344 while (1) {
345 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200346 stop = abort ? atomic_read(abort) : 0;
347 if (stop || !host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 break;
349 spin_unlock_irqrestore(&host->lock, flags);
350 schedule();
351 spin_lock_irqsave(&host->lock, flags);
352 }
353 set_current_state(TASK_RUNNING);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200354 if (!stop)
355 host->claimed = 1;
356 else
357 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 spin_unlock_irqrestore(&host->lock, flags);
359 remove_wait_queue(&host->wq, &wait);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200360 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Nicolas Pitre2342f332007-06-30 16:21:52 +0200363EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/**
366 * mmc_release_host - release a host
367 * @host: mmc host to release
368 *
369 * Release a MMC host, allowing others to claim the host
370 * for their operations.
371 */
372void mmc_release_host(struct mmc_host *host)
373{
374 unsigned long flags;
375
Pierre Ossmand84075c82007-08-09 13:23:56 +0200376 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377
378 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100379 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock_irqrestore(&host->lock, flags);
381
382 wake_up(&host->wq);
383}
384
385EXPORT_SYMBOL(mmc_release_host);
386
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100387/*
388 * Internal function that does the actual ios call to the host driver,
389 * optionally printing some debug output.
390 */
Russell King920e70c2006-05-04 18:22:51 +0100391static inline void mmc_set_ios(struct mmc_host *host)
392{
393 struct mmc_ios *ios = &host->ios;
394
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100395 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
396 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100397 mmc_hostname(host), ios->clock, ios->bus_mode,
398 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100399 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800400
Russell King920e70c2006-05-04 18:22:51 +0100401 host->ops->set_ios(host, ios);
402}
403
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100404/*
405 * Control chip select pin on a host.
406 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100407void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700408{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100409 host->ios.chip_select = mode;
410 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
413/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100414 * Sets the host clock to the highest possible frequency that
415 * is below "hz".
416 */
417void mmc_set_clock(struct mmc_host *host, unsigned int hz)
418{
419 WARN_ON(hz < host->f_min);
420
421 if (hz > host->f_max)
422 hz = host->f_max;
423
424 host->ios.clock = hz;
425 mmc_set_ios(host);
426}
427
428/*
429 * Change the bus mode (open drain/push-pull) of a host.
430 */
431void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
432{
433 host->ios.bus_mode = mode;
434 mmc_set_ios(host);
435}
436
437/*
438 * Change data bus width of a host.
439 */
440void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
441{
442 host->ios.bus_width = width;
443 mmc_set_ios(host);
444}
445
446/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 * Mask off any voltages we don't support and select
448 * the lowest voltage
449 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100450u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
452 int bit;
453
454 ocr &= host->ocr_avail;
455
456 bit = ffs(ocr);
457 if (bit) {
458 bit -= 1;
459
Timo Teras63ef7312006-11-02 19:43:27 +0100460 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100463 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 } else {
465 ocr = 0;
466 }
467
468 return ocr;
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100472 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100474void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100476 host->ios.timing = timing;
477 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700478}
479
480/*
Russell King45f82452005-12-14 14:57:35 +0000481 * Apply power to the MMC stack. This is a two-stage process.
482 * First, we enable power to the card without the clock running.
483 * We then wait a bit for the power to stabilise. Finally,
484 * enable the bus drivers and clock to the card.
485 *
486 * We must _NOT_ enable the clock prior to power stablising.
487 *
488 * If a host does all the power sequencing itself, ignore the
489 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
491static void mmc_power_up(struct mmc_host *host)
492{
493 int bit = fls(host->ocr_avail) - 1;
494
495 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -0700496 if (mmc_host_is_spi(host)) {
497 host->ios.chip_select = MMC_CS_HIGH;
498 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
499 } else {
500 host->ios.chip_select = MMC_CS_DONTCARE;
501 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700504 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100505 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100506 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200508 /*
509 * This delay should be sufficient to allow the power supply
510 * to reach the minimum voltage.
511 */
512 mmc_delay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 host->ios.clock = host->f_min;
515 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100516 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200518 /*
519 * This delay must be at least 74 clock sizes, or 1 ms, or the
520 * time required to reach a stable voltage.
521 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 mmc_delay(2);
523}
524
525static void mmc_power_off(struct mmc_host *host)
526{
527 host->ios.clock = 0;
528 host->ios.vdd = 0;
David Brownellaf517152007-08-08 09:11:32 -0700529 if (!mmc_host_is_spi(host)) {
530 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
531 host->ios.chip_select = MMC_CS_DONTCARE;
532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700534 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100535 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100536 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*
Adrian Bunk39361852007-07-25 00:40:58 +0200540 * Cleanup when the last reference to the bus operator is dropped.
541 */
Adrian Bunk261172f2008-04-13 21:15:47 +0300542static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +0200543{
544 BUG_ON(!host);
545 BUG_ON(host->bus_refs);
546 BUG_ON(!host->bus_dead);
547
548 host->bus_ops = NULL;
549}
550
551/*
552 * Increase reference count of bus operator
553 */
554static inline void mmc_bus_get(struct mmc_host *host)
555{
556 unsigned long flags;
557
558 spin_lock_irqsave(&host->lock, flags);
559 host->bus_refs++;
560 spin_unlock_irqrestore(&host->lock, flags);
561}
562
563/*
564 * Decrease reference count of bus operator and free it if
565 * it is the last reference.
566 */
567static inline void mmc_bus_put(struct mmc_host *host)
568{
569 unsigned long flags;
570
571 spin_lock_irqsave(&host->lock, flags);
572 host->bus_refs--;
573 if ((host->bus_refs == 0) && host->bus_ops)
574 __mmc_release_bus(host);
575 spin_unlock_irqrestore(&host->lock, flags);
576}
577
578/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100579 * Assign a mmc bus handler to a host. Only one bus handler may control a
580 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100582void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100584 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100586 BUG_ON(!host);
587 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100588
Pierre Ossmand84075c82007-08-09 13:23:56 +0200589 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100590
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100591 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100592
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100593 BUG_ON(host->bus_ops);
594 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100595
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100596 host->bus_ops = ops;
597 host->bus_refs = 1;
598 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100600 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
603/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100604 * Remove the current bus handler from a host. Assumes that there are
605 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100607void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100609 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100611 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Pierre Ossmand84075c82007-08-09 13:23:56 +0200613 WARN_ON(!host->claimed);
614 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100616 spin_lock_irqsave(&host->lock, flags);
617
618 host->bus_dead = 1;
619
620 spin_unlock_irqrestore(&host->lock, flags);
621
622 mmc_power_off(host);
623
624 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627/**
628 * mmc_detect_change - process change of state on a MMC socket
629 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +0100630 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
Pierre Ossman67a61c42007-07-11 20:22:11 +0200632 * MMC drivers should call this when they detect a card has been
633 * inserted or removed. The MMC layer will confirm that any
634 * present card is still functional, and initialize any newly
635 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Richard Purdie8dc00332005-09-08 17:53:01 +0100637void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100639#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200640 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -0700641 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200642 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -0700643 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100644#endif
645
David Howellsc4028952006-11-22 14:57:56 +0000646 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647}
648
649EXPORT_SYMBOL(mmc_detect_change);
650
651
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200652void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
David Howellsc4028952006-11-22 14:57:56 +0000654 struct mmc_host *host =
655 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100656 u32 ocr;
657 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100659 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100661 if (host->bus_ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100663 * Only we can add a new handler, so it's safe to
664 * release the lock here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100666 mmc_bus_put(host);
667
Anton Vorontsov28f52482008-06-17 18:17:15 +0400668 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
669 goto out;
670
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100671 mmc_claim_host(host);
672
673 mmc_power_up(host);
674 mmc_go_idle(host);
675
676 mmc_send_if_cond(host, host->ocr_avail);
677
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200678 /*
679 * First we search for SDIO...
680 */
681 err = mmc_send_io_op_cond(host, 0, &ocr);
682 if (!err) {
683 if (mmc_attach_sdio(host, ocr))
684 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400685 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200686 }
687
688 /*
689 * ...then normal SD...
690 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100691 err = mmc_send_app_op_cond(host, 0, &ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200692 if (!err) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100693 if (mmc_attach_sd(host, ocr))
694 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400695 goto out;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100696 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200697
698 /*
699 * ...and finally MMC.
700 */
701 err = mmc_send_op_cond(host, 0, &ocr);
702 if (!err) {
703 if (mmc_attach_mmc(host, ocr))
704 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400705 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200706 }
707
708 mmc_release_host(host);
709 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100710 } else {
711 if (host->bus_ops->detect && !host->bus_dead)
712 host->bus_ops->detect(host);
713
714 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 }
Anton Vorontsov28f52482008-06-17 18:17:15 +0400716out:
717 if (host->caps & MMC_CAP_NEEDS_POLL)
718 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200721void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200723 mmc_power_off(host);
724 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200727void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100729#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200730 unsigned long flags;
731 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100732 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200733 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100734#endif
735
736 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) {
740 if (host->bus_ops->remove)
741 host->bus_ops->remove(host);
742
743 mmc_claim_host(host);
744 mmc_detach_bus(host);
745 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100747 mmc_bus_put(host);
748
749 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 mmc_power_off(host);
752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#ifdef CONFIG_PM
755
756/**
757 * mmc_suspend_host - suspend a host
758 * @host: mmc host
759 * @state: suspend mode (PM_SUSPEND_xxx)
760 */
Pavel Macheke5378ca2005-04-16 15:25:29 -0700761int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762{
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200763 mmc_flush_scheduled_work();
764
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100765 mmc_bus_get(host);
766 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200767 if (host->bus_ops->suspend)
768 host->bus_ops->suspend(host);
769 if (!host->bus_ops->resume) {
770 if (host->bus_ops->remove)
771 host->bus_ops->remove(host);
772
773 mmc_claim_host(host);
774 mmc_detach_bus(host);
775 mmc_release_host(host);
776 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200777 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100778 mmc_bus_put(host);
779
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 return 0;
783}
784
785EXPORT_SYMBOL(mmc_suspend_host);
786
787/**
788 * mmc_resume_host - resume a previously suspended host
789 * @host: mmc host
790 */
791int mmc_resume_host(struct mmc_host *host)
792{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200793 mmc_bus_get(host);
794 if (host->bus_ops && !host->bus_dead) {
795 mmc_power_up(host);
796 BUG_ON(!host->bus_ops->resume);
797 host->bus_ops->resume(host);
798 }
799 mmc_bus_put(host);
800
801 /*
802 * We add a slight delay here so that resume can progress
803 * in parallel.
804 */
805 mmc_detect_change(host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 return 0;
808}
809
810EXPORT_SYMBOL(mmc_resume_host);
811
812#endif
813
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200814static int __init mmc_init(void)
815{
816 int ret;
817
818 workqueue = create_singlethread_workqueue("kmmcd");
819 if (!workqueue)
820 return -ENOMEM;
821
822 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200823 if (ret)
824 goto destroy_workqueue;
825
826 ret = mmc_register_host_class();
827 if (ret)
828 goto unregister_bus;
829
830 ret = sdio_register_bus();
831 if (ret)
832 goto unregister_host_class;
833
834 return 0;
835
836unregister_host_class:
837 mmc_unregister_host_class();
838unregister_bus:
839 mmc_unregister_bus();
840destroy_workqueue:
841 destroy_workqueue(workqueue);
842
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200843 return ret;
844}
845
846static void __exit mmc_exit(void)
847{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200848 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200849 mmc_unregister_host_class();
850 mmc_unregister_bus();
851 destroy_workqueue(workqueue);
852}
853
Nicolas Pitre26074962007-06-16 02:07:53 -0400854subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200855module_exit(mmc_exit);
856
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857MODULE_LICENSE("GPL");