Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / drivers / mmc / core / core.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
aaac1b47 3 * linux/drivers/mmc/core/core.c
1da177e4
LT
4 *
5 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
5b4fd9ae 6 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
ad3868b2 7 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
bce40a36 8 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
1da177e4 9 */
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/interrupt.h>
13#include <linux/completion.h>
14#include <linux/device.h>
15#include <linux/delay.h>
16#include <linux/pagemap.h>
17#include <linux/err.h>
af8350c7 18#include <linux/leds.h>
b57c43ad 19#include <linux/scatterlist.h>
86e8286a 20#include <linux/log2.h>
e594573d 21#include <linux/pm_runtime.h>
35eb6db1 22#include <linux/suspend.h>
1b676f70
PF
23#include <linux/fault-inject.h>
24#include <linux/random.h>
950d56ac 25#include <linux/slab.h>
6e9e318b 26#include <linux/of.h>
1da177e4
LT
27
28#include <linux/mmc/card.h>
29#include <linux/mmc/host.h>
da7fbe58
PO
30#include <linux/mmc/mmc.h>
31#include <linux/mmc/sd.h>
740a221e 32#include <linux/mmc/slot-gpio.h>
1da177e4 33
7962fc37
BW
34#define CREATE_TRACE_POINTS
35#include <trace/events/mmc.h>
36
aaac1b47 37#include "core.h"
4facdde1 38#include "card.h"
93f1c150 39#include "crypto.h"
ffce2e7e
PO
40#include "bus.h"
41#include "host.h"
e29a7d73 42#include "sdio_bus.h"
3aa8793f 43#include "pwrseq.h"
da7fbe58
PO
44
45#include "mmc_ops.h"
46#include "sd_ops.h"
5c4e6f13 47#include "sdio_ops.h"
1da177e4 48
12182aff
UH
49/* The max erase timeout, used when host->max_busy_timeout isn't specified */
50#define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */
ad9be7ff 51#define SD_DISCARD_TIMEOUT_MS (250)
12182aff 52
fa550189 53static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
ffce2e7e 54
af517150
DB
55/*
56 * Enabling software CRCs on the data blocks can be a significant (30%)
57 * performance cost, and for other reasons may not always be desired.
ff50df9a 58 * So we allow it to be disabled.
af517150 59 */
90ab5ee9 60bool use_spi_crc = 1;
af517150
DB
61module_param(use_spi_crc, bool, 0);
62
ffce2e7e
PO
63static int mmc_schedule_delayed_work(struct delayed_work *work,
64 unsigned long delay)
65{
520bd7a8
UH
66 /*
67 * We use the system_freezable_wq, because of two reasons.
68 * First, it allows several works (not the same work item) to be
69 * executed simultaneously. Second, the queue becomes frozen when
70 * userspace becomes frozen during system PM.
71 */
72 return queue_delayed_work(system_freezable_wq, work, delay);
ffce2e7e
PO
73}
74
1b676f70
PF
75#ifdef CONFIG_FAIL_MMC_REQUEST
76
77/*
78 * Internal function. Inject random data errors.
79 * If mmc_data is NULL no errors are injected.
80 */
81static void mmc_should_fail_request(struct mmc_host *host,
82 struct mmc_request *mrq)
83{
84 struct mmc_command *cmd = mrq->cmd;
85 struct mmc_data *data = mrq->data;
86 static const int data_errors[] = {
87 -ETIMEDOUT,
88 -EILSEQ,
89 -EIO,
90 };
91
92 if (!data)
93 return;
94
e5723f95 95 if ((cmd && cmd->error) || data->error ||
1b676f70
PF
96 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
97 return;
98
8032bf12
JD
99 data->error = data_errors[get_random_u32_below(ARRAY_SIZE(data_errors))];
100 data->bytes_xfered = get_random_u32_below(data->bytes_xfered >> 9) << 9;
1b676f70
PF
101}
102
103#else /* CONFIG_FAIL_MMC_REQUEST */
104
105static inline void mmc_should_fail_request(struct mmc_host *host,
106 struct mmc_request *mrq)
107{
108}
109
110#endif /* CONFIG_FAIL_MMC_REQUEST */
111
5163af5a
AH
112static inline void mmc_complete_cmd(struct mmc_request *mrq)
113{
114 if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion))
115 complete_all(&mrq->cmd_completion);
116}
117
118void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq)
119{
120 if (!mrq->cap_cmd_during_tfr)
121 return;
122
123 mmc_complete_cmd(mrq);
124
125 pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n",
126 mmc_hostname(host), mrq->cmd->opcode);
127}
128EXPORT_SYMBOL(mmc_command_done);
129
1da177e4 130/**
fe10c6ab
RK
131 * mmc_request_done - finish processing an MMC request
132 * @host: MMC host which completed request
133 * @mrq: MMC request which request
1da177e4
LT
134 *
135 * MMC drivers should call this function when they have completed
fe10c6ab 136 * their processing of a request.
1da177e4
LT
137 */
138void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
139{
140 struct mmc_command *cmd = mrq->cmd;
920e70c5
RK
141 int err = cmd->error;
142
bd11e8bd 143 /* Flag re-tuning needed on CRC errors */
b98e7e8d 144 if (!mmc_op_tuning(cmd->opcode) &&
0a55f4ab 145 !host->retune_crc_disable &&
031277d4 146 (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) ||
bd11e8bd 147 (mrq->data && mrq->data->error == -EILSEQ) ||
031277d4 148 (mrq->stop && mrq->stop->error == -EILSEQ)))
bd11e8bd
AH
149 mmc_retune_needed(host);
150
af517150
DB
151 if (err && cmd->retries && mmc_host_is_spi(host)) {
152 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
153 cmd->retries = 0;
154 }
155
5163af5a
AH
156 if (host->ongoing_mrq == mrq)
157 host->ongoing_mrq = NULL;
158
159 mmc_complete_cmd(mrq);
160
7962fc37
BW
161 trace_mmc_request_done(host, mrq);
162
67b8360a
LW
163 /*
164 * We list various conditions for the command to be considered
165 * properly done:
166 *
167 * - There was no error, OK fine then
168 * - We are not doing some kind of retry
169 * - The card was removed (...so just complete everything no matter
170 * if there are errors or retries)
171 */
172 if (!err || !cmd->retries || mmc_card_removed(host->card)) {
1b676f70
PF
173 mmc_should_fail_request(host, mrq);
174
5163af5a
AH
175 if (!host->ongoing_mrq)
176 led_trigger_event(host->led, LED_OFF);
af8350c7 177
fc75b708
AG
178 if (mrq->sbc) {
179 pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n",
180 mmc_hostname(host), mrq->sbc->opcode,
181 mrq->sbc->error,
182 mrq->sbc->resp[0], mrq->sbc->resp[1],
183 mrq->sbc->resp[2], mrq->sbc->resp[3]);
184 }
185
e4d21708
PO
186 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
187 mmc_hostname(host), cmd->opcode, err,
188 cmd->resp[0], cmd->resp[1],
189 cmd->resp[2], cmd->resp[3]);
190
191 if (mrq->data) {
192 pr_debug("%s: %d bytes transferred: %d\n",
193 mmc_hostname(host),
194 mrq->data->bytes_xfered, mrq->data->error);
195 }
196
197 if (mrq->stop) {
198 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
199 mmc_hostname(host), mrq->stop->opcode,
200 mrq->stop->error,
201 mrq->stop->resp[0], mrq->stop->resp[1],
202 mrq->stop->resp[2], mrq->stop->resp[3]);
203 }
1da177e4 204 }
67b8360a
LW
205 /*
206 * Request starter must handle retries - see
207 * mmc_wait_for_req_done().
208 */
209 if (mrq->done)
210 mrq->done(mrq);
1da177e4
LT
211}
212
213EXPORT_SYMBOL(mmc_request_done);
214
90a81489
AH
215static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
216{
217 int err;
218
219 /* Assumes host controller has been runtime resumed by mmc_claim_host */
220 err = mmc_retune(host);
221 if (err) {
222 mrq->cmd->error = err;
223 mmc_request_done(host, mrq);
224 return;
225 }
226
5d3f6ef0
HG
227 /*
228 * For sdio rw commands we must wait for card busy otherwise some
229 * sdio devices won't work properly.
f328c76e 230 * And bypass I/O abort, reset and bus suspend operations.
5d3f6ef0 231 */
f328c76e 232 if (sdio_is_io_busy(mrq->cmd->opcode, mrq->cmd->arg) &&
233 host->ops->card_busy) {
5d3f6ef0
HG
234 int tries = 500; /* Wait aprox 500ms at maximum */
235
236 while (host->ops->card_busy(host) && --tries)
237 mmc_delay(1);
238
239 if (tries == 0) {
240 mrq->cmd->error = -EBUSY;
241 mmc_request_done(host, mrq);
242 return;
243 }
244 }
245
5163af5a
AH
246 if (mrq->cap_cmd_during_tfr) {
247 host->ongoing_mrq = mrq;
248 /*
249 * Retry path could come through here without having waiting on
250 * cmd_completion, so ensure it is reinitialised.
251 */
252 reinit_completion(&mrq->cmd_completion);
253 }
254
7962fc37
BW
255 trace_mmc_request_start(host, mrq);
256
3e207c8c
AH
257 if (host->cqe_on)
258 host->cqe_ops->cqe_off(host);
259
90a81489
AH
260 host->ops->request(host, mrq);
261}
262
72a5af55
AH
263static void mmc_mrq_pr_debug(struct mmc_host *host, struct mmc_request *mrq,
264 bool cqe)
1da177e4 265{
7b2fd4f2
JC
266 if (mrq->sbc) {
267 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
268 mmc_hostname(host), mrq->sbc->opcode,
269 mrq->sbc->arg, mrq->sbc->flags);
270 }
271
4b67e63f 272 if (mrq->cmd) {
72a5af55
AH
273 pr_debug("%s: starting %sCMD%u arg %08x flags %08x\n",
274 mmc_hostname(host), cqe ? "CQE direct " : "",
275 mrq->cmd->opcode, mrq->cmd->arg, mrq->cmd->flags);
276 } else if (cqe) {
277 pr_debug("%s: starting CQE transfer for tag %d blkaddr %u\n",
278 mmc_hostname(host), mrq->tag, mrq->data->blk_addr);
4b67e63f 279 }
1da177e4 280
e4d21708
PO
281 if (mrq->data) {
282 pr_debug("%s: blksz %d blocks %d flags %08x "
283 "tsac %d ms nsac %d\n",
284 mmc_hostname(host), mrq->data->blksz,
285 mrq->data->blocks, mrq->data->flags,
ce252edd 286 mrq->data->timeout_ns / 1000000,
e4d21708
PO
287 mrq->data->timeout_clks);
288 }
289
290 if (mrq->stop) {
291 pr_debug("%s: CMD%u arg %08x flags %08x\n",
292 mmc_hostname(host), mrq->stop->opcode,
293 mrq->stop->arg, mrq->stop->flags);
294 }
4b67e63f
AH
295}
296
f34bdd2f 297static int mmc_mrq_prep(struct mmc_host *host, struct mmc_request *mrq)
4b67e63f 298{
b044b1bc 299 unsigned int i, sz = 0;
4b67e63f 300 struct scatterlist *sg;
1da177e4 301
f34bdd2f
AH
302 if (mrq->cmd) {
303 mrq->cmd->error = 0;
304 mrq->cmd->mrq = mrq;
305 mrq->cmd->data = mrq->data;
306 }
cce411e6
AG
307 if (mrq->sbc) {
308 mrq->sbc->error = 0;
309 mrq->sbc->mrq = mrq;
310 }
1da177e4 311 if (mrq->data) {
6ff897ff
SL
312 if (mrq->data->blksz > host->max_blk_size ||
313 mrq->data->blocks > host->max_blk_count ||
314 mrq->data->blocks * mrq->data->blksz > host->max_req_size)
315 return -EINVAL;
b044b1bc 316
a84756c5
PO
317 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
318 sz += sg->length;
6ff897ff
SL
319 if (sz != mrq->data->blocks * mrq->data->blksz)
320 return -EINVAL;
b044b1bc 321
1da177e4
LT
322 mrq->data->error = 0;
323 mrq->data->mrq = mrq;
324 if (mrq->stop) {
325 mrq->data->stop = mrq->stop;
326 mrq->stop->error = 0;
327 mrq->stop->mrq = mrq;
328 }
329 }
f34bdd2f
AH
330
331 return 0;
332}
333
cb39f61e 334int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
f34bdd2f
AH
335{
336 int err;
337
c48e13e8 338 if (mrq->cmd->has_ext_addr)
403a0293
AA
339 mmc_send_ext_addr(host, mrq->cmd->ext_addr);
340
d2383318
AH
341 init_completion(&mrq->cmd_completion);
342
f34bdd2f
AH
343 mmc_retune_hold(host);
344
345 if (mmc_card_removed(host->card))
346 return -ENOMEDIUM;
347
72a5af55 348 mmc_mrq_pr_debug(host, mrq, false);
f34bdd2f
AH
349
350 WARN_ON(!host->claimed);
351
352 err = mmc_mrq_prep(host, mrq);
353 if (err)
354 return err;
355
9a9f7e13
VS
356 if (host->uhs2_sd_tran)
357 mmc_uhs2_prepare_cmd(host, mrq);
358
66c036e0 359 led_trigger_event(host->led, LED_FULL);
90a81489 360 __mmc_start_request(host, mrq);
f100c1c2
AH
361
362 return 0;
1da177e4 363}
cb39f61e 364EXPORT_SYMBOL(mmc_start_request);
1da177e4 365
1da177e4
LT
366static void mmc_wait_done(struct mmc_request *mrq)
367{
aa8b683a
PF
368 complete(&mrq->completion);
369}
370
5163af5a
AH
371static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host)
372{
373 struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq);
374
375 /*
376 * If there is an ongoing transfer, wait for the command line to become
377 * available.
378 */
379 if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion))
380 wait_for_completion(&ongoing_mrq->cmd_completion);
381}
382
956d9fd5 383static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
aa8b683a 384{
f100c1c2
AH
385 int err;
386
5163af5a
AH
387 mmc_wait_ongoing_tfr_cmd(host);
388
aa8b683a
PF
389 init_completion(&mrq->completion);
390 mrq->done = mmc_wait_done;
f100c1c2
AH
391
392 err = mmc_start_request(host, mrq);
393 if (err) {
394 mrq->cmd->error = err;
5163af5a 395 mmc_complete_cmd(mrq);
d3049504 396 complete(&mrq->completion);
d3049504 397 }
f100c1c2
AH
398
399 return err;
aa8b683a
PF
400}
401
5163af5a 402void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
aa8b683a 403{
08a7e1df
AH
404 struct mmc_command *cmd;
405
406 while (1) {
407 wait_for_completion(&mrq->completion);
408
409 cmd = mrq->cmd;
775a9362 410
d3049504
AH
411 if (!cmd->error || !cmd->retries ||
412 mmc_card_removed(host->card))
08a7e1df
AH
413 break;
414
90a81489
AH
415 mmc_retune_recheck(host);
416
08a7e1df
AH
417 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
418 mmc_hostname(host), cmd->opcode, cmd->error);
419 cmd->retries--;
420 cmd->error = 0;
90a81489 421 __mmc_start_request(host, mrq);
08a7e1df 422 }
90a81489
AH
423
424 mmc_retune_release(host);
aa8b683a 425}
5163af5a
AH
426EXPORT_SYMBOL(mmc_wait_for_req_done);
427
72a5af55
AH
428/*
429 * mmc_cqe_start_req - Start a CQE request.
430 * @host: MMC host to start the request
431 * @mrq: request to start
432 *
433 * Start the request, re-tuning if needed and it is possible. Returns an error
434 * code if the request fails to start or -EBUSY if CQE is busy.
435 */
436int mmc_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
437{
438 int err;
439
440 /*
441 * CQE cannot process re-tuning commands. Caller must hold retuning
442 * while CQE is in use. Re-tuning can happen here only when CQE has no
443 * active requests i.e. this is the first. Note, re-tuning will call
444 * ->cqe_off().
445 */
446 err = mmc_retune(host);
447 if (err)
448 goto out_err;
449
450 mrq->host = host;
451
452 mmc_mrq_pr_debug(host, mrq, true);
453
454 err = mmc_mrq_prep(host, mrq);
455 if (err)
456 goto out_err;
457
9a9f7e13
VS
458 if (host->uhs2_sd_tran)
459 mmc_uhs2_prepare_cmd(host, mrq);
460
72a5af55
AH
461 err = host->cqe_ops->cqe_request(host, mrq);
462 if (err)
463 goto out_err;
464
465 trace_mmc_request_start(host, mrq);
466
467 return 0;
468
469out_err:
470 if (mrq->cmd) {
471 pr_debug("%s: failed to start CQE direct CMD%u, error %d\n",
472 mmc_hostname(host), mrq->cmd->opcode, err);
473 } else {
474 pr_debug("%s: failed to start CQE transfer for tag %d, error %d\n",
475 mmc_hostname(host), mrq->tag, err);
476 }
477 return err;
478}
479EXPORT_SYMBOL(mmc_cqe_start_req);
480
481/**
482 * mmc_cqe_request_done - CQE has finished processing an MMC request
483 * @host: MMC host which completed request
484 * @mrq: MMC request which completed
485 *
486 * CQE drivers should call this function when they have completed
487 * their processing of a request.
488 */
489void mmc_cqe_request_done(struct mmc_host *host, struct mmc_request *mrq)
490{
491 mmc_should_fail_request(host, mrq);
492
493 /* Flag re-tuning needed on CRC errors */
494 if ((mrq->cmd && mrq->cmd->error == -EILSEQ) ||
495 (mrq->data && mrq->data->error == -EILSEQ))
496 mmc_retune_needed(host);
497
498 trace_mmc_request_done(host, mrq);
499
500 if (mrq->cmd) {
501 pr_debug("%s: CQE req done (direct CMD%u): %d\n",
502 mmc_hostname(host), mrq->cmd->opcode, mrq->cmd->error);
503 } else {
504 pr_debug("%s: CQE transfer done tag %d\n",
505 mmc_hostname(host), mrq->tag);
506 }
507
508 if (mrq->data) {
509 pr_debug("%s: %d bytes transferred: %d\n",
510 mmc_hostname(host),
511 mrq->data->bytes_xfered, mrq->data->error);
512 }
513
514 mrq->done(mrq);
515}
516EXPORT_SYMBOL(mmc_cqe_request_done);
517
518/**
519 * mmc_cqe_post_req - CQE post process of a completed MMC request
520 * @host: MMC host
521 * @mrq: MMC request to be processed
522 */
523void mmc_cqe_post_req(struct mmc_host *host, struct mmc_request *mrq)
524{
525 if (host->cqe_ops->cqe_post_req)
526 host->cqe_ops->cqe_post_req(host, mrq);
527}
528EXPORT_SYMBOL(mmc_cqe_post_req);
529
530/* Arbitrary 1 second timeout */
531#define MMC_CQE_RECOVERY_TIMEOUT 1000
532
533/*
534 * mmc_cqe_recovery - Recover from CQE errors.
535 * @host: MMC host to recover
536 *
ff50df9a 537 * Recovery consists of stopping CQE, stopping eMMC, discarding the queue
72a5af55
AH
538 * in eMMC, and discarding the queue in CQE. CQE must call
539 * mmc_cqe_request_done() on all requests. An error is returned if the eMMC
540 * fails to discard its queue.
541 */
542int mmc_cqe_recovery(struct mmc_host *host)
543{
544 struct mmc_command cmd;
545 int err;
546
547 mmc_retune_hold_now(host);
548
549 /*
550 * Recovery is expected seldom, if at all, but it reduces performance,
551 * so make sure it is not completely silent.
552 */
553 pr_warn("%s: running CQE recovery\n", mmc_hostname(host));
554
555 host->cqe_ops->cqe_recovery_start(host);
556
557 memset(&cmd, 0, sizeof(cmd));
6b1dc622 558 cmd.opcode = MMC_STOP_TRANSMISSION;
ed97550d 559 cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */
6b1dc622 560 cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT;
8155d1fa 561 mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
72a5af55 562
c616696a
AH
563 mmc_poll_for_busy(host->card, MMC_CQE_RECOVERY_TIMEOUT, true, MMC_BUSY_IO);
564
72a5af55
AH
565 memset(&cmd, 0, sizeof(cmd));
566 cmd.opcode = MMC_CMDQ_TASK_MGMT;
567 cmd.arg = 1; /* Discard entire queue */
ed97550d 568 cmd.flags = MMC_RSP_R1B_NO_CRC | MMC_CMD_AC; /* Ignore CRC */
6b1dc622 569 cmd.busy_timeout = MMC_CQE_RECOVERY_TIMEOUT;
8155d1fa 570 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
72a5af55
AH
571
572 host->cqe_ops->cqe_recovery_finish(host);
573
8155d1fa
AH
574 if (err)
575 err = mmc_wait_for_cmd(host, &cmd, MMC_CMD_RETRIES);
576
72a5af55
AH
577 mmc_retune_release(host);
578
579 return err;
580}
581EXPORT_SYMBOL(mmc_cqe_recovery);
582
5163af5a
AH
583/**
584 * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done
585 * @host: MMC host
586 * @mrq: MMC request
587 *
588 * mmc_is_req_done() is used with requests that have
589 * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after
590 * starting a request and before waiting for it to complete. That is,
591 * either in between calls to mmc_start_req(), or after mmc_wait_for_req()
592 * and before mmc_wait_for_req_done(). If it is called at other times the
593 * result is not meaningful.
594 */
595bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq)
596{
126b6270 597 return completion_done(&mrq->completion);
5163af5a
AH
598}
599EXPORT_SYMBOL(mmc_is_req_done);
aa8b683a 600
67a61c48
PO
601/**
602 * mmc_wait_for_req - start a request and wait for completion
603 * @host: MMC host to start command
604 * @mrq: MMC request to start
605 *
606 * Start a new MMC custom command request for a host, and wait
5163af5a
AH
607 * for the command to complete. In the case of 'cap_cmd_during_tfr'
608 * requests, the transfer is ongoing and the caller can issue further
609 * commands that do not use the data lines, and then wait by calling
610 * mmc_wait_for_req_done().
611 * Does not attempt to parse the response.
67a61c48
PO
612 */
613void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
1da177e4 614{
aa8b683a 615 __mmc_start_req(host, mrq);
5163af5a
AH
616
617 if (!mrq->cap_cmd_during_tfr)
618 mmc_wait_for_req_done(host, mrq);
1da177e4 619}
1da177e4
LT
620EXPORT_SYMBOL(mmc_wait_for_req);
621
622/**
623 * mmc_wait_for_cmd - start a command and wait for completion
624 * @host: MMC host to start command
625 * @cmd: MMC command to start
626 * @retries: maximum number of retries
627 *
628 * Start a new MMC command for a host, and wait for the command
629 * to complete. Return any error that occurred while the command
630 * was executing. Do not attempt to parse the response.
631 */
632int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
633{
c7836d15 634 struct mmc_request mrq = {};
1da177e4 635
d84075c8 636 WARN_ON(!host->claimed);
1da177e4 637
1da177e4
LT
638 memset(cmd->resp, 0, sizeof(cmd->resp));
639 cmd->retries = retries;
640
641 mrq.cmd = cmd;
642 cmd->data = NULL;
643
644 mmc_wait_for_req(host, &mrq);
645
646 return cmd->error;
647}
648
649EXPORT_SYMBOL(mmc_wait_for_cmd);
650
d773d725
RK
651/**
652 * mmc_set_data_timeout - set the timeout for a data command
653 * @data: data phase for command
654 * @card: the MMC card associated with the data transfer
67a61c48
PO
655 *
656 * Computes the data timeout parameters according to the
657 * correct algorithm given the card type.
d773d725 658 */
b146d26a 659void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
d773d725
RK
660{
661 unsigned int mult;
662
e6f918bf
PO
663 /*
664 * SDIO cards only define an upper 1 s limit on access.
665 */
666 if (mmc_card_sdio(card)) {
667 data->timeout_ns = 1000000000;
668 data->timeout_clks = 0;
669 return;
670 }
671
d773d725
RK
672 /*
673 * SD cards use a 100 multiplier rather than 10
674 */
675 mult = mmc_card_sd(card) ? 100 : 10;
676
677 /*
678 * Scale up the multiplier (and therefore the timeout) by
679 * the r2w factor for writes.
680 */
b146d26a 681 if (data->flags & MMC_DATA_WRITE)
d773d725
RK
682 mult <<= card->csd.r2w_factor;
683
4406ae21
SL
684 data->timeout_ns = card->csd.taac_ns * mult;
685 data->timeout_clks = card->csd.taac_clks * mult;
d773d725
RK
686
687 /*
688 * SD cards also have an upper limit on the timeout.
689 */
690 if (mmc_card_sd(card)) {
691 unsigned int timeout_us, limit_us;
692
693 timeout_us = data->timeout_ns / 1000;
9eadcc05 694 if (card->host->ios.clock)
e9b86841 695 timeout_us += data->timeout_clks * 1000 /
9eadcc05 696 (card->host->ios.clock / 1000);
d773d725 697
b146d26a 698 if (data->flags & MMC_DATA_WRITE)
493890e7 699 /*
3bdc9ba8
PW
700 * The MMC spec "It is strongly recommended
701 * for hosts to implement more than 500ms
702 * timeout value even if the card indicates
703 * the 250ms maximum busy length." Even the
704 * previous value of 300ms is known to be
705 * insufficient for some cards.
493890e7 706 */
3bdc9ba8 707 limit_us = 3000000;
d773d725
RK
708 else
709 limit_us = 100000;
710
fba68bd2
PL
711 /*
712 * SDHC cards always use these fixed values.
713 */
6ca2920d 714 if (timeout_us > limit_us) {
d773d725
RK
715 data->timeout_ns = limit_us * 1000;
716 data->timeout_clks = 0;
717 }
f7bf11a3
SW
718
719 /* assign limit value if invalid */
720 if (timeout_us == 0)
721 data->timeout_ns = limit_us * 1000;
d773d725 722 }
6de5fc9c
SNX
723
724 /*
725 * Some cards require longer data read timeout than indicated in CSD.
726 * Address this by setting the read timeout to a "reasonably high"
32ecd320 727 * value. For the cards tested, 600ms has proven enough. If necessary,
6de5fc9c
SNX
728 * this value can be increased if other problematic cards require this.
729 */
730 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
32ecd320 731 data->timeout_ns = 600000000;
6de5fc9c
SNX
732 data->timeout_clks = 0;
733 }
734
c0c88871
WM
735 /*
736 * Some cards need very high timeouts if driven in SPI mode.
737 * The worst observed timeout was 900ms after writing a
738 * continuous stream of data until the internal logic
739 * overflowed.
740 */
741 if (mmc_host_is_spi(card->host)) {
742 if (data->flags & MMC_DATA_WRITE) {
743 if (data->timeout_ns < 1000000000)
744 data->timeout_ns = 1000000000; /* 1s */
745 } else {
746 if (data->timeout_ns < 100000000)
747 data->timeout_ns = 100000000; /* 100ms */
748 }
749 }
d773d725
RK
750}
751EXPORT_SYMBOL(mmc_set_data_timeout);
752
6c0cedd1
AH
753/*
754 * Allow claiming an already claimed host if the context is the same or there is
755 * no context but the task is the same.
756 */
757static inline bool mmc_ctx_matches(struct mmc_host *host, struct mmc_ctx *ctx,
758 struct task_struct *task)
759{
760 return host->claimer == ctx ||
761 (!ctx && task && host->claimer->task == task);
762}
763
764static inline void mmc_ctx_set_claimer(struct mmc_host *host,
765 struct mmc_ctx *ctx,
766 struct task_struct *task)
767{
768 if (!host->claimer) {
769 if (ctx)
770 host->claimer = ctx;
771 else
772 host->claimer = &host->default_ctx;
773 }
774 if (task)
775 host->claimer->task = task;
776}
777
1da177e4 778/**
2342f332 779 * __mmc_claim_host - exclusively claim a host
1da177e4 780 * @host: mmc host to claim
6c0cedd1
AH
781 * @ctx: context that claims the host or NULL in which case the default
782 * context will be used
2342f332 783 * @abort: whether or not the operation should be aborted
1da177e4 784 *
2342f332
NP
785 * Claim a host for a set of operations. If @abort is non null and
786 * dereference a non-zero value then this will return prematurely with
787 * that non-zero value without acquiring the lock. Returns zero
788 * with the lock held otherwise.
1da177e4 789 */
6c0cedd1
AH
790int __mmc_claim_host(struct mmc_host *host, struct mmc_ctx *ctx,
791 atomic_t *abort)
1da177e4 792{
6c0cedd1 793 struct task_struct *task = ctx ? NULL : current;
1da177e4
LT
794 DECLARE_WAITQUEUE(wait, current);
795 unsigned long flags;
2342f332 796 int stop;
9250aea7 797 bool pm = false;
1da177e4 798
cf795bfb
PO
799 might_sleep();
800
1da177e4
LT
801 add_wait_queue(&host->wq, &wait);
802 spin_lock_irqsave(&host->lock, flags);
803 while (1) {
804 set_current_state(TASK_UNINTERRUPTIBLE);
2342f332 805 stop = abort ? atomic_read(abort) : 0;
6c0cedd1 806 if (stop || !host->claimed || mmc_ctx_matches(host, ctx, task))
1da177e4
LT
807 break;
808 spin_unlock_irqrestore(&host->lock, flags);
809 schedule();
810 spin_lock_irqsave(&host->lock, flags);
811 }
812 set_current_state(TASK_RUNNING);
319a3f14 813 if (!stop) {
2342f332 814 host->claimed = 1;
6c0cedd1 815 mmc_ctx_set_claimer(host, ctx, task);
319a3f14 816 host->claim_cnt += 1;
9250aea7
UH
817 if (host->claim_cnt == 1)
818 pm = true;
319a3f14 819 } else
2342f332 820 wake_up(&host->wq);
1da177e4
LT
821 spin_unlock_irqrestore(&host->lock, flags);
822 remove_wait_queue(&host->wq, &wait);
9250aea7
UH
823
824 if (pm)
825 pm_runtime_get_sync(mmc_dev(host));
826
2342f332 827 return stop;
1da177e4 828}
2342f332 829EXPORT_SYMBOL(__mmc_claim_host);
8ea926b2 830
ab1efd27 831/**
907d2e7c 832 * mmc_release_host - release a host
ab1efd27
UH
833 * @host: mmc host to release
834 *
907d2e7c
AH
835 * Release a MMC host, allowing others to claim the host
836 * for their operations.
ab1efd27 837 */
907d2e7c 838void mmc_release_host(struct mmc_host *host)
8ea926b2
AH
839{
840 unsigned long flags;
841
907d2e7c
AH
842 WARN_ON(!host->claimed);
843
8ea926b2 844 spin_lock_irqsave(&host->lock, flags);
319a3f14
AH
845 if (--host->claim_cnt) {
846 /* Release for nested claim */
847 spin_unlock_irqrestore(&host->lock, flags);
848 } else {
849 host->claimed = 0;
6c0cedd1 850 host->claimer->task = NULL;
319a3f14
AH
851 host->claimer = NULL;
852 spin_unlock_irqrestore(&host->lock, flags);
853 wake_up(&host->wq);
9250aea7 854 pm_runtime_mark_last_busy(mmc_dev(host));
7d5ef512
UH
855 if (host->caps & MMC_CAP_SYNC_RUNTIME_PM)
856 pm_runtime_put_sync_suspend(mmc_dev(host));
857 else
858 pm_runtime_put_autosuspend(mmc_dev(host));
319a3f14 859 }
8ea926b2 860}
1da177e4
LT
861EXPORT_SYMBOL(mmc_release_host);
862
e94cfef6
UH
863/*
864 * This is a helper function, which fetches a runtime pm reference for the
865 * card device and also claims the host.
866 */
6c0cedd1 867void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx)
e94cfef6
UH
868{
869 pm_runtime_get_sync(&card->dev);
6c0cedd1 870 __mmc_claim_host(card->host, ctx, NULL);
e94cfef6
UH
871}
872EXPORT_SYMBOL(mmc_get_card);
873
874/*
875 * This is a helper function, which releases the host and drops the runtime
876 * pm reference for the card device.
877 */
6c0cedd1 878void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx)
e94cfef6 879{
6c0cedd1
AH
880 struct mmc_host *host = card->host;
881
882 WARN_ON(ctx && host->claimer != ctx);
883
884 mmc_release_host(host);
e94cfef6
UH
885 pm_runtime_mark_last_busy(&card->dev);
886 pm_runtime_put_autosuspend(&card->dev);
887}
888EXPORT_SYMBOL(mmc_put_card);
889
7ea239d9
PO
890/*
891 * Internal function that does the actual ios call to the host driver,
892 * optionally printing some debug output.
893 */
920e70c5
RK
894static inline void mmc_set_ios(struct mmc_host *host)
895{
896 struct mmc_ios *ios = &host->ios;
897
cd9277c0
PO
898 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
899 "width %u timing %u\n",
920e70c5
RK
900 mmc_hostname(host), ios->clock, ios->bus_mode,
901 ios->power_mode, ios->chip_select, ios->vdd,
ed9feec7 902 1 << ios->bus_width, ios->timing);
fba68bd2 903
920e70c5
RK
904 host->ops->set_ios(host, ios);
905}
906
7ea239d9
PO
907/*
908 * Control chip select pin on a host.
909 */
da7fbe58 910void mmc_set_chip_select(struct mmc_host *host, int mode)
1da177e4 911{
da7fbe58
PO
912 host->ios.chip_select = mode;
913 mmc_set_ios(host);
1da177e4
LT
914}
915
7ea239d9
PO
916/*
917 * Sets the host clock to the highest possible frequency that
918 * is below "hz".
919 */
9eadcc05 920void mmc_set_clock(struct mmc_host *host, unsigned int hz)
7ea239d9 921{
6a98f1e8 922 WARN_ON(hz && hz < host->f_min);
7ea239d9
PO
923
924 if (hz > host->f_max)
925 hz = host->f_max;
926
927 host->ios.clock = hz;
928 mmc_set_ios(host);
929}
930
63e415c6
AH
931int mmc_execute_tuning(struct mmc_card *card)
932{
933 struct mmc_host *host = card->host;
934 u32 opcode;
935 int err;
936
937 if (!host->ops->execute_tuning)
938 return 0;
939
3e207c8c
AH
940 if (host->cqe_on)
941 host->cqe_ops->cqe_off(host);
942
63e415c6
AH
943 if (mmc_card_mmc(card))
944 opcode = MMC_SEND_TUNING_BLOCK_HS200;
945 else
946 opcode = MMC_SEND_TUNING_BLOCK;
947
63e415c6 948 err = host->ops->execute_tuning(host, opcode);
83359288 949 if (!err) {
8ffb2611 950 mmc_retune_clear(host);
79d5a65a 951 mmc_retune_enable(host);
83359288 952 return 0;
77347eda 953 }
63e415c6 954
83359288 955 /* Only print error when we don't check for card removal */
91f059c9 956 if (!host->detect_change) {
07d97d87
RK
957 pr_err("%s: tuning execution failed: %d\n",
958 mmc_hostname(host), err);
91f059c9
SSB
959 mmc_debugfs_err_stats_inc(host, MMC_ERR_TUNING);
960 }
63e415c6
AH
961
962 return err;
963}
964
7ea239d9
PO
965/*
966 * Change the bus mode (open drain/push-pull) of a host.
967 */
968void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
969{
970 host->ios.bus_mode = mode;
971 mmc_set_ios(host);
972}
973
0f8d8ea6
AH
974/*
975 * Change data bus width of a host.
976 */
977void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
978{
4c4cb171
PR
979 host->ios.bus_width = width;
980 mmc_set_ios(host);
0f8d8ea6
AH
981}
982
2d079c43
JR
983/*
984 * Set initial state after a power cycle or a hw_reset.
985 */
986void mmc_set_initial_state(struct mmc_host *host)
987{
3e207c8c
AH
988 if (host->cqe_on)
989 host->cqe_ops->cqe_off(host);
990
79d5a65a
AH
991 mmc_retune_disable(host);
992
2d079c43
JR
993 if (mmc_host_is_spi(host))
994 host->ios.chip_select = MMC_CS_HIGH;
995 else
996 host->ios.chip_select = MMC_CS_DONTCARE;
997 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
998 host->ios.bus_width = MMC_BUS_WIDTH_1;
999 host->ios.timing = MMC_TIMING_LEGACY;
75e8a228 1000 host->ios.drv_type = 0;
81ac2af6
SL
1001 host->ios.enhanced_strobe = false;
1002
1003 /*
1004 * Make sure we are in non-enhanced strobe mode before we
1005 * actually enable it in ext_csd.
1006 */
1007 if ((host->caps2 & MMC_CAP2_HS400_ES) &&
1008 host->ops->hs400_enhanced_strobe)
1009 host->ops->hs400_enhanced_strobe(host, &host->ios);
2d079c43
JR
1010
1011 mmc_set_ios(host);
93f1c150
EB
1012
1013 mmc_crypto_set_initial_state(host);
2d079c43
JR
1014}
1015
86e8286a
AV
1016/**
1017 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1018 * @vdd: voltage (mV)
1019 * @low_bits: prefer low bits in boundary cases
1020 *
1021 * This function returns the OCR bit number according to the provided @vdd
1022 * value. If conversion is not possible a negative errno value returned.
1023 *
1024 * Depending on the @low_bits flag the function prefers low or high OCR bits
1025 * on boundary voltages. For example,
1026 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1027 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1028 *
1029 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1030 */
1031static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1032{
1033 const int max_bit = ilog2(MMC_VDD_35_36);
1034 int bit;
1035
1036 if (vdd < 1650 || vdd > 3600)
1037 return -EINVAL;
1038
1039 if (vdd >= 1650 && vdd <= 1950)
1040 return ilog2(MMC_VDD_165_195);
1041
1042 if (low_bits)
1043 vdd -= 1;
1044
1045 /* Base 2000 mV, step 100 mV, bit's base 8. */
1046 bit = (vdd - 2000) / 100 + 8;
1047 if (bit > max_bit)
1048 return max_bit;
1049 return bit;
1050}
1051
1052/**
1053 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1054 * @vdd_min: minimum voltage value (mV)
1055 * @vdd_max: maximum voltage value (mV)
1056 *
1057 * This function returns the OCR mask bits according to the provided @vdd_min
1058 * and @vdd_max values. If conversion is not possible the function returns 0.
1059 *
1060 * Notes wrt boundary cases:
1061 * This function sets the OCR bits for all boundary voltages, for example
1062 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1063 * MMC_VDD_34_35 mask.
1064 */
1065u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1066{
1067 u32 mask = 0;
1068
1069 if (vdd_max < vdd_min)
1070 return 0;
1071
1072 /* Prefer high bits for the boundary vdd_max values. */
1073 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1074 if (vdd_max < 0)
1075 return 0;
1076
1077 /* Prefer low bits for the boundary vdd_min values. */
1078 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1079 if (vdd_min < 0)
1080 return 0;
1081
1082 /* Fill the mask, from max bit to min bit. */
1083 while (vdd_max >= vdd_min)
1084 mask |= 1 << vdd_max--;
1085
1086 return mask;
1087}
86e8286a 1088
25185f3f
SH
1089static int mmc_of_get_func_num(struct device_node *node)
1090{
1091 u32 reg;
1092 int ret;
1093
1094 ret = of_property_read_u32(node, "reg", &reg);
1095 if (ret < 0)
1096 return ret;
1097
1098 return reg;
1099}
1100
1101struct device_node *mmc_of_find_child_device(struct mmc_host *host,
1102 unsigned func_num)
1103{
1104 struct device_node *node;
1105
1106 if (!host->parent || !host->parent->of_node)
1107 return NULL;
1108
1109 for_each_child_of_node(host->parent->of_node, node) {
1110 if (mmc_of_get_func_num(node) == func_num)
1111 return node;
1112 }
1113
1114 return NULL;
1115}
1116
1da177e4
LT
1117/*
1118 * Mask off any voltages we don't support and select
1119 * the lowest voltage
1120 */
7ea239d9 1121u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
1da177e4
LT
1122{
1123 int bit;
1124
726d6f23
UH
1125 /*
1126 * Sanity check the voltages that the card claims to
1127 * support.
1128 */
1129 if (ocr & 0x7F) {
1130 dev_warn(mmc_dev(host),
1131 "card claims to support voltages below defined range\n");
1132 ocr &= ~0x7F;
1133 }
1134
1da177e4 1135 ocr &= host->ocr_avail;
ce69d37b
UH
1136 if (!ocr) {
1137 dev_warn(mmc_dev(host), "no support for card's volts\n");
1138 return 0;
1139 }
1da177e4 1140
9a9f7e13 1141 if (!mmc_card_uhs2(host) && host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
ce69d37b 1142 bit = ffs(ocr) - 1;
63ef731a 1143 ocr &= 3 << bit;
ce69d37b 1144 mmc_power_cycle(host, ocr);
1da177e4 1145 } else {
ce69d37b 1146 bit = fls(ocr) - 1;
39a72dbf
YG
1147 /*
1148 * The bit variable represents the highest voltage bit set in
1149 * the OCR register.
1150 * To keep a range of 2 values (e.g. 3.2V/3.3V and 3.3V/3.4V),
1151 * we must shift the mask '3' with (bit - 1).
1152 */
1153 ocr &= 3 << (bit - 1);
ce69d37b
UH
1154 if (bit != host->ios.vdd)
1155 dev_warn(mmc_dev(host), "exceeding card's volts\n");
1da177e4
LT
1156 }
1157
1158 return ocr;
1159}
1160
4e74b6b3 1161int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
567c8903
JR
1162{
1163 int err = 0;
1164 int old_signal_voltage = host->ios.signal_voltage;
1165
1166 host->ios.signal_voltage = signal_voltage;
9eadcc05 1167 if (host->ops->start_signal_voltage_switch)
567c8903 1168 err = host->ops->start_signal_voltage_switch(host, &host->ios);
567c8903
JR
1169
1170 if (err)
1171 host->ios.signal_voltage = old_signal_voltage;
1172
1173 return err;
1174
1175}
1176
508c9864
UH
1177void mmc_set_initial_signal_voltage(struct mmc_host *host)
1178{
1179 /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
1180 if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330))
1181 dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
1182 else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
1183 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
1184 else if (!mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120))
1185 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
1186}
1187
3f496afb
AH
1188int mmc_host_set_uhs_voltage(struct mmc_host *host)
1189{
1190 u32 clock;
1191
1192 /*
1193 * During a signal voltage level switch, the clock must be gated
1194 * for 5 ms according to the SD spec
1195 */
1196 clock = host->ios.clock;
1197 host->ios.clock = 0;
1198 mmc_set_ios(host);
1199
1200 if (mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180))
1201 return -EAGAIN;
1202
1203 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
1204 mmc_delay(10);
1205 host->ios.clock = clock;
1206 mmc_set_ios(host);
1207
1208 return 0;
1209}
1210
2ed573b6 1211int mmc_set_uhs_voltage(struct mmc_host *host, u32 ocr)
f2119df6 1212{
c7836d15 1213 struct mmc_command cmd = {};
f2119df6
AN
1214 int err = 0;
1215
0797e5f1
JR
1216 /*
1217 * If we cannot switch voltages, return failure so the caller
1218 * can continue without UHS mode
1219 */
1220 if (!host->ops->start_signal_voltage_switch)
1221 return -EPERM;
1222 if (!host->ops->card_busy)
6606110d
JP
1223 pr_warn("%s: cannot verify signal voltage switch\n",
1224 mmc_hostname(host));
0797e5f1
JR
1225
1226 cmd.opcode = SD_SWITCH_VOLTAGE;
1227 cmd.arg = 0;
1228 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1229
1230 err = mmc_wait_for_cmd(host, &cmd, 0);
1231 if (err)
147186f5 1232 goto power_cycle;
9eadcc05
UH
1233
1234 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1235 return -EIO;
0797e5f1 1236
0797e5f1
JR
1237 /*
1238 * The card should drive cmd and dat[0:3] low immediately
1239 * after the response of cmd11, but wait 1 ms to be sure
1240 */
1241 mmc_delay(1);
1242 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1243 err = -EAGAIN;
1244 goto power_cycle;
1245 }
f2119df6 1246
3f496afb 1247 if (mmc_host_set_uhs_voltage(host)) {
0797e5f1
JR
1248 /*
1249 * Voltages may not have been switched, but we've already
1250 * sent CMD11, so a power cycle is required anyway
1251 */
1252 err = -EAGAIN;
1253 goto power_cycle;
f2119df6
AN
1254 }
1255
0797e5f1
JR
1256 /* Wait for at least 1 ms according to spec */
1257 mmc_delay(1);
1258
1259 /*
1260 * Failure to switch is indicated by the card holding
1261 * dat[0:3] low
1262 */
1263 if (host->ops->card_busy && host->ops->card_busy(host))
1264 err = -EAGAIN;
1265
1266power_cycle:
1267 if (err) {
1268 pr_debug("%s: Signal voltage switch failed, "
1269 "power cycling card\n", mmc_hostname(host));
0f791fda 1270 mmc_power_cycle(host, ocr);
0797e5f1
JR
1271 }
1272
0797e5f1 1273 return err;
f2119df6
AN
1274}
1275
b57c43ad 1276/*
7ea239d9 1277 * Select timing parameters for host.
b57c43ad 1278 */
7ea239d9 1279void mmc_set_timing(struct mmc_host *host, unsigned int timing)
b57c43ad 1280{
7ea239d9
PO
1281 host->ios.timing = timing;
1282 mmc_set_ios(host);
b57c43ad
PO
1283}
1284
d6d50a15
AN
1285/*
1286 * Select appropriate driver type for host.
1287 */
1288void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1289{
1290 host->ios.drv_type = drv_type;
1291 mmc_set_ios(host);
1292}
1293
e23350b3
AH
1294int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr,
1295 int card_drv_type, int *drv_type)
1296{
1297 struct mmc_host *host = card->host;
1298 int host_drv_type = SD_DRIVER_TYPE_B;
e23350b3
AH
1299
1300 *drv_type = 0;
1301
1302 if (!host->ops->select_drive_strength)
1303 return 0;
1304
1305 /* Use SD definition of driver strength for hosts */
1306 if (host->caps & MMC_CAP_DRIVER_TYPE_A)
1307 host_drv_type |= SD_DRIVER_TYPE_A;
1308
1309 if (host->caps & MMC_CAP_DRIVER_TYPE_C)
1310 host_drv_type |= SD_DRIVER_TYPE_C;
1311
1312 if (host->caps & MMC_CAP_DRIVER_TYPE_D)
1313 host_drv_type |= SD_DRIVER_TYPE_D;
1314
1315 /*
1316 * The drive strength that the hardware can support
1317 * depends on the board design. Pass the appropriate
1318 * information and let the hardware specific code
1319 * return what is possible given the options
1320 */
9eadcc05
UH
1321 return host->ops->select_drive_strength(card, max_dtr,
1322 host_drv_type,
1323 card_drv_type,
1324 drv_type);
e23350b3
AH
1325}
1326
1da177e4 1327/*
45f8245b
RK
1328 * Apply power to the MMC stack. This is a two-stage process.
1329 * First, we enable power to the card without the clock running.
1330 * We then wait a bit for the power to stabilise. Finally,
1331 * enable the bus drivers and clock to the card.
1332 *
1333 * We must _NOT_ enable the clock prior to power stablising.
1334 *
1335 * If a host does all the power sequencing itself, ignore the
1336 * initial MMC_POWER_UP stage.
1da177e4 1337 */
4a065193 1338void mmc_power_up(struct mmc_host *host, u32 ocr)
1da177e4 1339{
fa550189
UH
1340 if (host->ios.power_mode == MMC_POWER_ON)
1341 return;
1342
3aa8793f
UH
1343 mmc_pwrseq_pre_power_on(host);
1344
4a065193 1345 host->ios.vdd = fls(ocr) - 1;
1da177e4 1346 host->ios.power_mode = MMC_POWER_UP;
2d079c43
JR
1347 /* Set initial state and call mmc_set_ios */
1348 mmc_set_initial_state(host);
1da177e4 1349
508c9864 1350 mmc_set_initial_signal_voltage(host);
108ecc4c 1351
f9996aee
PO
1352 /*
1353 * This delay should be sufficient to allow the power supply
1354 * to reach the minimum voltage.
1355 */
6d796c68 1356 mmc_delay(host->ios.power_delay_ms);
1da177e4 1357
4febb7e2
UH
1358 mmc_pwrseq_post_power_on(host);
1359
88ae8b86 1360 host->ios.clock = host->f_init;
8dfd0374 1361
1da177e4 1362 host->ios.power_mode = MMC_POWER_ON;
920e70c5 1363 mmc_set_ios(host);
1da177e4 1364
f9996aee
PO
1365 /*
1366 * This delay must be at least 74 clock sizes, or 1 ms, or the
1367 * time required to reach a stable voltage.
1368 */
6d796c68 1369 mmc_delay(host->ios.power_delay_ms);
1da177e4
LT
1370}
1371
7f7e4129 1372void mmc_power_off(struct mmc_host *host)
1da177e4 1373{
fa550189
UH
1374 if (host->ios.power_mode == MMC_POWER_OFF)
1375 return;
1376
3aa8793f
UH
1377 mmc_pwrseq_power_off(host);
1378
1da177e4
LT
1379 host->ios.clock = 0;
1380 host->ios.vdd = 0;
b33d46c3 1381
1da177e4 1382 host->ios.power_mode = MMC_POWER_OFF;
2d079c43
JR
1383 /* Set initial state and call mmc_set_ios */
1384 mmc_set_initial_state(host);
778e277c 1385
041beb1d
DD
1386 /*
1387 * Some configurations, such as the 802.11 SDIO card in the OLPC
1388 * XO-1.5, require a short delay after poweroff before the card
1389 * can be successfully turned on again.
1390 */
1391 mmc_delay(1);
1da177e4
LT
1392}
1393
4a065193 1394void mmc_power_cycle(struct mmc_host *host, u32 ocr)
276e090f
JR
1395{
1396 mmc_power_off(host);
1397 /* Wait at least 1 ms according to SD spec */
1398 mmc_delay(1);
4a065193 1399 mmc_power_up(host, ocr);
276e090f
JR
1400}
1401
1da177e4 1402/*
7ea239d9
PO
1403 * Assign a mmc bus handler to a host. Only one bus handler may control a
1404 * host at any given time.
1da177e4 1405 */
7ea239d9 1406void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
1da177e4 1407{
7ea239d9 1408 host->bus_ops = ops;
b57c43ad
PO
1409}
1410
7ea239d9 1411/*
7f7e4129 1412 * Remove the current bus handler from a host.
7ea239d9
PO
1413 */
1414void mmc_detach_bus(struct mmc_host *host)
7ccd266e 1415{
e9ce2ce1 1416 host->bus_ops = NULL;
1da177e4
LT
1417}
1418
2ac55d5e 1419void _mmc_detect_change(struct mmc_host *host, unsigned long delay, bool cd_irq)
bbd43682 1420{
bbd43682 1421 /*
b52fb259
UH
1422 * Prevent system sleep for 5s to allow user space to consume the
1423 * corresponding uevent. This is especially useful, when CD irq is used
1424 * as a system wakeup, but doesn't hurt in other cases.
bbd43682 1425 */
b52fb259
UH
1426 if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL))
1427 __pm_wakeup_event(host->ws, 5000);
bbd43682
UH
1428
1429 host->detect_change = 1;
1430 mmc_schedule_delayed_work(&host->detect, delay);
1431}
1432
1da177e4
LT
1433/**
1434 * mmc_detect_change - process change of state on a MMC socket
1435 * @host: host which changed state.
8dc00335 1436 * @delay: optional delay to wait before detection (jiffies)
1da177e4 1437 *
67a61c48
PO
1438 * MMC drivers should call this when they detect a card has been
1439 * inserted or removed. The MMC layer will confirm that any
1440 * present card is still functional, and initialize any newly
1441 * inserted.
1da177e4 1442 */
8dc00335 1443void mmc_detect_change(struct mmc_host *host, unsigned long delay)
1da177e4 1444{
bbd43682 1445 _mmc_detect_change(host, delay, true);
1da177e4 1446}
1da177e4
LT
1447EXPORT_SYMBOL(mmc_detect_change);
1448
dfe86cba
AH
1449void mmc_init_erase(struct mmc_card *card)
1450{
1451 unsigned int sz;
1452
1453 if (is_power_of_2(card->erase_size))
1454 card->erase_shift = ffs(card->erase_size) - 1;
1455 else
1456 card->erase_shift = 0;
1457
1458 /*
1459 * It is possible to erase an arbitrarily large area of an SD or MMC
1460 * card. That is not desirable because it can take a long time
1461 * (minutes) potentially delaying more important I/O, and also the
1462 * timeout calculations become increasingly hugely over-estimated.
1463 * Consequently, 'pref_erase' is defined as a guide to limit erases
1464 * to that size and alignment.
1465 *
1466 * For SD cards that define Allocation Unit size, limit erases to one
c6d8fd61
GG
1467 * Allocation Unit at a time.
1468 * For MMC, have a stab at ai good value and for modern cards it will
1469 * end up being 4MiB. Note that if the value is too small, it can end
1470 * up taking longer to erase. Also note, erase_size is already set to
1471 * High Capacity Erase Size if available when this function is called.
dfe86cba
AH
1472 */
1473 if (mmc_card_sd(card) && card->ssr.au) {
1474 card->pref_erase = card->ssr.au;
1475 card->erase_shift = ffs(card->ssr.au) - 1;
cc8aa7de 1476 } else if (card->erase_size) {
dfe86cba
AH
1477 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1478 if (sz < 128)
1479 card->pref_erase = 512 * 1024 / 512;
1480 else if (sz < 512)
1481 card->pref_erase = 1024 * 1024 / 512;
1482 else if (sz < 1024)
1483 card->pref_erase = 2 * 1024 * 1024 / 512;
1484 else
1485 card->pref_erase = 4 * 1024 * 1024 / 512;
1486 if (card->pref_erase < card->erase_size)
1487 card->pref_erase = card->erase_size;
1488 else {
1489 sz = card->pref_erase % card->erase_size;
1490 if (sz)
1491 card->pref_erase += card->erase_size - sz;
1492 }
cc8aa7de
CD
1493 } else
1494 card->pref_erase = 0;
dfe86cba
AH
1495}
1496
489d1445
CL
1497static bool is_trim_arg(unsigned int arg)
1498{
1499 return (arg & MMC_TRIM_OR_DISCARD_ARGS) && arg != MMC_DISCARD_ARG;
1500}
1501
eaa02f75
AW
1502static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1503 unsigned int arg, unsigned int qty)
dfe86cba
AH
1504{
1505 unsigned int erase_timeout;
1506
7194efb8
AH
1507 if (arg == MMC_DISCARD_ARG ||
1508 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1509 erase_timeout = card->ext_csd.trim_timeout;
1510 } else if (card->ext_csd.erase_group_def & 1) {
dfe86cba
AH
1511 /* High Capacity Erase Group Size uses HC timeouts */
1512 if (arg == MMC_TRIM_ARG)
1513 erase_timeout = card->ext_csd.trim_timeout;
1514 else
1515 erase_timeout = card->ext_csd.hc_erase_timeout;
1516 } else {
1517 /* CSD Erase Group Size uses write timeout */
1518 unsigned int mult = (10 << card->csd.r2w_factor);
4406ae21 1519 unsigned int timeout_clks = card->csd.taac_clks * mult;
dfe86cba
AH
1520 unsigned int timeout_us;
1521
4406ae21
SL
1522 /* Avoid overflow: e.g. taac_ns=80000000 mult=1280 */
1523 if (card->csd.taac_ns < 1000000)
1524 timeout_us = (card->csd.taac_ns * mult) / 1000;
dfe86cba 1525 else
4406ae21 1526 timeout_us = (card->csd.taac_ns / 1000) * mult;
dfe86cba
AH
1527
1528 /*
1529 * ios.clock is only a target. The real clock rate might be
1530 * less but not that much less, so fudge it by multiplying by 2.
1531 */
1532 timeout_clks <<= 1;
1533 timeout_us += (timeout_clks * 1000) /
9eadcc05 1534 (card->host->ios.clock / 1000);
dfe86cba
AH
1535
1536 erase_timeout = timeout_us / 1000;
1537
1538 /*
1539 * Theoretically, the calculation could underflow so round up
1540 * to 1ms in that case.
1541 */
1542 if (!erase_timeout)
1543 erase_timeout = 1;
1544 }
1545
1546 /* Multiplier for secure operations */
1547 if (arg & MMC_SECURE_ARGS) {
1548 if (arg == MMC_SECURE_ERASE_ARG)
1549 erase_timeout *= card->ext_csd.sec_erase_mult;
1550 else
1551 erase_timeout *= card->ext_csd.sec_trim_mult;
1552 }
1553
1554 erase_timeout *= qty;
1555
1556 /*
1557 * Ensure at least a 1 second timeout for SPI as per
1558 * 'mmc_set_data_timeout()'
1559 */
1560 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1561 erase_timeout = 1000;
1562
eaa02f75 1563 return erase_timeout;
dfe86cba
AH
1564}
1565
eaa02f75
AW
1566static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1567 unsigned int arg,
1568 unsigned int qty)
dfe86cba 1569{
eaa02f75
AW
1570 unsigned int erase_timeout;
1571
ad9be7ff
AA
1572 /* for DISCARD none of the below calculation applies.
1573 * the busy timeout is 250msec per discard command.
1574 */
1575 if (arg == SD_DISCARD_ARG)
1576 return SD_DISCARD_TIMEOUT_MS;
1577
dfe86cba
AH
1578 if (card->ssr.erase_timeout) {
1579 /* Erase timeout specified in SD Status Register (SSR) */
eaa02f75
AW
1580 erase_timeout = card->ssr.erase_timeout * qty +
1581 card->ssr.erase_offset;
dfe86cba
AH
1582 } else {
1583 /*
1584 * Erase timeout not specified in SD Status Register (SSR) so
1585 * use 250ms per write block.
1586 */
eaa02f75 1587 erase_timeout = 250 * qty;
dfe86cba
AH
1588 }
1589
1590 /* Must not be less than 1 second */
eaa02f75
AW
1591 if (erase_timeout < 1000)
1592 erase_timeout = 1000;
1593
1594 return erase_timeout;
dfe86cba
AH
1595}
1596
eaa02f75
AW
1597static unsigned int mmc_erase_timeout(struct mmc_card *card,
1598 unsigned int arg,
1599 unsigned int qty)
dfe86cba
AH
1600{
1601 if (mmc_card_sd(card))
eaa02f75 1602 return mmc_sd_erase_timeout(card, arg, qty);
dfe86cba 1603 else
eaa02f75 1604 return mmc_mmc_erase_timeout(card, arg, qty);
dfe86cba
AH
1605}
1606
9b9c665a
AA
1607static int mmc_do_erase(struct mmc_card *card, sector_t from,
1608 sector_t to, unsigned int arg)
dfe86cba 1609{
c7836d15 1610 struct mmc_command cmd = {};
bb4eecf2 1611 unsigned int qty = 0, busy_timeout = 0;
e62f1e0b 1612 bool use_r1b_resp;
dfe86cba
AH
1613 int err;
1614
8f11d106
AH
1615 mmc_retune_hold(card->host);
1616
dfe86cba
AH
1617 /*
1618 * qty is used to calculate the erase timeout which depends on how many
1619 * erase groups (or allocation units in SD terminology) are affected.
1620 * We count erasing part of an erase group as one erase group.
1621 * For SD, the allocation units are always a power of 2. For MMC, the
1622 * erase group size is almost certainly also power of 2, but it does not
1623 * seem to insist on that in the JEDEC standard, so we fall back to
1624 * division in that case. SD may not specify an allocation unit size,
1625 * in which case the timeout is based on the number of write blocks.
1626 *
1627 * Note that the timeout for secure trim 2 will only be correct if the
1628 * number of erase groups specified is the same as the total of all
1629 * preceding secure trim 1 commands. Since the power may have been
1630 * lost since the secure trim 1 commands occurred, it is generally
1631 * impossible to calculate the secure trim 2 timeout correctly.
1632 */
1633 if (card->erase_shift)
1634 qty += ((to >> card->erase_shift) -
1635 (from >> card->erase_shift)) + 1;
1636 else if (mmc_card_sd(card))
1637 qty += to - from + 1;
1638 else
9b9c665a
AA
1639 qty += (mmc_sector_div(to, card->erase_size) -
1640 mmc_sector_div(from, card->erase_size)) + 1;
dfe86cba
AH
1641
1642 if (!mmc_card_blockaddr(card)) {
1643 from <<= 9;
1644 to <<= 9;
1645 }
1646
dfe86cba
AH
1647 if (mmc_card_sd(card))
1648 cmd.opcode = SD_ERASE_WR_BLK_START;
1649 else
1650 cmd.opcode = MMC_ERASE_GROUP_START;
1651 cmd.arg = from;
1652 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
c2d8d495
AA
1653
1654 if (mmc_card_ult_capacity(card)) {
1655 cmd.ext_addr = from >> 32;
1656 cmd.has_ext_addr = true;
1657 }
1658
dfe86cba
AH
1659 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1660 if (err) {
a3c76eb9 1661 pr_err("mmc_erase: group start error %d, "
dfe86cba 1662 "status %#x\n", err, cmd.resp[0]);
67716327 1663 err = -EIO;
dfe86cba
AH
1664 goto out;
1665 }
1666
1667 memset(&cmd, 0, sizeof(struct mmc_command));
1668 if (mmc_card_sd(card))
1669 cmd.opcode = SD_ERASE_WR_BLK_END;
1670 else
1671 cmd.opcode = MMC_ERASE_GROUP_END;
1672 cmd.arg = to;
1673 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
c2d8d495
AA
1674
1675 if (mmc_card_ult_capacity(card)) {
1676 cmd.ext_addr = to >> 32;
1677 cmd.has_ext_addr = true;
1678 }
1679
dfe86cba
AH
1680 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1681 if (err) {
a3c76eb9 1682 pr_err("mmc_erase: group end error %d, status %#x\n",
dfe86cba 1683 err, cmd.resp[0]);
67716327 1684 err = -EIO;
dfe86cba
AH
1685 goto out;
1686 }
1687
1688 memset(&cmd, 0, sizeof(struct mmc_command));
1689 cmd.opcode = MMC_ERASE;
1690 cmd.arg = arg;
bb4eecf2 1691 busy_timeout = mmc_erase_timeout(card, arg, qty);
e62f1e0b 1692 use_r1b_resp = mmc_prepare_busy_cmd(card->host, &cmd, busy_timeout);
bb4eecf2 1693
dfe86cba
AH
1694 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1695 if (err) {
a3c76eb9 1696 pr_err("mmc_erase: erase error %d, status %#x\n",
dfe86cba
AH
1697 err, cmd.resp[0]);
1698 err = -EIO;
1699 goto out;
1700 }
1701
1702 if (mmc_host_is_spi(card->host))
1703 goto out;
1704
bb4eecf2
BW
1705 /*
1706 * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling
1707 * shall be avoided.
1708 */
1709 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp)
1710 goto out;
1711
0d84c3e6 1712 /* Let's poll to find out when the erase operation completes. */
04f967ad 1713 err = mmc_poll_for_busy(card, busy_timeout, false, MMC_BUSY_ERASE);
8fee476b 1714
dfe86cba 1715out:
8f11d106 1716 mmc_retune_release(card->host);
dfe86cba
AH
1717 return err;
1718}
1719
71085123 1720static unsigned int mmc_align_erase_size(struct mmc_card *card,
9b9c665a
AA
1721 sector_t *from,
1722 sector_t *to,
71085123
BW
1723 unsigned int nr)
1724{
9b9c665a
AA
1725 sector_t from_new = *from;
1726 unsigned int nr_new = nr, rem;
71085123 1727
6c689886
BW
1728 /*
1729 * When the 'card->erase_size' is power of 2, we can use round_up/down()
1730 * to align the erase size efficiently.
1731 */
1732 if (is_power_of_2(card->erase_size)) {
9b9c665a 1733 sector_t temp = from_new;
6c689886
BW
1734
1735 from_new = round_up(temp, card->erase_size);
1736 rem = from_new - temp;
1737
71085123
BW
1738 if (nr_new > rem)
1739 nr_new -= rem;
1740 else
1741 return 0;
71085123 1742
6c689886
BW
1743 nr_new = round_down(nr_new, card->erase_size);
1744 } else {
9b9c665a 1745 rem = mmc_sector_mod(from_new, card->erase_size);
6c689886
BW
1746 if (rem) {
1747 rem = card->erase_size - rem;
1748 from_new += rem;
1749 if (nr_new > rem)
1750 nr_new -= rem;
1751 else
1752 return 0;
1753 }
1754
1755 rem = nr_new % card->erase_size;
1756 if (rem)
1757 nr_new -= rem;
1758 }
71085123
BW
1759
1760 if (nr_new == 0)
1761 return 0;
1762
1763 *to = from_new + nr_new;
1764 *from = from_new;
1765
1766 return nr_new;
1767}
1768
dfe86cba
AH
1769/**
1770 * mmc_erase - erase sectors.
1771 * @card: card to erase
1772 * @from: first sector to erase
1773 * @nr: number of sectors to erase
bc47e2f6 1774 * @arg: erase command argument
dfe86cba
AH
1775 *
1776 * Caller must claim host before calling this function.
1777 */
9b9c665a 1778int mmc_erase(struct mmc_card *card, sector_t from, unsigned int nr,
dfe86cba
AH
1779 unsigned int arg)
1780{
9b9c665a
AA
1781 unsigned int rem;
1782 sector_t to = from + nr;
1783
642c28ab 1784 int err;
dfe86cba 1785
94fe2580 1786 if (!(card->csd.cmdclass & CCC_ERASE))
dfe86cba
AH
1787 return -EOPNOTSUPP;
1788
1789 if (!card->erase_size)
1790 return -EOPNOTSUPP;
1791
bc47e2f6 1792 if (mmc_card_sd(card) && arg != SD_ERASE_ARG && arg != SD_DISCARD_ARG)
dfe86cba
AH
1793 return -EOPNOTSUPP;
1794
bc47e2f6 1795 if (mmc_card_mmc(card) && (arg & MMC_SECURE_ARGS) &&
dfe86cba
AH
1796 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1797 return -EOPNOTSUPP;
1798
489d1445 1799 if (mmc_card_mmc(card) && is_trim_arg(arg) &&
dfe86cba
AH
1800 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1801 return -EOPNOTSUPP;
1802
1803 if (arg == MMC_SECURE_ERASE_ARG) {
9b9c665a 1804 if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size)
dfe86cba
AH
1805 return -EINVAL;
1806 }
1807
71085123
BW
1808 if (arg == MMC_ERASE_ARG)
1809 nr = mmc_align_erase_size(card, &from, &to, nr);
dfe86cba
AH
1810
1811 if (nr == 0)
1812 return 0;
1813
dfe86cba
AH
1814 if (to <= from)
1815 return -EINVAL;
1816
1817 /* 'from' and 'to' are inclusive */
1818 to -= 1;
1819
642c28ab
DJ
1820 /*
1821 * Special case where only one erase-group fits in the timeout budget:
1822 * If the region crosses an erase-group boundary on this particular
1823 * case, we will be trimming more than one erase-group which, does not
1824 * fit in the timeout budget of the controller, so we need to split it
1825 * and call mmc_do_erase() twice if necessary. This special case is
1826 * identified by the card->eg_boundary flag.
1827 */
9b9c665a 1828 rem = card->erase_size - mmc_sector_mod(from, card->erase_size);
489d1445 1829 if ((arg & MMC_TRIM_OR_DISCARD_ARGS) && card->eg_boundary && nr > rem) {
642c28ab
DJ
1830 err = mmc_do_erase(card, from, from + rem - 1, arg);
1831 from += rem;
1832 if ((err) || (to <= from))
1833 return err;
1834 }
1835
dfe86cba
AH
1836 return mmc_do_erase(card, from, to, arg);
1837}
1838EXPORT_SYMBOL(mmc_erase);
1839
5513d9be 1840bool mmc_card_can_erase(struct mmc_card *card)
dfe86cba 1841{
5513d9be 1842 return (card->csd.cmdclass & CCC_ERASE && card->erase_size);
dfe86cba 1843}
5513d9be 1844EXPORT_SYMBOL(mmc_card_can_erase);
dfe86cba 1845
b89d05f6 1846bool mmc_card_can_trim(struct mmc_card *card)
dfe86cba 1847{
b89d05f6
WS
1848 return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
1849 (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)));
dfe86cba 1850}
b89d05f6 1851EXPORT_SYMBOL(mmc_card_can_trim);
dfe86cba 1852
76d62cde 1853bool mmc_card_can_discard(struct mmc_card *card)
b3bf9153
KP
1854{
1855 /*
1856 * As there's no way to detect the discard support bit at v4.5
1857 * use the s/w feature support filed.
1858 */
76d62cde 1859 return (card->ext_csd.feature_support & MMC_DISCARD_FEATURE);
b3bf9153 1860}
76d62cde 1861EXPORT_SYMBOL(mmc_card_can_discard);
b3bf9153 1862
2abda048 1863bool mmc_card_can_sanitize(struct mmc_card *card)
d9ddd629 1864{
b89d05f6 1865 if (!mmc_card_can_trim(card) && !mmc_card_can_erase(card))
2abda048 1866 return false;
d9ddd629 1867 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2abda048
WS
1868 return true;
1869 return false;
d9ddd629 1870}
d9ddd629 1871
55e0961a 1872bool mmc_card_can_secure_erase_trim(struct mmc_card *card)
dfe86cba 1873{
55e0961a
WS
1874 return ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
1875 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN));
dfe86cba 1876}
55e0961a 1877EXPORT_SYMBOL(mmc_card_can_secure_erase_trim);
dfe86cba 1878
9b9c665a 1879int mmc_erase_group_aligned(struct mmc_card *card, sector_t from,
dfe86cba
AH
1880 unsigned int nr)
1881{
1882 if (!card->erase_size)
1883 return 0;
9b9c665a 1884 if (mmc_sector_mod(from, card->erase_size) || nr % card->erase_size)
dfe86cba
AH
1885 return 0;
1886 return 1;
1887}
1888EXPORT_SYMBOL(mmc_erase_group_aligned);
1da177e4 1889
e056a1b5
AH
1890static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1891 unsigned int arg)
1892{
1893 struct mmc_host *host = card->host;
bb4eecf2 1894 unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
e056a1b5 1895 unsigned int last_timeout = 0;
12182aff
UH
1896 unsigned int max_busy_timeout = host->max_busy_timeout ?
1897 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
e056a1b5 1898
bb4eecf2 1899 if (card->erase_shift) {
e056a1b5 1900 max_qty = UINT_MAX >> card->erase_shift;
bb4eecf2
BW
1901 min_qty = card->pref_erase >> card->erase_shift;
1902 } else if (mmc_card_sd(card)) {
e056a1b5 1903 max_qty = UINT_MAX;
bb4eecf2
BW
1904 min_qty = card->pref_erase;
1905 } else {
e056a1b5 1906 max_qty = UINT_MAX / card->erase_size;
bb4eecf2
BW
1907 min_qty = card->pref_erase / card->erase_size;
1908 }
e056a1b5 1909
bb4eecf2
BW
1910 /*
1911 * We should not only use 'host->max_busy_timeout' as the limitation
1912 * when deciding the max discard sectors. We should set a balance value
1913 * to improve the erase speed, and it can not get too long timeout at
1914 * the same time.
1915 *
1916 * Here we set 'card->pref_erase' as the minimal discard sectors no
1917 * matter what size of 'host->max_busy_timeout', but if the
1918 * 'host->max_busy_timeout' is large enough for more discard sectors,
1919 * then we can continue to increase the max discard sectors until we
12182aff
UH
1920 * get a balance value. In cases when the 'host->max_busy_timeout'
1921 * isn't specified, use the default max erase timeout.
bb4eecf2 1922 */
e056a1b5
AH
1923 do {
1924 y = 0;
1925 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1926 timeout = mmc_erase_timeout(card, arg, qty + x);
bb4eecf2 1927
12182aff 1928 if (qty + x > min_qty && timeout > max_busy_timeout)
e056a1b5 1929 break;
bb4eecf2 1930
e056a1b5
AH
1931 if (timeout < last_timeout)
1932 break;
1933 last_timeout = timeout;
1934 y = x;
1935 }
1936 qty += y;
1937 } while (y);
1938
1939 if (!qty)
1940 return 0;
1941
642c28ab
DJ
1942 /*
1943 * When specifying a sector range to trim, chances are we might cross
1944 * an erase-group boundary even if the amount of sectors is less than
1945 * one erase-group.
1946 * If we can only fit one erase-group in the controller timeout budget,
1947 * we have to care that erase-group boundaries are not crossed by a
1948 * single trim operation. We flag that special case with "eg_boundary".
1949 * In all other cases we can just decrement qty and pretend that we
1950 * always touch (qty + 1) erase-groups as a simple optimization.
1951 */
e056a1b5 1952 if (qty == 1)
642c28ab
DJ
1953 card->eg_boundary = 1;
1954 else
1955 qty--;
e056a1b5
AH
1956
1957 /* Convert qty to sectors */
1958 if (card->erase_shift)
642c28ab 1959 max_discard = qty << card->erase_shift;
e056a1b5 1960 else if (mmc_card_sd(card))
642c28ab 1961 max_discard = qty + 1;
e056a1b5 1962 else
642c28ab 1963 max_discard = qty * card->erase_size;
e056a1b5
AH
1964
1965 return max_discard;
1966}
1967
1968unsigned int mmc_calc_max_discard(struct mmc_card *card)
1969{
1970 struct mmc_host *host = card->host;
1971 unsigned int max_discard, max_trim;
1972
e056a1b5
AH
1973 /*
1974 * Without erase_group_def set, MMC erase timeout depends on clock
1975 * frequence which can change. In that case, the best choice is
1976 * just the preferred erase size.
1977 */
1978 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1979 return card->pref_erase;
1980
1981 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
b89d05f6 1982 if (mmc_card_can_trim(card)) {
e056a1b5 1983 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
d4721339 1984 if (max_trim < max_discard || max_discard == 0)
e056a1b5
AH
1985 max_discard = max_trim;
1986 } else if (max_discard < card->erase_size) {
1987 max_discard = 0;
1988 }
1989 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
12182aff
UH
1990 mmc_hostname(host), max_discard, host->max_busy_timeout ?
1991 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
e056a1b5
AH
1992 return max_discard;
1993}
1994EXPORT_SYMBOL(mmc_calc_max_discard);
1995
33e6d74d
UH
1996bool mmc_card_is_blockaddr(struct mmc_card *card)
1997{
1998 return card ? mmc_card_blockaddr(card) : false;
1999}
2000EXPORT_SYMBOL(mmc_card_is_blockaddr);
2001
0f8d8ea6
AH
2002int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2003{
c7836d15 2004 struct mmc_command cmd = {};
0f8d8ea6 2005
1712c937
ZX
2006 if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
2007 mmc_card_hs400(card) || mmc_card_hs400es(card))
0f8d8ea6
AH
2008 return 0;
2009
0f8d8ea6
AH
2010 cmd.opcode = MMC_SET_BLOCKLEN;
2011 cmd.arg = blocklen;
2012 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2013 return mmc_wait_for_cmd(card->host, &cmd, 5);
2014}
2015EXPORT_SYMBOL(mmc_set_blocklen);
2016
b2499518
AH
2017static void mmc_hw_reset_for_init(struct mmc_host *host)
2018{
52c8212d
UH
2019 mmc_pwrseq_reset(host);
2020
32f18e59 2021 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->card_hw_reset)
b2499518 2022 return;
32f18e59 2023 host->ops->card_hw_reset(host);
b2499518
AH
2024}
2025
3439c588
WS
2026/**
2027 * mmc_hw_reset - reset the card in hardware
b71597ed 2028 * @card: card to be reset
3439c588
WS
2029 *
2030 * Hard reset the card. This function is only for upper layers, like the
2031 * block layer or card drivers. You cannot use it in host drivers (struct
2032 * mmc_card might be gone then).
2033 *
2034 * Return: 0 on success, -errno on failure
2035 */
b71597ed 2036int mmc_hw_reset(struct mmc_card *card)
b2499518 2037{
b71597ed 2038 struct mmc_host *host = card->host;
f855a371 2039 int ret;
b2499518 2040
3a3db603 2041 ret = host->bus_ops->hw_reset(host);
2ac55d5e 2042 if (ret < 0)
3a3db603 2043 pr_warn("%s: tried to HW reset card, got error %d\n",
4e6c7178 2044 mmc_hostname(host), ret);
b2499518 2045
f855a371 2046 return ret;
b2499518 2047}
b2499518
AH
2048EXPORT_SYMBOL(mmc_hw_reset);
2049
9723f69d 2050int mmc_sw_reset(struct mmc_card *card)
1433269c 2051{
9723f69d 2052 struct mmc_host *host = card->host;
1433269c
UH
2053 int ret;
2054
fefdd3c9 2055 if (!host->bus_ops->sw_reset)
1433269c 2056 return -EOPNOTSUPP;
1433269c
UH
2057
2058 ret = host->bus_ops->sw_reset(host);
1433269c
UH
2059 if (ret)
2060 pr_warn("%s: tried to SW reset card, got error %d\n",
2061 mmc_hostname(host), ret);
2062
2063 return ret;
2064}
2065EXPORT_SYMBOL(mmc_sw_reset);
2066
807e8e40
AR
2067static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2068{
2069 host->f_init = freq;
2070
69f25f9b 2071 pr_debug("%s: %s: trying to init card at %u Hz\n",
807e8e40 2072 mmc_hostname(host), __func__, host->f_init);
69f25f9b 2073
4a065193 2074 mmc_power_up(host, host->ocr_avail);
2f94e55a 2075
b2499518
AH
2076 /*
2077 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2078 * do a hardware reset if possible.
2079 */
2080 mmc_hw_reset_for_init(host);
2081
2f94e55a
PR
2082 /*
2083 * sdio_reset sends CMD52 to reset card. Since we do not know
2084 * if the card is being re-initialized, just send it. CMD52
2085 * should be ignored by SD/eMMC cards.
100a606d 2086 * Skip it if we already know that we do not support SDIO commands
2f94e55a 2087 */
100a606d
CC
2088 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
2089 sdio_reset(host);
2090
807e8e40
AR
2091 mmc_go_idle(host);
2092
ead49373
UH
2093 if (!(host->caps2 & MMC_CAP2_NO_SD)) {
2094 if (mmc_send_if_cond_pcie(host, host->ocr_avail))
2095 goto out;
2096 if (mmc_card_sd_express(host))
2097 return 0;
2098 }
807e8e40
AR
2099
2100 /* Order's important: probe SDIO, then SD, then MMC */
100a606d
CC
2101 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
2102 if (!mmc_attach_sdio(host))
2103 return 0;
2104
1b8d79c5
UH
2105 if (!(host->caps2 & MMC_CAP2_NO_SD))
2106 if (!mmc_attach_sd(host))
2107 return 0;
2108
a0c3b68c
SL
2109 if (!(host->caps2 & MMC_CAP2_NO_MMC))
2110 if (!mmc_attach_mmc(host))
2111 return 0;
807e8e40 2112
ead49373 2113out:
807e8e40
AR
2114 mmc_power_off(host);
2115 return -EIO;
2116}
2117
d3049504
AH
2118int _mmc_detect_card_removed(struct mmc_host *host)
2119{
2120 int ret;
2121
d3049504
AH
2122 if (!host->card || mmc_card_removed(host->card))
2123 return 1;
2124
2125 ret = host->bus_ops->alive(host);
1450734e
KL
2126
2127 /*
2128 * Card detect status and alive check may be out of sync if card is
2129 * removed slowly, when card detect switch changes while card/slot
2130 * pads are still contacted in hardware (refer to "SD Card Mechanical
2131 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2132 * detect work 200ms later for this case.
2133 */
2134 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2135 mmc_detect_change(host, msecs_to_jiffies(200));
2136 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2137 }
2138
d3049504
AH
2139 if (ret) {
2140 mmc_card_set_removed(host->card);
2141 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2142 }
2143
2144 return ret;
2145}
2146
2147int mmc_detect_card_removed(struct mmc_host *host)
2148{
2149 struct mmc_card *card = host->card;
f0cc9cf9 2150 int ret;
d3049504
AH
2151
2152 WARN_ON(!host->claimed);
f0cc9cf9
UH
2153
2154 if (!card)
2155 return 1;
2156
6067bafe 2157 if (!mmc_card_is_removable(host))
1ff2575b
UH
2158 return 0;
2159
f0cc9cf9 2160 ret = mmc_card_removed(card);
d3049504
AH
2161 /*
2162 * The card will be considered unchanged unless we have been asked to
2163 * detect a change or host requires polling to provide card detection.
2164 */
b6891679 2165 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
f0cc9cf9 2166 return ret;
d3049504
AH
2167
2168 host->detect_change = 0;
f0cc9cf9
UH
2169 if (!ret) {
2170 ret = _mmc_detect_card_removed(host);
b6891679 2171 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
f0cc9cf9
UH
2172 /*
2173 * Schedule a detect work as soon as possible to let a
2174 * rescan handle the card removal.
2175 */
2176 cancel_delayed_work(&host->detect);
bbd43682 2177 _mmc_detect_change(host, 0, false);
f0cc9cf9
UH
2178 }
2179 }
d3049504 2180
f0cc9cf9 2181 return ret;
d3049504
AH
2182}
2183EXPORT_SYMBOL(mmc_detect_card_removed);
2184
dc913385
DO
2185int mmc_card_alternative_gpt_sector(struct mmc_card *card, sector_t *gpt_sector)
2186{
2187 unsigned int boot_sectors_num;
2188
2189 if ((!(card->host->caps2 & MMC_CAP2_ALT_GPT_TEGRA)))
2190 return -EOPNOTSUPP;
2191
2192 /* filter out unrelated cards */
2193 if (card->ext_csd.rev < 3 ||
2194 !mmc_card_mmc(card) ||
2195 !mmc_card_is_blockaddr(card) ||
2196 mmc_card_is_removable(card->host))
2197 return -ENOENT;
2198
2199 /*
2200 * eMMC storage has two special boot partitions in addition to the
2201 * main one. NVIDIA's bootloader linearizes eMMC boot0->boot1->main
2202 * accesses, this means that the partition table addresses are shifted
2203 * by the size of boot partitions. In accordance with the eMMC
2204 * specification, the boot partition size is calculated as follows:
2205 *
2206 * boot partition size = 128K byte x BOOT_SIZE_MULT
2207 *
2208 * Calculate number of sectors occupied by the both boot partitions.
2209 */
2210 boot_sectors_num = card->ext_csd.raw_boot_mult * SZ_128K /
2211 SZ_512 * MMC_NUM_BOOT_PARTITION;
2212
2213 /* Defined by NVIDIA and used by Android devices. */
2214 *gpt_sector = card->ext_csd.sectors - boot_sectors_num - 1;
2215
2216 return 0;
2217}
2218EXPORT_SYMBOL(mmc_card_alternative_gpt_sector);
2219
fa700d73 2220void mmc_rescan(struct work_struct *work)
1da177e4 2221{
fa700d73
UH
2222 struct mmc_host *host =
2223 container_of(work, struct mmc_host, detect.work);
88ae8b86 2224 int i;
4c2ef25f 2225
807e8e40 2226 if (host->rescan_disable)
4c2ef25f 2227 return;
1da177e4 2228
3339d1e3 2229 /* If there is a non-removable card registered, only scan once */
6067bafe 2230 if (!mmc_card_is_removable(host) && host->rescan_entered)
3339d1e3
JR
2231 return;
2232 host->rescan_entered = 1;
2233
86236813 2234 if (host->trigger_card_event && host->ops->card_event) {
d234d212 2235 mmc_claim_host(host);
86236813 2236 host->ops->card_event(host);
d234d212 2237 mmc_release_host(host);
86236813
UH
2238 host->trigger_card_event = false;
2239 }
2240
99b4ddd8 2241 /* Verify a registered card to be functional, else remove it. */
e9ce2ce1 2242 if (host->bus_ops)
94d89efb
JS
2243 host->bus_ops->detect(host);
2244
d3049504
AH
2245 host->detect_change = 0;
2246
94d89efb 2247 /* if there still is a card present, stop here */
e9ce2ce1 2248 if (host->bus_ops != NULL)
94d89efb 2249 goto out;
1da177e4 2250
d234d212 2251 mmc_claim_host(host);
6067bafe 2252 if (mmc_card_is_removable(host) && host->ops->get_cd &&
c1b55bfc 2253 host->ops->get_cd(host) == 0) {
fa550189
UH
2254 mmc_power_off(host);
2255 mmc_release_host(host);
94d89efb 2256 goto out;
fa550189 2257 }
1da177e4 2258
ead49373
UH
2259 /* If an SD express card is present, then leave it as is. */
2260 if (mmc_card_sd_express(host)) {
2261 mmc_release_host(host);
2262 goto out;
2263 }
2264
79daeb24
UH
2265 /*
2266 * Ideally we should favor initialization of legacy SD cards and defer
2267 * UHS-II enumeration. However, it seems like cards doesn't reliably
2268 * announce their support for UHS-II in the response to the ACMD41,
2269 * while initializing the legacy SD interface. Therefore, let's start
2270 * with UHS-II for now.
2271 */
2272 if (!mmc_attach_sd_uhs2(host)) {
2273 mmc_release_host(host);
2274 goto out;
2275 }
2276
88ae8b86 2277 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
661cf2d8
MM
2278 unsigned int freq = freqs[i];
2279 if (freq > host->f_max) {
2280 if (i + 1 < ARRAY_SIZE(freqs))
2281 continue;
2282 freq = host->f_max;
2283 }
2284 if (!mmc_rescan_try_freq(host, max(freq, host->f_min)))
807e8e40 2285 break;
06b2233a 2286 if (freqs[i] <= host->f_min)
807e8e40 2287 break;
88ae8b86 2288 }
91f059c9 2289
f6ca8f90
MG
2290 /* A non-removable card should have been detected by now. */
2291 if (!mmc_card_is_removable(host) && !host->bus_ops)
2292 pr_info("%s: Failed to initialize a non-removable card",
2293 mmc_hostname(host));
2294
91f059c9
SSB
2295 /*
2296 * Ignore the command timeout errors observed during
2297 * the card init as those are excepted.
2298 */
2299 host->err_stats[MMC_ERR_CMD_TIMEOUT] = 0;
807e8e40
AR
2300 mmc_release_host(host);
2301
2302 out:
28f52482
AV
2303 if (host->caps & MMC_CAP_NEEDS_POLL)
2304 mmc_schedule_delayed_work(&host->detect, HZ);
1da177e4
LT
2305}
2306
b93931a6 2307void mmc_start_host(struct mmc_host *host)
1da177e4 2308{
79daeb24
UH
2309 bool power_up = !(host->caps2 &
2310 (MMC_CAP2_NO_PRESCAN_POWERUP | MMC_CAP2_SD_UHS2));
2311
661cf2d8 2312 host->f_init = max(min(freqs[0], host->f_max), host->f_min);
d9adcc12 2313 host->rescan_disable = 0;
8d1ffc8c 2314
79daeb24 2315 if (power_up) {
c2c24819 2316 mmc_claim_host(host);
4a065193 2317 mmc_power_up(host, host->ocr_avail);
c2c24819
UH
2318 mmc_release_host(host);
2319 }
8d1ffc8c 2320
740a221e 2321 mmc_gpiod_request_cd_irq(host);
fa700d73 2322 _mmc_detect_change(host, 0, false);
1da177e4
LT
2323}
2324
66c915d0 2325void __mmc_stop_host(struct mmc_host *host)
1da177e4 2326{
87a0d90f
UH
2327 if (host->rescan_disable)
2328 return;
2329
03dbaa04 2330 if (host->slot.cd_irq >= 0) {
36f1d7e8 2331 mmc_gpio_set_cd_wake(host, false);
740a221e 2332 disable_irq(host->slot.cd_irq);
03dbaa04 2333 }
3b91e550 2334
d9adcc12 2335 host->rescan_disable = 1;
d9bcbf34 2336 cancel_delayed_work_sync(&host->detect);
66c915d0
UH
2337}
2338
2339void mmc_stop_host(struct mmc_host *host)
2340{
2341 __mmc_stop_host(host);
3b91e550 2342
da68c4eb
NP
2343 /* clear pm flags now and let card drivers set them as needed */
2344 host->pm_flags = 0;
2345
e9ce2ce1 2346 if (host->bus_ops) {
0db13fc2 2347 /* Calling bus_ops->remove() with a claimed host can deadlock */
58a8a4a1 2348 host->bus_ops->remove(host);
7ea239d9
PO
2349 mmc_claim_host(host);
2350 mmc_detach_bus(host);
7f7e4129 2351 mmc_power_off(host);
7ea239d9 2352 mmc_release_host(host);
53509f0f 2353 return;
1da177e4 2354 }
7ea239d9 2355
8d1ffc8c 2356 mmc_claim_host(host);
1da177e4 2357 mmc_power_off(host);
8d1ffc8c 2358 mmc_release_host(host);
1da177e4
LT
2359}
2360
ffce2e7e
PO
2361static int __init mmc_init(void)
2362{
2363 int ret;
2364
ffce2e7e 2365 ret = mmc_register_bus();
e29a7d73 2366 if (ret)
520bd7a8 2367 return ret;
e29a7d73
PO
2368
2369 ret = mmc_register_host_class();
2370 if (ret)
2371 goto unregister_bus;
2372
2373 ret = sdio_register_bus();
2374 if (ret)
2375 goto unregister_host_class;
2376
2377 return 0;
2378
2379unregister_host_class:
2380 mmc_unregister_host_class();
2381unregister_bus:
2382 mmc_unregister_bus();
ffce2e7e
PO
2383 return ret;
2384}
2385
2386static void __exit mmc_exit(void)
2387{
e29a7d73 2388 sdio_unregister_bus();
ffce2e7e
PO
2389 mmc_unregister_host_class();
2390 mmc_unregister_bus();
ffce2e7e
PO
2391}
2392
26074962 2393subsys_initcall(mmc_init);
ffce2e7e
PO
2394module_exit(mmc_exit);
2395
8eb57fd0 2396MODULE_DESCRIPTION("MMC core driver");
1da177e4 2397MODULE_LICENSE("GPL");