mmc: omap: remove clock rate hard coding
[linux-block.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>
e594573d 25#include <linux/pm_runtime.h>
35eb6db1 26#include <linux/suspend.h>
1b676f70
PF
27#include <linux/fault-inject.h>
28#include <linux/random.h>
1da177e4
LT
29
30#include <linux/mmc/card.h>
31#include <linux/mmc/host.h>
da7fbe58
PO
32#include <linux/mmc/mmc.h>
33#include <linux/mmc/sd.h>
1da177e4 34
aaac1b47 35#include "core.h"
ffce2e7e
PO
36#include "bus.h"
37#include "host.h"
e29a7d73 38#include "sdio_bus.h"
da7fbe58
PO
39
40#include "mmc_ops.h"
41#include "sd_ops.h"
5c4e6f13 42#include "sdio_ops.h"
1da177e4 43
ffce2e7e
PO
44static struct workqueue_struct *workqueue;
45
af517150
DB
46/*
47 * Enabling software CRCs on the data blocks can be a significant (30%)
48 * performance cost, and for other reasons may not always be desired.
49 * So we allow it it to be disabled.
50 */
51int use_spi_crc = 1;
52module_param(use_spi_crc, bool, 0);
53
bd68e083
BH
54/*
55 * We normally treat cards as removed during suspend if they are not
56 * known to be on a non-removable bus, to avoid the risk of writing
57 * back data to a different card after resume. Allow this to be
58 * overridden if necessary.
59 */
60#ifdef CONFIG_MMC_UNSAFE_RESUME
61int mmc_assume_removable;
62#else
63int mmc_assume_removable = 1;
64#endif
71d7d3d1 65EXPORT_SYMBOL(mmc_assume_removable);
bd68e083
BH
66module_param_named(removable, mmc_assume_removable, bool, 0644);
67MODULE_PARM_DESC(
68 removable,
69 "MMC/SD cards are removable and may be removed during suspend");
70
ffce2e7e
PO
71/*
72 * Internal function. Schedule delayed work in the MMC work queue.
73 */
74static int mmc_schedule_delayed_work(struct delayed_work *work,
75 unsigned long delay)
76{
77 return queue_delayed_work(workqueue, work, delay);
78}
79
80/*
81 * Internal function. Flush all scheduled work from the MMC work queue.
82 */
83static void mmc_flush_scheduled_work(void)
84{
85 flush_workqueue(workqueue);
86}
87
1b676f70
PF
88#ifdef CONFIG_FAIL_MMC_REQUEST
89
90/*
91 * Internal function. Inject random data errors.
92 * If mmc_data is NULL no errors are injected.
93 */
94static void mmc_should_fail_request(struct mmc_host *host,
95 struct mmc_request *mrq)
96{
97 struct mmc_command *cmd = mrq->cmd;
98 struct mmc_data *data = mrq->data;
99 static const int data_errors[] = {
100 -ETIMEDOUT,
101 -EILSEQ,
102 -EIO,
103 };
104
105 if (!data)
106 return;
107
108 if (cmd->error || data->error ||
109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
110 return;
111
112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
114}
115
116#else /* CONFIG_FAIL_MMC_REQUEST */
117
118static inline void mmc_should_fail_request(struct mmc_host *host,
119 struct mmc_request *mrq)
120{
121}
122
123#endif /* CONFIG_FAIL_MMC_REQUEST */
124
1da177e4 125/**
fe10c6ab
RK
126 * mmc_request_done - finish processing an MMC request
127 * @host: MMC host which completed request
128 * @mrq: MMC request which request
1da177e4
LT
129 *
130 * MMC drivers should call this function when they have completed
fe10c6ab 131 * their processing of a request.
1da177e4
LT
132 */
133void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
134{
135 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
136 int err = cmd->error;
137
af517150
DB
138 if (err && cmd->retries && mmc_host_is_spi(host)) {
139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
140 cmd->retries = 0;
141 }
142
d3049504 143 if (err && cmd->retries && !mmc_card_removed(host->card)) {
08a7e1df
AH
144 /*
145 * Request starter must handle retries - see
146 * mmc_wait_for_req_done().
147 */
148 if (mrq->done)
149 mrq->done(mrq);
e4d21708 150 } else {
1b676f70
PF
151 mmc_should_fail_request(host, mrq);
152
af8350c7
PO
153 led_trigger_event(host->led, LED_OFF);
154
e4d21708
PO
155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
156 mmc_hostname(host), cmd->opcode, err,
157 cmd->resp[0], cmd->resp[1],
158 cmd->resp[2], cmd->resp[3]);
159
160 if (mrq->data) {
161 pr_debug("%s: %d bytes transferred: %d\n",
162 mmc_hostname(host),
163 mrq->data->bytes_xfered, mrq->data->error);
164 }
165
166 if (mrq->stop) {
167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), mrq->stop->opcode,
169 mrq->stop->error,
170 mrq->stop->resp[0], mrq->stop->resp[1],
171 mrq->stop->resp[2], mrq->stop->resp[3]);
172 }
173
174 if (mrq->done)
175 mrq->done(mrq);
04566831 176
08c14071 177 mmc_host_clk_release(host);
1da177e4
LT
178 }
179}
180
181EXPORT_SYMBOL(mmc_request_done);
182
39361851 183static void
1da177e4
LT
184mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
185{
976d9276
PO
186#ifdef CONFIG_MMC_DEBUG
187 unsigned int i, sz;
a84756c5 188 struct scatterlist *sg;
976d9276
PO
189#endif
190
920e70c5
RK
191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
192 mmc_hostname(host), mrq->cmd->opcode,
193 mrq->cmd->arg, mrq->cmd->flags);
1da177e4 194
e4d21708
PO
195 if (mrq->data) {
196 pr_debug("%s: blksz %d blocks %d flags %08x "
197 "tsac %d ms nsac %d\n",
198 mmc_hostname(host), mrq->data->blksz,
199 mrq->data->blocks, mrq->data->flags,
ce252edd 200 mrq->data->timeout_ns / 1000000,
e4d21708
PO
201 mrq->data->timeout_clks);
202 }
203
204 if (mrq->stop) {
205 pr_debug("%s: CMD%u arg %08x flags %08x\n",
206 mmc_hostname(host), mrq->stop->opcode,
207 mrq->stop->arg, mrq->stop->flags);
208 }
209
f22ee4ed 210 WARN_ON(!host->claimed);
1da177e4
LT
211
212 mrq->cmd->error = 0;
213 mrq->cmd->mrq = mrq;
214 if (mrq->data) {
fe4a3c7a 215 BUG_ON(mrq->data->blksz > host->max_blk_size);
55db890a
PO
216 BUG_ON(mrq->data->blocks > host->max_blk_count);
217 BUG_ON(mrq->data->blocks * mrq->data->blksz >
218 host->max_req_size);
fe4a3c7a 219
976d9276
PO
220#ifdef CONFIG_MMC_DEBUG
221 sz = 0;
a84756c5
PO
222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
223 sz += sg->length;
976d9276
PO
224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
225#endif
226
1da177e4
LT
227 mrq->cmd->data = mrq->data;
228 mrq->data->error = 0;
229 mrq->data->mrq = mrq;
230 if (mrq->stop) {
231 mrq->data->stop = mrq->stop;
232 mrq->stop->error = 0;
233 mrq->stop->mrq = mrq;
234 }
235 }
08c14071 236 mmc_host_clk_hold(host);
66c036e0 237 led_trigger_event(host->led, LED_FULL);
1da177e4
LT
238 host->ops->request(host, mrq);
239}
240
1da177e4
LT
241static void mmc_wait_done(struct mmc_request *mrq)
242{
aa8b683a
PF
243 complete(&mrq->completion);
244}
245
246static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
247{
248 init_completion(&mrq->completion);
249 mrq->done = mmc_wait_done;
d3049504
AH
250 if (mmc_card_removed(host->card)) {
251 mrq->cmd->error = -ENOMEDIUM;
252 complete(&mrq->completion);
253 return;
254 }
aa8b683a
PF
255 mmc_start_request(host, mrq);
256}
257
258static void mmc_wait_for_req_done(struct mmc_host *host,
259 struct mmc_request *mrq)
260{
08a7e1df
AH
261 struct mmc_command *cmd;
262
263 while (1) {
264 wait_for_completion(&mrq->completion);
265
266 cmd = mrq->cmd;
d3049504
AH
267 if (!cmd->error || !cmd->retries ||
268 mmc_card_removed(host->card))
08a7e1df
AH
269 break;
270
271 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
272 mmc_hostname(host), cmd->opcode, cmd->error);
273 cmd->retries--;
274 cmd->error = 0;
275 host->ops->request(host, mrq);
276 }
aa8b683a
PF
277}
278
279/**
280 * mmc_pre_req - Prepare for a new request
281 * @host: MMC host to prepare command
282 * @mrq: MMC request to prepare for
283 * @is_first_req: true if there is no previous started request
284 * that may run in parellel to this call, otherwise false
285 *
286 * mmc_pre_req() is called in prior to mmc_start_req() to let
287 * host prepare for the new request. Preparation of a request may be
288 * performed while another request is running on the host.
289 */
290static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
291 bool is_first_req)
292{
293 if (host->ops->pre_req)
294 host->ops->pre_req(host, mrq, is_first_req);
295}
296
297/**
298 * mmc_post_req - Post process a completed request
299 * @host: MMC host to post process command
300 * @mrq: MMC request to post process for
301 * @err: Error, if non zero, clean up any resources made in pre_req
302 *
303 * Let the host post process a completed request. Post processing of
304 * a request may be performed while another reuqest is running.
305 */
306static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
307 int err)
308{
309 if (host->ops->post_req)
310 host->ops->post_req(host, mrq, err);
1da177e4
LT
311}
312
aa8b683a
PF
313/**
314 * mmc_start_req - start a non-blocking request
315 * @host: MMC host to start command
316 * @areq: async request to start
317 * @error: out parameter returns 0 for success, otherwise non zero
318 *
319 * Start a new MMC custom command request for a host.
320 * If there is on ongoing async request wait for completion
321 * of that request and start the new one and return.
322 * Does not wait for the new request to complete.
323 *
324 * Returns the completed request, NULL in case of none completed.
325 * Wait for the an ongoing request (previoulsy started) to complete and
326 * return the completed request. If there is no ongoing request, NULL
327 * is returned without waiting. NULL is not an error condition.
328 */
329struct mmc_async_req *mmc_start_req(struct mmc_host *host,
330 struct mmc_async_req *areq, int *error)
331{
332 int err = 0;
333 struct mmc_async_req *data = host->areq;
334
335 /* Prepare a new request */
336 if (areq)
337 mmc_pre_req(host, areq->mrq, !host->areq);
338
339 if (host->areq) {
340 mmc_wait_for_req_done(host, host->areq->mrq);
341 err = host->areq->err_check(host->card, host->areq);
342 if (err) {
7c8a2829 343 /* post process the completed failed request */
aa8b683a
PF
344 mmc_post_req(host, host->areq->mrq, 0);
345 if (areq)
7c8a2829
PF
346 /*
347 * Cancel the new prepared request, because
348 * it can't run until the failed
349 * request has been properly handled.
350 */
aa8b683a
PF
351 mmc_post_req(host, areq->mrq, -EINVAL);
352
353 host->areq = NULL;
354 goto out;
355 }
356 }
357
358 if (areq)
359 __mmc_start_req(host, areq->mrq);
360
361 if (host->areq)
362 mmc_post_req(host, host->areq->mrq, 0);
363
364 host->areq = areq;
365 out:
366 if (error)
367 *error = err;
368 return data;
369}
370EXPORT_SYMBOL(mmc_start_req);
371
67a61c48
PO
372/**
373 * mmc_wait_for_req - start a request and wait for completion
374 * @host: MMC host to start command
375 * @mrq: MMC request to start
376 *
377 * Start a new MMC custom command request for a host, and wait
378 * for the command to complete. Does not attempt to parse the
379 * response.
380 */
381void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 382{
aa8b683a
PF
383 __mmc_start_req(host, mrq);
384 mmc_wait_for_req_done(host, mrq);
1da177e4 385}
1da177e4
LT
386EXPORT_SYMBOL(mmc_wait_for_req);
387
eb0d8f13
JC
388/**
389 * mmc_interrupt_hpi - Issue for High priority Interrupt
390 * @card: the MMC card associated with the HPI transfer
391 *
392 * Issued High Priority Interrupt, and check for card status
393 * util out-of prg-state.
394 */
395int mmc_interrupt_hpi(struct mmc_card *card)
396{
397 int err;
398 u32 status;
399
400 BUG_ON(!card);
401
402 if (!card->ext_csd.hpi_en) {
403 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
404 return 1;
405 }
406
407 mmc_claim_host(card->host);
408 err = mmc_send_status(card, &status);
409 if (err) {
410 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
411 goto out;
412 }
413
414 /*
415 * If the card status is in PRG-state, we can send the HPI command.
416 */
417 if (R1_CURRENT_STATE(status) == R1_STATE_PRG) {
418 do {
419 /*
420 * We don't know when the HPI command will finish
421 * processing, so we need to resend HPI until out
422 * of prg-state, and keep checking the card status
423 * with SEND_STATUS. If a timeout error occurs when
424 * sending the HPI command, we are already out of
425 * prg-state.
426 */
427 err = mmc_send_hpi_cmd(card, &status);
428 if (err)
429 pr_debug("%s: abort HPI (%d error)\n",
430 mmc_hostname(card->host), err);
431
432 err = mmc_send_status(card, &status);
433 if (err)
434 break;
435 } while (R1_CURRENT_STATE(status) == R1_STATE_PRG);
436 } else
437 pr_debug("%s: Left prg-state\n", mmc_hostname(card->host));
438
439out:
440 mmc_release_host(card->host);
441 return err;
442}
443EXPORT_SYMBOL(mmc_interrupt_hpi);
444
1da177e4
LT
445/**
446 * mmc_wait_for_cmd - start a command and wait for completion
447 * @host: MMC host to start command
448 * @cmd: MMC command to start
449 * @retries: maximum number of retries
450 *
451 * Start a new MMC command for a host, and wait for the command
452 * to complete. Return any error that occurred while the command
453 * was executing. Do not attempt to parse the response.
454 */
455int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
456{
ad5fd972 457 struct mmc_request mrq = {NULL};
1da177e4 458
d84075c8 459 WARN_ON(!host->claimed);
1da177e4 460
1da177e4
LT
461 memset(cmd->resp, 0, sizeof(cmd->resp));
462 cmd->retries = retries;
463
464 mrq.cmd = cmd;
465 cmd->data = NULL;
466
467 mmc_wait_for_req(host, &mrq);
468
469 return cmd->error;
470}
471
472EXPORT_SYMBOL(mmc_wait_for_cmd);
473
d773d725
RK
474/**
475 * mmc_set_data_timeout - set the timeout for a data command
476 * @data: data phase for command
477 * @card: the MMC card associated with the data transfer
67a61c48
PO
478 *
479 * Computes the data timeout parameters according to the
480 * correct algorithm given the card type.
d773d725 481 */
b146d26a 482void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
483{
484 unsigned int mult;
485
e6f918bf
PO
486 /*
487 * SDIO cards only define an upper 1 s limit on access.
488 */
489 if (mmc_card_sdio(card)) {
490 data->timeout_ns = 1000000000;
491 data->timeout_clks = 0;
492 return;
493 }
494
d773d725
RK
495 /*
496 * SD cards use a 100 multiplier rather than 10
497 */
498 mult = mmc_card_sd(card) ? 100 : 10;
499
500 /*
501 * Scale up the multiplier (and therefore the timeout) by
502 * the r2w factor for writes.
503 */
b146d26a 504 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
505 mult <<= card->csd.r2w_factor;
506
507 data->timeout_ns = card->csd.tacc_ns * mult;
508 data->timeout_clks = card->csd.tacc_clks * mult;
509
510 /*
511 * SD cards also have an upper limit on the timeout.
512 */
513 if (mmc_card_sd(card)) {
514 unsigned int timeout_us, limit_us;
515
516 timeout_us = data->timeout_ns / 1000;
e9b86841
LW
517 if (mmc_host_clk_rate(card->host))
518 timeout_us += data->timeout_clks * 1000 /
519 (mmc_host_clk_rate(card->host) / 1000);
d773d725 520
b146d26a 521 if (data->flags & MMC_DATA_WRITE)
493890e7
PO
522 /*
523 * The limit is really 250 ms, but that is
524 * insufficient for some crappy cards.
525 */
526 limit_us = 300000;
d773d725
RK
527 else
528 limit_us = 100000;
529
fba68bd2
PL
530 /*
531 * SDHC cards always use these fixed values.
532 */
533 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
d773d725
RK
534 data->timeout_ns = limit_us * 1000;
535 data->timeout_clks = 0;
536 }
537 }
6de5fc9c
SNX
538
539 /*
540 * Some cards require longer data read timeout than indicated in CSD.
541 * Address this by setting the read timeout to a "reasonably high"
542 * value. For the cards tested, 300ms has proven enough. If necessary,
543 * this value can be increased if other problematic cards require this.
544 */
545 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
546 data->timeout_ns = 300000000;
547 data->timeout_clks = 0;
548 }
549
c0c88871
WM
550 /*
551 * Some cards need very high timeouts if driven in SPI mode.
552 * The worst observed timeout was 900ms after writing a
553 * continuous stream of data until the internal logic
554 * overflowed.
555 */
556 if (mmc_host_is_spi(card->host)) {
557 if (data->flags & MMC_DATA_WRITE) {
558 if (data->timeout_ns < 1000000000)
559 data->timeout_ns = 1000000000; /* 1s */
560 } else {
561 if (data->timeout_ns < 100000000)
562 data->timeout_ns = 100000000; /* 100ms */
563 }
564 }
d773d725
RK
565}
566EXPORT_SYMBOL(mmc_set_data_timeout);
567
ad3868b2
PO
568/**
569 * mmc_align_data_size - pads a transfer size to a more optimal value
570 * @card: the MMC card associated with the data transfer
571 * @sz: original transfer size
572 *
573 * Pads the original data size with a number of extra bytes in
574 * order to avoid controller bugs and/or performance hits
575 * (e.g. some controllers revert to PIO for certain sizes).
576 *
577 * Returns the improved size, which might be unmodified.
578 *
579 * Note that this function is only relevant when issuing a
580 * single scatter gather entry.
581 */
582unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
583{
584 /*
585 * FIXME: We don't have a system for the controller to tell
586 * the core about its problems yet, so for now we just 32-bit
587 * align the size.
588 */
589 sz = ((sz + 3) / 4) * 4;
590
591 return sz;
592}
593EXPORT_SYMBOL(mmc_align_data_size);
594
8ea926b2
AH
595/**
596 * mmc_host_enable - enable a host.
597 * @host: mmc host to enable
598 *
599 * Hosts that support power saving can use the 'enable' and 'disable'
600 * methods to exit and enter power saving states. For more information
601 * see comments for struct mmc_host_ops.
602 */
603int mmc_host_enable(struct mmc_host *host)
604{
605 if (!(host->caps & MMC_CAP_DISABLE))
606 return 0;
607
608 if (host->en_dis_recurs)
609 return 0;
610
611 if (host->nesting_cnt++)
612 return 0;
613
614 cancel_delayed_work_sync(&host->disable);
615
616 if (host->enabled)
617 return 0;
618
619 if (host->ops->enable) {
620 int err;
621
622 host->en_dis_recurs = 1;
623 err = host->ops->enable(host);
624 host->en_dis_recurs = 0;
625
626 if (err) {
627 pr_debug("%s: enable error %d\n",
628 mmc_hostname(host), err);
629 return err;
630 }
631 }
632 host->enabled = 1;
633 return 0;
634}
635EXPORT_SYMBOL(mmc_host_enable);
636
637static int mmc_host_do_disable(struct mmc_host *host, int lazy)
638{
639 if (host->ops->disable) {
640 int err;
641
642 host->en_dis_recurs = 1;
643 err = host->ops->disable(host, lazy);
644 host->en_dis_recurs = 0;
645
646 if (err < 0) {
647 pr_debug("%s: disable error %d\n",
648 mmc_hostname(host), err);
649 return err;
650 }
651 if (err > 0) {
652 unsigned long delay = msecs_to_jiffies(err);
653
654 mmc_schedule_delayed_work(&host->disable, delay);
655 }
656 }
657 host->enabled = 0;
658 return 0;
659}
660
661/**
662 * mmc_host_disable - disable a host.
663 * @host: mmc host to disable
664 *
665 * Hosts that support power saving can use the 'enable' and 'disable'
666 * methods to exit and enter power saving states. For more information
667 * see comments for struct mmc_host_ops.
668 */
669int mmc_host_disable(struct mmc_host *host)
670{
671 int err;
672
673 if (!(host->caps & MMC_CAP_DISABLE))
674 return 0;
675
676 if (host->en_dis_recurs)
677 return 0;
678
679 if (--host->nesting_cnt)
680 return 0;
681
682 if (!host->enabled)
683 return 0;
684
685 err = mmc_host_do_disable(host, 0);
686 return err;
687}
688EXPORT_SYMBOL(mmc_host_disable);
689
1da177e4 690/**
2342f332 691 * __mmc_claim_host - exclusively claim a host
1da177e4 692 * @host: mmc host to claim
2342f332 693 * @abort: whether or not the operation should be aborted
1da177e4 694 *
2342f332
NP
695 * Claim a host for a set of operations. If @abort is non null and
696 * dereference a non-zero value then this will return prematurely with
697 * that non-zero value without acquiring the lock. Returns zero
698 * with the lock held otherwise.
1da177e4 699 */
2342f332 700int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
1da177e4
LT
701{
702 DECLARE_WAITQUEUE(wait, current);
703 unsigned long flags;
2342f332 704 int stop;
1da177e4 705
cf795bfb
PO
706 might_sleep();
707
1da177e4
LT
708 add_wait_queue(&host->wq, &wait);
709 spin_lock_irqsave(&host->lock, flags);
710 while (1) {
711 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332 712 stop = abort ? atomic_read(abort) : 0;
319a3f14 713 if (stop || !host->claimed || host->claimer == current)
1da177e4
LT
714 break;
715 spin_unlock_irqrestore(&host->lock, flags);
716 schedule();
717 spin_lock_irqsave(&host->lock, flags);
718 }
719 set_current_state(TASK_RUNNING);
319a3f14 720 if (!stop) {
2342f332 721 host->claimed = 1;
319a3f14
AH
722 host->claimer = current;
723 host->claim_cnt += 1;
724 } else
2342f332 725 wake_up(&host->wq);
1da177e4
LT
726 spin_unlock_irqrestore(&host->lock, flags);
727 remove_wait_queue(&host->wq, &wait);
8ea926b2
AH
728 if (!stop)
729 mmc_host_enable(host);
2342f332 730 return stop;
1da177e4
LT
731}
732
2342f332 733EXPORT_SYMBOL(__mmc_claim_host);
1da177e4 734
319a3f14
AH
735/**
736 * mmc_try_claim_host - try exclusively to claim a host
737 * @host: mmc host to claim
738 *
739 * Returns %1 if the host is claimed, %0 otherwise.
740 */
741int mmc_try_claim_host(struct mmc_host *host)
8ea926b2
AH
742{
743 int claimed_host = 0;
744 unsigned long flags;
745
746 spin_lock_irqsave(&host->lock, flags);
319a3f14 747 if (!host->claimed || host->claimer == current) {
8ea926b2 748 host->claimed = 1;
319a3f14
AH
749 host->claimer = current;
750 host->claim_cnt += 1;
8ea926b2
AH
751 claimed_host = 1;
752 }
753 spin_unlock_irqrestore(&host->lock, flags);
754 return claimed_host;
755}
319a3f14 756EXPORT_SYMBOL(mmc_try_claim_host);
8ea926b2 757
ab1efd27
UH
758/**
759 * mmc_do_release_host - release a claimed host
760 * @host: mmc host to release
761 *
762 * If you successfully claimed a host, this function will
763 * release it again.
764 */
765void mmc_do_release_host(struct mmc_host *host)
8ea926b2
AH
766{
767 unsigned long flags;
768
769 spin_lock_irqsave(&host->lock, flags);
319a3f14
AH
770 if (--host->claim_cnt) {
771 /* Release for nested claim */
772 spin_unlock_irqrestore(&host->lock, flags);
773 } else {
774 host->claimed = 0;
775 host->claimer = NULL;
776 spin_unlock_irqrestore(&host->lock, flags);
777 wake_up(&host->wq);
778 }
8ea926b2 779}
ab1efd27 780EXPORT_SYMBOL(mmc_do_release_host);
8ea926b2
AH
781
782void mmc_host_deeper_disable(struct work_struct *work)
783{
784 struct mmc_host *host =
785 container_of(work, struct mmc_host, disable.work);
786
787 /* If the host is claimed then we do not want to disable it anymore */
788 if (!mmc_try_claim_host(host))
789 return;
790 mmc_host_do_disable(host, 1);
791 mmc_do_release_host(host);
792}
793
794/**
795 * mmc_host_lazy_disable - lazily disable a host.
796 * @host: mmc host to disable
797 *
798 * Hosts that support power saving can use the 'enable' and 'disable'
799 * methods to exit and enter power saving states. For more information
800 * see comments for struct mmc_host_ops.
801 */
802int mmc_host_lazy_disable(struct mmc_host *host)
803{
804 if (!(host->caps & MMC_CAP_DISABLE))
805 return 0;
806
807 if (host->en_dis_recurs)
808 return 0;
809
810 if (--host->nesting_cnt)
811 return 0;
812
813 if (!host->enabled)
814 return 0;
815
816 if (host->disable_delay) {
817 mmc_schedule_delayed_work(&host->disable,
818 msecs_to_jiffies(host->disable_delay));
819 return 0;
820 } else
821 return mmc_host_do_disable(host, 1);
822}
823EXPORT_SYMBOL(mmc_host_lazy_disable);
824
1da177e4
LT
825/**
826 * mmc_release_host - release a host
827 * @host: mmc host to release
828 *
829 * Release a MMC host, allowing others to claim the host
830 * for their operations.
831 */
832void mmc_release_host(struct mmc_host *host)
833{
d84075c8 834 WARN_ON(!host->claimed);
1da177e4 835
8ea926b2 836 mmc_host_lazy_disable(host);
1da177e4 837
8ea926b2 838 mmc_do_release_host(host);
1da177e4
LT
839}
840
841EXPORT_SYMBOL(mmc_release_host);
842
7ea239d9
PO
843/*
844 * Internal function that does the actual ios call to the host driver,
845 * optionally printing some debug output.
846 */
920e70c5
RK
847static inline void mmc_set_ios(struct mmc_host *host)
848{
849 struct mmc_ios *ios = &host->ios;
850
cd9277c0
PO
851 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
852 "width %u timing %u\n",
920e70c5
RK
853 mmc_hostname(host), ios->clock, ios->bus_mode,
854 ios->power_mode, ios->chip_select, ios->vdd,
cd9277c0 855 ios->bus_width, ios->timing);
fba68bd2 856
04566831
LW
857 if (ios->clock > 0)
858 mmc_set_ungated(host);
920e70c5
RK
859 host->ops->set_ios(host, ios);
860}
861
7ea239d9
PO
862/*
863 * Control chip select pin on a host.
864 */
da7fbe58 865void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 866{
778e277c 867 mmc_host_clk_hold(host);
da7fbe58
PO
868 host->ios.chip_select = mode;
869 mmc_set_ios(host);
778e277c 870 mmc_host_clk_release(host);
1da177e4
LT
871}
872
7ea239d9
PO
873/*
874 * Sets the host clock to the highest possible frequency that
875 * is below "hz".
876 */
778e277c 877static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
7ea239d9
PO
878{
879 WARN_ON(hz < host->f_min);
880
881 if (hz > host->f_max)
882 hz = host->f_max;
883
884 host->ios.clock = hz;
885 mmc_set_ios(host);
886}
887
778e277c
MW
888void mmc_set_clock(struct mmc_host *host, unsigned int hz)
889{
890 mmc_host_clk_hold(host);
891 __mmc_set_clock(host, hz);
892 mmc_host_clk_release(host);
893}
894
04566831
LW
895#ifdef CONFIG_MMC_CLKGATE
896/*
897 * This gates the clock by setting it to 0 Hz.
898 */
899void mmc_gate_clock(struct mmc_host *host)
900{
901 unsigned long flags;
902
903 spin_lock_irqsave(&host->clk_lock, flags);
904 host->clk_old = host->ios.clock;
905 host->ios.clock = 0;
906 host->clk_gated = true;
907 spin_unlock_irqrestore(&host->clk_lock, flags);
908 mmc_set_ios(host);
909}
910
911/*
912 * This restores the clock from gating by using the cached
913 * clock value.
914 */
915void mmc_ungate_clock(struct mmc_host *host)
916{
917 /*
918 * We should previously have gated the clock, so the clock shall
919 * be 0 here! The clock may however be 0 during initialization,
920 * when some request operations are performed before setting
921 * the frequency. When ungate is requested in that situation
922 * we just ignore the call.
923 */
924 if (host->clk_old) {
925 BUG_ON(host->ios.clock);
926 /* This call will also set host->clk_gated to false */
778e277c 927 __mmc_set_clock(host, host->clk_old);
04566831
LW
928 }
929}
930
931void mmc_set_ungated(struct mmc_host *host)
932{
933 unsigned long flags;
934
935 /*
936 * We've been given a new frequency while the clock is gated,
937 * so make sure we regard this as ungating it.
938 */
939 spin_lock_irqsave(&host->clk_lock, flags);
940 host->clk_gated = false;
941 spin_unlock_irqrestore(&host->clk_lock, flags);
942}
943
944#else
945void mmc_set_ungated(struct mmc_host *host)
946{
947}
948#endif
949
7ea239d9
PO
950/*
951 * Change the bus mode (open drain/push-pull) of a host.
952 */
953void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
954{
778e277c 955 mmc_host_clk_hold(host);
7ea239d9
PO
956 host->ios.bus_mode = mode;
957 mmc_set_ios(host);
778e277c 958 mmc_host_clk_release(host);
7ea239d9
PO
959}
960
0f8d8ea6
AH
961/*
962 * Change data bus width of a host.
963 */
964void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
965{
778e277c 966 mmc_host_clk_hold(host);
4c4cb171
PR
967 host->ios.bus_width = width;
968 mmc_set_ios(host);
778e277c 969 mmc_host_clk_release(host);
0f8d8ea6
AH
970}
971
86e8286a
AV
972/**
973 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
974 * @vdd: voltage (mV)
975 * @low_bits: prefer low bits in boundary cases
976 *
977 * This function returns the OCR bit number according to the provided @vdd
978 * value. If conversion is not possible a negative errno value returned.
979 *
980 * Depending on the @low_bits flag the function prefers low or high OCR bits
981 * on boundary voltages. For example,
982 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
983 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
984 *
985 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
986 */
987static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
988{
989 const int max_bit = ilog2(MMC_VDD_35_36);
990 int bit;
991
992 if (vdd < 1650 || vdd > 3600)
993 return -EINVAL;
994
995 if (vdd >= 1650 && vdd <= 1950)
996 return ilog2(MMC_VDD_165_195);
997
998 if (low_bits)
999 vdd -= 1;
1000
1001 /* Base 2000 mV, step 100 mV, bit's base 8. */
1002 bit = (vdd - 2000) / 100 + 8;
1003 if (bit > max_bit)
1004 return max_bit;
1005 return bit;
1006}
1007
1008/**
1009 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1010 * @vdd_min: minimum voltage value (mV)
1011 * @vdd_max: maximum voltage value (mV)
1012 *
1013 * This function returns the OCR mask bits according to the provided @vdd_min
1014 * and @vdd_max values. If conversion is not possible the function returns 0.
1015 *
1016 * Notes wrt boundary cases:
1017 * This function sets the OCR bits for all boundary voltages, for example
1018 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1019 * MMC_VDD_34_35 mask.
1020 */
1021u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1022{
1023 u32 mask = 0;
1024
1025 if (vdd_max < vdd_min)
1026 return 0;
1027
1028 /* Prefer high bits for the boundary vdd_max values. */
1029 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1030 if (vdd_max < 0)
1031 return 0;
1032
1033 /* Prefer low bits for the boundary vdd_min values. */
1034 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1035 if (vdd_min < 0)
1036 return 0;
1037
1038 /* Fill the mask, from max bit to min bit. */
1039 while (vdd_max >= vdd_min)
1040 mask |= 1 << vdd_max--;
1041
1042 return mask;
1043}
1044EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1045
5c13941a
DB
1046#ifdef CONFIG_REGULATOR
1047
1048/**
1049 * mmc_regulator_get_ocrmask - return mask of supported voltages
1050 * @supply: regulator to use
1051 *
1052 * This returns either a negative errno, or a mask of voltages that
1053 * can be provided to MMC/SD/SDIO devices using the specified voltage
1054 * regulator. This would normally be called before registering the
1055 * MMC host adapter.
1056 */
1057int mmc_regulator_get_ocrmask(struct regulator *supply)
1058{
1059 int result = 0;
1060 int count;
1061 int i;
1062
1063 count = regulator_count_voltages(supply);
1064 if (count < 0)
1065 return count;
1066
1067 for (i = 0; i < count; i++) {
1068 int vdd_uV;
1069 int vdd_mV;
1070
1071 vdd_uV = regulator_list_voltage(supply, i);
1072 if (vdd_uV <= 0)
1073 continue;
1074
1075 vdd_mV = vdd_uV / 1000;
1076 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1077 }
1078
1079 return result;
1080}
1081EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
1082
1083/**
1084 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
99fc5131 1085 * @mmc: the host to regulate
5c13941a 1086 * @supply: regulator to use
99fc5131 1087 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
5c13941a
DB
1088 *
1089 * Returns zero on success, else negative errno.
1090 *
1091 * MMC host drivers may use this to enable or disable a regulator using
1092 * a particular supply voltage. This would normally be called from the
1093 * set_ios() method.
1094 */
99fc5131
LW
1095int mmc_regulator_set_ocr(struct mmc_host *mmc,
1096 struct regulator *supply,
1097 unsigned short vdd_bit)
5c13941a
DB
1098{
1099 int result = 0;
1100 int min_uV, max_uV;
5c13941a
DB
1101
1102 if (vdd_bit) {
1103 int tmp;
1104 int voltage;
1105
1106 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1107 * bits this regulator doesn't quite support ... don't
1108 * be too picky, most cards and regulators are OK with
1109 * a 0.1V range goof (it's a small error percentage).
1110 */
1111 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1112 if (tmp == 0) {
1113 min_uV = 1650 * 1000;
1114 max_uV = 1950 * 1000;
1115 } else {
1116 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1117 max_uV = min_uV + 100 * 1000;
1118 }
1119
1120 /* avoid needless changes to this voltage; the regulator
1121 * might not allow this operation
1122 */
1123 voltage = regulator_get_voltage(supply);
1124 if (voltage < 0)
1125 result = voltage;
1126 else if (voltage < min_uV || voltage > max_uV)
1127 result = regulator_set_voltage(supply, min_uV, max_uV);
1128 else
1129 result = 0;
1130
99fc5131 1131 if (result == 0 && !mmc->regulator_enabled) {
5c13941a 1132 result = regulator_enable(supply);
99fc5131
LW
1133 if (!result)
1134 mmc->regulator_enabled = true;
1135 }
1136 } else if (mmc->regulator_enabled) {
5c13941a 1137 result = regulator_disable(supply);
99fc5131
LW
1138 if (result == 0)
1139 mmc->regulator_enabled = false;
5c13941a
DB
1140 }
1141
99fc5131
LW
1142 if (result)
1143 dev_err(mmc_dev(mmc),
1144 "could not set regulator OCR (%d)\n", result);
5c13941a
DB
1145 return result;
1146}
1147EXPORT_SYMBOL(mmc_regulator_set_ocr);
1148
99fc5131 1149#endif /* CONFIG_REGULATOR */
5c13941a 1150
1da177e4
LT
1151/*
1152 * Mask off any voltages we don't support and select
1153 * the lowest voltage
1154 */
7ea239d9 1155u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
1156{
1157 int bit;
1158
1159 ocr &= host->ocr_avail;
1160
1161 bit = ffs(ocr);
1162 if (bit) {
1163 bit -= 1;
1164
63ef731a 1165 ocr &= 3 << bit;
1da177e4 1166
778e277c 1167 mmc_host_clk_hold(host);
1da177e4 1168 host->ios.vdd = bit;
920e70c5 1169 mmc_set_ios(host);
778e277c 1170 mmc_host_clk_release(host);
1da177e4 1171 } else {
f6e10b86
DB
1172 pr_warning("%s: host doesn't support card's voltages\n",
1173 mmc_hostname(host));
1da177e4
LT
1174 ocr = 0;
1175 }
1176
1177 return ocr;
1178}
1179
261bbd46 1180int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
f2119df6
AN
1181{
1182 struct mmc_command cmd = {0};
1183 int err = 0;
1184
1185 BUG_ON(!host);
1186
1187 /*
1188 * Send CMD11 only if the request is to switch the card to
1189 * 1.8V signalling.
1190 */
261bbd46 1191 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
f2119df6
AN
1192 cmd.opcode = SD_SWITCH_VOLTAGE;
1193 cmd.arg = 0;
1194 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1195
1196 err = mmc_wait_for_cmd(host, &cmd, 0);
1197 if (err)
1198 return err;
1199
1200 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1201 return -EIO;
1202 }
1203
1204 host->ios.signal_voltage = signal_voltage;
1205
1206 if (host->ops->start_signal_voltage_switch)
1207 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1208
1209 return err;
1210}
1211
b57c43ad 1212/*
7ea239d9 1213 * Select timing parameters for host.
b57c43ad 1214 */
7ea239d9 1215void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 1216{
778e277c 1217 mmc_host_clk_hold(host);
7ea239d9
PO
1218 host->ios.timing = timing;
1219 mmc_set_ios(host);
778e277c 1220 mmc_host_clk_release(host);
b57c43ad
PO
1221}
1222
d6d50a15
AN
1223/*
1224 * Select appropriate driver type for host.
1225 */
1226void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1227{
778e277c 1228 mmc_host_clk_hold(host);
d6d50a15
AN
1229 host->ios.drv_type = drv_type;
1230 mmc_set_ios(host);
778e277c 1231 mmc_host_clk_release(host);
d6d50a15
AN
1232}
1233
a80f1627
G
1234static void mmc_poweroff_notify(struct mmc_host *host)
1235{
1236 struct mmc_card *card;
1237 unsigned int timeout;
1238 unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION;
1239 int err = 0;
1240
1241 card = host->card;
1242
1243 /*
1244 * Send power notify command only if card
1245 * is mmc and notify state is powered ON
1246 */
1247 if (card && mmc_card_mmc(card) &&
1248 (card->poweroff_notify_state == MMC_POWERED_ON)) {
1249
1250 if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
1251 notify_type = EXT_CSD_POWER_OFF_SHORT;
1252 timeout = card->ext_csd.generic_cmd6_time;
1253 card->poweroff_notify_state = MMC_POWEROFF_SHORT;
1254 } else {
1255 notify_type = EXT_CSD_POWER_OFF_LONG;
1256 timeout = card->ext_csd.power_off_longtime;
1257 card->poweroff_notify_state = MMC_POWEROFF_LONG;
1258 }
1259
1260 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1261 EXT_CSD_POWER_OFF_NOTIFICATION,
1262 notify_type, timeout);
1263
1264 if (err && err != -EBADMSG)
1265 pr_err("Device failed to respond within %d poweroff "
1266 "time. Forcefully powering down the device\n",
1267 timeout);
1268
1269 /* Set the card state to no notification after the poweroff */
1270 card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
1271 }
1272}
1273
1da177e4 1274/*
45f8245b
RK
1275 * Apply power to the MMC stack. This is a two-stage process.
1276 * First, we enable power to the card without the clock running.
1277 * We then wait a bit for the power to stabilise. Finally,
1278 * enable the bus drivers and clock to the card.
1279 *
1280 * We must _NOT_ enable the clock prior to power stablising.
1281 *
1282 * If a host does all the power sequencing itself, ignore the
1283 * initial MMC_POWER_UP stage.
1da177e4
LT
1284 */
1285static void mmc_power_up(struct mmc_host *host)
1286{
500f3564
BR
1287 int bit;
1288
778e277c
MW
1289 mmc_host_clk_hold(host);
1290
500f3564
BR
1291 /* If ocr is set, we use it */
1292 if (host->ocr)
1293 bit = ffs(host->ocr) - 1;
1294 else
1295 bit = fls(host->ocr_avail) - 1;
1da177e4
LT
1296
1297 host->ios.vdd = bit;
44669034 1298 if (mmc_host_is_spi(host))
af517150 1299 host->ios.chip_select = MMC_CS_HIGH;
44669034 1300 else
af517150 1301 host->ios.chip_select = MMC_CS_DONTCARE;
44669034 1302 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1da177e4 1303 host->ios.power_mode = MMC_POWER_UP;
f218278a 1304 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 1305 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 1306 mmc_set_ios(host);
1da177e4 1307
f9996aee
PO
1308 /*
1309 * This delay should be sufficient to allow the power supply
1310 * to reach the minimum voltage.
1311 */
79bccc5a 1312 mmc_delay(10);
1da177e4 1313
88ae8b86 1314 host->ios.clock = host->f_init;
8dfd0374 1315
1da177e4 1316 host->ios.power_mode = MMC_POWER_ON;
920e70c5 1317 mmc_set_ios(host);
1da177e4 1318
f9996aee
PO
1319 /*
1320 * This delay must be at least 74 clock sizes, or 1 ms, or the
1321 * time required to reach a stable voltage.
1322 */
79bccc5a 1323 mmc_delay(10);
778e277c
MW
1324
1325 mmc_host_clk_release(host);
1da177e4
LT
1326}
1327
7f7e4129 1328void mmc_power_off(struct mmc_host *host)
1da177e4 1329{
778e277c
MW
1330 mmc_host_clk_hold(host);
1331
1da177e4
LT
1332 host->ios.clock = 0;
1333 host->ios.vdd = 0;
b33d46c3 1334
a80f1627 1335 mmc_poweroff_notify(host);
bec8726a 1336
b33d46c3
UH
1337 /*
1338 * Reset ocr mask to be the highest possible voltage supported for
1339 * this mmc host. This value will be used at next power up.
1340 */
1341 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1342
af517150
DB
1343 if (!mmc_host_is_spi(host)) {
1344 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1345 host->ios.chip_select = MMC_CS_DONTCARE;
1346 }
1da177e4 1347 host->ios.power_mode = MMC_POWER_OFF;
f218278a 1348 host->ios.bus_width = MMC_BUS_WIDTH_1;
cd9277c0 1349 host->ios.timing = MMC_TIMING_LEGACY;
920e70c5 1350 mmc_set_ios(host);
778e277c 1351
041beb1d
DD
1352 /*
1353 * Some configurations, such as the 802.11 SDIO card in the OLPC
1354 * XO-1.5, require a short delay after poweroff before the card
1355 * can be successfully turned on again.
1356 */
1357 mmc_delay(1);
1358
778e277c 1359 mmc_host_clk_release(host);
1da177e4
LT
1360}
1361
39361851
AB
1362/*
1363 * Cleanup when the last reference to the bus operator is dropped.
1364 */
261172fd 1365static void __mmc_release_bus(struct mmc_host *host)
39361851
AB
1366{
1367 BUG_ON(!host);
1368 BUG_ON(host->bus_refs);
1369 BUG_ON(!host->bus_dead);
1370
1371 host->bus_ops = NULL;
1372}
1373
1374/*
1375 * Increase reference count of bus operator
1376 */
1377static inline void mmc_bus_get(struct mmc_host *host)
1378{
1379 unsigned long flags;
1380
1381 spin_lock_irqsave(&host->lock, flags);
1382 host->bus_refs++;
1383 spin_unlock_irqrestore(&host->lock, flags);
1384}
1385
1386/*
1387 * Decrease reference count of bus operator and free it if
1388 * it is the last reference.
1389 */
1390static inline void mmc_bus_put(struct mmc_host *host)
1391{
1392 unsigned long flags;
1393
1394 spin_lock_irqsave(&host->lock, flags);
1395 host->bus_refs--;
1396 if ((host->bus_refs == 0) && host->bus_ops)
1397 __mmc_release_bus(host);
1398 spin_unlock_irqrestore(&host->lock, flags);
1399}
1400
1da177e4 1401/*
7ea239d9
PO
1402 * Assign a mmc bus handler to a host. Only one bus handler may control a
1403 * host at any given time.
1da177e4 1404 */
7ea239d9 1405void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 1406{
7ea239d9 1407 unsigned long flags;
e45a1bd2 1408
7ea239d9
PO
1409 BUG_ON(!host);
1410 BUG_ON(!ops);
b855885e 1411
d84075c8 1412 WARN_ON(!host->claimed);
bce40a36 1413
7ea239d9 1414 spin_lock_irqsave(&host->lock, flags);
bce40a36 1415
7ea239d9
PO
1416 BUG_ON(host->bus_ops);
1417 BUG_ON(host->bus_refs);
b57c43ad 1418
7ea239d9
PO
1419 host->bus_ops = ops;
1420 host->bus_refs = 1;
1421 host->bus_dead = 0;
b57c43ad 1422
7ea239d9 1423 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
1424}
1425
7ea239d9 1426/*
7f7e4129 1427 * Remove the current bus handler from a host.
7ea239d9
PO
1428 */
1429void mmc_detach_bus(struct mmc_host *host)
7ccd266e 1430{
7ea239d9 1431 unsigned long flags;
7ccd266e 1432
7ea239d9 1433 BUG_ON(!host);
7ccd266e 1434
d84075c8
PO
1435 WARN_ON(!host->claimed);
1436 WARN_ON(!host->bus_ops);
cd9277c0 1437
7ea239d9 1438 spin_lock_irqsave(&host->lock, flags);
7ccd266e 1439
7ea239d9 1440 host->bus_dead = 1;
7ccd266e 1441
7ea239d9 1442 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 1443
7ea239d9 1444 mmc_bus_put(host);
1da177e4
LT
1445}
1446
1da177e4
LT
1447/**
1448 * mmc_detect_change - process change of state on a MMC socket
1449 * @host: host which changed state.
8dc00335 1450 * @delay: optional delay to wait before detection (jiffies)
1da177e4 1451 *
67a61c48
PO
1452 * MMC drivers should call this when they detect a card has been
1453 * inserted or removed. The MMC layer will confirm that any
1454 * present card is still functional, and initialize any newly
1455 * inserted.
1da177e4 1456 */
8dc00335 1457void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1458{
3b91e550 1459#ifdef CONFIG_MMC_DEBUG
1efd48b3 1460 unsigned long flags;
01f41ec7 1461 spin_lock_irqsave(&host->lock, flags);
d84075c8 1462 WARN_ON(host->removed);
01f41ec7 1463 spin_unlock_irqrestore(&host->lock, flags);
3b91e550 1464#endif
d3049504 1465 host->detect_change = 1;
c4028958 1466 mmc_schedule_delayed_work(&host->detect, delay);
1da177e4
LT
1467}
1468
1469EXPORT_SYMBOL(mmc_detect_change);
1470
dfe86cba
AH
1471void mmc_init_erase(struct mmc_card *card)
1472{
1473 unsigned int sz;
1474
1475 if (is_power_of_2(card->erase_size))
1476 card->erase_shift = ffs(card->erase_size) - 1;
1477 else
1478 card->erase_shift = 0;
1479
1480 /*
1481 * It is possible to erase an arbitrarily large area of an SD or MMC
1482 * card. That is not desirable because it can take a long time
1483 * (minutes) potentially delaying more important I/O, and also the
1484 * timeout calculations become increasingly hugely over-estimated.
1485 * Consequently, 'pref_erase' is defined as a guide to limit erases
1486 * to that size and alignment.
1487 *
1488 * For SD cards that define Allocation Unit size, limit erases to one
1489 * Allocation Unit at a time. For MMC cards that define High Capacity
1490 * Erase Size, whether it is switched on or not, limit to that size.
1491 * Otherwise just have a stab at a good value. For modern cards it
1492 * will end up being 4MiB. Note that if the value is too small, it
1493 * can end up taking longer to erase.
1494 */
1495 if (mmc_card_sd(card) && card->ssr.au) {
1496 card->pref_erase = card->ssr.au;
1497 card->erase_shift = ffs(card->ssr.au) - 1;
1498 } else if (card->ext_csd.hc_erase_size) {
1499 card->pref_erase = card->ext_csd.hc_erase_size;
1500 } else {
1501 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1502 if (sz < 128)
1503 card->pref_erase = 512 * 1024 / 512;
1504 else if (sz < 512)
1505 card->pref_erase = 1024 * 1024 / 512;
1506 else if (sz < 1024)
1507 card->pref_erase = 2 * 1024 * 1024 / 512;
1508 else
1509 card->pref_erase = 4 * 1024 * 1024 / 512;
1510 if (card->pref_erase < card->erase_size)
1511 card->pref_erase = card->erase_size;
1512 else {
1513 sz = card->pref_erase % card->erase_size;
1514 if (sz)
1515 card->pref_erase += card->erase_size - sz;
1516 }
1517 }
1518}
1519
eaa02f75
AW
1520static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1521 unsigned int arg, unsigned int qty)
dfe86cba
AH
1522{
1523 unsigned int erase_timeout;
1524
1525 if (card->ext_csd.erase_group_def & 1) {
1526 /* High Capacity Erase Group Size uses HC timeouts */
1527 if (arg == MMC_TRIM_ARG)
1528 erase_timeout = card->ext_csd.trim_timeout;
1529 else
1530 erase_timeout = card->ext_csd.hc_erase_timeout;
1531 } else {
1532 /* CSD Erase Group Size uses write timeout */
1533 unsigned int mult = (10 << card->csd.r2w_factor);
1534 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1535 unsigned int timeout_us;
1536
1537 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1538 if (card->csd.tacc_ns < 1000000)
1539 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1540 else
1541 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1542
1543 /*
1544 * ios.clock is only a target. The real clock rate might be
1545 * less but not that much less, so fudge it by multiplying by 2.
1546 */
1547 timeout_clks <<= 1;
1548 timeout_us += (timeout_clks * 1000) /
4cf8c6dd 1549 (mmc_host_clk_rate(card->host) / 1000);
dfe86cba
AH
1550
1551 erase_timeout = timeout_us / 1000;
1552
1553 /*
1554 * Theoretically, the calculation could underflow so round up
1555 * to 1ms in that case.
1556 */
1557 if (!erase_timeout)
1558 erase_timeout = 1;
1559 }
1560
1561 /* Multiplier for secure operations */
1562 if (arg & MMC_SECURE_ARGS) {
1563 if (arg == MMC_SECURE_ERASE_ARG)
1564 erase_timeout *= card->ext_csd.sec_erase_mult;
1565 else
1566 erase_timeout *= card->ext_csd.sec_trim_mult;
1567 }
1568
1569 erase_timeout *= qty;
1570
1571 /*
1572 * Ensure at least a 1 second timeout for SPI as per
1573 * 'mmc_set_data_timeout()'
1574 */
1575 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1576 erase_timeout = 1000;
1577
eaa02f75 1578 return erase_timeout;
dfe86cba
AH
1579}
1580
eaa02f75
AW
1581static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1582 unsigned int arg,
1583 unsigned int qty)
dfe86cba 1584{
eaa02f75
AW
1585 unsigned int erase_timeout;
1586
dfe86cba
AH
1587 if (card->ssr.erase_timeout) {
1588 /* Erase timeout specified in SD Status Register (SSR) */
eaa02f75
AW
1589 erase_timeout = card->ssr.erase_timeout * qty +
1590 card->ssr.erase_offset;
dfe86cba
AH
1591 } else {
1592 /*
1593 * Erase timeout not specified in SD Status Register (SSR) so
1594 * use 250ms per write block.
1595 */
eaa02f75 1596 erase_timeout = 250 * qty;
dfe86cba
AH
1597 }
1598
1599 /* Must not be less than 1 second */
eaa02f75
AW
1600 if (erase_timeout < 1000)
1601 erase_timeout = 1000;
1602
1603 return erase_timeout;
dfe86cba
AH
1604}
1605
eaa02f75
AW
1606static unsigned int mmc_erase_timeout(struct mmc_card *card,
1607 unsigned int arg,
1608 unsigned int qty)
dfe86cba
AH
1609{
1610 if (mmc_card_sd(card))
eaa02f75 1611 return mmc_sd_erase_timeout(card, arg, qty);
dfe86cba 1612 else
eaa02f75 1613 return mmc_mmc_erase_timeout(card, arg, qty);
dfe86cba
AH
1614}
1615
1616static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1617 unsigned int to, unsigned int arg)
1618{
1278dba1 1619 struct mmc_command cmd = {0};
dfe86cba
AH
1620 unsigned int qty = 0;
1621 int err;
1622
1623 /*
1624 * qty is used to calculate the erase timeout which depends on how many
1625 * erase groups (or allocation units in SD terminology) are affected.
1626 * We count erasing part of an erase group as one erase group.
1627 * For SD, the allocation units are always a power of 2. For MMC, the
1628 * erase group size is almost certainly also power of 2, but it does not
1629 * seem to insist on that in the JEDEC standard, so we fall back to
1630 * division in that case. SD may not specify an allocation unit size,
1631 * in which case the timeout is based on the number of write blocks.
1632 *
1633 * Note that the timeout for secure trim 2 will only be correct if the
1634 * number of erase groups specified is the same as the total of all
1635 * preceding secure trim 1 commands. Since the power may have been
1636 * lost since the secure trim 1 commands occurred, it is generally
1637 * impossible to calculate the secure trim 2 timeout correctly.
1638 */
1639 if (card->erase_shift)
1640 qty += ((to >> card->erase_shift) -
1641 (from >> card->erase_shift)) + 1;
1642 else if (mmc_card_sd(card))
1643 qty += to - from + 1;
1644 else
1645 qty += ((to / card->erase_size) -
1646 (from / card->erase_size)) + 1;
1647
1648 if (!mmc_card_blockaddr(card)) {
1649 from <<= 9;
1650 to <<= 9;
1651 }
1652
dfe86cba
AH
1653 if (mmc_card_sd(card))
1654 cmd.opcode = SD_ERASE_WR_BLK_START;
1655 else
1656 cmd.opcode = MMC_ERASE_GROUP_START;
1657 cmd.arg = from;
1658 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1659 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1660 if (err) {
a3c76eb9 1661 pr_err("mmc_erase: group start error %d, "
dfe86cba 1662 "status %#x\n", err, cmd.resp[0]);
67716327 1663 err = -EIO;
dfe86cba
AH
1664 goto out;
1665 }
1666
1667 memset(&cmd, 0, sizeof(struct mmc_command));
1668 if (mmc_card_sd(card))
1669 cmd.opcode = SD_ERASE_WR_BLK_END;
1670 else
1671 cmd.opcode = MMC_ERASE_GROUP_END;
1672 cmd.arg = to;
1673 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1674 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1675 if (err) {
a3c76eb9 1676 pr_err("mmc_erase: group end error %d, status %#x\n",
dfe86cba 1677 err, cmd.resp[0]);
67716327 1678 err = -EIO;
dfe86cba
AH
1679 goto out;
1680 }
1681
1682 memset(&cmd, 0, sizeof(struct mmc_command));
1683 cmd.opcode = MMC_ERASE;
1684 cmd.arg = arg;
1685 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
eaa02f75 1686 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
dfe86cba
AH
1687 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1688 if (err) {
a3c76eb9 1689 pr_err("mmc_erase: erase error %d, status %#x\n",
dfe86cba
AH
1690 err, cmd.resp[0]);
1691 err = -EIO;
1692 goto out;
1693 }
1694
1695 if (mmc_host_is_spi(card->host))
1696 goto out;
1697
1698 do {
1699 memset(&cmd, 0, sizeof(struct mmc_command));
1700 cmd.opcode = MMC_SEND_STATUS;
1701 cmd.arg = card->rca << 16;
1702 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1703 /* Do not retry else we can't see errors */
1704 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1705 if (err || (cmd.resp[0] & 0xFDF92000)) {
a3c76eb9 1706 pr_err("error %d requesting status %#x\n",
dfe86cba
AH
1707 err, cmd.resp[0]);
1708 err = -EIO;
1709 goto out;
1710 }
1711 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
7435bb79 1712 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
dfe86cba
AH
1713out:
1714 return err;
1715}
1716
1717/**
1718 * mmc_erase - erase sectors.
1719 * @card: card to erase
1720 * @from: first sector to erase
1721 * @nr: number of sectors to erase
1722 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1723 *
1724 * Caller must claim host before calling this function.
1725 */
1726int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1727 unsigned int arg)
1728{
1729 unsigned int rem, to = from + nr;
1730
1731 if (!(card->host->caps & MMC_CAP_ERASE) ||
1732 !(card->csd.cmdclass & CCC_ERASE))
1733 return -EOPNOTSUPP;
1734
1735 if (!card->erase_size)
1736 return -EOPNOTSUPP;
1737
1738 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1739 return -EOPNOTSUPP;
1740
1741 if ((arg & MMC_SECURE_ARGS) &&
1742 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1743 return -EOPNOTSUPP;
1744
1745 if ((arg & MMC_TRIM_ARGS) &&
1746 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1747 return -EOPNOTSUPP;
1748
1749 if (arg == MMC_SECURE_ERASE_ARG) {
1750 if (from % card->erase_size || nr % card->erase_size)
1751 return -EINVAL;
1752 }
1753
1754 if (arg == MMC_ERASE_ARG) {
1755 rem = from % card->erase_size;
1756 if (rem) {
1757 rem = card->erase_size - rem;
1758 from += rem;
1759 if (nr > rem)
1760 nr -= rem;
1761 else
1762 return 0;
1763 }
1764 rem = nr % card->erase_size;
1765 if (rem)
1766 nr -= rem;
1767 }
1768
1769 if (nr == 0)
1770 return 0;
1771
1772 to = from + nr;
1773
1774 if (to <= from)
1775 return -EINVAL;
1776
1777 /* 'from' and 'to' are inclusive */
1778 to -= 1;
1779
1780 return mmc_do_erase(card, from, to, arg);
1781}
1782EXPORT_SYMBOL(mmc_erase);
1783
1784int mmc_can_erase(struct mmc_card *card)
1785{
1786 if ((card->host->caps & MMC_CAP_ERASE) &&
1787 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1788 return 1;
1789 return 0;
1790}
1791EXPORT_SYMBOL(mmc_can_erase);
1792
1793int mmc_can_trim(struct mmc_card *card)
1794{
1795 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1796 return 1;
b3bf9153
KP
1797 if (mmc_can_discard(card))
1798 return 1;
dfe86cba
AH
1799 return 0;
1800}
1801EXPORT_SYMBOL(mmc_can_trim);
1802
b3bf9153
KP
1803int mmc_can_discard(struct mmc_card *card)
1804{
1805 /*
1806 * As there's no way to detect the discard support bit at v4.5
1807 * use the s/w feature support filed.
1808 */
1809 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
1810 return 1;
1811 return 0;
1812}
1813EXPORT_SYMBOL(mmc_can_discard);
1814
d9ddd629
KP
1815int mmc_can_sanitize(struct mmc_card *card)
1816{
1817 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
1818 return 1;
1819 return 0;
1820}
1821EXPORT_SYMBOL(mmc_can_sanitize);
1822
dfe86cba
AH
1823int mmc_can_secure_erase_trim(struct mmc_card *card)
1824{
1825 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1826 return 1;
1827 return 0;
1828}
1829EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1830
1831int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1832 unsigned int nr)
1833{
1834 if (!card->erase_size)
1835 return 0;
1836 if (from % card->erase_size || nr % card->erase_size)
1837 return 0;
1838 return 1;
1839}
1840EXPORT_SYMBOL(mmc_erase_group_aligned);
1da177e4 1841
e056a1b5
AH
1842static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1843 unsigned int arg)
1844{
1845 struct mmc_host *host = card->host;
1846 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1847 unsigned int last_timeout = 0;
1848
1849 if (card->erase_shift)
1850 max_qty = UINT_MAX >> card->erase_shift;
1851 else if (mmc_card_sd(card))
1852 max_qty = UINT_MAX;
1853 else
1854 max_qty = UINT_MAX / card->erase_size;
1855
1856 /* Find the largest qty with an OK timeout */
1857 do {
1858 y = 0;
1859 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1860 timeout = mmc_erase_timeout(card, arg, qty + x);
1861 if (timeout > host->max_discard_to)
1862 break;
1863 if (timeout < last_timeout)
1864 break;
1865 last_timeout = timeout;
1866 y = x;
1867 }
1868 qty += y;
1869 } while (y);
1870
1871 if (!qty)
1872 return 0;
1873
1874 if (qty == 1)
1875 return 1;
1876
1877 /* Convert qty to sectors */
1878 if (card->erase_shift)
1879 max_discard = --qty << card->erase_shift;
1880 else if (mmc_card_sd(card))
1881 max_discard = qty;
1882 else
1883 max_discard = --qty * card->erase_size;
1884
1885 return max_discard;
1886}
1887
1888unsigned int mmc_calc_max_discard(struct mmc_card *card)
1889{
1890 struct mmc_host *host = card->host;
1891 unsigned int max_discard, max_trim;
1892
1893 if (!host->max_discard_to)
1894 return UINT_MAX;
1895
1896 /*
1897 * Without erase_group_def set, MMC erase timeout depends on clock
1898 * frequence which can change. In that case, the best choice is
1899 * just the preferred erase size.
1900 */
1901 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1902 return card->pref_erase;
1903
1904 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1905 if (mmc_can_trim(card)) {
1906 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1907 if (max_trim < max_discard)
1908 max_discard = max_trim;
1909 } else if (max_discard < card->erase_size) {
1910 max_discard = 0;
1911 }
1912 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1913 mmc_hostname(host), max_discard, host->max_discard_to);
1914 return max_discard;
1915}
1916EXPORT_SYMBOL(mmc_calc_max_discard);
1917
0f8d8ea6
AH
1918int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1919{
1278dba1 1920 struct mmc_command cmd = {0};
0f8d8ea6
AH
1921
1922 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1923 return 0;
1924
0f8d8ea6
AH
1925 cmd.opcode = MMC_SET_BLOCKLEN;
1926 cmd.arg = blocklen;
1927 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1928 return mmc_wait_for_cmd(card->host, &cmd, 5);
1929}
1930EXPORT_SYMBOL(mmc_set_blocklen);
1931
b2499518
AH
1932static void mmc_hw_reset_for_init(struct mmc_host *host)
1933{
1934 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1935 return;
1936 mmc_host_clk_hold(host);
1937 host->ops->hw_reset(host);
1938 mmc_host_clk_release(host);
1939}
1940
1941int mmc_can_reset(struct mmc_card *card)
1942{
1943 u8 rst_n_function;
1944
1945 if (!mmc_card_mmc(card))
1946 return 0;
1947 rst_n_function = card->ext_csd.rst_n_function;
1948 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1949 return 0;
1950 return 1;
1951}
1952EXPORT_SYMBOL(mmc_can_reset);
1953
1954static int mmc_do_hw_reset(struct mmc_host *host, int check)
1955{
1956 struct mmc_card *card = host->card;
1957
1958 if (!host->bus_ops->power_restore)
1959 return -EOPNOTSUPP;
1960
1961 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1962 return -EOPNOTSUPP;
1963
1964 if (!card)
1965 return -EINVAL;
1966
1967 if (!mmc_can_reset(card))
1968 return -EOPNOTSUPP;
1969
1970 mmc_host_clk_hold(host);
1971 mmc_set_clock(host, host->f_init);
1972
1973 host->ops->hw_reset(host);
1974
1975 /* If the reset has happened, then a status command will fail */
1976 if (check) {
1977 struct mmc_command cmd = {0};
1978 int err;
1979
1980 cmd.opcode = MMC_SEND_STATUS;
1981 if (!mmc_host_is_spi(card->host))
1982 cmd.arg = card->rca << 16;
1983 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1984 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1985 if (!err) {
1986 mmc_host_clk_release(host);
1987 return -ENOSYS;
1988 }
1989 }
1990
1991 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
1992 if (mmc_host_is_spi(host)) {
1993 host->ios.chip_select = MMC_CS_HIGH;
1994 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1995 } else {
1996 host->ios.chip_select = MMC_CS_DONTCARE;
1997 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1998 }
1999 host->ios.bus_width = MMC_BUS_WIDTH_1;
2000 host->ios.timing = MMC_TIMING_LEGACY;
2001 mmc_set_ios(host);
2002
2003 mmc_host_clk_release(host);
2004
2005 return host->bus_ops->power_restore(host);
2006}
2007
2008int mmc_hw_reset(struct mmc_host *host)
2009{
2010 return mmc_do_hw_reset(host, 0);
2011}
2012EXPORT_SYMBOL(mmc_hw_reset);
2013
2014int mmc_hw_reset_check(struct mmc_host *host)
2015{
2016 return mmc_do_hw_reset(host, 1);
2017}
2018EXPORT_SYMBOL(mmc_hw_reset_check);
2019
807e8e40
AR
2020static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2021{
2022 host->f_init = freq;
2023
2024#ifdef CONFIG_MMC_DEBUG
2025 pr_info("%s: %s: trying to init card at %u Hz\n",
2026 mmc_hostname(host), __func__, host->f_init);
2027#endif
2028 mmc_power_up(host);
2f94e55a 2029
b2499518
AH
2030 /*
2031 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2032 * do a hardware reset if possible.
2033 */
2034 mmc_hw_reset_for_init(host);
2035
2f94e55a
PR
2036 /*
2037 * sdio_reset sends CMD52 to reset card. Since we do not know
2038 * if the card is being re-initialized, just send it. CMD52
2039 * should be ignored by SD/eMMC cards.
2040 */
807e8e40
AR
2041 sdio_reset(host);
2042 mmc_go_idle(host);
2043
2044 mmc_send_if_cond(host, host->ocr_avail);
2045
2046 /* Order's important: probe SDIO, then SD, then MMC */
2047 if (!mmc_attach_sdio(host))
2048 return 0;
2049 if (!mmc_attach_sd(host))
2050 return 0;
2051 if (!mmc_attach_mmc(host))
2052 return 0;
2053
2054 mmc_power_off(host);
2055 return -EIO;
2056}
2057
d3049504
AH
2058int _mmc_detect_card_removed(struct mmc_host *host)
2059{
2060 int ret;
2061
2062 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2063 return 0;
2064
2065 if (!host->card || mmc_card_removed(host->card))
2066 return 1;
2067
2068 ret = host->bus_ops->alive(host);
2069 if (ret) {
2070 mmc_card_set_removed(host->card);
2071 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2072 }
2073
2074 return ret;
2075}
2076
2077int mmc_detect_card_removed(struct mmc_host *host)
2078{
2079 struct mmc_card *card = host->card;
2080
2081 WARN_ON(!host->claimed);
2082 /*
2083 * The card will be considered unchanged unless we have been asked to
2084 * detect a change or host requires polling to provide card detection.
2085 */
2086 if (card && !host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
2087 return mmc_card_removed(card);
2088
2089 host->detect_change = 0;
2090
2091 return _mmc_detect_card_removed(host);
2092}
2093EXPORT_SYMBOL(mmc_detect_card_removed);
2094
b93931a6 2095void mmc_rescan(struct work_struct *work)
1da177e4 2096{
807e8e40 2097 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
c4028958
DH
2098 struct mmc_host *host =
2099 container_of(work, struct mmc_host, detect.work);
88ae8b86 2100 int i;
4c2ef25f 2101
807e8e40 2102 if (host->rescan_disable)
4c2ef25f 2103 return;
1da177e4 2104
7ea239d9 2105 mmc_bus_get(host);
b855885e 2106
30201e7f
OBC
2107 /*
2108 * if there is a _removable_ card registered, check whether it is
2109 * still present
2110 */
2111 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
bad3baba 2112 && !(host->caps & MMC_CAP_NONREMOVABLE))
94d89efb
JS
2113 host->bus_ops->detect(host);
2114
d3049504
AH
2115 host->detect_change = 0;
2116
c5841798
CB
2117 /*
2118 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2119 * the card is no longer present.
2120 */
94d89efb 2121 mmc_bus_put(host);
94d89efb
JS
2122 mmc_bus_get(host);
2123
2124 /* if there still is a card present, stop here */
2125 if (host->bus_ops != NULL) {
7ea239d9 2126 mmc_bus_put(host);
94d89efb
JS
2127 goto out;
2128 }
1da177e4 2129
94d89efb
JS
2130 /*
2131 * Only we can add a new handler, so it's safe to
2132 * release the lock here.
2133 */
2134 mmc_bus_put(host);
1da177e4 2135
94d89efb
JS
2136 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
2137 goto out;
1da177e4 2138
807e8e40 2139 mmc_claim_host(host);
88ae8b86 2140 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
807e8e40
AR
2141 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2142 break;
06b2233a 2143 if (freqs[i] <= host->f_min)
807e8e40 2144 break;
88ae8b86 2145 }
807e8e40
AR
2146 mmc_release_host(host);
2147
2148 out:
28f52482
AV
2149 if (host->caps & MMC_CAP_NEEDS_POLL)
2150 mmc_schedule_delayed_work(&host->detect, HZ);
1da177e4
LT
2151}
2152
b93931a6 2153void mmc_start_host(struct mmc_host *host)
1da177e4 2154{
b93931a6
PO
2155 mmc_power_off(host);
2156 mmc_detect_change(host, 0);
1da177e4
LT
2157}
2158
b93931a6 2159void mmc_stop_host(struct mmc_host *host)
1da177e4 2160{
3b91e550 2161#ifdef CONFIG_MMC_DEBUG
1efd48b3
PO
2162 unsigned long flags;
2163 spin_lock_irqsave(&host->lock, flags);
3b91e550 2164 host->removed = 1;
1efd48b3 2165 spin_unlock_irqrestore(&host->lock, flags);
3b91e550
PO
2166#endif
2167
8ea926b2
AH
2168 if (host->caps & MMC_CAP_DISABLE)
2169 cancel_delayed_work(&host->disable);
d9bcbf34 2170 cancel_delayed_work_sync(&host->detect);
3b91e550
PO
2171 mmc_flush_scheduled_work();
2172
da68c4eb
NP
2173 /* clear pm flags now and let card drivers set them as needed */
2174 host->pm_flags = 0;
2175
7ea239d9
PO
2176 mmc_bus_get(host);
2177 if (host->bus_ops && !host->bus_dead) {
2178 if (host->bus_ops->remove)
2179 host->bus_ops->remove(host);
2180
2181 mmc_claim_host(host);
2182 mmc_detach_bus(host);
7f7e4129 2183 mmc_power_off(host);
7ea239d9 2184 mmc_release_host(host);
53509f0f
DK
2185 mmc_bus_put(host);
2186 return;
1da177e4 2187 }
7ea239d9
PO
2188 mmc_bus_put(host);
2189
2190 BUG_ON(host->card);
1da177e4
LT
2191
2192 mmc_power_off(host);
2193}
2194
12ae637f 2195int mmc_power_save_host(struct mmc_host *host)
eae1aeee 2196{
12ae637f
OBC
2197 int ret = 0;
2198
bb9cab94
DD
2199#ifdef CONFIG_MMC_DEBUG
2200 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2201#endif
2202
eae1aeee
AH
2203 mmc_bus_get(host);
2204
2205 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2206 mmc_bus_put(host);
12ae637f 2207 return -EINVAL;
eae1aeee
AH
2208 }
2209
2210 if (host->bus_ops->power_save)
12ae637f 2211 ret = host->bus_ops->power_save(host);
eae1aeee
AH
2212
2213 mmc_bus_put(host);
2214
2215 mmc_power_off(host);
12ae637f
OBC
2216
2217 return ret;
eae1aeee
AH
2218}
2219EXPORT_SYMBOL(mmc_power_save_host);
2220
12ae637f 2221int mmc_power_restore_host(struct mmc_host *host)
eae1aeee 2222{
12ae637f
OBC
2223 int ret;
2224
bb9cab94
DD
2225#ifdef CONFIG_MMC_DEBUG
2226 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2227#endif
2228
eae1aeee
AH
2229 mmc_bus_get(host);
2230
2231 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2232 mmc_bus_put(host);
12ae637f 2233 return -EINVAL;
eae1aeee
AH
2234 }
2235
2236 mmc_power_up(host);
12ae637f 2237 ret = host->bus_ops->power_restore(host);
eae1aeee
AH
2238
2239 mmc_bus_put(host);
12ae637f
OBC
2240
2241 return ret;
eae1aeee
AH
2242}
2243EXPORT_SYMBOL(mmc_power_restore_host);
2244
b1ebe384
JL
2245int mmc_card_awake(struct mmc_host *host)
2246{
2247 int err = -ENOSYS;
2248
2249 mmc_bus_get(host);
2250
2251 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2252 err = host->bus_ops->awake(host);
2253
2254 mmc_bus_put(host);
2255
2256 return err;
2257}
2258EXPORT_SYMBOL(mmc_card_awake);
2259
2260int mmc_card_sleep(struct mmc_host *host)
2261{
2262 int err = -ENOSYS;
2263
2264 mmc_bus_get(host);
2265
c99872a1 2266 if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
b1ebe384
JL
2267 err = host->bus_ops->sleep(host);
2268
2269 mmc_bus_put(host);
2270
2271 return err;
2272}
2273EXPORT_SYMBOL(mmc_card_sleep);
2274
2275int mmc_card_can_sleep(struct mmc_host *host)
2276{
2277 struct mmc_card *card = host->card;
2278
2279 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2280 return 1;
2281 return 0;
2282}
2283EXPORT_SYMBOL(mmc_card_can_sleep);
2284
881d1c25
SJ
2285/*
2286 * Flush the cache to the non-volatile storage.
2287 */
2288int mmc_flush_cache(struct mmc_card *card)
2289{
2290 struct mmc_host *host = card->host;
2291 int err = 0;
2292
2293 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2294 return err;
2295
2296 if (mmc_card_mmc(card) &&
2297 (card->ext_csd.cache_size > 0) &&
2298 (card->ext_csd.cache_ctrl & 1)) {
2299 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2300 EXT_CSD_FLUSH_CACHE, 1, 0);
2301 if (err)
2302 pr_err("%s: cache flush error %d\n",
2303 mmc_hostname(card->host), err);
2304 }
2305
2306 return err;
2307}
2308EXPORT_SYMBOL(mmc_flush_cache);
2309
2310/*
2311 * Turn the cache ON/OFF.
2312 * Turning the cache OFF shall trigger flushing of the data
2313 * to the non-volatile storage.
2314 */
2315int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2316{
2317 struct mmc_card *card = host->card;
2318 int err = 0;
2319
2320 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2321 mmc_card_is_removable(host))
2322 return err;
2323
2324 if (card && mmc_card_mmc(card) &&
2325 (card->ext_csd.cache_size > 0)) {
2326 enable = !!enable;
2327
2328 if (card->ext_csd.cache_ctrl ^ enable)
2329 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2330 EXT_CSD_CACHE_CTRL, enable, 0);
2331 if (err)
2332 pr_err("%s: cache %s error %d\n",
2333 mmc_hostname(card->host),
2334 enable ? "on" : "off",
2335 err);
2336 else
2337 card->ext_csd.cache_ctrl = enable;
2338 }
2339
2340 return err;
2341}
2342EXPORT_SYMBOL(mmc_cache_ctrl);
2343
1da177e4
LT
2344#ifdef CONFIG_PM
2345
2346/**
2347 * mmc_suspend_host - suspend a host
2348 * @host: mmc host
1da177e4 2349 */
1a13f8fa 2350int mmc_suspend_host(struct mmc_host *host)
1da177e4 2351{
95cdfb72
NP
2352 int err = 0;
2353
8ea926b2
AH
2354 if (host->caps & MMC_CAP_DISABLE)
2355 cancel_delayed_work(&host->disable);
7de427d0 2356 cancel_delayed_work(&host->detect);
b5af25be 2357 mmc_flush_scheduled_work();
881d1c25
SJ
2358 err = mmc_cache_ctrl(host, 0);
2359 if (err)
2360 goto out;
b5af25be 2361
7ea239d9
PO
2362 mmc_bus_get(host);
2363 if (host->bus_ops && !host->bus_dead) {
b6ad726e
UH
2364
2365 /*
2366 * A long response time is not acceptable for device drivers
2367 * when doing suspend. Prevent mmc_claim_host in the suspend
2368 * sequence, to potentially wait "forever" by trying to
2369 * pre-claim the host.
2370 */
2371 if (mmc_try_claim_host(host)) {
a80f1627
G
2372 if (host->bus_ops->suspend) {
2373 /*
2374 * For eMMC 4.5 device send notify command
2375 * before sleep, because in sleep state eMMC 4.5
2376 * devices respond to only RESET and AWAKE cmd
2377 */
2378 mmc_poweroff_notify(host);
b6ad726e 2379 err = host->bus_ops->suspend(host);
a80f1627 2380 }
49df7807
SRT
2381 mmc_do_release_host(host);
2382
b6ad726e
UH
2383 if (err == -ENOSYS || !host->bus_ops->resume) {
2384 /*
2385 * We simply "remove" the card in this case.
2386 * It will be redetected on resume.
2387 */
2388 if (host->bus_ops->remove)
2389 host->bus_ops->remove(host);
2390 mmc_claim_host(host);
2391 mmc_detach_bus(host);
2392 mmc_power_off(host);
2393 mmc_release_host(host);
2394 host->pm_flags = 0;
2395 err = 0;
2396 }
b6ad726e
UH
2397 } else {
2398 err = -EBUSY;
1c8cf9c9 2399 }
b5af25be 2400 }
7ea239d9
PO
2401 mmc_bus_put(host);
2402
a5e9425d 2403 if (!err && !mmc_card_keep_power(host))
95cdfb72 2404 mmc_power_off(host);
1da177e4 2405
881d1c25 2406out:
95cdfb72 2407 return err;
1da177e4
LT
2408}
2409
2410EXPORT_SYMBOL(mmc_suspend_host);
2411
2412/**
2413 * mmc_resume_host - resume a previously suspended host
2414 * @host: mmc host
2415 */
2416int mmc_resume_host(struct mmc_host *host)
2417{
95cdfb72
NP
2418 int err = 0;
2419
6abaa0c9
PO
2420 mmc_bus_get(host);
2421 if (host->bus_ops && !host->bus_dead) {
a5e9425d 2422 if (!mmc_card_keep_power(host)) {
da68c4eb
NP
2423 mmc_power_up(host);
2424 mmc_select_voltage(host, host->ocr);
e594573d
OBC
2425 /*
2426 * Tell runtime PM core we just powered up the card,
2427 * since it still believes the card is powered off.
2428 * Note that currently runtime PM is only enabled
2429 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2430 */
2431 if (mmc_card_sdio(host->card) &&
2432 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2433 pm_runtime_disable(&host->card->dev);
2434 pm_runtime_set_active(&host->card->dev);
2435 pm_runtime_enable(&host->card->dev);
2436 }
da68c4eb 2437 }
6abaa0c9 2438 BUG_ON(!host->bus_ops->resume);
95cdfb72
NP
2439 err = host->bus_ops->resume(host);
2440 if (err) {
a3c76eb9 2441 pr_warning("%s: error %d during resume "
95cdfb72
NP
2442 "(card was removed?)\n",
2443 mmc_hostname(host), err);
95cdfb72
NP
2444 err = 0;
2445 }
6abaa0c9 2446 }
a8e6df73 2447 host->pm_flags &= ~MMC_PM_KEEP_POWER;
6abaa0c9
PO
2448 mmc_bus_put(host);
2449
95cdfb72 2450 return err;
1da177e4 2451}
1da177e4
LT
2452EXPORT_SYMBOL(mmc_resume_host);
2453
4c2ef25f
ML
2454/* Do the card removal on suspend if card is assumed removeable
2455 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2456 to sync the card.
2457*/
2458int mmc_pm_notify(struct notifier_block *notify_block,
2459 unsigned long mode, void *unused)
2460{
2461 struct mmc_host *host = container_of(
2462 notify_block, struct mmc_host, pm_notify);
2463 unsigned long flags;
2464
2465
2466 switch (mode) {
2467 case PM_HIBERNATION_PREPARE:
2468 case PM_SUSPEND_PREPARE:
2469
2470 spin_lock_irqsave(&host->lock, flags);
2471 host->rescan_disable = 1;
bec8726a 2472 host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
4c2ef25f
ML
2473 spin_unlock_irqrestore(&host->lock, flags);
2474 cancel_delayed_work_sync(&host->detect);
2475
2476 if (!host->bus_ops || host->bus_ops->suspend)
2477 break;
2478
2479 mmc_claim_host(host);
2480
2481 if (host->bus_ops->remove)
2482 host->bus_ops->remove(host);
2483
2484 mmc_detach_bus(host);
7f7e4129 2485 mmc_power_off(host);
4c2ef25f
ML
2486 mmc_release_host(host);
2487 host->pm_flags = 0;
2488 break;
2489
2490 case PM_POST_SUSPEND:
2491 case PM_POST_HIBERNATION:
274476f8 2492 case PM_POST_RESTORE:
4c2ef25f
ML
2493
2494 spin_lock_irqsave(&host->lock, flags);
2495 host->rescan_disable = 0;
bec8726a 2496 host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
4c2ef25f
ML
2497 spin_unlock_irqrestore(&host->lock, flags);
2498 mmc_detect_change(host, 0);
2499
2500 }
2501
2502 return 0;
2503}
1da177e4
LT
2504#endif
2505
ffce2e7e
PO
2506static int __init mmc_init(void)
2507{
2508 int ret;
2509
0d9ee5b2 2510 workqueue = alloc_ordered_workqueue("kmmcd", 0);
ffce2e7e
PO
2511 if (!workqueue)
2512 return -ENOMEM;
2513
2514 ret = mmc_register_bus();
e29a7d73
PO
2515 if (ret)
2516 goto destroy_workqueue;
2517
2518 ret = mmc_register_host_class();
2519 if (ret)
2520 goto unregister_bus;
2521
2522 ret = sdio_register_bus();
2523 if (ret)
2524 goto unregister_host_class;
2525
2526 return 0;
2527
2528unregister_host_class:
2529 mmc_unregister_host_class();
2530unregister_bus:
2531 mmc_unregister_bus();
2532destroy_workqueue:
2533 destroy_workqueue(workqueue);
2534
ffce2e7e
PO
2535 return ret;
2536}
2537
2538static void __exit mmc_exit(void)
2539{
e29a7d73 2540 sdio_unregister_bus();
ffce2e7e
PO
2541 mmc_unregister_host_class();
2542 mmc_unregister_bus();
2543 destroy_workqueue(workqueue);
2544}
2545
26074962 2546subsys_initcall(mmc_init);
ffce2e7e
PO
2547module_exit(mmc_exit);
2548
1da177e4 2549MODULE_LICENSE("GPL");