blob: a3c4e0fe94344c360bea4cbb3af08960797812bc [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <linux/mmc/card.h>
31#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010032#include <linux/mmc/mmc.h>
33#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010035#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020036#include "bus.h"
37#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020038#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010039
40#include "mmc_ops.h"
41#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020042#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Pierre Ossmanffce2e72007-05-19 14:32:22 +020044static struct workqueue_struct *workqueue;
45
46/*
David Brownellaf517152007-08-08 09:11:32 -070047 * Enabling software CRCs on the data blocks can be a significant (30%)
48 * performance cost, and for other reasons may not always be desired.
49 * So we allow it it to be disabled.
50 */
51int use_spi_crc = 1;
52module_param(use_spi_crc, bool, 0);
53
54/*
Ben Hutchingsbd68e082009-12-14 18:01:29 -080055 * We normally treat cards as removed during suspend if they are not
56 * known to be on a non-removable bus, to avoid the risk of writing
57 * back data to a different card after resume. Allow this to be
58 * overridden if necessary.
59 */
60#ifdef CONFIG_MMC_UNSAFE_RESUME
61int mmc_assume_removable;
62#else
63int mmc_assume_removable = 1;
64#endif
Matt Fleming71d7d3d2010-09-27 09:42:19 +010065EXPORT_SYMBOL(mmc_assume_removable);
Ben Hutchingsbd68e082009-12-14 18:01:29 -080066module_param_named(removable, mmc_assume_removable, bool, 0644);
67MODULE_PARM_DESC(
68 removable,
69 "MMC/SD cards are removable and may be removed during suspend");
70
71/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020072 * Internal function. Schedule delayed work in the MMC work queue.
73 */
74static int mmc_schedule_delayed_work(struct delayed_work *work,
75 unsigned long delay)
76{
77 return queue_delayed_work(workqueue, work, delay);
78}
79
80/*
81 * Internal function. Flush all scheduled work from the MMC work queue.
82 */
83static void mmc_flush_scheduled_work(void)
84{
85 flush_workqueue(workqueue);
86}
87
Per Forlin1b676f72011-08-19 14:52:37 +020088#ifdef CONFIG_FAIL_MMC_REQUEST
89
90/*
91 * Internal function. Inject random data errors.
92 * If mmc_data is NULL no errors are injected.
93 */
94static void mmc_should_fail_request(struct mmc_host *host,
95 struct mmc_request *mrq)
96{
97 struct mmc_command *cmd = mrq->cmd;
98 struct mmc_data *data = mrq->data;
99 static const int data_errors[] = {
100 -ETIMEDOUT,
101 -EILSEQ,
102 -EIO,
103 };
104
105 if (!data)
106 return;
107
108 if (cmd->error || data->error ||
109 !should_fail(&host->fail_mmc_request, data->blksz * data->blocks))
110 return;
111
112 data->error = data_errors[random32() % ARRAY_SIZE(data_errors)];
113 data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9;
114}
115
116#else /* CONFIG_FAIL_MMC_REQUEST */
117
118static inline void mmc_should_fail_request(struct mmc_host *host,
119 struct mmc_request *mrq)
120{
121}
122
123#endif /* CONFIG_FAIL_MMC_REQUEST */
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125/**
Russell Kingfe10c6a2006-05-04 13:51:45 +0100126 * mmc_request_done - finish processing an MMC request
127 * @host: MMC host which completed request
128 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 *
130 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +0100131 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 */
133void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
134{
135 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +0100136 int err = cmd->error;
137
David Brownellaf517152007-08-08 09:11:32 -0700138 if (err && cmd->retries && mmc_host_is_spi(host)) {
139 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
140 cmd->retries = 0;
141 }
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 if (err && cmd->retries) {
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300144 /*
145 * Request starter must handle retries - see
146 * mmc_wait_for_req_done().
147 */
148 if (mrq->done)
149 mrq->done(mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +0200150 } else {
Per Forlin1b676f72011-08-19 14:52:37 +0200151 mmc_should_fail_request(host, mrq);
152
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200153 led_trigger_event(host->led, LED_OFF);
154
Pierre Ossmane4d21702007-07-24 21:46:49 +0200155 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
156 mmc_hostname(host), cmd->opcode, err,
157 cmd->resp[0], cmd->resp[1],
158 cmd->resp[2], cmd->resp[3]);
159
160 if (mrq->data) {
161 pr_debug("%s: %d bytes transferred: %d\n",
162 mmc_hostname(host),
163 mrq->data->bytes_xfered, mrq->data->error);
164 }
165
166 if (mrq->stop) {
167 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
168 mmc_hostname(host), mrq->stop->opcode,
169 mrq->stop->error,
170 mrq->stop->resp[0], mrq->stop->resp[1],
171 mrq->stop->resp[2], mrq->stop->resp[3]);
172 }
173
174 if (mrq->done)
175 mrq->done(mrq);
Linus Walleij04566832010-11-08 21:36:50 -0500176
Mika Westerberg08c14072011-08-18 15:23:47 +0300177 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179}
180
181EXPORT_SYMBOL(mmc_request_done);
182
Adrian Bunk39361852007-07-25 00:40:58 +0200183static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
185{
Pierre Ossman976d9272007-04-13 22:47:01 +0200186#ifdef CONFIG_MMC_DEBUG
187 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200188 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200189#endif
190
Russell King920e70c2006-05-04 18:22:51 +0100191 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
192 mmc_hostname(host), mrq->cmd->opcode,
193 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Pierre Ossmane4d21702007-07-24 21:46:49 +0200195 if (mrq->data) {
196 pr_debug("%s: blksz %d blocks %d flags %08x "
197 "tsac %d ms nsac %d\n",
198 mmc_hostname(host), mrq->data->blksz,
199 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200200 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200201 mrq->data->timeout_clks);
202 }
203
204 if (mrq->stop) {
205 pr_debug("%s: CMD%u arg %08x flags %08x\n",
206 mmc_hostname(host), mrq->stop->opcode,
207 mrq->stop->arg, mrq->stop->flags);
208 }
209
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100210 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 mrq->cmd->error = 0;
213 mrq->cmd->mrq = mrq;
214 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100215 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100216 BUG_ON(mrq->data->blocks > host->max_blk_count);
217 BUG_ON(mrq->data->blocks * mrq->data->blksz >
218 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100219
Pierre Ossman976d9272007-04-13 22:47:01 +0200220#ifdef CONFIG_MMC_DEBUG
221 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200222 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
223 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200224 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
225#endif
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 mrq->cmd->data = mrq->data;
228 mrq->data->error = 0;
229 mrq->data->mrq = mrq;
230 if (mrq->stop) {
231 mrq->data->stop = mrq->stop;
232 mrq->stop->error = 0;
233 mrq->stop->mrq = mrq;
234 }
235 }
Mika Westerberg08c14072011-08-18 15:23:47 +0300236 mmc_host_clk_hold(host);
Pierre Tardy66c036e2011-02-06 19:02:48 +0100237 led_trigger_event(host->led, LED_FULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 host->ops->request(host, mrq);
239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241static void mmc_wait_done(struct mmc_request *mrq)
242{
Per Forlinaa8b6832011-07-01 18:55:22 +0200243 complete(&mrq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
Per Forlinaa8b6832011-07-01 18:55:22 +0200246static void __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
247{
248 init_completion(&mrq->completion);
249 mrq->done = mmc_wait_done;
250 mmc_start_request(host, mrq);
251}
252
253static void mmc_wait_for_req_done(struct mmc_host *host,
254 struct mmc_request *mrq)
255{
Adrian Hunter08a7e1d2011-10-03 15:33:33 +0300256 struct mmc_command *cmd;
257
258 while (1) {
259 wait_for_completion(&mrq->completion);
260
261 cmd = mrq->cmd;
262 if (!cmd->error || !cmd->retries)
263 break;
264
265 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
266 mmc_hostname(host), cmd->opcode, cmd->error);
267 cmd->retries--;
268 cmd->error = 0;
269 host->ops->request(host, mrq);
270 }
Per Forlinaa8b6832011-07-01 18:55:22 +0200271}
272
273/**
274 * mmc_pre_req - Prepare for a new request
275 * @host: MMC host to prepare command
276 * @mrq: MMC request to prepare for
277 * @is_first_req: true if there is no previous started request
278 * that may run in parellel to this call, otherwise false
279 *
280 * mmc_pre_req() is called in prior to mmc_start_req() to let
281 * host prepare for the new request. Preparation of a request may be
282 * performed while another request is running on the host.
283 */
284static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
285 bool is_first_req)
286{
287 if (host->ops->pre_req)
288 host->ops->pre_req(host, mrq, is_first_req);
289}
290
291/**
292 * mmc_post_req - Post process a completed request
293 * @host: MMC host to post process command
294 * @mrq: MMC request to post process for
295 * @err: Error, if non zero, clean up any resources made in pre_req
296 *
297 * Let the host post process a completed request. Post processing of
298 * a request may be performed while another reuqest is running.
299 */
300static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
301 int err)
302{
303 if (host->ops->post_req)
304 host->ops->post_req(host, mrq, err);
305}
306
307/**
308 * mmc_start_req - start a non-blocking request
309 * @host: MMC host to start command
310 * @areq: async request to start
311 * @error: out parameter returns 0 for success, otherwise non zero
312 *
313 * Start a new MMC custom command request for a host.
314 * If there is on ongoing async request wait for completion
315 * of that request and start the new one and return.
316 * Does not wait for the new request to complete.
317 *
318 * Returns the completed request, NULL in case of none completed.
319 * Wait for the an ongoing request (previoulsy started) to complete and
320 * return the completed request. If there is no ongoing request, NULL
321 * is returned without waiting. NULL is not an error condition.
322 */
323struct mmc_async_req *mmc_start_req(struct mmc_host *host,
324 struct mmc_async_req *areq, int *error)
325{
326 int err = 0;
327 struct mmc_async_req *data = host->areq;
328
329 /* Prepare a new request */
330 if (areq)
331 mmc_pre_req(host, areq->mrq, !host->areq);
332
333 if (host->areq) {
334 mmc_wait_for_req_done(host, host->areq->mrq);
335 err = host->areq->err_check(host->card, host->areq);
336 if (err) {
Per Forlin7c8a2822011-08-29 15:35:58 +0200337 /* post process the completed failed request */
Per Forlinaa8b6832011-07-01 18:55:22 +0200338 mmc_post_req(host, host->areq->mrq, 0);
339 if (areq)
Per Forlin7c8a2822011-08-29 15:35:58 +0200340 /*
341 * Cancel the new prepared request, because
342 * it can't run until the failed
343 * request has been properly handled.
344 */
Per Forlinaa8b6832011-07-01 18:55:22 +0200345 mmc_post_req(host, areq->mrq, -EINVAL);
346
347 host->areq = NULL;
348 goto out;
349 }
350 }
351
352 if (areq)
353 __mmc_start_req(host, areq->mrq);
354
355 if (host->areq)
356 mmc_post_req(host, host->areq->mrq, 0);
357
358 host->areq = areq;
359 out:
360 if (error)
361 *error = err;
362 return data;
363}
364EXPORT_SYMBOL(mmc_start_req);
365
Pierre Ossman67a61c42007-07-11 20:22:11 +0200366/**
367 * mmc_wait_for_req - start a request and wait for completion
368 * @host: MMC host to start command
369 * @mrq: MMC request to start
370 *
371 * Start a new MMC custom command request for a host, and wait
372 * for the command to complete. Does not attempt to parse the
373 * response.
374 */
375void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Per Forlinaa8b6832011-07-01 18:55:22 +0200377 __mmc_start_req(host, mrq);
378 mmc_wait_for_req_done(host, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380EXPORT_SYMBOL(mmc_wait_for_req);
381
382/**
383 * mmc_wait_for_cmd - start a command and wait for completion
384 * @host: MMC host to start command
385 * @cmd: MMC command to start
386 * @retries: maximum number of retries
387 *
388 * Start a new MMC command for a host, and wait for the command
389 * to complete. Return any error that occurred while the command
390 * was executing. Do not attempt to parse the response.
391 */
392int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
393{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530394 struct mmc_request mrq = {NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Pierre Ossmand84075c82007-08-09 13:23:56 +0200396 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 memset(cmd->resp, 0, sizeof(cmd->resp));
399 cmd->retries = retries;
400
401 mrq.cmd = cmd;
402 cmd->data = NULL;
403
404 mmc_wait_for_req(host, &mrq);
405
406 return cmd->error;
407}
408
409EXPORT_SYMBOL(mmc_wait_for_cmd);
410
Pierre Ossman335eadf2005-09-06 15:18:50 -0700411/**
Russell Kingd773d722006-09-07 15:57:12 +0100412 * mmc_set_data_timeout - set the timeout for a data command
413 * @data: data phase for command
414 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200415 *
416 * Computes the data timeout parameters according to the
417 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100418 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200419void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100420{
421 unsigned int mult;
422
423 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200424 * SDIO cards only define an upper 1 s limit on access.
425 */
426 if (mmc_card_sdio(card)) {
427 data->timeout_ns = 1000000000;
428 data->timeout_clks = 0;
429 return;
430 }
431
432 /*
Russell Kingd773d722006-09-07 15:57:12 +0100433 * SD cards use a 100 multiplier rather than 10
434 */
435 mult = mmc_card_sd(card) ? 100 : 10;
436
437 /*
438 * Scale up the multiplier (and therefore the timeout) by
439 * the r2w factor for writes.
440 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200441 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100442 mult <<= card->csd.r2w_factor;
443
444 data->timeout_ns = card->csd.tacc_ns * mult;
445 data->timeout_clks = card->csd.tacc_clks * mult;
446
447 /*
448 * SD cards also have an upper limit on the timeout.
449 */
450 if (mmc_card_sd(card)) {
451 unsigned int timeout_us, limit_us;
452
453 timeout_us = data->timeout_ns / 1000;
Linus Walleije9b86842011-01-05 00:44:32 +0100454 if (mmc_host_clk_rate(card->host))
455 timeout_us += data->timeout_clks * 1000 /
456 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100457
Pierre Ossmanb146d262007-07-24 19:16:54 +0200458 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100459 /*
460 * The limit is really 250 ms, but that is
461 * insufficient for some crappy cards.
462 */
463 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100464 else
465 limit_us = 100000;
466
Philip Langdalefba68bd2007-01-04 06:57:32 -0800467 /*
468 * SDHC cards always use these fixed values.
469 */
470 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100471 data->timeout_ns = limit_us * 1000;
472 data->timeout_clks = 0;
473 }
474 }
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100475 /*
476 * Some cards need very high timeouts if driven in SPI mode.
477 * The worst observed timeout was 900ms after writing a
478 * continuous stream of data until the internal logic
479 * overflowed.
480 */
481 if (mmc_host_is_spi(card->host)) {
482 if (data->flags & MMC_DATA_WRITE) {
483 if (data->timeout_ns < 1000000000)
484 data->timeout_ns = 1000000000; /* 1s */
485 } else {
486 if (data->timeout_ns < 100000000)
487 data->timeout_ns = 100000000; /* 100ms */
488 }
489 }
Russell Kingd773d722006-09-07 15:57:12 +0100490}
491EXPORT_SYMBOL(mmc_set_data_timeout);
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200494 * mmc_align_data_size - pads a transfer size to a more optimal value
495 * @card: the MMC card associated with the data transfer
496 * @sz: original transfer size
497 *
498 * Pads the original data size with a number of extra bytes in
499 * order to avoid controller bugs and/or performance hits
500 * (e.g. some controllers revert to PIO for certain sizes).
501 *
502 * Returns the improved size, which might be unmodified.
503 *
504 * Note that this function is only relevant when issuing a
505 * single scatter gather entry.
506 */
507unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
508{
509 /*
510 * FIXME: We don't have a system for the controller to tell
511 * the core about its problems yet, so for now we just 32-bit
512 * align the size.
513 */
514 sz = ((sz + 3) / 4) * 4;
515
516 return sz;
517}
518EXPORT_SYMBOL(mmc_align_data_size);
519
520/**
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700521 * mmc_host_enable - enable a host.
522 * @host: mmc host to enable
523 *
524 * Hosts that support power saving can use the 'enable' and 'disable'
525 * methods to exit and enter power saving states. For more information
526 * see comments for struct mmc_host_ops.
527 */
528int mmc_host_enable(struct mmc_host *host)
529{
530 if (!(host->caps & MMC_CAP_DISABLE))
531 return 0;
532
533 if (host->en_dis_recurs)
534 return 0;
535
536 if (host->nesting_cnt++)
537 return 0;
538
539 cancel_delayed_work_sync(&host->disable);
540
541 if (host->enabled)
542 return 0;
543
544 if (host->ops->enable) {
545 int err;
546
547 host->en_dis_recurs = 1;
548 err = host->ops->enable(host);
549 host->en_dis_recurs = 0;
550
551 if (err) {
552 pr_debug("%s: enable error %d\n",
553 mmc_hostname(host), err);
554 return err;
555 }
556 }
557 host->enabled = 1;
558 return 0;
559}
560EXPORT_SYMBOL(mmc_host_enable);
561
562static int mmc_host_do_disable(struct mmc_host *host, int lazy)
563{
564 if (host->ops->disable) {
565 int err;
566
567 host->en_dis_recurs = 1;
568 err = host->ops->disable(host, lazy);
569 host->en_dis_recurs = 0;
570
571 if (err < 0) {
572 pr_debug("%s: disable error %d\n",
573 mmc_hostname(host), err);
574 return err;
575 }
576 if (err > 0) {
577 unsigned long delay = msecs_to_jiffies(err);
578
579 mmc_schedule_delayed_work(&host->disable, delay);
580 }
581 }
582 host->enabled = 0;
583 return 0;
584}
585
586/**
587 * mmc_host_disable - disable a host.
588 * @host: mmc host to disable
589 *
590 * Hosts that support power saving can use the 'enable' and 'disable'
591 * methods to exit and enter power saving states. For more information
592 * see comments for struct mmc_host_ops.
593 */
594int mmc_host_disable(struct mmc_host *host)
595{
596 int err;
597
598 if (!(host->caps & MMC_CAP_DISABLE))
599 return 0;
600
601 if (host->en_dis_recurs)
602 return 0;
603
604 if (--host->nesting_cnt)
605 return 0;
606
607 if (!host->enabled)
608 return 0;
609
610 err = mmc_host_do_disable(host, 0);
611 return err;
612}
613EXPORT_SYMBOL(mmc_host_disable);
614
615/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200616 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200618 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200620 * Claim a host for a set of operations. If @abort is non null and
621 * dereference a non-zero value then this will return prematurely with
622 * that non-zero value without acquiring the lock. Returns zero
623 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200625int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
627 DECLARE_WAITQUEUE(wait, current);
628 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200629 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200631 might_sleep();
632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 add_wait_queue(&host->wq, &wait);
634 spin_lock_irqsave(&host->lock, flags);
635 while (1) {
636 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200637 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700638 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640 spin_unlock_irqrestore(&host->lock, flags);
641 schedule();
642 spin_lock_irqsave(&host->lock, flags);
643 }
644 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700645 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200646 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700647 host->claimer = current;
648 host->claim_cnt += 1;
649 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200650 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 spin_unlock_irqrestore(&host->lock, flags);
652 remove_wait_queue(&host->wq, &wait);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700653 if (!stop)
654 mmc_host_enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200655 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656}
657
Nicolas Pitre2342f332007-06-30 16:21:52 +0200658EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
Adrian Hunter319a3f12009-09-22 16:44:30 -0700660/**
661 * mmc_try_claim_host - try exclusively to claim a host
662 * @host: mmc host to claim
663 *
664 * Returns %1 if the host is claimed, %0 otherwise.
665 */
666int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700667{
668 int claimed_host = 0;
669 unsigned long flags;
670
671 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700672 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700673 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700674 host->claimer = current;
675 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700676 claimed_host = 1;
677 }
678 spin_unlock_irqrestore(&host->lock, flags);
679 return claimed_host;
680}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700681EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700682
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100683/**
684 * mmc_do_release_host - release a claimed host
685 * @host: mmc host to release
686 *
687 * If you successfully claimed a host, this function will
688 * release it again.
689 */
690void mmc_do_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700691{
692 unsigned long flags;
693
694 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700695 if (--host->claim_cnt) {
696 /* Release for nested claim */
697 spin_unlock_irqrestore(&host->lock, flags);
698 } else {
699 host->claimed = 0;
700 host->claimer = NULL;
701 spin_unlock_irqrestore(&host->lock, flags);
702 wake_up(&host->wq);
703 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700704}
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100705EXPORT_SYMBOL(mmc_do_release_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700706
707void mmc_host_deeper_disable(struct work_struct *work)
708{
709 struct mmc_host *host =
710 container_of(work, struct mmc_host, disable.work);
711
712 /* If the host is claimed then we do not want to disable it anymore */
713 if (!mmc_try_claim_host(host))
714 return;
715 mmc_host_do_disable(host, 1);
716 mmc_do_release_host(host);
717}
718
719/**
720 * mmc_host_lazy_disable - lazily disable a host.
721 * @host: mmc host to disable
722 *
723 * Hosts that support power saving can use the 'enable' and 'disable'
724 * methods to exit and enter power saving states. For more information
725 * see comments for struct mmc_host_ops.
726 */
727int mmc_host_lazy_disable(struct mmc_host *host)
728{
729 if (!(host->caps & MMC_CAP_DISABLE))
730 return 0;
731
732 if (host->en_dis_recurs)
733 return 0;
734
735 if (--host->nesting_cnt)
736 return 0;
737
738 if (!host->enabled)
739 return 0;
740
741 if (host->disable_delay) {
742 mmc_schedule_delayed_work(&host->disable,
743 msecs_to_jiffies(host->disable_delay));
744 return 0;
745 } else
746 return mmc_host_do_disable(host, 1);
747}
748EXPORT_SYMBOL(mmc_host_lazy_disable);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750/**
751 * mmc_release_host - release a host
752 * @host: mmc host to release
753 *
754 * Release a MMC host, allowing others to claim the host
755 * for their operations.
756 */
757void mmc_release_host(struct mmc_host *host)
758{
Pierre Ossmand84075c82007-08-09 13:23:56 +0200759 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700761 mmc_host_lazy_disable(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700763 mmc_do_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764}
765
766EXPORT_SYMBOL(mmc_release_host);
767
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100768/*
769 * Internal function that does the actual ios call to the host driver,
770 * optionally printing some debug output.
771 */
Russell King920e70c2006-05-04 18:22:51 +0100772static inline void mmc_set_ios(struct mmc_host *host)
773{
774 struct mmc_ios *ios = &host->ios;
775
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100776 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
777 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100778 mmc_hostname(host), ios->clock, ios->bus_mode,
779 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100780 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800781
Linus Walleij04566832010-11-08 21:36:50 -0500782 if (ios->clock > 0)
783 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +0100784 host->ops->set_ios(host, ios);
785}
786
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100787/*
788 * Control chip select pin on a host.
789 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100790void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700791{
Mika Westerberg778e2772011-08-18 15:23:48 +0300792 mmc_host_clk_hold(host);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100793 host->ios.chip_select = mode;
794 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300795 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
798/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100799 * Sets the host clock to the highest possible frequency that
800 * is below "hz".
801 */
Mika Westerberg778e2772011-08-18 15:23:48 +0300802static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100803{
804 WARN_ON(hz < host->f_min);
805
806 if (hz > host->f_max)
807 hz = host->f_max;
808
809 host->ios.clock = hz;
810 mmc_set_ios(host);
811}
812
Mika Westerberg778e2772011-08-18 15:23:48 +0300813void mmc_set_clock(struct mmc_host *host, unsigned int hz)
814{
815 mmc_host_clk_hold(host);
816 __mmc_set_clock(host, hz);
817 mmc_host_clk_release(host);
818}
819
Linus Walleij04566832010-11-08 21:36:50 -0500820#ifdef CONFIG_MMC_CLKGATE
821/*
822 * This gates the clock by setting it to 0 Hz.
823 */
824void mmc_gate_clock(struct mmc_host *host)
825{
826 unsigned long flags;
827
828 spin_lock_irqsave(&host->clk_lock, flags);
829 host->clk_old = host->ios.clock;
830 host->ios.clock = 0;
831 host->clk_gated = true;
832 spin_unlock_irqrestore(&host->clk_lock, flags);
833 mmc_set_ios(host);
834}
835
836/*
837 * This restores the clock from gating by using the cached
838 * clock value.
839 */
840void mmc_ungate_clock(struct mmc_host *host)
841{
842 /*
843 * We should previously have gated the clock, so the clock shall
844 * be 0 here! The clock may however be 0 during initialization,
845 * when some request operations are performed before setting
846 * the frequency. When ungate is requested in that situation
847 * we just ignore the call.
848 */
849 if (host->clk_old) {
850 BUG_ON(host->ios.clock);
851 /* This call will also set host->clk_gated to false */
Mika Westerberg778e2772011-08-18 15:23:48 +0300852 __mmc_set_clock(host, host->clk_old);
Linus Walleij04566832010-11-08 21:36:50 -0500853 }
854}
855
856void mmc_set_ungated(struct mmc_host *host)
857{
858 unsigned long flags;
859
860 /*
861 * We've been given a new frequency while the clock is gated,
862 * so make sure we regard this as ungating it.
863 */
864 spin_lock_irqsave(&host->clk_lock, flags);
865 host->clk_gated = false;
866 spin_unlock_irqrestore(&host->clk_lock, flags);
867}
868
869#else
870void mmc_set_ungated(struct mmc_host *host)
871{
872}
873#endif
874
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100875/*
876 * Change the bus mode (open drain/push-pull) of a host.
877 */
878void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
879{
Mika Westerberg778e2772011-08-18 15:23:48 +0300880 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100881 host->ios.bus_mode = mode;
882 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300883 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100884}
885
886/*
887 * Change data bus width of a host.
888 */
889void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
890{
Mika Westerberg778e2772011-08-18 15:23:48 +0300891 mmc_host_clk_hold(host);
Philip Rakity4c4cb172011-05-13 11:17:18 +0530892 host->ios.bus_width = width;
893 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300894 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100895}
896
Anton Vorontsov86e82862008-11-26 22:54:17 +0300897/**
898 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
899 * @vdd: voltage (mV)
900 * @low_bits: prefer low bits in boundary cases
901 *
902 * This function returns the OCR bit number according to the provided @vdd
903 * value. If conversion is not possible a negative errno value returned.
904 *
905 * Depending on the @low_bits flag the function prefers low or high OCR bits
906 * on boundary voltages. For example,
907 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
908 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
909 *
910 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
911 */
912static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
913{
914 const int max_bit = ilog2(MMC_VDD_35_36);
915 int bit;
916
917 if (vdd < 1650 || vdd > 3600)
918 return -EINVAL;
919
920 if (vdd >= 1650 && vdd <= 1950)
921 return ilog2(MMC_VDD_165_195);
922
923 if (low_bits)
924 vdd -= 1;
925
926 /* Base 2000 mV, step 100 mV, bit's base 8. */
927 bit = (vdd - 2000) / 100 + 8;
928 if (bit > max_bit)
929 return max_bit;
930 return bit;
931}
932
933/**
934 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
935 * @vdd_min: minimum voltage value (mV)
936 * @vdd_max: maximum voltage value (mV)
937 *
938 * This function returns the OCR mask bits according to the provided @vdd_min
939 * and @vdd_max values. If conversion is not possible the function returns 0.
940 *
941 * Notes wrt boundary cases:
942 * This function sets the OCR bits for all boundary voltages, for example
943 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
944 * MMC_VDD_34_35 mask.
945 */
946u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
947{
948 u32 mask = 0;
949
950 if (vdd_max < vdd_min)
951 return 0;
952
953 /* Prefer high bits for the boundary vdd_max values. */
954 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
955 if (vdd_max < 0)
956 return 0;
957
958 /* Prefer low bits for the boundary vdd_min values. */
959 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
960 if (vdd_min < 0)
961 return 0;
962
963 /* Fill the mask, from max bit to min bit. */
964 while (vdd_max >= vdd_min)
965 mask |= 1 << vdd_max--;
966
967 return mask;
968}
969EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
970
David Brownell5c139412009-03-11 03:30:43 -0800971#ifdef CONFIG_REGULATOR
972
973/**
974 * mmc_regulator_get_ocrmask - return mask of supported voltages
975 * @supply: regulator to use
976 *
977 * This returns either a negative errno, or a mask of voltages that
978 * can be provided to MMC/SD/SDIO devices using the specified voltage
979 * regulator. This would normally be called before registering the
980 * MMC host adapter.
981 */
982int mmc_regulator_get_ocrmask(struct regulator *supply)
983{
984 int result = 0;
985 int count;
986 int i;
987
988 count = regulator_count_voltages(supply);
989 if (count < 0)
990 return count;
991
992 for (i = 0; i < count; i++) {
993 int vdd_uV;
994 int vdd_mV;
995
996 vdd_uV = regulator_list_voltage(supply, i);
997 if (vdd_uV <= 0)
998 continue;
999
1000 vdd_mV = vdd_uV / 1000;
1001 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
1002 }
1003
1004 return result;
1005}
1006EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
1007
1008/**
1009 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -04001010 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -08001011 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -04001012 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -08001013 *
1014 * Returns zero on success, else negative errno.
1015 *
1016 * MMC host drivers may use this to enable or disable a regulator using
1017 * a particular supply voltage. This would normally be called from the
1018 * set_ios() method.
1019 */
Linus Walleij99fc5132010-09-29 01:08:27 -04001020int mmc_regulator_set_ocr(struct mmc_host *mmc,
1021 struct regulator *supply,
1022 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -08001023{
1024 int result = 0;
1025 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -08001026
1027 if (vdd_bit) {
1028 int tmp;
1029 int voltage;
1030
1031 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1032 * bits this regulator doesn't quite support ... don't
1033 * be too picky, most cards and regulators are OK with
1034 * a 0.1V range goof (it's a small error percentage).
1035 */
1036 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1037 if (tmp == 0) {
1038 min_uV = 1650 * 1000;
1039 max_uV = 1950 * 1000;
1040 } else {
1041 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1042 max_uV = min_uV + 100 * 1000;
1043 }
1044
1045 /* avoid needless changes to this voltage; the regulator
1046 * might not allow this operation
1047 */
1048 voltage = regulator_get_voltage(supply);
1049 if (voltage < 0)
1050 result = voltage;
1051 else if (voltage < min_uV || voltage > max_uV)
1052 result = regulator_set_voltage(supply, min_uV, max_uV);
1053 else
1054 result = 0;
1055
Linus Walleij99fc5132010-09-29 01:08:27 -04001056 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001057 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001058 if (!result)
1059 mmc->regulator_enabled = true;
1060 }
1061 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001062 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001063 if (result == 0)
1064 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -08001065 }
1066
Linus Walleij99fc5132010-09-29 01:08:27 -04001067 if (result)
1068 dev_err(mmc_dev(mmc),
1069 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -08001070 return result;
1071}
1072EXPORT_SYMBOL(mmc_regulator_set_ocr);
1073
Linus Walleij99fc5132010-09-29 01:08:27 -04001074#endif /* CONFIG_REGULATOR */
David Brownell5c139412009-03-11 03:30:43 -08001075
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001076/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 * Mask off any voltages we don't support and select
1078 * the lowest voltage
1079 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001080u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082 int bit;
1083
1084 ocr &= host->ocr_avail;
1085
1086 bit = ffs(ocr);
1087 if (bit) {
1088 bit -= 1;
1089
Timo Teras63ef7312006-11-02 19:43:27 +01001090 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Mika Westerberg778e2772011-08-18 15:23:48 +03001092 mmc_host_clk_hold(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +01001094 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001095 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
David Brownellf6e10b82008-12-31 09:50:30 -08001097 pr_warning("%s: host doesn't support card's voltages\n",
1098 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 ocr = 0;
1100 }
1101
1102 return ocr;
1103}
1104
Philip Rakity261bbd42011-05-13 11:17:17 +05301105int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
Arindam Nathf2119df2011-05-05 12:18:57 +05301106{
1107 struct mmc_command cmd = {0};
1108 int err = 0;
1109
1110 BUG_ON(!host);
1111
1112 /*
1113 * Send CMD11 only if the request is to switch the card to
1114 * 1.8V signalling.
1115 */
Philip Rakity261bbd42011-05-13 11:17:17 +05301116 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
Arindam Nathf2119df2011-05-05 12:18:57 +05301117 cmd.opcode = SD_SWITCH_VOLTAGE;
1118 cmd.arg = 0;
1119 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1120
1121 err = mmc_wait_for_cmd(host, &cmd, 0);
1122 if (err)
1123 return err;
1124
1125 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1126 return -EIO;
1127 }
1128
1129 host->ios.signal_voltage = signal_voltage;
1130
1131 if (host->ops->start_signal_voltage_switch)
1132 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1133
1134 return err;
1135}
1136
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001138 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001140void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
Mika Westerberg778e2772011-08-18 15:23:48 +03001142 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001143 host->ios.timing = timing;
1144 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001145 mmc_host_clk_release(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001146}
1147
1148/*
Arindam Nathd6d50a12011-05-05 12:18:59 +05301149 * Select appropriate driver type for host.
1150 */
1151void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1152{
Mika Westerberg778e2772011-08-18 15:23:48 +03001153 mmc_host_clk_hold(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301154 host->ios.drv_type = drv_type;
1155 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001156 mmc_host_clk_release(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301157}
1158
1159/*
Russell King45f82452005-12-14 14:57:35 +00001160 * Apply power to the MMC stack. This is a two-stage process.
1161 * First, we enable power to the card without the clock running.
1162 * We then wait a bit for the power to stabilise. Finally,
1163 * enable the bus drivers and clock to the card.
1164 *
1165 * We must _NOT_ enable the clock prior to power stablising.
1166 *
1167 * If a host does all the power sequencing itself, ignore the
1168 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 */
1170static void mmc_power_up(struct mmc_host *host)
1171{
Balaji Rao500f3562009-09-22 16:44:18 -07001172 int bit;
1173
Mika Westerberg778e2772011-08-18 15:23:48 +03001174 mmc_host_clk_hold(host);
1175
Balaji Rao500f3562009-09-22 16:44:18 -07001176 /* If ocr is set, we use it */
1177 if (host->ocr)
1178 bit = ffs(host->ocr) - 1;
1179 else
1180 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 host->ios.vdd = bit;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001183 if (mmc_host_is_spi(host))
David Brownellaf517152007-08-08 09:11:32 -07001184 host->ios.chip_select = MMC_CS_HIGH;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001185 else
David Brownellaf517152007-08-08 09:11:32 -07001186 host->ios.chip_select = MMC_CS_DONTCARE;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001187 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001189 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001190 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001191 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001193 /*
1194 * This delay should be sufficient to allow the power supply
1195 * to reach the minimum voltage.
1196 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001197 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001199 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01001202 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001204 /*
1205 * This delay must be at least 74 clock sizes, or 1 ms, or the
1206 * time required to reach a stable voltage.
1207 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001208 mmc_delay(10);
Mika Westerberg778e2772011-08-18 15:23:48 +03001209
1210 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001213void mmc_power_off(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214{
Girish K Sbec87262011-10-13 12:04:16 +05301215 struct mmc_card *card;
1216 unsigned int notify_type;
1217 unsigned int timeout;
1218 int err;
1219
Mika Westerberg778e2772011-08-18 15:23:48 +03001220 mmc_host_clk_hold(host);
1221
Girish K Sbec87262011-10-13 12:04:16 +05301222 card = host->card;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 host->ios.clock = 0;
1224 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001225
Girish K Sbec87262011-10-13 12:04:16 +05301226 if (card && mmc_card_mmc(card) &&
1227 (card->poweroff_notify_state == MMC_POWERED_ON)) {
1228
1229 if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) {
1230 notify_type = EXT_CSD_POWER_OFF_SHORT;
1231 timeout = card->ext_csd.generic_cmd6_time;
1232 card->poweroff_notify_state = MMC_POWEROFF_SHORT;
1233 } else {
1234 notify_type = EXT_CSD_POWER_OFF_LONG;
1235 timeout = card->ext_csd.power_off_longtime;
1236 card->poweroff_notify_state = MMC_POWEROFF_LONG;
1237 }
1238
1239 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1240 EXT_CSD_POWER_OFF_NOTIFICATION,
1241 notify_type, timeout);
1242
1243 if (err && err != -EBADMSG)
1244 pr_err("Device failed to respond within %d poweroff "
1245 "time. Forcefully powering down the device\n",
1246 timeout);
1247
1248 /* Set the card state to no notification after the poweroff */
1249 card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION;
1250 }
1251
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001252 /*
1253 * Reset ocr mask to be the highest possible voltage supported for
1254 * this mmc host. This value will be used at next power up.
1255 */
1256 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1257
David Brownellaf517152007-08-08 09:11:32 -07001258 if (!mmc_host_is_spi(host)) {
1259 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1260 host->ios.chip_select = MMC_CS_DONTCARE;
1261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001263 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001264 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001265 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001266
Daniel Drake041beb12011-09-07 10:22:09 +01001267 /*
1268 * Some configurations, such as the 802.11 SDIO card in the OLPC
1269 * XO-1.5, require a short delay after poweroff before the card
1270 * can be successfully turned on again.
1271 */
1272 mmc_delay(1);
1273
Mika Westerberg778e2772011-08-18 15:23:48 +03001274 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275}
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277/*
Adrian Bunk39361852007-07-25 00:40:58 +02001278 * Cleanup when the last reference to the bus operator is dropped.
1279 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001280static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001281{
1282 BUG_ON(!host);
1283 BUG_ON(host->bus_refs);
1284 BUG_ON(!host->bus_dead);
1285
1286 host->bus_ops = NULL;
1287}
1288
1289/*
1290 * Increase reference count of bus operator
1291 */
1292static inline void mmc_bus_get(struct mmc_host *host)
1293{
1294 unsigned long flags;
1295
1296 spin_lock_irqsave(&host->lock, flags);
1297 host->bus_refs++;
1298 spin_unlock_irqrestore(&host->lock, flags);
1299}
1300
1301/*
1302 * Decrease reference count of bus operator and free it if
1303 * it is the last reference.
1304 */
1305static inline void mmc_bus_put(struct mmc_host *host)
1306{
1307 unsigned long flags;
1308
1309 spin_lock_irqsave(&host->lock, flags);
1310 host->bus_refs--;
1311 if ((host->bus_refs == 0) && host->bus_ops)
1312 __mmc_release_bus(host);
1313 spin_unlock_irqrestore(&host->lock, flags);
1314}
1315
1316/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001317 * Assign a mmc bus handler to a host. Only one bus handler may control a
1318 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001320void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001322 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001324 BUG_ON(!host);
1325 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001326
Pierre Ossmand84075c82007-08-09 13:23:56 +02001327 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001328
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001329 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001330
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001331 BUG_ON(host->bus_ops);
1332 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001333
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001334 host->bus_ops = ops;
1335 host->bus_refs = 1;
1336 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001338 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339}
1340
1341/*
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001342 * Remove the current bus handler from a host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001344void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001346 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001348 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Pierre Ossmand84075c82007-08-09 13:23:56 +02001350 WARN_ON(!host->claimed);
1351 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001353 spin_lock_irqsave(&host->lock, flags);
1354
1355 host->bus_dead = 1;
1356
1357 spin_unlock_irqrestore(&host->lock, flags);
1358
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001359 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360}
1361
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362/**
1363 * mmc_detect_change - process change of state on a MMC socket
1364 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001365 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001367 * MMC drivers should call this when they detect a card has been
1368 * inserted or removed. The MMC layer will confirm that any
1369 * present card is still functional, and initialize any newly
1370 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001372void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001374#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001375 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001376 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001377 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001378 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001379#endif
1380
David Howellsc4028952006-11-22 14:57:56 +00001381 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384EXPORT_SYMBOL(mmc_detect_change);
1385
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001386void mmc_init_erase(struct mmc_card *card)
1387{
1388 unsigned int sz;
1389
1390 if (is_power_of_2(card->erase_size))
1391 card->erase_shift = ffs(card->erase_size) - 1;
1392 else
1393 card->erase_shift = 0;
1394
1395 /*
1396 * It is possible to erase an arbitrarily large area of an SD or MMC
1397 * card. That is not desirable because it can take a long time
1398 * (minutes) potentially delaying more important I/O, and also the
1399 * timeout calculations become increasingly hugely over-estimated.
1400 * Consequently, 'pref_erase' is defined as a guide to limit erases
1401 * to that size and alignment.
1402 *
1403 * For SD cards that define Allocation Unit size, limit erases to one
1404 * Allocation Unit at a time. For MMC cards that define High Capacity
1405 * Erase Size, whether it is switched on or not, limit to that size.
1406 * Otherwise just have a stab at a good value. For modern cards it
1407 * will end up being 4MiB. Note that if the value is too small, it
1408 * can end up taking longer to erase.
1409 */
1410 if (mmc_card_sd(card) && card->ssr.au) {
1411 card->pref_erase = card->ssr.au;
1412 card->erase_shift = ffs(card->ssr.au) - 1;
1413 } else if (card->ext_csd.hc_erase_size) {
1414 card->pref_erase = card->ext_csd.hc_erase_size;
1415 } else {
1416 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1417 if (sz < 128)
1418 card->pref_erase = 512 * 1024 / 512;
1419 else if (sz < 512)
1420 card->pref_erase = 1024 * 1024 / 512;
1421 else if (sz < 1024)
1422 card->pref_erase = 2 * 1024 * 1024 / 512;
1423 else
1424 card->pref_erase = 4 * 1024 * 1024 / 512;
1425 if (card->pref_erase < card->erase_size)
1426 card->pref_erase = card->erase_size;
1427 else {
1428 sz = card->pref_erase % card->erase_size;
1429 if (sz)
1430 card->pref_erase += card->erase_size - sz;
1431 }
1432 }
1433}
1434
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001435static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1436 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001437{
1438 unsigned int erase_timeout;
1439
1440 if (card->ext_csd.erase_group_def & 1) {
1441 /* High Capacity Erase Group Size uses HC timeouts */
1442 if (arg == MMC_TRIM_ARG)
1443 erase_timeout = card->ext_csd.trim_timeout;
1444 else
1445 erase_timeout = card->ext_csd.hc_erase_timeout;
1446 } else {
1447 /* CSD Erase Group Size uses write timeout */
1448 unsigned int mult = (10 << card->csd.r2w_factor);
1449 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1450 unsigned int timeout_us;
1451
1452 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1453 if (card->csd.tacc_ns < 1000000)
1454 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1455 else
1456 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1457
1458 /*
1459 * ios.clock is only a target. The real clock rate might be
1460 * less but not that much less, so fudge it by multiplying by 2.
1461 */
1462 timeout_clks <<= 1;
1463 timeout_us += (timeout_clks * 1000) /
Adrian Hunter4cf8c6d2011-06-23 13:40:27 +03001464 (mmc_host_clk_rate(card->host) / 1000);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001465
1466 erase_timeout = timeout_us / 1000;
1467
1468 /*
1469 * Theoretically, the calculation could underflow so round up
1470 * to 1ms in that case.
1471 */
1472 if (!erase_timeout)
1473 erase_timeout = 1;
1474 }
1475
1476 /* Multiplier for secure operations */
1477 if (arg & MMC_SECURE_ARGS) {
1478 if (arg == MMC_SECURE_ERASE_ARG)
1479 erase_timeout *= card->ext_csd.sec_erase_mult;
1480 else
1481 erase_timeout *= card->ext_csd.sec_trim_mult;
1482 }
1483
1484 erase_timeout *= qty;
1485
1486 /*
1487 * Ensure at least a 1 second timeout for SPI as per
1488 * 'mmc_set_data_timeout()'
1489 */
1490 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1491 erase_timeout = 1000;
1492
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001493 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001494}
1495
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001496static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1497 unsigned int arg,
1498 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001499{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001500 unsigned int erase_timeout;
1501
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001502 if (card->ssr.erase_timeout) {
1503 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001504 erase_timeout = card->ssr.erase_timeout * qty +
1505 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001506 } else {
1507 /*
1508 * Erase timeout not specified in SD Status Register (SSR) so
1509 * use 250ms per write block.
1510 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001511 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001512 }
1513
1514 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001515 if (erase_timeout < 1000)
1516 erase_timeout = 1000;
1517
1518 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001519}
1520
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001521static unsigned int mmc_erase_timeout(struct mmc_card *card,
1522 unsigned int arg,
1523 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001524{
1525 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001526 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001527 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001528 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001529}
1530
1531static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1532 unsigned int to, unsigned int arg)
1533{
Chris Ball1278dba2011-04-13 23:40:30 -04001534 struct mmc_command cmd = {0};
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001535 unsigned int qty = 0;
1536 int err;
1537
1538 /*
1539 * qty is used to calculate the erase timeout which depends on how many
1540 * erase groups (or allocation units in SD terminology) are affected.
1541 * We count erasing part of an erase group as one erase group.
1542 * For SD, the allocation units are always a power of 2. For MMC, the
1543 * erase group size is almost certainly also power of 2, but it does not
1544 * seem to insist on that in the JEDEC standard, so we fall back to
1545 * division in that case. SD may not specify an allocation unit size,
1546 * in which case the timeout is based on the number of write blocks.
1547 *
1548 * Note that the timeout for secure trim 2 will only be correct if the
1549 * number of erase groups specified is the same as the total of all
1550 * preceding secure trim 1 commands. Since the power may have been
1551 * lost since the secure trim 1 commands occurred, it is generally
1552 * impossible to calculate the secure trim 2 timeout correctly.
1553 */
1554 if (card->erase_shift)
1555 qty += ((to >> card->erase_shift) -
1556 (from >> card->erase_shift)) + 1;
1557 else if (mmc_card_sd(card))
1558 qty += to - from + 1;
1559 else
1560 qty += ((to / card->erase_size) -
1561 (from / card->erase_size)) + 1;
1562
1563 if (!mmc_card_blockaddr(card)) {
1564 from <<= 9;
1565 to <<= 9;
1566 }
1567
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001568 if (mmc_card_sd(card))
1569 cmd.opcode = SD_ERASE_WR_BLK_START;
1570 else
1571 cmd.opcode = MMC_ERASE_GROUP_START;
1572 cmd.arg = from;
1573 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1574 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1575 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301576 pr_err("mmc_erase: group start error %d, "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001577 "status %#x\n", err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03001578 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001579 goto out;
1580 }
1581
1582 memset(&cmd, 0, sizeof(struct mmc_command));
1583 if (mmc_card_sd(card))
1584 cmd.opcode = SD_ERASE_WR_BLK_END;
1585 else
1586 cmd.opcode = MMC_ERASE_GROUP_END;
1587 cmd.arg = to;
1588 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1589 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1590 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301591 pr_err("mmc_erase: group end error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001592 err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03001593 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001594 goto out;
1595 }
1596
1597 memset(&cmd, 0, sizeof(struct mmc_command));
1598 cmd.opcode = MMC_ERASE;
1599 cmd.arg = arg;
1600 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001601 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001602 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1603 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301604 pr_err("mmc_erase: erase error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001605 err, cmd.resp[0]);
1606 err = -EIO;
1607 goto out;
1608 }
1609
1610 if (mmc_host_is_spi(card->host))
1611 goto out;
1612
1613 do {
1614 memset(&cmd, 0, sizeof(struct mmc_command));
1615 cmd.opcode = MMC_SEND_STATUS;
1616 cmd.arg = card->rca << 16;
1617 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1618 /* Do not retry else we can't see errors */
1619 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1620 if (err || (cmd.resp[0] & 0xFDF92000)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301621 pr_err("error %d requesting status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001622 err, cmd.resp[0]);
1623 err = -EIO;
1624 goto out;
1625 }
1626 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
Jaehoon Chung7435bb72011-08-10 18:46:28 +09001627 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001628out:
1629 return err;
1630}
1631
1632/**
1633 * mmc_erase - erase sectors.
1634 * @card: card to erase
1635 * @from: first sector to erase
1636 * @nr: number of sectors to erase
1637 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1638 *
1639 * Caller must claim host before calling this function.
1640 */
1641int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1642 unsigned int arg)
1643{
1644 unsigned int rem, to = from + nr;
1645
1646 if (!(card->host->caps & MMC_CAP_ERASE) ||
1647 !(card->csd.cmdclass & CCC_ERASE))
1648 return -EOPNOTSUPP;
1649
1650 if (!card->erase_size)
1651 return -EOPNOTSUPP;
1652
1653 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1654 return -EOPNOTSUPP;
1655
1656 if ((arg & MMC_SECURE_ARGS) &&
1657 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1658 return -EOPNOTSUPP;
1659
1660 if ((arg & MMC_TRIM_ARGS) &&
1661 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1662 return -EOPNOTSUPP;
1663
1664 if (arg == MMC_SECURE_ERASE_ARG) {
1665 if (from % card->erase_size || nr % card->erase_size)
1666 return -EINVAL;
1667 }
1668
1669 if (arg == MMC_ERASE_ARG) {
1670 rem = from % card->erase_size;
1671 if (rem) {
1672 rem = card->erase_size - rem;
1673 from += rem;
1674 if (nr > rem)
1675 nr -= rem;
1676 else
1677 return 0;
1678 }
1679 rem = nr % card->erase_size;
1680 if (rem)
1681 nr -= rem;
1682 }
1683
1684 if (nr == 0)
1685 return 0;
1686
1687 to = from + nr;
1688
1689 if (to <= from)
1690 return -EINVAL;
1691
1692 /* 'from' and 'to' are inclusive */
1693 to -= 1;
1694
1695 return mmc_do_erase(card, from, to, arg);
1696}
1697EXPORT_SYMBOL(mmc_erase);
1698
1699int mmc_can_erase(struct mmc_card *card)
1700{
1701 if ((card->host->caps & MMC_CAP_ERASE) &&
1702 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1703 return 1;
1704 return 0;
1705}
1706EXPORT_SYMBOL(mmc_can_erase);
1707
1708int mmc_can_trim(struct mmc_card *card)
1709{
1710 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1711 return 1;
1712 return 0;
1713}
1714EXPORT_SYMBOL(mmc_can_trim);
1715
1716int mmc_can_secure_erase_trim(struct mmc_card *card)
1717{
1718 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1719 return 1;
1720 return 0;
1721}
1722EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1723
1724int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1725 unsigned int nr)
1726{
1727 if (!card->erase_size)
1728 return 0;
1729 if (from % card->erase_size || nr % card->erase_size)
1730 return 0;
1731 return 1;
1732}
1733EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
Adrian Huntere056a1b2011-06-28 17:16:02 +03001735static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1736 unsigned int arg)
1737{
1738 struct mmc_host *host = card->host;
1739 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1740 unsigned int last_timeout = 0;
1741
1742 if (card->erase_shift)
1743 max_qty = UINT_MAX >> card->erase_shift;
1744 else if (mmc_card_sd(card))
1745 max_qty = UINT_MAX;
1746 else
1747 max_qty = UINT_MAX / card->erase_size;
1748
1749 /* Find the largest qty with an OK timeout */
1750 do {
1751 y = 0;
1752 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1753 timeout = mmc_erase_timeout(card, arg, qty + x);
1754 if (timeout > host->max_discard_to)
1755 break;
1756 if (timeout < last_timeout)
1757 break;
1758 last_timeout = timeout;
1759 y = x;
1760 }
1761 qty += y;
1762 } while (y);
1763
1764 if (!qty)
1765 return 0;
1766
1767 if (qty == 1)
1768 return 1;
1769
1770 /* Convert qty to sectors */
1771 if (card->erase_shift)
1772 max_discard = --qty << card->erase_shift;
1773 else if (mmc_card_sd(card))
1774 max_discard = qty;
1775 else
1776 max_discard = --qty * card->erase_size;
1777
1778 return max_discard;
1779}
1780
1781unsigned int mmc_calc_max_discard(struct mmc_card *card)
1782{
1783 struct mmc_host *host = card->host;
1784 unsigned int max_discard, max_trim;
1785
1786 if (!host->max_discard_to)
1787 return UINT_MAX;
1788
1789 /*
1790 * Without erase_group_def set, MMC erase timeout depends on clock
1791 * frequence which can change. In that case, the best choice is
1792 * just the preferred erase size.
1793 */
1794 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1795 return card->pref_erase;
1796
1797 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1798 if (mmc_can_trim(card)) {
1799 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1800 if (max_trim < max_discard)
1801 max_discard = max_trim;
1802 } else if (max_discard < card->erase_size) {
1803 max_discard = 0;
1804 }
1805 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1806 mmc_hostname(host), max_discard, host->max_discard_to);
1807 return max_discard;
1808}
1809EXPORT_SYMBOL(mmc_calc_max_discard);
1810
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001811int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1812{
Chris Ball1278dba2011-04-13 23:40:30 -04001813 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001814
1815 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1816 return 0;
1817
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001818 cmd.opcode = MMC_SET_BLOCKLEN;
1819 cmd.arg = blocklen;
1820 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1821 return mmc_wait_for_cmd(card->host, &cmd, 5);
1822}
1823EXPORT_SYMBOL(mmc_set_blocklen);
1824
Adrian Hunterb2499512011-08-29 16:42:11 +03001825static void mmc_hw_reset_for_init(struct mmc_host *host)
1826{
1827 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1828 return;
1829 mmc_host_clk_hold(host);
1830 host->ops->hw_reset(host);
1831 mmc_host_clk_release(host);
1832}
1833
1834int mmc_can_reset(struct mmc_card *card)
1835{
1836 u8 rst_n_function;
1837
1838 if (!mmc_card_mmc(card))
1839 return 0;
1840 rst_n_function = card->ext_csd.rst_n_function;
1841 if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED)
1842 return 0;
1843 return 1;
1844}
1845EXPORT_SYMBOL(mmc_can_reset);
1846
1847static int mmc_do_hw_reset(struct mmc_host *host, int check)
1848{
1849 struct mmc_card *card = host->card;
1850
1851 if (!host->bus_ops->power_restore)
1852 return -EOPNOTSUPP;
1853
1854 if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset)
1855 return -EOPNOTSUPP;
1856
1857 if (!card)
1858 return -EINVAL;
1859
1860 if (!mmc_can_reset(card))
1861 return -EOPNOTSUPP;
1862
1863 mmc_host_clk_hold(host);
1864 mmc_set_clock(host, host->f_init);
1865
1866 host->ops->hw_reset(host);
1867
1868 /* If the reset has happened, then a status command will fail */
1869 if (check) {
1870 struct mmc_command cmd = {0};
1871 int err;
1872
1873 cmd.opcode = MMC_SEND_STATUS;
1874 if (!mmc_host_is_spi(card->host))
1875 cmd.arg = card->rca << 16;
1876 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
1877 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1878 if (!err) {
1879 mmc_host_clk_release(host);
1880 return -ENOSYS;
1881 }
1882 }
1883
1884 host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR);
1885 if (mmc_host_is_spi(host)) {
1886 host->ios.chip_select = MMC_CS_HIGH;
1887 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1888 } else {
1889 host->ios.chip_select = MMC_CS_DONTCARE;
1890 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1891 }
1892 host->ios.bus_width = MMC_BUS_WIDTH_1;
1893 host->ios.timing = MMC_TIMING_LEGACY;
1894 mmc_set_ios(host);
1895
1896 mmc_host_clk_release(host);
1897
1898 return host->bus_ops->power_restore(host);
1899}
1900
1901int mmc_hw_reset(struct mmc_host *host)
1902{
1903 return mmc_do_hw_reset(host, 0);
1904}
1905EXPORT_SYMBOL(mmc_hw_reset);
1906
1907int mmc_hw_reset_check(struct mmc_host *host)
1908{
1909 return mmc_do_hw_reset(host, 1);
1910}
1911EXPORT_SYMBOL(mmc_hw_reset_check);
1912
Andy Ross807e8e42011-01-03 10:36:56 -08001913static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1914{
1915 host->f_init = freq;
1916
1917#ifdef CONFIG_MMC_DEBUG
1918 pr_info("%s: %s: trying to init card at %u Hz\n",
1919 mmc_hostname(host), __func__, host->f_init);
1920#endif
1921 mmc_power_up(host);
Philip Rakity2f94e552011-02-13 23:12:28 -08001922
1923 /*
Adrian Hunterb2499512011-08-29 16:42:11 +03001924 * Some eMMCs (with VCCQ always on) may not be reset after power up, so
1925 * do a hardware reset if possible.
1926 */
1927 mmc_hw_reset_for_init(host);
1928
1929 /*
Philip Rakity2f94e552011-02-13 23:12:28 -08001930 * sdio_reset sends CMD52 to reset card. Since we do not know
1931 * if the card is being re-initialized, just send it. CMD52
1932 * should be ignored by SD/eMMC cards.
1933 */
Andy Ross807e8e42011-01-03 10:36:56 -08001934 sdio_reset(host);
1935 mmc_go_idle(host);
1936
1937 mmc_send_if_cond(host, host->ocr_avail);
1938
1939 /* Order's important: probe SDIO, then SD, then MMC */
1940 if (!mmc_attach_sdio(host))
1941 return 0;
1942 if (!mmc_attach_sd(host))
1943 return 0;
1944 if (!mmc_attach_mmc(host))
1945 return 0;
1946
1947 mmc_power_off(host);
1948 return -EIO;
1949}
1950
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001951void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952{
Andy Ross807e8e42011-01-03 10:36:56 -08001953 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
David Howellsc4028952006-11-22 14:57:56 +00001954 struct mmc_host *host =
1955 container_of(work, struct mmc_host, detect.work);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001956 int i;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001957
Andy Ross807e8e42011-01-03 10:36:56 -08001958 if (host->rescan_disable)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001961 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02001963 /*
1964 * if there is a _removable_ card registered, check whether it is
1965 * still present
1966 */
1967 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
Ohad Ben-Cohenbad3bab2011-03-08 23:32:02 +02001968 && !(host->caps & MMC_CAP_NONREMOVABLE))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001969 host->bus_ops->detect(host);
1970
Chris Ballc5841792011-01-04 12:20:22 -05001971 /*
1972 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1973 * the card is no longer present.
1974 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001975 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001976 mmc_bus_get(host);
1977
1978 /* if there still is a card present, stop here */
1979 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001980 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001981 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001983
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001984 /*
1985 * Only we can add a new handler, so it's safe to
1986 * release the lock here.
1987 */
1988 mmc_bus_put(host);
1989
1990 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1991 goto out;
1992
Andy Ross807e8e42011-01-03 10:36:56 -08001993 mmc_claim_host(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001994 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
Andy Ross807e8e42011-01-03 10:36:56 -08001995 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1996 break;
Jaehoon Chung06b22332011-05-12 17:18:59 +09001997 if (freqs[i] <= host->f_min)
Andy Ross807e8e42011-01-03 10:36:56 -08001998 break;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001999 }
Andy Ross807e8e42011-01-03 10:36:56 -08002000 mmc_release_host(host);
2001
2002 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04002003 if (host->caps & MMC_CAP_NEEDS_POLL)
2004 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005}
2006
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002007void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008{
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002009 mmc_power_off(host);
2010 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Pierre Ossmanb93931a2007-05-19 14:06:24 +02002013void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014{
Pierre Ossman3b91e552007-02-11 20:43:19 +01002015#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002016 unsigned long flags;
2017 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002018 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02002019 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002020#endif
2021
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002022 if (host->caps & MMC_CAP_DISABLE)
2023 cancel_delayed_work(&host->disable);
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01002024 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01002025 mmc_flush_scheduled_work();
2026
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002027 /* clear pm flags now and let card drivers set them as needed */
2028 host->pm_flags = 0;
2029
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002030 mmc_bus_get(host);
2031 if (host->bus_ops && !host->bus_dead) {
2032 if (host->bus_ops->remove)
2033 host->bus_ops->remove(host);
2034
2035 mmc_claim_host(host);
2036 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002037 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002038 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07002039 mmc_bus_put(host);
2040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002042 mmc_bus_put(host);
2043
2044 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 mmc_power_off(host);
2047}
2048
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002049int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002050{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002051 int ret = 0;
2052
Daniel Drakebb9cab92011-07-17 16:38:41 +01002053#ifdef CONFIG_MMC_DEBUG
2054 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
2055#endif
2056
Adrian Huntereae1aee2009-09-22 16:44:33 -07002057 mmc_bus_get(host);
2058
2059 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2060 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002061 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002062 }
2063
2064 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002065 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002066
2067 mmc_bus_put(host);
2068
2069 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002070
2071 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002072}
2073EXPORT_SYMBOL(mmc_power_save_host);
2074
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002075int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07002076{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002077 int ret;
2078
Daniel Drakebb9cab92011-07-17 16:38:41 +01002079#ifdef CONFIG_MMC_DEBUG
2080 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
2081#endif
2082
Adrian Huntereae1aee2009-09-22 16:44:33 -07002083 mmc_bus_get(host);
2084
2085 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
2086 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002087 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002088 }
2089
2090 mmc_power_up(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002091 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07002092
2093 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02002094
2095 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07002096}
2097EXPORT_SYMBOL(mmc_power_restore_host);
2098
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07002099int mmc_card_awake(struct mmc_host *host)
2100{
2101 int err = -ENOSYS;
2102
2103 mmc_bus_get(host);
2104
2105 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2106 err = host->bus_ops->awake(host);
2107
2108 mmc_bus_put(host);
2109
2110 return err;
2111}
2112EXPORT_SYMBOL(mmc_card_awake);
2113
2114int mmc_card_sleep(struct mmc_host *host)
2115{
2116 int err = -ENOSYS;
2117
2118 mmc_bus_get(host);
2119
2120 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
2121 err = host->bus_ops->sleep(host);
2122
2123 mmc_bus_put(host);
2124
2125 return err;
2126}
2127EXPORT_SYMBOL(mmc_card_sleep);
2128
2129int mmc_card_can_sleep(struct mmc_host *host)
2130{
2131 struct mmc_card *card = host->card;
2132
2133 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
2134 return 1;
2135 return 0;
2136}
2137EXPORT_SYMBOL(mmc_card_can_sleep);
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139#ifdef CONFIG_PM
2140
2141/**
2142 * mmc_suspend_host - suspend a host
2143 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002145int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002147 int err = 0;
2148
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002149 if (host->caps & MMC_CAP_DISABLE)
2150 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02002151 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002152 mmc_flush_scheduled_work();
2153
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002154 mmc_bus_get(host);
2155 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002156 if (host->bus_ops->suspend)
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002157 err = host->bus_ops->suspend(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02002158 if (err == -ENOSYS || !host->bus_ops->resume) {
2159 /*
2160 * We simply "remove" the card in this case.
2161 * It will be redetected on resume.
2162 */
2163 if (host->bus_ops->remove)
2164 host->bus_ops->remove(host);
2165 mmc_claim_host(host);
2166 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002167 mmc_power_off(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02002168 mmc_release_host(host);
2169 host->pm_flags = 0;
2170 err = 0;
2171 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002172 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002173 mmc_bus_put(host);
2174
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002175 if (!err && !mmc_card_keep_power(host))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002176 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002178 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179}
2180
2181EXPORT_SYMBOL(mmc_suspend_host);
2182
2183/**
2184 * mmc_resume_host - resume a previously suspended host
2185 * @host: mmc host
2186 */
2187int mmc_resume_host(struct mmc_host *host)
2188{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002189 int err = 0;
2190
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002191 mmc_bus_get(host);
2192 if (host->bus_ops && !host->bus_dead) {
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002193 if (!mmc_card_keep_power(host)) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002194 mmc_power_up(host);
2195 mmc_select_voltage(host, host->ocr);
Ohad Ben-Cohene5945732010-11-28 07:21:30 +02002196 /*
2197 * Tell runtime PM core we just powered up the card,
2198 * since it still believes the card is powered off.
2199 * Note that currently runtime PM is only enabled
2200 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2201 */
2202 if (mmc_card_sdio(host->card) &&
2203 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2204 pm_runtime_disable(&host->card->dev);
2205 pm_runtime_set_active(&host->card->dev);
2206 pm_runtime_enable(&host->card->dev);
2207 }
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002208 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002209 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002210 err = host->bus_ops->resume(host);
2211 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302212 pr_warning("%s: error %d during resume "
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002213 "(card was removed?)\n",
2214 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002215 err = 0;
2216 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002217 }
Eliad Pellera8e6df72011-05-09 11:32:31 +03002218 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002219 mmc_bus_put(host);
2220
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002221 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223EXPORT_SYMBOL(mmc_resume_host);
2224
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002225/* Do the card removal on suspend if card is assumed removeable
2226 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2227 to sync the card.
2228*/
2229int mmc_pm_notify(struct notifier_block *notify_block,
2230 unsigned long mode, void *unused)
2231{
2232 struct mmc_host *host = container_of(
2233 notify_block, struct mmc_host, pm_notify);
2234 unsigned long flags;
2235
2236
2237 switch (mode) {
2238 case PM_HIBERNATION_PREPARE:
2239 case PM_SUSPEND_PREPARE:
2240
2241 spin_lock_irqsave(&host->lock, flags);
2242 host->rescan_disable = 1;
Girish K Sbec87262011-10-13 12:04:16 +05302243 host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002244 spin_unlock_irqrestore(&host->lock, flags);
2245 cancel_delayed_work_sync(&host->detect);
2246
2247 if (!host->bus_ops || host->bus_ops->suspend)
2248 break;
2249
2250 mmc_claim_host(host);
2251
2252 if (host->bus_ops->remove)
2253 host->bus_ops->remove(host);
2254
2255 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002256 mmc_power_off(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002257 mmc_release_host(host);
2258 host->pm_flags = 0;
2259 break;
2260
2261 case PM_POST_SUSPEND:
2262 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01002263 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002264
2265 spin_lock_irqsave(&host->lock, flags);
2266 host->rescan_disable = 0;
Girish K Sbec87262011-10-13 12:04:16 +05302267 host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002268 spin_unlock_irqrestore(&host->lock, flags);
2269 mmc_detect_change(host, 0);
2270
2271 }
2272
2273 return 0;
2274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275#endif
2276
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002277static int __init mmc_init(void)
2278{
2279 int ret;
2280
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002281 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002282 if (!workqueue)
2283 return -ENOMEM;
2284
2285 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002286 if (ret)
2287 goto destroy_workqueue;
2288
2289 ret = mmc_register_host_class();
2290 if (ret)
2291 goto unregister_bus;
2292
2293 ret = sdio_register_bus();
2294 if (ret)
2295 goto unregister_host_class;
2296
2297 return 0;
2298
2299unregister_host_class:
2300 mmc_unregister_host_class();
2301unregister_bus:
2302 mmc_unregister_bus();
2303destroy_workqueue:
2304 destroy_workqueue(workqueue);
2305
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002306 return ret;
2307}
2308
2309static void __exit mmc_exit(void)
2310{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002311 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002312 mmc_unregister_host_class();
2313 mmc_unregister_bus();
2314 destroy_workqueue(workqueue);
2315}
2316
Nicolas Pitre26074962007-06-16 02:07:53 -04002317subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002318module_exit(mmc_exit);
2319
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320MODULE_LICENSE("GPL");