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