blob: 007c44443332cf0551ab5fe66756b3e7a62931d8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossmanaaac1b42007-02-28 15:33:10 +01002 * linux/drivers/mmc/core/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
Pierre Ossman5b4fd9a2005-09-06 15:18:56 -07005 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
Pierre Ossmanad3868b2008-06-28 12:52:45 +02006 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Philip Langdalebce40a32006-10-21 12:35:02 +02007 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#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>
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020021#include <linux/leds.h>
Pierre Ossmanb57c43a2005-09-06 15:18:53 -070022#include <linux/scatterlist.h>
Anton Vorontsov86e82862008-11-26 22:54:17 +030023#include <linux/log2.h>
David Brownell5c139412009-03-11 03:30:43 -080024#include <linux/regulator/consumer.h>
Ohad Ben-Cohene5945732010-11-28 07:21:30 +020025#include <linux/pm_runtime.h>
Ulf Hanssonbbd43682013-09-20 11:02:35 +020026#include <linux/pm_wakeup.h>
Amerigo Wang35eb6db2011-07-25 17:13:11 -070027#include <linux/suspend.h>
Per Forlin1b676f72011-08-19 14:52:37 +020028#include <linux/fault-inject.h>
29#include <linux/random.h>
Jaehoon Chung950d56a2012-09-17 08:42:02 +000030#include <linux/slab.h>
Haijun Zhang6e9e3182013-08-26 09:19:22 +080031#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/mmc/card.h>
34#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010035#include <linux/mmc/mmc.h>
36#include <linux/mmc/sd.h>
Adrian Hunter740a2212014-03-10 15:02:41 +020037#include <linux/mmc/slot-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010039#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020040#include "bus.h"
41#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020042#include "sdio_bus.h"
Ulf Hansson3aa87932014-11-28 14:38:36 +010043#include "pwrseq.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010044
45#include "mmc_ops.h"
46#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020047#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Trey Ramsay8fee4762012-11-16 09:31:41 -060049/* If the device is not responding */
50#define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
51
Jaehoon Chung950d56a2012-09-17 08:42:02 +000052/*
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
Pierre Ossmanffce2e72007-05-19 14:32:22 +020058static struct workqueue_struct *workqueue;
Ulf Hanssonfa550182012-05-09 16:15:26 +020059static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
Pierre Ossmanffce2e72007-05-19 14:32:22 +020060
61/*
David Brownellaf517152007-08-08 09:11:32 -070062 * 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 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103066bool use_spi_crc = 1;
David Brownellaf517152007-08-08 09:11:32 -070067module_param(use_spi_crc, bool, 0);
68
69/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020070 * 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
Per Forlin1b676f72011-08-19 14:52:37 +020086#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
Akinobu Mita2e744fc2013-04-29 16:21:31 -0700110 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
111 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
Per Forlin1b676f72011-08-19 14:52:37 +0200112}
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/**
Russell Kingfe10c6a2006-05-04 13:51:45 +0100124 * mmc_request_done - finish processing an MMC request
125 * @host: MMC host which completed request
126 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 *
128 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +0100129 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 */
131void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
132{
133 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +0100134 int err = cmd->error;
135
David Brownellaf517152007-08-08 09:11:32 -0700136 if (err && cmd->retries && mmc_host_is_spi(host)) {
137 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
138 cmd->retries = 0;
139 }
140
Adrian Hunterd3049502011-11-28 16:22:00 +0200141 if (err && cmd->retries && !mmc_card_removed(host->card)) {
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300142 /*
143 * Request starter must handle retries - see
144 * mmc_wait_for_req_done().
145 */
146 if (mrq->done)
147 mrq->done(mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +0200148 } else {
Per Forlin1b676f72011-08-19 14:52:37 +0200149 mmc_should_fail_request(host, mrq);
150
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200151 led_trigger_event(host->led, LED_OFF);
152
Andrew Gabbasovfc75b702014-10-01 07:14:10 -0500153 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
Pierre Ossmane4d21702007-07-24 21:46:49 +0200161 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);
Linus Walleij04566832010-11-08 21:36:50 -0500182
Mika Westerberg08c14072011-08-18 15:23:47 +0300183 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
185}
186
187EXPORT_SYMBOL(mmc_request_done);
188
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200189static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Pierre Ossman976d9272007-04-13 22:47:01 +0200191#ifdef CONFIG_MMC_DEBUG
192 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200193 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200194#endif
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200195 if (mmc_card_removed(host->card))
196 return -ENOMEDIUM;
Pierre Ossman976d9272007-04-13 22:47:01 +0200197
Jaehoon Chung7b2fd4f2012-02-07 14:13:10 +0900198 if (mrq->sbc) {
199 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
200 mmc_hostname(host), mrq->sbc->opcode,
201 mrq->sbc->arg, mrq->sbc->flags);
202 }
203
Russell King920e70c2006-05-04 18:22:51 +0100204 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
205 mmc_hostname(host), mrq->cmd->opcode,
206 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Pierre Ossmane4d21702007-07-24 21:46:49 +0200208 if (mrq->data) {
209 pr_debug("%s: blksz %d blocks %d flags %08x "
210 "tsac %d ms nsac %d\n",
211 mmc_hostname(host), mrq->data->blksz,
212 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200213 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200214 mrq->data->timeout_clks);
215 }
216
217 if (mrq->stop) {
218 pr_debug("%s: CMD%u arg %08x flags %08x\n",
219 mmc_hostname(host), mrq->stop->opcode,
220 mrq->stop->arg, mrq->stop->flags);
221 }
222
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100223 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 mrq->cmd->error = 0;
226 mrq->cmd->mrq = mrq;
Andrew Gabbasovcce411e2014-10-01 07:14:09 -0500227 if (mrq->sbc) {
228 mrq->sbc->error = 0;
229 mrq->sbc->mrq = mrq;
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100232 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100233 BUG_ON(mrq->data->blocks > host->max_blk_count);
234 BUG_ON(mrq->data->blocks * mrq->data->blksz >
235 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100236
Pierre Ossman976d9272007-04-13 22:47:01 +0200237#ifdef CONFIG_MMC_DEBUG
238 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200239 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
240 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200241 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
242#endif
243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 mrq->cmd->data = mrq->data;
245 mrq->data->error = 0;
246 mrq->data->mrq = mrq;
247 if (mrq->stop) {
248 mrq->data->stop = mrq->stop;
249 mrq->stop->error = 0;
250 mrq->stop->mrq = mrq;
251 }
252 }
Mika Westerberg08c14072011-08-18 15:23:47 +0300253 mmc_host_clk_hold(host);
Pierre Tardy66c036e2011-02-06 19:02:48 +0100254 led_trigger_event(host->led, LED_FULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 host->ops->request(host, mrq);
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200256
257 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000260/**
261 * mmc_start_bkops - start BKOPS for supported cards
262 * @card: MMC card to start BKOPS
263 * @form_exception: A flag to indicate if this function was
264 * called due to an exception raised by the card
265 *
266 * Start background operations whenever requested.
267 * When the urgent BKOPS bit is set in a R1 command response
268 * then background operations should be started immediately.
269*/
270void mmc_start_bkops(struct mmc_card *card, bool from_exception)
271{
272 int err;
273 int timeout;
274 bool use_busy_signal;
275
276 BUG_ON(!card);
277
Alexey Skidanov0501be62015-01-29 10:49:43 +0200278 if (!card->ext_csd.man_bkops_en || mmc_card_doing_bkops(card))
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000279 return;
280
281 err = mmc_read_bkops_status(card);
282 if (err) {
283 pr_err("%s: Failed to read bkops status: %d\n",
284 mmc_hostname(card->host), err);
285 return;
286 }
287
288 if (!card->ext_csd.raw_bkops_status)
289 return;
290
291 if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
292 from_exception)
293 return;
294
295 mmc_claim_host(card->host);
296 if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
297 timeout = MMC_BKOPS_MAX_TIMEOUT;
298 use_busy_signal = true;
299 } else {
300 timeout = 0;
301 use_busy_signal = false;
302 }
303
304 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Ulf Hansson4509f84772014-01-08 16:09:33 +0100305 EXT_CSD_BKOPS_START, 1, timeout,
306 use_busy_signal, true, false);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000307 if (err) {
308 pr_warn("%s: Error %d starting bkops\n",
309 mmc_hostname(card->host), err);
310 goto out;
311 }
312
313 /*
314 * For urgent bkops status (LEVEL_2 and more)
315 * bkops executed synchronously, otherwise
316 * the operation is in progress
317 */
318 if (!use_busy_signal)
319 mmc_card_set_doing_bkops(card);
320out:
321 mmc_release_host(card->host);
322}
323EXPORT_SYMBOL(mmc_start_bkops);
324
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500325/*
326 * mmc_wait_data_done() - done callback for data request
327 * @mrq: done data request
328 *
329 * Wakes up mmc context, passed as a callback to host controller driver
330 */
331static void mmc_wait_data_done(struct mmc_request *mrq)
332{
333 mrq->host->context_info.is_done_rcv = true;
334 wake_up_interruptible(&mrq->host->context_info.wait);
335}
336
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static void mmc_wait_done(struct mmc_request *mrq)
338{
Per Forlinaa8b6832011-07-01 18:55:22 +0200339 complete(&mrq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500342/*
343 *__mmc_start_data_req() - starts data request
344 * @host: MMC host to start the request
345 * @mrq: data request to start
346 *
347 * Sets the done callback to be called when request is completed by the card.
348 * Starts data mmc request execution
349 */
350static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
351{
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200352 int err;
353
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500354 mrq->done = mmc_wait_data_done;
355 mrq->host = host;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500356
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200357 err = mmc_start_request(host, mrq);
358 if (err) {
359 mrq->cmd->error = err;
360 mmc_wait_data_done(mrq);
361 }
362
363 return err;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500364}
365
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100366static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
Per Forlinaa8b6832011-07-01 18:55:22 +0200367{
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200368 int err;
369
Per Forlinaa8b6832011-07-01 18:55:22 +0200370 init_completion(&mrq->completion);
371 mrq->done = mmc_wait_done;
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200372
373 err = mmc_start_request(host, mrq);
374 if (err) {
375 mrq->cmd->error = err;
Adrian Hunterd3049502011-11-28 16:22:00 +0200376 complete(&mrq->completion);
Adrian Hunterd3049502011-11-28 16:22:00 +0200377 }
Adrian Hunterf100c1c2014-12-05 19:41:02 +0200378
379 return err;
Per Forlinaa8b6832011-07-01 18:55:22 +0200380}
381
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500382/*
383 * mmc_wait_for_data_req_done() - wait for request completed
384 * @host: MMC host to prepare the command.
385 * @mrq: MMC request to wait for
386 *
387 * Blocks MMC context till host controller will ack end of data request
388 * execution or new request notification arrives from the block layer.
389 * Handles command retries.
390 *
391 * Returns enum mmc_blk_status after checking errors.
392 */
393static int mmc_wait_for_data_req_done(struct mmc_host *host,
394 struct mmc_request *mrq,
395 struct mmc_async_req *next_req)
396{
397 struct mmc_command *cmd;
398 struct mmc_context_info *context_info = &host->context_info;
399 int err;
400 unsigned long flags;
401
402 while (1) {
403 wait_event_interruptible(context_info->wait,
404 (context_info->is_done_rcv ||
405 context_info->is_new_req));
406 spin_lock_irqsave(&context_info->lock, flags);
407 context_info->is_waiting_last_req = false;
408 spin_unlock_irqrestore(&context_info->lock, flags);
409 if (context_info->is_done_rcv) {
410 context_info->is_done_rcv = false;
411 context_info->is_new_req = false;
412 cmd = mrq->cmd;
Maya Erez775a9362013-04-18 15:41:55 +0300413
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500414 if (!cmd->error || !cmd->retries ||
415 mmc_card_removed(host->card)) {
416 err = host->areq->err_check(host->card,
417 host->areq);
418 break; /* return err */
419 } else {
420 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
421 mmc_hostname(host),
422 cmd->opcode, cmd->error);
423 cmd->retries--;
424 cmd->error = 0;
425 host->ops->request(host, mrq);
426 continue; /* wait for done/new event again */
427 }
428 } else if (context_info->is_new_req) {
429 context_info->is_new_req = false;
430 if (!next_req) {
431 err = MMC_BLK_NEW_REQUEST;
432 break; /* return err */
433 }
434 }
435 }
436 return err;
437}
438
Per Forlinaa8b6832011-07-01 18:55:22 +0200439static void mmc_wait_for_req_done(struct mmc_host *host,
440 struct mmc_request *mrq)
441{
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300442 struct mmc_command *cmd;
443
444 while (1) {
445 wait_for_completion(&mrq->completion);
446
447 cmd = mrq->cmd;
Maya Erez775a9362013-04-18 15:41:55 +0300448
449 /*
450 * If host has timed out waiting for the sanitize
451 * to complete, card might be still in programming state
452 * so let's try to bring the card out of programming
453 * state.
454 */
455 if (cmd->sanitize_busy && cmd->error == -ETIMEDOUT) {
456 if (!mmc_interrupt_hpi(host->card)) {
Joe Perches66061102014-09-12 14:56:56 -0700457 pr_warn("%s: %s: Interrupted sanitize\n",
458 mmc_hostname(host), __func__);
Maya Erez775a9362013-04-18 15:41:55 +0300459 cmd->error = 0;
460 break;
461 } else {
462 pr_err("%s: %s: Failed to interrupt sanitize\n",
463 mmc_hostname(host), __func__);
464 }
465 }
Adrian Hunterd3049502011-11-28 16:22:00 +0200466 if (!cmd->error || !cmd->retries ||
467 mmc_card_removed(host->card))
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300468 break;
469
470 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
471 mmc_hostname(host), cmd->opcode, cmd->error);
472 cmd->retries--;
473 cmd->error = 0;
474 host->ops->request(host, mrq);
475 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200476}
477
478/**
479 * mmc_pre_req - Prepare for a new request
480 * @host: MMC host to prepare command
481 * @mrq: MMC request to prepare for
482 * @is_first_req: true if there is no previous started request
483 * that may run in parellel to this call, otherwise false
484 *
485 * mmc_pre_req() is called in prior to mmc_start_req() to let
486 * host prepare for the new request. Preparation of a request may be
487 * performed while another request is running on the host.
488 */
489static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
490 bool is_first_req)
491{
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500492 if (host->ops->pre_req) {
493 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +0200494 host->ops->pre_req(host, mrq, is_first_req);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500495 mmc_host_clk_release(host);
496 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200497}
498
499/**
500 * mmc_post_req - Post process a completed request
501 * @host: MMC host to post process command
502 * @mrq: MMC request to post process for
503 * @err: Error, if non zero, clean up any resources made in pre_req
504 *
505 * Let the host post process a completed request. Post processing of
506 * a request may be performed while another reuqest is running.
507 */
508static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
509 int err)
510{
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500511 if (host->ops->post_req) {
512 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +0200513 host->ops->post_req(host, mrq, err);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500514 mmc_host_clk_release(host);
515 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200516}
517
518/**
519 * mmc_start_req - start a non-blocking request
520 * @host: MMC host to start command
521 * @areq: async request to start
522 * @error: out parameter returns 0 for success, otherwise non zero
523 *
524 * Start a new MMC custom command request for a host.
525 * If there is on ongoing async request wait for completion
526 * of that request and start the new one and return.
527 * Does not wait for the new request to complete.
528 *
529 * Returns the completed request, NULL in case of none completed.
530 * Wait for the an ongoing request (previoulsy started) to complete and
531 * return the completed request. If there is no ongoing request, NULL
532 * is returned without waiting. NULL is not an error condition.
533 */
534struct mmc_async_req *mmc_start_req(struct mmc_host *host,
535 struct mmc_async_req *areq, int *error)
536{
537 int err = 0;
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100538 int start_err = 0;
Per Forlinaa8b6832011-07-01 18:55:22 +0200539 struct mmc_async_req *data = host->areq;
540
541 /* Prepare a new request */
542 if (areq)
543 mmc_pre_req(host, areq->mrq, !host->areq);
544
545 if (host->areq) {
Jaehoon Chungf5c27582013-02-01 14:32:22 +0900546 err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq);
547 if (err == MMC_BLK_NEW_REQUEST) {
548 if (error)
549 *error = err;
550 /*
551 * The previous request was not completed,
552 * nothing to return
553 */
554 return NULL;
555 }
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000556 /*
557 * Check BKOPS urgency for each R1 response
558 */
559 if (host->card && mmc_card_mmc(host->card) &&
560 ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
561 (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
Srinivas Kandagatla64b12a62014-10-08 12:24:24 +0100562 (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) {
563
564 /* Cancel the prepared request */
565 if (areq)
566 mmc_post_req(host, areq->mrq, -EINVAL);
567
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000568 mmc_start_bkops(host->card, true);
Srinivas Kandagatla64b12a62014-10-08 12:24:24 +0100569
570 /* prepare the request again */
571 if (areq)
572 mmc_pre_req(host, areq->mrq, !host->areq);
573 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200574 }
575
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100576 if (!err && areq)
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500577 start_err = __mmc_start_data_req(host, areq->mrq);
Per Forlinaa8b6832011-07-01 18:55:22 +0200578
579 if (host->areq)
580 mmc_post_req(host, host->areq->mrq, 0);
581
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100582 /* Cancel a prepared request if it was not started. */
583 if ((err || start_err) && areq)
Jaehoon Chungf5c27582013-02-01 14:32:22 +0900584 mmc_post_req(host, areq->mrq, -EINVAL);
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100585
586 if (err)
587 host->areq = NULL;
588 else
589 host->areq = areq;
590
Per Forlinaa8b6832011-07-01 18:55:22 +0200591 if (error)
592 *error = err;
593 return data;
594}
595EXPORT_SYMBOL(mmc_start_req);
596
Pierre Ossman67a61c42007-07-11 20:22:11 +0200597/**
598 * mmc_wait_for_req - start a request and wait for completion
599 * @host: MMC host to start command
600 * @mrq: MMC request to start
601 *
602 * Start a new MMC custom command request for a host, and wait
603 * for the command to complete. Does not attempt to parse the
604 * response.
605 */
606void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
Per Forlinaa8b6832011-07-01 18:55:22 +0200608 __mmc_start_req(host, mrq);
609 mmc_wait_for_req_done(host, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611EXPORT_SYMBOL(mmc_wait_for_req);
612
613/**
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400614 * mmc_interrupt_hpi - Issue for High priority Interrupt
615 * @card: the MMC card associated with the HPI transfer
616 *
617 * Issued High Priority Interrupt, and check for card status
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000618 * until out-of prg-state.
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400619 */
620int mmc_interrupt_hpi(struct mmc_card *card)
621{
622 int err;
623 u32 status;
Venkatraman S6af9e962012-06-22 11:42:36 +0530624 unsigned long prg_wait;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400625
626 BUG_ON(!card);
627
628 if (!card->ext_csd.hpi_en) {
629 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
630 return 1;
631 }
632
633 mmc_claim_host(card->host);
634 err = mmc_send_status(card, &status);
635 if (err) {
636 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
637 goto out;
638 }
639
Venkatraman S6af9e962012-06-22 11:42:36 +0530640 switch (R1_CURRENT_STATE(status)) {
641 case R1_STATE_IDLE:
642 case R1_STATE_READY:
643 case R1_STATE_STBY:
Venkatraman S211d4fe2012-08-07 19:24:45 +0530644 case R1_STATE_TRAN:
Venkatraman S6af9e962012-06-22 11:42:36 +0530645 /*
Venkatraman S211d4fe2012-08-07 19:24:45 +0530646 * In idle and transfer states, HPI is not needed and the caller
Venkatraman S6af9e962012-06-22 11:42:36 +0530647 * can issue the next intended command immediately
648 */
649 goto out;
650 case R1_STATE_PRG:
651 break;
652 default:
653 /* In all other states, it's illegal to issue HPI */
654 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
655 mmc_hostname(card->host), R1_CURRENT_STATE(status));
656 err = -EINVAL;
657 goto out;
658 }
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400659
Venkatraman S6af9e962012-06-22 11:42:36 +0530660 err = mmc_send_hpi_cmd(card, &status);
661 if (err)
662 goto out;
663
664 prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
665 do {
666 err = mmc_send_status(card, &status);
667
668 if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
669 break;
670 if (time_after(jiffies, prg_wait))
671 err = -ETIMEDOUT;
672 } while (!err);
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400673
674out:
675 mmc_release_host(card->host);
676 return err;
677}
678EXPORT_SYMBOL(mmc_interrupt_hpi);
679
680/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * mmc_wait_for_cmd - start a command and wait for completion
682 * @host: MMC host to start command
683 * @cmd: MMC command to start
684 * @retries: maximum number of retries
685 *
686 * Start a new MMC command for a host, and wait for the command
687 * to complete. Return any error that occurred while the command
688 * was executing. Do not attempt to parse the response.
689 */
690int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
691{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530692 struct mmc_request mrq = {NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Pierre Ossmand84075c82007-08-09 13:23:56 +0200694 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 memset(cmd->resp, 0, sizeof(cmd->resp));
697 cmd->retries = retries;
698
699 mrq.cmd = cmd;
700 cmd->data = NULL;
701
702 mmc_wait_for_req(host, &mrq);
703
704 return cmd->error;
705}
706
707EXPORT_SYMBOL(mmc_wait_for_cmd);
708
Pierre Ossman335eadf2005-09-06 15:18:50 -0700709/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000710 * mmc_stop_bkops - stop ongoing BKOPS
711 * @card: MMC card to check BKOPS
712 *
713 * Send HPI command to stop ongoing background operations to
714 * allow rapid servicing of foreground operations, e.g. read/
715 * writes. Wait until the card comes out of the programming state
716 * to avoid errors in servicing read/write requests.
717 */
718int mmc_stop_bkops(struct mmc_card *card)
719{
720 int err = 0;
721
722 BUG_ON(!card);
723 err = mmc_interrupt_hpi(card);
724
725 /*
726 * If err is EINVAL, we can't issue an HPI.
727 * It should complete the BKOPS.
728 */
729 if (!err || (err == -EINVAL)) {
730 mmc_card_clr_doing_bkops(card);
731 err = 0;
732 }
733
734 return err;
735}
736EXPORT_SYMBOL(mmc_stop_bkops);
737
738int mmc_read_bkops_status(struct mmc_card *card)
739{
740 int err;
741 u8 *ext_csd;
742
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000743 mmc_claim_host(card->host);
Ulf Hanssonb2cada72014-10-17 11:48:23 +0200744 err = mmc_get_ext_csd(card, &ext_csd);
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000745 mmc_release_host(card->host);
746 if (err)
Ulf Hanssonb2cada72014-10-17 11:48:23 +0200747 return err;
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000748
749 card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
750 card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000751 kfree(ext_csd);
Ulf Hanssonb2cada72014-10-17 11:48:23 +0200752 return 0;
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000753}
754EXPORT_SYMBOL(mmc_read_bkops_status);
755
756/**
Russell Kingd773d722006-09-07 15:57:12 +0100757 * mmc_set_data_timeout - set the timeout for a data command
758 * @data: data phase for command
759 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200760 *
761 * Computes the data timeout parameters according to the
762 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100763 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200764void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100765{
766 unsigned int mult;
767
768 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200769 * SDIO cards only define an upper 1 s limit on access.
770 */
771 if (mmc_card_sdio(card)) {
772 data->timeout_ns = 1000000000;
773 data->timeout_clks = 0;
774 return;
775 }
776
777 /*
Russell Kingd773d722006-09-07 15:57:12 +0100778 * SD cards use a 100 multiplier rather than 10
779 */
780 mult = mmc_card_sd(card) ? 100 : 10;
781
782 /*
783 * Scale up the multiplier (and therefore the timeout) by
784 * the r2w factor for writes.
785 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200786 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100787 mult <<= card->csd.r2w_factor;
788
789 data->timeout_ns = card->csd.tacc_ns * mult;
790 data->timeout_clks = card->csd.tacc_clks * mult;
791
792 /*
793 * SD cards also have an upper limit on the timeout.
794 */
795 if (mmc_card_sd(card)) {
796 unsigned int timeout_us, limit_us;
797
798 timeout_us = data->timeout_ns / 1000;
Linus Walleije9b86842011-01-05 00:44:32 +0100799 if (mmc_host_clk_rate(card->host))
800 timeout_us += data->timeout_clks * 1000 /
801 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100802
Pierre Ossmanb146d262007-07-24 19:16:54 +0200803 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100804 /*
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -0600805 * The MMC spec "It is strongly recommended
806 * for hosts to implement more than 500ms
807 * timeout value even if the card indicates
808 * the 250ms maximum busy length." Even the
809 * previous value of 300ms is known to be
810 * insufficient for some cards.
Pierre Ossman493890e2008-10-26 12:37:25 +0100811 */
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -0600812 limit_us = 3000000;
Russell Kingd773d722006-09-07 15:57:12 +0100813 else
814 limit_us = 100000;
815
Philip Langdalefba68bd2007-01-04 06:57:32 -0800816 /*
817 * SDHC cards always use these fixed values.
818 */
819 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100820 data->timeout_ns = limit_us * 1000;
821 data->timeout_clks = 0;
822 }
Stefan Wahrenf7bf11a2014-04-03 17:32:05 +0200823
824 /* assign limit value if invalid */
825 if (timeout_us == 0)
826 data->timeout_ns = limit_us * 1000;
Russell Kingd773d722006-09-07 15:57:12 +0100827 }
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +0100828
829 /*
830 * Some cards require longer data read timeout than indicated in CSD.
831 * Address this by setting the read timeout to a "reasonably high"
832 * value. For the cards tested, 300ms has proven enough. If necessary,
833 * this value can be increased if other problematic cards require this.
834 */
835 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
836 data->timeout_ns = 300000000;
837 data->timeout_clks = 0;
838 }
839
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100840 /*
841 * Some cards need very high timeouts if driven in SPI mode.
842 * The worst observed timeout was 900ms after writing a
843 * continuous stream of data until the internal logic
844 * overflowed.
845 */
846 if (mmc_host_is_spi(card->host)) {
847 if (data->flags & MMC_DATA_WRITE) {
848 if (data->timeout_ns < 1000000000)
849 data->timeout_ns = 1000000000; /* 1s */
850 } else {
851 if (data->timeout_ns < 100000000)
852 data->timeout_ns = 100000000; /* 100ms */
853 }
854 }
Russell Kingd773d722006-09-07 15:57:12 +0100855}
856EXPORT_SYMBOL(mmc_set_data_timeout);
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200859 * mmc_align_data_size - pads a transfer size to a more optimal value
860 * @card: the MMC card associated with the data transfer
861 * @sz: original transfer size
862 *
863 * Pads the original data size with a number of extra bytes in
864 * order to avoid controller bugs and/or performance hits
865 * (e.g. some controllers revert to PIO for certain sizes).
866 *
867 * Returns the improved size, which might be unmodified.
868 *
869 * Note that this function is only relevant when issuing a
870 * single scatter gather entry.
871 */
872unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
873{
874 /*
875 * FIXME: We don't have a system for the controller to tell
876 * the core about its problems yet, so for now we just 32-bit
877 * align the size.
878 */
879 sz = ((sz + 3) / 4) * 4;
880
881 return sz;
882}
883EXPORT_SYMBOL(mmc_align_data_size);
884
885/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200886 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200888 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200890 * Claim a host for a set of operations. If @abort is non null and
891 * dereference a non-zero value then this will return prematurely with
892 * that non-zero value without acquiring the lock. Returns zero
893 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200895int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
897 DECLARE_WAITQUEUE(wait, current);
898 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200899 int stop;
Ulf Hansson9250aea2015-03-27 12:15:15 +0100900 bool pm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200902 might_sleep();
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 add_wait_queue(&host->wq, &wait);
905 spin_lock_irqsave(&host->lock, flags);
906 while (1) {
907 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200908 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700909 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 break;
911 spin_unlock_irqrestore(&host->lock, flags);
912 schedule();
913 spin_lock_irqsave(&host->lock, flags);
914 }
915 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700916 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200917 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700918 host->claimer = current;
919 host->claim_cnt += 1;
Ulf Hansson9250aea2015-03-27 12:15:15 +0100920 if (host->claim_cnt == 1)
921 pm = true;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700922 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200923 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 spin_unlock_irqrestore(&host->lock, flags);
925 remove_wait_queue(&host->wq, &wait);
Ulf Hansson9250aea2015-03-27 12:15:15 +0100926
927 if (pm)
928 pm_runtime_get_sync(mmc_dev(host));
929
Nicolas Pitre2342f332007-06-30 16:21:52 +0200930 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
Nicolas Pitre2342f332007-06-30 16:21:52 +0200932EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Adrian Hunter319a3f12009-09-22 16:44:30 -0700934/**
Adrian Hunter907d2e72012-02-29 09:17:21 +0200935 * mmc_release_host - release a host
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100936 * @host: mmc host to release
937 *
Adrian Hunter907d2e72012-02-29 09:17:21 +0200938 * Release a MMC host, allowing others to claim the host
939 * for their operations.
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100940 */
Adrian Hunter907d2e72012-02-29 09:17:21 +0200941void mmc_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700942{
943 unsigned long flags;
944
Adrian Hunter907d2e72012-02-29 09:17:21 +0200945 WARN_ON(!host->claimed);
946
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700947 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700948 if (--host->claim_cnt) {
949 /* Release for nested claim */
950 spin_unlock_irqrestore(&host->lock, flags);
951 } else {
952 host->claimed = 0;
953 host->claimer = NULL;
954 spin_unlock_irqrestore(&host->lock, flags);
955 wake_up(&host->wq);
Ulf Hansson9250aea2015-03-27 12:15:15 +0100956 pm_runtime_mark_last_busy(mmc_dev(host));
957 pm_runtime_put_autosuspend(mmc_dev(host));
Adrian Hunter319a3f12009-09-22 16:44:30 -0700958 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700959}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960EXPORT_SYMBOL(mmc_release_host);
961
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100962/*
Ulf Hanssone94cfef2013-05-02 14:02:38 +0200963 * This is a helper function, which fetches a runtime pm reference for the
964 * card device and also claims the host.
965 */
966void mmc_get_card(struct mmc_card *card)
967{
968 pm_runtime_get_sync(&card->dev);
969 mmc_claim_host(card->host);
970}
971EXPORT_SYMBOL(mmc_get_card);
972
973/*
974 * This is a helper function, which releases the host and drops the runtime
975 * pm reference for the card device.
976 */
977void mmc_put_card(struct mmc_card *card)
978{
979 mmc_release_host(card->host);
980 pm_runtime_mark_last_busy(&card->dev);
981 pm_runtime_put_autosuspend(&card->dev);
982}
983EXPORT_SYMBOL(mmc_put_card);
984
985/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100986 * Internal function that does the actual ios call to the host driver,
987 * optionally printing some debug output.
988 */
Russell King920e70c2006-05-04 18:22:51 +0100989static inline void mmc_set_ios(struct mmc_host *host)
990{
991 struct mmc_ios *ios = &host->ios;
992
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100993 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
994 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100995 mmc_hostname(host), ios->clock, ios->bus_mode,
996 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100997 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800998
Linus Walleij04566832010-11-08 21:36:50 -0500999 if (ios->clock > 0)
1000 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +01001001 host->ops->set_ios(host, ios);
1002}
1003
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001004/*
1005 * Control chip select pin on a host.
1006 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01001007void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001008{
Mika Westerberg778e2772011-08-18 15:23:48 +03001009 mmc_host_clk_hold(host);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01001010 host->ios.chip_select = mode;
1011 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001012 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013}
1014
1015/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001016 * Sets the host clock to the highest possible frequency that
1017 * is below "hz".
1018 */
Mika Westerberg778e2772011-08-18 15:23:48 +03001019static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001020{
Adrian Hunter6a98f1e2014-09-23 23:00:26 +03001021 WARN_ON(hz && hz < host->f_min);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001022
1023 if (hz > host->f_max)
1024 hz = host->f_max;
1025
1026 host->ios.clock = hz;
1027 mmc_set_ios(host);
1028}
1029
Mika Westerberg778e2772011-08-18 15:23:48 +03001030void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1031{
1032 mmc_host_clk_hold(host);
1033 __mmc_set_clock(host, hz);
1034 mmc_host_clk_release(host);
1035}
1036
Linus Walleij04566832010-11-08 21:36:50 -05001037#ifdef CONFIG_MMC_CLKGATE
1038/*
1039 * This gates the clock by setting it to 0 Hz.
1040 */
1041void mmc_gate_clock(struct mmc_host *host)
1042{
1043 unsigned long flags;
1044
1045 spin_lock_irqsave(&host->clk_lock, flags);
1046 host->clk_old = host->ios.clock;
1047 host->ios.clock = 0;
1048 host->clk_gated = true;
1049 spin_unlock_irqrestore(&host->clk_lock, flags);
1050 mmc_set_ios(host);
1051}
1052
1053/*
1054 * This restores the clock from gating by using the cached
1055 * clock value.
1056 */
1057void mmc_ungate_clock(struct mmc_host *host)
1058{
1059 /*
1060 * We should previously have gated the clock, so the clock shall
1061 * be 0 here! The clock may however be 0 during initialization,
1062 * when some request operations are performed before setting
1063 * the frequency. When ungate is requested in that situation
1064 * we just ignore the call.
1065 */
1066 if (host->clk_old) {
1067 BUG_ON(host->ios.clock);
1068 /* This call will also set host->clk_gated to false */
Mika Westerberg778e2772011-08-18 15:23:48 +03001069 __mmc_set_clock(host, host->clk_old);
Linus Walleij04566832010-11-08 21:36:50 -05001070 }
1071}
1072
1073void mmc_set_ungated(struct mmc_host *host)
1074{
1075 unsigned long flags;
1076
1077 /*
1078 * We've been given a new frequency while the clock is gated,
1079 * so make sure we regard this as ungating it.
1080 */
1081 spin_lock_irqsave(&host->clk_lock, flags);
1082 host->clk_gated = false;
1083 spin_unlock_irqrestore(&host->clk_lock, flags);
1084}
1085
1086#else
1087void mmc_set_ungated(struct mmc_host *host)
1088{
1089}
1090#endif
1091
Adrian Hunter63e415c2014-12-05 19:40:59 +02001092int mmc_execute_tuning(struct mmc_card *card)
1093{
1094 struct mmc_host *host = card->host;
1095 u32 opcode;
1096 int err;
1097
1098 if (!host->ops->execute_tuning)
1099 return 0;
1100
1101 if (mmc_card_mmc(card))
1102 opcode = MMC_SEND_TUNING_BLOCK_HS200;
1103 else
1104 opcode = MMC_SEND_TUNING_BLOCK;
1105
1106 mmc_host_clk_hold(host);
1107 err = host->ops->execute_tuning(host, opcode);
1108 mmc_host_clk_release(host);
1109
1110 if (err)
1111 pr_err("%s: tuning execution failed\n", mmc_hostname(host));
Adrian Hunter79d5a652015-05-07 13:10:13 +03001112 else
1113 mmc_retune_enable(host);
Adrian Hunter63e415c2014-12-05 19:40:59 +02001114
1115 return err;
1116}
1117
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001118/*
1119 * Change the bus mode (open drain/push-pull) of a host.
1120 */
1121void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1122{
Mika Westerberg778e2772011-08-18 15:23:48 +03001123 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001124 host->ios.bus_mode = mode;
1125 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001126 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001127}
1128
1129/*
1130 * Change data bus width of a host.
1131 */
1132void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1133{
Mika Westerberg778e2772011-08-18 15:23:48 +03001134 mmc_host_clk_hold(host);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301135 host->ios.bus_width = width;
1136 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001137 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001138}
1139
Johan Rudholm2d079c42014-11-06 14:46:54 +01001140/*
1141 * Set initial state after a power cycle or a hw_reset.
1142 */
1143void mmc_set_initial_state(struct mmc_host *host)
1144{
Adrian Hunter79d5a652015-05-07 13:10:13 +03001145 mmc_retune_disable(host);
1146
Johan Rudholm2d079c42014-11-06 14:46:54 +01001147 if (mmc_host_is_spi(host))
1148 host->ios.chip_select = MMC_CS_HIGH;
1149 else
1150 host->ios.chip_select = MMC_CS_DONTCARE;
1151 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1152 host->ios.bus_width = MMC_BUS_WIDTH_1;
1153 host->ios.timing = MMC_TIMING_LEGACY;
1154
1155 mmc_set_ios(host);
1156}
1157
Anton Vorontsov86e82862008-11-26 22:54:17 +03001158/**
1159 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1160 * @vdd: voltage (mV)
1161 * @low_bits: prefer low bits in boundary cases
1162 *
1163 * This function returns the OCR bit number according to the provided @vdd
1164 * value. If conversion is not possible a negative errno value returned.
1165 *
1166 * Depending on the @low_bits flag the function prefers low or high OCR bits
1167 * on boundary voltages. For example,
1168 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1169 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1170 *
1171 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1172 */
1173static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1174{
1175 const int max_bit = ilog2(MMC_VDD_35_36);
1176 int bit;
1177
1178 if (vdd < 1650 || vdd > 3600)
1179 return -EINVAL;
1180
1181 if (vdd >= 1650 && vdd <= 1950)
1182 return ilog2(MMC_VDD_165_195);
1183
1184 if (low_bits)
1185 vdd -= 1;
1186
1187 /* Base 2000 mV, step 100 mV, bit's base 8. */
1188 bit = (vdd - 2000) / 100 + 8;
1189 if (bit > max_bit)
1190 return max_bit;
1191 return bit;
1192}
1193
1194/**
1195 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1196 * @vdd_min: minimum voltage value (mV)
1197 * @vdd_max: maximum voltage value (mV)
1198 *
1199 * This function returns the OCR mask bits according to the provided @vdd_min
1200 * and @vdd_max values. If conversion is not possible the function returns 0.
1201 *
1202 * Notes wrt boundary cases:
1203 * This function sets the OCR bits for all boundary voltages, for example
1204 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1205 * MMC_VDD_34_35 mask.
1206 */
1207u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1208{
1209 u32 mask = 0;
1210
1211 if (vdd_max < vdd_min)
1212 return 0;
1213
1214 /* Prefer high bits for the boundary vdd_max values. */
1215 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1216 if (vdd_max < 0)
1217 return 0;
1218
1219 /* Prefer low bits for the boundary vdd_min values. */
1220 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1221 if (vdd_min < 0)
1222 return 0;
1223
1224 /* Fill the mask, from max bit to min bit. */
1225 while (vdd_max >= vdd_min)
1226 mask |= 1 << vdd_max--;
1227
1228 return mask;
1229}
1230EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1231
Haijun Zhang6e9e3182013-08-26 09:19:22 +08001232#ifdef CONFIG_OF
1233
1234/**
1235 * mmc_of_parse_voltage - return mask of supported voltages
1236 * @np: The device node need to be parsed.
1237 * @mask: mask of voltages available for MMC/SD/SDIO
1238 *
1239 * 1. Return zero on success.
1240 * 2. Return negative errno: voltage-range is invalid.
1241 */
1242int mmc_of_parse_voltage(struct device_node *np, u32 *mask)
1243{
1244 const u32 *voltage_ranges;
1245 int num_ranges, i;
1246
1247 voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges);
1248 num_ranges = num_ranges / sizeof(*voltage_ranges) / 2;
1249 if (!voltage_ranges || !num_ranges) {
1250 pr_info("%s: voltage-ranges unspecified\n", np->full_name);
1251 return -EINVAL;
1252 }
1253
1254 for (i = 0; i < num_ranges; i++) {
1255 const int j = i * 2;
1256 u32 ocr_mask;
1257
1258 ocr_mask = mmc_vddrange_to_ocrmask(
1259 be32_to_cpu(voltage_ranges[j]),
1260 be32_to_cpu(voltage_ranges[j + 1]));
1261 if (!ocr_mask) {
1262 pr_err("%s: voltage-range #%d is invalid\n",
1263 np->full_name, i);
1264 return -EINVAL;
1265 }
1266 *mask |= ocr_mask;
1267 }
1268
1269 return 0;
1270}
1271EXPORT_SYMBOL(mmc_of_parse_voltage);
1272
1273#endif /* CONFIG_OF */
1274
Sascha Hauer25185f32014-06-30 11:07:25 +02001275static int mmc_of_get_func_num(struct device_node *node)
1276{
1277 u32 reg;
1278 int ret;
1279
1280 ret = of_property_read_u32(node, "reg", &reg);
1281 if (ret < 0)
1282 return ret;
1283
1284 return reg;
1285}
1286
1287struct device_node *mmc_of_find_child_device(struct mmc_host *host,
1288 unsigned func_num)
1289{
1290 struct device_node *node;
1291
1292 if (!host->parent || !host->parent->of_node)
1293 return NULL;
1294
1295 for_each_child_of_node(host->parent->of_node, node) {
1296 if (mmc_of_get_func_num(node) == func_num)
1297 return node;
1298 }
1299
1300 return NULL;
1301}
1302
David Brownell5c139412009-03-11 03:30:43 -08001303#ifdef CONFIG_REGULATOR
1304
1305/**
1306 * mmc_regulator_get_ocrmask - return mask of supported voltages
1307 * @supply: regulator to use
1308 *
1309 * This returns either a negative errno, or a mask of voltages that
1310 * can be provided to MMC/SD/SDIO devices using the specified voltage
1311 * regulator. This would normally be called before registering the
1312 * MMC host adapter.
1313 */
1314int mmc_regulator_get_ocrmask(struct regulator *supply)
1315{
1316 int result = 0;
1317 int count;
1318 int i;
Javier Martinez Canillas9ed7ca82014-08-14 14:39:00 +02001319 int vdd_uV;
1320 int vdd_mV;
David Brownell5c139412009-03-11 03:30:43 -08001321
1322 count = regulator_count_voltages(supply);
1323 if (count < 0)
1324 return count;
1325
1326 for (i = 0; i < count; i++) {
David Brownell5c139412009-03-11 03:30:43 -08001327 vdd_uV = regulator_list_voltage(supply, i);
1328 if (vdd_uV <= 0)
1329 continue;
1330
1331 vdd_mV = vdd_uV / 1000;
1332 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1333 }
1334
Javier Martinez Canillas9ed7ca82014-08-14 14:39:00 +02001335 if (!result) {
1336 vdd_uV = regulator_get_voltage(supply);
1337 if (vdd_uV <= 0)
1338 return vdd_uV;
1339
1340 vdd_mV = vdd_uV / 1000;
1341 result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1342 }
1343
David Brownell5c139412009-03-11 03:30:43 -08001344 return result;
1345}
Chris Ball45a6b322012-06-11 09:39:12 -04001346EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
David Brownell5c139412009-03-11 03:30:43 -08001347
1348/**
1349 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -04001350 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -08001351 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -04001352 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -08001353 *
1354 * Returns zero on success, else negative errno.
1355 *
1356 * MMC host drivers may use this to enable or disable a regulator using
1357 * a particular supply voltage. This would normally be called from the
1358 * set_ios() method.
1359 */
Linus Walleij99fc5132010-09-29 01:08:27 -04001360int mmc_regulator_set_ocr(struct mmc_host *mmc,
1361 struct regulator *supply,
1362 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -08001363{
1364 int result = 0;
1365 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -08001366
1367 if (vdd_bit) {
1368 int tmp;
David Brownell5c139412009-03-11 03:30:43 -08001369
Chris Ball9cde5b72012-09-19 22:27:04 +08001370 /*
1371 * REVISIT mmc_vddrange_to_ocrmask() may have set some
David Brownell5c139412009-03-11 03:30:43 -08001372 * bits this regulator doesn't quite support ... don't
1373 * be too picky, most cards and regulators are OK with
1374 * a 0.1V range goof (it's a small error percentage).
1375 */
1376 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1377 if (tmp == 0) {
1378 min_uV = 1650 * 1000;
1379 max_uV = 1950 * 1000;
1380 } else {
1381 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1382 max_uV = min_uV + 100 * 1000;
1383 }
1384
Tim Krygerca6429d2014-08-11 22:05:12 -07001385 result = regulator_set_voltage(supply, min_uV, max_uV);
Linus Walleij99fc5132010-09-29 01:08:27 -04001386 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001387 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001388 if (!result)
1389 mmc->regulator_enabled = true;
1390 }
1391 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001392 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001393 if (result == 0)
1394 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -08001395 }
1396
Linus Walleij99fc5132010-09-29 01:08:27 -04001397 if (result)
1398 dev_err(mmc_dev(mmc),
1399 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -08001400 return result;
1401}
Chris Ball45a6b322012-06-11 09:39:12 -04001402EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
David Brownell5c139412009-03-11 03:30:43 -08001403
Tim Kryger4d1f52f2014-05-06 15:57:01 -07001404#endif /* CONFIG_REGULATOR */
1405
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001406int mmc_regulator_get_supply(struct mmc_host *mmc)
1407{
1408 struct device *dev = mmc_dev(mmc);
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001409 int ret;
1410
Tim Kryger4d1f52f2014-05-06 15:57:01 -07001411 mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
Mark Brownbc35d5e2013-07-29 21:58:01 +01001412 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001413
Tim Kryger4d1f52f2014-05-06 15:57:01 -07001414 if (IS_ERR(mmc->supply.vmmc)) {
1415 if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
1416 return -EPROBE_DEFER;
1417 dev_info(dev, "No vmmc regulator found\n");
1418 } else {
1419 ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc);
1420 if (ret > 0)
1421 mmc->ocr_avail = ret;
1422 else
1423 dev_warn(dev, "Failed getting OCR mask: %d\n", ret);
1424 }
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001425
Tim Kryger4d1f52f2014-05-06 15:57:01 -07001426 if (IS_ERR(mmc->supply.vqmmc)) {
1427 if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
1428 return -EPROBE_DEFER;
1429 dev_info(dev, "No vqmmc regulator found\n");
1430 }
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001431
1432 return 0;
1433}
1434EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1435
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 * Mask off any voltages we don't support and select
1438 * the lowest voltage
1439 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001440u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441{
1442 int bit;
1443
Ulf Hansson726d6f22013-09-16 12:06:15 +02001444 /*
1445 * Sanity check the voltages that the card claims to
1446 * support.
1447 */
1448 if (ocr & 0x7F) {
1449 dev_warn(mmc_dev(host),
1450 "card claims to support voltages below defined range\n");
1451 ocr &= ~0x7F;
1452 }
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 ocr &= host->ocr_avail;
Ulf Hanssonce69d372013-09-16 11:28:42 +02001455 if (!ocr) {
1456 dev_warn(mmc_dev(host), "no support for card's volts\n");
1457 return 0;
1458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Ulf Hanssonce69d372013-09-16 11:28:42 +02001460 if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
1461 bit = ffs(ocr) - 1;
Timo Teras63ef7312006-11-02 19:43:27 +01001462 ocr &= 3 << bit;
Ulf Hanssonce69d372013-09-16 11:28:42 +02001463 mmc_power_cycle(host, ocr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 } else {
Ulf Hanssonce69d372013-09-16 11:28:42 +02001465 bit = fls(ocr) - 1;
1466 ocr &= 3 << bit;
1467 if (bit != host->ios.vdd)
1468 dev_warn(mmc_dev(host), "exceeding card's volts\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 }
1470
1471 return ocr;
1472}
1473
Johan Rudholm567c8902013-01-28 15:08:27 +01001474int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1475{
1476 int err = 0;
1477 int old_signal_voltage = host->ios.signal_voltage;
1478
1479 host->ios.signal_voltage = signal_voltage;
1480 if (host->ops->start_signal_voltage_switch) {
1481 mmc_host_clk_hold(host);
1482 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1483 mmc_host_clk_release(host);
1484 }
1485
1486 if (err)
1487 host->ios.signal_voltage = old_signal_voltage;
1488
1489 return err;
1490
1491}
1492
Ulf Hansson0f791fd2013-09-12 15:36:34 +02001493int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
Arindam Nathf2119df2011-05-05 12:18:57 +05301494{
1495 struct mmc_command cmd = {0};
1496 int err = 0;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001497 u32 clock;
Arindam Nathf2119df2011-05-05 12:18:57 +05301498
1499 BUG_ON(!host);
1500
1501 /*
1502 * Send CMD11 only if the request is to switch the card to
1503 * 1.8V signalling.
1504 */
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001505 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1506 return __mmc_set_signal_voltage(host, signal_voltage);
Arindam Nathf2119df2011-05-05 12:18:57 +05301507
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001508 /*
1509 * If we cannot switch voltages, return failure so the caller
1510 * can continue without UHS mode
1511 */
1512 if (!host->ops->start_signal_voltage_switch)
1513 return -EPERM;
1514 if (!host->ops->card_busy)
Joe Perches66061102014-09-12 14:56:56 -07001515 pr_warn("%s: cannot verify signal voltage switch\n",
1516 mmc_hostname(host));
Arindam Nathf2119df2011-05-05 12:18:57 +05301517
Vincent Yangc6eb5882014-11-21 08:51:40 +08001518 mmc_host_clk_hold(host);
1519
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001520 cmd.opcode = SD_SWITCH_VOLTAGE;
1521 cmd.arg = 0;
1522 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1523
1524 err = mmc_wait_for_cmd(host, &cmd, 0);
1525 if (err)
Vincent Yangc6eb5882014-11-21 08:51:40 +08001526 goto err_command;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001527
Vincent Yangc6eb5882014-11-21 08:51:40 +08001528 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) {
1529 err = -EIO;
1530 goto err_command;
1531 }
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001532 /*
1533 * The card should drive cmd and dat[0:3] low immediately
1534 * after the response of cmd11, but wait 1 ms to be sure
1535 */
1536 mmc_delay(1);
1537 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1538 err = -EAGAIN;
1539 goto power_cycle;
1540 }
1541 /*
1542 * During a signal voltage level switch, the clock must be gated
1543 * for 5 ms according to the SD spec
1544 */
1545 clock = host->ios.clock;
1546 host->ios.clock = 0;
1547 mmc_set_ios(host);
1548
1549 if (__mmc_set_signal_voltage(host, signal_voltage)) {
1550 /*
1551 * Voltages may not have been switched, but we've already
1552 * sent CMD11, so a power cycle is required anyway
1553 */
1554 err = -EAGAIN;
1555 goto power_cycle;
Arindam Nathf2119df2011-05-05 12:18:57 +05301556 }
1557
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001558 /* Keep clock gated for at least 5 ms */
1559 mmc_delay(5);
1560 host->ios.clock = clock;
1561 mmc_set_ios(host);
1562
1563 /* Wait for at least 1 ms according to spec */
1564 mmc_delay(1);
1565
1566 /*
1567 * Failure to switch is indicated by the card holding
1568 * dat[0:3] low
1569 */
1570 if (host->ops->card_busy && host->ops->card_busy(host))
1571 err = -EAGAIN;
1572
1573power_cycle:
1574 if (err) {
1575 pr_debug("%s: Signal voltage switch failed, "
1576 "power cycling card\n", mmc_hostname(host));
Ulf Hansson0f791fd2013-09-12 15:36:34 +02001577 mmc_power_cycle(host, ocr);
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001578 }
1579
Vincent Yangc6eb5882014-11-21 08:51:40 +08001580err_command:
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001581 mmc_host_clk_release(host);
1582
1583 return err;
Arindam Nathf2119df2011-05-05 12:18:57 +05301584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001587 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001589void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
Mika Westerberg778e2772011-08-18 15:23:48 +03001591 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001592 host->ios.timing = timing;
1593 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001594 mmc_host_clk_release(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001595}
1596
1597/*
Arindam Nathd6d50a12011-05-05 12:18:59 +05301598 * Select appropriate driver type for host.
1599 */
1600void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1601{
Mika Westerberg778e2772011-08-18 15:23:48 +03001602 mmc_host_clk_hold(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301603 host->ios.drv_type = drv_type;
1604 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001605 mmc_host_clk_release(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301606}
1607
1608/*
Russell King45f82452005-12-14 14:57:35 +00001609 * Apply power to the MMC stack. This is a two-stage process.
1610 * First, we enable power to the card without the clock running.
1611 * We then wait a bit for the power to stabilise. Finally,
1612 * enable the bus drivers and clock to the card.
1613 *
1614 * We must _NOT_ enable the clock prior to power stablising.
1615 *
1616 * If a host does all the power sequencing itself, ignore the
1617 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
Ulf Hansson4a065192013-09-12 14:36:53 +02001619void mmc_power_up(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620{
Ulf Hanssonfa550182012-05-09 16:15:26 +02001621 if (host->ios.power_mode == MMC_POWER_ON)
1622 return;
1623
Mika Westerberg778e2772011-08-18 15:23:48 +03001624 mmc_host_clk_hold(host);
1625
Ulf Hansson3aa87932014-11-28 14:38:36 +01001626 mmc_pwrseq_pre_power_on(host);
1627
Ulf Hansson4a065192013-09-12 14:36:53 +02001628 host->ios.vdd = fls(ocr) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 host->ios.power_mode = MMC_POWER_UP;
Johan Rudholm2d079c42014-11-06 14:46:54 +01001630 /* Set initial state and call mmc_set_ios */
1631 mmc_set_initial_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Tim Krygerceae98f2014-04-24 14:44:26 -07001633 /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */
1634 if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330) == 0)
1635 dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n");
1636 else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180) == 0)
1637 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n");
1638 else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120) == 0)
1639 dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n");
Aaron Lu108ecc42012-07-10 16:55:37 +08001640
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001641 /*
1642 * This delay should be sufficient to allow the power supply
1643 * to reach the minimum voltage.
1644 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001645 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Ulf Hansson4febb7e2015-02-02 16:01:14 +01001647 mmc_pwrseq_post_power_on(host);
1648
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001649 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02001650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01001652 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001654 /*
1655 * This delay must be at least 74 clock sizes, or 1 ms, or the
1656 * time required to reach a stable voltage.
1657 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001658 mmc_delay(10);
Mika Westerberg778e2772011-08-18 15:23:48 +03001659
1660 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661}
1662
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001663void mmc_power_off(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664{
Ulf Hanssonfa550182012-05-09 16:15:26 +02001665 if (host->ios.power_mode == MMC_POWER_OFF)
1666 return;
1667
Mika Westerberg778e2772011-08-18 15:23:48 +03001668 mmc_host_clk_hold(host);
1669
Ulf Hansson3aa87932014-11-28 14:38:36 +01001670 mmc_pwrseq_power_off(host);
1671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 host->ios.clock = 0;
1673 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 host->ios.power_mode = MMC_POWER_OFF;
Johan Rudholm2d079c42014-11-06 14:46:54 +01001676 /* Set initial state and call mmc_set_ios */
1677 mmc_set_initial_state(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001678
Daniel Drake041beb12011-09-07 10:22:09 +01001679 /*
1680 * Some configurations, such as the 802.11 SDIO card in the OLPC
1681 * XO-1.5, require a short delay after poweroff before the card
1682 * can be successfully turned on again.
1683 */
1684 mmc_delay(1);
1685
Mika Westerberg778e2772011-08-18 15:23:48 +03001686 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687}
1688
Ulf Hansson4a065192013-09-12 14:36:53 +02001689void mmc_power_cycle(struct mmc_host *host, u32 ocr)
Johan Rudholm276e0902013-01-28 15:08:25 +01001690{
1691 mmc_power_off(host);
1692 /* Wait at least 1 ms according to SD spec */
1693 mmc_delay(1);
Ulf Hansson4a065192013-09-12 14:36:53 +02001694 mmc_power_up(host, ocr);
Johan Rudholm276e0902013-01-28 15:08:25 +01001695}
1696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697/*
Adrian Bunk39361852007-07-25 00:40:58 +02001698 * Cleanup when the last reference to the bus operator is dropped.
1699 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001700static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001701{
1702 BUG_ON(!host);
1703 BUG_ON(host->bus_refs);
1704 BUG_ON(!host->bus_dead);
1705
1706 host->bus_ops = NULL;
1707}
1708
1709/*
1710 * Increase reference count of bus operator
1711 */
1712static inline void mmc_bus_get(struct mmc_host *host)
1713{
1714 unsigned long flags;
1715
1716 spin_lock_irqsave(&host->lock, flags);
1717 host->bus_refs++;
1718 spin_unlock_irqrestore(&host->lock, flags);
1719}
1720
1721/*
1722 * Decrease reference count of bus operator and free it if
1723 * it is the last reference.
1724 */
1725static inline void mmc_bus_put(struct mmc_host *host)
1726{
1727 unsigned long flags;
1728
1729 spin_lock_irqsave(&host->lock, flags);
1730 host->bus_refs--;
1731 if ((host->bus_refs == 0) && host->bus_ops)
1732 __mmc_release_bus(host);
1733 spin_unlock_irqrestore(&host->lock, flags);
1734}
1735
1736/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001737 * Assign a mmc bus handler to a host. Only one bus handler may control a
1738 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001740void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001742 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001744 BUG_ON(!host);
1745 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001746
Pierre Ossmand84075c82007-08-09 13:23:56 +02001747 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001748
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001749 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001750
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001751 BUG_ON(host->bus_ops);
1752 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001753
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001754 host->bus_ops = ops;
1755 host->bus_refs = 1;
1756 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001758 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/*
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001762 * Remove the current bus handler from a host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001764void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001766 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001768 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
Pierre Ossmand84075c82007-08-09 13:23:56 +02001770 WARN_ON(!host->claimed);
1771 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001773 spin_lock_irqsave(&host->lock, flags);
1774
1775 host->bus_dead = 1;
1776
1777 spin_unlock_irqrestore(&host->lock, flags);
1778
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001779 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780}
1781
Ulf Hanssonbbd43682013-09-20 11:02:35 +02001782static void _mmc_detect_change(struct mmc_host *host, unsigned long delay,
1783 bool cd_irq)
1784{
1785#ifdef CONFIG_MMC_DEBUG
1786 unsigned long flags;
1787 spin_lock_irqsave(&host->lock, flags);
1788 WARN_ON(host->removed);
1789 spin_unlock_irqrestore(&host->lock, flags);
1790#endif
1791
1792 /*
1793 * If the device is configured as wakeup, we prevent a new sleep for
1794 * 5 s to give provision for user space to consume the event.
1795 */
1796 if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) &&
1797 device_can_wakeup(mmc_dev(host)))
1798 pm_wakeup_event(mmc_dev(host), 5000);
1799
1800 host->detect_change = 1;
1801 mmc_schedule_delayed_work(&host->detect, delay);
1802}
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804/**
1805 * mmc_detect_change - process change of state on a MMC socket
1806 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001807 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001809 * MMC drivers should call this when they detect a card has been
1810 * inserted or removed. The MMC layer will confirm that any
1811 * present card is still functional, and initialize any newly
1812 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001814void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815{
Ulf Hanssonbbd43682013-09-20 11:02:35 +02001816 _mmc_detect_change(host, delay, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818EXPORT_SYMBOL(mmc_detect_change);
1819
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001820void mmc_init_erase(struct mmc_card *card)
1821{
1822 unsigned int sz;
1823
1824 if (is_power_of_2(card->erase_size))
1825 card->erase_shift = ffs(card->erase_size) - 1;
1826 else
1827 card->erase_shift = 0;
1828
1829 /*
1830 * It is possible to erase an arbitrarily large area of an SD or MMC
1831 * card. That is not desirable because it can take a long time
1832 * (minutes) potentially delaying more important I/O, and also the
1833 * timeout calculations become increasingly hugely over-estimated.
1834 * Consequently, 'pref_erase' is defined as a guide to limit erases
1835 * to that size and alignment.
1836 *
1837 * For SD cards that define Allocation Unit size, limit erases to one
1838 * Allocation Unit at a time. For MMC cards that define High Capacity
1839 * Erase Size, whether it is switched on or not, limit to that size.
1840 * Otherwise just have a stab at a good value. For modern cards it
1841 * will end up being 4MiB. Note that if the value is too small, it
1842 * can end up taking longer to erase.
1843 */
1844 if (mmc_card_sd(card) && card->ssr.au) {
1845 card->pref_erase = card->ssr.au;
1846 card->erase_shift = ffs(card->ssr.au) - 1;
1847 } else if (card->ext_csd.hc_erase_size) {
1848 card->pref_erase = card->ext_csd.hc_erase_size;
Chuanxiao Dongcc8aa7d2014-08-14 18:29:24 +08001849 } else if (card->erase_size) {
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001850 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1851 if (sz < 128)
1852 card->pref_erase = 512 * 1024 / 512;
1853 else if (sz < 512)
1854 card->pref_erase = 1024 * 1024 / 512;
1855 else if (sz < 1024)
1856 card->pref_erase = 2 * 1024 * 1024 / 512;
1857 else
1858 card->pref_erase = 4 * 1024 * 1024 / 512;
1859 if (card->pref_erase < card->erase_size)
1860 card->pref_erase = card->erase_size;
1861 else {
1862 sz = card->pref_erase % card->erase_size;
1863 if (sz)
1864 card->pref_erase += card->erase_size - sz;
1865 }
Chuanxiao Dongcc8aa7d2014-08-14 18:29:24 +08001866 } else
1867 card->pref_erase = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001868}
1869
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001870static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1871 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001872{
1873 unsigned int erase_timeout;
1874
Adrian Hunter7194efb2012-04-05 14:45:47 +03001875 if (arg == MMC_DISCARD_ARG ||
1876 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1877 erase_timeout = card->ext_csd.trim_timeout;
1878 } else if (card->ext_csd.erase_group_def & 1) {
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001879 /* High Capacity Erase Group Size uses HC timeouts */
1880 if (arg == MMC_TRIM_ARG)
1881 erase_timeout = card->ext_csd.trim_timeout;
1882 else
1883 erase_timeout = card->ext_csd.hc_erase_timeout;
1884 } else {
1885 /* CSD Erase Group Size uses write timeout */
1886 unsigned int mult = (10 << card->csd.r2w_factor);
1887 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1888 unsigned int timeout_us;
1889
1890 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1891 if (card->csd.tacc_ns < 1000000)
1892 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1893 else
1894 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1895
1896 /*
1897 * ios.clock is only a target. The real clock rate might be
1898 * less but not that much less, so fudge it by multiplying by 2.
1899 */
1900 timeout_clks <<= 1;
1901 timeout_us += (timeout_clks * 1000) /
Adrian Hunter4cf8c6d2011-06-23 13:40:27 +03001902 (mmc_host_clk_rate(card->host) / 1000);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001903
1904 erase_timeout = timeout_us / 1000;
1905
1906 /*
1907 * Theoretically, the calculation could underflow so round up
1908 * to 1ms in that case.
1909 */
1910 if (!erase_timeout)
1911 erase_timeout = 1;
1912 }
1913
1914 /* Multiplier for secure operations */
1915 if (arg & MMC_SECURE_ARGS) {
1916 if (arg == MMC_SECURE_ERASE_ARG)
1917 erase_timeout *= card->ext_csd.sec_erase_mult;
1918 else
1919 erase_timeout *= card->ext_csd.sec_trim_mult;
1920 }
1921
1922 erase_timeout *= qty;
1923
1924 /*
1925 * Ensure at least a 1 second timeout for SPI as per
1926 * 'mmc_set_data_timeout()'
1927 */
1928 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1929 erase_timeout = 1000;
1930
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001931 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001932}
1933
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001934static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1935 unsigned int arg,
1936 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001937{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001938 unsigned int erase_timeout;
1939
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001940 if (card->ssr.erase_timeout) {
1941 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001942 erase_timeout = card->ssr.erase_timeout * qty +
1943 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001944 } else {
1945 /*
1946 * Erase timeout not specified in SD Status Register (SSR) so
1947 * use 250ms per write block.
1948 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001949 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001950 }
1951
1952 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001953 if (erase_timeout < 1000)
1954 erase_timeout = 1000;
1955
1956 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001957}
1958
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001959static unsigned int mmc_erase_timeout(struct mmc_card *card,
1960 unsigned int arg,
1961 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001962{
1963 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001964 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001965 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001966 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001967}
1968
1969static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1970 unsigned int to, unsigned int arg)
1971{
Chris Ball1278dba2011-04-13 23:40:30 -04001972 struct mmc_command cmd = {0};
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001973 unsigned int qty = 0;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001974 unsigned long timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001975 int err;
1976
1977 /*
1978 * qty is used to calculate the erase timeout which depends on how many
1979 * erase groups (or allocation units in SD terminology) are affected.
1980 * We count erasing part of an erase group as one erase group.
1981 * For SD, the allocation units are always a power of 2. For MMC, the
1982 * erase group size is almost certainly also power of 2, but it does not
1983 * seem to insist on that in the JEDEC standard, so we fall back to
1984 * division in that case. SD may not specify an allocation unit size,
1985 * in which case the timeout is based on the number of write blocks.
1986 *
1987 * Note that the timeout for secure trim 2 will only be correct if the
1988 * number of erase groups specified is the same as the total of all
1989 * preceding secure trim 1 commands. Since the power may have been
1990 * lost since the secure trim 1 commands occurred, it is generally
1991 * impossible to calculate the secure trim 2 timeout correctly.
1992 */
1993 if (card->erase_shift)
1994 qty += ((to >> card->erase_shift) -
1995 (from >> card->erase_shift)) + 1;
1996 else if (mmc_card_sd(card))
1997 qty += to - from + 1;
1998 else
1999 qty += ((to / card->erase_size) -
2000 (from / card->erase_size)) + 1;
2001
2002 if (!mmc_card_blockaddr(card)) {
2003 from <<= 9;
2004 to <<= 9;
2005 }
2006
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002007 if (mmc_card_sd(card))
2008 cmd.opcode = SD_ERASE_WR_BLK_START;
2009 else
2010 cmd.opcode = MMC_ERASE_GROUP_START;
2011 cmd.arg = from;
2012 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2013 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2014 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302015 pr_err("mmc_erase: group start error %d, "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002016 "status %#x\n", err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03002017 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002018 goto out;
2019 }
2020
2021 memset(&cmd, 0, sizeof(struct mmc_command));
2022 if (mmc_card_sd(card))
2023 cmd.opcode = SD_ERASE_WR_BLK_END;
2024 else
2025 cmd.opcode = MMC_ERASE_GROUP_END;
2026 cmd.arg = to;
2027 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2028 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2029 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302030 pr_err("mmc_erase: group end error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002031 err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03002032 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002033 goto out;
2034 }
2035
2036 memset(&cmd, 0, sizeof(struct mmc_command));
2037 cmd.opcode = MMC_ERASE;
2038 cmd.arg = arg;
2039 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01002040 cmd.busy_timeout = mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002041 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2042 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302043 pr_err("mmc_erase: erase error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002044 err, cmd.resp[0]);
2045 err = -EIO;
2046 goto out;
2047 }
2048
2049 if (mmc_host_is_spi(card->host))
2050 goto out;
2051
Trey Ramsay8fee4762012-11-16 09:31:41 -06002052 timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002053 do {
2054 memset(&cmd, 0, sizeof(struct mmc_command));
2055 cmd.opcode = MMC_SEND_STATUS;
2056 cmd.arg = card->rca << 16;
2057 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
2058 /* Do not retry else we can't see errors */
2059 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2060 if (err || (cmd.resp[0] & 0xFDF92000)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302061 pr_err("error %d requesting status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002062 err, cmd.resp[0]);
2063 err = -EIO;
2064 goto out;
2065 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06002066
2067 /* Timeout if the device never becomes ready for data and
2068 * never leaves the program state.
2069 */
2070 if (time_after(jiffies, timeout)) {
2071 pr_err("%s: Card stuck in programming state! %s\n",
2072 mmc_hostname(card->host), __func__);
2073 err = -EIO;
2074 goto out;
2075 }
2076
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002077 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
Trey Ramsay8fee4762012-11-16 09:31:41 -06002078 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002079out:
2080 return err;
2081}
2082
2083/**
2084 * mmc_erase - erase sectors.
2085 * @card: card to erase
2086 * @from: first sector to erase
2087 * @nr: number of sectors to erase
2088 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
2089 *
2090 * Caller must claim host before calling this function.
2091 */
2092int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
2093 unsigned int arg)
2094{
2095 unsigned int rem, to = from + nr;
2096
2097 if (!(card->host->caps & MMC_CAP_ERASE) ||
2098 !(card->csd.cmdclass & CCC_ERASE))
2099 return -EOPNOTSUPP;
2100
2101 if (!card->erase_size)
2102 return -EOPNOTSUPP;
2103
2104 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
2105 return -EOPNOTSUPP;
2106
2107 if ((arg & MMC_SECURE_ARGS) &&
2108 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
2109 return -EOPNOTSUPP;
2110
2111 if ((arg & MMC_TRIM_ARGS) &&
2112 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
2113 return -EOPNOTSUPP;
2114
2115 if (arg == MMC_SECURE_ERASE_ARG) {
2116 if (from % card->erase_size || nr % card->erase_size)
2117 return -EINVAL;
2118 }
2119
2120 if (arg == MMC_ERASE_ARG) {
2121 rem = from % card->erase_size;
2122 if (rem) {
2123 rem = card->erase_size - rem;
2124 from += rem;
2125 if (nr > rem)
2126 nr -= rem;
2127 else
2128 return 0;
2129 }
2130 rem = nr % card->erase_size;
2131 if (rem)
2132 nr -= rem;
2133 }
2134
2135 if (nr == 0)
2136 return 0;
2137
2138 to = from + nr;
2139
2140 if (to <= from)
2141 return -EINVAL;
2142
2143 /* 'from' and 'to' are inclusive */
2144 to -= 1;
2145
2146 return mmc_do_erase(card, from, to, arg);
2147}
2148EXPORT_SYMBOL(mmc_erase);
2149
2150int mmc_can_erase(struct mmc_card *card)
2151{
2152 if ((card->host->caps & MMC_CAP_ERASE) &&
2153 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
2154 return 1;
2155 return 0;
2156}
2157EXPORT_SYMBOL(mmc_can_erase);
2158
2159int mmc_can_trim(struct mmc_card *card)
2160{
2161 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
2162 return 1;
2163 return 0;
2164}
2165EXPORT_SYMBOL(mmc_can_trim);
2166
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09002167int mmc_can_discard(struct mmc_card *card)
2168{
2169 /*
2170 * As there's no way to detect the discard support bit at v4.5
2171 * use the s/w feature support filed.
2172 */
2173 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2174 return 1;
2175 return 0;
2176}
2177EXPORT_SYMBOL(mmc_can_discard);
2178
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09002179int mmc_can_sanitize(struct mmc_card *card)
2180{
Adrian Hunter28302812012-04-05 14:45:48 +03002181 if (!mmc_can_trim(card) && !mmc_can_erase(card))
2182 return 0;
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09002183 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2184 return 1;
2185 return 0;
2186}
2187EXPORT_SYMBOL(mmc_can_sanitize);
2188
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002189int mmc_can_secure_erase_trim(struct mmc_card *card)
2190{
Lukas Czerner5204d002014-06-18 13:18:07 +02002191 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
2192 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002193 return 1;
2194 return 0;
2195}
2196EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2197
2198int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2199 unsigned int nr)
2200{
2201 if (!card->erase_size)
2202 return 0;
2203 if (from % card->erase_size || nr % card->erase_size)
2204 return 0;
2205 return 1;
2206}
2207EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Adrian Huntere056a1b2011-06-28 17:16:02 +03002209static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2210 unsigned int arg)
2211{
2212 struct mmc_host *host = card->host;
2213 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2214 unsigned int last_timeout = 0;
2215
2216 if (card->erase_shift)
2217 max_qty = UINT_MAX >> card->erase_shift;
2218 else if (mmc_card_sd(card))
2219 max_qty = UINT_MAX;
2220 else
2221 max_qty = UINT_MAX / card->erase_size;
2222
2223 /* Find the largest qty with an OK timeout */
2224 do {
2225 y = 0;
2226 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2227 timeout = mmc_erase_timeout(card, arg, qty + x);
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002228 if (timeout > host->max_busy_timeout)
Adrian Huntere056a1b2011-06-28 17:16:02 +03002229 break;
2230 if (timeout < last_timeout)
2231 break;
2232 last_timeout = timeout;
2233 y = x;
2234 }
2235 qty += y;
2236 } while (y);
2237
2238 if (!qty)
2239 return 0;
2240
2241 if (qty == 1)
2242 return 1;
2243
2244 /* Convert qty to sectors */
2245 if (card->erase_shift)
2246 max_discard = --qty << card->erase_shift;
2247 else if (mmc_card_sd(card))
2248 max_discard = qty;
2249 else
2250 max_discard = --qty * card->erase_size;
2251
2252 return max_discard;
2253}
2254
2255unsigned int mmc_calc_max_discard(struct mmc_card *card)
2256{
2257 struct mmc_host *host = card->host;
2258 unsigned int max_discard, max_trim;
2259
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002260 if (!host->max_busy_timeout)
Adrian Huntere056a1b2011-06-28 17:16:02 +03002261 return UINT_MAX;
2262
2263 /*
2264 * Without erase_group_def set, MMC erase timeout depends on clock
2265 * frequence which can change. In that case, the best choice is
2266 * just the preferred erase size.
2267 */
2268 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2269 return card->pref_erase;
2270
2271 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2272 if (mmc_can_trim(card)) {
2273 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2274 if (max_trim < max_discard)
2275 max_discard = max_trim;
2276 } else if (max_discard < card->erase_size) {
2277 max_discard = 0;
2278 }
2279 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
Ulf Hansson68eb80e2013-12-18 09:57:38 +01002280 mmc_hostname(host), max_discard, host->max_busy_timeout);
Adrian Huntere056a1b2011-06-28 17:16:02 +03002281 return max_discard;
2282}
2283EXPORT_SYMBOL(mmc_calc_max_discard);
2284
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002285int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2286{
Chris Ball1278dba2011-04-13 23:40:30 -04002287 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002288
Seungwon Jeoncdc99172014-04-23 17:07:35 +09002289 if (mmc_card_blockaddr(card) || mmc_card_ddr52(card))
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002290 return 0;
2291
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002292 cmd.opcode = MMC_SET_BLOCKLEN;
2293 cmd.arg = blocklen;
2294 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2295 return mmc_wait_for_cmd(card->host, &cmd, 5);
2296}
2297EXPORT_SYMBOL(mmc_set_blocklen);
2298
Loic Pallardy67c79db2012-08-06 17:12:30 +02002299int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2300 bool is_rel_write)
2301{
2302 struct mmc_command cmd = {0};
2303
2304 cmd.opcode = MMC_SET_BLOCK_COUNT;
2305 cmd.arg = blockcount & 0x0000FFFF;
2306 if (is_rel_write)
2307 cmd.arg |= 1 << 31;
2308 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2309 return mmc_wait_for_cmd(card->host, &cmd, 5);
2310}
2311EXPORT_SYMBOL(mmc_set_blockcount);
2312
Adrian Hunterb2499512011-08-29 16:42:11 +03002313static void mmc_hw_reset_for_init(struct mmc_host *host)
2314{
2315 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2316 return;
2317 mmc_host_clk_hold(host);
2318 host->ops->hw_reset(host);
2319 mmc_host_clk_release(host);
2320}
2321
Johan Rudholm83533ab2015-01-12 15:38:04 +01002322int mmc_hw_reset(struct mmc_host *host)
Adrian Hunterb2499512011-08-29 16:42:11 +03002323{
Johan Rudholmf855a372015-01-12 15:38:05 +01002324 int ret;
Adrian Hunterb2499512011-08-29 16:42:11 +03002325
Johan Rudholmf855a372015-01-12 15:38:05 +01002326 if (!host->card)
Adrian Hunterb2499512011-08-29 16:42:11 +03002327 return -EINVAL;
2328
Johan Rudholmf855a372015-01-12 15:38:05 +01002329 mmc_bus_get(host);
2330 if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
2331 mmc_bus_put(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03002332 return -EOPNOTSUPP;
Adrian Hunterb2499512011-08-29 16:42:11 +03002333 }
2334
Johan Rudholmf855a372015-01-12 15:38:05 +01002335 ret = host->bus_ops->reset(host);
2336 mmc_bus_put(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03002337
Johan Rudholmf855a372015-01-12 15:38:05 +01002338 pr_warn("%s: tried to reset card\n", mmc_hostname(host));
Adrian Hunterb2499512011-08-29 16:42:11 +03002339
Johan Rudholmf855a372015-01-12 15:38:05 +01002340 return ret;
Adrian Hunterb2499512011-08-29 16:42:11 +03002341}
Adrian Hunterb2499512011-08-29 16:42:11 +03002342EXPORT_SYMBOL(mmc_hw_reset);
2343
Andy Ross807e8e42011-01-03 10:36:56 -08002344static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2345{
2346 host->f_init = freq;
2347
2348#ifdef CONFIG_MMC_DEBUG
2349 pr_info("%s: %s: trying to init card at %u Hz\n",
2350 mmc_hostname(host), __func__, host->f_init);
2351#endif
Ulf Hansson4a065192013-09-12 14:36:53 +02002352 mmc_power_up(host, host->ocr_avail);
Philip Rakity2f94e552011-02-13 23:12:28 -08002353
2354 /*
Adrian Hunterb2499512011-08-29 16:42:11 +03002355 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2356 * do a hardware reset if possible.
2357 */
2358 mmc_hw_reset_for_init(host);
2359
2360 /*
Philip Rakity2f94e552011-02-13 23:12:28 -08002361 * sdio_reset sends CMD52 to reset card. Since we do not know
2362 * if the card is being re-initialized, just send it. CMD52
2363 * should be ignored by SD/eMMC cards.
2364 */
Andy Ross807e8e42011-01-03 10:36:56 -08002365 sdio_reset(host);
2366 mmc_go_idle(host);
2367
2368 mmc_send_if_cond(host, host->ocr_avail);
2369
2370 /* Order's important: probe SDIO, then SD, then MMC */
2371 if (!mmc_attach_sdio(host))
2372 return 0;
2373 if (!mmc_attach_sd(host))
2374 return 0;
2375 if (!mmc_attach_mmc(host))
2376 return 0;
2377
2378 mmc_power_off(host);
2379 return -EIO;
2380}
2381
Adrian Hunterd3049502011-11-28 16:22:00 +02002382int _mmc_detect_card_removed(struct mmc_host *host)
2383{
2384 int ret;
2385
Ulf Hansson5601aaf2013-10-30 23:15:30 +01002386 if (host->caps & MMC_CAP_NONREMOVABLE)
Adrian Hunterd3049502011-11-28 16:22:00 +02002387 return 0;
2388
2389 if (!host->card || mmc_card_removed(host->card))
2390 return 1;
2391
2392 ret = host->bus_ops->alive(host);
Kevin Liu14507342013-02-28 15:29:29 +08002393
2394 /*
2395 * Card detect status and alive check may be out of sync if card is
2396 * removed slowly, when card detect switch changes while card/slot
2397 * pads are still contacted in hardware (refer to "SD Card Mechanical
2398 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2399 * detect work 200ms later for this case.
2400 */
2401 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2402 mmc_detect_change(host, msecs_to_jiffies(200));
2403 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2404 }
2405
Adrian Hunterd3049502011-11-28 16:22:00 +02002406 if (ret) {
2407 mmc_card_set_removed(host->card);
2408 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2409 }
2410
2411 return ret;
2412}
2413
2414int mmc_detect_card_removed(struct mmc_host *host)
2415{
2416 struct mmc_card *card = host->card;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002417 int ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002418
2419 WARN_ON(!host->claimed);
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002420
2421 if (!card)
2422 return 1;
2423
2424 ret = mmc_card_removed(card);
Adrian Hunterd3049502011-11-28 16:22:00 +02002425 /*
2426 * The card will be considered unchanged unless we have been asked to
2427 * detect a change or host requires polling to provide card detection.
2428 */
Ulf Hanssonb6891672013-04-18 11:02:07 +02002429 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002430 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002431
2432 host->detect_change = 0;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002433 if (!ret) {
2434 ret = _mmc_detect_card_removed(host);
Ulf Hanssonb6891672013-04-18 11:02:07 +02002435 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002436 /*
2437 * Schedule a detect work as soon as possible to let a
2438 * rescan handle the card removal.
2439 */
2440 cancel_delayed_work(&host->detect);
Ulf Hanssonbbd43682013-09-20 11:02:35 +02002441 _mmc_detect_change(host, 0, false);
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002442 }
2443 }
Adrian Hunterd3049502011-11-28 16:22:00 +02002444
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002445 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002446}
2447EXPORT_SYMBOL(mmc_detect_card_removed);
2448
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002449void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
David Howellsc4028952006-11-22 14:57:56 +00002451 struct mmc_host *host =
2452 container_of(work, struct mmc_host, detect.work);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002453 int i;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002454
Markus Mayerfa372a52014-04-08 15:19:43 -07002455 if (host->trigger_card_event && host->ops->card_event) {
2456 host->ops->card_event(host);
2457 host->trigger_card_event = false;
2458 }
2459
Andy Ross807e8e42011-01-03 10:36:56 -08002460 if (host->rescan_disable)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002461 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Johan Rudholm3339d1e2012-08-23 13:40:55 +02002463 /* If there is a non-removable card registered, only scan once */
2464 if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2465 return;
2466 host->rescan_entered = 1;
2467
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002468 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02002470 /*
2471 * if there is a _removable_ card registered, check whether it is
2472 * still present
2473 */
Ulf Hansson5601aaf2013-10-30 23:15:30 +01002474 if (host->bus_ops && !host->bus_dead
Ohad Ben-Cohenbad3bab2011-03-08 23:32:02 +02002475 && !(host->caps & MMC_CAP_NONREMOVABLE))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002476 host->bus_ops->detect(host);
2477
Adrian Hunterd3049502011-11-28 16:22:00 +02002478 host->detect_change = 0;
2479
Chris Ballc5841792011-01-04 12:20:22 -05002480 /*
2481 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2482 * the card is no longer present.
2483 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002484 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002485 mmc_bus_get(host);
2486
2487 /* if there still is a card present, stop here */
2488 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002489 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002490 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002492
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002493 /*
2494 * Only we can add a new handler, so it's safe to
2495 * release the lock here.
2496 */
2497 mmc_bus_put(host);
2498
Sascha Hauerc1b55bf2013-12-05 14:34:46 +01002499 if (!(host->caps & MMC_CAP_NONREMOVABLE) && host->ops->get_cd &&
2500 host->ops->get_cd(host) == 0) {
Ulf Hanssonfa550182012-05-09 16:15:26 +02002501 mmc_claim_host(host);
2502 mmc_power_off(host);
2503 mmc_release_host(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002504 goto out;
Ulf Hanssonfa550182012-05-09 16:15:26 +02002505 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002506
Andy Ross807e8e42011-01-03 10:36:56 -08002507 mmc_claim_host(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002508 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
Andy Ross807e8e42011-01-03 10:36:56 -08002509 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2510 break;
Jaehoon Chung06b22332011-05-12 17:18:59 +09002511 if (freqs[i] <= host->f_min)
Andy Ross807e8e42011-01-03 10:36:56 -08002512 break;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002513 }
Andy Ross807e8e42011-01-03 10:36:56 -08002514 mmc_release_host(host);
2515
2516 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04002517 if (host->caps & MMC_CAP_NEEDS_POLL)
2518 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519}
2520
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002521void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522{
Ulf Hanssonfa550182012-05-09 16:15:26 +02002523 host->f_init = max(freqs[0], host->f_min);
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02002524 host->rescan_disable = 0;
Roger Tseng8af465d2014-09-24 17:07:13 +08002525 host->ios.power_mode = MMC_POWER_UNDEFINED;
Adrian Huntera08b17b2013-04-15 11:27:25 -04002526 if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
2527 mmc_power_off(host);
2528 else
Ulf Hansson4a065192013-09-12 14:36:53 +02002529 mmc_power_up(host, host->ocr_avail);
Adrian Hunter740a2212014-03-10 15:02:41 +02002530 mmc_gpiod_request_cd_irq(host);
Ulf Hanssonbbd43682013-09-20 11:02:35 +02002531 _mmc_detect_change(host, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002534void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Pierre Ossman3b91e552007-02-11 20:43:19 +01002536#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002537 unsigned long flags;
2538 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002539 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002540 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002541#endif
Adrian Hunter740a2212014-03-10 15:02:41 +02002542 if (host->slot.cd_irq >= 0)
2543 disable_irq(host->slot.cd_irq);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002544
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02002545 host->rescan_disable = 1;
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01002546 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002547 mmc_flush_scheduled_work();
2548
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002549 /* clear pm flags now and let card drivers set them as needed */
2550 host->pm_flags = 0;
2551
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002552 mmc_bus_get(host);
2553 if (host->bus_ops && !host->bus_dead) {
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002554 /* Calling bus_ops->remove() with a claimed host can deadlock */
Ulf Hansson58a8a4a2013-06-10 17:03:36 +02002555 host->bus_ops->remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002556 mmc_claim_host(host);
2557 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002558 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002559 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07002560 mmc_bus_put(host);
2561 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002563 mmc_bus_put(host);
2564
2565 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
2567 mmc_power_off(host);
2568}
2569
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002570int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002571{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002572 int ret = 0;
2573
Daniel Drakebb9cab92011-07-17 16:38:41 +01002574#ifdef CONFIG_MMC_DEBUG
2575 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2576#endif
2577
Adrian Huntereae1aee2009-09-22 16:44:33 -07002578 mmc_bus_get(host);
2579
Ulf Hansson5601aaf2013-10-30 23:15:30 +01002580 if (!host->bus_ops || host->bus_dead) {
Adrian Huntereae1aee2009-09-22 16:44:33 -07002581 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002582 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002583 }
2584
2585 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002586 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002587
2588 mmc_bus_put(host);
2589
2590 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002591
2592 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002593}
2594EXPORT_SYMBOL(mmc_power_save_host);
2595
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002596int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002597{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002598 int ret;
2599
Daniel Drakebb9cab92011-07-17 16:38:41 +01002600#ifdef CONFIG_MMC_DEBUG
2601 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2602#endif
2603
Adrian Huntereae1aee2009-09-22 16:44:33 -07002604 mmc_bus_get(host);
2605
Ulf Hansson5601aaf2013-10-30 23:15:30 +01002606 if (!host->bus_ops || host->bus_dead) {
Adrian Huntereae1aee2009-09-22 16:44:33 -07002607 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002608 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002609 }
2610
Ulf Hansson69041152013-09-13 11:31:33 +02002611 mmc_power_up(host, host->card->ocr);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002612 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002613
2614 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002615
2616 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002617}
2618EXPORT_SYMBOL(mmc_power_restore_host);
2619
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002620/*
2621 * Flush the cache to the non-volatile storage.
2622 */
2623int mmc_flush_cache(struct mmc_card *card)
2624{
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002625 int err = 0;
2626
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002627 if (mmc_card_mmc(card) &&
2628 (card->ext_csd.cache_size > 0) &&
2629 (card->ext_csd.cache_ctrl & 1)) {
2630 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2631 EXT_CSD_FLUSH_CACHE, 1, 0);
2632 if (err)
2633 pr_err("%s: cache flush error %d\n",
2634 mmc_hostname(card->host), err);
2635 }
2636
2637 return err;
2638}
2639EXPORT_SYMBOL(mmc_flush_cache);
2640
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641#ifdef CONFIG_PM
2642
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002643/* Do the card removal on suspend if card is assumed removeable
2644 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2645 to sync the card.
2646*/
2647int mmc_pm_notify(struct notifier_block *notify_block,
2648 unsigned long mode, void *unused)
2649{
2650 struct mmc_host *host = container_of(
2651 notify_block, struct mmc_host, pm_notify);
2652 unsigned long flags;
Ulf Hansson810cadd2013-06-10 17:03:37 +02002653 int err = 0;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002654
2655 switch (mode) {
2656 case PM_HIBERNATION_PREPARE:
2657 case PM_SUSPEND_PREPARE:
Grygorii Strashko184af162015-04-23 13:43:43 +03002658 case PM_RESTORE_PREPARE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002659 spin_lock_irqsave(&host->lock, flags);
2660 host->rescan_disable = 1;
2661 spin_unlock_irqrestore(&host->lock, flags);
2662 cancel_delayed_work_sync(&host->detect);
2663
Ulf Hansson810cadd2013-06-10 17:03:37 +02002664 if (!host->bus_ops)
2665 break;
2666
2667 /* Validate prerequisites for suspend */
2668 if (host->bus_ops->pre_suspend)
2669 err = host->bus_ops->pre_suspend(host);
Ulf Hansson5601aaf2013-10-30 23:15:30 +01002670 if (!err)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002671 break;
2672
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002673 /* Calling bus_ops->remove() with a claimed host can deadlock */
Ulf Hansson58a8a4a2013-06-10 17:03:36 +02002674 host->bus_ops->remove(host);
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002675 mmc_claim_host(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002676 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002677 mmc_power_off(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002678 mmc_release_host(host);
2679 host->pm_flags = 0;
2680 break;
2681
2682 case PM_POST_SUSPEND:
2683 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01002684 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002685
2686 spin_lock_irqsave(&host->lock, flags);
2687 host->rescan_disable = 0;
2688 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hanssonbbd43682013-09-20 11:02:35 +02002689 _mmc_detect_change(host, 0, false);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002690
2691 }
2692
2693 return 0;
2694}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695#endif
2696
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002697/**
2698 * mmc_init_context_info() - init synchronization context
2699 * @host: mmc host
2700 *
2701 * Init struct context_info needed to implement asynchronous
2702 * request mechanism, used by mmc core, host driver and mmc requests
2703 * supplier.
2704 */
2705void mmc_init_context_info(struct mmc_host *host)
2706{
2707 spin_lock_init(&host->context_info.lock);
2708 host->context_info.is_new_req = false;
2709 host->context_info.is_done_rcv = false;
2710 host->context_info.is_waiting_last_req = false;
2711 init_waitqueue_head(&host->context_info.wait);
2712}
2713
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002714static int __init mmc_init(void)
2715{
2716 int ret;
2717
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002718 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002719 if (!workqueue)
2720 return -ENOMEM;
2721
2722 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002723 if (ret)
2724 goto destroy_workqueue;
2725
2726 ret = mmc_register_host_class();
2727 if (ret)
2728 goto unregister_bus;
2729
2730 ret = sdio_register_bus();
2731 if (ret)
2732 goto unregister_host_class;
2733
2734 return 0;
2735
2736unregister_host_class:
2737 mmc_unregister_host_class();
2738unregister_bus:
2739 mmc_unregister_bus();
2740destroy_workqueue:
2741 destroy_workqueue(workqueue);
2742
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002743 return ret;
2744}
2745
2746static void __exit mmc_exit(void)
2747{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002748 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002749 mmc_unregister_host_class();
2750 mmc_unregister_bus();
2751 destroy_workqueue(workqueue);
2752}
2753
Nicolas Pitre26074962007-06-16 02:07:53 -04002754subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002755module_exit(mmc_exit);
2756
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757MODULE_LICENSE("GPL");