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