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