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