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