blob: df6ce4a06cf37f42c11935ce7f4e2f5d92409758 [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>
Anton Vorontsov86e82862008-11-26 22:54:17 +030023#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include <linux/mmc/card.h>
26#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010027#include <linux/mmc/mmc.h>
28#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010030#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020031#include "bus.h"
32#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020033#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010034
35#include "mmc_ops.h"
36#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020037#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Pierre Ossmanffce2e72007-05-19 14:32:22 +020039static struct workqueue_struct *workqueue;
40
41/*
David Brownellaf517152007-08-08 09:11:32 -070042 * Enabling software CRCs on the data blocks can be a significant (30%)
43 * performance cost, and for other reasons may not always be desired.
44 * So we allow it it to be disabled.
45 */
46int use_spi_crc = 1;
47module_param(use_spi_crc, bool, 0);
48
49/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020050 * Internal function. Schedule delayed work in the MMC work queue.
51 */
52static int mmc_schedule_delayed_work(struct delayed_work *work,
53 unsigned long delay)
54{
55 return queue_delayed_work(workqueue, work, delay);
56}
57
58/*
59 * Internal function. Flush all scheduled work from the MMC work queue.
60 */
61static void mmc_flush_scheduled_work(void)
62{
63 flush_workqueue(workqueue);
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/**
Russell Kingfe10c6a2006-05-04 13:51:45 +010067 * mmc_request_done - finish processing an MMC request
68 * @host: MMC host which completed request
69 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 *
71 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010072 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 */
74void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
75{
76 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010077 int err = cmd->error;
78
David Brownellaf517152007-08-08 09:11:32 -070079 if (err && cmd->retries && mmc_host_is_spi(host)) {
80 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
81 cmd->retries = 0;
82 }
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 if (err && cmd->retries) {
Pierre Ossmane4d21702007-07-24 21:46:49 +020085 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
86 mmc_hostname(host), cmd->opcode, err);
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 cmd->retries--;
89 cmd->error = 0;
90 host->ops->request(host, mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +020091 } else {
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020092 led_trigger_event(host->led, LED_OFF);
93
Pierre Ossmane4d21702007-07-24 21:46:49 +020094 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
95 mmc_hostname(host), cmd->opcode, err,
96 cmd->resp[0], cmd->resp[1],
97 cmd->resp[2], cmd->resp[3]);
98
99 if (mrq->data) {
100 pr_debug("%s: %d bytes transferred: %d\n",
101 mmc_hostname(host),
102 mrq->data->bytes_xfered, mrq->data->error);
103 }
104
105 if (mrq->stop) {
106 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
107 mmc_hostname(host), mrq->stop->opcode,
108 mrq->stop->error,
109 mrq->stop->resp[0], mrq->stop->resp[1],
110 mrq->stop->resp[2], mrq->stop->resp[3]);
111 }
112
113 if (mrq->done)
114 mrq->done(mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
116}
117
118EXPORT_SYMBOL(mmc_request_done);
119
Adrian Bunk39361852007-07-25 00:40:58 +0200120static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
122{
Pierre Ossman976d9272007-04-13 22:47:01 +0200123#ifdef CONFIG_MMC_DEBUG
124 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200125 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200126#endif
127
Russell King920e70c2006-05-04 18:22:51 +0100128 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
129 mmc_hostname(host), mrq->cmd->opcode,
130 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Pierre Ossmane4d21702007-07-24 21:46:49 +0200132 if (mrq->data) {
133 pr_debug("%s: blksz %d blocks %d flags %08x "
134 "tsac %d ms nsac %d\n",
135 mmc_hostname(host), mrq->data->blksz,
136 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200137 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200138 mrq->data->timeout_clks);
139 }
140
141 if (mrq->stop) {
142 pr_debug("%s: CMD%u arg %08x flags %08x\n",
143 mmc_hostname(host), mrq->stop->opcode,
144 mrq->stop->arg, mrq->stop->flags);
145 }
146
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100147 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200149 led_trigger_event(host->led, LED_FULL);
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 mrq->cmd->error = 0;
152 mrq->cmd->mrq = mrq;
153 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100154 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100155 BUG_ON(mrq->data->blocks > host->max_blk_count);
156 BUG_ON(mrq->data->blocks * mrq->data->blksz >
157 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100158
Pierre Ossman976d9272007-04-13 22:47:01 +0200159#ifdef CONFIG_MMC_DEBUG
160 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200161 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
162 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200163 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
164#endif
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 mrq->cmd->data = mrq->data;
167 mrq->data->error = 0;
168 mrq->data->mrq = mrq;
169 if (mrq->stop) {
170 mrq->data->stop = mrq->stop;
171 mrq->stop->error = 0;
172 mrq->stop->mrq = mrq;
173 }
174 }
175 host->ops->request(host, mrq);
176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178static void mmc_wait_done(struct mmc_request *mrq)
179{
180 complete(mrq->done_data);
181}
182
Pierre Ossman67a61c42007-07-11 20:22:11 +0200183/**
184 * mmc_wait_for_req - start a request and wait for completion
185 * @host: MMC host to start command
186 * @mrq: MMC request to start
187 *
188 * Start a new MMC custom command request for a host, and wait
189 * for the command to complete. Does not attempt to parse the
190 * response.
191 */
192void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700194 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196 mrq->done_data = &complete;
197 mrq->done = mmc_wait_done;
198
199 mmc_start_request(host, mrq);
200
201 wait_for_completion(&complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
204EXPORT_SYMBOL(mmc_wait_for_req);
205
206/**
207 * mmc_wait_for_cmd - start a command and wait for completion
208 * @host: MMC host to start command
209 * @cmd: MMC command to start
210 * @retries: maximum number of retries
211 *
212 * Start a new MMC command for a host, and wait for the command
213 * to complete. Return any error that occurred while the command
214 * was executing. Do not attempt to parse the response.
215 */
216int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
217{
218 struct mmc_request mrq;
219
Pierre Ossmand84075c82007-08-09 13:23:56 +0200220 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 memset(&mrq, 0, sizeof(struct mmc_request));
223
224 memset(cmd->resp, 0, sizeof(cmd->resp));
225 cmd->retries = retries;
226
227 mrq.cmd = cmd;
228 cmd->data = NULL;
229
230 mmc_wait_for_req(host, &mrq);
231
232 return cmd->error;
233}
234
235EXPORT_SYMBOL(mmc_wait_for_cmd);
236
Pierre Ossman335eadf2005-09-06 15:18:50 -0700237/**
Russell Kingd773d722006-09-07 15:57:12 +0100238 * mmc_set_data_timeout - set the timeout for a data command
239 * @data: data phase for command
240 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200241 *
242 * Computes the data timeout parameters according to the
243 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100244 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200245void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100246{
247 unsigned int mult;
248
249 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200250 * SDIO cards only define an upper 1 s limit on access.
251 */
252 if (mmc_card_sdio(card)) {
253 data->timeout_ns = 1000000000;
254 data->timeout_clks = 0;
255 return;
256 }
257
258 /*
Russell Kingd773d722006-09-07 15:57:12 +0100259 * SD cards use a 100 multiplier rather than 10
260 */
261 mult = mmc_card_sd(card) ? 100 : 10;
262
263 /*
264 * Scale up the multiplier (and therefore the timeout) by
265 * the r2w factor for writes.
266 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200267 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100268 mult <<= card->csd.r2w_factor;
269
270 data->timeout_ns = card->csd.tacc_ns * mult;
271 data->timeout_clks = card->csd.tacc_clks * mult;
272
273 /*
274 * SD cards also have an upper limit on the timeout.
275 */
276 if (mmc_card_sd(card)) {
277 unsigned int timeout_us, limit_us;
278
279 timeout_us = data->timeout_ns / 1000;
280 timeout_us += data->timeout_clks * 1000 /
281 (card->host->ios.clock / 1000);
282
Pierre Ossmanb146d262007-07-24 19:16:54 +0200283 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100284 /*
285 * The limit is really 250 ms, but that is
286 * insufficient for some crappy cards.
287 */
288 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100289 else
290 limit_us = 100000;
291
Philip Langdalefba68bd2007-01-04 06:57:32 -0800292 /*
293 * SDHC cards always use these fixed values.
294 */
295 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100296 data->timeout_ns = limit_us * 1000;
297 data->timeout_clks = 0;
298 }
299 }
300}
301EXPORT_SYMBOL(mmc_set_data_timeout);
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200304 * mmc_align_data_size - pads a transfer size to a more optimal value
305 * @card: the MMC card associated with the data transfer
306 * @sz: original transfer size
307 *
308 * Pads the original data size with a number of extra bytes in
309 * order to avoid controller bugs and/or performance hits
310 * (e.g. some controllers revert to PIO for certain sizes).
311 *
312 * Returns the improved size, which might be unmodified.
313 *
314 * Note that this function is only relevant when issuing a
315 * single scatter gather entry.
316 */
317unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
318{
319 /*
320 * FIXME: We don't have a system for the controller to tell
321 * the core about its problems yet, so for now we just 32-bit
322 * align the size.
323 */
324 sz = ((sz + 3) / 4) * 4;
325
326 return sz;
327}
328EXPORT_SYMBOL(mmc_align_data_size);
329
330/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200331 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200333 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200335 * Claim a host for a set of operations. If @abort is non null and
336 * dereference a non-zero value then this will return prematurely with
337 * that non-zero value without acquiring the lock. Returns zero
338 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200340int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 DECLARE_WAITQUEUE(wait, current);
343 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200344 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200346 might_sleep();
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 add_wait_queue(&host->wq, &wait);
349 spin_lock_irqsave(&host->lock, flags);
350 while (1) {
351 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200352 stop = abort ? atomic_read(abort) : 0;
353 if (stop || !host->claimed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 break;
355 spin_unlock_irqrestore(&host->lock, flags);
356 schedule();
357 spin_lock_irqsave(&host->lock, flags);
358 }
359 set_current_state(TASK_RUNNING);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200360 if (!stop)
361 host->claimed = 1;
362 else
363 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_unlock_irqrestore(&host->lock, flags);
365 remove_wait_queue(&host->wq, &wait);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200366 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Nicolas Pitre2342f332007-06-30 16:21:52 +0200369EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371/**
372 * mmc_release_host - release a host
373 * @host: mmc host to release
374 *
375 * Release a MMC host, allowing others to claim the host
376 * for their operations.
377 */
378void mmc_release_host(struct mmc_host *host)
379{
380 unsigned long flags;
381
Pierre Ossmand84075c82007-08-09 13:23:56 +0200382 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100385 host->claimed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_unlock_irqrestore(&host->lock, flags);
387
388 wake_up(&host->wq);
389}
390
391EXPORT_SYMBOL(mmc_release_host);
392
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100393/*
394 * Internal function that does the actual ios call to the host driver,
395 * optionally printing some debug output.
396 */
Russell King920e70c2006-05-04 18:22:51 +0100397static inline void mmc_set_ios(struct mmc_host *host)
398{
399 struct mmc_ios *ios = &host->ios;
400
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100401 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
402 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100403 mmc_hostname(host), ios->clock, ios->bus_mode,
404 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100405 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800406
Russell King920e70c2006-05-04 18:22:51 +0100407 host->ops->set_ios(host, ios);
408}
409
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100410/*
411 * Control chip select pin on a host.
412 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100413void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700414{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100415 host->ios.chip_select = mode;
416 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
419/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100420 * Sets the host clock to the highest possible frequency that
421 * is below "hz".
422 */
423void mmc_set_clock(struct mmc_host *host, unsigned int hz)
424{
425 WARN_ON(hz < host->f_min);
426
427 if (hz > host->f_max)
428 hz = host->f_max;
429
430 host->ios.clock = hz;
431 mmc_set_ios(host);
432}
433
434/*
435 * Change the bus mode (open drain/push-pull) of a host.
436 */
437void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
438{
439 host->ios.bus_mode = mode;
440 mmc_set_ios(host);
441}
442
443/*
444 * Change data bus width of a host.
445 */
446void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
447{
448 host->ios.bus_width = width;
449 mmc_set_ios(host);
450}
451
Anton Vorontsov86e82862008-11-26 22:54:17 +0300452/**
453 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
454 * @vdd: voltage (mV)
455 * @low_bits: prefer low bits in boundary cases
456 *
457 * This function returns the OCR bit number according to the provided @vdd
458 * value. If conversion is not possible a negative errno value returned.
459 *
460 * Depending on the @low_bits flag the function prefers low or high OCR bits
461 * on boundary voltages. For example,
462 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
463 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
464 *
465 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
466 */
467static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
468{
469 const int max_bit = ilog2(MMC_VDD_35_36);
470 int bit;
471
472 if (vdd < 1650 || vdd > 3600)
473 return -EINVAL;
474
475 if (vdd >= 1650 && vdd <= 1950)
476 return ilog2(MMC_VDD_165_195);
477
478 if (low_bits)
479 vdd -= 1;
480
481 /* Base 2000 mV, step 100 mV, bit's base 8. */
482 bit = (vdd - 2000) / 100 + 8;
483 if (bit > max_bit)
484 return max_bit;
485 return bit;
486}
487
488/**
489 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
490 * @vdd_min: minimum voltage value (mV)
491 * @vdd_max: maximum voltage value (mV)
492 *
493 * This function returns the OCR mask bits according to the provided @vdd_min
494 * and @vdd_max values. If conversion is not possible the function returns 0.
495 *
496 * Notes wrt boundary cases:
497 * This function sets the OCR bits for all boundary voltages, for example
498 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
499 * MMC_VDD_34_35 mask.
500 */
501u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
502{
503 u32 mask = 0;
504
505 if (vdd_max < vdd_min)
506 return 0;
507
508 /* Prefer high bits for the boundary vdd_max values. */
509 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
510 if (vdd_max < 0)
511 return 0;
512
513 /* Prefer low bits for the boundary vdd_min values. */
514 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
515 if (vdd_min < 0)
516 return 0;
517
518 /* Fill the mask, from max bit to min bit. */
519 while (vdd_max >= vdd_min)
520 mask |= 1 << vdd_max--;
521
522 return mask;
523}
524EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
525
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100526/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 * Mask off any voltages we don't support and select
528 * the lowest voltage
529 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100530u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
532 int bit;
533
534 ocr &= host->ocr_avail;
535
536 bit = ffs(ocr);
537 if (bit) {
538 bit -= 1;
539
Timo Teras63ef7312006-11-02 19:43:27 +0100540 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100543 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 } else {
David Brownellf6e10b82008-12-31 09:50:30 -0800545 pr_warning("%s: host doesn't support card's voltages\n",
546 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 ocr = 0;
548 }
549
550 return ocr;
551}
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100554 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100556void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100558 host->ios.timing = timing;
559 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700560}
561
562/*
Russell King45f82452005-12-14 14:57:35 +0000563 * Apply power to the MMC stack. This is a two-stage process.
564 * First, we enable power to the card without the clock running.
565 * We then wait a bit for the power to stabilise. Finally,
566 * enable the bus drivers and clock to the card.
567 *
568 * We must _NOT_ enable the clock prior to power stablising.
569 *
570 * If a host does all the power sequencing itself, ignore the
571 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 */
573static void mmc_power_up(struct mmc_host *host)
574{
575 int bit = fls(host->ocr_avail) - 1;
576
577 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -0700578 if (mmc_host_is_spi(host)) {
579 host->ios.chip_select = MMC_CS_HIGH;
580 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
581 } else {
582 host->ios.chip_select = MMC_CS_DONTCARE;
583 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700586 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100587 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100588 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200590 /*
591 * This delay should be sufficient to allow the power supply
592 * to reach the minimum voltage.
593 */
594 mmc_delay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 host->ios.clock = host->f_min;
597 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100598 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200600 /*
601 * This delay must be at least 74 clock sizes, or 1 ms, or the
602 * time required to reach a stable voltage.
603 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 mmc_delay(2);
605}
606
607static void mmc_power_off(struct mmc_host *host)
608{
609 host->ios.clock = 0;
610 host->ios.vdd = 0;
David Brownellaf517152007-08-08 09:11:32 -0700611 if (!mmc_host_is_spi(host)) {
612 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
613 host->ios.chip_select = MMC_CS_DONTCARE;
614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700616 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100617 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100618 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619}
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
Adrian Bunk39361852007-07-25 00:40:58 +0200622 * Cleanup when the last reference to the bus operator is dropped.
623 */
Adrian Bunk261172f2008-04-13 21:15:47 +0300624static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +0200625{
626 BUG_ON(!host);
627 BUG_ON(host->bus_refs);
628 BUG_ON(!host->bus_dead);
629
630 host->bus_ops = NULL;
631}
632
633/*
634 * Increase reference count of bus operator
635 */
636static inline void mmc_bus_get(struct mmc_host *host)
637{
638 unsigned long flags;
639
640 spin_lock_irqsave(&host->lock, flags);
641 host->bus_refs++;
642 spin_unlock_irqrestore(&host->lock, flags);
643}
644
645/*
646 * Decrease reference count of bus operator and free it if
647 * it is the last reference.
648 */
649static inline void mmc_bus_put(struct mmc_host *host)
650{
651 unsigned long flags;
652
653 spin_lock_irqsave(&host->lock, flags);
654 host->bus_refs--;
655 if ((host->bus_refs == 0) && host->bus_ops)
656 __mmc_release_bus(host);
657 spin_unlock_irqrestore(&host->lock, flags);
658}
659
660/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100661 * Assign a mmc bus handler to a host. Only one bus handler may control a
662 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100664void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100666 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100668 BUG_ON(!host);
669 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100670
Pierre Ossmand84075c82007-08-09 13:23:56 +0200671 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100672
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100673 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100674
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100675 BUG_ON(host->bus_ops);
676 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100677
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100678 host->bus_ops = ops;
679 host->bus_refs = 1;
680 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100682 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100686 * Remove the current bus handler from a host. Assumes that there are
687 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100689void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100691 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100693 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Pierre Ossmand84075c82007-08-09 13:23:56 +0200695 WARN_ON(!host->claimed);
696 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100698 spin_lock_irqsave(&host->lock, flags);
699
700 host->bus_dead = 1;
701
702 spin_unlock_irqrestore(&host->lock, flags);
703
704 mmc_power_off(host);
705
706 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709/**
710 * mmc_detect_change - process change of state on a MMC socket
711 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +0100712 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 *
Pierre Ossman67a61c42007-07-11 20:22:11 +0200714 * MMC drivers should call this when they detect a card has been
715 * inserted or removed. The MMC layer will confirm that any
716 * present card is still functional, and initialize any newly
717 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Richard Purdie8dc00332005-09-08 17:53:01 +0100719void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100721#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200722 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -0700723 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +0200724 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -0700725 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100726#endif
727
David Howellsc4028952006-11-22 14:57:56 +0000728 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
731EXPORT_SYMBOL(mmc_detect_change);
732
733
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200734void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
David Howellsc4028952006-11-22 14:57:56 +0000736 struct mmc_host *host =
737 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100738 u32 ocr;
739 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100741 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100743 if (host->bus_ops == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 /*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100745 * Only we can add a new handler, so it's safe to
746 * release the lock here.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100748 mmc_bus_put(host);
749
Anton Vorontsov28f52482008-06-17 18:17:15 +0400750 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
751 goto out;
752
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100753 mmc_claim_host(host);
754
755 mmc_power_up(host);
756 mmc_go_idle(host);
757
758 mmc_send_if_cond(host, host->ocr_avail);
759
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200760 /*
761 * First we search for SDIO...
762 */
763 err = mmc_send_io_op_cond(host, 0, &ocr);
764 if (!err) {
765 if (mmc_attach_sdio(host, ocr))
766 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400767 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200768 }
769
770 /*
771 * ...then normal SD...
772 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100773 err = mmc_send_app_op_cond(host, 0, &ocr);
Pierre Ossman17b04292007-07-22 22:18:46 +0200774 if (!err) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100775 if (mmc_attach_sd(host, ocr))
776 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400777 goto out;
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100778 }
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200779
780 /*
781 * ...and finally MMC.
782 */
783 err = mmc_send_op_cond(host, 0, &ocr);
784 if (!err) {
785 if (mmc_attach_mmc(host, ocr))
786 mmc_power_off(host);
Anton Vorontsov28f52482008-06-17 18:17:15 +0400787 goto out;
Pierre Ossman5c4e6f12007-05-21 20:23:20 +0200788 }
789
790 mmc_release_host(host);
791 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100792 } else {
793 if (host->bus_ops->detect && !host->bus_dead)
794 host->bus_ops->detect(host);
795
796 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
Anton Vorontsov28f52482008-06-17 18:17:15 +0400798out:
799 if (host->caps & MMC_CAP_NEEDS_POLL)
800 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801}
802
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200803void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200805 mmc_power_off(host);
806 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Pierre Ossmanb93931a2007-05-19 14:06:24 +0200809void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Pierre Ossman3b91e552007-02-11 20:43:19 +0100811#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200812 unsigned long flags;
813 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100814 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +0200815 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +0100816#endif
817
818 mmc_flush_scheduled_work();
819
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100820 mmc_bus_get(host);
821 if (host->bus_ops && !host->bus_dead) {
822 if (host->bus_ops->remove)
823 host->bus_ops->remove(host);
824
825 mmc_claim_host(host);
826 mmc_detach_bus(host);
827 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100829 mmc_bus_put(host);
830
831 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832
833 mmc_power_off(host);
834}
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836#ifdef CONFIG_PM
837
838/**
839 * mmc_suspend_host - suspend a host
840 * @host: mmc host
841 * @state: suspend mode (PM_SUSPEND_xxx)
842 */
Pavel Macheke5378ca2005-04-16 15:25:29 -0700843int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200845 mmc_flush_scheduled_work();
846
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100847 mmc_bus_get(host);
848 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200849 if (host->bus_ops->suspend)
850 host->bus_ops->suspend(host);
851 if (!host->bus_ops->resume) {
852 if (host->bus_ops->remove)
853 host->bus_ops->remove(host);
854
855 mmc_claim_host(host);
856 mmc_detach_bus(host);
857 mmc_release_host(host);
858 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +0200859 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100860 mmc_bus_put(host);
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 return 0;
865}
866
867EXPORT_SYMBOL(mmc_suspend_host);
868
869/**
870 * mmc_resume_host - resume a previously suspended host
871 * @host: mmc host
872 */
873int mmc_resume_host(struct mmc_host *host)
874{
Pierre Ossman6abaa0c2007-05-01 16:00:02 +0200875 mmc_bus_get(host);
876 if (host->bus_ops && !host->bus_dead) {
877 mmc_power_up(host);
878 BUG_ON(!host->bus_ops->resume);
879 host->bus_ops->resume(host);
880 }
881 mmc_bus_put(host);
882
883 /*
884 * We add a slight delay here so that resume can progress
885 * in parallel.
886 */
887 mmc_detect_change(host, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
889 return 0;
890}
891
892EXPORT_SYMBOL(mmc_resume_host);
893
894#endif
895
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200896static int __init mmc_init(void)
897{
898 int ret;
899
900 workqueue = create_singlethread_workqueue("kmmcd");
901 if (!workqueue)
902 return -ENOMEM;
903
904 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200905 if (ret)
906 goto destroy_workqueue;
907
908 ret = mmc_register_host_class();
909 if (ret)
910 goto unregister_bus;
911
912 ret = sdio_register_bus();
913 if (ret)
914 goto unregister_host_class;
915
916 return 0;
917
918unregister_host_class:
919 mmc_unregister_host_class();
920unregister_bus:
921 mmc_unregister_bus();
922destroy_workqueue:
923 destroy_workqueue(workqueue);
924
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200925 return ret;
926}
927
928static void __exit mmc_exit(void)
929{
Pierre Ossmane29a7d72007-05-26 13:48:18 +0200930 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200931 mmc_unregister_host_class();
932 mmc_unregister_bus();
933 destroy_workqueue(workqueue);
934}
935
Nicolas Pitre26074962007-06-16 02:07:53 -0400936subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +0200937module_exit(mmc_exit);
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939MODULE_LICENSE("GPL");