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