]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/mmc/core/core.c
mmc: Move regulator handling closer to core
[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
bd68e083
BH
50/*
51 * We normally treat cards as removed during suspend if they are not
52 * known to be on a non-removable bus, to avoid the risk of writing
53 * back data to a different card after resume. Allow this to be
54 * overridden if necessary.
55 */
56#ifdef CONFIG_MMC_UNSAFE_RESUME
57int mmc_assume_removable;
58#else
59int mmc_assume_removable = 1;
60#endif
71d7d3d1 61EXPORT_SYMBOL(mmc_assume_removable);
bd68e083
BH
62module_param_named(removable, mmc_assume_removable, bool, 0644);
63MODULE_PARM_DESC(
64 removable,
65 "MMC/SD cards are removable and may be removed during suspend");
66
ffce2e7e
PO
67/*
68 * Internal function. Schedule delayed work in the MMC work queue.
69 */
70static int mmc_schedule_delayed_work(struct delayed_work *work,
71 unsigned long delay)
72{
73 return queue_delayed_work(workqueue, work, delay);
74}
75
76/*
77 * Internal function. Flush all scheduled work from the MMC work queue.
78 */
79static void mmc_flush_scheduled_work(void)
80{
81 flush_workqueue(workqueue);
82}
83
1da177e4 84/**
fe10c6ab
RK
85 * mmc_request_done - finish processing an MMC request
86 * @host: MMC host which completed request
87 * @mrq: MMC request which request
1da177e4
LT
88 *
89 * MMC drivers should call this function when they have completed
fe10c6ab 90 * their processing of a request.
1da177e4
LT
91 */
92void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
93{
94 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
95 int err = cmd->error;
96
af517150
DB
97 if (err && cmd->retries && mmc_host_is_spi(host)) {
98 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
99 cmd->retries = 0;
100 }
101
1da177e4 102 if (err && cmd->retries) {
e4d21708
PO
103 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
104 mmc_hostname(host), cmd->opcode, err);
105
1da177e4
LT
106 cmd->retries--;
107 cmd->error = 0;
108 host->ops->request(host, mrq);
e4d21708 109 } else {
af8350c7
PO
110 led_trigger_event(host->led, LED_OFF);
111
e4d21708
PO
112 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
113 mmc_hostname(host), cmd->opcode, err,
114 cmd->resp[0], cmd->resp[1],
115 cmd->resp[2], cmd->resp[3]);
116
117 if (mrq->data) {
118 pr_debug("%s: %d bytes transferred: %d\n",
119 mmc_hostname(host),
120 mrq->data->bytes_xfered, mrq->data->error);
121 }
122
123 if (mrq->stop) {
124 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
125 mmc_hostname(host), mrq->stop->opcode,
126 mrq->stop->error,
127 mrq->stop->resp[0], mrq->stop->resp[1],
128 mrq->stop->resp[2], mrq->stop->resp[3]);
129 }
130
131 if (mrq->done)
132 mrq->done(mrq);
1da177e4
LT
133 }
134}
135
136EXPORT_SYMBOL(mmc_request_done);
137
39361851 138static void
1da177e4
LT
139mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
140{
976d9276
PO
141#ifdef CONFIG_MMC_DEBUG
142 unsigned int i, sz;
a84756c5 143 struct scatterlist *sg;
976d9276
PO
144#endif
145
920e70c5
RK
146 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
147 mmc_hostname(host), mrq->cmd->opcode,
148 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 149
e4d21708
PO
150 if (mrq->data) {
151 pr_debug("%s: blksz %d blocks %d flags %08x "
152 "tsac %d ms nsac %d\n",
153 mmc_hostname(host), mrq->data->blksz,
154 mrq->data->blocks, mrq->data->flags,
ce252edd 155 mrq->data->timeout_ns / 1000000,
e4d21708
PO
156 mrq->data->timeout_clks);
157 }
158
159 if (mrq->stop) {
160 pr_debug("%s: CMD%u arg %08x flags %08x\n",
161 mmc_hostname(host), mrq->stop->opcode,
162 mrq->stop->arg, mrq->stop->flags);
163 }
164
f22ee4ed 165 WARN_ON(!host->claimed);
1da177e4 166
af8350c7
PO
167 led_trigger_event(host->led, LED_FULL);
168
1da177e4
LT
169 mrq->cmd->error = 0;
170 mrq->cmd->mrq = mrq;
171 if (mrq->data) {
fe4a3c7a 172 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
173 BUG_ON(mrq->data->blocks > host->max_blk_count);
174 BUG_ON(mrq->data->blocks * mrq->data->blksz >
175 host->max_req_size);
fe4a3c7a 176
976d9276
PO
177#ifdef CONFIG_MMC_DEBUG
178 sz = 0;
a84756c5
PO
179 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
180 sz += sg->length;
976d9276
PO
181 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
182#endif
183
1da177e4
LT
184 mrq->cmd->data = mrq->data;
185 mrq->data->error = 0;
186 mrq->data->mrq = mrq;
187 if (mrq->stop) {
188 mrq->data->stop = mrq->stop;
189 mrq->stop->error = 0;
190 mrq->stop->mrq = mrq;
191 }
192 }
193 host->ops->request(host, mrq);
194}
195
1da177e4
LT
196static void mmc_wait_done(struct mmc_request *mrq)
197{
198 complete(mrq->done_data);
199}
200
67a61c48
PO
201/**
202 * mmc_wait_for_req - start a request and wait for completion
203 * @host: MMC host to start command
204 * @mrq: MMC request to start
205 *
206 * Start a new MMC custom command request for a host, and wait
207 * for the command to complete. Does not attempt to parse the
208 * response.
209 */
210void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 211{
0afffc72 212 DECLARE_COMPLETION_ONSTACK(complete);
1da177e4
LT
213
214 mrq->done_data = &complete;
215 mrq->done = mmc_wait_done;
216
217 mmc_start_request(host, mrq);
218
219 wait_for_completion(&complete);
1da177e4
LT
220}
221
222EXPORT_SYMBOL(mmc_wait_for_req);
223
224/**
225 * mmc_wait_for_cmd - start a command and wait for completion
226 * @host: MMC host to start command
227 * @cmd: MMC command to start
228 * @retries: maximum number of retries
229 *
230 * Start a new MMC command for a host, and wait for the command
231 * to complete. Return any error that occurred while the command
232 * was executing. Do not attempt to parse the response.
233 */
234int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
235{
236 struct mmc_request mrq;
237
d84075c8 238 WARN_ON(!host->claimed);
1da177e4
LT
239
240 memset(&mrq, 0, sizeof(struct mmc_request));
241
242 memset(cmd->resp, 0, sizeof(cmd->resp));
243 cmd->retries = retries;
244
245 mrq.cmd = cmd;
246 cmd->data = NULL;
247
248 mmc_wait_for_req(host, &mrq);
249
250 return cmd->error;
251}
252
253EXPORT_SYMBOL(mmc_wait_for_cmd);
254
d773d725
RK
255/**
256 * mmc_set_data_timeout - set the timeout for a data command
257 * @data: data phase for command
258 * @card: the MMC card associated with the data transfer
67a61c48
PO
259 *
260 * Computes the data timeout parameters according to the
261 * correct algorithm given the card type.
d773d725 262 */
b146d26a 263void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
264{
265 unsigned int mult;
266
e6f918bf
PO
267 /*
268 * SDIO cards only define an upper 1 s limit on access.
269 */
270 if (mmc_card_sdio(card)) {
271 data->timeout_ns = 1000000000;
272 data->timeout_clks = 0;
273 return;
274 }
275
d773d725
RK
276 /*
277 * SD cards use a 100 multiplier rather than 10
278 */
279 mult = mmc_card_sd(card) ? 100 : 10;
280
281 /*
282 * Scale up the multiplier (and therefore the timeout) by
283 * the r2w factor for writes.
284 */
b146d26a 285 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
286 mult <<= card->csd.r2w_factor;
287
288 data->timeout_ns = card->csd.tacc_ns * mult;
289 data->timeout_clks = card->csd.tacc_clks * mult;
290
291 /*
292 * SD cards also have an upper limit on the timeout.
293 */
294 if (mmc_card_sd(card)) {
295 unsigned int timeout_us, limit_us;
296
297 timeout_us = data->timeout_ns / 1000;
298 timeout_us += data->timeout_clks * 1000 /
299 (card->host->ios.clock / 1000);
300
b146d26a 301 if (data->flags & MMC_DATA_WRITE)
493890e7
PO
302 /*
303 * The limit is really 250 ms, but that is
304 * insufficient for some crappy cards.
305 */
306 limit_us = 300000;
d773d725
RK
307 else
308 limit_us = 100000;
309
fba68bd2
PL
310 /*
311 * SDHC cards always use these fixed values.
312 */
313 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
314 data->timeout_ns = limit_us * 1000;
315 data->timeout_clks = 0;
316 }
317 }
c0c88871
WM
318 /*
319 * Some cards need very high timeouts if driven in SPI mode.
320 * The worst observed timeout was 900ms after writing a
321 * continuous stream of data until the internal logic
322 * overflowed.
323 */
324 if (mmc_host_is_spi(card->host)) {
325 if (data->flags & MMC_DATA_WRITE) {
326 if (data->timeout_ns < 1000000000)
327 data->timeout_ns = 1000000000; /* 1s */
328 } else {
329 if (data->timeout_ns < 100000000)
330 data->timeout_ns = 100000000; /* 100ms */
331 }
332 }
d773d725
RK
333}
334EXPORT_SYMBOL(mmc_set_data_timeout);
335
ad3868b2
PO
336/**
337 * mmc_align_data_size - pads a transfer size to a more optimal value
338 * @card: the MMC card associated with the data transfer
339 * @sz: original transfer size
340 *
341 * Pads the original data size with a number of extra bytes in
342 * order to avoid controller bugs and/or performance hits
343 * (e.g. some controllers revert to PIO for certain sizes).
344 *
345 * Returns the improved size, which might be unmodified.
346 *
347 * Note that this function is only relevant when issuing a
348 * single scatter gather entry.
349 */
350unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
351{
352 /*
353 * FIXME: We don't have a system for the controller to tell
354 * the core about its problems yet, so for now we just 32-bit
355 * align the size.
356 */
357 sz = ((sz + 3) / 4) * 4;
358
359 return sz;
360}
361EXPORT_SYMBOL(mmc_align_data_size);
362
8ea926b2
AH
363/**
364 * mmc_host_enable - enable a host.
365 * @host: mmc host to enable
366 *
367 * Hosts that support power saving can use the 'enable' and 'disable'
368 * methods to exit and enter power saving states. For more information
369 * see comments for struct mmc_host_ops.
370 */
371int mmc_host_enable(struct mmc_host *host)
372{
373 if (!(host->caps & MMC_CAP_DISABLE))
374 return 0;
375
376 if (host->en_dis_recurs)
377 return 0;
378
379 if (host->nesting_cnt++)
380 return 0;
381
382 cancel_delayed_work_sync(&host->disable);
383
384 if (host->enabled)
385 return 0;
386
387 if (host->ops->enable) {
388 int err;
389
390 host->en_dis_recurs = 1;
391 err = host->ops->enable(host);
392 host->en_dis_recurs = 0;
393
394 if (err) {
395 pr_debug("%s: enable error %d\n",
396 mmc_hostname(host), err);
397 return err;
398 }
399 }
400 host->enabled = 1;
401 return 0;
402}
403EXPORT_SYMBOL(mmc_host_enable);
404
405static int mmc_host_do_disable(struct mmc_host *host, int lazy)
406{
407 if (host->ops->disable) {
408 int err;
409
410 host->en_dis_recurs = 1;
411 err = host->ops->disable(host, lazy);
412 host->en_dis_recurs = 0;
413
414 if (err < 0) {
415 pr_debug("%s: disable error %d\n",
416 mmc_hostname(host), err);
417 return err;
418 }
419 if (err > 0) {
420 unsigned long delay = msecs_to_jiffies(err);
421
422 mmc_schedule_delayed_work(&host->disable, delay);
423 }
424 }
425 host->enabled = 0;
426 return 0;
427}
428
429/**
430 * mmc_host_disable - disable a host.
431 * @host: mmc host to disable
432 *
433 * Hosts that support power saving can use the 'enable' and 'disable'
434 * methods to exit and enter power saving states. For more information
435 * see comments for struct mmc_host_ops.
436 */
437int mmc_host_disable(struct mmc_host *host)
438{
439 int err;
440
441 if (!(host->caps & MMC_CAP_DISABLE))
442 return 0;
443
444 if (host->en_dis_recurs)
445 return 0;
446
447 if (--host->nesting_cnt)
448 return 0;
449
450 if (!host->enabled)
451 return 0;
452
453 err = mmc_host_do_disable(host, 0);
454 return err;
455}
456EXPORT_SYMBOL(mmc_host_disable);
457
1da177e4 458/**
2342f332 459 * __mmc_claim_host - exclusively claim a host
1da177e4 460 * @host: mmc host to claim
2342f332 461 * @abort: whether or not the operation should be aborted
1da177e4 462 *
2342f332
NP
463 * Claim a host for a set of operations. If @abort is non null and
464 * dereference a non-zero value then this will return prematurely with
465 * that non-zero value without acquiring the lock. Returns zero
466 * with the lock held otherwise.
1da177e4 467 */
2342f332 468int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1da177e4
LT
469{
470 DECLARE_WAITQUEUE(wait, current);
471 unsigned long flags;
2342f332 472 int stop;
1da177e4 473
cf795bfb
PO
474 might_sleep();
475
1da177e4
LT
476 add_wait_queue(&host->wq, &wait);
477 spin_lock_irqsave(&host->lock, flags);
478 while (1) {
479 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332 480 stop = abort ? atomic_read(abort) : 0;
319a3f14 481 if (stop || !host->claimed || host->claimer == current)
1da177e4
LT
482 break;
483 spin_unlock_irqrestore(&host->lock, flags);
484 schedule();
485 spin_lock_irqsave(&host->lock, flags);
486 }
487 set_current_state(TASK_RUNNING);
319a3f14 488 if (!stop) {
2342f332 489 host->claimed = 1;
319a3f14
AH
490 host->claimer = current;
491 host->claim_cnt += 1;
492 } else
2342f332 493 wake_up(&host->wq);
1da177e4
LT
494 spin_unlock_irqrestore(&host->lock, flags);
495 remove_wait_queue(&host->wq, &wait);
8ea926b2
AH
496 if (!stop)
497 mmc_host_enable(host);
2342f332 498 return stop;
1da177e4
LT
499}
500
2342f332 501EXPORT_SYMBOL(__mmc_claim_host);
1da177e4 502
319a3f14
AH
503/**
504 * mmc_try_claim_host - try exclusively to claim a host
505 * @host: mmc host to claim
506 *
507 * Returns %1 if the host is claimed, %0 otherwise.
508 */
509int mmc_try_claim_host(struct mmc_host *host)
8ea926b2
AH
510{
511 int claimed_host = 0;
512 unsigned long flags;
513
514 spin_lock_irqsave(&host->lock, flags);
319a3f14 515 if (!host->claimed || host->claimer == current) {
8ea926b2 516 host->claimed = 1;
319a3f14
AH
517 host->claimer = current;
518 host->claim_cnt += 1;
8ea926b2
AH
519 claimed_host = 1;
520 }
521 spin_unlock_irqrestore(&host->lock, flags);
522 return claimed_host;
523}
319a3f14 524EXPORT_SYMBOL(mmc_try_claim_host);
8ea926b2
AH
525
526static void mmc_do_release_host(struct mmc_host *host)
527{
528 unsigned long flags;
529
530 spin_lock_irqsave(&host->lock, flags);
319a3f14
AH
531 if (--host->claim_cnt) {
532 /* Release for nested claim */
533 spin_unlock_irqrestore(&host->lock, flags);
534 } else {
535 host->claimed = 0;
536 host->claimer = NULL;
537 spin_unlock_irqrestore(&host->lock, flags);
538 wake_up(&host->wq);
539 }
8ea926b2
AH
540}
541
542void mmc_host_deeper_disable(struct work_struct *work)
543{
544 struct mmc_host *host =
545 container_of(work, struct mmc_host, disable.work);
546
547 /* If the host is claimed then we do not want to disable it anymore */
548 if (!mmc_try_claim_host(host))
549 return;
550 mmc_host_do_disable(host, 1);
551 mmc_do_release_host(host);
552}
553
554/**
555 * mmc_host_lazy_disable - lazily disable a host.
556 * @host: mmc host to disable
557 *
558 * Hosts that support power saving can use the 'enable' and 'disable'
559 * methods to exit and enter power saving states. For more information
560 * see comments for struct mmc_host_ops.
561 */
562int mmc_host_lazy_disable(struct mmc_host *host)
563{
564 if (!(host->caps & MMC_CAP_DISABLE))
565 return 0;
566
567 if (host->en_dis_recurs)
568 return 0;
569
570 if (--host->nesting_cnt)
571 return 0;
572
573 if (!host->enabled)
574 return 0;
575
576 if (host->disable_delay) {
577 mmc_schedule_delayed_work(&host->disable,
578 msecs_to_jiffies(host->disable_delay));
579 return 0;
580 } else
581 return mmc_host_do_disable(host, 1);
582}
583EXPORT_SYMBOL(mmc_host_lazy_disable);
584
1da177e4
LT
585/**
586 * mmc_release_host - release a host
587 * @host: mmc host to release
588 *
589 * Release a MMC host, allowing others to claim the host
590 * for their operations.
591 */
592void mmc_release_host(struct mmc_host *host)
593{
d84075c8 594 WARN_ON(!host->claimed);
1da177e4 595
8ea926b2 596 mmc_host_lazy_disable(host);
1da177e4 597
8ea926b2 598 mmc_do_release_host(host);
1da177e4
LT
599}
600
601EXPORT_SYMBOL(mmc_release_host);
602
7ea239d9
PO
603/*
604 * Internal function that does the actual ios call to the host driver,
605 * optionally printing some debug output.
606 */
920e70c5
RK
607static inline void mmc_set_ios(struct mmc_host *host)
608{
609 struct mmc_ios *ios = &host->ios;
610
cd9277c0
PO
611 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
612 "width %u timing %u\n",
920e70c5
RK
613 mmc_hostname(host), ios->clock, ios->bus_mode,
614 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 615 ios->bus_width, ios->timing);
fba68bd2 616
920e70c5
RK
617 host->ops->set_ios(host, ios);
618}
619
7ea239d9
PO
620/*
621 * Control chip select pin on a host.
622 */
da7fbe58 623void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 624{
da7fbe58
PO
625 host->ios.chip_select = mode;
626 mmc_set_ios(host);
1da177e4
LT
627}
628
7ea239d9
PO
629/*
630 * Sets the host clock to the highest possible frequency that
631 * is below "hz".
632 */
633void mmc_set_clock(struct mmc_host *host, unsigned int hz)
634{
635 WARN_ON(hz < host->f_min);
636
637 if (hz > host->f_max)
638 hz = host->f_max;
639
640 host->ios.clock = hz;
641 mmc_set_ios(host);
642}
643
644/*
645 * Change the bus mode (open drain/push-pull) of a host.
646 */
647void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
648{
649 host->ios.bus_mode = mode;
650 mmc_set_ios(host);
651}
652
653/*
654 * Change data bus width of a host.
655 */
656void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
657{
658 host->ios.bus_width = width;
659 mmc_set_ios(host);
660}
661
86e8286a
AV
662/**
663 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
664 * @vdd: voltage (mV)
665 * @low_bits: prefer low bits in boundary cases
666 *
667 * This function returns the OCR bit number according to the provided @vdd
668 * value. If conversion is not possible a negative errno value returned.
669 *
670 * Depending on the @low_bits flag the function prefers low or high OCR bits
671 * on boundary voltages. For example,
672 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
673 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
674 *
675 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
676 */
677static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
678{
679 const int max_bit = ilog2(MMC_VDD_35_36);
680 int bit;
681
682 if (vdd < 1650 || vdd > 3600)
683 return -EINVAL;
684
685 if (vdd >= 1650 && vdd <= 1950)
686 return ilog2(MMC_VDD_165_195);
687
688 if (low_bits)
689 vdd -= 1;
690
691 /* Base 2000 mV, step 100 mV, bit's base 8. */
692 bit = (vdd - 2000) / 100 + 8;
693 if (bit > max_bit)
694 return max_bit;
695 return bit;
696}
697
698/**
699 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
700 * @vdd_min: minimum voltage value (mV)
701 * @vdd_max: maximum voltage value (mV)
702 *
703 * This function returns the OCR mask bits according to the provided @vdd_min
704 * and @vdd_max values. If conversion is not possible the function returns 0.
705 *
706 * Notes wrt boundary cases:
707 * This function sets the OCR bits for all boundary voltages, for example
708 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
709 * MMC_VDD_34_35 mask.
710 */
711u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
712{
713 u32 mask = 0;
714
715 if (vdd_max < vdd_min)
716 return 0;
717
718 /* Prefer high bits for the boundary vdd_max values. */
719 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
720 if (vdd_max < 0)
721 return 0;
722
723 /* Prefer low bits for the boundary vdd_min values. */
724 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
725 if (vdd_min < 0)
726 return 0;
727
728 /* Fill the mask, from max bit to min bit. */
729 while (vdd_max >= vdd_min)
730 mask |= 1 << vdd_max--;
731
732 return mask;
733}
734EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
735
5c13941a
DB
736#ifdef CONFIG_REGULATOR
737
738/**
739 * mmc_regulator_get_ocrmask - return mask of supported voltages
740 * @supply: regulator to use
741 *
742 * This returns either a negative errno, or a mask of voltages that
743 * can be provided to MMC/SD/SDIO devices using the specified voltage
744 * regulator. This would normally be called before registering the
745 * MMC host adapter.
746 */
747int mmc_regulator_get_ocrmask(struct regulator *supply)
748{
749 int result = 0;
750 int count;
751 int i;
752
753 count = regulator_count_voltages(supply);
754 if (count < 0)
755 return count;
756
757 for (i = 0; i < count; i++) {
758 int vdd_uV;
759 int vdd_mV;
760
761 vdd_uV = regulator_list_voltage(supply, i);
762 if (vdd_uV <= 0)
763 continue;
764
765 vdd_mV = vdd_uV / 1000;
766 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
767 }
768
769 return result;
770}
771EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
772
773/**
774 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
99fc5131 775 * @mmc: the host to regulate
5c13941a 776 * @supply: regulator to use
99fc5131 777 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
5c13941a
DB
778 *
779 * Returns zero on success, else negative errno.
780 *
781 * MMC host drivers may use this to enable or disable a regulator using
782 * a particular supply voltage. This would normally be called from the
783 * set_ios() method.
784 */
99fc5131
LW
785int mmc_regulator_set_ocr(struct mmc_host *mmc,
786 struct regulator *supply,
787 unsigned short vdd_bit)
5c13941a
DB
788{
789 int result = 0;
790 int min_uV, max_uV;
5c13941a
DB
791
792 if (vdd_bit) {
793 int tmp;
794 int voltage;
795
796 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
797 * bits this regulator doesn't quite support ... don't
798 * be too picky, most cards and regulators are OK with
799 * a 0.1V range goof (it's a small error percentage).
800 */
801 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
802 if (tmp == 0) {
803 min_uV = 1650 * 1000;
804 max_uV = 1950 * 1000;
805 } else {
806 min_uV = 1900 * 1000 + tmp * 100 * 1000;
807 max_uV = min_uV + 100 * 1000;
808 }
809
810 /* avoid needless changes to this voltage; the regulator
811 * might not allow this operation
812 */
813 voltage = regulator_get_voltage(supply);
814 if (voltage < 0)
815 result = voltage;
816 else if (voltage < min_uV || voltage > max_uV)
817 result = regulator_set_voltage(supply, min_uV, max_uV);
818 else
819 result = 0;
820
99fc5131 821 if (result == 0 && !mmc->regulator_enabled) {
5c13941a 822 result = regulator_enable(supply);
99fc5131
LW
823 if (!result)
824 mmc->regulator_enabled = true;
825 }
826 } else if (mmc->regulator_enabled) {
5c13941a 827 result = regulator_disable(supply);
99fc5131
LW
828 if (result == 0)
829 mmc->regulator_enabled = false;
5c13941a
DB
830 }
831
99fc5131
LW
832 if (result)
833 dev_err(mmc_dev(mmc),
834 "could not set regulator OCR (%d)\n", result);
5c13941a
DB
835 return result;
836}
837EXPORT_SYMBOL(mmc_regulator_set_ocr);
838
99fc5131 839#endif /* CONFIG_REGULATOR */
5c13941a 840
1da177e4
LT
841/*
842 * Mask off any voltages we don't support and select
843 * the lowest voltage
844 */
7ea239d9 845u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
846{
847 int bit;
848
849 ocr &= host->ocr_avail;
850
851 bit = ffs(ocr);
852 if (bit) {
853 bit -= 1;
854
63ef731a 855 ocr &= 3 << bit;
1da177e4
LT
856
857 host->ios.vdd = bit;
920e70c5 858 mmc_set_ios(host);
1da177e4 859 } else {
f6e10b86
DB
860 pr_warning("%s: host doesn't support card's voltages\n",
861 mmc_hostname(host));
1da177e4
LT
862 ocr = 0;
863 }
864
865 return ocr;
866}
867
b57c43ad 868/*
7ea239d9 869 * Select timing parameters for host.
b57c43ad 870 */
7ea239d9 871void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 872{
7ea239d9
PO
873 host->ios.timing = timing;
874 mmc_set_ios(host);
b57c43ad
PO
875}
876
1da177e4 877/*
45f8245b
RK
878 * Apply power to the MMC stack. This is a two-stage process.
879 * First, we enable power to the card without the clock running.
880 * We then wait a bit for the power to stabilise. Finally,
881 * enable the bus drivers and clock to the card.
882 *
883 * We must _NOT_ enable the clock prior to power stablising.
884 *
885 * If a host does all the power sequencing itself, ignore the
886 * initial MMC_POWER_UP stage.
1da177e4
LT
887 */
888static void mmc_power_up(struct mmc_host *host)
889{
500f3564
BR
890 int bit;
891
892 /* If ocr is set, we use it */
893 if (host->ocr)
894 bit = ffs(host->ocr) - 1;
895 else
896 bit = fls(host->ocr_avail) - 1;
1da177e4
LT
897
898 host->ios.vdd = bit;
af517150
DB
899 if (mmc_host_is_spi(host)) {
900 host->ios.chip_select = MMC_CS_HIGH;
901 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
902 } else {
903 host->ios.chip_select = MMC_CS_DONTCARE;
904 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
905 }
1da177e4 906 host->ios.power_mode = MMC_POWER_UP;
f218278a 907 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 908 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 909 mmc_set_ios(host);
1da177e4 910
f9996aee
PO
911 /*
912 * This delay should be sufficient to allow the power supply
913 * to reach the minimum voltage.
914 */
79bccc5a 915 mmc_delay(10);
1da177e4 916
88ae8b86 917 host->ios.clock = host->f_init;
8dfd0374 918
1da177e4 919 host->ios.power_mode = MMC_POWER_ON;
920e70c5 920 mmc_set_ios(host);
1da177e4 921
f9996aee
PO
922 /*
923 * This delay must be at least 74 clock sizes, or 1 ms, or the
924 * time required to reach a stable voltage.
925 */
79bccc5a 926 mmc_delay(10);
1da177e4
LT
927}
928
929static void mmc_power_off(struct mmc_host *host)
930{
931 host->ios.clock = 0;
932 host->ios.vdd = 0;
af517150
DB
933 if (!mmc_host_is_spi(host)) {
934 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
935 host->ios.chip_select = MMC_CS_DONTCARE;
936 }
1da177e4 937 host->ios.power_mode = MMC_POWER_OFF;
f218278a 938 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 939 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 940 mmc_set_ios(host);
1da177e4
LT
941}
942
39361851
AB
943/*
944 * Cleanup when the last reference to the bus operator is dropped.
945 */
261172fd 946static void __mmc_release_bus(struct mmc_host *host)
39361851
AB
947{
948 BUG_ON(!host);
949 BUG_ON(host->bus_refs);
950 BUG_ON(!host->bus_dead);
951
952 host->bus_ops = NULL;
953}
954
955/*
956 * Increase reference count of bus operator
957 */
958static inline void mmc_bus_get(struct mmc_host *host)
959{
960 unsigned long flags;
961
962 spin_lock_irqsave(&host->lock, flags);
963 host->bus_refs++;
964 spin_unlock_irqrestore(&host->lock, flags);
965}
966
967/*
968 * Decrease reference count of bus operator and free it if
969 * it is the last reference.
970 */
971static inline void mmc_bus_put(struct mmc_host *host)
972{
973 unsigned long flags;
974
975 spin_lock_irqsave(&host->lock, flags);
976 host->bus_refs--;
977 if ((host->bus_refs == 0) && host->bus_ops)
978 __mmc_release_bus(host);
979 spin_unlock_irqrestore(&host->lock, flags);
980}
981
1da177e4 982/*
7ea239d9
PO
983 * Assign a mmc bus handler to a host. Only one bus handler may control a
984 * host at any given time.
1da177e4 985 */
7ea239d9 986void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 987{
7ea239d9 988 unsigned long flags;
e45a1bd2 989
7ea239d9
PO
990 BUG_ON(!host);
991 BUG_ON(!ops);
b855885e 992
d84075c8 993 WARN_ON(!host->claimed);
bce40a36 994
7ea239d9 995 spin_lock_irqsave(&host->lock, flags);
bce40a36 996
7ea239d9
PO
997 BUG_ON(host->bus_ops);
998 BUG_ON(host->bus_refs);
b57c43ad 999
7ea239d9
PO
1000 host->bus_ops = ops;
1001 host->bus_refs = 1;
1002 host->bus_dead = 0;
b57c43ad 1003
7ea239d9 1004 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
1005}
1006
7ea239d9
PO
1007/*
1008 * Remove the current bus handler from a host. Assumes that there are
1009 * no interesting cards left, so the bus is powered down.
1010 */
1011void mmc_detach_bus(struct mmc_host *host)
7ccd266e 1012{
7ea239d9 1013 unsigned long flags;
7ccd266e 1014
7ea239d9 1015 BUG_ON(!host);
7ccd266e 1016
d84075c8
PO
1017 WARN_ON(!host->claimed);
1018 WARN_ON(!host->bus_ops);
cd9277c0 1019
7ea239d9 1020 spin_lock_irqsave(&host->lock, flags);
7ccd266e 1021
7ea239d9 1022 host->bus_dead = 1;
7ccd266e 1023
7ea239d9 1024 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 1025
7ea239d9 1026 mmc_power_off(host);
1da177e4 1027
7ea239d9 1028 mmc_bus_put(host);
1da177e4
LT
1029}
1030
1da177e4
LT
1031/**
1032 * mmc_detect_change - process change of state on a MMC socket
1033 * @host: host which changed state.
8dc00335 1034 * @delay: optional delay to wait before detection (jiffies)
1da177e4 1035 *
67a61c48
PO
1036 * MMC drivers should call this when they detect a card has been
1037 * inserted or removed. The MMC layer will confirm that any
1038 * present card is still functional, and initialize any newly
1039 * inserted.
1da177e4 1040 */
8dc00335 1041void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1042{
3b91e550 1043#ifdef CONFIG_MMC_DEBUG
1efd48b3 1044 unsigned long flags;
01f41ec7 1045 spin_lock_irqsave(&host->lock, flags);
d84075c8 1046 WARN_ON(host->removed);
01f41ec7 1047 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
1048#endif
1049
c4028958 1050 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
1051}
1052
1053EXPORT_SYMBOL(mmc_detect_change);
1054
dfe86cba
AH
1055void mmc_init_erase(struct mmc_card *card)
1056{
1057 unsigned int sz;
1058
1059 if (is_power_of_2(card->erase_size))
1060 card->erase_shift = ffs(card->erase_size) - 1;
1061 else
1062 card->erase_shift = 0;
1063
1064 /*
1065 * It is possible to erase an arbitrarily large area of an SD or MMC
1066 * card. That is not desirable because it can take a long time
1067 * (minutes) potentially delaying more important I/O, and also the
1068 * timeout calculations become increasingly hugely over-estimated.
1069 * Consequently, 'pref_erase' is defined as a guide to limit erases
1070 * to that size and alignment.
1071 *
1072 * For SD cards that define Allocation Unit size, limit erases to one
1073 * Allocation Unit at a time. For MMC cards that define High Capacity
1074 * Erase Size, whether it is switched on or not, limit to that size.
1075 * Otherwise just have a stab at a good value. For modern cards it
1076 * will end up being 4MiB. Note that if the value is too small, it
1077 * can end up taking longer to erase.
1078 */
1079 if (mmc_card_sd(card) && card->ssr.au) {
1080 card->pref_erase = card->ssr.au;
1081 card->erase_shift = ffs(card->ssr.au) - 1;
1082 } else if (card->ext_csd.hc_erase_size) {
1083 card->pref_erase = card->ext_csd.hc_erase_size;
1084 } else {
1085 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1086 if (sz < 128)
1087 card->pref_erase = 512 * 1024 / 512;
1088 else if (sz < 512)
1089 card->pref_erase = 1024 * 1024 / 512;
1090 else if (sz < 1024)
1091 card->pref_erase = 2 * 1024 * 1024 / 512;
1092 else
1093 card->pref_erase = 4 * 1024 * 1024 / 512;
1094 if (card->pref_erase < card->erase_size)
1095 card->pref_erase = card->erase_size;
1096 else {
1097 sz = card->pref_erase % card->erase_size;
1098 if (sz)
1099 card->pref_erase += card->erase_size - sz;
1100 }
1101 }
1102}
1103
1104static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1105 struct mmc_command *cmd,
1106 unsigned int arg, unsigned int qty)
1107{
1108 unsigned int erase_timeout;
1109
1110 if (card->ext_csd.erase_group_def & 1) {
1111 /* High Capacity Erase Group Size uses HC timeouts */
1112 if (arg == MMC_TRIM_ARG)
1113 erase_timeout = card->ext_csd.trim_timeout;
1114 else
1115 erase_timeout = card->ext_csd.hc_erase_timeout;
1116 } else {
1117 /* CSD Erase Group Size uses write timeout */
1118 unsigned int mult = (10 << card->csd.r2w_factor);
1119 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1120 unsigned int timeout_us;
1121
1122 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1123 if (card->csd.tacc_ns < 1000000)
1124 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1125 else
1126 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1127
1128 /*
1129 * ios.clock is only a target. The real clock rate might be
1130 * less but not that much less, so fudge it by multiplying by 2.
1131 */
1132 timeout_clks <<= 1;
1133 timeout_us += (timeout_clks * 1000) /
1134 (card->host->ios.clock / 1000);
1135
1136 erase_timeout = timeout_us / 1000;
1137
1138 /*
1139 * Theoretically, the calculation could underflow so round up
1140 * to 1ms in that case.
1141 */
1142 if (!erase_timeout)
1143 erase_timeout = 1;
1144 }
1145
1146 /* Multiplier for secure operations */
1147 if (arg & MMC_SECURE_ARGS) {
1148 if (arg == MMC_SECURE_ERASE_ARG)
1149 erase_timeout *= card->ext_csd.sec_erase_mult;
1150 else
1151 erase_timeout *= card->ext_csd.sec_trim_mult;
1152 }
1153
1154 erase_timeout *= qty;
1155
1156 /*
1157 * Ensure at least a 1 second timeout for SPI as per
1158 * 'mmc_set_data_timeout()'
1159 */
1160 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1161 erase_timeout = 1000;
1162
1163 cmd->erase_timeout = erase_timeout;
1164}
1165
1166static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1167 struct mmc_command *cmd, unsigned int arg,
1168 unsigned int qty)
1169{
1170 if (card->ssr.erase_timeout) {
1171 /* Erase timeout specified in SD Status Register (SSR) */
1172 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1173 card->ssr.erase_offset;
1174 } else {
1175 /*
1176 * Erase timeout not specified in SD Status Register (SSR) so
1177 * use 250ms per write block.
1178 */
1179 cmd->erase_timeout = 250 * qty;
1180 }
1181
1182 /* Must not be less than 1 second */
1183 if (cmd->erase_timeout < 1000)
1184 cmd->erase_timeout = 1000;
1185}
1186
1187static void mmc_set_erase_timeout(struct mmc_card *card,
1188 struct mmc_command *cmd, unsigned int arg,
1189 unsigned int qty)
1190{
1191 if (mmc_card_sd(card))
1192 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1193 else
1194 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1195}
1196
1197static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1198 unsigned int to, unsigned int arg)
1199{
1200 struct mmc_command cmd;
1201 unsigned int qty = 0;
1202 int err;
1203
1204 /*
1205 * qty is used to calculate the erase timeout which depends on how many
1206 * erase groups (or allocation units in SD terminology) are affected.
1207 * We count erasing part of an erase group as one erase group.
1208 * For SD, the allocation units are always a power of 2. For MMC, the
1209 * erase group size is almost certainly also power of 2, but it does not
1210 * seem to insist on that in the JEDEC standard, so we fall back to
1211 * division in that case. SD may not specify an allocation unit size,
1212 * in which case the timeout is based on the number of write blocks.
1213 *
1214 * Note that the timeout for secure trim 2 will only be correct if the
1215 * number of erase groups specified is the same as the total of all
1216 * preceding secure trim 1 commands. Since the power may have been
1217 * lost since the secure trim 1 commands occurred, it is generally
1218 * impossible to calculate the secure trim 2 timeout correctly.
1219 */
1220 if (card->erase_shift)
1221 qty += ((to >> card->erase_shift) -
1222 (from >> card->erase_shift)) + 1;
1223 else if (mmc_card_sd(card))
1224 qty += to - from + 1;
1225 else
1226 qty += ((to / card->erase_size) -
1227 (from / card->erase_size)) + 1;
1228
1229 if (!mmc_card_blockaddr(card)) {
1230 from <<= 9;
1231 to <<= 9;
1232 }
1233
1234 memset(&cmd, 0, sizeof(struct mmc_command));
1235 if (mmc_card_sd(card))
1236 cmd.opcode = SD_ERASE_WR_BLK_START;
1237 else
1238 cmd.opcode = MMC_ERASE_GROUP_START;
1239 cmd.arg = from;
1240 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1241 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1242 if (err) {
1243 printk(KERN_ERR "mmc_erase: group start error %d, "
1244 "status %#x\n", err, cmd.resp[0]);
1245 err = -EINVAL;
1246 goto out;
1247 }
1248
1249 memset(&cmd, 0, sizeof(struct mmc_command));
1250 if (mmc_card_sd(card))
1251 cmd.opcode = SD_ERASE_WR_BLK_END;
1252 else
1253 cmd.opcode = MMC_ERASE_GROUP_END;
1254 cmd.arg = to;
1255 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1256 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1257 if (err) {
1258 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1259 err, cmd.resp[0]);
1260 err = -EINVAL;
1261 goto out;
1262 }
1263
1264 memset(&cmd, 0, sizeof(struct mmc_command));
1265 cmd.opcode = MMC_ERASE;
1266 cmd.arg = arg;
1267 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1268 mmc_set_erase_timeout(card, &cmd, arg, qty);
1269 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1270 if (err) {
1271 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1272 err, cmd.resp[0]);
1273 err = -EIO;
1274 goto out;
1275 }
1276
1277 if (mmc_host_is_spi(card->host))
1278 goto out;
1279
1280 do {
1281 memset(&cmd, 0, sizeof(struct mmc_command));
1282 cmd.opcode = MMC_SEND_STATUS;
1283 cmd.arg = card->rca << 16;
1284 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1285 /* Do not retry else we can't see errors */
1286 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1287 if (err || (cmd.resp[0] & 0xFDF92000)) {
1288 printk(KERN_ERR "error %d requesting status %#x\n",
1289 err, cmd.resp[0]);
1290 err = -EIO;
1291 goto out;
1292 }
1293 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1294 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1295out:
1296 return err;
1297}
1298
1299/**
1300 * mmc_erase - erase sectors.
1301 * @card: card to erase
1302 * @from: first sector to erase
1303 * @nr: number of sectors to erase
1304 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1305 *
1306 * Caller must claim host before calling this function.
1307 */
1308int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1309 unsigned int arg)
1310{
1311 unsigned int rem, to = from + nr;
1312
1313 if (!(card->host->caps & MMC_CAP_ERASE) ||
1314 !(card->csd.cmdclass & CCC_ERASE))
1315 return -EOPNOTSUPP;
1316
1317 if (!card->erase_size)
1318 return -EOPNOTSUPP;
1319
1320 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1321 return -EOPNOTSUPP;
1322
1323 if ((arg & MMC_SECURE_ARGS) &&
1324 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1325 return -EOPNOTSUPP;
1326
1327 if ((arg & MMC_TRIM_ARGS) &&
1328 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1329 return -EOPNOTSUPP;
1330
1331 if (arg == MMC_SECURE_ERASE_ARG) {
1332 if (from % card->erase_size || nr % card->erase_size)
1333 return -EINVAL;
1334 }
1335
1336 if (arg == MMC_ERASE_ARG) {
1337 rem = from % card->erase_size;
1338 if (rem) {
1339 rem = card->erase_size - rem;
1340 from += rem;
1341 if (nr > rem)
1342 nr -= rem;
1343 else
1344 return 0;
1345 }
1346 rem = nr % card->erase_size;
1347 if (rem)
1348 nr -= rem;
1349 }
1350
1351 if (nr == 0)
1352 return 0;
1353
1354 to = from + nr;
1355
1356 if (to <= from)
1357 return -EINVAL;
1358
1359 /* 'from' and 'to' are inclusive */
1360 to -= 1;
1361
1362 return mmc_do_erase(card, from, to, arg);
1363}
1364EXPORT_SYMBOL(mmc_erase);
1365
1366int mmc_can_erase(struct mmc_card *card)
1367{
1368 if ((card->host->caps & MMC_CAP_ERASE) &&
1369 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1370 return 1;
1371 return 0;
1372}
1373EXPORT_SYMBOL(mmc_can_erase);
1374
1375int mmc_can_trim(struct mmc_card *card)
1376{
1377 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1378 return 1;
1379 return 0;
1380}
1381EXPORT_SYMBOL(mmc_can_trim);
1382
1383int mmc_can_secure_erase_trim(struct mmc_card *card)
1384{
1385 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1386 return 1;
1387 return 0;
1388}
1389EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1390
1391int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1392 unsigned int nr)
1393{
1394 if (!card->erase_size)
1395 return 0;
1396 if (from % card->erase_size || nr % card->erase_size)
1397 return 0;
1398 return 1;
1399}
1400EXPORT_SYMBOL(mmc_erase_group_aligned);
1da177e4 1401
b93931a6 1402void mmc_rescan(struct work_struct *work)
1da177e4 1403{
c4028958
DH
1404 struct mmc_host *host =
1405 container_of(work, struct mmc_host, detect.work);
7ea239d9
PO
1406 u32 ocr;
1407 int err;
4c2ef25f 1408 unsigned long flags;
88ae8b86
HT
1409 int i;
1410 const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
4c2ef25f
ML
1411
1412 spin_lock_irqsave(&host->lock, flags);
1413
1414 if (host->rescan_disable) {
1415 spin_unlock_irqrestore(&host->lock, flags);
1416 return;
1417 }
1418
1419 spin_unlock_irqrestore(&host->lock, flags);
1420
1da177e4 1421
7ea239d9 1422 mmc_bus_get(host);
b855885e 1423
94d89efb
JS
1424 /* if there is a card registered, check whether it is still present */
1425 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1426 host->bus_ops->detect(host);
1427
1428 mmc_bus_put(host);
1429
1430
1431 mmc_bus_get(host);
1432
1433 /* if there still is a card present, stop here */
1434 if (host->bus_ops != NULL) {
7ea239d9 1435 mmc_bus_put(host);
94d89efb
JS
1436 goto out;
1437 }
1da177e4 1438
94d89efb 1439 /* detect a newly inserted card */
28f52482 1440
94d89efb
JS
1441 /*
1442 * Only we can add a new handler, so it's safe to
1443 * release the lock here.
1444 */
1445 mmc_bus_put(host);
1da177e4 1446
94d89efb
JS
1447 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1448 goto out;
1da177e4 1449
88ae8b86
HT
1450 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1451 mmc_claim_host(host);
1da177e4 1452
88ae8b86
HT
1453 if (freqs[i] >= host->f_min)
1454 host->f_init = freqs[i];
1455 else if (!i || freqs[i-1] > host->f_min)
1456 host->f_init = host->f_min;
1457 else {
1458 mmc_release_host(host);
1459 goto out;
1460 }
4d0b8611 1461#ifdef CONFIG_MMC_DEBUG
88ae8b86
HT
1462 pr_info("%s: %s: trying to init card at %u Hz\n",
1463 mmc_hostname(host), __func__, host->f_init);
4d0b8611 1464#endif
88ae8b86
HT
1465 mmc_power_up(host);
1466 sdio_reset(host);
1467 mmc_go_idle(host);
5c4e6f13 1468
88ae8b86 1469 mmc_send_if_cond(host, host->ocr_avail);
7310ece8 1470
88ae8b86
HT
1471 /*
1472 * First we search for SDIO...
1473 */
1474 err = mmc_send_io_op_cond(host, 0, &ocr);
1475 if (!err) {
1476 if (mmc_attach_sdio(host, ocr)) {
1477 mmc_claim_host(host);
1478 /*
1479 * Try SDMEM (but not MMC) even if SDIO
1480 * is broken.
1481 */
1482 if (mmc_send_app_op_cond(host, 0, &ocr))
1483 goto out_fail;
1484
1485 if (mmc_attach_sd(host, ocr))
1486 mmc_power_off(host);
1487 }
1488 goto out;
1489 }
1490
1491 /*
1492 * ...then normal SD...
1493 */
1494 err = mmc_send_app_op_cond(host, 0, &ocr);
1495 if (!err) {
7310ece8
MM
1496 if (mmc_attach_sd(host, ocr))
1497 mmc_power_off(host);
88ae8b86 1498 goto out;
7310ece8 1499 }
5c4e6f13 1500
88ae8b86
HT
1501 /*
1502 * ...and finally MMC.
1503 */
1504 err = mmc_send_op_cond(host, 0, &ocr);
1505 if (!err) {
1506 if (mmc_attach_mmc(host, ocr))
1507 mmc_power_off(host);
1508 goto out;
1509 }
94d89efb 1510
7310ece8 1511out_fail:
88ae8b86
HT
1512 mmc_release_host(host);
1513 mmc_power_off(host);
1514 }
28f52482
AV
1515out:
1516 if (host->caps & MMC_CAP_NEEDS_POLL)
1517 mmc_schedule_delayed_work(&host->detect, HZ);
1da177e4
LT
1518}
1519
b93931a6 1520void mmc_start_host(struct mmc_host *host)
1da177e4 1521{
b93931a6
PO
1522 mmc_power_off(host);
1523 mmc_detect_change(host, 0);
1da177e4
LT
1524}
1525
b93931a6 1526void mmc_stop_host(struct mmc_host *host)
1da177e4 1527{
3b91e550 1528#ifdef CONFIG_MMC_DEBUG
1efd48b3
PO
1529 unsigned long flags;
1530 spin_lock_irqsave(&host->lock, flags);
3b91e550 1531 host->removed = 1;
1efd48b3 1532 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
1533#endif
1534
8ea926b2
AH
1535 if (host->caps & MMC_CAP_DISABLE)
1536 cancel_delayed_work(&host->disable);
7de427d0 1537 cancel_delayed_work(&host->detect);
3b91e550
PO
1538 mmc_flush_scheduled_work();
1539
da68c4eb
NP
1540 /* clear pm flags now and let card drivers set them as needed */
1541 host->pm_flags = 0;
1542
7ea239d9
PO
1543 mmc_bus_get(host);
1544 if (host->bus_ops && !host->bus_dead) {
1545 if (host->bus_ops->remove)
1546 host->bus_ops->remove(host);
1547
1548 mmc_claim_host(host);
1549 mmc_detach_bus(host);
1550 mmc_release_host(host);
53509f0f
DK
1551 mmc_bus_put(host);
1552 return;
1da177e4 1553 }
7ea239d9
PO
1554 mmc_bus_put(host);
1555
1556 BUG_ON(host->card);
1da177e4
LT
1557
1558 mmc_power_off(host);
1559}
1560
eae1aeee
AH
1561void mmc_power_save_host(struct mmc_host *host)
1562{
1563 mmc_bus_get(host);
1564
1565 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1566 mmc_bus_put(host);
1567 return;
1568 }
1569
1570 if (host->bus_ops->power_save)
1571 host->bus_ops->power_save(host);
1572
1573 mmc_bus_put(host);
1574
1575 mmc_power_off(host);
1576}
1577EXPORT_SYMBOL(mmc_power_save_host);
1578
1579void mmc_power_restore_host(struct mmc_host *host)
1580{
1581 mmc_bus_get(host);
1582
1583 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1584 mmc_bus_put(host);
1585 return;
1586 }
1587
1588 mmc_power_up(host);
1589 host->bus_ops->power_restore(host);
1590
1591 mmc_bus_put(host);
1592}
1593EXPORT_SYMBOL(mmc_power_restore_host);
1594
b1ebe384
JL
1595int mmc_card_awake(struct mmc_host *host)
1596{
1597 int err = -ENOSYS;
1598
1599 mmc_bus_get(host);
1600
1601 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1602 err = host->bus_ops->awake(host);
1603
1604 mmc_bus_put(host);
1605
1606 return err;
1607}
1608EXPORT_SYMBOL(mmc_card_awake);
1609
1610int mmc_card_sleep(struct mmc_host *host)
1611{
1612 int err = -ENOSYS;
1613
1614 mmc_bus_get(host);
1615
1616 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1617 err = host->bus_ops->sleep(host);
1618
1619 mmc_bus_put(host);
1620
1621 return err;
1622}
1623EXPORT_SYMBOL(mmc_card_sleep);
1624
1625int mmc_card_can_sleep(struct mmc_host *host)
1626{
1627 struct mmc_card *card = host->card;
1628
1629 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1630 return 1;
1631 return 0;
1632}
1633EXPORT_SYMBOL(mmc_card_can_sleep);
1634
1da177e4
LT
1635#ifdef CONFIG_PM
1636
1637/**
1638 * mmc_suspend_host - suspend a host
1639 * @host: mmc host
1da177e4 1640 */
1a13f8fa 1641int mmc_suspend_host(struct mmc_host *host)
1da177e4 1642{
95cdfb72
NP
1643 int err = 0;
1644
8ea926b2
AH
1645 if (host->caps & MMC_CAP_DISABLE)
1646 cancel_delayed_work(&host->disable);
7de427d0 1647 cancel_delayed_work(&host->detect);
b5af25be
PO
1648 mmc_flush_scheduled_work();
1649
7ea239d9
PO
1650 mmc_bus_get(host);
1651 if (host->bus_ops && !host->bus_dead) {
6abaa0c9 1652 if (host->bus_ops->suspend)
95cdfb72 1653 err = host->bus_ops->suspend(host);
1c8cf9c9
OBC
1654 if (err == -ENOSYS || !host->bus_ops->resume) {
1655 /*
1656 * We simply "remove" the card in this case.
1657 * It will be redetected on resume.
1658 */
1659 if (host->bus_ops->remove)
1660 host->bus_ops->remove(host);
1661 mmc_claim_host(host);
1662 mmc_detach_bus(host);
1663 mmc_release_host(host);
1664 host->pm_flags = 0;
1665 err = 0;
1666 }
b5af25be 1667 }
7ea239d9
PO
1668 mmc_bus_put(host);
1669
da68c4eb 1670 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
95cdfb72 1671 mmc_power_off(host);
1da177e4 1672
95cdfb72 1673 return err;
1da177e4
LT
1674}
1675
1676EXPORT_SYMBOL(mmc_suspend_host);
1677
1678/**
1679 * mmc_resume_host - resume a previously suspended host
1680 * @host: mmc host
1681 */
1682int mmc_resume_host(struct mmc_host *host)
1683{
95cdfb72
NP
1684 int err = 0;
1685
6abaa0c9
PO
1686 mmc_bus_get(host);
1687 if (host->bus_ops && !host->bus_dead) {
da68c4eb
NP
1688 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1689 mmc_power_up(host);
1690 mmc_select_voltage(host, host->ocr);
1691 }
6abaa0c9 1692 BUG_ON(!host->bus_ops->resume);
95cdfb72
NP
1693 err = host->bus_ops->resume(host);
1694 if (err) {
1695 printk(KERN_WARNING "%s: error %d during resume "
1696 "(card was removed?)\n",
1697 mmc_hostname(host), err);
95cdfb72
NP
1698 err = 0;
1699 }
6abaa0c9
PO
1700 }
1701 mmc_bus_put(host);
1702
95cdfb72 1703 return err;
1da177e4 1704}
1da177e4
LT
1705EXPORT_SYMBOL(mmc_resume_host);
1706
4c2ef25f
ML
1707/* Do the card removal on suspend if card is assumed removeable
1708 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1709 to sync the card.
1710*/
1711int mmc_pm_notify(struct notifier_block *notify_block,
1712 unsigned long mode, void *unused)
1713{
1714 struct mmc_host *host = container_of(
1715 notify_block, struct mmc_host, pm_notify);
1716 unsigned long flags;
1717
1718
1719 switch (mode) {
1720 case PM_HIBERNATION_PREPARE:
1721 case PM_SUSPEND_PREPARE:
1722
1723 spin_lock_irqsave(&host->lock, flags);
1724 host->rescan_disable = 1;
1725 spin_unlock_irqrestore(&host->lock, flags);
1726 cancel_delayed_work_sync(&host->detect);
1727
1728 if (!host->bus_ops || host->bus_ops->suspend)
1729 break;
1730
1731 mmc_claim_host(host);
1732
1733 if (host->bus_ops->remove)
1734 host->bus_ops->remove(host);
1735
1736 mmc_detach_bus(host);
1737 mmc_release_host(host);
1738 host->pm_flags = 0;
1739 break;
1740
1741 case PM_POST_SUSPEND:
1742 case PM_POST_HIBERNATION:
1743
1744 spin_lock_irqsave(&host->lock, flags);
1745 host->rescan_disable = 0;
1746 spin_unlock_irqrestore(&host->lock, flags);
1747 mmc_detect_change(host, 0);
1748
1749 }
1750
1751 return 0;
1752}
1da177e4
LT
1753#endif
1754
ffce2e7e
PO
1755static int __init mmc_init(void)
1756{
1757 int ret;
1758
1759 workqueue = create_singlethread_workqueue("kmmcd");
1760 if (!workqueue)
1761 return -ENOMEM;
1762
1763 ret = mmc_register_bus();
e29a7d73
PO
1764 if (ret)
1765 goto destroy_workqueue;
1766
1767 ret = mmc_register_host_class();
1768 if (ret)
1769 goto unregister_bus;
1770
1771 ret = sdio_register_bus();
1772 if (ret)
1773 goto unregister_host_class;
1774
1775 return 0;
1776
1777unregister_host_class:
1778 mmc_unregister_host_class();
1779unregister_bus:
1780 mmc_unregister_bus();
1781destroy_workqueue:
1782 destroy_workqueue(workqueue);
1783
ffce2e7e
PO
1784 return ret;
1785}
1786
1787static void __exit mmc_exit(void)
1788{
e29a7d73 1789 sdio_unregister_bus();
ffce2e7e
PO
1790 mmc_unregister_host_class();
1791 mmc_unregister_bus();
1792 destroy_workqueue(workqueue);
1793}
1794
26074962 1795subsys_initcall(mmc_init);
ffce2e7e
PO
1796module_exit(mmc_exit);
1797
1da177e4 1798MODULE_LICENSE("GPL");