]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mmc/core/core.c
mmc: add 'enable' and 'disable' methods to mmc host
[net-next-2.6.git] / drivers / mmc / core / core.c
CommitLineData
1da177e4 1/*
aaac1b47 2 * linux/drivers/mmc/core/core.c
1da177e4
LT
3 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae 5 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
ad3868b2 6 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
bce40a36 7 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
1da177e4
LT
8 *
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 */
1da177e4
LT
13#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>
af8350c7 21#include <linux/leds.h>
b57c43ad 22#include <linux/scatterlist.h>
86e8286a 23#include <linux/log2.h>
5c13941a 24#include <linux/regulator/consumer.h>
1da177e4
LT
25
26#include <linux/mmc/card.h>
27#include <linux/mmc/host.h>
da7fbe58
PO
28#include <linux/mmc/mmc.h>
29#include <linux/mmc/sd.h>
1da177e4 30
aaac1b47 31#include "core.h"
ffce2e7e
PO
32#include "bus.h"
33#include "host.h"
e29a7d73 34#include "sdio_bus.h"
da7fbe58
PO
35
36#include "mmc_ops.h"
37#include "sd_ops.h"
5c4e6f13 38#include "sdio_ops.h"
1da177e4 39
ffce2e7e
PO
40static struct workqueue_struct *workqueue;
41
af517150
DB
42/*
43 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
46 */
47int use_spi_crc = 1;
48module_param(use_spi_crc, bool, 0);
49
ffce2e7e
PO
50/*
51 * Internal function. Schedule delayed work in the MMC work queue.
52 */
53static int mmc_schedule_delayed_work(struct delayed_work *work,
54 unsigned long delay)
55{
56 return queue_delayed_work(workqueue, work, delay);
57}
58
59/*
60 * Internal function. Flush all scheduled work from the MMC work queue.
61 */
62static void mmc_flush_scheduled_work(void)
63{
64 flush_workqueue(workqueue);
65}
66
1da177e4 67/**
fe10c6ab
RK
68 * mmc_request_done - finish processing an MMC request
69 * @host: MMC host which completed request
70 * @mrq: MMC request which request
1da177e4
LT
71 *
72 * MMC drivers should call this function when they have completed
fe10c6ab 73 * their processing of a request.
1da177e4
LT
74 */
75void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
76{
77 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
78 int err = cmd->error;
79
af517150
DB
80 if (err && cmd->retries && mmc_host_is_spi(host)) {
81 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
82 cmd->retries = 0;
83 }
84
1da177e4 85 if (err && cmd->retries) {
e4d21708
PO
86 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
87 mmc_hostname(host), cmd->opcode, err);
88
1da177e4
LT
89 cmd->retries--;
90 cmd->error = 0;
91 host->ops->request(host, mrq);
e4d21708 92 } else {
af8350c7
PO
93 led_trigger_event(host->led, LED_OFF);
94
e4d21708
PO
95 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
96 mmc_hostname(host), cmd->opcode, err,
97 cmd->resp[0], cmd->resp[1],
98 cmd->resp[2], cmd->resp[3]);
99
100 if (mrq->data) {
101 pr_debug("%s: %d bytes transferred: %d\n",
102 mmc_hostname(host),
103 mrq->data->bytes_xfered, mrq->data->error);
104 }
105
106 if (mrq->stop) {
107 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
108 mmc_hostname(host), mrq->stop->opcode,
109 mrq->stop->error,
110 mrq->stop->resp[0], mrq->stop->resp[1],
111 mrq->stop->resp[2], mrq->stop->resp[3]);
112 }
113
114 if (mrq->done)
115 mrq->done(mrq);
1da177e4
LT
116 }
117}
118
119EXPORT_SYMBOL(mmc_request_done);
120
39361851 121static void
1da177e4
LT
122mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
123{
976d9276
PO
124#ifdef CONFIG_MMC_DEBUG
125 unsigned int i, sz;
a84756c5 126 struct scatterlist *sg;
976d9276
PO
127#endif
128
920e70c5
RK
129 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
130 mmc_hostname(host), mrq->cmd->opcode,
131 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 132
e4d21708
PO
133 if (mrq->data) {
134 pr_debug("%s: blksz %d blocks %d flags %08x "
135 "tsac %d ms nsac %d\n",
136 mmc_hostname(host), mrq->data->blksz,
137 mrq->data->blocks, mrq->data->flags,
ce252edd 138 mrq->data->timeout_ns / 1000000,
e4d21708
PO
139 mrq->data->timeout_clks);
140 }
141
142 if (mrq->stop) {
143 pr_debug("%s: CMD%u arg %08x flags %08x\n",
144 mmc_hostname(host), mrq->stop->opcode,
145 mrq->stop->arg, mrq->stop->flags);
146 }
147
f22ee4ed 148 WARN_ON(!host->claimed);
1da177e4 149
af8350c7
PO
150 led_trigger_event(host->led, LED_FULL);
151
1da177e4
LT
152 mrq->cmd->error = 0;
153 mrq->cmd->mrq = mrq;
154 if (mrq->data) {
fe4a3c7a 155 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
156 BUG_ON(mrq->data->blocks > host->max_blk_count);
157 BUG_ON(mrq->data->blocks * mrq->data->blksz >
158 host->max_req_size);
fe4a3c7a 159
976d9276
PO
160#ifdef CONFIG_MMC_DEBUG
161 sz = 0;
a84756c5
PO
162 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
163 sz += sg->length;
976d9276
PO
164 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
165#endif
166
1da177e4
LT
167 mrq->cmd->data = mrq->data;
168 mrq->data->error = 0;
169 mrq->data->mrq = mrq;
170 if (mrq->stop) {
171 mrq->data->stop = mrq->stop;
172 mrq->stop->error = 0;
173 mrq->stop->mrq = mrq;
174 }
175 }
176 host->ops->request(host, mrq);
177}
178
1da177e4
LT
179static void mmc_wait_done(struct mmc_request *mrq)
180{
181 complete(mrq->done_data);
182}
183
67a61c48
PO
184/**
185 * mmc_wait_for_req - start a request and wait for completion
186 * @host: MMC host to start command
187 * @mrq: MMC request to start
188 *
189 * Start a new MMC custom command request for a host, and wait
190 * for the command to complete. Does not attempt to parse the
191 * response.
192 */
193void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 194{
0afffc72 195 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
196
197 mrq->done_data = &complete;
198 mrq->done = mmc_wait_done;
199
200 mmc_start_request(host, mrq);
201
202 wait_for_completion(&complete);
1da177e4
LT
203}
204
205EXPORT_SYMBOL(mmc_wait_for_req);
206
207/**
208 * mmc_wait_for_cmd - start a command and wait for completion
209 * @host: MMC host to start command
210 * @cmd: MMC command to start
211 * @retries: maximum number of retries
212 *
213 * Start a new MMC command for a host, and wait for the command
214 * to complete. Return any error that occurred while the command
215 * was executing. Do not attempt to parse the response.
216 */
217int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
218{
219 struct mmc_request mrq;
220
d84075c8 221 WARN_ON(!host->claimed);
1da177e4
LT
222
223 memset(&mrq, 0, sizeof(struct mmc_request));
224
225 memset(cmd->resp, 0, sizeof(cmd->resp));
226 cmd->retries = retries;
227
228 mrq.cmd = cmd;
229 cmd->data = NULL;
230
231 mmc_wait_for_req(host, &mrq);
232
233 return cmd->error;
234}
235
236EXPORT_SYMBOL(mmc_wait_for_cmd);
237
d773d725
RK
238/**
239 * mmc_set_data_timeout - set the timeout for a data command
240 * @data: data phase for command
241 * @card: the MMC card associated with the data transfer
67a61c48
PO
242 *
243 * Computes the data timeout parameters according to the
244 * correct algorithm given the card type.
d773d725 245 */
b146d26a 246void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
247{
248 unsigned int mult;
249
e6f918bf
PO
250 /*
251 * SDIO cards only define an upper 1 s limit on access.
252 */
253 if (mmc_card_sdio(card)) {
254 data->timeout_ns = 1000000000;
255 data->timeout_clks = 0;
256 return;
257 }
258
d773d725
RK
259 /*
260 * SD cards use a 100 multiplier rather than 10
261 */
262 mult = mmc_card_sd(card) ? 100 : 10;
263
264 /*
265 * Scale up the multiplier (and therefore the timeout) by
266 * the r2w factor for writes.
267 */
b146d26a 268 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
269 mult <<= card->csd.r2w_factor;
270
271 data->timeout_ns = card->csd.tacc_ns * mult;
272 data->timeout_clks = card->csd.tacc_clks * mult;
273
274 /*
275 * SD cards also have an upper limit on the timeout.
276 */
277 if (mmc_card_sd(card)) {
278 unsigned int timeout_us, limit_us;
279
280 timeout_us = data->timeout_ns / 1000;
281 timeout_us += data->timeout_clks * 1000 /
282 (card->host->ios.clock / 1000);
283
b146d26a 284 if (data->flags & MMC_DATA_WRITE)
493890e7
PO
285 /*
286 * The limit is really 250 ms, but that is
287 * insufficient for some crappy cards.
288 */
289 limit_us = 300000;
d773d725
RK
290 else
291 limit_us = 100000;
292
fba68bd2
PL
293 /*
294 * SDHC cards always use these fixed values.
295 */
296 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
297 data->timeout_ns = limit_us * 1000;
298 data->timeout_clks = 0;
299 }
300 }
c0c88871
WM
301 /*
302 * Some cards need very high timeouts if driven in SPI mode.
303 * The worst observed timeout was 900ms after writing a
304 * continuous stream of data until the internal logic
305 * overflowed.
306 */
307 if (mmc_host_is_spi(card->host)) {
308 if (data->flags & MMC_DATA_WRITE) {
309 if (data->timeout_ns < 1000000000)
310 data->timeout_ns = 1000000000; /* 1s */
311 } else {
312 if (data->timeout_ns < 100000000)
313 data->timeout_ns = 100000000; /* 100ms */
314 }
315 }
d773d725
RK
316}
317EXPORT_SYMBOL(mmc_set_data_timeout);
318
ad3868b2
PO
319/**
320 * mmc_align_data_size - pads a transfer size to a more optimal value
321 * @card: the MMC card associated with the data transfer
322 * @sz: original transfer size
323 *
324 * Pads the original data size with a number of extra bytes in
325 * order to avoid controller bugs and/or performance hits
326 * (e.g. some controllers revert to PIO for certain sizes).
327 *
328 * Returns the improved size, which might be unmodified.
329 *
330 * Note that this function is only relevant when issuing a
331 * single scatter gather entry.
332 */
333unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
334{
335 /*
336 * FIXME: We don't have a system for the controller to tell
337 * the core about its problems yet, so for now we just 32-bit
338 * align the size.
339 */
340 sz = ((sz + 3) / 4) * 4;
341
342 return sz;
343}
344EXPORT_SYMBOL(mmc_align_data_size);
345
8ea926b2
AH
346/**
347 * mmc_host_enable - enable a host.
348 * @host: mmc host to enable
349 *
350 * Hosts that support power saving can use the 'enable' and 'disable'
351 * methods to exit and enter power saving states. For more information
352 * see comments for struct mmc_host_ops.
353 */
354int mmc_host_enable(struct mmc_host *host)
355{
356 if (!(host->caps & MMC_CAP_DISABLE))
357 return 0;
358
359 if (host->en_dis_recurs)
360 return 0;
361
362 if (host->nesting_cnt++)
363 return 0;
364
365 cancel_delayed_work_sync(&host->disable);
366
367 if (host->enabled)
368 return 0;
369
370 if (host->ops->enable) {
371 int err;
372
373 host->en_dis_recurs = 1;
374 err = host->ops->enable(host);
375 host->en_dis_recurs = 0;
376
377 if (err) {
378 pr_debug("%s: enable error %d\n",
379 mmc_hostname(host), err);
380 return err;
381 }
382 }
383 host->enabled = 1;
384 return 0;
385}
386EXPORT_SYMBOL(mmc_host_enable);
387
388static int mmc_host_do_disable(struct mmc_host *host, int lazy)
389{
390 if (host->ops->disable) {
391 int err;
392
393 host->en_dis_recurs = 1;
394 err = host->ops->disable(host, lazy);
395 host->en_dis_recurs = 0;
396
397 if (err < 0) {
398 pr_debug("%s: disable error %d\n",
399 mmc_hostname(host), err);
400 return err;
401 }
402 if (err > 0) {
403 unsigned long delay = msecs_to_jiffies(err);
404
405 mmc_schedule_delayed_work(&host->disable, delay);
406 }
407 }
408 host->enabled = 0;
409 return 0;
410}
411
412/**
413 * mmc_host_disable - disable a host.
414 * @host: mmc host to disable
415 *
416 * Hosts that support power saving can use the 'enable' and 'disable'
417 * methods to exit and enter power saving states. For more information
418 * see comments for struct mmc_host_ops.
419 */
420int mmc_host_disable(struct mmc_host *host)
421{
422 int err;
423
424 if (!(host->caps & MMC_CAP_DISABLE))
425 return 0;
426
427 if (host->en_dis_recurs)
428 return 0;
429
430 if (--host->nesting_cnt)
431 return 0;
432
433 if (!host->enabled)
434 return 0;
435
436 err = mmc_host_do_disable(host, 0);
437 return err;
438}
439EXPORT_SYMBOL(mmc_host_disable);
440
1da177e4 441/**
2342f332 442 * __mmc_claim_host - exclusively claim a host
1da177e4 443 * @host: mmc host to claim
2342f332 444 * @abort: whether or not the operation should be aborted
1da177e4 445 *
2342f332
NP
446 * Claim a host for a set of operations. If @abort is non null and
447 * dereference a non-zero value then this will return prematurely with
448 * that non-zero value without acquiring the lock. Returns zero
449 * with the lock held otherwise.
1da177e4 450 */
2342f332 451int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1da177e4
LT
452{
453 DECLARE_WAITQUEUE(wait, current);
454 unsigned long flags;
2342f332 455 int stop;
1da177e4 456
cf795bfb
PO
457 might_sleep();
458
1da177e4
LT
459 add_wait_queue(&host->wq, &wait);
460 spin_lock_irqsave(&host->lock, flags);
461 while (1) {
462 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332
NP
463 stop = abort ? atomic_read(abort) : 0;
464 if (stop || !host->claimed)
1da177e4
LT
465 break;
466 spin_unlock_irqrestore(&host->lock, flags);
467 schedule();
468 spin_lock_irqsave(&host->lock, flags);
469 }
470 set_current_state(TASK_RUNNING);
2342f332
NP
471 if (!stop)
472 host->claimed = 1;
473 else
474 wake_up(&host->wq);
1da177e4
LT
475 spin_unlock_irqrestore(&host->lock, flags);
476 remove_wait_queue(&host->wq, &wait);
8ea926b2
AH
477 if (!stop)
478 mmc_host_enable(host);
2342f332 479 return stop;
1da177e4
LT
480}
481
2342f332 482EXPORT_SYMBOL(__mmc_claim_host);
1da177e4 483
8ea926b2
AH
484static int mmc_try_claim_host(struct mmc_host *host)
485{
486 int claimed_host = 0;
487 unsigned long flags;
488
489 spin_lock_irqsave(&host->lock, flags);
490 if (!host->claimed) {
491 host->claimed = 1;
492 claimed_host = 1;
493 }
494 spin_unlock_irqrestore(&host->lock, flags);
495 return claimed_host;
496}
497
498static void mmc_do_release_host(struct mmc_host *host)
499{
500 unsigned long flags;
501
502 spin_lock_irqsave(&host->lock, flags);
503 host->claimed = 0;
504 spin_unlock_irqrestore(&host->lock, flags);
505
506 wake_up(&host->wq);
507}
508
509void mmc_host_deeper_disable(struct work_struct *work)
510{
511 struct mmc_host *host =
512 container_of(work, struct mmc_host, disable.work);
513
514 /* If the host is claimed then we do not want to disable it anymore */
515 if (!mmc_try_claim_host(host))
516 return;
517 mmc_host_do_disable(host, 1);
518 mmc_do_release_host(host);
519}
520
521/**
522 * mmc_host_lazy_disable - lazily disable a host.
523 * @host: mmc host to disable
524 *
525 * Hosts that support power saving can use the 'enable' and 'disable'
526 * methods to exit and enter power saving states. For more information
527 * see comments for struct mmc_host_ops.
528 */
529int mmc_host_lazy_disable(struct mmc_host *host)
530{
531 if (!(host->caps & MMC_CAP_DISABLE))
532 return 0;
533
534 if (host->en_dis_recurs)
535 return 0;
536
537 if (--host->nesting_cnt)
538 return 0;
539
540 if (!host->enabled)
541 return 0;
542
543 if (host->disable_delay) {
544 mmc_schedule_delayed_work(&host->disable,
545 msecs_to_jiffies(host->disable_delay));
546 return 0;
547 } else
548 return mmc_host_do_disable(host, 1);
549}
550EXPORT_SYMBOL(mmc_host_lazy_disable);
551
1da177e4
LT
552/**
553 * mmc_release_host - release a host
554 * @host: mmc host to release
555 *
556 * Release a MMC host, allowing others to claim the host
557 * for their operations.
558 */
559void mmc_release_host(struct mmc_host *host)
560{
d84075c8 561 WARN_ON(!host->claimed);
1da177e4 562
8ea926b2 563 mmc_host_lazy_disable(host);
1da177e4 564
8ea926b2 565 mmc_do_release_host(host);
1da177e4
LT
566}
567
568EXPORT_SYMBOL(mmc_release_host);
569
7ea239d9
PO
570/*
571 * Internal function that does the actual ios call to the host driver,
572 * optionally printing some debug output.
573 */
920e70c5
RK
574static inline void mmc_set_ios(struct mmc_host *host)
575{
576 struct mmc_ios *ios = &host->ios;
577
cd9277c0
PO
578 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
579 "width %u timing %u\n",
920e70c5
RK
580 mmc_hostname(host), ios->clock, ios->bus_mode,
581 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 582 ios->bus_width, ios->timing);
fba68bd2 583
920e70c5
RK
584 host->ops->set_ios(host, ios);
585}
586
7ea239d9
PO
587/*
588 * Control chip select pin on a host.
589 */
da7fbe58 590void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 591{
da7fbe58
PO
592 host->ios.chip_select = mode;
593 mmc_set_ios(host);
1da177e4
LT
594}
595
7ea239d9
PO
596/*
597 * Sets the host clock to the highest possible frequency that
598 * is below "hz".
599 */
600void mmc_set_clock(struct mmc_host *host, unsigned int hz)
601{
602 WARN_ON(hz < host->f_min);
603
604 if (hz > host->f_max)
605 hz = host->f_max;
606
607 host->ios.clock = hz;
608 mmc_set_ios(host);
609}
610
611/*
612 * Change the bus mode (open drain/push-pull) of a host.
613 */
614void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
615{
616 host->ios.bus_mode = mode;
617 mmc_set_ios(host);
618}
619
620/*
621 * Change data bus width of a host.
622 */
623void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
624{
625 host->ios.bus_width = width;
626 mmc_set_ios(host);
627}
628
86e8286a
AV
629/**
630 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
631 * @vdd: voltage (mV)
632 * @low_bits: prefer low bits in boundary cases
633 *
634 * This function returns the OCR bit number according to the provided @vdd
635 * value. If conversion is not possible a negative errno value returned.
636 *
637 * Depending on the @low_bits flag the function prefers low or high OCR bits
638 * on boundary voltages. For example,
639 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
640 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
641 *
642 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
643 */
644static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
645{
646 const int max_bit = ilog2(MMC_VDD_35_36);
647 int bit;
648
649 if (vdd < 1650 || vdd > 3600)
650 return -EINVAL;
651
652 if (vdd >= 1650 && vdd <= 1950)
653 return ilog2(MMC_VDD_165_195);
654
655 if (low_bits)
656 vdd -= 1;
657
658 /* Base 2000 mV, step 100 mV, bit's base 8. */
659 bit = (vdd - 2000) / 100 + 8;
660 if (bit > max_bit)
661 return max_bit;
662 return bit;
663}
664
665/**
666 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
667 * @vdd_min: minimum voltage value (mV)
668 * @vdd_max: maximum voltage value (mV)
669 *
670 * This function returns the OCR mask bits according to the provided @vdd_min
671 * and @vdd_max values. If conversion is not possible the function returns 0.
672 *
673 * Notes wrt boundary cases:
674 * This function sets the OCR bits for all boundary voltages, for example
675 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
676 * MMC_VDD_34_35 mask.
677 */
678u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
679{
680 u32 mask = 0;
681
682 if (vdd_max < vdd_min)
683 return 0;
684
685 /* Prefer high bits for the boundary vdd_max values. */
686 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
687 if (vdd_max < 0)
688 return 0;
689
690 /* Prefer low bits for the boundary vdd_min values. */
691 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
692 if (vdd_min < 0)
693 return 0;
694
695 /* Fill the mask, from max bit to min bit. */
696 while (vdd_max >= vdd_min)
697 mask |= 1 << vdd_max--;
698
699 return mask;
700}
701EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
702
5c13941a
DB
703#ifdef CONFIG_REGULATOR
704
705/**
706 * mmc_regulator_get_ocrmask - return mask of supported voltages
707 * @supply: regulator to use
708 *
709 * This returns either a negative errno, or a mask of voltages that
710 * can be provided to MMC/SD/SDIO devices using the specified voltage
711 * regulator. This would normally be called before registering the
712 * MMC host adapter.
713 */
714int mmc_regulator_get_ocrmask(struct regulator *supply)
715{
716 int result = 0;
717 int count;
718 int i;
719
720 count = regulator_count_voltages(supply);
721 if (count < 0)
722 return count;
723
724 for (i = 0; i < count; i++) {
725 int vdd_uV;
726 int vdd_mV;
727
728 vdd_uV = regulator_list_voltage(supply, i);
729 if (vdd_uV <= 0)
730 continue;
731
732 vdd_mV = vdd_uV / 1000;
733 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
734 }
735
736 return result;
737}
738EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
739
740/**
741 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
742 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
743 * @supply: regulator to use
744 *
745 * Returns zero on success, else negative errno.
746 *
747 * MMC host drivers may use this to enable or disable a regulator using
748 * a particular supply voltage. This would normally be called from the
749 * set_ios() method.
750 */
751int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
752{
753 int result = 0;
754 int min_uV, max_uV;
755 int enabled;
756
757 enabled = regulator_is_enabled(supply);
758 if (enabled < 0)
759 return enabled;
760
761 if (vdd_bit) {
762 int tmp;
763 int voltage;
764
765 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
766 * bits this regulator doesn't quite support ... don't
767 * be too picky, most cards and regulators are OK with
768 * a 0.1V range goof (it's a small error percentage).
769 */
770 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
771 if (tmp == 0) {
772 min_uV = 1650 * 1000;
773 max_uV = 1950 * 1000;
774 } else {
775 min_uV = 1900 * 1000 + tmp * 100 * 1000;
776 max_uV = min_uV + 100 * 1000;
777 }
778
779 /* avoid needless changes to this voltage; the regulator
780 * might not allow this operation
781 */
782 voltage = regulator_get_voltage(supply);
783 if (voltage < 0)
784 result = voltage;
785 else if (voltage < min_uV || voltage > max_uV)
786 result = regulator_set_voltage(supply, min_uV, max_uV);
787 else
788 result = 0;
789
790 if (result == 0 && !enabled)
791 result = regulator_enable(supply);
792 } else if (enabled) {
793 result = regulator_disable(supply);
794 }
795
796 return result;
797}
798EXPORT_SYMBOL(mmc_regulator_set_ocr);
799
800#endif
801
1da177e4
LT
802/*
803 * Mask off any voltages we don't support and select
804 * the lowest voltage
805 */
7ea239d9 806u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
807{
808 int bit;
809
810 ocr &= host->ocr_avail;
811
812 bit = ffs(ocr);
813 if (bit) {
814 bit -= 1;
815
63ef731a 816 ocr &= 3 << bit;
1da177e4
LT
817
818 host->ios.vdd = bit;
920e70c5 819 mmc_set_ios(host);
1da177e4 820 } else {
f6e10b86
DB
821 pr_warning("%s: host doesn't support card's voltages\n",
822 mmc_hostname(host));
1da177e4
LT
823 ocr = 0;
824 }
825
826 return ocr;
827}
828
b57c43ad 829/*
7ea239d9 830 * Select timing parameters for host.
b57c43ad 831 */
7ea239d9 832void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 833{
7ea239d9
PO
834 host->ios.timing = timing;
835 mmc_set_ios(host);
b57c43ad
PO
836}
837
1da177e4 838/*
45f8245b
RK
839 * Apply power to the MMC stack. This is a two-stage process.
840 * First, we enable power to the card without the clock running.
841 * We then wait a bit for the power to stabilise. Finally,
842 * enable the bus drivers and clock to the card.
843 *
844 * We must _NOT_ enable the clock prior to power stablising.
845 *
846 * If a host does all the power sequencing itself, ignore the
847 * initial MMC_POWER_UP stage.
1da177e4
LT
848 */
849static void mmc_power_up(struct mmc_host *host)
850{
500f3564
BR
851 int bit;
852
853 /* If ocr is set, we use it */
854 if (host->ocr)
855 bit = ffs(host->ocr) - 1;
856 else
857 bit = fls(host->ocr_avail) - 1;
1da177e4
LT
858
859 host->ios.vdd = bit;
af517150
DB
860 if (mmc_host_is_spi(host)) {
861 host->ios.chip_select = MMC_CS_HIGH;
862 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
863 } else {
864 host->ios.chip_select = MMC_CS_DONTCARE;
865 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
866 }
1da177e4 867 host->ios.power_mode = MMC_POWER_UP;
f218278a 868 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 869 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 870 mmc_set_ios(host);
1da177e4 871
f9996aee
PO
872 /*
873 * This delay should be sufficient to allow the power supply
874 * to reach the minimum voltage.
875 */
79bccc5a 876 mmc_delay(10);
1da177e4 877
8dfd0374
SH
878 if (host->f_min > 400000) {
879 pr_warning("%s: Minimum clock frequency too high for "
880 "identification mode\n", mmc_hostname(host));
881 host->ios.clock = host->f_min;
882 } else
883 host->ios.clock = 400000;
884
1da177e4 885 host->ios.power_mode = MMC_POWER_ON;
920e70c5 886 mmc_set_ios(host);
1da177e4 887
f9996aee
PO
888 /*
889 * This delay must be at least 74 clock sizes, or 1 ms, or the
890 * time required to reach a stable voltage.
891 */
79bccc5a 892 mmc_delay(10);
1da177e4
LT
893}
894
895static void mmc_power_off(struct mmc_host *host)
896{
897 host->ios.clock = 0;
898 host->ios.vdd = 0;
af517150
DB
899 if (!mmc_host_is_spi(host)) {
900 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
901 host->ios.chip_select = MMC_CS_DONTCARE;
902 }
1da177e4 903 host->ios.power_mode = MMC_POWER_OFF;
f218278a 904 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 905 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 906 mmc_set_ios(host);
1da177e4
LT
907}
908
39361851
AB
909/*
910 * Cleanup when the last reference to the bus operator is dropped.
911 */
261172fd 912static void __mmc_release_bus(struct mmc_host *host)
39361851
AB
913{
914 BUG_ON(!host);
915 BUG_ON(host->bus_refs);
916 BUG_ON(!host->bus_dead);
917
918 host->bus_ops = NULL;
919}
920
921/*
922 * Increase reference count of bus operator
923 */
924static inline void mmc_bus_get(struct mmc_host *host)
925{
926 unsigned long flags;
927
928 spin_lock_irqsave(&host->lock, flags);
929 host->bus_refs++;
930 spin_unlock_irqrestore(&host->lock, flags);
931}
932
933/*
934 * Decrease reference count of bus operator and free it if
935 * it is the last reference.
936 */
937static inline void mmc_bus_put(struct mmc_host *host)
938{
939 unsigned long flags;
940
941 spin_lock_irqsave(&host->lock, flags);
942 host->bus_refs--;
943 if ((host->bus_refs == 0) && host->bus_ops)
944 __mmc_release_bus(host);
945 spin_unlock_irqrestore(&host->lock, flags);
946}
947
1da177e4 948/*
7ea239d9
PO
949 * Assign a mmc bus handler to a host. Only one bus handler may control a
950 * host at any given time.
1da177e4 951 */
7ea239d9 952void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 953{
7ea239d9 954 unsigned long flags;
e45a1bd2 955
7ea239d9
PO
956 BUG_ON(!host);
957 BUG_ON(!ops);
b855885e 958
d84075c8 959 WARN_ON(!host->claimed);
bce40a36 960
7ea239d9 961 spin_lock_irqsave(&host->lock, flags);
bce40a36 962
7ea239d9
PO
963 BUG_ON(host->bus_ops);
964 BUG_ON(host->bus_refs);
b57c43ad 965
7ea239d9
PO
966 host->bus_ops = ops;
967 host->bus_refs = 1;
968 host->bus_dead = 0;
b57c43ad 969
7ea239d9 970 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
971}
972
7ea239d9
PO
973/*
974 * Remove the current bus handler from a host. Assumes that there are
975 * no interesting cards left, so the bus is powered down.
976 */
977void mmc_detach_bus(struct mmc_host *host)
7ccd266e 978{
7ea239d9 979 unsigned long flags;
7ccd266e 980
7ea239d9 981 BUG_ON(!host);
7ccd266e 982
d84075c8
PO
983 WARN_ON(!host->claimed);
984 WARN_ON(!host->bus_ops);
cd9277c0 985
7ea239d9 986 spin_lock_irqsave(&host->lock, flags);
7ccd266e 987
7ea239d9 988 host->bus_dead = 1;
7ccd266e 989
7ea239d9 990 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 991
7ea239d9 992 mmc_power_off(host);
1da177e4 993
7ea239d9 994 mmc_bus_put(host);
1da177e4
LT
995}
996
1da177e4
LT
997/**
998 * mmc_detect_change - process change of state on a MMC socket
999 * @host: host which changed state.
8dc00335 1000 * @delay: optional delay to wait before detection (jiffies)
1da177e4 1001 *
67a61c48
PO
1002 * MMC drivers should call this when they detect a card has been
1003 * inserted or removed. The MMC layer will confirm that any
1004 * present card is still functional, and initialize any newly
1005 * inserted.
1da177e4 1006 */
8dc00335 1007void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1008{
3b91e550 1009#ifdef CONFIG_MMC_DEBUG
1efd48b3 1010 unsigned long flags;
01f41ec7 1011 spin_lock_irqsave(&host->lock, flags);
d84075c8 1012 WARN_ON(host->removed);
01f41ec7 1013 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
1014#endif
1015
c4028958 1016 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
1017}
1018
1019EXPORT_SYMBOL(mmc_detect_change);
1020
1021
b93931a6 1022void mmc_rescan(struct work_struct *work)
1da177e4 1023{
c4028958
DH
1024 struct mmc_host *host =
1025 container_of(work, struct mmc_host, detect.work);
7ea239d9
PO
1026 u32 ocr;
1027 int err;
1da177e4 1028
7ea239d9 1029 mmc_bus_get(host);
b855885e 1030
94d89efb
JS
1031 /* if there is a card registered, check whether it is still present */
1032 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1033 host->bus_ops->detect(host);
1034
1035 mmc_bus_put(host);
1036
1037
1038 mmc_bus_get(host);
1039
1040 /* if there still is a card present, stop here */
1041 if (host->bus_ops != NULL) {
7ea239d9 1042 mmc_bus_put(host);
94d89efb
JS
1043 goto out;
1044 }
1da177e4 1045
94d89efb 1046 /* detect a newly inserted card */
28f52482 1047
94d89efb
JS
1048 /*
1049 * Only we can add a new handler, so it's safe to
1050 * release the lock here.
1051 */
1052 mmc_bus_put(host);
1da177e4 1053
94d89efb
JS
1054 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1055 goto out;
1da177e4 1056
94d89efb 1057 mmc_claim_host(host);
1da177e4 1058
94d89efb
JS
1059 mmc_power_up(host);
1060 mmc_go_idle(host);
5c4e6f13 1061
94d89efb 1062 mmc_send_if_cond(host, host->ocr_avail);
5c4e6f13 1063
94d89efb
JS
1064 /*
1065 * First we search for SDIO...
1066 */
1067 err = mmc_send_io_op_cond(host, 0, &ocr);
1068 if (!err) {
1069 if (mmc_attach_sdio(host, ocr))
1070 mmc_power_off(host);
1071 goto out;
1072 }
5c4e6f13 1073
94d89efb
JS
1074 /*
1075 * ...then normal SD...
1076 */
1077 err = mmc_send_app_op_cond(host, 0, &ocr);
1078 if (!err) {
1079 if (mmc_attach_sd(host, ocr))
1080 mmc_power_off(host);
1081 goto out;
1082 }
7ea239d9 1083
94d89efb
JS
1084 /*
1085 * ...and finally MMC.
1086 */
1087 err = mmc_send_op_cond(host, 0, &ocr);
1088 if (!err) {
1089 if (mmc_attach_mmc(host, ocr))
1090 mmc_power_off(host);
1091 goto out;
7ea239d9 1092 }
94d89efb
JS
1093
1094 mmc_release_host(host);
1095 mmc_power_off(host);
1096
28f52482
AV
1097out:
1098 if (host->caps & MMC_CAP_NEEDS_POLL)
1099 mmc_schedule_delayed_work(&host->detect, HZ);
1da177e4
LT
1100}
1101
b93931a6 1102void mmc_start_host(struct mmc_host *host)
1da177e4 1103{
b93931a6
PO
1104 mmc_power_off(host);
1105 mmc_detect_change(host, 0);
1da177e4
LT
1106}
1107
b93931a6 1108void mmc_stop_host(struct mmc_host *host)
1da177e4 1109{
3b91e550 1110#ifdef CONFIG_MMC_DEBUG
1efd48b3
PO
1111 unsigned long flags;
1112 spin_lock_irqsave(&host->lock, flags);
3b91e550 1113 host->removed = 1;
1efd48b3 1114 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
1115#endif
1116
8ea926b2
AH
1117 if (host->caps & MMC_CAP_DISABLE)
1118 cancel_delayed_work(&host->disable);
7de427d0 1119 cancel_delayed_work(&host->detect);
3b91e550
PO
1120 mmc_flush_scheduled_work();
1121
7ea239d9
PO
1122 mmc_bus_get(host);
1123 if (host->bus_ops && !host->bus_dead) {
1124 if (host->bus_ops->remove)
1125 host->bus_ops->remove(host);
1126
1127 mmc_claim_host(host);
1128 mmc_detach_bus(host);
1129 mmc_release_host(host);
1da177e4 1130 }
7ea239d9
PO
1131 mmc_bus_put(host);
1132
1133 BUG_ON(host->card);
1da177e4
LT
1134
1135 mmc_power_off(host);
1136}
1137
1da177e4
LT
1138#ifdef CONFIG_PM
1139
1140/**
1141 * mmc_suspend_host - suspend a host
1142 * @host: mmc host
1143 * @state: suspend mode (PM_SUSPEND_xxx)
1144 */
e5378ca8 1145int mmc_suspend_host(struct mmc_host *host, pm_message_t state)
1da177e4 1146{
8ea926b2
AH
1147 if (host->caps & MMC_CAP_DISABLE)
1148 cancel_delayed_work(&host->disable);
7de427d0 1149 cancel_delayed_work(&host->detect);
b5af25be
PO
1150 mmc_flush_scheduled_work();
1151
7ea239d9
PO
1152 mmc_bus_get(host);
1153 if (host->bus_ops && !host->bus_dead) {
6abaa0c9
PO
1154 if (host->bus_ops->suspend)
1155 host->bus_ops->suspend(host);
1156 if (!host->bus_ops->resume) {
1157 if (host->bus_ops->remove)
1158 host->bus_ops->remove(host);
1159
1160 mmc_claim_host(host);
1161 mmc_detach_bus(host);
1162 mmc_release_host(host);
1163 }
b5af25be 1164 }
7ea239d9
PO
1165 mmc_bus_put(host);
1166
1da177e4 1167 mmc_power_off(host);
1da177e4
LT
1168
1169 return 0;
1170}
1171
1172EXPORT_SYMBOL(mmc_suspend_host);
1173
1174/**
1175 * mmc_resume_host - resume a previously suspended host
1176 * @host: mmc host
1177 */
1178int mmc_resume_host(struct mmc_host *host)
1179{
6abaa0c9
PO
1180 mmc_bus_get(host);
1181 if (host->bus_ops && !host->bus_dead) {
1182 mmc_power_up(host);
d3096f88 1183 mmc_select_voltage(host, host->ocr);
6abaa0c9
PO
1184 BUG_ON(!host->bus_ops->resume);
1185 host->bus_ops->resume(host);
1186 }
1187 mmc_bus_put(host);
1188
1189 /*
1190 * We add a slight delay here so that resume can progress
1191 * in parallel.
1192 */
1193 mmc_detect_change(host, 1);
1da177e4
LT
1194
1195 return 0;
1196}
1197
1198EXPORT_SYMBOL(mmc_resume_host);
1199
1200#endif
1201
ffce2e7e
PO
1202static int __init mmc_init(void)
1203{
1204 int ret;
1205
1206 workqueue = create_singlethread_workqueue("kmmcd");
1207 if (!workqueue)
1208 return -ENOMEM;
1209
1210 ret = mmc_register_bus();
e29a7d73
PO
1211 if (ret)
1212 goto destroy_workqueue;
1213
1214 ret = mmc_register_host_class();
1215 if (ret)
1216 goto unregister_bus;
1217
1218 ret = sdio_register_bus();
1219 if (ret)
1220 goto unregister_host_class;
1221
1222 return 0;
1223
1224unregister_host_class:
1225 mmc_unregister_host_class();
1226unregister_bus:
1227 mmc_unregister_bus();
1228destroy_workqueue:
1229 destroy_workqueue(workqueue);
1230
ffce2e7e
PO
1231 return ret;
1232}
1233
1234static void __exit mmc_exit(void)
1235{
e29a7d73 1236 sdio_unregister_bus();
ffce2e7e
PO
1237 mmc_unregister_host_class();
1238 mmc_unregister_bus();
1239 destroy_workqueue(workqueue);
1240}
1241
26074962 1242subsys_initcall(mmc_init);
ffce2e7e
PO
1243module_exit(mmc_exit);
1244
1da177e4 1245MODULE_LICENSE("GPL");