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