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