blob: 3bf1c462eded498c9824757ffea2971274ad2049 [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>
Amerigo Wang35eb6db2011-07-25 17:13:11 -070026#include <linux/suspend.h>
Per Forlin1b676f72011-08-19 14:52:37 +020027#include <linux/fault-inject.h>
28#include <linux/random.h>
Jaehoon Chung950d56a2012-09-17 08:42:02 +000029#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31#include <linux/mmc/card.h>
32#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010033#include <linux/mmc/mmc.h>
34#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010036#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020037#include "bus.h"
38#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020039#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010040
41#include "mmc_ops.h"
42#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020043#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Trey Ramsay8fee4762012-11-16 09:31:41 -060045/* If the device is not responding */
46#define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
47
Jaehoon Chung950d56a2012-09-17 08:42:02 +000048/*
49 * Background operations can take a long time, depending on the housekeeping
50 * operations the card has to perform.
51 */
52#define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */
53
Pierre Ossmanffce2e72007-05-19 14:32:22 +020054static struct workqueue_struct *workqueue;
Ulf Hanssonfa550182012-05-09 16:15:26 +020055static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
Pierre Ossmanffce2e72007-05-19 14:32:22 +020056
57/*
David Brownellaf517152007-08-08 09:11:32 -070058 * Enabling software CRCs on the data blocks can be a significant (30%)
59 * performance cost, and for other reasons may not always be desired.
60 * So we allow it it to be disabled.
61 */
Rusty Russell90ab5ee2012-01-13 09:32:20 +103062bool use_spi_crc = 1;
David Brownellaf517152007-08-08 09:11:32 -070063module_param(use_spi_crc, bool, 0);
64
65/*
Ben Hutchingsbd68e082009-12-14 18:01:29 -080066 * We normally treat cards as removed during suspend if they are not
67 * known to be on a non-removable bus, to avoid the risk of writing
68 * back data to a different card after resume. Allow this to be
69 * overridden if necessary.
70 */
71#ifdef CONFIG_MMC_UNSAFE_RESUME
Rusty Russell90ab5ee2012-01-13 09:32:20 +103072bool mmc_assume_removable;
Ben Hutchingsbd68e082009-12-14 18:01:29 -080073#else
Rusty Russell90ab5ee2012-01-13 09:32:20 +103074bool mmc_assume_removable = 1;
Ben Hutchingsbd68e082009-12-14 18:01:29 -080075#endif
Matt Fleming71d7d3d2010-09-27 09:42:19 +010076EXPORT_SYMBOL(mmc_assume_removable);
Ben Hutchingsbd68e082009-12-14 18:01:29 -080077module_param_named(removable, mmc_assume_removable, bool, 0644);
78MODULE_PARM_DESC(
79 removable,
80 "MMC/SD cards are removable and may be removed during suspend");
81
82/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020083 * Internal function. Schedule delayed work in the MMC work queue.
84 */
85static int mmc_schedule_delayed_work(struct delayed_work *work,
86 unsigned long delay)
87{
88 return queue_delayed_work(workqueue, work, delay);
89}
90
91/*
92 * Internal function. Flush all scheduled work from the MMC work queue.
93 */
94static void mmc_flush_scheduled_work(void)
95{
96 flush_workqueue(workqueue);
97}
98
Per Forlin1b676f72011-08-19 14:52:37 +020099#ifdef CONFIG_FAIL_MMC_REQUEST
100
101/*
102 * Internal function. Inject random data errors.
103 * If mmc_data is NULL no errors are injected.
104 */
105static void mmc_should_fail_request(struct mmc_host *host,
106 struct mmc_request *mrq)
107{
108 struct mmc_command *cmd = mrq->cmd;
109 struct mmc_data *data = mrq->data;
110 static const int data_errors[] = {
111 -ETIMEDOUT,
112 -EILSEQ,
113 -EIO,
114 };
115
116 if (!data)
117 return;
118
119 if (cmd->error || data->error ||
120 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
121 return;
122
123 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
124 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
125}
126
127#else /* CONFIG_FAIL_MMC_REQUEST */
128
129static inline void mmc_should_fail_request(struct mmc_host *host,
130 struct mmc_request *mrq)
131{
132}
133
134#endif /* CONFIG_FAIL_MMC_REQUEST */
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/**
Russell Kingfe10c6a2006-05-04 13:51:45 +0100137 * mmc_request_done - finish processing an MMC request
138 * @host: MMC host which completed request
139 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +0100142 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
144void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
145{
146 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +0100147 int err = cmd->error;
148
David Brownellaf517152007-08-08 09:11:32 -0700149 if (err && cmd->retries && mmc_host_is_spi(host)) {
150 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
151 cmd->retries = 0;
152 }
153
Adrian Hunterd3049502011-11-28 16:22:00 +0200154 if (err && cmd->retries && !mmc_card_removed(host->card)) {
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300155 /*
156 * Request starter must handle retries - see
157 * mmc_wait_for_req_done().
158 */
159 if (mrq->done)
160 mrq->done(mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +0200161 } else {
Per Forlin1b676f72011-08-19 14:52:37 +0200162 mmc_should_fail_request(host, mrq);
163
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200164 led_trigger_event(host->led, LED_OFF);
165
Pierre Ossmane4d21702007-07-24 21:46:49 +0200166 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
167 mmc_hostname(host), cmd->opcode, err,
168 cmd->resp[0], cmd->resp[1],
169 cmd->resp[2], cmd->resp[3]);
170
171 if (mrq->data) {
172 pr_debug("%s: %d bytes transferred: %d\n",
173 mmc_hostname(host),
174 mrq->data->bytes_xfered, mrq->data->error);
175 }
176
177 if (mrq->stop) {
178 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
179 mmc_hostname(host), mrq->stop->opcode,
180 mrq->stop->error,
181 mrq->stop->resp[0], mrq->stop->resp[1],
182 mrq->stop->resp[2], mrq->stop->resp[3]);
183 }
184
185 if (mrq->done)
186 mrq->done(mrq);
Linus Walleij04566832010-11-08 21:36:50 -0500187
Mika Westerberg08c14072011-08-18 15:23:47 +0300188 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190}
191
192EXPORT_SYMBOL(mmc_request_done);
193
Adrian Bunk39361852007-07-25 00:40:58 +0200194static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
196{
Pierre Ossman976d9272007-04-13 22:47:01 +0200197#ifdef CONFIG_MMC_DEBUG
198 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200199 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200200#endif
201
Jaehoon Chung7b2fd4f2012-02-07 14:13:10 +0900202 if (mrq->sbc) {
203 pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n",
204 mmc_hostname(host), mrq->sbc->opcode,
205 mrq->sbc->arg, mrq->sbc->flags);
206 }
207
Russell King920e70c2006-05-04 18:22:51 +0100208 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
209 mmc_hostname(host), mrq->cmd->opcode,
210 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Pierre Ossmane4d21702007-07-24 21:46:49 +0200212 if (mrq->data) {
213 pr_debug("%s: blksz %d blocks %d flags %08x "
214 "tsac %d ms nsac %d\n",
215 mmc_hostname(host), mrq->data->blksz,
216 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200217 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200218 mrq->data->timeout_clks);
219 }
220
221 if (mrq->stop) {
222 pr_debug("%s: CMD%u arg %08x flags %08x\n",
223 mmc_hostname(host), mrq->stop->opcode,
224 mrq->stop->arg, mrq->stop->flags);
225 }
226
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100227 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 mrq->cmd->error = 0;
230 mrq->cmd->mrq = mrq;
231 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);
256}
257
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000258/**
259 * mmc_start_bkops - start BKOPS for supported cards
260 * @card: MMC card to start BKOPS
261 * @form_exception: A flag to indicate if this function was
262 * called due to an exception raised by the card
263 *
264 * Start background operations whenever requested.
265 * When the urgent BKOPS bit is set in a R1 command response
266 * then background operations should be started immediately.
267*/
268void mmc_start_bkops(struct mmc_card *card, bool from_exception)
269{
270 int err;
271 int timeout;
272 bool use_busy_signal;
273
274 BUG_ON(!card);
275
276 if (!card->ext_csd.bkops_en || mmc_card_doing_bkops(card))
277 return;
278
279 err = mmc_read_bkops_status(card);
280 if (err) {
281 pr_err("%s: Failed to read bkops status: %d\n",
282 mmc_hostname(card->host), err);
283 return;
284 }
285
286 if (!card->ext_csd.raw_bkops_status)
287 return;
288
289 if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 &&
290 from_exception)
291 return;
292
293 mmc_claim_host(card->host);
294 if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) {
295 timeout = MMC_BKOPS_MAX_TIMEOUT;
296 use_busy_signal = true;
297 } else {
298 timeout = 0;
299 use_busy_signal = false;
300 }
301
302 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
303 EXT_CSD_BKOPS_START, 1, timeout, use_busy_signal);
304 if (err) {
305 pr_warn("%s: Error %d starting bkops\n",
306 mmc_hostname(card->host), err);
307 goto out;
308 }
309
310 /*
311 * For urgent bkops status (LEVEL_2 and more)
312 * bkops executed synchronously, otherwise
313 * the operation is in progress
314 */
315 if (!use_busy_signal)
316 mmc_card_set_doing_bkops(card);
317out:
318 mmc_release_host(card->host);
319}
320EXPORT_SYMBOL(mmc_start_bkops);
321
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500322/*
323 * mmc_wait_data_done() - done callback for data request
324 * @mrq: done data request
325 *
326 * Wakes up mmc context, passed as a callback to host controller driver
327 */
328static void mmc_wait_data_done(struct mmc_request *mrq)
329{
330 mrq->host->context_info.is_done_rcv = true;
331 wake_up_interruptible(&mrq->host->context_info.wait);
332}
333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334static void mmc_wait_done(struct mmc_request *mrq)
335{
Per Forlinaa8b6832011-07-01 18:55:22 +0200336 complete(&mrq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337}
338
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500339/*
340 *__mmc_start_data_req() - starts data request
341 * @host: MMC host to start the request
342 * @mrq: data request to start
343 *
344 * Sets the done callback to be called when request is completed by the card.
345 * Starts data mmc request execution
346 */
347static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
348{
349 mrq->done = mmc_wait_data_done;
350 mrq->host = host;
351 if (mmc_card_removed(host->card)) {
352 mrq->cmd->error = -ENOMEDIUM;
Seungwon Jeon9b844962013-01-22 19:48:03 +0900353 mmc_wait_data_done(mrq);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500354 return -ENOMEDIUM;
355 }
356 mmc_start_request(host, mrq);
357
358 return 0;
359}
360
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100361static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
Per Forlinaa8b6832011-07-01 18:55:22 +0200362{
363 init_completion(&mrq->completion);
364 mrq->done = mmc_wait_done;
Adrian Hunterd3049502011-11-28 16:22:00 +0200365 if (mmc_card_removed(host->card)) {
366 mrq->cmd->error = -ENOMEDIUM;
367 complete(&mrq->completion);
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100368 return -ENOMEDIUM;
Adrian Hunterd3049502011-11-28 16:22:00 +0200369 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200370 mmc_start_request(host, mrq);
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100371 return 0;
Per Forlinaa8b6832011-07-01 18:55:22 +0200372}
373
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500374/*
375 * mmc_wait_for_data_req_done() - wait for request completed
376 * @host: MMC host to prepare the command.
377 * @mrq: MMC request to wait for
378 *
379 * Blocks MMC context till host controller will ack end of data request
380 * execution or new request notification arrives from the block layer.
381 * Handles command retries.
382 *
383 * Returns enum mmc_blk_status after checking errors.
384 */
385static int mmc_wait_for_data_req_done(struct mmc_host *host,
386 struct mmc_request *mrq,
387 struct mmc_async_req *next_req)
388{
389 struct mmc_command *cmd;
390 struct mmc_context_info *context_info = &host->context_info;
391 int err;
392 unsigned long flags;
393
394 while (1) {
395 wait_event_interruptible(context_info->wait,
396 (context_info->is_done_rcv ||
397 context_info->is_new_req));
398 spin_lock_irqsave(&context_info->lock, flags);
399 context_info->is_waiting_last_req = false;
400 spin_unlock_irqrestore(&context_info->lock, flags);
401 if (context_info->is_done_rcv) {
402 context_info->is_done_rcv = false;
403 context_info->is_new_req = false;
404 cmd = mrq->cmd;
405 if (!cmd->error || !cmd->retries ||
406 mmc_card_removed(host->card)) {
407 err = host->areq->err_check(host->card,
408 host->areq);
409 break; /* return err */
410 } else {
411 pr_info("%s: req failed (CMD%u): %d, retrying...\n",
412 mmc_hostname(host),
413 cmd->opcode, cmd->error);
414 cmd->retries--;
415 cmd->error = 0;
416 host->ops->request(host, mrq);
417 continue; /* wait for done/new event again */
418 }
419 } else if (context_info->is_new_req) {
420 context_info->is_new_req = false;
421 if (!next_req) {
422 err = MMC_BLK_NEW_REQUEST;
423 break; /* return err */
424 }
425 }
426 }
427 return err;
428}
429
Per Forlinaa8b6832011-07-01 18:55:22 +0200430static void mmc_wait_for_req_done(struct mmc_host *host,
431 struct mmc_request *mrq)
432{
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300433 struct mmc_command *cmd;
434
435 while (1) {
436 wait_for_completion(&mrq->completion);
437
438 cmd = mrq->cmd;
Adrian Hunterd3049502011-11-28 16:22:00 +0200439 if (!cmd->error || !cmd->retries ||
440 mmc_card_removed(host->card))
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300441 break;
442
443 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
444 mmc_hostname(host), cmd->opcode, cmd->error);
445 cmd->retries--;
446 cmd->error = 0;
447 host->ops->request(host, mrq);
448 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200449}
450
451/**
452 * mmc_pre_req - Prepare for a new request
453 * @host: MMC host to prepare command
454 * @mrq: MMC request to prepare for
455 * @is_first_req: true if there is no previous started request
456 * that may run in parellel to this call, otherwise false
457 *
458 * mmc_pre_req() is called in prior to mmc_start_req() to let
459 * host prepare for the new request. Preparation of a request may be
460 * performed while another request is running on the host.
461 */
462static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
463 bool is_first_req)
464{
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500465 if (host->ops->pre_req) {
466 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +0200467 host->ops->pre_req(host, mrq, is_first_req);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500468 mmc_host_clk_release(host);
469 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200470}
471
472/**
473 * mmc_post_req - Post process a completed request
474 * @host: MMC host to post process command
475 * @mrq: MMC request to post process for
476 * @err: Error, if non zero, clean up any resources made in pre_req
477 *
478 * Let the host post process a completed request. Post processing of
479 * a request may be performed while another reuqest is running.
480 */
481static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
482 int err)
483{
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500484 if (host->ops->post_req) {
485 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +0200486 host->ops->post_req(host, mrq, err);
Sujit Reddy Thumma2c4967f742012-02-04 16:14:50 -0500487 mmc_host_clk_release(host);
488 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200489}
490
491/**
492 * mmc_start_req - start a non-blocking request
493 * @host: MMC host to start command
494 * @areq: async request to start
495 * @error: out parameter returns 0 for success, otherwise non zero
496 *
497 * Start a new MMC custom command request for a host.
498 * If there is on ongoing async request wait for completion
499 * of that request and start the new one and return.
500 * Does not wait for the new request to complete.
501 *
502 * Returns the completed request, NULL in case of none completed.
503 * Wait for the an ongoing request (previoulsy started) to complete and
504 * return the completed request. If there is no ongoing request, NULL
505 * is returned without waiting. NULL is not an error condition.
506 */
507struct mmc_async_req *mmc_start_req(struct mmc_host *host,
508 struct mmc_async_req *areq, int *error)
509{
510 int err = 0;
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100511 int start_err = 0;
Per Forlinaa8b6832011-07-01 18:55:22 +0200512 struct mmc_async_req *data = host->areq;
513
514 /* Prepare a new request */
515 if (areq)
516 mmc_pre_req(host, areq->mrq, !host->areq);
517
518 if (host->areq) {
Jaehoon Chungf5c27582013-02-01 14:32:22 +0900519 err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq);
520 if (err == MMC_BLK_NEW_REQUEST) {
521 if (error)
522 *error = err;
523 /*
524 * The previous request was not completed,
525 * nothing to return
526 */
527 return NULL;
528 }
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000529 /*
530 * Check BKOPS urgency for each R1 response
531 */
532 if (host->card && mmc_card_mmc(host->card) &&
533 ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) ||
534 (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) &&
535 (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT))
536 mmc_start_bkops(host->card, true);
Per Forlinaa8b6832011-07-01 18:55:22 +0200537 }
538
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100539 if (!err && areq)
Konstantin Dorfman2220eed2013-01-14 14:28:17 -0500540 start_err = __mmc_start_data_req(host, areq->mrq);
Per Forlinaa8b6832011-07-01 18:55:22 +0200541
542 if (host->areq)
543 mmc_post_req(host, host->areq->mrq, 0);
544
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100545 /* Cancel a prepared request if it was not started. */
546 if ((err || start_err) && areq)
Jaehoon Chungf5c27582013-02-01 14:32:22 +0900547 mmc_post_req(host, areq->mrq, -EINVAL);
Ulf Hansson956d9fd2012-03-05 15:52:43 +0100548
549 if (err)
550 host->areq = NULL;
551 else
552 host->areq = areq;
553
Per Forlinaa8b6832011-07-01 18:55:22 +0200554 if (error)
555 *error = err;
556 return data;
557}
558EXPORT_SYMBOL(mmc_start_req);
559
Pierre Ossman67a61c42007-07-11 20:22:11 +0200560/**
561 * mmc_wait_for_req - start a request and wait for completion
562 * @host: MMC host to start command
563 * @mrq: MMC request to start
564 *
565 * Start a new MMC custom command request for a host, and wait
566 * for the command to complete. Does not attempt to parse the
567 * response.
568 */
569void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Per Forlinaa8b6832011-07-01 18:55:22 +0200571 __mmc_start_req(host, mrq);
572 mmc_wait_for_req_done(host, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574EXPORT_SYMBOL(mmc_wait_for_req);
575
576/**
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400577 * mmc_interrupt_hpi - Issue for High priority Interrupt
578 * @card: the MMC card associated with the HPI transfer
579 *
580 * Issued High Priority Interrupt, and check for card status
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000581 * until out-of prg-state.
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400582 */
583int mmc_interrupt_hpi(struct mmc_card *card)
584{
585 int err;
586 u32 status;
Venkatraman S6af9e962012-06-22 11:42:36 +0530587 unsigned long prg_wait;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400588
589 BUG_ON(!card);
590
591 if (!card->ext_csd.hpi_en) {
592 pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host));
593 return 1;
594 }
595
596 mmc_claim_host(card->host);
597 err = mmc_send_status(card, &status);
598 if (err) {
599 pr_err("%s: Get card status fail\n", mmc_hostname(card->host));
600 goto out;
601 }
602
Venkatraman S6af9e962012-06-22 11:42:36 +0530603 switch (R1_CURRENT_STATE(status)) {
604 case R1_STATE_IDLE:
605 case R1_STATE_READY:
606 case R1_STATE_STBY:
Venkatraman S211d4fe2012-08-07 19:24:45 +0530607 case R1_STATE_TRAN:
Venkatraman S6af9e962012-06-22 11:42:36 +0530608 /*
Venkatraman S211d4fe2012-08-07 19:24:45 +0530609 * In idle and transfer states, HPI is not needed and the caller
Venkatraman S6af9e962012-06-22 11:42:36 +0530610 * can issue the next intended command immediately
611 */
612 goto out;
613 case R1_STATE_PRG:
614 break;
615 default:
616 /* In all other states, it's illegal to issue HPI */
617 pr_debug("%s: HPI cannot be sent. Card state=%d\n",
618 mmc_hostname(card->host), R1_CURRENT_STATE(status));
619 err = -EINVAL;
620 goto out;
621 }
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400622
Venkatraman S6af9e962012-06-22 11:42:36 +0530623 err = mmc_send_hpi_cmd(card, &status);
624 if (err)
625 goto out;
626
627 prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time);
628 do {
629 err = mmc_send_status(card, &status);
630
631 if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN)
632 break;
633 if (time_after(jiffies, prg_wait))
634 err = -ETIMEDOUT;
635 } while (!err);
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -0400636
637out:
638 mmc_release_host(card->host);
639 return err;
640}
641EXPORT_SYMBOL(mmc_interrupt_hpi);
642
643/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 * mmc_wait_for_cmd - start a command and wait for completion
645 * @host: MMC host to start command
646 * @cmd: MMC command to start
647 * @retries: maximum number of retries
648 *
649 * Start a new MMC command for a host, and wait for the command
650 * to complete. Return any error that occurred while the command
651 * was executing. Do not attempt to parse the response.
652 */
653int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
654{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530655 struct mmc_request mrq = {NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Pierre Ossmand84075c82007-08-09 13:23:56 +0200657 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 memset(cmd->resp, 0, sizeof(cmd->resp));
660 cmd->retries = retries;
661
662 mrq.cmd = cmd;
663 cmd->data = NULL;
664
665 mmc_wait_for_req(host, &mrq);
666
667 return cmd->error;
668}
669
670EXPORT_SYMBOL(mmc_wait_for_cmd);
671
Pierre Ossman335eadf2005-09-06 15:18:50 -0700672/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +0000673 * mmc_stop_bkops - stop ongoing BKOPS
674 * @card: MMC card to check BKOPS
675 *
676 * Send HPI command to stop ongoing background operations to
677 * allow rapid servicing of foreground operations, e.g. read/
678 * writes. Wait until the card comes out of the programming state
679 * to avoid errors in servicing read/write requests.
680 */
681int mmc_stop_bkops(struct mmc_card *card)
682{
683 int err = 0;
684
685 BUG_ON(!card);
686 err = mmc_interrupt_hpi(card);
687
688 /*
689 * If err is EINVAL, we can't issue an HPI.
690 * It should complete the BKOPS.
691 */
692 if (!err || (err == -EINVAL)) {
693 mmc_card_clr_doing_bkops(card);
694 err = 0;
695 }
696
697 return err;
698}
699EXPORT_SYMBOL(mmc_stop_bkops);
700
701int mmc_read_bkops_status(struct mmc_card *card)
702{
703 int err;
704 u8 *ext_csd;
705
706 /*
707 * In future work, we should consider storing the entire ext_csd.
708 */
709 ext_csd = kmalloc(512, GFP_KERNEL);
710 if (!ext_csd) {
711 pr_err("%s: could not allocate buffer to receive the ext_csd.\n",
712 mmc_hostname(card->host));
713 return -ENOMEM;
714 }
715
716 mmc_claim_host(card->host);
717 err = mmc_send_ext_csd(card, ext_csd);
718 mmc_release_host(card->host);
719 if (err)
720 goto out;
721
722 card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS];
723 card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS];
724out:
725 kfree(ext_csd);
726 return err;
727}
728EXPORT_SYMBOL(mmc_read_bkops_status);
729
730/**
Russell Kingd773d722006-09-07 15:57:12 +0100731 * mmc_set_data_timeout - set the timeout for a data command
732 * @data: data phase for command
733 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200734 *
735 * Computes the data timeout parameters according to the
736 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100737 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200738void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100739{
740 unsigned int mult;
741
742 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200743 * SDIO cards only define an upper 1 s limit on access.
744 */
745 if (mmc_card_sdio(card)) {
746 data->timeout_ns = 1000000000;
747 data->timeout_clks = 0;
748 return;
749 }
750
751 /*
Russell Kingd773d722006-09-07 15:57:12 +0100752 * SD cards use a 100 multiplier rather than 10
753 */
754 mult = mmc_card_sd(card) ? 100 : 10;
755
756 /*
757 * Scale up the multiplier (and therefore the timeout) by
758 * the r2w factor for writes.
759 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200760 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100761 mult <<= card->csd.r2w_factor;
762
763 data->timeout_ns = card->csd.tacc_ns * mult;
764 data->timeout_clks = card->csd.tacc_clks * mult;
765
766 /*
767 * SD cards also have an upper limit on the timeout.
768 */
769 if (mmc_card_sd(card)) {
770 unsigned int timeout_us, limit_us;
771
772 timeout_us = data->timeout_ns / 1000;
Linus Walleije9b86842011-01-05 00:44:32 +0100773 if (mmc_host_clk_rate(card->host))
774 timeout_us += data->timeout_clks * 1000 /
775 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100776
Pierre Ossmanb146d262007-07-24 19:16:54 +0200777 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100778 /*
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -0600779 * The MMC spec "It is strongly recommended
780 * for hosts to implement more than 500ms
781 * timeout value even if the card indicates
782 * the 250ms maximum busy length." Even the
783 * previous value of 300ms is known to be
784 * insufficient for some cards.
Pierre Ossman493890e2008-10-26 12:37:25 +0100785 */
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -0600786 limit_us = 3000000;
Russell Kingd773d722006-09-07 15:57:12 +0100787 else
788 limit_us = 100000;
789
Philip Langdalefba68bd2007-01-04 06:57:32 -0800790 /*
791 * SDHC cards always use these fixed values.
792 */
793 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100794 data->timeout_ns = limit_us * 1000;
795 data->timeout_clks = 0;
796 }
797 }
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +0100798
799 /*
800 * Some cards require longer data read timeout than indicated in CSD.
801 * Address this by setting the read timeout to a "reasonably high"
802 * value. For the cards tested, 300ms has proven enough. If necessary,
803 * this value can be increased if other problematic cards require this.
804 */
805 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
806 data->timeout_ns = 300000000;
807 data->timeout_clks = 0;
808 }
809
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100810 /*
811 * Some cards need very high timeouts if driven in SPI mode.
812 * The worst observed timeout was 900ms after writing a
813 * continuous stream of data until the internal logic
814 * overflowed.
815 */
816 if (mmc_host_is_spi(card->host)) {
817 if (data->flags & MMC_DATA_WRITE) {
818 if (data->timeout_ns < 1000000000)
819 data->timeout_ns = 1000000000; /* 1s */
820 } else {
821 if (data->timeout_ns < 100000000)
822 data->timeout_ns = 100000000; /* 100ms */
823 }
824 }
Russell Kingd773d722006-09-07 15:57:12 +0100825}
826EXPORT_SYMBOL(mmc_set_data_timeout);
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200829 * mmc_align_data_size - pads a transfer size to a more optimal value
830 * @card: the MMC card associated with the data transfer
831 * @sz: original transfer size
832 *
833 * Pads the original data size with a number of extra bytes in
834 * order to avoid controller bugs and/or performance hits
835 * (e.g. some controllers revert to PIO for certain sizes).
836 *
837 * Returns the improved size, which might be unmodified.
838 *
839 * Note that this function is only relevant when issuing a
840 * single scatter gather entry.
841 */
842unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
843{
844 /*
845 * FIXME: We don't have a system for the controller to tell
846 * the core about its problems yet, so for now we just 32-bit
847 * align the size.
848 */
849 sz = ((sz + 3) / 4) * 4;
850
851 return sz;
852}
853EXPORT_SYMBOL(mmc_align_data_size);
854
855/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200856 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200858 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200860 * Claim a host for a set of operations. If @abort is non null and
861 * dereference a non-zero value then this will return prematurely with
862 * that non-zero value without acquiring the lock. Returns zero
863 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200865int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
867 DECLARE_WAITQUEUE(wait, current);
868 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200869 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200871 might_sleep();
872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 add_wait_queue(&host->wq, &wait);
874 spin_lock_irqsave(&host->lock, flags);
875 while (1) {
876 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200877 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700878 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 break;
880 spin_unlock_irqrestore(&host->lock, flags);
881 schedule();
882 spin_lock_irqsave(&host->lock, flags);
883 }
884 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700885 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200886 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700887 host->claimer = current;
888 host->claim_cnt += 1;
889 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200890 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 spin_unlock_irqrestore(&host->lock, flags);
892 remove_wait_queue(&host->wq, &wait);
Adrian Hunter907d2e72012-02-29 09:17:21 +0200893 if (host->ops->enable && !stop && host->claim_cnt == 1)
894 host->ops->enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200895 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Nicolas Pitre2342f332007-06-30 16:21:52 +0200898EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Adrian Hunter319a3f12009-09-22 16:44:30 -0700900/**
901 * mmc_try_claim_host - try exclusively to claim a host
902 * @host: mmc host to claim
903 *
904 * Returns %1 if the host is claimed, %0 otherwise.
905 */
906int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700907{
908 int claimed_host = 0;
909 unsigned long flags;
910
911 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700912 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700913 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700914 host->claimer = current;
915 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700916 claimed_host = 1;
917 }
918 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter907d2e72012-02-29 09:17:21 +0200919 if (host->ops->enable && claimed_host && host->claim_cnt == 1)
920 host->ops->enable(host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700921 return claimed_host;
922}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700923EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700924
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100925/**
Adrian Hunter907d2e72012-02-29 09:17:21 +0200926 * mmc_release_host - release a host
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100927 * @host: mmc host to release
928 *
Adrian Hunter907d2e72012-02-29 09:17:21 +0200929 * Release a MMC host, allowing others to claim the host
930 * for their operations.
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100931 */
Adrian Hunter907d2e72012-02-29 09:17:21 +0200932void mmc_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700933{
934 unsigned long flags;
935
Adrian Hunter907d2e72012-02-29 09:17:21 +0200936 WARN_ON(!host->claimed);
937
938 if (host->ops->disable && host->claim_cnt == 1)
939 host->ops->disable(host);
940
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700941 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700942 if (--host->claim_cnt) {
943 /* Release for nested claim */
944 spin_unlock_irqrestore(&host->lock, flags);
945 } else {
946 host->claimed = 0;
947 host->claimer = NULL;
948 spin_unlock_irqrestore(&host->lock, flags);
949 wake_up(&host->wq);
950 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700951}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952EXPORT_SYMBOL(mmc_release_host);
953
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100954/*
955 * Internal function that does the actual ios call to the host driver,
956 * optionally printing some debug output.
957 */
Russell King920e70c2006-05-04 18:22:51 +0100958static inline void mmc_set_ios(struct mmc_host *host)
959{
960 struct mmc_ios *ios = &host->ios;
961
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100962 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
963 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100964 mmc_hostname(host), ios->clock, ios->bus_mode,
965 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100966 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800967
Linus Walleij04566832010-11-08 21:36:50 -0500968 if (ios->clock > 0)
969 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +0100970 host->ops->set_ios(host, ios);
971}
972
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100973/*
974 * Control chip select pin on a host.
975 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100976void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700977{
Mika Westerberg778e2772011-08-18 15:23:48 +0300978 mmc_host_clk_hold(host);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100979 host->ios.chip_select = mode;
980 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300981 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982}
983
984/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100985 * Sets the host clock to the highest possible frequency that
986 * is below "hz".
987 */
Mika Westerberg778e2772011-08-18 15:23:48 +0300988static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100989{
990 WARN_ON(hz < host->f_min);
991
992 if (hz > host->f_max)
993 hz = host->f_max;
994
995 host->ios.clock = hz;
996 mmc_set_ios(host);
997}
998
Mika Westerberg778e2772011-08-18 15:23:48 +0300999void mmc_set_clock(struct mmc_host *host, unsigned int hz)
1000{
1001 mmc_host_clk_hold(host);
1002 __mmc_set_clock(host, hz);
1003 mmc_host_clk_release(host);
1004}
1005
Linus Walleij04566832010-11-08 21:36:50 -05001006#ifdef CONFIG_MMC_CLKGATE
1007/*
1008 * This gates the clock by setting it to 0 Hz.
1009 */
1010void mmc_gate_clock(struct mmc_host *host)
1011{
1012 unsigned long flags;
1013
1014 spin_lock_irqsave(&host->clk_lock, flags);
1015 host->clk_old = host->ios.clock;
1016 host->ios.clock = 0;
1017 host->clk_gated = true;
1018 spin_unlock_irqrestore(&host->clk_lock, flags);
1019 mmc_set_ios(host);
1020}
1021
1022/*
1023 * This restores the clock from gating by using the cached
1024 * clock value.
1025 */
1026void mmc_ungate_clock(struct mmc_host *host)
1027{
1028 /*
1029 * We should previously have gated the clock, so the clock shall
1030 * be 0 here! The clock may however be 0 during initialization,
1031 * when some request operations are performed before setting
1032 * the frequency. When ungate is requested in that situation
1033 * we just ignore the call.
1034 */
1035 if (host->clk_old) {
1036 BUG_ON(host->ios.clock);
1037 /* This call will also set host->clk_gated to false */
Mika Westerberg778e2772011-08-18 15:23:48 +03001038 __mmc_set_clock(host, host->clk_old);
Linus Walleij04566832010-11-08 21:36:50 -05001039 }
1040}
1041
1042void mmc_set_ungated(struct mmc_host *host)
1043{
1044 unsigned long flags;
1045
1046 /*
1047 * We've been given a new frequency while the clock is gated,
1048 * so make sure we regard this as ungating it.
1049 */
1050 spin_lock_irqsave(&host->clk_lock, flags);
1051 host->clk_gated = false;
1052 spin_unlock_irqrestore(&host->clk_lock, flags);
1053}
1054
1055#else
1056void mmc_set_ungated(struct mmc_host *host)
1057{
1058}
1059#endif
1060
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001061/*
1062 * Change the bus mode (open drain/push-pull) of a host.
1063 */
1064void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
1065{
Mika Westerberg778e2772011-08-18 15:23:48 +03001066 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001067 host->ios.bus_mode = mode;
1068 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001069 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001070}
1071
1072/*
1073 * Change data bus width of a host.
1074 */
1075void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
1076{
Mika Westerberg778e2772011-08-18 15:23:48 +03001077 mmc_host_clk_hold(host);
Philip Rakity4c4cb172011-05-13 11:17:18 +05301078 host->ios.bus_width = width;
1079 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001080 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001081}
1082
Anton Vorontsov86e82862008-11-26 22:54:17 +03001083/**
1084 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
1085 * @vdd: voltage (mV)
1086 * @low_bits: prefer low bits in boundary cases
1087 *
1088 * This function returns the OCR bit number according to the provided @vdd
1089 * value. If conversion is not possible a negative errno value returned.
1090 *
1091 * Depending on the @low_bits flag the function prefers low or high OCR bits
1092 * on boundary voltages. For example,
1093 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
1094 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
1095 *
1096 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
1097 */
1098static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
1099{
1100 const int max_bit = ilog2(MMC_VDD_35_36);
1101 int bit;
1102
1103 if (vdd < 1650 || vdd > 3600)
1104 return -EINVAL;
1105
1106 if (vdd >= 1650 && vdd <= 1950)
1107 return ilog2(MMC_VDD_165_195);
1108
1109 if (low_bits)
1110 vdd -= 1;
1111
1112 /* Base 2000 mV, step 100 mV, bit's base 8. */
1113 bit = (vdd - 2000) / 100 + 8;
1114 if (bit > max_bit)
1115 return max_bit;
1116 return bit;
1117}
1118
1119/**
1120 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
1121 * @vdd_min: minimum voltage value (mV)
1122 * @vdd_max: maximum voltage value (mV)
1123 *
1124 * This function returns the OCR mask bits according to the provided @vdd_min
1125 * and @vdd_max values. If conversion is not possible the function returns 0.
1126 *
1127 * Notes wrt boundary cases:
1128 * This function sets the OCR bits for all boundary voltages, for example
1129 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
1130 * MMC_VDD_34_35 mask.
1131 */
1132u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
1133{
1134 u32 mask = 0;
1135
1136 if (vdd_max < vdd_min)
1137 return 0;
1138
1139 /* Prefer high bits for the boundary vdd_max values. */
1140 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
1141 if (vdd_max < 0)
1142 return 0;
1143
1144 /* Prefer low bits for the boundary vdd_min values. */
1145 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
1146 if (vdd_min < 0)
1147 return 0;
1148
1149 /* Fill the mask, from max bit to min bit. */
1150 while (vdd_max >= vdd_min)
1151 mask |= 1 << vdd_max--;
1152
1153 return mask;
1154}
1155EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
1156
David Brownell5c139412009-03-11 03:30:43 -08001157#ifdef CONFIG_REGULATOR
1158
1159/**
1160 * mmc_regulator_get_ocrmask - return mask of supported voltages
1161 * @supply: regulator to use
1162 *
1163 * This returns either a negative errno, or a mask of voltages that
1164 * can be provided to MMC/SD/SDIO devices using the specified voltage
1165 * regulator. This would normally be called before registering the
1166 * MMC host adapter.
1167 */
1168int mmc_regulator_get_ocrmask(struct regulator *supply)
1169{
1170 int result = 0;
1171 int count;
1172 int i;
1173
1174 count = regulator_count_voltages(supply);
1175 if (count < 0)
1176 return count;
1177
1178 for (i = 0; i < count; i++) {
1179 int vdd_uV;
1180 int vdd_mV;
1181
1182 vdd_uV = regulator_list_voltage(supply, i);
1183 if (vdd_uV <= 0)
1184 continue;
1185
1186 vdd_mV = vdd_uV / 1000;
1187 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1188 }
1189
1190 return result;
1191}
Chris Ball45a6b322012-06-11 09:39:12 -04001192EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
David Brownell5c139412009-03-11 03:30:43 -08001193
1194/**
1195 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -04001196 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -08001197 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -04001198 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -08001199 *
1200 * Returns zero on success, else negative errno.
1201 *
1202 * MMC host drivers may use this to enable or disable a regulator using
1203 * a particular supply voltage. This would normally be called from the
1204 * set_ios() method.
1205 */
Linus Walleij99fc5132010-09-29 01:08:27 -04001206int mmc_regulator_set_ocr(struct mmc_host *mmc,
1207 struct regulator *supply,
1208 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -08001209{
1210 int result = 0;
1211 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -08001212
1213 if (vdd_bit) {
1214 int tmp;
1215 int voltage;
1216
Chris Ball9cde5b72012-09-19 22:27:04 +08001217 /*
1218 * REVISIT mmc_vddrange_to_ocrmask() may have set some
David Brownell5c139412009-03-11 03:30:43 -08001219 * bits this regulator doesn't quite support ... don't
1220 * be too picky, most cards and regulators are OK with
1221 * a 0.1V range goof (it's a small error percentage).
1222 */
1223 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1224 if (tmp == 0) {
1225 min_uV = 1650 * 1000;
1226 max_uV = 1950 * 1000;
1227 } else {
1228 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1229 max_uV = min_uV + 100 * 1000;
1230 }
1231
Chris Ball9cde5b72012-09-19 22:27:04 +08001232 /*
1233 * If we're using a fixed/static regulator, don't call
1234 * regulator_set_voltage; it would fail.
David Brownell5c139412009-03-11 03:30:43 -08001235 */
1236 voltage = regulator_get_voltage(supply);
Jaehoon Chung6e8201f2012-01-16 17:49:01 +09001237
Marek Szyprowski5f56a8e2012-12-04 15:01:02 +01001238 if (!regulator_can_change_voltage(supply))
Jaehoon Chung6e8201f2012-01-16 17:49:01 +09001239 min_uV = max_uV = voltage;
1240
David Brownell5c139412009-03-11 03:30:43 -08001241 if (voltage < 0)
1242 result = voltage;
1243 else if (voltage < min_uV || voltage > max_uV)
1244 result = regulator_set_voltage(supply, min_uV, max_uV);
1245 else
1246 result = 0;
1247
Linus Walleij99fc5132010-09-29 01:08:27 -04001248 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001249 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001250 if (!result)
1251 mmc->regulator_enabled = true;
1252 }
1253 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001254 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001255 if (result == 0)
1256 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -08001257 }
1258
Linus Walleij99fc5132010-09-29 01:08:27 -04001259 if (result)
1260 dev_err(mmc_dev(mmc),
1261 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -08001262 return result;
1263}
Chris Ball45a6b322012-06-11 09:39:12 -04001264EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
David Brownell5c139412009-03-11 03:30:43 -08001265
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04001266int mmc_regulator_get_supply(struct mmc_host *mmc)
1267{
1268 struct device *dev = mmc_dev(mmc);
1269 struct regulator *supply;
1270 int ret;
1271
1272 supply = devm_regulator_get(dev, "vmmc");
1273 mmc->supply.vmmc = supply;
1274 mmc->supply.vqmmc = devm_regulator_get(dev, "vqmmc");
1275
1276 if (IS_ERR(supply))
1277 return PTR_ERR(supply);
1278
1279 ret = mmc_regulator_get_ocrmask(supply);
1280 if (ret > 0)
1281 mmc->ocr_avail = ret;
1282 else
1283 dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret);
1284
1285 return 0;
1286}
1287EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
1288
Linus Walleij99fc5132010-09-29 01:08:27 -04001289#endif /* CONFIG_REGULATOR */
David Brownell5c139412009-03-11 03:30:43 -08001290
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001291/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 * Mask off any voltages we don't support and select
1293 * the lowest voltage
1294 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001295u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296{
1297 int bit;
1298
1299 ocr &= host->ocr_avail;
1300
1301 bit = ffs(ocr);
1302 if (bit) {
1303 bit -= 1;
1304
Timo Teras63ef7312006-11-02 19:43:27 +01001305 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Mika Westerberg778e2772011-08-18 15:23:48 +03001307 mmc_host_clk_hold(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +01001309 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001310 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 } else {
David Brownellf6e10b82008-12-31 09:50:30 -08001312 pr_warning("%s: host doesn't support card's voltages\n",
1313 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 ocr = 0;
1315 }
1316
1317 return ocr;
1318}
1319
Johan Rudholm567c8902013-01-28 15:08:27 +01001320int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
1321{
1322 int err = 0;
1323 int old_signal_voltage = host->ios.signal_voltage;
1324
1325 host->ios.signal_voltage = signal_voltage;
1326 if (host->ops->start_signal_voltage_switch) {
1327 mmc_host_clk_hold(host);
1328 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1329 mmc_host_clk_release(host);
1330 }
1331
1332 if (err)
1333 host->ios.signal_voltage = old_signal_voltage;
1334
1335 return err;
1336
1337}
1338
1339int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage)
Arindam Nathf2119df2011-05-05 12:18:57 +05301340{
1341 struct mmc_command cmd = {0};
1342 int err = 0;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001343 u32 clock;
Arindam Nathf2119df2011-05-05 12:18:57 +05301344
1345 BUG_ON(!host);
1346
1347 /*
1348 * Send CMD11 only if the request is to switch the card to
1349 * 1.8V signalling.
1350 */
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001351 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
1352 return __mmc_set_signal_voltage(host, signal_voltage);
Arindam Nathf2119df2011-05-05 12:18:57 +05301353
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001354 /*
1355 * If we cannot switch voltages, return failure so the caller
1356 * can continue without UHS mode
1357 */
1358 if (!host->ops->start_signal_voltage_switch)
1359 return -EPERM;
1360 if (!host->ops->card_busy)
1361 pr_warning("%s: cannot verify signal voltage switch\n",
1362 mmc_hostname(host));
Arindam Nathf2119df2011-05-05 12:18:57 +05301363
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001364 cmd.opcode = SD_SWITCH_VOLTAGE;
1365 cmd.arg = 0;
1366 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1367
1368 err = mmc_wait_for_cmd(host, &cmd, 0);
1369 if (err)
1370 return err;
1371
1372 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1373 return -EIO;
1374
1375 mmc_host_clk_hold(host);
1376 /*
1377 * The card should drive cmd and dat[0:3] low immediately
1378 * after the response of cmd11, but wait 1 ms to be sure
1379 */
1380 mmc_delay(1);
1381 if (host->ops->card_busy && !host->ops->card_busy(host)) {
1382 err = -EAGAIN;
1383 goto power_cycle;
1384 }
1385 /*
1386 * During a signal voltage level switch, the clock must be gated
1387 * for 5 ms according to the SD spec
1388 */
1389 clock = host->ios.clock;
1390 host->ios.clock = 0;
1391 mmc_set_ios(host);
1392
1393 if (__mmc_set_signal_voltage(host, signal_voltage)) {
1394 /*
1395 * Voltages may not have been switched, but we've already
1396 * sent CMD11, so a power cycle is required anyway
1397 */
1398 err = -EAGAIN;
1399 goto power_cycle;
Arindam Nathf2119df2011-05-05 12:18:57 +05301400 }
1401
Johan Rudholm0797e5f2013-01-28 15:08:28 +01001402 /* Keep clock gated for at least 5 ms */
1403 mmc_delay(5);
1404 host->ios.clock = clock;
1405 mmc_set_ios(host);
1406
1407 /* Wait for at least 1 ms according to spec */
1408 mmc_delay(1);
1409
1410 /*
1411 * Failure to switch is indicated by the card holding
1412 * dat[0:3] low
1413 */
1414 if (host->ops->card_busy && host->ops->card_busy(host))
1415 err = -EAGAIN;
1416
1417power_cycle:
1418 if (err) {
1419 pr_debug("%s: Signal voltage switch failed, "
1420 "power cycling card\n", mmc_hostname(host));
1421 mmc_power_cycle(host);
1422 }
1423
1424 mmc_host_clk_release(host);
1425
1426 return err;
Arindam Nathf2119df2011-05-05 12:18:57 +05301427}
1428
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001430 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001432void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433{
Mika Westerberg778e2772011-08-18 15:23:48 +03001434 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001435 host->ios.timing = timing;
1436 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001437 mmc_host_clk_release(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001438}
1439
1440/*
Arindam Nathd6d50a12011-05-05 12:18:59 +05301441 * Select appropriate driver type for host.
1442 */
1443void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1444{
Mika Westerberg778e2772011-08-18 15:23:48 +03001445 mmc_host_clk_hold(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301446 host->ios.drv_type = drv_type;
1447 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001448 mmc_host_clk_release(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301449}
1450
1451/*
Russell King45f82452005-12-14 14:57:35 +00001452 * Apply power to the MMC stack. This is a two-stage process.
1453 * First, we enable power to the card without the clock running.
1454 * We then wait a bit for the power to stabilise. Finally,
1455 * enable the bus drivers and clock to the card.
1456 *
1457 * We must _NOT_ enable the clock prior to power stablising.
1458 *
1459 * If a host does all the power sequencing itself, ignore the
1460 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 */
1462static void mmc_power_up(struct mmc_host *host)
1463{
Balaji Rao500f3562009-09-22 16:44:18 -07001464 int bit;
1465
Ulf Hanssonfa550182012-05-09 16:15:26 +02001466 if (host->ios.power_mode == MMC_POWER_ON)
1467 return;
1468
Mika Westerberg778e2772011-08-18 15:23:48 +03001469 mmc_host_clk_hold(host);
1470
Balaji Rao500f3562009-09-22 16:44:18 -07001471 /* If ocr is set, we use it */
1472 if (host->ocr)
1473 bit = ffs(host->ocr) - 1;
1474 else
1475 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
1477 host->ios.vdd = bit;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001478 if (mmc_host_is_spi(host))
David Brownellaf517152007-08-08 09:11:32 -07001479 host->ios.chip_select = MMC_CS_HIGH;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001480 else
David Brownellaf517152007-08-08 09:11:32 -07001481 host->ios.chip_select = MMC_CS_DONTCARE;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001482 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001484 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001485 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001486 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Aaron Lu108ecc42012-07-10 16:55:37 +08001488 /* Set signal voltage to 3.3V */
Johan Rudholm567c8902013-01-28 15:08:27 +01001489 __mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330);
Aaron Lu108ecc42012-07-10 16:55:37 +08001490
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001491 /*
1492 * This delay should be sufficient to allow the power supply
1493 * to reach the minimum voltage.
1494 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001495 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001497 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01001500 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001502 /*
1503 * This delay must be at least 74 clock sizes, or 1 ms, or the
1504 * time required to reach a stable voltage.
1505 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001506 mmc_delay(10);
Mika Westerberg778e2772011-08-18 15:23:48 +03001507
1508 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001511void mmc_power_off(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512{
Ulf Hanssonfa550182012-05-09 16:15:26 +02001513 if (host->ios.power_mode == MMC_POWER_OFF)
1514 return;
1515
Mika Westerberg778e2772011-08-18 15:23:48 +03001516 mmc_host_clk_hold(host);
1517
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 host->ios.clock = 0;
1519 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001520
Girish K Sbec87262011-10-13 12:04:16 +05301521
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001522 /*
1523 * Reset ocr mask to be the highest possible voltage supported for
1524 * this mmc host. This value will be used at next power up.
1525 */
1526 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1527
David Brownellaf517152007-08-08 09:11:32 -07001528 if (!mmc_host_is_spi(host)) {
1529 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1530 host->ios.chip_select = MMC_CS_DONTCARE;
1531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001533 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001534 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001535 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001536
Daniel Drake041beb12011-09-07 10:22:09 +01001537 /*
1538 * Some configurations, such as the 802.11 SDIO card in the OLPC
1539 * XO-1.5, require a short delay after poweroff before the card
1540 * can be successfully turned on again.
1541 */
1542 mmc_delay(1);
1543
Mika Westerberg778e2772011-08-18 15:23:48 +03001544 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545}
1546
Johan Rudholm276e0902013-01-28 15:08:25 +01001547void mmc_power_cycle(struct mmc_host *host)
1548{
1549 mmc_power_off(host);
1550 /* Wait at least 1 ms according to SD spec */
1551 mmc_delay(1);
1552 mmc_power_up(host);
1553}
1554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555/*
Adrian Bunk39361852007-07-25 00:40:58 +02001556 * Cleanup when the last reference to the bus operator is dropped.
1557 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001558static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001559{
1560 BUG_ON(!host);
1561 BUG_ON(host->bus_refs);
1562 BUG_ON(!host->bus_dead);
1563
1564 host->bus_ops = NULL;
1565}
1566
1567/*
1568 * Increase reference count of bus operator
1569 */
1570static inline void mmc_bus_get(struct mmc_host *host)
1571{
1572 unsigned long flags;
1573
1574 spin_lock_irqsave(&host->lock, flags);
1575 host->bus_refs++;
1576 spin_unlock_irqrestore(&host->lock, flags);
1577}
1578
1579/*
1580 * Decrease reference count of bus operator and free it if
1581 * it is the last reference.
1582 */
1583static inline void mmc_bus_put(struct mmc_host *host)
1584{
1585 unsigned long flags;
1586
1587 spin_lock_irqsave(&host->lock, flags);
1588 host->bus_refs--;
1589 if ((host->bus_refs == 0) && host->bus_ops)
1590 __mmc_release_bus(host);
1591 spin_unlock_irqrestore(&host->lock, flags);
1592}
1593
1594/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001595 * Assign a mmc bus handler to a host. Only one bus handler may control a
1596 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001598void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001600 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001602 BUG_ON(!host);
1603 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001604
Pierre Ossmand84075c82007-08-09 13:23:56 +02001605 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001606
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001607 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001608
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001609 BUG_ON(host->bus_ops);
1610 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001611
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001612 host->bus_ops = ops;
1613 host->bus_refs = 1;
1614 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001616 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
1619/*
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001620 * Remove the current bus handler from a host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001622void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001624 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001626 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Pierre Ossmand84075c82007-08-09 13:23:56 +02001628 WARN_ON(!host->claimed);
1629 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001631 spin_lock_irqsave(&host->lock, flags);
1632
1633 host->bus_dead = 1;
1634
1635 spin_unlock_irqrestore(&host->lock, flags);
1636
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001637 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640/**
1641 * mmc_detect_change - process change of state on a MMC socket
1642 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001643 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001645 * MMC drivers should call this when they detect a card has been
1646 * inserted or removed. The MMC layer will confirm that any
1647 * present card is still functional, and initialize any newly
1648 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001650void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001652#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001653 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001654 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001655 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001656 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001657#endif
Adrian Hunterd3049502011-11-28 16:22:00 +02001658 host->detect_change = 1;
David Howellsc4028952006-11-22 14:57:56 +00001659 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
1662EXPORT_SYMBOL(mmc_detect_change);
1663
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001664void mmc_init_erase(struct mmc_card *card)
1665{
1666 unsigned int sz;
1667
1668 if (is_power_of_2(card->erase_size))
1669 card->erase_shift = ffs(card->erase_size) - 1;
1670 else
1671 card->erase_shift = 0;
1672
1673 /*
1674 * It is possible to erase an arbitrarily large area of an SD or MMC
1675 * card. That is not desirable because it can take a long time
1676 * (minutes) potentially delaying more important I/O, and also the
1677 * timeout calculations become increasingly hugely over-estimated.
1678 * Consequently, 'pref_erase' is defined as a guide to limit erases
1679 * to that size and alignment.
1680 *
1681 * For SD cards that define Allocation Unit size, limit erases to one
1682 * Allocation Unit at a time. For MMC cards that define High Capacity
1683 * Erase Size, whether it is switched on or not, limit to that size.
1684 * Otherwise just have a stab at a good value. For modern cards it
1685 * will end up being 4MiB. Note that if the value is too small, it
1686 * can end up taking longer to erase.
1687 */
1688 if (mmc_card_sd(card) && card->ssr.au) {
1689 card->pref_erase = card->ssr.au;
1690 card->erase_shift = ffs(card->ssr.au) - 1;
1691 } else if (card->ext_csd.hc_erase_size) {
1692 card->pref_erase = card->ext_csd.hc_erase_size;
1693 } else {
1694 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1695 if (sz < 128)
1696 card->pref_erase = 512 * 1024 / 512;
1697 else if (sz < 512)
1698 card->pref_erase = 1024 * 1024 / 512;
1699 else if (sz < 1024)
1700 card->pref_erase = 2 * 1024 * 1024 / 512;
1701 else
1702 card->pref_erase = 4 * 1024 * 1024 / 512;
1703 if (card->pref_erase < card->erase_size)
1704 card->pref_erase = card->erase_size;
1705 else {
1706 sz = card->pref_erase % card->erase_size;
1707 if (sz)
1708 card->pref_erase += card->erase_size - sz;
1709 }
1710 }
1711}
1712
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001713static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1714 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001715{
1716 unsigned int erase_timeout;
1717
Adrian Hunter7194efb2012-04-05 14:45:47 +03001718 if (arg == MMC_DISCARD_ARG ||
1719 (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) {
1720 erase_timeout = card->ext_csd.trim_timeout;
1721 } else if (card->ext_csd.erase_group_def & 1) {
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001722 /* High Capacity Erase Group Size uses HC timeouts */
1723 if (arg == MMC_TRIM_ARG)
1724 erase_timeout = card->ext_csd.trim_timeout;
1725 else
1726 erase_timeout = card->ext_csd.hc_erase_timeout;
1727 } else {
1728 /* CSD Erase Group Size uses write timeout */
1729 unsigned int mult = (10 << card->csd.r2w_factor);
1730 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1731 unsigned int timeout_us;
1732
1733 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1734 if (card->csd.tacc_ns < 1000000)
1735 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1736 else
1737 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1738
1739 /*
1740 * ios.clock is only a target. The real clock rate might be
1741 * less but not that much less, so fudge it by multiplying by 2.
1742 */
1743 timeout_clks <<= 1;
1744 timeout_us += (timeout_clks * 1000) /
Adrian Hunter4cf8c6d2011-06-23 13:40:27 +03001745 (mmc_host_clk_rate(card->host) / 1000);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001746
1747 erase_timeout = timeout_us / 1000;
1748
1749 /*
1750 * Theoretically, the calculation could underflow so round up
1751 * to 1ms in that case.
1752 */
1753 if (!erase_timeout)
1754 erase_timeout = 1;
1755 }
1756
1757 /* Multiplier for secure operations */
1758 if (arg & MMC_SECURE_ARGS) {
1759 if (arg == MMC_SECURE_ERASE_ARG)
1760 erase_timeout *= card->ext_csd.sec_erase_mult;
1761 else
1762 erase_timeout *= card->ext_csd.sec_trim_mult;
1763 }
1764
1765 erase_timeout *= qty;
1766
1767 /*
1768 * Ensure at least a 1 second timeout for SPI as per
1769 * 'mmc_set_data_timeout()'
1770 */
1771 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1772 erase_timeout = 1000;
1773
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001774 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001775}
1776
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001777static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1778 unsigned int arg,
1779 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001780{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001781 unsigned int erase_timeout;
1782
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001783 if (card->ssr.erase_timeout) {
1784 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001785 erase_timeout = card->ssr.erase_timeout * qty +
1786 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001787 } else {
1788 /*
1789 * Erase timeout not specified in SD Status Register (SSR) so
1790 * use 250ms per write block.
1791 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001792 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001793 }
1794
1795 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001796 if (erase_timeout < 1000)
1797 erase_timeout = 1000;
1798
1799 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001800}
1801
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001802static unsigned int mmc_erase_timeout(struct mmc_card *card,
1803 unsigned int arg,
1804 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001805{
1806 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001807 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001808 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001809 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001810}
1811
1812static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1813 unsigned int to, unsigned int arg)
1814{
Chris Ball1278dba2011-04-13 23:40:30 -04001815 struct mmc_command cmd = {0};
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001816 unsigned int qty = 0;
Trey Ramsay8fee4762012-11-16 09:31:41 -06001817 unsigned long timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001818 int err;
1819
1820 /*
1821 * qty is used to calculate the erase timeout which depends on how many
1822 * erase groups (or allocation units in SD terminology) are affected.
1823 * We count erasing part of an erase group as one erase group.
1824 * For SD, the allocation units are always a power of 2. For MMC, the
1825 * erase group size is almost certainly also power of 2, but it does not
1826 * seem to insist on that in the JEDEC standard, so we fall back to
1827 * division in that case. SD may not specify an allocation unit size,
1828 * in which case the timeout is based on the number of write blocks.
1829 *
1830 * Note that the timeout for secure trim 2 will only be correct if the
1831 * number of erase groups specified is the same as the total of all
1832 * preceding secure trim 1 commands. Since the power may have been
1833 * lost since the secure trim 1 commands occurred, it is generally
1834 * impossible to calculate the secure trim 2 timeout correctly.
1835 */
1836 if (card->erase_shift)
1837 qty += ((to >> card->erase_shift) -
1838 (from >> card->erase_shift)) + 1;
1839 else if (mmc_card_sd(card))
1840 qty += to - from + 1;
1841 else
1842 qty += ((to / card->erase_size) -
1843 (from / card->erase_size)) + 1;
1844
1845 if (!mmc_card_blockaddr(card)) {
1846 from <<= 9;
1847 to <<= 9;
1848 }
1849
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001850 if (mmc_card_sd(card))
1851 cmd.opcode = SD_ERASE_WR_BLK_START;
1852 else
1853 cmd.opcode = MMC_ERASE_GROUP_START;
1854 cmd.arg = from;
1855 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1856 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1857 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301858 pr_err("mmc_erase: group start error %d, "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001859 "status %#x\n", err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03001860 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001861 goto out;
1862 }
1863
1864 memset(&cmd, 0, sizeof(struct mmc_command));
1865 if (mmc_card_sd(card))
1866 cmd.opcode = SD_ERASE_WR_BLK_END;
1867 else
1868 cmd.opcode = MMC_ERASE_GROUP_END;
1869 cmd.arg = to;
1870 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1871 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1872 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301873 pr_err("mmc_erase: group end error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001874 err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03001875 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001876 goto out;
1877 }
1878
1879 memset(&cmd, 0, sizeof(struct mmc_command));
1880 cmd.opcode = MMC_ERASE;
1881 cmd.arg = arg;
1882 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001883 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001884 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1885 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301886 pr_err("mmc_erase: erase error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001887 err, cmd.resp[0]);
1888 err = -EIO;
1889 goto out;
1890 }
1891
1892 if (mmc_host_is_spi(card->host))
1893 goto out;
1894
Trey Ramsay8fee4762012-11-16 09:31:41 -06001895 timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001896 do {
1897 memset(&cmd, 0, sizeof(struct mmc_command));
1898 cmd.opcode = MMC_SEND_STATUS;
1899 cmd.arg = card->rca << 16;
1900 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1901 /* Do not retry else we can't see errors */
1902 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1903 if (err || (cmd.resp[0] & 0xFDF92000)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301904 pr_err("error %d requesting status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001905 err, cmd.resp[0]);
1906 err = -EIO;
1907 goto out;
1908 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06001909
1910 /* Timeout if the device never becomes ready for data and
1911 * never leaves the program state.
1912 */
1913 if (time_after(jiffies, timeout)) {
1914 pr_err("%s: Card stuck in programming state! %s\n",
1915 mmc_hostname(card->host), __func__);
1916 err = -EIO;
1917 goto out;
1918 }
1919
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001920 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
Trey Ramsay8fee4762012-11-16 09:31:41 -06001921 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001922out:
1923 return err;
1924}
1925
1926/**
1927 * mmc_erase - erase sectors.
1928 * @card: card to erase
1929 * @from: first sector to erase
1930 * @nr: number of sectors to erase
1931 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1932 *
1933 * Caller must claim host before calling this function.
1934 */
1935int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1936 unsigned int arg)
1937{
1938 unsigned int rem, to = from + nr;
1939
1940 if (!(card->host->caps & MMC_CAP_ERASE) ||
1941 !(card->csd.cmdclass & CCC_ERASE))
1942 return -EOPNOTSUPP;
1943
1944 if (!card->erase_size)
1945 return -EOPNOTSUPP;
1946
1947 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1948 return -EOPNOTSUPP;
1949
1950 if ((arg & MMC_SECURE_ARGS) &&
1951 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1952 return -EOPNOTSUPP;
1953
1954 if ((arg & MMC_TRIM_ARGS) &&
1955 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1956 return -EOPNOTSUPP;
1957
1958 if (arg == MMC_SECURE_ERASE_ARG) {
1959 if (from % card->erase_size || nr % card->erase_size)
1960 return -EINVAL;
1961 }
1962
1963 if (arg == MMC_ERASE_ARG) {
1964 rem = from % card->erase_size;
1965 if (rem) {
1966 rem = card->erase_size - rem;
1967 from += rem;
1968 if (nr > rem)
1969 nr -= rem;
1970 else
1971 return 0;
1972 }
1973 rem = nr % card->erase_size;
1974 if (rem)
1975 nr -= rem;
1976 }
1977
1978 if (nr == 0)
1979 return 0;
1980
1981 to = from + nr;
1982
1983 if (to <= from)
1984 return -EINVAL;
1985
1986 /* 'from' and 'to' are inclusive */
1987 to -= 1;
1988
1989 return mmc_do_erase(card, from, to, arg);
1990}
1991EXPORT_SYMBOL(mmc_erase);
1992
1993int mmc_can_erase(struct mmc_card *card)
1994{
1995 if ((card->host->caps & MMC_CAP_ERASE) &&
1996 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1997 return 1;
1998 return 0;
1999}
2000EXPORT_SYMBOL(mmc_can_erase);
2001
2002int mmc_can_trim(struct mmc_card *card)
2003{
2004 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
2005 return 1;
2006 return 0;
2007}
2008EXPORT_SYMBOL(mmc_can_trim);
2009
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09002010int mmc_can_discard(struct mmc_card *card)
2011{
2012 /*
2013 * As there's no way to detect the discard support bit at v4.5
2014 * use the s/w feature support filed.
2015 */
2016 if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE)
2017 return 1;
2018 return 0;
2019}
2020EXPORT_SYMBOL(mmc_can_discard);
2021
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09002022int mmc_can_sanitize(struct mmc_card *card)
2023{
Adrian Hunter28302812012-04-05 14:45:48 +03002024 if (!mmc_can_trim(card) && !mmc_can_erase(card))
2025 return 0;
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09002026 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
2027 return 1;
2028 return 0;
2029}
2030EXPORT_SYMBOL(mmc_can_sanitize);
2031
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07002032int mmc_can_secure_erase_trim(struct mmc_card *card)
2033{
2034 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
2035 return 1;
2036 return 0;
2037}
2038EXPORT_SYMBOL(mmc_can_secure_erase_trim);
2039
2040int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
2041 unsigned int nr)
2042{
2043 if (!card->erase_size)
2044 return 0;
2045 if (from % card->erase_size || nr % card->erase_size)
2046 return 0;
2047 return 1;
2048}
2049EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050
Adrian Huntere056a1b2011-06-28 17:16:02 +03002051static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
2052 unsigned int arg)
2053{
2054 struct mmc_host *host = card->host;
2055 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
2056 unsigned int last_timeout = 0;
2057
2058 if (card->erase_shift)
2059 max_qty = UINT_MAX >> card->erase_shift;
2060 else if (mmc_card_sd(card))
2061 max_qty = UINT_MAX;
2062 else
2063 max_qty = UINT_MAX / card->erase_size;
2064
2065 /* Find the largest qty with an OK timeout */
2066 do {
2067 y = 0;
2068 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
2069 timeout = mmc_erase_timeout(card, arg, qty + x);
2070 if (timeout > host->max_discard_to)
2071 break;
2072 if (timeout < last_timeout)
2073 break;
2074 last_timeout = timeout;
2075 y = x;
2076 }
2077 qty += y;
2078 } while (y);
2079
2080 if (!qty)
2081 return 0;
2082
2083 if (qty == 1)
2084 return 1;
2085
2086 /* Convert qty to sectors */
2087 if (card->erase_shift)
2088 max_discard = --qty << card->erase_shift;
2089 else if (mmc_card_sd(card))
2090 max_discard = qty;
2091 else
2092 max_discard = --qty * card->erase_size;
2093
2094 return max_discard;
2095}
2096
2097unsigned int mmc_calc_max_discard(struct mmc_card *card)
2098{
2099 struct mmc_host *host = card->host;
2100 unsigned int max_discard, max_trim;
2101
2102 if (!host->max_discard_to)
2103 return UINT_MAX;
2104
2105 /*
2106 * Without erase_group_def set, MMC erase timeout depends on clock
2107 * frequence which can change. In that case, the best choice is
2108 * just the preferred erase size.
2109 */
2110 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
2111 return card->pref_erase;
2112
2113 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
2114 if (mmc_can_trim(card)) {
2115 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
2116 if (max_trim < max_discard)
2117 max_discard = max_trim;
2118 } else if (max_discard < card->erase_size) {
2119 max_discard = 0;
2120 }
2121 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
2122 mmc_hostname(host), max_discard, host->max_discard_to);
2123 return max_discard;
2124}
2125EXPORT_SYMBOL(mmc_calc_max_discard);
2126
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002127int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
2128{
Chris Ball1278dba2011-04-13 23:40:30 -04002129 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002130
2131 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
2132 return 0;
2133
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03002134 cmd.opcode = MMC_SET_BLOCKLEN;
2135 cmd.arg = blocklen;
2136 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2137 return mmc_wait_for_cmd(card->host, &cmd, 5);
2138}
2139EXPORT_SYMBOL(mmc_set_blocklen);
2140
Loic Pallardy67c79db2012-08-06 17:12:30 +02002141int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount,
2142 bool is_rel_write)
2143{
2144 struct mmc_command cmd = {0};
2145
2146 cmd.opcode = MMC_SET_BLOCK_COUNT;
2147 cmd.arg = blockcount & 0x0000FFFF;
2148 if (is_rel_write)
2149 cmd.arg |= 1 << 31;
2150 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
2151 return mmc_wait_for_cmd(card->host, &cmd, 5);
2152}
2153EXPORT_SYMBOL(mmc_set_blockcount);
2154
Adrian Hunterb2499512011-08-29 16:42:11 +03002155static void mmc_hw_reset_for_init(struct mmc_host *host)
2156{
2157 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2158 return;
2159 mmc_host_clk_hold(host);
2160 host->ops->hw_reset(host);
2161 mmc_host_clk_release(host);
2162}
2163
2164int mmc_can_reset(struct mmc_card *card)
2165{
2166 u8 rst_n_function;
2167
2168 if (!mmc_card_mmc(card))
2169 return 0;
2170 rst_n_function = card->ext_csd.rst_n_function;
2171 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
2172 return 0;
2173 return 1;
2174}
2175EXPORT_SYMBOL(mmc_can_reset);
2176
2177static int mmc_do_hw_reset(struct mmc_host *host, int check)
2178{
2179 struct mmc_card *card = host->card;
2180
2181 if (!host->bus_ops->power_restore)
2182 return -EOPNOTSUPP;
2183
2184 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
2185 return -EOPNOTSUPP;
2186
2187 if (!card)
2188 return -EINVAL;
2189
2190 if (!mmc_can_reset(card))
2191 return -EOPNOTSUPP;
2192
2193 mmc_host_clk_hold(host);
2194 mmc_set_clock(host, host->f_init);
2195
2196 host->ops->hw_reset(host);
2197
2198 /* If the reset has happened, then a status command will fail */
2199 if (check) {
2200 struct mmc_command cmd = {0};
2201 int err;
2202
2203 cmd.opcode = MMC_SEND_STATUS;
2204 if (!mmc_host_is_spi(card->host))
2205 cmd.arg = card->rca << 16;
2206 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
2207 err = mmc_wait_for_cmd(card->host, &cmd, 0);
2208 if (!err) {
2209 mmc_host_clk_release(host);
2210 return -ENOSYS;
2211 }
2212 }
2213
2214 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
2215 if (mmc_host_is_spi(host)) {
2216 host->ios.chip_select = MMC_CS_HIGH;
2217 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
2218 } else {
2219 host->ios.chip_select = MMC_CS_DONTCARE;
2220 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
2221 }
2222 host->ios.bus_width = MMC_BUS_WIDTH_1;
2223 host->ios.timing = MMC_TIMING_LEGACY;
2224 mmc_set_ios(host);
2225
2226 mmc_host_clk_release(host);
2227
2228 return host->bus_ops->power_restore(host);
2229}
2230
2231int mmc_hw_reset(struct mmc_host *host)
2232{
2233 return mmc_do_hw_reset(host, 0);
2234}
2235EXPORT_SYMBOL(mmc_hw_reset);
2236
2237int mmc_hw_reset_check(struct mmc_host *host)
2238{
2239 return mmc_do_hw_reset(host, 1);
2240}
2241EXPORT_SYMBOL(mmc_hw_reset_check);
2242
Andy Ross807e8e42011-01-03 10:36:56 -08002243static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
2244{
2245 host->f_init = freq;
2246
2247#ifdef CONFIG_MMC_DEBUG
2248 pr_info("%s: %s: trying to init card at %u Hz\n",
2249 mmc_hostname(host), __func__, host->f_init);
2250#endif
2251 mmc_power_up(host);
Philip Rakity2f94e552011-02-13 23:12:28 -08002252
2253 /*
Adrian Hunterb2499512011-08-29 16:42:11 +03002254 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
2255 * do a hardware reset if possible.
2256 */
2257 mmc_hw_reset_for_init(host);
2258
2259 /*
Philip Rakity2f94e552011-02-13 23:12:28 -08002260 * sdio_reset sends CMD52 to reset card. Since we do not know
2261 * if the card is being re-initialized, just send it. CMD52
2262 * should be ignored by SD/eMMC cards.
2263 */
Andy Ross807e8e42011-01-03 10:36:56 -08002264 sdio_reset(host);
2265 mmc_go_idle(host);
2266
2267 mmc_send_if_cond(host, host->ocr_avail);
2268
2269 /* Order's important: probe SDIO, then SD, then MMC */
2270 if (!mmc_attach_sdio(host))
2271 return 0;
2272 if (!mmc_attach_sd(host))
2273 return 0;
2274 if (!mmc_attach_mmc(host))
2275 return 0;
2276
2277 mmc_power_off(host);
2278 return -EIO;
2279}
2280
Adrian Hunterd3049502011-11-28 16:22:00 +02002281int _mmc_detect_card_removed(struct mmc_host *host)
2282{
2283 int ret;
2284
2285 if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive)
2286 return 0;
2287
2288 if (!host->card || mmc_card_removed(host->card))
2289 return 1;
2290
2291 ret = host->bus_ops->alive(host);
Kevin Liu14507342013-02-28 15:29:29 +08002292
2293 /*
2294 * Card detect status and alive check may be out of sync if card is
2295 * removed slowly, when card detect switch changes while card/slot
2296 * pads are still contacted in hardware (refer to "SD Card Mechanical
2297 * Addendum, Appendix C: Card Detection Switch"). So reschedule a
2298 * detect work 200ms later for this case.
2299 */
2300 if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) {
2301 mmc_detect_change(host, msecs_to_jiffies(200));
2302 pr_debug("%s: card removed too slowly\n", mmc_hostname(host));
2303 }
2304
Adrian Hunterd3049502011-11-28 16:22:00 +02002305 if (ret) {
2306 mmc_card_set_removed(host->card);
2307 pr_debug("%s: card remove detected\n", mmc_hostname(host));
2308 }
2309
2310 return ret;
2311}
2312
2313int mmc_detect_card_removed(struct mmc_host *host)
2314{
2315 struct mmc_card *card = host->card;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002316 int ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002317
2318 WARN_ON(!host->claimed);
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002319
2320 if (!card)
2321 return 1;
2322
2323 ret = mmc_card_removed(card);
Adrian Hunterd3049502011-11-28 16:22:00 +02002324 /*
2325 * The card will be considered unchanged unless we have been asked to
2326 * detect a change or host requires polling to provide card detection.
2327 */
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002328 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL) &&
2329 !(host->caps2 & MMC_CAP2_DETECT_ON_ERR))
2330 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002331
2332 host->detect_change = 0;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002333 if (!ret) {
2334 ret = _mmc_detect_card_removed(host);
2335 if (ret && (host->caps2 & MMC_CAP2_DETECT_ON_ERR)) {
2336 /*
2337 * Schedule a detect work as soon as possible to let a
2338 * rescan handle the card removal.
2339 */
2340 cancel_delayed_work(&host->detect);
2341 mmc_detect_change(host, 0);
2342 }
2343 }
Adrian Hunterd3049502011-11-28 16:22:00 +02002344
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01002345 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02002346}
2347EXPORT_SYMBOL(mmc_detect_card_removed);
2348
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002349void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350{
David Howellsc4028952006-11-22 14:57:56 +00002351 struct mmc_host *host =
2352 container_of(work, struct mmc_host, detect.work);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002353 int i;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002354
Andy Ross807e8e42011-01-03 10:36:56 -08002355 if (host->rescan_disable)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002356 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Johan Rudholm3339d1e2012-08-23 13:40:55 +02002358 /* If there is a non-removable card registered, only scan once */
2359 if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered)
2360 return;
2361 host->rescan_entered = 1;
2362
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002363 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02002365 /*
2366 * if there is a _removable_ card registered, check whether it is
2367 * still present
2368 */
2369 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
Ohad Ben-Cohenbad3bab2011-03-08 23:32:02 +02002370 && !(host->caps & MMC_CAP_NONREMOVABLE))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002371 host->bus_ops->detect(host);
2372
Adrian Hunterd3049502011-11-28 16:22:00 +02002373 host->detect_change = 0;
2374
Chris Ballc5841792011-01-04 12:20:22 -05002375 /*
2376 * Let mmc_bus_put() free the bus/bus_ops if we've found that
2377 * the card is no longer present.
2378 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002379 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002380 mmc_bus_get(host);
2381
2382 /* if there still is a card present, stop here */
2383 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002384 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002385 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002387
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002388 /*
2389 * Only we can add a new handler, so it's safe to
2390 * release the lock here.
2391 */
2392 mmc_bus_put(host);
2393
Ulf Hanssonfa550182012-05-09 16:15:26 +02002394 if (host->ops->get_cd && host->ops->get_cd(host) == 0) {
2395 mmc_claim_host(host);
2396 mmc_power_off(host);
2397 mmc_release_host(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002398 goto out;
Ulf Hanssonfa550182012-05-09 16:15:26 +02002399 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03002400
Andy Ross807e8e42011-01-03 10:36:56 -08002401 mmc_claim_host(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002402 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
Andy Ross807e8e42011-01-03 10:36:56 -08002403 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
2404 break;
Jaehoon Chung06b22332011-05-12 17:18:59 +09002405 if (freqs[i] <= host->f_min)
Andy Ross807e8e42011-01-03 10:36:56 -08002406 break;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08002407 }
Andy Ross807e8e42011-01-03 10:36:56 -08002408 mmc_release_host(host);
2409
2410 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04002411 if (host->caps & MMC_CAP_NEEDS_POLL)
2412 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413}
2414
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002415void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416{
Ulf Hanssonfa550182012-05-09 16:15:26 +02002417 host->f_init = max(freqs[0], host->f_min);
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02002418 host->rescan_disable = 0;
Ulf Hanssonfa550182012-05-09 16:15:26 +02002419 mmc_power_up(host);
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002420 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421}
2422
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002423void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424{
Pierre Ossman3b91e552007-02-11 20:43:19 +01002425#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002426 unsigned long flags;
2427 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002428 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002429 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002430#endif
2431
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02002432 host->rescan_disable = 1;
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01002433 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002434 mmc_flush_scheduled_work();
2435
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002436 /* clear pm flags now and let card drivers set them as needed */
2437 host->pm_flags = 0;
2438
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002439 mmc_bus_get(host);
2440 if (host->bus_ops && !host->bus_dead) {
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002441 /* Calling bus_ops->remove() with a claimed host can deadlock */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002442 if (host->bus_ops->remove)
2443 host->bus_ops->remove(host);
2444
2445 mmc_claim_host(host);
2446 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002447 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002448 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07002449 mmc_bus_put(host);
2450 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002452 mmc_bus_put(host);
2453
2454 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
2456 mmc_power_off(host);
2457}
2458
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002459int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002460{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002461 int ret = 0;
2462
Daniel Drakebb9cab92011-07-17 16:38:41 +01002463#ifdef CONFIG_MMC_DEBUG
2464 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2465#endif
2466
Adrian Huntereae1aee2009-09-22 16:44:33 -07002467 mmc_bus_get(host);
2468
2469 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2470 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002471 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002472 }
2473
2474 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002475 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002476
2477 mmc_bus_put(host);
2478
2479 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002480
2481 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002482}
2483EXPORT_SYMBOL(mmc_power_save_host);
2484
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002485int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002486{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002487 int ret;
2488
Daniel Drakebb9cab92011-07-17 16:38:41 +01002489#ifdef CONFIG_MMC_DEBUG
2490 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2491#endif
2492
Adrian Huntereae1aee2009-09-22 16:44:33 -07002493 mmc_bus_get(host);
2494
2495 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2496 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002497 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002498 }
2499
2500 mmc_power_up(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002501 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002502
2503 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002504
2505 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002506}
2507EXPORT_SYMBOL(mmc_power_restore_host);
2508
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07002509int mmc_card_awake(struct mmc_host *host)
2510{
2511 int err = -ENOSYS;
2512
Ulf Hanssonaa9df4f2011-12-19 16:24:19 +01002513 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2514 return 0;
2515
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07002516 mmc_bus_get(host);
2517
2518 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2519 err = host->bus_ops->awake(host);
2520
2521 mmc_bus_put(host);
2522
2523 return err;
2524}
2525EXPORT_SYMBOL(mmc_card_awake);
2526
2527int mmc_card_sleep(struct mmc_host *host)
2528{
2529 int err = -ENOSYS;
2530
Ulf Hanssonaa9df4f2011-12-19 16:24:19 +01002531 if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD)
2532 return 0;
2533
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07002534 mmc_bus_get(host);
2535
Kyungmin Parkc99872a2011-11-17 13:34:33 +09002536 if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep)
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07002537 err = host->bus_ops->sleep(host);
2538
2539 mmc_bus_put(host);
2540
2541 return err;
2542}
2543EXPORT_SYMBOL(mmc_card_sleep);
2544
2545int mmc_card_can_sleep(struct mmc_host *host)
2546{
2547 struct mmc_card *card = host->card;
2548
2549 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2550 return 1;
2551 return 0;
2552}
2553EXPORT_SYMBOL(mmc_card_can_sleep);
2554
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002555/*
2556 * Flush the cache to the non-volatile storage.
2557 */
2558int mmc_flush_cache(struct mmc_card *card)
2559{
2560 struct mmc_host *host = card->host;
2561 int err = 0;
2562
2563 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL))
2564 return err;
2565
2566 if (mmc_card_mmc(card) &&
2567 (card->ext_csd.cache_size > 0) &&
2568 (card->ext_csd.cache_ctrl & 1)) {
2569 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
2570 EXT_CSD_FLUSH_CACHE, 1, 0);
2571 if (err)
2572 pr_err("%s: cache flush error %d\n",
2573 mmc_hostname(card->host), err);
2574 }
2575
2576 return err;
2577}
2578EXPORT_SYMBOL(mmc_flush_cache);
2579
2580/*
2581 * Turn the cache ON/OFF.
2582 * Turning the cache OFF shall trigger flushing of the data
2583 * to the non-volatile storage.
Maya Erez881d9262013-01-28 16:44:22 -05002584 * This function should be called with host claimed
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002585 */
2586int mmc_cache_ctrl(struct mmc_host *host, u8 enable)
2587{
2588 struct mmc_card *card = host->card;
Seungwon Jeon8bc06782011-12-09 17:47:17 +09002589 unsigned int timeout;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002590 int err = 0;
2591
2592 if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) ||
2593 mmc_card_is_removable(host))
2594 return err;
2595
2596 if (card && mmc_card_mmc(card) &&
2597 (card->ext_csd.cache_size > 0)) {
2598 enable = !!enable;
2599
Seungwon Jeon8bc06782011-12-09 17:47:17 +09002600 if (card->ext_csd.cache_ctrl ^ enable) {
2601 timeout = enable ? card->ext_csd.generic_cmd6_time : 0;
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002602 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
Seungwon Jeon8bc06782011-12-09 17:47:17 +09002603 EXT_CSD_CACHE_CTRL, enable, timeout);
2604 if (err)
2605 pr_err("%s: cache %s error %d\n",
2606 mmc_hostname(card->host),
2607 enable ? "on" : "off",
2608 err);
2609 else
2610 card->ext_csd.cache_ctrl = enable;
2611 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002612 }
2613
2614 return err;
2615}
2616EXPORT_SYMBOL(mmc_cache_ctrl);
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618#ifdef CONFIG_PM
2619
2620/**
2621 * mmc_suspend_host - suspend a host
2622 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002624int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002626 int err = 0;
2627
Jorg Schummer7de427d2009-02-19 13:17:03 +02002628 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002629 mmc_flush_scheduled_work();
Seungwon Jeon17e9ff52011-12-26 18:03:05 +09002630
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002631 mmc_bus_get(host);
2632 if (host->bus_ops && !host->bus_dead) {
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002633 if (host->bus_ops->suspend) {
2634 if (mmc_card_doing_bkops(host->card)) {
2635 err = mmc_stop_bkops(host->card);
2636 if (err)
2637 goto out;
2638 }
Ulf Hansson7c570912012-04-19 11:55:25 +02002639 err = host->bus_ops->suspend(host);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002640 }
Sujit Reddy Thumma49df7802011-11-23 08:43:18 +05302641
Ulf Hansson7c570912012-04-19 11:55:25 +02002642 if (err == -ENOSYS || !host->bus_ops->resume) {
2643 /*
2644 * We simply "remove" the card in this case.
2645 * It will be redetected on resume. (Calling
2646 * bus_ops->remove() with a claimed host can
2647 * deadlock.)
2648 */
2649 if (host->bus_ops->remove)
2650 host->bus_ops->remove(host);
2651 mmc_claim_host(host);
2652 mmc_detach_bus(host);
2653 mmc_power_off(host);
2654 mmc_release_host(host);
2655 host->pm_flags = 0;
2656 err = 0;
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02002657 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002658 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002659 mmc_bus_put(host);
2660
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002661 if (!err && !mmc_card_keep_power(host))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002662 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Seungwon Jeon881d1c22011-10-14 14:03:21 +09002664out:
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002665 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666}
2667
2668EXPORT_SYMBOL(mmc_suspend_host);
2669
2670/**
2671 * mmc_resume_host - resume a previously suspended host
2672 * @host: mmc host
2673 */
2674int mmc_resume_host(struct mmc_host *host)
2675{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002676 int err = 0;
2677
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002678 mmc_bus_get(host);
2679 if (host->bus_ops && !host->bus_dead) {
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002680 if (!mmc_card_keep_power(host)) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002681 mmc_power_up(host);
2682 mmc_select_voltage(host, host->ocr);
Ohad Ben-Cohene5945732010-11-28 07:21:30 +02002683 /*
2684 * Tell runtime PM core we just powered up the card,
2685 * since it still believes the card is powered off.
2686 * Note that currently runtime PM is only enabled
2687 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2688 */
2689 if (mmc_card_sdio(host->card) &&
2690 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2691 pm_runtime_disable(&host->card->dev);
2692 pm_runtime_set_active(&host->card->dev);
2693 pm_runtime_enable(&host->card->dev);
2694 }
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002695 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002696 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002697 err = host->bus_ops->resume(host);
2698 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302699 pr_warning("%s: error %d during resume "
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002700 "(card was removed?)\n",
2701 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002702 err = 0;
2703 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002704 }
Eliad Pellera8e6df72011-05-09 11:32:31 +03002705 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002706 mmc_bus_put(host);
2707
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002708 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710EXPORT_SYMBOL(mmc_resume_host);
2711
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002712/* Do the card removal on suspend if card is assumed removeable
2713 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2714 to sync the card.
2715*/
2716int mmc_pm_notify(struct notifier_block *notify_block,
2717 unsigned long mode, void *unused)
2718{
2719 struct mmc_host *host = container_of(
2720 notify_block, struct mmc_host, pm_notify);
2721 unsigned long flags;
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002722 int err = 0;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002723
2724 switch (mode) {
2725 case PM_HIBERNATION_PREPARE:
2726 case PM_SUSPEND_PREPARE:
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002727 if (host->card && mmc_card_mmc(host->card) &&
2728 mmc_card_doing_bkops(host->card)) {
2729 err = mmc_stop_bkops(host->card);
2730 if (err) {
2731 pr_err("%s: didn't stop bkops\n",
2732 mmc_hostname(host));
2733 return err;
2734 }
2735 mmc_card_clr_doing_bkops(host->card);
2736 }
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002737
2738 spin_lock_irqsave(&host->lock, flags);
2739 host->rescan_disable = 1;
2740 spin_unlock_irqrestore(&host->lock, flags);
2741 cancel_delayed_work_sync(&host->detect);
2742
2743 if (!host->bus_ops || host->bus_ops->suspend)
2744 break;
2745
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002746 /* Calling bus_ops->remove() with a claimed host can deadlock */
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002747 if (host->bus_ops->remove)
2748 host->bus_ops->remove(host);
2749
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01002750 mmc_claim_host(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002751 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002752 mmc_power_off(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002753 mmc_release_host(host);
2754 host->pm_flags = 0;
2755 break;
2756
2757 case PM_POST_SUSPEND:
2758 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01002759 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002760
2761 spin_lock_irqsave(&host->lock, flags);
2762 host->rescan_disable = 0;
2763 spin_unlock_irqrestore(&host->lock, flags);
2764 mmc_detect_change(host, 0);
2765
2766 }
2767
2768 return 0;
2769}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770#endif
2771
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05002772/**
2773 * mmc_init_context_info() - init synchronization context
2774 * @host: mmc host
2775 *
2776 * Init struct context_info needed to implement asynchronous
2777 * request mechanism, used by mmc core, host driver and mmc requests
2778 * supplier.
2779 */
2780void mmc_init_context_info(struct mmc_host *host)
2781{
2782 spin_lock_init(&host->context_info.lock);
2783 host->context_info.is_new_req = false;
2784 host->context_info.is_done_rcv = false;
2785 host->context_info.is_waiting_last_req = false;
2786 init_waitqueue_head(&host->context_info.wait);
2787}
2788
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002789static int __init mmc_init(void)
2790{
2791 int ret;
2792
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002793 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002794 if (!workqueue)
2795 return -ENOMEM;
2796
2797 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002798 if (ret)
2799 goto destroy_workqueue;
2800
2801 ret = mmc_register_host_class();
2802 if (ret)
2803 goto unregister_bus;
2804
2805 ret = sdio_register_bus();
2806 if (ret)
2807 goto unregister_host_class;
2808
2809 return 0;
2810
2811unregister_host_class:
2812 mmc_unregister_host_class();
2813unregister_bus:
2814 mmc_unregister_bus();
2815destroy_workqueue:
2816 destroy_workqueue(workqueue);
2817
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002818 return ret;
2819}
2820
2821static void __exit mmc_exit(void)
2822{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002823 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002824 mmc_unregister_host_class();
2825 mmc_unregister_bus();
2826 destroy_workqueue(workqueue);
2827}
2828
Nicolas Pitre26074962007-06-16 02:07:53 -04002829subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002830module_exit(mmc_exit);
2831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832MODULE_LICENSE("GPL");