mmc: sdhci-acpi: Add support for ACPI HID of AMD Controller with HS400
[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>
bbd43682 26#include <linux/pm_wakeup.h>
35eb6db1 27#include <linux/suspend.h>
1b676f70
PF
28#include <linux/fault-inject.h>
29#include <linux/random.h>
950d56ac 30#include <linux/slab.h>
6e9e318b 31#include <linux/of.h>
1da177e4
LT
32
33#include <linux/mmc/card.h>
34#include <linux/mmc/host.h>
da7fbe58
PO
35#include <linux/mmc/mmc.h>
36#include <linux/mmc/sd.h>
740a221e 37#include <linux/mmc/slot-gpio.h>
1da177e4 38
7962fc37
BW
39#define CREATE_TRACE_POINTS
40#include <trace/events/mmc.h>
41
aaac1b47 42#include "core.h"
4facdde1 43#include "card.h"
ffce2e7e
PO
44#include "bus.h"
45#include "host.h"
e29a7d73 46#include "sdio_bus.h"
3aa8793f 47#include "pwrseq.h"
da7fbe58
PO
48
49#include "mmc_ops.h"
50#include "sd_ops.h"
5c4e6f13 51#include "sdio_ops.h"
1da177e4 52
8fee476b
TR
53/* If the device is not responding */
54#define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
55
12182aff
UH
56/* The max erase timeout, used when host->max_busy_timeout isn't specified */
57#define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
58
fa550189 59static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
ffce2e7e 60
af517150
DB
61/*
62 * Enabling software CRCs on the data blocks can be a significant (30%)
63 * performance cost, and for other reasons may not always be desired.
64 * So we allow it it to be disabled.
65 */
90ab5ee9 66bool use_spi_crc = 1;
af517150
DB
67module_param(use_spi_crc, bool, 0);
68
c3d53d0d
AH
69#ifdef CONFIG_MMC_MQ_DEFAULT
70bool mmc_use_blk_mq = true;
71#else
72bool mmc_use_blk_mq = false;
73#endif
74module_param_named(use_blk_mq, mmc_use_blk_mq, bool, S_IWUSR | S_IRUGO);
75
ffce2e7e
PO
76static int mmc_schedule_delayed_work(struct delayed_work *work,
77 unsigned long delay)
78{
520bd7a8
UH
79 /*
80 * We use the system_freezable_wq, because of two reasons.
81 * First, it allows several works (not the same work item) to be
82 * executed simultaneously. Second, the queue becomes frozen when
83 * userspace becomes frozen during system PM.
84 */
85 return queue_delayed_work(system_freezable_wq, work, delay);
ffce2e7e
PO
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
2e744fcb
AM
112 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
1b676f70
PF
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
5163af5a
AH
125static inline void mmc_complete_cmd(struct mmc_request *mrq)
126{
127 if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion))
128 complete_all(&mrq->cmd_completion);
129}
130
131void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq)
132{
133 if (!mrq->cap_cmd_during_tfr)
134 return;
135
136 mmc_complete_cmd(mrq);
137
138 pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n",
139 mmc_hostname(host), mrq->cmd->opcode);
140}
141EXPORT_SYMBOL(mmc_command_done);
142
1da177e4 143/**
fe10c6ab
RK
144 * mmc_request_done - finish processing an MMC request
145 * @host: MMC host which completed request
146 * @mrq: MMC request which request
1da177e4
LT
147 *
148 * MMC drivers should call this function when they have completed
fe10c6ab 149 * their processing of a request.
1da177e4
LT
150 */
151void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
152{
153 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
154 int err = cmd->error;
155
bd11e8bd 156 /* Flag re-tuning needed on CRC errors */
031277d4
CJ
157 if ((cmd->opcode != MMC_SEND_TUNING_BLOCK &&
158 cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) &&
159 (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
bd11e8bd 160 (mrq->data && mrq->data->error == -EILSEQ) ||
031277d4 161 (mrq->stop && mrq->stop->error == -EILSEQ)))
bd11e8bd
AH
162 mmc_retune_needed(host);
163
af517150
DB
164 if (err && cmd->retries && mmc_host_is_spi(host)) {
165 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
166 cmd->retries = 0;
167 }
168
5163af5a
AH
169 if (host->ongoing_mrq == mrq)
170 host->ongoing_mrq = NULL;
171
172 mmc_complete_cmd(mrq);
173
7962fc37
BW
174 trace_mmc_request_done(host, mrq);
175
67b8360a
LW
176 /*
177 * We list various conditions for the command to be considered
178 * properly done:
179 *
180 * - There was no error, OK fine then
181 * - We are not doing some kind of retry
182 * - The card was removed (...so just complete everything no matter
183 * if there are errors or retries)
184 */
185 if (!err || !cmd->retries || mmc_card_removed(host->card)) {
1b676f70
PF
186 mmc_should_fail_request(host, mrq);
187
5163af5a
AH
188 if (!host->ongoing_mrq)
189 led_trigger_event(host->led, LED_OFF);
af8350c7 190
fc75b708
AG
191 if (mrq->sbc) {
192 pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
193 mmc_hostname(host), mrq->sbc->opcode,
194 mrq->sbc->error,
195 mrq->sbc->resp[0], mrq->sbc->resp[1],
196 mrq->sbc->resp[2], mrq->sbc->resp[3]);
197 }
198
e4d21708
PO
199 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
200 mmc_hostname(host), cmd->opcode, err,
201 cmd->resp[0], cmd->resp[1],
202 cmd->resp[2], cmd->resp[3]);
203
204 if (mrq->data) {
205 pr_debug("%s: %d bytes transferred: %d\n",
206 mmc_hostname(host),
207 mrq->data->bytes_xfered, mrq->data->error);
208 }
209
210 if (mrq->stop) {
211 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
212 mmc_hostname(host), mrq->stop->opcode,
213 mrq->stop->error,
214 mrq->stop->resp[0], mrq->stop->resp[1],
215 mrq->stop->resp[2], mrq->stop->resp[3]);
216 }
1da177e4 217 }
67b8360a
LW
218 /*
219 * Request starter must handle retries - see
220 * mmc_wait_for_req_done().
221 */
222 if (mrq->done)
223 mrq->done(mrq);
1da177e4
LT
224}
225
226EXPORT_SYMBOL(mmc_request_done);
227
90a81489
AH
228static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
229{
230 int err;
231
232 /* Assumes host controller has been runtime resumed by mmc_claim_host */
233 err = mmc_retune(host);
234 if (err) {
235 mrq->cmd->error = err;
236 mmc_request_done(host, mrq);
237 return;
238 }
239
5d3f6ef0
HG
240 /*
241 * For sdio rw commands we must wait for card busy otherwise some
242 * sdio devices won't work properly.
f328c76e 243 * And bypass I/O abort, reset and bus suspend operations.
5d3f6ef0 244 */
f328c76e 245 if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) &&
246 host->ops->card_busy) {
5d3f6ef0
HG
247 int tries = 500; /* Wait aprox 500ms at maximum */
248
249 while (host->ops->card_busy(host) && --tries)
250 mmc_delay(1);
251
252 if (tries == 0) {
253 mrq->cmd->error = -EBUSY;
254 mmc_request_done(host, mrq);
255 return;
256 }
257 }
258
5163af5a
AH
259 if (mrq->cap_cmd_during_tfr) {
260 host->ongoing_mrq = mrq;
261 /*
262 * Retry path could come through here without having waiting on
263 * cmd_completion, so ensure it is reinitialised.
264 */
265 reinit_completion(&mrq->cmd_completion);
266 }
267
7962fc37
BW
268 trace_mmc_request_start(host, mrq);
269
3e207c8c
AH
270 if (host->cqe_on)
271 host->cqe_ops->cqe_off(host);
272
90a81489
AH
273 host->ops->request(host, mrq);
274}
275
72a5af55
AH
276static void mmc_mrq_pr_debug(struct mmc_host *host, struct mmc_request *mrq,
277 bool cqe)
1da177e4 278{
7b2fd4f2
JC
279 if (mrq->sbc) {
280 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
281 mmc_hostname(host), mrq->sbc->opcode,
282 mrq->sbc->arg, mrq->sbc->flags);
283 }
284
4b67e63f 285 if (mrq->cmd) {
72a5af55
AH
286 pr_debug("%s: starting %sCMD%u arg %08x flags %08x\n",
287 mmc_hostname(host), cqe ? "CQE direct " : "",
288 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
289 } else if (cqe) {
290 pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n",
291 mmc_hostname(host), mrq->tag, mrq->data->blk_addr);
4b67e63f 292 }
1da177e4 293
e4d21708
PO
294 if (mrq->data) {
295 pr_debug("%s: blksz %d blocks %d flags %08x "
296 "tsac %d ms nsac %d\n",
297 mmc_hostname(host), mrq->data->blksz,
298 mrq->data->blocks, mrq->data->flags,
ce252edd 299 mrq->data->timeout_ns / 1000000,
e4d21708
PO
300 mrq->data->timeout_clks);
301 }
302
303 if (mrq->stop) {
304 pr_debug("%s: CMD%u arg %08x flags %08x\n",
305 mmc_hostname(host), mrq->stop->opcode,
306 mrq->stop->arg, mrq->stop->flags);
307 }
4b67e63f
AH
308}
309
f34bdd2f 310static int mmc_mrq_prep(struct mmc_host *host, struct mmc_request *mrq)
4b67e63f 311{
b044b1bc 312 unsigned int i, sz = 0;
4b67e63f 313 struct scatterlist *sg;
1da177e4 314
f34bdd2f
AH
315 if (mrq->cmd) {
316 mrq->cmd->error = 0;
317 mrq->cmd->mrq = mrq;
318 mrq->cmd->data = mrq->data;
319 }
cce411e6
AG
320 if (mrq->sbc) {
321 mrq->sbc->error = 0;
322 mrq->sbc->mrq = mrq;
323 }
1da177e4 324 if (mrq->data) {
6ff897ff
SL
325 if (mrq->data->blksz > host->max_blk_size ||
326 mrq->data->blocks > host->max_blk_count ||
327 mrq->data->blocks * mrq->data->blksz > host->max_req_size)
328 return -EINVAL;
b044b1bc 329
a84756c5
PO
330 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
331 sz += sg->length;
6ff897ff
SL
332 if (sz != mrq->data->blocks * mrq->data->blksz)
333 return -EINVAL;
b044b1bc 334
1da177e4
LT
335 mrq->data->error = 0;
336 mrq->data->mrq = mrq;
337 if (mrq->stop) {
338 mrq->data->stop = mrq->stop;
339 mrq->stop->error = 0;
340 mrq->stop->mrq = mrq;
341 }
342 }
f34bdd2f
AH
343
344 return 0;
345}
346
cb39f61e 347int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
f34bdd2f
AH
348{
349 int err;
350
351 mmc_retune_hold(host);
352
353 if (mmc_card_removed(host->card))
354 return -ENOMEDIUM;
355
72a5af55 356 mmc_mrq_pr_debug(host, mrq, false);
f34bdd2f
AH
357
358 WARN_ON(!host->claimed);
359
360 err = mmc_mrq_prep(host, mrq);
361 if (err)
362 return err;
363
66c036e0 364 led_trigger_event(host->led, LED_FULL);
90a81489 365 __mmc_start_request(host, mrq);
f100c1c2
AH
366
367 return 0;
1da177e4 368}
cb39f61e 369EXPORT_SYMBOL(mmc_start_request);
1da177e4 370
2220eedf
KD
371/*
372 * mmc_wait_data_done() - done callback for data request
373 * @mrq: done data request
374 *
375 * Wakes up mmc context, passed as a callback to host controller driver
376 */
377static void mmc_wait_data_done(struct mmc_request *mrq)
378{
71f8a4b8
JF
379 struct mmc_context_info *context_info = &mrq->host->context_info;
380
381 context_info->is_done_rcv = true;
382 wake_up_interruptible(&context_info->wait);
2220eedf
KD
383}
384
1da177e4
LT
385static void mmc_wait_done(struct mmc_request *mrq)
386{
aa8b683a
PF
387 complete(&mrq->completion);
388}
389
5163af5a
AH
390static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host)
391{
392 struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq);
393
394 /*
395 * If there is an ongoing transfer, wait for the command line to become
396 * available.
397 */
398 if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion))
399 wait_for_completion(&ongoing_mrq->cmd_completion);
400}
401
2220eedf
KD
402/*
403 *__mmc_start_data_req() - starts data request
404 * @host: MMC host to start the request
405 * @mrq: data request to start
406 *
407 * Sets the done callback to be called when request is completed by the card.
408 * Starts data mmc request execution
5163af5a
AH
409 * If an ongoing transfer is already in progress, wait for the command line
410 * to become available before sending another command.
2220eedf
KD
411 */
412static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
413{
f100c1c2
AH
414 int err;
415
5163af5a
AH
416 mmc_wait_ongoing_tfr_cmd(host);
417
2220eedf
KD
418 mrq->done = mmc_wait_data_done;
419 mrq->host = host;
f100c1c2 420
5163af5a
AH
421 init_completion(&mrq->cmd_completion);
422
f100c1c2
AH
423 err = mmc_start_request(host, mrq);
424 if (err) {
425 mrq->cmd->error = err;
5163af5a 426 mmc_complete_cmd(mrq);
9b844961 427 mmc_wait_data_done(mrq);
2220eedf 428 }
2220eedf 429
f100c1c2 430 return err;
2220eedf
KD
431}
432
956d9fd5 433static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
aa8b683a 434{
f100c1c2
AH
435 int err;
436
5163af5a
AH
437 mmc_wait_ongoing_tfr_cmd(host);
438
aa8b683a
PF
439 init_completion(&mrq->completion);
440 mrq->done = mmc_wait_done;
f100c1c2 441
5163af5a
AH
442 init_completion(&mrq->cmd_completion);
443
f100c1c2
AH
444 err = mmc_start_request(host, mrq);
445 if (err) {
446 mrq->cmd->error = err;
5163af5a 447 mmc_complete_cmd(mrq);
d3049504 448 complete(&mrq->completion);
d3049504 449 }
f100c1c2
AH
450
451 return err;
aa8b683a
PF
452}
453
5163af5a 454void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
aa8b683a 455{
08a7e1df
AH
456 struct mmc_command *cmd;
457
458 while (1) {
459 wait_for_completion(&mrq->completion);
460
461 cmd = mrq->cmd;
775a9362
ME
462
463 /*
464 * If host has timed out waiting for the sanitize
465 * to complete, card might be still in programming state
466 * so let's try to bring the card out of programming
467 * state.
468 */
469 if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
470 if (!mmc_interrupt_hpi(host->card)) {
6606110d
JP
471 pr_warn("%s: %s: Interrupted sanitize\n",
472 mmc_hostname(host), __func__);
775a9362
ME
473 cmd->error = 0;
474 break;
475 } else {
476 pr_err("%s: %s: Failed to interrupt sanitize\n",
477 mmc_hostname(host), __func__);
478 }
479 }
d3049504
AH
480 if (!cmd->error || !cmd->retries ||
481 mmc_card_removed(host->card))
08a7e1df
AH
482 break;
483
90a81489
AH
484 mmc_retune_recheck(host);
485
08a7e1df
AH
486 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
487 mmc_hostname(host), cmd->opcode, cmd->error);
488 cmd->retries--;
489 cmd->error = 0;
90a81489 490 __mmc_start_request(host, mrq);
08a7e1df 491 }
90a81489
AH
492
493 mmc_retune_release(host);
aa8b683a 494}
5163af5a
AH
495EXPORT_SYMBOL(mmc_wait_for_req_done);
496
72a5af55
AH
497/*
498 * mmc_cqe_start_req - Start a CQE request.
499 * @host: MMC host to start the request
500 * @mrq: request to start
501 *
502 * Start the request, re-tuning if needed and it is possible. Returns an error
503 * code if the request fails to start or -EBUSY if CQE is busy.
504 */
505int mmc_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
506{
507 int err;
508
509 /*
510 * CQE cannot process re-tuning commands. Caller must hold retuning
511 * while CQE is in use. Re-tuning can happen here only when CQE has no
512 * active requests i.e. this is the first. Note, re-tuning will call
513 * ->cqe_off().
514 */
515 err = mmc_retune(host);
516 if (err)
517 goto out_err;
518
519 mrq->host = host;
520
521 mmc_mrq_pr_debug(host, mrq, true);
522
523 err = mmc_mrq_prep(host, mrq);
524 if (err)
525 goto out_err;
526
527 err = host->cqe_ops->cqe_request(host, mrq);
528 if (err)
529 goto out_err;
530
531 trace_mmc_request_start(host, mrq);
532
533 return 0;
534
535out_err:
536 if (mrq->cmd) {
537 pr_debug("%s: failed to start CQE direct CMD%u, error %d\n",
538 mmc_hostname(host), mrq->cmd->opcode, err);
539 } else {
540 pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n",
541 mmc_hostname(host), mrq->tag, err);
542 }
543 return err;
544}
545EXPORT_SYMBOL(mmc_cqe_start_req);
546
547/**
548 * mmc_cqe_request_done - CQE has finished processing an MMC request
549 * @host: MMC host which completed request
550 * @mrq: MMC request which completed
551 *
552 * CQE drivers should call this function when they have completed
553 * their processing of a request.
554 */
555void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq)
556{
557 mmc_should_fail_request(host, mrq);
558
559 /* Flag re-tuning needed on CRC errors */
560 if ((mrq->cmd && mrq->cmd->error == -EILSEQ) ||
561 (mrq->data && mrq->data->error == -EILSEQ))
562 mmc_retune_needed(host);
563
564 trace_mmc_request_done(host, mrq);
565
566 if (mrq->cmd) {
567 pr_debug("%s: CQE req done (direct CMD%u): %d\n",
568 mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error);
569 } else {
570 pr_debug("%s: CQE transfer done tag %d\n",
571 mmc_hostname(host), mrq->tag);
572 }
573
574 if (mrq->data) {
575 pr_debug("%s: %d bytes transferred: %d\n",
576 mmc_hostname(host),
577 mrq->data->bytes_xfered, mrq->data->error);
578 }
579
580 mrq->done(mrq);
581}
582EXPORT_SYMBOL(mmc_cqe_request_done);
583
584/**
585 * mmc_cqe_post_req - CQE post process of a completed MMC request
586 * @host: MMC host
587 * @mrq: MMC request to be processed
588 */
589void mmc_cqe_post_req(struct mmc_host *host, struct mmc_request *mrq)
590{
591 if (host->cqe_ops->cqe_post_req)
592 host->cqe_ops->cqe_post_req(host, mrq);
593}
594EXPORT_SYMBOL(mmc_cqe_post_req);
595
596/* Arbitrary 1 second timeout */
597#define MMC_CQE_RECOVERY_TIMEOUT 1000
598
599/*
600 * mmc_cqe_recovery - Recover from CQE errors.
601 * @host: MMC host to recover
602 *
603 * Recovery consists of stopping CQE, stopping eMMC, discarding the queue in
604 * in eMMC, and discarding the queue in CQE. CQE must call
605 * mmc_cqe_request_done() on all requests. An error is returned if the eMMC
606 * fails to discard its queue.
607 */
608int mmc_cqe_recovery(struct mmc_host *host)
609{
610 struct mmc_command cmd;
611 int err;
612
613 mmc_retune_hold_now(host);
614
615 /*
616 * Recovery is expected seldom, if at all, but it reduces performance,
617 * so make sure it is not completely silent.
618 */
619 pr_warn("%s: running CQE recovery\n", mmc_hostname(host));
620
621 host->cqe_ops->cqe_recovery_start(host);
622
623 memset(&cmd, 0, sizeof(cmd));
624 cmd.opcode = MMC_STOP_TRANSMISSION,
625 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC,
626 cmd.flags &= ~MMC_RSP_CRC; /* Ignore CRC */
627 cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT,
628 mmc_wait_for_cmd(host, &cmd, 0);
629
630 memset(&cmd, 0, sizeof(cmd));
631 cmd.opcode = MMC_CMDQ_TASK_MGMT;
632 cmd.arg = 1; /* Discard entire queue */
633 cmd.flags = MMC_RSP_R1B | MMC_CMD_AC;
634 cmd.flags &= ~MMC_RSP_CRC; /* Ignore CRC */
635 cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT,
636 err = mmc_wait_for_cmd(host, &cmd, 0);
637
638 host->cqe_ops->cqe_recovery_finish(host);
639
640 mmc_retune_release(host);
641
642 return err;
643}
644EXPORT_SYMBOL(mmc_cqe_recovery);
645
5163af5a
AH
646/**
647 * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
648 * @host: MMC host
649 * @mrq: MMC request
650 *
651 * mmc_is_req_done() is used with requests that have
652 * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
653 * starting a request and before waiting for it to complete. That is,
654 * either in between calls to mmc_start_req(), or after mmc_wait_for_req()
655 * and before mmc_wait_for_req_done(). If it is called at other times the
656 * result is not meaningful.
657 */
658bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq)
659{
660 if (host->areq)
661 return host->context_info.is_done_rcv;
662 else
663 return completion_done(&mrq->completion);
664}
665EXPORT_SYMBOL(mmc_is_req_done);
aa8b683a 666
37dac068
LW
667/**
668 * mmc_finalize_areq() - finalize an asynchronous request
669 * @host: MMC host to finalize any ongoing request on
670 *
671 * Returns the status of the ongoing asynchronous request, but
672 * MMC_BLK_SUCCESS if no request was going on.
673 */
674static enum mmc_blk_status mmc_finalize_areq(struct mmc_host *host)
675{
0e72f95b 676 struct mmc_context_info *context_info = &host->context_info;
37dac068
LW
677 enum mmc_blk_status status;
678
679 if (!host->areq)
680 return MMC_BLK_SUCCESS;
681
0e72f95b
LW
682 while (1) {
683 wait_event_interruptible(context_info->wait,
684 (context_info->is_done_rcv ||
685 context_info->is_new_req));
686
687 if (context_info->is_done_rcv) {
688 struct mmc_command *cmd;
689
690 context_info->is_done_rcv = false;
691 cmd = host->areq->mrq->cmd;
692
693 if (!cmd->error || !cmd->retries ||
694 mmc_card_removed(host->card)) {
695 status = host->areq->err_check(host->card,
696 host->areq);
697 break; /* return status */
698 } else {
699 mmc_retune_recheck(host);
700 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
701 mmc_hostname(host),
702 cmd->opcode, cmd->error);
703 cmd->retries--;
704 cmd->error = 0;
705 __mmc_start_request(host, host->areq->mrq);
706 continue; /* wait for done/new event again */
707 }
708 }
709
710 return MMC_BLK_NEW_REQUEST;
711 }
712
713 mmc_retune_release(host);
37dac068
LW
714
715 /*
716 * Check BKOPS urgency for each R1 response
717 */
718 if (host->card && mmc_card_mmc(host->card) &&
719 ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
720 (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
721 (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
722 mmc_start_bkops(host->card, true);
723 }
724
725 return status;
726}
727
aa8b683a 728/**
c3399ef5 729 * mmc_start_areq - start an asynchronous request
aa8b683a 730 * @host: MMC host to start command
c3399ef5
LW
731 * @areq: asynchronous request to start
732 * @ret_stat: out parameter for status
aa8b683a
PF
733 *
734 * Start a new MMC custom command request for a host.
735 * If there is on ongoing async request wait for completion
736 * of that request and start the new one and return.
737 * Does not wait for the new request to complete.
738 *
739 * Returns the completed request, NULL in case of none completed.
740 * Wait for the an ongoing request (previoulsy started) to complete and
741 * return the completed request. If there is no ongoing request, NULL
742 * is returned without waiting. NULL is not an error condition.
743 */
c3399ef5
LW
744struct mmc_async_req *mmc_start_areq(struct mmc_host *host,
745 struct mmc_async_req *areq,
746 enum mmc_blk_status *ret_stat)
aa8b683a 747{
37dac068 748 enum mmc_blk_status status;
956d9fd5 749 int start_err = 0;
5744d50d 750 struct mmc_async_req *previous = host->areq;
aa8b683a
PF
751
752 /* Prepare a new request */
753 if (areq)
d3c6aac3 754 mmc_pre_req(host, areq->mrq);
aa8b683a 755
37dac068
LW
756 /* Finalize previous request */
757 status = mmc_finalize_areq(host);
5744d50d
LW
758 if (ret_stat)
759 *ret_stat = status;
37dac068
LW
760
761 /* The previous request is still going on... */
5744d50d 762 if (status == MMC_BLK_NEW_REQUEST)
37dac068 763 return NULL;
aa8b683a 764
37dac068 765 /* Fine so far, start the new request! */
8e8b3f51 766 if (status == MMC_BLK_SUCCESS && areq)
2220eedf 767 start_err = __mmc_start_data_req(host, areq->mrq);
aa8b683a 768
37dac068 769 /* Postprocess the old request at this point */
aa8b683a
PF
770 if (host->areq)
771 mmc_post_req(host, host->areq->mrq, 0);
772
37dac068 773 /* Cancel a prepared request if it was not started. */
8e8b3f51 774 if ((status != MMC_BLK_SUCCESS || start_err) && areq)
f5c2758f 775 mmc_post_req(host, areq->mrq, -EINVAL);
956d9fd5 776
8e8b3f51 777 if (status != MMC_BLK_SUCCESS)
956d9fd5
UH
778 host->areq = NULL;
779 else
780 host->areq = areq;
781
5744d50d 782 return previous;
aa8b683a 783}
c3399ef5 784EXPORT_SYMBOL(mmc_start_areq);
aa8b683a 785
67a61c48
PO
786/**
787 * mmc_wait_for_req - start a request and wait for completion
788 * @host: MMC host to start command
789 * @mrq: MMC request to start
790 *
791 * Start a new MMC custom command request for a host, and wait
5163af5a
AH
792 * for the command to complete. In the case of 'cap_cmd_during_tfr'
793 * requests, the transfer is ongoing and the caller can issue further
794 * commands that do not use the data lines, and then wait by calling
795 * mmc_wait_for_req_done().
796 * Does not attempt to parse the response.
67a61c48
PO
797 */
798void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 799{
aa8b683a 800 __mmc_start_req(host, mrq);
5163af5a
AH
801
802 if (!mrq->cap_cmd_during_tfr)
803 mmc_wait_for_req_done(host, mrq);
1da177e4 804}
1da177e4
LT
805EXPORT_SYMBOL(mmc_wait_for_req);
806
807/**
808 * mmc_wait_for_cmd - start a command and wait for completion
809 * @host: MMC host to start command
810 * @cmd: MMC command to start
811 * @retries: maximum number of retries
812 *
813 * Start a new MMC command for a host, and wait for the command
814 * to complete. Return any error that occurred while the command
815 * was executing. Do not attempt to parse the response.
816 */
817int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
818{
c7836d15 819 struct mmc_request mrq = {};
1da177e4 820
d84075c8 821 WARN_ON(!host->claimed);
1da177e4 822
1da177e4
LT
823 memset(cmd->resp, 0, sizeof(cmd->resp));
824 cmd->retries = retries;
825
826 mrq.cmd = cmd;
827 cmd->data = NULL;
828
829 mmc_wait_for_req(host, &mrq);
830
831 return cmd->error;
832}
833
834EXPORT_SYMBOL(mmc_wait_for_cmd);
835
d773d725
RK
836/**
837 * mmc_set_data_timeout - set the timeout for a data command
838 * @data: data phase for command
839 * @card: the MMC card associated with the data transfer
67a61c48
PO
840 *
841 * Computes the data timeout parameters according to the
842 * correct algorithm given the card type.
d773d725 843 */
b146d26a 844void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
845{
846 unsigned int mult;
847
e6f918bf
PO
848 /*
849 * SDIO cards only define an upper 1 s limit on access.
850 */
851 if (mmc_card_sdio(card)) {
852 data->timeout_ns = 1000000000;
853 data->timeout_clks = 0;
854 return;
855 }
856
d773d725
RK
857 /*
858 * SD cards use a 100 multiplier rather than 10
859 */
860 mult = mmc_card_sd(card) ? 100 : 10;
861
862 /*
863 * Scale up the multiplier (and therefore the timeout) by
864 * the r2w factor for writes.
865 */
b146d26a 866 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
867 mult <<= card->csd.r2w_factor;
868
4406ae21
SL
869 data->timeout_ns = card->csd.taac_ns * mult;
870 data->timeout_clks = card->csd.taac_clks * mult;
d773d725
RK
871
872 /*
873 * SD cards also have an upper limit on the timeout.
874 */
875 if (mmc_card_sd(card)) {
876 unsigned int timeout_us, limit_us;
877
878 timeout_us = data->timeout_ns / 1000;
9eadcc05 879 if (card->host->ios.clock)
e9b86841 880 timeout_us += data->timeout_clks * 1000 /
9eadcc05 881 (card->host->ios.clock / 1000);
d773d725 882
b146d26a 883 if (data->flags & MMC_DATA_WRITE)
493890e7 884 /*
3bdc9ba8
PW
885 * The MMC spec "It is strongly recommended
886 * for hosts to implement more than 500ms
887 * timeout value even if the card indicates
888 * the 250ms maximum busy length." Even the
889 * previous value of 300ms is known to be
890 * insufficient for some cards.
493890e7 891 */
3bdc9ba8 892 limit_us = 3000000;
d773d725
RK
893 else
894 limit_us = 100000;
895
fba68bd2
PL
896 /*
897 * SDHC cards always use these fixed values.
898 */
6ca2920d 899 if (timeout_us > limit_us) {
d773d725
RK
900 data->timeout_ns = limit_us * 1000;
901 data->timeout_clks = 0;
902 }
f7bf11a3
SW
903
904 /* assign limit value if invalid */
905 if (timeout_us == 0)
906 data->timeout_ns = limit_us * 1000;
d773d725 907 }
6de5fc9c
SNX
908
909 /*
910 * Some cards require longer data read timeout than indicated in CSD.
911 * Address this by setting the read timeout to a "reasonably high"
32ecd320 912 * value. For the cards tested, 600ms has proven enough. If necessary,
6de5fc9c
SNX
913 * this value can be increased if other problematic cards require this.
914 */
915 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
32ecd320 916 data->timeout_ns = 600000000;
6de5fc9c
SNX
917 data->timeout_clks = 0;
918 }
919
c0c88871
WM
920 /*
921 * Some cards need very high timeouts if driven in SPI mode.
922 * The worst observed timeout was 900ms after writing a
923 * continuous stream of data until the internal logic
924 * overflowed.
925 */
926 if (mmc_host_is_spi(card->host)) {
927 if (data->flags & MMC_DATA_WRITE) {
928 if (data->timeout_ns < 1000000000)
929 data->timeout_ns = 1000000000; /* 1s */
930 } else {
931 if (data->timeout_ns < 100000000)
932 data->timeout_ns = 100000000; /* 100ms */
933 }
934 }
d773d725
RK
935}
936EXPORT_SYMBOL(mmc_set_data_timeout);
937
ad3868b2
PO
938/**
939 * mmc_align_data_size - pads a transfer size to a more optimal value
940 * @card: the MMC card associated with the data transfer
941 * @sz: original transfer size
942 *
943 * Pads the original data size with a number of extra bytes in
944 * order to avoid controller bugs and/or performance hits
945 * (e.g. some controllers revert to PIO for certain sizes).
946 *
947 * Returns the improved size, which might be unmodified.
948 *
949 * Note that this function is only relevant when issuing a
950 * single scatter gather entry.
951 */
952unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
953{
954 /*
955 * FIXME: We don't have a system for the controller to tell
956 * the core about its problems yet, so for now we just 32-bit
957 * align the size.
958 */
959 sz = ((sz + 3) / 4) * 4;
960
961 return sz;
962}
963EXPORT_SYMBOL(mmc_align_data_size);
964
6c0cedd1
AH
965/*
966 * Allow claiming an already claimed host if the context is the same or there is
967 * no context but the task is the same.
968 */
969static inline bool mmc_ctx_matches(struct mmc_host *host, struct mmc_ctx *ctx,
970 struct task_struct *task)
971{
972 return host->claimer == ctx ||
973 (!ctx && task && host->claimer->task == task);
974}
975
976static inline void mmc_ctx_set_claimer(struct mmc_host *host,
977 struct mmc_ctx *ctx,
978 struct task_struct *task)
979{
980 if (!host->claimer) {
981 if (ctx)
982 host->claimer = ctx;
983 else
984 host->claimer = &host->default_ctx;
985 }
986 if (task)
987 host->claimer->task = task;
988}
989
1da177e4 990/**
2342f332 991 * __mmc_claim_host - exclusively claim a host
1da177e4 992 * @host: mmc host to claim
6c0cedd1
AH
993 * @ctx: context that claims the host or NULL in which case the default
994 * context will be used
2342f332 995 * @abort: whether or not the operation should be aborted
1da177e4 996 *
2342f332
NP
997 * Claim a host for a set of operations. If @abort is non null and
998 * dereference a non-zero value then this will return prematurely with
999 * that non-zero value without acquiring the lock. Returns zero
1000 * with the lock held otherwise.
1da177e4 1001 */
6c0cedd1
AH
1002int __mmc_claim_host(struct mmc_host *host, struct mmc_ctx *ctx,
1003 atomic_t *abort)
1da177e4 1004{
6c0cedd1 1005 struct task_struct *task = ctx ? NULL : current;
1da177e4
LT
1006 DECLARE_WAITQUEUE(wait, current);
1007 unsigned long flags;
2342f332 1008 int stop;
9250aea7 1009 bool pm = false;
1da177e4 1010
cf795bfb
PO
1011 might_sleep();
1012
1da177e4
LT
1013 add_wait_queue(&host->wq, &wait);
1014 spin_lock_irqsave(&host->lock, flags);
1015 while (1) {
1016 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332 1017 stop = abort ? atomic_read(abort) : 0;
6c0cedd1 1018 if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task))
1da177e4
LT
1019 break;
1020 spin_unlock_irqrestore(&host->lock, flags);
1021 schedule();
1022 spin_lock_irqsave(&host->lock, flags);
1023 }
1024 set_current_state(TASK_RUNNING);
319a3f14 1025 if (!stop) {
2342f332 1026 host->claimed = 1;
6c0cedd1 1027 mmc_ctx_set_claimer(host, ctx, task);
319a3f14 1028 host->claim_cnt += 1;
9250aea7
UH
1029 if (host->claim_cnt == 1)
1030 pm = true;
319a3f14 1031 } else
2342f332 1032 wake_up(&host->wq);
1da177e4
LT
1033 spin_unlock_irqrestore(&host->lock, flags);
1034 remove_wait_queue(&host->wq, &wait);
9250aea7
UH
1035
1036 if (pm)
1037 pm_runtime_get_sync(mmc_dev(host));
1038
2342f332 1039 return stop;
1da177e4 1040}
2342f332 1041EXPORT_SYMBOL(__mmc_claim_host);
8ea926b2 1042
ab1efd27 1043/**
907d2e7c 1044 * mmc_release_host - release a host
ab1efd27
UH
1045 * @host: mmc host to release
1046 *
907d2e7c
AH
1047 * Release a MMC host, allowing others to claim the host
1048 * for their operations.
ab1efd27 1049 */
907d2e7c 1050void mmc_release_host(struct mmc_host *host)
8ea926b2
AH
1051{
1052 unsigned long flags;
1053
907d2e7c
AH
1054 WARN_ON(!host->claimed);
1055
8ea926b2 1056 spin_lock_irqsave(&host->lock, flags);
319a3f14
AH
1057 if (--host->claim_cnt) {
1058 /* Release for nested claim */
1059 spin_unlock_irqrestore(&host->lock, flags);
1060 } else {
1061 host->claimed = 0;
6c0cedd1 1062 host->claimer->task = NULL;
319a3f14
AH
1063 host->claimer = NULL;
1064 spin_unlock_irqrestore(&host->lock, flags);
1065 wake_up(&host->wq);
9250aea7
UH
1066 pm_runtime_mark_last_busy(mmc_dev(host));
1067 pm_runtime_put_autosuspend(mmc_dev(host));
319a3f14 1068 }
8ea926b2 1069}
1da177e4
LT
1070EXPORT_SYMBOL(mmc_release_host);
1071
e94cfef6
UH
1072/*
1073 * This is a helper function, which fetches a runtime pm reference for the
1074 * card device and also claims the host.
1075 */
6c0cedd1 1076void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx)
e94cfef6
UH
1077{
1078 pm_runtime_get_sync(&card->dev);
6c0cedd1 1079 __mmc_claim_host(card->host, ctx, NULL);
e94cfef6
UH
1080}
1081EXPORT_SYMBOL(mmc_get_card);
1082
1083/*
1084 * This is a helper function, which releases the host and drops the runtime
1085 * pm reference for the card device.
1086 */
6c0cedd1 1087void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx)
e94cfef6 1088{
6c0cedd1
AH
1089 struct mmc_host *host = card->host;
1090
1091 WARN_ON(ctx && host->claimer != ctx);
1092
1093 mmc_release_host(host);
e94cfef6
UH
1094 pm_runtime_mark_last_busy(&card->dev);
1095 pm_runtime_put_autosuspend(&card->dev);
1096}
1097EXPORT_SYMBOL(mmc_put_card);
1098
7ea239d9
PO
1099/*
1100 * Internal function that does the actual ios call to the host driver,
1101 * optionally printing some debug output.
1102 */
920e70c5
RK
1103static inline void mmc_set_ios(struct mmc_host *host)
1104{
1105 struct mmc_ios *ios = &host->ios;
1106
cd9277c0
PO
1107 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
1108 "width %u timing %u\n",
920e70c5
RK
1109 mmc_hostname(host), ios->clock, ios->bus_mode,
1110 ios->power_mode, ios->chip_select, ios->vdd,
ed9feec7 1111 1 << ios->bus_width, ios->timing);
fba68bd2 1112
920e70c5
RK
1113 host->ops->set_ios(host, ios);
1114}
1115
7ea239d9
PO
1116/*
1117 * Control chip select pin on a host.
1118 */
da7fbe58 1119void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 1120{
da7fbe58
PO
1121 host->ios.chip_select = mode;
1122 mmc_set_ios(host);
1da177e4
LT
1123}
1124
7ea239d9
PO
1125/*
1126 * Sets the host clock to the highest possible frequency that
1127 * is below "hz".
1128 */
9eadcc05 1129void mmc_set_clock(struct mmc_host *host, unsigned int hz)
7ea239d9 1130{
6a98f1e8 1131 WARN_ON(hz && hz < host->f_min);
7ea239d9
PO
1132
1133 if (hz > host->f_max)
1134 hz = host->f_max;
1135
1136 host->ios.clock = hz;
1137 mmc_set_ios(host);
1138}
1139
63e415c6
AH
1140int mmc_execute_tuning(struct mmc_card *card)
1141{
1142 struct mmc_host *host = card->host;
1143 u32 opcode;
1144 int err;
1145
1146 if (!host->ops->execute_tuning)
1147 return 0;
1148
3e207c8c
AH
1149 if (host->cqe_on)
1150 host->cqe_ops->cqe_off(host);
1151
63e415c6
AH
1152 if (mmc_card_mmc(card))
1153 opcode = MMC_SEND_TUNING_BLOCK_HS200;
1154 else
1155 opcode = MMC_SEND_TUNING_BLOCK;
1156
63e415c6 1157 err = host->ops->execute_tuning(host, opcode);
63e415c6
AH
1158
1159 if (err)
07d97d87
RK
1160 pr_err("%s: tuning execution failed: %d\n",
1161 mmc_hostname(host), err);
79d5a65a
AH
1162 else
1163 mmc_retune_enable(host);
63e415c6
AH
1164
1165 return err;
1166}
1167
7ea239d9
PO
1168/*
1169 * Change the bus mode (open drain/push-pull) of a host.
1170 */
1171void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1172{
1173 host->ios.bus_mode = mode;
1174 mmc_set_ios(host);
1175}
1176
0f8d8ea6
AH
1177/*
1178 * Change data bus width of a host.
1179 */
1180void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1181{
4c4cb171
PR
1182 host->ios.bus_width = width;
1183 mmc_set_ios(host);
0f8d8ea6
AH
1184}
1185
2d079c43
JR
1186/*
1187 * Set initial state after a power cycle or a hw_reset.
1188 */
1189void mmc_set_initial_state(struct mmc_host *host)
1190{
3e207c8c
AH
1191 if (host->cqe_on)
1192 host->cqe_ops->cqe_off(host);
1193
79d5a65a
AH
1194 mmc_retune_disable(host);
1195
2d079c43
JR
1196 if (mmc_host_is_spi(host))
1197 host->ios.chip_select = MMC_CS_HIGH;
1198 else
1199 host->ios.chip_select = MMC_CS_DONTCARE;
1200 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1201 host->ios.bus_width = MMC_BUS_WIDTH_1;
1202 host->ios.timing = MMC_TIMING_LEGACY;
75e8a228 1203 host->ios.drv_type = 0;
81ac2af6
SL
1204 host->ios.enhanced_strobe = false;
1205
1206 /*
1207 * Make sure we are in non-enhanced strobe mode before we
1208 * actually enable it in ext_csd.
1209 */
1210 if ((host->caps2 & MMC_CAP2_HS400_ES) &&
1211 host->ops->hs400_enhanced_strobe)
1212 host->ops->hs400_enhanced_strobe(host, &host->ios);
2d079c43
JR
1213
1214 mmc_set_ios(host);
1215}
1216
86e8286a
AV
1217/**
1218 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1219 * @vdd: voltage (mV)
1220 * @low_bits: prefer low bits in boundary cases
1221 *
1222 * This function returns the OCR bit number according to the provided @vdd
1223 * value. If conversion is not possible a negative errno value returned.
1224 *
1225 * Depending on the @low_bits flag the function prefers low or high OCR bits
1226 * on boundary voltages. For example,
1227 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1228 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1229 *
1230 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1231 */
1232static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1233{
1234 const int max_bit = ilog2(MMC_VDD_35_36);
1235 int bit;
1236
1237 if (vdd < 1650 || vdd > 3600)
1238 return -EINVAL;
1239
1240 if (vdd >= 1650 && vdd <= 1950)
1241 return ilog2(MMC_VDD_165_195);
1242
1243 if (low_bits)
1244 vdd -= 1;
1245
1246 /* Base 2000 mV, step 100 mV, bit's base 8. */
1247 bit = (vdd - 2000) / 100 + 8;
1248 if (bit > max_bit)
1249 return max_bit;
1250 return bit;
1251}
1252
1253/**
1254 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1255 * @vdd_min: minimum voltage value (mV)
1256 * @vdd_max: maximum voltage value (mV)
1257 *
1258 * This function returns the OCR mask bits according to the provided @vdd_min
1259 * and @vdd_max values. If conversion is not possible the function returns 0.
1260 *
1261 * Notes wrt boundary cases:
1262 * This function sets the OCR bits for all boundary voltages, for example
1263 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1264 * MMC_VDD_34_35 mask.
1265 */
1266u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1267{
1268 u32 mask = 0;
1269
1270 if (vdd_max < vdd_min)
1271 return 0;
1272
1273 /* Prefer high bits for the boundary vdd_max values. */
1274 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1275 if (vdd_max < 0)
1276 return 0;
1277
1278 /* Prefer low bits for the boundary vdd_min values. */
1279 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1280 if (vdd_min < 0)
1281 return 0;
1282
1283 /* Fill the mask, from max bit to min bit. */
1284 while (vdd_max >= vdd_min)
1285 mask |= 1 << vdd_max--;
1286
1287 return mask;
1288}
1289EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1290
6e9e318b
HZ
1291#ifdef CONFIG_OF
1292
1293/**
1294 * mmc_of_parse_voltage - return mask of supported voltages
1295 * @np: The device node need to be parsed.
1296 * @mask: mask of voltages available for MMC/SD/SDIO
1297 *
cf925747
RK
1298 * Parse the "voltage-ranges" DT property, returning zero if it is not
1299 * found, negative errno if the voltage-range specification is invalid,
1300 * or one if the voltage-range is specified and successfully parsed.
6e9e318b
HZ
1301 */
1302int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1303{
1304 const u32 *voltage_ranges;
1305 int num_ranges, i;
1306
1307 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1308 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
10a16a01 1309 if (!voltage_ranges) {
bf892de9 1310 pr_debug("%pOF: voltage-ranges unspecified\n", np);
cf925747 1311 return 0;
10a16a01
RK
1312 }
1313 if (!num_ranges) {
bf892de9 1314 pr_err("%pOF: voltage-ranges empty\n", np);
6e9e318b
HZ
1315 return -EINVAL;
1316 }
1317
1318 for (i = 0; i < num_ranges; i++) {
1319 const int j = i * 2;
1320 u32 ocr_mask;
1321
1322 ocr_mask = mmc_vddrange_to_ocrmask(
1323 be32_to_cpu(voltage_ranges[j]),
1324 be32_to_cpu(voltage_ranges[j + 1]));
1325 if (!ocr_mask) {
bf892de9
RH
1326 pr_err("%pOF: voltage-range #%d is invalid\n",
1327 np, i);
6e9e318b
HZ
1328 return -EINVAL;
1329 }
1330 *mask |= ocr_mask;
1331 }
1332
cf925747 1333 return 1;
6e9e318b
HZ
1334}
1335EXPORT_SYMBOL(mmc_of_parse_voltage);
1336
1337#endif /* CONFIG_OF */
1338
25185f3f
SH
1339static int mmc_of_get_func_num(struct device_node *node)
1340{
1341 u32 reg;
1342 int ret;
1343
1344 ret = of_property_read_u32(node, "reg", &reg);
1345 if (ret < 0)
1346 return ret;
1347
1348 return reg;
1349}
1350
1351struct device_node *mmc_of_find_child_device(struct mmc_host *host,
1352 unsigned func_num)
1353{
1354 struct device_node *node;
1355
1356 if (!host->parent || !host->parent->of_node)
1357 return NULL;
1358
1359 for_each_child_of_node(host->parent->of_node, node) {
1360 if (mmc_of_get_func_num(node) == func_num)
1361 return node;
1362 }
1363
1364 return NULL;
1365}
1366
5c13941a
DB
1367#ifdef CONFIG_REGULATOR
1368
310c805e
HS
1369/**
1370 * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage
1371 * @vdd_bit: OCR bit number
1372 * @min_uV: minimum voltage value (mV)
1373 * @max_uV: maximum voltage value (mV)
1374 *
1375 * This function returns the voltage range according to the provided OCR
1376 * bit number. If conversion is not possible a negative errno value returned.
1377 */
1378static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV)
1379{
1380 int tmp;
1381
1382 if (!vdd_bit)
1383 return -EINVAL;
1384
1385 /*
1386 * REVISIT mmc_vddrange_to_ocrmask() may have set some
1387 * bits this regulator doesn't quite support ... don't
1388 * be too picky, most cards and regulators are OK with
1389 * a 0.1V range goof (it's a small error percentage).
1390 */
1391 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1392 if (tmp == 0) {
1393 *min_uV = 1650 * 1000;
1394 *max_uV = 1950 * 1000;
1395 } else {
1396 *min_uV = 1900 * 1000 + tmp * 100 * 1000;
1397 *max_uV = *min_uV + 100 * 1000;
1398 }
1399
1400 return 0;
1401}
1402
5c13941a
DB
1403/**
1404 * mmc_regulator_get_ocrmask - return mask of supported voltages
1405 * @supply: regulator to use
1406 *
1407 * This returns either a negative errno, or a mask of voltages that
1408 * can be provided to MMC/SD/SDIO devices using the specified voltage
1409 * regulator. This would normally be called before registering the
1410 * MMC host adapter.
1411 */
1412int mmc_regulator_get_ocrmask(struct regulator *supply)
1413{
1414 int result = 0;
1415 int count;
1416 int i;
9ed7ca89
JMC
1417 int vdd_uV;
1418 int vdd_mV;
5c13941a
DB
1419
1420 count = regulator_count_voltages(supply);
1421 if (count < 0)
1422 return count;
1423
1424 for (i = 0; i < count; i++) {
5c13941a
DB
1425 vdd_uV = regulator_list_voltage(supply, i);
1426 if (vdd_uV <= 0)
1427 continue;
1428
1429 vdd_mV = vdd_uV / 1000;
1430 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1431 }
1432
9ed7ca89
JMC
1433 if (!result) {
1434 vdd_uV = regulator_get_voltage(supply);
1435 if (vdd_uV <= 0)
1436 return vdd_uV;
1437
1438 vdd_mV = vdd_uV / 1000;
1439 result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1440 }
1441
5c13941a
DB
1442 return result;
1443}
45a6b32e 1444EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
5c13941a
DB
1445
1446/**
1447 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
99fc5131 1448 * @mmc: the host to regulate
5c13941a 1449 * @supply: regulator to use
99fc5131 1450 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
5c13941a
DB
1451 *
1452 * Returns zero on success, else negative errno.
1453 *
1454 * MMC host drivers may use this to enable or disable a regulator using
1455 * a particular supply voltage. This would normally be called from the
1456 * set_ios() method.
1457 */
99fc5131
LW
1458int mmc_regulator_set_ocr(struct mmc_host *mmc,
1459 struct regulator *supply,
1460 unsigned short vdd_bit)
5c13941a
DB
1461{
1462 int result = 0;
1463 int min_uV, max_uV;
5c13941a
DB
1464
1465 if (vdd_bit) {
310c805e 1466 mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
5c13941a 1467
ca6429d4 1468 result = regulator_set_voltage(supply, min_uV, max_uV);
99fc5131 1469 if (result == 0 && !mmc->regulator_enabled) {
5c13941a 1470 result = regulator_enable(supply);
99fc5131
LW
1471 if (!result)
1472 mmc->regulator_enabled = true;
1473 }
1474 } else if (mmc->regulator_enabled) {
5c13941a 1475 result = regulator_disable(supply);
99fc5131
LW
1476 if (result == 0)
1477 mmc->regulator_enabled = false;
5c13941a
DB
1478 }
1479
99fc5131
LW
1480 if (result)
1481 dev_err(mmc_dev(mmc),
1482 "could not set regulator OCR (%d)\n", result);
5c13941a
DB
1483 return result;
1484}
45a6b32e 1485EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
5c13941a 1486
2086f801
DA
1487static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator,
1488 int min_uV, int target_uV,
1489 int max_uV)
1490{
1491 /*
1492 * Check if supported first to avoid errors since we may try several
1493 * signal levels during power up and don't want to show errors.
1494 */
1495 if (!regulator_is_supported_voltage(regulator, min_uV, max_uV))
1496 return -EINVAL;
1497
1498 return regulator_set_voltage_triplet(regulator, min_uV, target_uV,
1499 max_uV);
1500}
1501
1502/**
1503 * mmc_regulator_set_vqmmc - Set VQMMC as per the ios
1504 *
1505 * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible.
1506 * That will match the behavior of old boards where VQMMC and VMMC were supplied
1507 * by the same supply. The Bus Operating conditions for 3.3V signaling in the
1508 * SD card spec also define VQMMC in terms of VMMC.
1509 * If this is not possible we'll try the full 2.7-3.6V of the spec.
1510 *
1511 * For 1.2V and 1.8V signaling we'll try to get as close as possible to the
1512 * requested voltage. This is definitely a good idea for UHS where there's a
1513 * separate regulator on the card that's trying to make 1.8V and it's best if
1514 * we match.
1515 *
1516 * This function is expected to be used by a controller's
1517 * start_signal_voltage_switch() function.
1518 */
1519int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios)
1520{
1521 struct device *dev = mmc_dev(mmc);
1522 int ret, volt, min_uV, max_uV;
1523
1524 /* If no vqmmc supply then we can't change the voltage */
1525 if (IS_ERR(mmc->supply.vqmmc))
1526 return -EINVAL;
1527
1528 switch (ios->signal_voltage) {
1529 case MMC_SIGNAL_VOLTAGE_120:
1530 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
1531 1100000, 1200000, 1300000);
1532 case MMC_SIGNAL_VOLTAGE_180:
1533 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
1534 1700000, 1800000, 1950000);
1535 case MMC_SIGNAL_VOLTAGE_330:
1536 ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV);
1537 if (ret < 0)
1538 return ret;
1539
1540 dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n",
1541 __func__, volt, max_uV);
1542
1543 min_uV = max(volt - 300000, 2700000);
1544 max_uV = min(max_uV + 200000, 3600000);
1545
1546 /*
1547 * Due to a limitation in the current implementation of
1548 * regulator_set_voltage_triplet() which is taking the lowest
1549 * voltage possible if below the target, search for a suitable
1550 * voltage in two steps and try to stay close to vmmc
1551 * with a 0.3V tolerance at first.
1552 */
1553 if (!mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
1554 min_uV, volt, max_uV))
1555 return 0;
1556
1557 return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc,
1558 2700000, volt, 3600000);
1559 default:
1560 return -EINVAL;
1561 }
1562}
1563EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
1564
4d1f52f9
TK
1565#endif /* CONFIG_REGULATOR */
1566
1f90e9a3
WS
1567/**
1568 * mmc_regulator_get_supply - try to get VMMC and VQMMC regulators for a host
1569 * @mmc: the host to regulate
1570 *
1571 * Returns 0 or errno. errno should be handled, it is either a critical error
1572 * or -EPROBE_DEFER. 0 means no critical error but it does not mean all
1573 * regulators have been found because they all are optional. If you require
1574 * certain regulators, you need to check separately in your driver if they got
1575 * populated after calling this function.
1576 */
e137788d
GL
1577int mmc_regulator_get_supply(struct mmc_host *mmc)
1578{
1579 struct device *dev = mmc_dev(mmc);
e137788d
GL
1580 int ret;
1581
4d1f52f9 1582 mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
bc35d5ed 1583 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
e137788d 1584
4d1f52f9
TK
1585 if (IS_ERR(mmc->supply.vmmc)) {
1586 if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
1587 return -EPROBE_DEFER;
6e1bbc51 1588 dev_dbg(dev, "No vmmc regulator found\n");
4d1f52f9
TK
1589 } else {
1590 ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
1591 if (ret > 0)
1592 mmc->ocr_avail = ret;
1593 else
1594 dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
1595 }
e137788d 1596
4d1f52f9
TK
1597 if (IS_ERR(mmc->supply.vqmmc)) {
1598 if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
1599 return -EPROBE_DEFER;
6e1bbc51 1600 dev_dbg(dev, "No vqmmc regulator found\n");
4d1f52f9 1601 }
e137788d
GL
1602
1603 return 0;
1604}
1605EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1606
1da177e4
LT
1607/*
1608 * Mask off any voltages we don't support and select
1609 * the lowest voltage
1610 */
7ea239d9 1611u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
1612{
1613 int bit;
1614
726d6f23
UH
1615 /*
1616 * Sanity check the voltages that the card claims to
1617 * support.
1618 */
1619 if (ocr & 0x7F) {
1620 dev_warn(mmc_dev(host),
1621 "card claims to support voltages below defined range\n");
1622 ocr &= ~0x7F;
1623 }
1624
1da177e4 1625 ocr &= host->ocr_avail;
ce69d37b
UH
1626 if (!ocr) {
1627 dev_warn(mmc_dev(host), "no support for card's volts\n");
1628 return 0;
1629 }
1da177e4 1630
ce69d37b
UH
1631 if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
1632 bit = ffs(ocr) - 1;
63ef731a 1633 ocr &= 3 << bit;
ce69d37b 1634 mmc_power_cycle(host, ocr);
1da177e4 1635 } else {
ce69d37b
UH
1636 bit = fls(ocr) - 1;
1637 ocr &= 3 << bit;
1638 if (bit != host->ios.vdd)
1639 dev_warn(mmc_dev(host), "exceeding card's volts\n");
1da177e4
LT
1640 }
1641
1642 return ocr;
1643}
1644
4e74b6b3 1645int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
567c8903
JR
1646{
1647 int err = 0;
1648 int old_signal_voltage = host->ios.signal_voltage;
1649
1650 host->ios.signal_voltage = signal_voltage;
9eadcc05 1651 if (host->ops->start_signal_voltage_switch)
567c8903 1652 err = host->ops->start_signal_voltage_switch(host, &host->ios);
567c8903
JR
1653
1654 if (err)
1655 host->ios.signal_voltage = old_signal_voltage;
1656
1657 return err;
1658
1659}
1660
3f496afb
AH
1661int mmc_host_set_uhs_voltage(struct mmc_host *host)
1662{
1663 u32 clock;
1664
1665 /*
1666 * During a signal voltage level switch, the clock must be gated
1667 * for 5 ms according to the SD spec
1668 */
1669 clock = host->ios.clock;
1670 host->ios.clock = 0;
1671 mmc_set_ios(host);
1672
1673 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
1674 return -EAGAIN;
1675
1676 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
1677 mmc_delay(10);
1678 host->ios.clock = clock;
1679 mmc_set_ios(host);
1680
1681 return 0;
1682}
1683
2ed573b6 1684int mmc_set_uhs_voltage(struct mmc_host *host, u32 ocr)
f2119df6 1685{
c7836d15 1686 struct mmc_command cmd = {};
f2119df6
AN
1687 int err = 0;
1688
0797e5f1
JR
1689 /*
1690 * If we cannot switch voltages, return failure so the caller
1691 * can continue without UHS mode
1692 */
1693 if (!host->ops->start_signal_voltage_switch)
1694 return -EPERM;
1695 if (!host->ops->card_busy)
6606110d
JP
1696 pr_warn("%s: cannot verify signal voltage switch\n",
1697 mmc_hostname(host));
0797e5f1
JR
1698
1699 cmd.opcode = SD_SWITCH_VOLTAGE;
1700 cmd.arg = 0;
1701 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1702
1703 err = mmc_wait_for_cmd(host, &cmd, 0);
1704 if (err)
9eadcc05
UH
1705 return err;
1706
1707 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1708 return -EIO;
0797e5f1 1709
0797e5f1
JR
1710 /*
1711 * The card should drive cmd and dat[0:3] low immediately
1712 * after the response of cmd11, but wait 1 ms to be sure
1713 */
1714 mmc_delay(1);
1715 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1716 err = -EAGAIN;
1717 goto power_cycle;
1718 }
f2119df6 1719
3f496afb 1720 if (mmc_host_set_uhs_voltage(host)) {
0797e5f1
JR
1721 /*
1722 * Voltages may not have been switched, but we've already
1723 * sent CMD11, so a power cycle is required anyway
1724 */
1725 err = -EAGAIN;
1726 goto power_cycle;
f2119df6
AN
1727 }
1728
0797e5f1
JR
1729 /* Wait for at least 1 ms according to spec */
1730 mmc_delay(1);
1731
1732 /*
1733 * Failure to switch is indicated by the card holding
1734 * dat[0:3] low
1735 */
1736 if (host->ops->card_busy && host->ops->card_busy(host))
1737 err = -EAGAIN;
1738
1739power_cycle:
1740 if (err) {
1741 pr_debug("%s: Signal voltage switch failed, "
1742 "power cycling card\n", mmc_hostname(host));
0f791fda 1743 mmc_power_cycle(host, ocr);
0797e5f1
JR
1744 }
1745
0797e5f1 1746 return err;
f2119df6
AN
1747}
1748
b57c43ad 1749/*
7ea239d9 1750 * Select timing parameters for host.
b57c43ad 1751 */
7ea239d9 1752void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 1753{
7ea239d9
PO
1754 host->ios.timing = timing;
1755 mmc_set_ios(host);
b57c43ad
PO
1756}
1757
d6d50a15
AN
1758/*
1759 * Select appropriate driver type for host.
1760 */
1761void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1762{
1763 host->ios.drv_type = drv_type;
1764 mmc_set_ios(host);
1765}
1766
e23350b3
AH
1767int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
1768 int card_drv_type, int *drv_type)
1769{
1770 struct mmc_host *host = card->host;
1771 int host_drv_type = SD_DRIVER_TYPE_B;
e23350b3
AH
1772
1773 *drv_type = 0;
1774
1775 if (!host->ops->select_drive_strength)
1776 return 0;
1777
1778 /* Use SD definition of driver strength for hosts */
1779 if (host->caps & MMC_CAP_DRIVER_TYPE_A)
1780 host_drv_type |= SD_DRIVER_TYPE_A;
1781
1782 if (host->caps & MMC_CAP_DRIVER_TYPE_C)
1783 host_drv_type |= SD_DRIVER_TYPE_C;
1784
1785 if (host->caps & MMC_CAP_DRIVER_TYPE_D)
1786 host_drv_type |= SD_DRIVER_TYPE_D;
1787
1788 /*
1789 * The drive strength that the hardware can support
1790 * depends on the board design. Pass the appropriate
1791 * information and let the hardware specific code
1792 * return what is possible given the options
1793 */
9eadcc05
UH
1794 return host->ops->select_drive_strength(card, max_dtr,
1795 host_drv_type,
1796 card_drv_type,
1797 drv_type);
e23350b3
AH
1798}
1799
1da177e4 1800/*
45f8245b
RK
1801 * Apply power to the MMC stack. This is a two-stage process.
1802 * First, we enable power to the card without the clock running.
1803 * We then wait a bit for the power to stabilise. Finally,
1804 * enable the bus drivers and clock to the card.
1805 *
1806 * We must _NOT_ enable the clock prior to power stablising.
1807 *
1808 * If a host does all the power sequencing itself, ignore the
1809 * initial MMC_POWER_UP stage.
1da177e4 1810 */
4a065193 1811void mmc_power_up(struct mmc_host *host, u32 ocr)
1da177e4 1812{
fa550189
UH
1813 if (host->ios.power_mode == MMC_POWER_ON)
1814 return;
1815
3aa8793f
UH
1816 mmc_pwrseq_pre_power_on(host);
1817
4a065193 1818 host->ios.vdd = fls(ocr) - 1;
1da177e4 1819 host->ios.power_mode = MMC_POWER_UP;
2d079c43
JR
1820 /* Set initial state and call mmc_set_ios */
1821 mmc_set_initial_state(host);
1da177e4 1822
ceae98f2 1823 /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
4e74b6b3 1824 if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330))
ceae98f2 1825 dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
4e74b6b3 1826 else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
ceae98f2 1827 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
4e74b6b3 1828 else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120))
ceae98f2 1829 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
108ecc4c 1830
f9996aee
PO
1831 /*
1832 * This delay should be sufficient to allow the power supply
1833 * to reach the minimum voltage.
1834 */
79bccc5a 1835 mmc_delay(10);
1da177e4 1836
4febb7e2
UH
1837 mmc_pwrseq_post_power_on(host);
1838
88ae8b86 1839 host->ios.clock = host->f_init;
8dfd0374 1840
1da177e4 1841 host->ios.power_mode = MMC_POWER_ON;
920e70c5 1842 mmc_set_ios(host);
1da177e4 1843
f9996aee
PO
1844 /*
1845 * This delay must be at least 74 clock sizes, or 1 ms, or the
1846 * time required to reach a stable voltage.
1847 */
79bccc5a 1848 mmc_delay(10);
1da177e4
LT
1849}
1850
7f7e4129 1851void mmc_power_off(struct mmc_host *host)
1da177e4 1852{
fa550189
UH
1853 if (host->ios.power_mode == MMC_POWER_OFF)
1854 return;
1855
3aa8793f
UH
1856 mmc_pwrseq_power_off(host);
1857
1da177e4
LT
1858 host->ios.clock = 0;
1859 host->ios.vdd = 0;
b33d46c3 1860
1da177e4 1861 host->ios.power_mode = MMC_POWER_OFF;
2d079c43
JR
1862 /* Set initial state and call mmc_set_ios */
1863 mmc_set_initial_state(host);
778e277c 1864
041beb1d
DD
1865 /*
1866 * Some configurations, such as the 802.11 SDIO card in the OLPC
1867 * XO-1.5, require a short delay after poweroff before the card
1868 * can be successfully turned on again.
1869 */
1870 mmc_delay(1);
1da177e4
LT
1871}
1872
4a065193 1873void mmc_power_cycle(struct mmc_host *host, u32 ocr)
276e090f
JR
1874{
1875 mmc_power_off(host);
1876 /* Wait at least 1 ms according to SD spec */
1877 mmc_delay(1);
4a065193 1878 mmc_power_up(host, ocr);
276e090f
JR
1879}
1880
39361851
AB
1881/*
1882 * Cleanup when the last reference to the bus operator is dropped.
1883 */
261172fd 1884static void __mmc_release_bus(struct mmc_host *host)
39361851 1885{
6ff897ff 1886 WARN_ON(!host->bus_dead);
39361851
AB
1887
1888 host->bus_ops = NULL;
1889}
1890
1891/*
1892 * Increase reference count of bus operator
1893 */
1894static inline void mmc_bus_get(struct mmc_host *host)
1895{
1896 unsigned long flags;
1897
1898 spin_lock_irqsave(&host->lock, flags);
1899 host->bus_refs++;
1900 spin_unlock_irqrestore(&host->lock, flags);
1901}
1902
1903/*
1904 * Decrease reference count of bus operator and free it if
1905 * it is the last reference.
1906 */
1907static inline void mmc_bus_put(struct mmc_host *host)
1908{
1909 unsigned long flags;
1910
1911 spin_lock_irqsave(&host->lock, flags);
1912 host->bus_refs--;
1913 if ((host->bus_refs == 0) && host->bus_ops)
1914 __mmc_release_bus(host);
1915 spin_unlock_irqrestore(&host->lock, flags);
1916}
1917
1da177e4 1918/*
7ea239d9
PO
1919 * Assign a mmc bus handler to a host. Only one bus handler may control a
1920 * host at any given time.
1da177e4 1921 */
7ea239d9 1922void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 1923{
7ea239d9 1924 unsigned long flags;
e45a1bd2 1925
d84075c8 1926 WARN_ON(!host->claimed);
bce40a36 1927
7ea239d9 1928 spin_lock_irqsave(&host->lock, flags);
bce40a36 1929
6ff897ff
SL
1930 WARN_ON(host->bus_ops);
1931 WARN_ON(host->bus_refs);
b57c43ad 1932
7ea239d9
PO
1933 host->bus_ops = ops;
1934 host->bus_refs = 1;
1935 host->bus_dead = 0;
b57c43ad 1936
7ea239d9 1937 spin_unlock_irqrestore(&host->lock, flags);
b57c43ad
PO
1938}
1939
7ea239d9 1940/*
7f7e4129 1941 * Remove the current bus handler from a host.
7ea239d9
PO
1942 */
1943void mmc_detach_bus(struct mmc_host *host)
7ccd266e 1944{
7ea239d9 1945 unsigned long flags;
7ccd266e 1946
d84075c8
PO
1947 WARN_ON(!host->claimed);
1948 WARN_ON(!host->bus_ops);
cd9277c0 1949
7ea239d9 1950 spin_lock_irqsave(&host->lock, flags);
7ccd266e 1951
7ea239d9 1952 host->bus_dead = 1;
7ccd266e 1953
7ea239d9 1954 spin_unlock_irqrestore(&host->lock, flags);
1da177e4 1955
7ea239d9 1956 mmc_bus_put(host);
1da177e4
LT
1957}
1958
bbd43682
UH
1959static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
1960 bool cd_irq)
1961{
bbd43682
UH
1962 /*
1963 * If the device is configured as wakeup, we prevent a new sleep for
1964 * 5 s to give provision for user space to consume the event.
1965 */
1966 if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
1967 device_can_wakeup(mmc_dev(host)))
1968 pm_wakeup_event(mmc_dev(host), 5000);
1969
1970 host->detect_change = 1;
1971 mmc_schedule_delayed_work(&host->detect, delay);
1972}
1973
1da177e4
LT
1974/**
1975 * mmc_detect_change - process change of state on a MMC socket
1976 * @host: host which changed state.
8dc00335 1977 * @delay: optional delay to wait before detection (jiffies)
1da177e4 1978 *
67a61c48
PO
1979 * MMC drivers should call this when they detect a card has been
1980 * inserted or removed. The MMC layer will confirm that any
1981 * present card is still functional, and initialize any newly
1982 * inserted.
1da177e4 1983 */
8dc00335 1984void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1985{
bbd43682 1986 _mmc_detect_change(host, delay, true);
1da177e4 1987}
1da177e4
LT
1988EXPORT_SYMBOL(mmc_detect_change);
1989
dfe86cba
AH
1990void mmc_init_erase(struct mmc_card *card)
1991{
1992 unsigned int sz;
1993
1994 if (is_power_of_2(card->erase_size))
1995 card->erase_shift = ffs(card->erase_size) - 1;
1996 else
1997 card->erase_shift = 0;
1998
1999 /*
2000 * It is possible to erase an arbitrarily large area of an SD or MMC
2001 * card. That is not desirable because it can take a long time
2002 * (minutes) potentially delaying more important I/O, and also the
2003 * timeout calculations become increasingly hugely over-estimated.
2004 * Consequently, 'pref_erase' is defined as a guide to limit erases
2005 * to that size and alignment.
2006 *
2007 * For SD cards that define Allocation Unit size, limit erases to one
c6d8fd61
GG
2008 * Allocation Unit at a time.
2009 * For MMC, have a stab at ai good value and for modern cards it will
2010 * end up being 4MiB. Note that if the value is too small, it can end
2011 * up taking longer to erase. Also note, erase_size is already set to
2012 * High Capacity Erase Size if available when this function is called.
dfe86cba
AH
2013 */
2014 if (mmc_card_sd(card) && card->ssr.au) {
2015 card->pref_erase = card->ssr.au;
2016 card->erase_shift = ffs(card->ssr.au) - 1;
cc8aa7de 2017 } else if (card->erase_size) {
dfe86cba
AH
2018 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
2019 if (sz < 128)
2020 card->pref_erase = 512 * 1024 / 512;
2021 else if (sz < 512)
2022 card->pref_erase = 1024 * 1024 / 512;
2023 else if (sz < 1024)
2024 card->pref_erase = 2 * 1024 * 1024 / 512;
2025 else
2026 card->pref_erase = 4 * 1024 * 1024 / 512;
2027 if (card->pref_erase < card->erase_size)
2028 card->pref_erase = card->erase_size;
2029 else {
2030 sz = card->pref_erase % card->erase_size;
2031 if (sz)
2032 card->pref_erase += card->erase_size - sz;
2033 }
cc8aa7de
CD
2034 } else
2035 card->pref_erase = 0;
dfe86cba
AH
2036}
2037
eaa02f75
AW
2038static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
2039 unsigned int arg, unsigned int qty)
dfe86cba
AH
2040{
2041 unsigned int erase_timeout;
2042
7194efb8
AH
2043 if (arg == MMC_DISCARD_ARG ||
2044 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
2045 erase_timeout = card->ext_csd.trim_timeout;
2046 } else if (card->ext_csd.erase_group_def & 1) {
dfe86cba
AH
2047 /* High Capacity Erase Group Size uses HC timeouts */
2048 if (arg == MMC_TRIM_ARG)
2049 erase_timeout = card->ext_csd.trim_timeout;
2050 else
2051 erase_timeout = card->ext_csd.hc_erase_timeout;
2052 } else {
2053 /* CSD Erase Group Size uses write timeout */
2054 unsigned int mult = (10 << card->csd.r2w_factor);
4406ae21 2055 unsigned int timeout_clks = card->csd.taac_clks * mult;
dfe86cba
AH
2056 unsigned int timeout_us;
2057
4406ae21
SL
2058 /* Avoid overflow: e.g. taac_ns=80000000 mult=1280 */
2059 if (card->csd.taac_ns < 1000000)
2060 timeout_us = (card->csd.taac_ns * mult) / 1000;
dfe86cba 2061 else
4406ae21 2062 timeout_us = (card->csd.taac_ns / 1000) * mult;
dfe86cba
AH
2063
2064 /*
2065 * ios.clock is only a target. The real clock rate might be
2066 * less but not that much less, so fudge it by multiplying by 2.
2067 */
2068 timeout_clks <<= 1;
2069 timeout_us += (timeout_clks * 1000) /
9eadcc05 2070 (card->host->ios.clock / 1000);
dfe86cba
AH
2071
2072 erase_timeout = timeout_us / 1000;
2073
2074 /*
2075 * Theoretically, the calculation could underflow so round up
2076 * to 1ms in that case.
2077 */
2078 if (!erase_timeout)
2079 erase_timeout = 1;
2080 }
2081
2082 /* Multiplier for secure operations */
2083 if (arg & MMC_SECURE_ARGS) {
2084 if (arg == MMC_SECURE_ERASE_ARG)
2085 erase_timeout *= card->ext_csd.sec_erase_mult;
2086 else
2087 erase_timeout *= card->ext_csd.sec_trim_mult;
2088 }
2089
2090 erase_timeout *= qty;
2091
2092 /*
2093 * Ensure at least a 1 second timeout for SPI as per
2094 * 'mmc_set_data_timeout()'
2095 */
2096 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
2097 erase_timeout = 1000;
2098
eaa02f75 2099 return erase_timeout;
dfe86cba
AH
2100}
2101
eaa02f75
AW
2102static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
2103 unsigned int arg,
2104 unsigned int qty)
dfe86cba 2105{
eaa02f75
AW
2106 unsigned int erase_timeout;
2107
dfe86cba
AH
2108 if (card->ssr.erase_timeout) {
2109 /* Erase timeout specified in SD Status Register (SSR) */
eaa02f75
AW
2110 erase_timeout = card->ssr.erase_timeout * qty +
2111 card->ssr.erase_offset;
dfe86cba
AH
2112 } else {
2113 /*
2114 * Erase timeout not specified in SD Status Register (SSR) so
2115 * use 250ms per write block.
2116 */
eaa02f75 2117 erase_timeout = 250 * qty;
dfe86cba
AH
2118 }
2119
2120 /* Must not be less than 1 second */
eaa02f75
AW
2121 if (erase_timeout < 1000)
2122 erase_timeout = 1000;
2123
2124 return erase_timeout;
dfe86cba
AH
2125}
2126
eaa02f75
AW
2127static unsigned int mmc_erase_timeout(struct mmc_card *card,
2128 unsigned int arg,
2129 unsigned int qty)
dfe86cba
AH
2130{
2131 if (mmc_card_sd(card))
eaa02f75 2132 return mmc_sd_erase_timeout(card, arg, qty);
dfe86cba 2133 else
eaa02f75 2134 return mmc_mmc_erase_timeout(card, arg, qty);
dfe86cba
AH
2135}
2136
2137static int mmc_do_erase(struct mmc_card *card, unsigned int from,
2138 unsigned int to, unsigned int arg)
2139{
c7836d15 2140 struct mmc_command cmd = {};
bb4eecf2
BW
2141 unsigned int qty = 0, busy_timeout = 0;
2142 bool use_r1b_resp = false;
8fee476b 2143 unsigned long timeout;
dfe86cba
AH
2144 int err;
2145
8f11d106
AH
2146 mmc_retune_hold(card->host);
2147
dfe86cba
AH
2148 /*
2149 * qty is used to calculate the erase timeout which depends on how many
2150 * erase groups (or allocation units in SD terminology) are affected.
2151 * We count erasing part of an erase group as one erase group.
2152 * For SD, the allocation units are always a power of 2. For MMC, the
2153 * erase group size is almost certainly also power of 2, but it does not
2154 * seem to insist on that in the JEDEC standard, so we fall back to
2155 * division in that case. SD may not specify an allocation unit size,
2156 * in which case the timeout is based on the number of write blocks.
2157 *
2158 * Note that the timeout for secure trim 2 will only be correct if the
2159 * number of erase groups specified is the same as the total of all
2160 * preceding secure trim 1 commands. Since the power may have been
2161 * lost since the secure trim 1 commands occurred, it is generally
2162 * impossible to calculate the secure trim 2 timeout correctly.
2163 */
2164 if (card->erase_shift)
2165 qty += ((to >> card->erase_shift) -
2166 (from >> card->erase_shift)) + 1;
2167 else if (mmc_card_sd(card))
2168 qty += to - from + 1;
2169 else
2170 qty += ((to / card->erase_size) -
2171 (from / card->erase_size)) + 1;
2172
2173 if (!mmc_card_blockaddr(card)) {
2174 from <<= 9;
2175 to <<= 9;
2176 }
2177
dfe86cba
AH
2178 if (mmc_card_sd(card))
2179 cmd.opcode = SD_ERASE_WR_BLK_START;
2180 else
2181 cmd.opcode = MMC_ERASE_GROUP_START;
2182 cmd.arg = from;
2183 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2184 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2185 if (err) {
a3c76eb9 2186 pr_err("mmc_erase: group start error %d, "
dfe86cba 2187 "status %#x\n", err, cmd.resp[0]);
67716327 2188 err = -EIO;
dfe86cba
AH
2189 goto out;
2190 }
2191
2192 memset(&cmd, 0, sizeof(struct mmc_command));
2193 if (mmc_card_sd(card))
2194 cmd.opcode = SD_ERASE_WR_BLK_END;
2195 else
2196 cmd.opcode = MMC_ERASE_GROUP_END;
2197 cmd.arg = to;
2198 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2199 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2200 if (err) {
a3c76eb9 2201 pr_err("mmc_erase: group end error %d, status %#x\n",
dfe86cba 2202 err, cmd.resp[0]);
67716327 2203 err = -EIO;
dfe86cba
AH
2204 goto out;
2205 }
2206
2207 memset(&cmd, 0, sizeof(struct mmc_command));
2208 cmd.opcode = MMC_ERASE;
2209 cmd.arg = arg;
bb4eecf2
BW
2210 busy_timeout = mmc_erase_timeout(card, arg, qty);
2211 /*
2212 * If the host controller supports busy signalling and the timeout for
2213 * the erase operation does not exceed the max_busy_timeout, we should
2214 * use R1B response. Or we need to prevent the host from doing hw busy
2215 * detection, which is done by converting to a R1 response instead.
2216 */
2217 if (card->host->max_busy_timeout &&
2218 busy_timeout > card->host->max_busy_timeout) {
2219 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2220 } else {
2221 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
2222 cmd.busy_timeout = busy_timeout;
2223 use_r1b_resp = true;
2224 }
2225
dfe86cba
AH
2226 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2227 if (err) {
a3c76eb9 2228 pr_err("mmc_erase: erase error %d, status %#x\n",
dfe86cba
AH
2229 err, cmd.resp[0]);
2230 err = -EIO;
2231 goto out;
2232 }
2233
2234 if (mmc_host_is_spi(card->host))
2235 goto out;
2236
bb4eecf2
BW
2237 /*
2238 * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling
2239 * shall be avoided.
2240 */
2241 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
2242 goto out;
2243
2244 timeout = jiffies + msecs_to_jiffies(busy_timeout);
dfe86cba
AH
2245 do {
2246 memset(&cmd, 0, sizeof(struct mmc_command));
2247 cmd.opcode = MMC_SEND_STATUS;
2248 cmd.arg = card->rca << 16;
2249 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2250 /* Do not retry else we can't see errors */
2251 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2252 if (err || (cmd.resp[0] & 0xFDF92000)) {
a3c76eb9 2253 pr_err("error %d requesting status %#x\n",
dfe86cba
AH
2254 err, cmd.resp[0]);
2255 err = -EIO;
2256 goto out;
2257 }
8fee476b
TR
2258
2259 /* Timeout if the device never becomes ready for data and
2260 * never leaves the program state.
2261 */
2262 if (time_after(jiffies, timeout)) {
2263 pr_err("%s: Card stuck in programming state! %s\n",
2264 mmc_hostname(card->host), __func__);
2265 err = -EIO;
2266 goto out;
2267 }
2268
dfe86cba 2269 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
8fee476b 2270 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
dfe86cba 2271out:
8f11d106 2272 mmc_retune_release(card->host);
dfe86cba
AH
2273 return err;
2274}
2275
71085123
BW
2276static unsigned int mmc_align_erase_size(struct mmc_card *card,
2277 unsigned int *from,
2278 unsigned int *to,
2279 unsigned int nr)
2280{
2281 unsigned int from_new = *from, nr_new = nr, rem;
2282
6c689886
BW
2283 /*
2284 * When the 'card->erase_size' is power of 2, we can use round_up/down()
2285 * to align the erase size efficiently.
2286 */
2287 if (is_power_of_2(card->erase_size)) {
2288 unsigned int temp = from_new;
2289
2290 from_new = round_up(temp, card->erase_size);
2291 rem = from_new - temp;
2292
71085123
BW
2293 if (nr_new > rem)
2294 nr_new -= rem;
2295 else
2296 return 0;
71085123 2297
6c689886
BW
2298 nr_new = round_down(nr_new, card->erase_size);
2299 } else {
2300 rem = from_new % card->erase_size;
2301 if (rem) {
2302 rem = card->erase_size - rem;
2303 from_new += rem;
2304 if (nr_new > rem)
2305 nr_new -= rem;
2306 else
2307 return 0;
2308 }
2309
2310 rem = nr_new % card->erase_size;
2311 if (rem)
2312 nr_new -= rem;
2313 }
71085123
BW
2314
2315 if (nr_new == 0)
2316 return 0;
2317
2318 *to = from_new + nr_new;
2319 *from = from_new;
2320
2321 return nr_new;
2322}
2323
dfe86cba
AH
2324/**
2325 * mmc_erase - erase sectors.
2326 * @card: card to erase
2327 * @from: first sector to erase
2328 * @nr: number of sectors to erase
2329 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2330 *
2331 * Caller must claim host before calling this function.
2332 */
2333int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2334 unsigned int arg)
2335{
2336 unsigned int rem, to = from + nr;
642c28ab 2337 int err;
dfe86cba
AH
2338
2339 if (!(card->host->caps & MMC_CAP_ERASE) ||
2340 !(card->csd.cmdclass & CCC_ERASE))
2341 return -EOPNOTSUPP;
2342
2343 if (!card->erase_size)
2344 return -EOPNOTSUPP;
2345
2346 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2347 return -EOPNOTSUPP;
2348
2349 if ((arg & MMC_SECURE_ARGS) &&
2350 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2351 return -EOPNOTSUPP;
2352
2353 if ((arg & MMC_TRIM_ARGS) &&
2354 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2355 return -EOPNOTSUPP;
2356
2357 if (arg == MMC_SECURE_ERASE_ARG) {
2358 if (from % card->erase_size || nr % card->erase_size)
2359 return -EINVAL;
2360 }
2361
71085123
BW
2362 if (arg == MMC_ERASE_ARG)
2363 nr = mmc_align_erase_size(card, &from, &to, nr);
dfe86cba
AH
2364
2365 if (nr == 0)
2366 return 0;
2367
dfe86cba
AH
2368 if (to <= from)
2369 return -EINVAL;
2370
2371 /* 'from' and 'to' are inclusive */
2372 to -= 1;
2373
642c28ab
DJ
2374 /*
2375 * Special case where only one erase-group fits in the timeout budget:
2376 * If the region crosses an erase-group boundary on this particular
2377 * case, we will be trimming more than one erase-group which, does not
2378 * fit in the timeout budget of the controller, so we need to split it
2379 * and call mmc_do_erase() twice if necessary. This special case is
2380 * identified by the card->eg_boundary flag.
2381 */
22d7e85f
RG
2382 rem = card->erase_size - (from % card->erase_size);
2383 if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
642c28ab
DJ
2384 err = mmc_do_erase(card, from, from + rem - 1, arg);
2385 from += rem;
2386 if ((err) || (to <= from))
2387 return err;
2388 }
2389
dfe86cba
AH
2390 return mmc_do_erase(card, from, to, arg);
2391}
2392EXPORT_SYMBOL(mmc_erase);
2393
2394int mmc_can_erase(struct mmc_card *card)
2395{
2396 if ((card->host->caps & MMC_CAP_ERASE) &&
2397 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2398 return 1;
2399 return 0;
2400}
2401EXPORT_SYMBOL(mmc_can_erase);
2402
2403int mmc_can_trim(struct mmc_card *card)
2404{
b5b4ff0a
SL
2405 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
2406 (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
dfe86cba
AH
2407 return 1;
2408 return 0;
2409}
2410EXPORT_SYMBOL(mmc_can_trim);
2411
b3bf9153
KP
2412int mmc_can_discard(struct mmc_card *card)
2413{
2414 /*
2415 * As there's no way to detect the discard support bit at v4.5
2416 * use the s/w feature support filed.
2417 */
2418 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2419 return 1;
2420 return 0;
2421}
2422EXPORT_SYMBOL(mmc_can_discard);
2423
d9ddd629
KP
2424int mmc_can_sanitize(struct mmc_card *card)
2425{
28302812
AH
2426 if (!mmc_can_trim(card) && !mmc_can_erase(card))
2427 return 0;
d9ddd629
KP
2428 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2429 return 1;
2430 return 0;
2431}
2432EXPORT_SYMBOL(mmc_can_sanitize);
2433
dfe86cba
AH
2434int mmc_can_secure_erase_trim(struct mmc_card *card)
2435{
5204d00f
LC
2436 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
2437 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
dfe86cba
AH
2438 return 1;
2439 return 0;
2440}
2441EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2442
2443int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2444 unsigned int nr)
2445{
2446 if (!card->erase_size)
2447 return 0;
2448 if (from % card->erase_size || nr % card->erase_size)
2449 return 0;
2450 return 1;
2451}
2452EXPORT_SYMBOL(mmc_erase_group_aligned);
1da177e4 2453
e056a1b5
AH
2454static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2455 unsigned int arg)
2456{
2457 struct mmc_host *host = card->host;
bb4eecf2 2458 unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
e056a1b5 2459 unsigned int last_timeout = 0;
12182aff
UH
2460 unsigned int max_busy_timeout = host->max_busy_timeout ?
2461 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
e056a1b5 2462
bb4eecf2 2463 if (card->erase_shift) {
e056a1b5 2464 max_qty = UINT_MAX >> card->erase_shift;
bb4eecf2
BW
2465 min_qty = card->pref_erase >> card->erase_shift;
2466 } else if (mmc_card_sd(card)) {
e056a1b5 2467 max_qty = UINT_MAX;
bb4eecf2
BW
2468 min_qty = card->pref_erase;
2469 } else {
e056a1b5 2470 max_qty = UINT_MAX / card->erase_size;
bb4eecf2
BW
2471 min_qty = card->pref_erase / card->erase_size;
2472 }
e056a1b5 2473
bb4eecf2
BW
2474 /*
2475 * We should not only use 'host->max_busy_timeout' as the limitation
2476 * when deciding the max discard sectors. We should set a balance value
2477 * to improve the erase speed, and it can not get too long timeout at
2478 * the same time.
2479 *
2480 * Here we set 'card->pref_erase' as the minimal discard sectors no
2481 * matter what size of 'host->max_busy_timeout', but if the
2482 * 'host->max_busy_timeout' is large enough for more discard sectors,
2483 * then we can continue to increase the max discard sectors until we
12182aff
UH
2484 * get a balance value. In cases when the 'host->max_busy_timeout'
2485 * isn't specified, use the default max erase timeout.
bb4eecf2 2486 */
e056a1b5
AH
2487 do {
2488 y = 0;
2489 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2490 timeout = mmc_erase_timeout(card, arg, qty + x);
bb4eecf2 2491
12182aff 2492 if (qty + x > min_qty && timeout > max_busy_timeout)
e056a1b5 2493 break;
bb4eecf2 2494
e056a1b5
AH
2495 if (timeout < last_timeout)
2496 break;
2497 last_timeout = timeout;
2498 y = x;
2499 }
2500 qty += y;
2501 } while (y);
2502
2503 if (!qty)
2504 return 0;
2505
642c28ab
DJ
2506 /*
2507 * When specifying a sector range to trim, chances are we might cross
2508 * an erase-group boundary even if the amount of sectors is less than
2509 * one erase-group.
2510 * If we can only fit one erase-group in the controller timeout budget,
2511 * we have to care that erase-group boundaries are not crossed by a
2512 * single trim operation. We flag that special case with "eg_boundary".
2513 * In all other cases we can just decrement qty and pretend that we
2514 * always touch (qty + 1) erase-groups as a simple optimization.
2515 */
e056a1b5 2516 if (qty == 1)
642c28ab
DJ
2517 card->eg_boundary = 1;
2518 else
2519 qty--;
e056a1b5
AH
2520
2521 /* Convert qty to sectors */
2522 if (card->erase_shift)
642c28ab 2523 max_discard = qty << card->erase_shift;
e056a1b5 2524 else if (mmc_card_sd(card))
642c28ab 2525 max_discard = qty + 1;
e056a1b5 2526 else
642c28ab 2527 max_discard = qty * card->erase_size;
e056a1b5
AH
2528
2529 return max_discard;
2530}
2531
2532unsigned int mmc_calc_max_discard(struct mmc_card *card)
2533{
2534 struct mmc_host *host = card->host;
2535 unsigned int max_discard, max_trim;
2536
e056a1b5
AH
2537 /*
2538 * Without erase_group_def set, MMC erase timeout depends on clock
2539 * frequence which can change. In that case, the best choice is
2540 * just the preferred erase size.
2541 */
2542 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2543 return card->pref_erase;
2544
2545 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2546 if (mmc_can_trim(card)) {
2547 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2548 if (max_trim < max_discard)
2549 max_discard = max_trim;
2550 } else if (max_discard < card->erase_size) {
2551 max_discard = 0;
2552 }
2553 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
12182aff
UH
2554 mmc_hostname(host), max_discard, host->max_busy_timeout ?
2555 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
e056a1b5
AH
2556 return max_discard;
2557}
2558EXPORT_SYMBOL(mmc_calc_max_discard);
2559
33e6d74d
UH
2560bool mmc_card_is_blockaddr(struct mmc_card *card)
2561{
2562 return card ? mmc_card_blockaddr(card) : false;
2563}
2564EXPORT_SYMBOL(mmc_card_is_blockaddr);
2565
0f8d8ea6
AH
2566int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2567{
c7836d15 2568 struct mmc_command cmd = {};
0f8d8ea6 2569
1712c937
ZX
2570 if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
2571 mmc_card_hs400(card) || mmc_card_hs400es(card))
0f8d8ea6
AH
2572 return 0;
2573
0f8d8ea6
AH
2574 cmd.opcode = MMC_SET_BLOCKLEN;
2575 cmd.arg = blocklen;
2576 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2577 return mmc_wait_for_cmd(card->host, &cmd, 5);
2578}
2579EXPORT_SYMBOL(mmc_set_blocklen);
2580
67c79db8
LP
2581int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2582 bool is_rel_write)
2583{
c7836d15 2584 struct mmc_command cmd = {};
67c79db8
LP
2585
2586 cmd.opcode = MMC_SET_BLOCK_COUNT;
2587 cmd.arg = blockcount & 0x0000FFFF;
2588 if (is_rel_write)
2589 cmd.arg |= 1 << 31;
2590 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2591 return mmc_wait_for_cmd(card->host, &cmd, 5);
2592}
2593EXPORT_SYMBOL(mmc_set_blockcount);
2594
b2499518
AH
2595static void mmc_hw_reset_for_init(struct mmc_host *host)
2596{
52c8212d
UH
2597 mmc_pwrseq_reset(host);
2598
b2499518
AH
2599 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2600 return;
b2499518 2601 host->ops->hw_reset(host);
b2499518
AH
2602}
2603
83533ab2 2604int mmc_hw_reset(struct mmc_host *host)
b2499518 2605{
f855a371 2606 int ret;
b2499518 2607
f855a371 2608 if (!host->card)
b2499518
AH
2609 return -EINVAL;
2610
f855a371
JR
2611 mmc_bus_get(host);
2612 if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
2613 mmc_bus_put(host);
b2499518 2614 return -EOPNOTSUPP;
b2499518
AH
2615 }
2616
f855a371
JR
2617 ret = host->bus_ops->reset(host);
2618 mmc_bus_put(host);
b2499518 2619
4e6c7178
GG
2620 if (ret)
2621 pr_warn("%s: tried to reset card, got error %d\n",
2622 mmc_hostname(host), ret);
b2499518 2623
f855a371 2624 return ret;
b2499518 2625}
b2499518
AH
2626EXPORT_SYMBOL(mmc_hw_reset);
2627
807e8e40
AR
2628static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2629{
2630 host->f_init = freq;
2631
69f25f9b 2632 pr_debug("%s: %s: trying to init card at %u Hz\n",
807e8e40 2633 mmc_hostname(host), __func__, host->f_init);
69f25f9b 2634
4a065193 2635 mmc_power_up(host, host->ocr_avail);
2f94e55a 2636
b2499518
AH
2637 /*
2638 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2639 * do a hardware reset if possible.
2640 */
2641 mmc_hw_reset_for_init(host);
2642
2f94e55a
PR
2643 /*
2644 * sdio_reset sends CMD52 to reset card. Since we do not know
2645 * if the card is being re-initialized, just send it. CMD52
2646 * should be ignored by SD/eMMC cards.
100a606d 2647 * Skip it if we already know that we do not support SDIO commands
2f94e55a 2648 */
100a606d
CC
2649 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
2650 sdio_reset(host);
2651
807e8e40
AR
2652 mmc_go_idle(host);
2653
1b8d79c5
UH
2654 if (!(host->caps2 & MMC_CAP2_NO_SD))
2655 mmc_send_if_cond(host, host->ocr_avail);
807e8e40
AR
2656
2657 /* Order's important: probe SDIO, then SD, then MMC */
100a606d
CC
2658 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
2659 if (!mmc_attach_sdio(host))
2660 return 0;
2661
1b8d79c5
UH
2662 if (!(host->caps2 & MMC_CAP2_NO_SD))
2663 if (!mmc_attach_sd(host))
2664 return 0;
2665
a0c3b68c
SL
2666 if (!(host->caps2 & MMC_CAP2_NO_MMC))
2667 if (!mmc_attach_mmc(host))
2668 return 0;
807e8e40
AR
2669
2670 mmc_power_off(host);
2671 return -EIO;
2672}
2673
d3049504
AH
2674int _mmc_detect_card_removed(struct mmc_host *host)
2675{
2676 int ret;
2677
d3049504
AH
2678 if (!host->card || mmc_card_removed(host->card))
2679 return 1;
2680
2681 ret = host->bus_ops->alive(host);
1450734e
KL
2682
2683 /*
2684 * Card detect status and alive check may be out of sync if card is
2685 * removed slowly, when card detect switch changes while card/slot
2686 * pads are still contacted in hardware (refer to "SD Card Mechanical
2687 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2688 * detect work 200ms later for this case.
2689 */
2690 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2691 mmc_detect_change(host, msecs_to_jiffies(200));
2692 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2693 }
2694
d3049504
AH
2695 if (ret) {
2696 mmc_card_set_removed(host->card);
2697 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2698 }
2699
2700 return ret;
2701}
2702
2703int mmc_detect_card_removed(struct mmc_host *host)
2704{
2705 struct mmc_card *card = host->card;
f0cc9cf9 2706 int ret;
d3049504
AH
2707
2708 WARN_ON(!host->claimed);
f0cc9cf9
UH
2709
2710 if (!card)
2711 return 1;
2712
6067bafe 2713 if (!mmc_card_is_removable(host))
1ff2575b
UH
2714 return 0;
2715
f0cc9cf9 2716 ret = mmc_card_removed(card);
d3049504
AH
2717 /*
2718 * The card will be considered unchanged unless we have been asked to
2719 * detect a change or host requires polling to provide card detection.
2720 */
b6891679 2721 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
f0cc9cf9 2722 return ret;
d3049504
AH
2723
2724 host->detect_change = 0;
f0cc9cf9
UH
2725 if (!ret) {
2726 ret = _mmc_detect_card_removed(host);
b6891679 2727 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
f0cc9cf9
UH
2728 /*
2729 * Schedule a detect work as soon as possible to let a
2730 * rescan handle the card removal.
2731 */
2732 cancel_delayed_work(&host->detect);
bbd43682 2733 _mmc_detect_change(host, 0, false);
f0cc9cf9
UH
2734 }
2735 }
d3049504 2736
f0cc9cf9 2737 return ret;
d3049504
AH
2738}
2739EXPORT_SYMBOL(mmc_detect_card_removed);
2740
b93931a6 2741void mmc_rescan(struct work_struct *work)
1da177e4 2742{
c4028958
DH
2743 struct mmc_host *host =
2744 container_of(work, struct mmc_host, detect.work);
88ae8b86 2745 int i;
4c2ef25f 2746
807e8e40 2747 if (host->rescan_disable)
4c2ef25f 2748 return;
1da177e4 2749
3339d1e3 2750 /* If there is a non-removable card registered, only scan once */
6067bafe 2751 if (!mmc_card_is_removable(host) && host->rescan_entered)
3339d1e3
JR
2752 return;
2753 host->rescan_entered = 1;
2754
86236813 2755 if (host->trigger_card_event && host->ops->card_event) {
d234d212 2756 mmc_claim_host(host);
86236813 2757 host->ops->card_event(host);
d234d212 2758 mmc_release_host(host);
86236813
UH
2759 host->trigger_card_event = false;
2760 }
2761
7ea239d9 2762 mmc_bus_get(host);
b855885e 2763
30201e7f
OBC
2764 /*
2765 * if there is a _removable_ card registered, check whether it is
2766 * still present
2767 */
6067bafe 2768 if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host))
94d89efb
JS
2769 host->bus_ops->detect(host);
2770
d3049504
AH
2771 host->detect_change = 0;
2772
c5841798
CB
2773 /*
2774 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2775 * the card is no longer present.
2776 */
94d89efb 2777 mmc_bus_put(host);
94d89efb
JS
2778 mmc_bus_get(host);
2779
2780 /* if there still is a card present, stop here */
2781 if (host->bus_ops != NULL) {
7ea239d9 2782 mmc_bus_put(host);
94d89efb
JS
2783 goto out;
2784 }
1da177e4 2785
94d89efb
JS
2786 /*
2787 * Only we can add a new handler, so it's safe to
2788 * release the lock here.
2789 */
2790 mmc_bus_put(host);
1da177e4 2791
d234d212 2792 mmc_claim_host(host);
6067bafe 2793 if (mmc_card_is_removable(host) && host->ops->get_cd &&
c1b55bfc 2794 host->ops->get_cd(host) == 0) {
fa550189
UH
2795 mmc_power_off(host);
2796 mmc_release_host(host);
94d89efb 2797 goto out;
fa550189 2798 }
1da177e4 2799
88ae8b86 2800 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
807e8e40
AR
2801 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2802 break;
06b2233a 2803 if (freqs[i] <= host->f_min)
807e8e40 2804 break;
88ae8b86 2805 }
807e8e40
AR
2806 mmc_release_host(host);
2807
2808 out:
28f52482
AV
2809 if (host->caps & MMC_CAP_NEEDS_POLL)
2810 mmc_schedule_delayed_work(&host->detect, HZ);
1da177e4
LT
2811}
2812
b93931a6 2813void mmc_start_host(struct mmc_host *host)
1da177e4 2814{
fa550189 2815 host->f_init = max(freqs[0], host->f_min);
d9adcc12 2816 host->rescan_disable = 0;
8af465db 2817 host->ios.power_mode = MMC_POWER_UNDEFINED;
8d1ffc8c 2818
c2c24819
UH
2819 if (!(host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)) {
2820 mmc_claim_host(host);
4a065193 2821 mmc_power_up(host, host->ocr_avail);
c2c24819
UH
2822 mmc_release_host(host);
2823 }
8d1ffc8c 2824
740a221e 2825 mmc_gpiod_request_cd_irq(host);
bbd43682 2826 _mmc_detect_change(host, 0, false);
1da177e4
LT
2827}
2828
b93931a6 2829void mmc_stop_host(struct mmc_host *host)
1da177e4 2830{
03dbaa04
AH
2831 if (host->slot.cd_irq >= 0) {
2832 if (host->slot.cd_wake_enabled)
2833 disable_irq_wake(host->slot.cd_irq);
740a221e 2834 disable_irq(host->slot.cd_irq);
03dbaa04 2835 }
3b91e550 2836
d9adcc12 2837 host->rescan_disable = 1;
d9bcbf34 2838 cancel_delayed_work_sync(&host->detect);
3b91e550 2839
da68c4eb
NP
2840 /* clear pm flags now and let card drivers set them as needed */
2841 host->pm_flags = 0;
2842
7ea239d9
PO
2843 mmc_bus_get(host);
2844 if (host->bus_ops && !host->bus_dead) {
0db13fc2 2845 /* Calling bus_ops->remove() with a claimed host can deadlock */
58a8a4a1 2846 host->bus_ops->remove(host);
7ea239d9
PO
2847 mmc_claim_host(host);
2848 mmc_detach_bus(host);
7f7e4129 2849 mmc_power_off(host);
7ea239d9 2850 mmc_release_host(host);
53509f0f
DK
2851 mmc_bus_put(host);
2852 return;
1da177e4 2853 }
7ea239d9
PO
2854 mmc_bus_put(host);
2855
8d1ffc8c 2856 mmc_claim_host(host);
1da177e4 2857 mmc_power_off(host);
8d1ffc8c 2858 mmc_release_host(host);
1da177e4
LT
2859}
2860
12ae637f 2861int mmc_power_save_host(struct mmc_host *host)
eae1aeee 2862{
12ae637f
OBC
2863 int ret = 0;
2864
69f25f9b 2865 pr_debug("%s: %s: powering down\n", mmc_hostname(host), __func__);
bb9cab94 2866
eae1aeee
AH
2867 mmc_bus_get(host);
2868
5601aaf7 2869 if (!host->bus_ops || host->bus_dead) {
eae1aeee 2870 mmc_bus_put(host);
12ae637f 2871 return -EINVAL;
eae1aeee
AH
2872 }
2873
2874 if (host->bus_ops->power_save)
12ae637f 2875 ret = host->bus_ops->power_save(host);
eae1aeee
AH
2876
2877 mmc_bus_put(host);
2878
2879 mmc_power_off(host);
12ae637f
OBC
2880
2881 return ret;
eae1aeee
AH
2882}
2883EXPORT_SYMBOL(mmc_power_save_host);
2884
12ae637f 2885int mmc_power_restore_host(struct mmc_host *host)
eae1aeee 2886{
12ae637f
OBC
2887 int ret;
2888
69f25f9b 2889 pr_debug("%s: %s: powering up\n", mmc_hostname(host), __func__);
bb9cab94 2890
eae1aeee
AH
2891 mmc_bus_get(host);
2892
5601aaf7 2893 if (!host->bus_ops || host->bus_dead) {
eae1aeee 2894 mmc_bus_put(host);
12ae637f 2895 return -EINVAL;
eae1aeee
AH
2896 }
2897
69041150 2898 mmc_power_up(host, host->card->ocr);
12ae637f 2899 ret = host->bus_ops->power_restore(host);
eae1aeee
AH
2900
2901 mmc_bus_put(host);
12ae637f
OBC
2902
2903 return ret;
eae1aeee
AH
2904}
2905EXPORT_SYMBOL(mmc_power_restore_host);
2906
8dede18e 2907#ifdef CONFIG_PM_SLEEP
4c2ef25f
ML
2908/* Do the card removal on suspend if card is assumed removeable
2909 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2910 to sync the card.
2911*/
8dede18e
UH
2912static int mmc_pm_notify(struct notifier_block *notify_block,
2913 unsigned long mode, void *unused)
4c2ef25f
ML
2914{
2915 struct mmc_host *host = container_of(
2916 notify_block, struct mmc_host, pm_notify);
2917 unsigned long flags;
810caddb 2918 int err = 0;
4c2ef25f
ML
2919
2920 switch (mode) {
2921 case PM_HIBERNATION_PREPARE:
2922 case PM_SUSPEND_PREPARE:
184af16b 2923 case PM_RESTORE_PREPARE:
4c2ef25f
ML
2924 spin_lock_irqsave(&host->lock, flags);
2925 host->rescan_disable = 1;
2926 spin_unlock_irqrestore(&host->lock, flags);
2927 cancel_delayed_work_sync(&host->detect);
2928
810caddb
UH
2929 if (!host->bus_ops)
2930 break;
2931
2932 /* Validate prerequisites for suspend */
2933 if (host->bus_ops->pre_suspend)
2934 err = host->bus_ops->pre_suspend(host);
5601aaf7 2935 if (!err)
4c2ef25f
ML
2936 break;
2937
0db13fc2 2938 /* Calling bus_ops->remove() with a claimed host can deadlock */
58a8a4a1 2939 host->bus_ops->remove(host);
0db13fc2 2940 mmc_claim_host(host);
4c2ef25f 2941 mmc_detach_bus(host);
7f7e4129 2942 mmc_power_off(host);
4c2ef25f
ML
2943 mmc_release_host(host);
2944 host->pm_flags = 0;
2945 break;
2946
2947 case PM_POST_SUSPEND:
2948 case PM_POST_HIBERNATION:
274476f8 2949 case PM_POST_RESTORE:
4c2ef25f
ML
2950
2951 spin_lock_irqsave(&host->lock, flags);
2952 host->rescan_disable = 0;
2953 spin_unlock_irqrestore(&host->lock, flags);
bbd43682 2954 _mmc_detect_change(host, 0, false);
4c2ef25f
ML
2955
2956 }
2957
2958 return 0;
2959}
8dede18e
UH
2960
2961void mmc_register_pm_notifier(struct mmc_host *host)
2962{
2963 host->pm_notify.notifier_call = mmc_pm_notify;
2964 register_pm_notifier(&host->pm_notify);
2965}
2966
2967void mmc_unregister_pm_notifier(struct mmc_host *host)
2968{
2969 unregister_pm_notifier(&host->pm_notify);
2970}
1da177e4
LT
2971#endif
2972
2220eedf
KD
2973/**
2974 * mmc_init_context_info() - init synchronization context
2975 * @host: mmc host
2976 *
2977 * Init struct context_info needed to implement asynchronous
2978 * request mechanism, used by mmc core, host driver and mmc requests
2979 * supplier.
2980 */
2981void mmc_init_context_info(struct mmc_host *host)
2982{
2220eedf
KD
2983 host->context_info.is_new_req = false;
2984 host->context_info.is_done_rcv = false;
2985 host->context_info.is_waiting_last_req = false;
2986 init_waitqueue_head(&host->context_info.wait);
2987}
2988
ffce2e7e
PO
2989static int __init mmc_init(void)
2990{
2991 int ret;
2992
ffce2e7e 2993 ret = mmc_register_bus();
e29a7d73 2994 if (ret)
520bd7a8 2995 return ret;
e29a7d73
PO
2996
2997 ret = mmc_register_host_class();
2998 if (ret)
2999 goto unregister_bus;
3000
3001 ret = sdio_register_bus();
3002 if (ret)
3003 goto unregister_host_class;
3004
3005 return 0;
3006
3007unregister_host_class:
3008 mmc_unregister_host_class();
3009unregister_bus:
3010 mmc_unregister_bus();
ffce2e7e
PO
3011 return ret;
3012}
3013
3014static void __exit mmc_exit(void)
3015{
e29a7d73 3016 sdio_unregister_bus();
ffce2e7e
PO
3017 mmc_unregister_host_class();
3018 mmc_unregister_bus();
ffce2e7e
PO
3019}
3020
26074962 3021subsys_initcall(mmc_init);
ffce2e7e
PO
3022module_exit(mmc_exit);
3023
1da177e4 3024MODULE_LICENSE("GPL");