blob: 0b4c2ed22bcee3a978509621d29d40edee720b3f [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) {
Pierre Ossmane4d21702007-07-24 21:46:49 +0200144 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
145 mmc_hostname(host), cmd->opcode, err);
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 cmd->retries--;
148 cmd->error = 0;
149 host->ops->request(host, 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{
256 wait_for_completion(&mrq->completion);
257}
258
259/**
260 * mmc_pre_req - Prepare for a new request
261 * @host: MMC host to prepare command
262 * @mrq: MMC request to prepare for
263 * @is_first_req: true if there is no previous started request
264 * that may run in parellel to this call, otherwise false
265 *
266 * mmc_pre_req() is called in prior to mmc_start_req() to let
267 * host prepare for the new request. Preparation of a request may be
268 * performed while another request is running on the host.
269 */
270static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
271 bool is_first_req)
272{
273 if (host->ops->pre_req)
274 host->ops->pre_req(host, mrq, is_first_req);
275}
276
277/**
278 * mmc_post_req - Post process a completed request
279 * @host: MMC host to post process command
280 * @mrq: MMC request to post process for
281 * @err: Error, if non zero, clean up any resources made in pre_req
282 *
283 * Let the host post process a completed request. Post processing of
284 * a request may be performed while another reuqest is running.
285 */
286static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
287 int err)
288{
289 if (host->ops->post_req)
290 host->ops->post_req(host, mrq, err);
291}
292
293/**
294 * mmc_start_req - start a non-blocking request
295 * @host: MMC host to start command
296 * @areq: async request to start
297 * @error: out parameter returns 0 for success, otherwise non zero
298 *
299 * Start a new MMC custom command request for a host.
300 * If there is on ongoing async request wait for completion
301 * of that request and start the new one and return.
302 * Does not wait for the new request to complete.
303 *
304 * Returns the completed request, NULL in case of none completed.
305 * Wait for the an ongoing request (previoulsy started) to complete and
306 * return the completed request. If there is no ongoing request, NULL
307 * is returned without waiting. NULL is not an error condition.
308 */
309struct mmc_async_req *mmc_start_req(struct mmc_host *host,
310 struct mmc_async_req *areq, int *error)
311{
312 int err = 0;
313 struct mmc_async_req *data = host->areq;
314
315 /* Prepare a new request */
316 if (areq)
317 mmc_pre_req(host, areq->mrq, !host->areq);
318
319 if (host->areq) {
320 mmc_wait_for_req_done(host, host->areq->mrq);
321 err = host->areq->err_check(host->card, host->areq);
322 if (err) {
Per Forlin7c8a2822011-08-29 15:35:58 +0200323 /* post process the completed failed request */
Per Forlinaa8b6832011-07-01 18:55:22 +0200324 mmc_post_req(host, host->areq->mrq, 0);
325 if (areq)
Per Forlin7c8a2822011-08-29 15:35:58 +0200326 /*
327 * Cancel the new prepared request, because
328 * it can't run until the failed
329 * request has been properly handled.
330 */
Per Forlinaa8b6832011-07-01 18:55:22 +0200331 mmc_post_req(host, areq->mrq, -EINVAL);
332
333 host->areq = NULL;
334 goto out;
335 }
336 }
337
338 if (areq)
339 __mmc_start_req(host, areq->mrq);
340
341 if (host->areq)
342 mmc_post_req(host, host->areq->mrq, 0);
343
344 host->areq = areq;
345 out:
346 if (error)
347 *error = err;
348 return data;
349}
350EXPORT_SYMBOL(mmc_start_req);
351
Pierre Ossman67a61c42007-07-11 20:22:11 +0200352/**
353 * mmc_wait_for_req - start a request and wait for completion
354 * @host: MMC host to start command
355 * @mrq: MMC request to start
356 *
357 * Start a new MMC custom command request for a host, and wait
358 * for the command to complete. Does not attempt to parse the
359 * response.
360 */
361void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Per Forlinaa8b6832011-07-01 18:55:22 +0200363 __mmc_start_req(host, mrq);
364 mmc_wait_for_req_done(host, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366EXPORT_SYMBOL(mmc_wait_for_req);
367
368/**
369 * mmc_wait_for_cmd - start a command and wait for completion
370 * @host: MMC host to start command
371 * @cmd: MMC command to start
372 * @retries: maximum number of retries
373 *
374 * Start a new MMC command for a host, and wait for the command
375 * to complete. Return any error that occurred while the command
376 * was executing. Do not attempt to parse the response.
377 */
378int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
379{
Venkatraman Sad5fd972011-08-25 00:30:50 +0530380 struct mmc_request mrq = {NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Pierre Ossmand84075c82007-08-09 13:23:56 +0200382 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 memset(cmd->resp, 0, sizeof(cmd->resp));
385 cmd->retries = retries;
386
387 mrq.cmd = cmd;
388 cmd->data = NULL;
389
390 mmc_wait_for_req(host, &mrq);
391
392 return cmd->error;
393}
394
395EXPORT_SYMBOL(mmc_wait_for_cmd);
396
Pierre Ossman335eadf2005-09-06 15:18:50 -0700397/**
Russell Kingd773d722006-09-07 15:57:12 +0100398 * mmc_set_data_timeout - set the timeout for a data command
399 * @data: data phase for command
400 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200401 *
402 * Computes the data timeout parameters according to the
403 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100404 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200405void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100406{
407 unsigned int mult;
408
409 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200410 * SDIO cards only define an upper 1 s limit on access.
411 */
412 if (mmc_card_sdio(card)) {
413 data->timeout_ns = 1000000000;
414 data->timeout_clks = 0;
415 return;
416 }
417
418 /*
Russell Kingd773d722006-09-07 15:57:12 +0100419 * SD cards use a 100 multiplier rather than 10
420 */
421 mult = mmc_card_sd(card) ? 100 : 10;
422
423 /*
424 * Scale up the multiplier (and therefore the timeout) by
425 * the r2w factor for writes.
426 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200427 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100428 mult <<= card->csd.r2w_factor;
429
430 data->timeout_ns = card->csd.tacc_ns * mult;
431 data->timeout_clks = card->csd.tacc_clks * mult;
432
433 /*
434 * SD cards also have an upper limit on the timeout.
435 */
436 if (mmc_card_sd(card)) {
437 unsigned int timeout_us, limit_us;
438
439 timeout_us = data->timeout_ns / 1000;
Linus Walleije9b86842011-01-05 00:44:32 +0100440 if (mmc_host_clk_rate(card->host))
441 timeout_us += data->timeout_clks * 1000 /
442 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100443
Pierre Ossmanb146d262007-07-24 19:16:54 +0200444 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100445 /*
446 * The limit is really 250 ms, but that is
447 * insufficient for some crappy cards.
448 */
449 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100450 else
451 limit_us = 100000;
452
Philip Langdalefba68bd2007-01-04 06:57:32 -0800453 /*
454 * SDHC cards always use these fixed values.
455 */
456 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100457 data->timeout_ns = limit_us * 1000;
458 data->timeout_clks = 0;
459 }
460 }
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100461 /*
462 * Some cards need very high timeouts if driven in SPI mode.
463 * The worst observed timeout was 900ms after writing a
464 * continuous stream of data until the internal logic
465 * overflowed.
466 */
467 if (mmc_host_is_spi(card->host)) {
468 if (data->flags & MMC_DATA_WRITE) {
469 if (data->timeout_ns < 1000000000)
470 data->timeout_ns = 1000000000; /* 1s */
471 } else {
472 if (data->timeout_ns < 100000000)
473 data->timeout_ns = 100000000; /* 100ms */
474 }
475 }
Russell Kingd773d722006-09-07 15:57:12 +0100476}
477EXPORT_SYMBOL(mmc_set_data_timeout);
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200480 * mmc_align_data_size - pads a transfer size to a more optimal value
481 * @card: the MMC card associated with the data transfer
482 * @sz: original transfer size
483 *
484 * Pads the original data size with a number of extra bytes in
485 * order to avoid controller bugs and/or performance hits
486 * (e.g. some controllers revert to PIO for certain sizes).
487 *
488 * Returns the improved size, which might be unmodified.
489 *
490 * Note that this function is only relevant when issuing a
491 * single scatter gather entry.
492 */
493unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
494{
495 /*
496 * FIXME: We don't have a system for the controller to tell
497 * the core about its problems yet, so for now we just 32-bit
498 * align the size.
499 */
500 sz = ((sz + 3) / 4) * 4;
501
502 return sz;
503}
504EXPORT_SYMBOL(mmc_align_data_size);
505
506/**
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700507 * mmc_host_enable - enable a host.
508 * @host: mmc host to enable
509 *
510 * Hosts that support power saving can use the 'enable' and 'disable'
511 * methods to exit and enter power saving states. For more information
512 * see comments for struct mmc_host_ops.
513 */
514int mmc_host_enable(struct mmc_host *host)
515{
516 if (!(host->caps & MMC_CAP_DISABLE))
517 return 0;
518
519 if (host->en_dis_recurs)
520 return 0;
521
522 if (host->nesting_cnt++)
523 return 0;
524
525 cancel_delayed_work_sync(&host->disable);
526
527 if (host->enabled)
528 return 0;
529
530 if (host->ops->enable) {
531 int err;
532
533 host->en_dis_recurs = 1;
534 err = host->ops->enable(host);
535 host->en_dis_recurs = 0;
536
537 if (err) {
538 pr_debug("%s: enable error %d\n",
539 mmc_hostname(host), err);
540 return err;
541 }
542 }
543 host->enabled = 1;
544 return 0;
545}
546EXPORT_SYMBOL(mmc_host_enable);
547
548static int mmc_host_do_disable(struct mmc_host *host, int lazy)
549{
550 if (host->ops->disable) {
551 int err;
552
553 host->en_dis_recurs = 1;
554 err = host->ops->disable(host, lazy);
555 host->en_dis_recurs = 0;
556
557 if (err < 0) {
558 pr_debug("%s: disable error %d\n",
559 mmc_hostname(host), err);
560 return err;
561 }
562 if (err > 0) {
563 unsigned long delay = msecs_to_jiffies(err);
564
565 mmc_schedule_delayed_work(&host->disable, delay);
566 }
567 }
568 host->enabled = 0;
569 return 0;
570}
571
572/**
573 * mmc_host_disable - disable a host.
574 * @host: mmc host to disable
575 *
576 * Hosts that support power saving can use the 'enable' and 'disable'
577 * methods to exit and enter power saving states. For more information
578 * see comments for struct mmc_host_ops.
579 */
580int mmc_host_disable(struct mmc_host *host)
581{
582 int err;
583
584 if (!(host->caps & MMC_CAP_DISABLE))
585 return 0;
586
587 if (host->en_dis_recurs)
588 return 0;
589
590 if (--host->nesting_cnt)
591 return 0;
592
593 if (!host->enabled)
594 return 0;
595
596 err = mmc_host_do_disable(host, 0);
597 return err;
598}
599EXPORT_SYMBOL(mmc_host_disable);
600
601/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200602 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200604 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200606 * Claim a host for a set of operations. If @abort is non null and
607 * dereference a non-zero value then this will return prematurely with
608 * that non-zero value without acquiring the lock. Returns zero
609 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200611int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
613 DECLARE_WAITQUEUE(wait, current);
614 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200615 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200617 might_sleep();
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 add_wait_queue(&host->wq, &wait);
620 spin_lock_irqsave(&host->lock, flags);
621 while (1) {
622 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200623 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700624 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 break;
626 spin_unlock_irqrestore(&host->lock, flags);
627 schedule();
628 spin_lock_irqsave(&host->lock, flags);
629 }
630 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700631 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200632 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700633 host->claimer = current;
634 host->claim_cnt += 1;
635 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200636 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 spin_unlock_irqrestore(&host->lock, flags);
638 remove_wait_queue(&host->wq, &wait);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700639 if (!stop)
640 mmc_host_enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200641 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Nicolas Pitre2342f332007-06-30 16:21:52 +0200644EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Adrian Hunter319a3f12009-09-22 16:44:30 -0700646/**
647 * mmc_try_claim_host - try exclusively to claim a host
648 * @host: mmc host to claim
649 *
650 * Returns %1 if the host is claimed, %0 otherwise.
651 */
652int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700653{
654 int claimed_host = 0;
655 unsigned long flags;
656
657 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700658 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700659 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700660 host->claimer = current;
661 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700662 claimed_host = 1;
663 }
664 spin_unlock_irqrestore(&host->lock, flags);
665 return claimed_host;
666}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700667EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700668
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100669/**
670 * mmc_do_release_host - release a claimed host
671 * @host: mmc host to release
672 *
673 * If you successfully claimed a host, this function will
674 * release it again.
675 */
676void mmc_do_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700677{
678 unsigned long flags;
679
680 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700681 if (--host->claim_cnt) {
682 /* Release for nested claim */
683 spin_unlock_irqrestore(&host->lock, flags);
684 } else {
685 host->claimed = 0;
686 host->claimer = NULL;
687 spin_unlock_irqrestore(&host->lock, flags);
688 wake_up(&host->wq);
689 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700690}
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100691EXPORT_SYMBOL(mmc_do_release_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700692
693void mmc_host_deeper_disable(struct work_struct *work)
694{
695 struct mmc_host *host =
696 container_of(work, struct mmc_host, disable.work);
697
698 /* If the host is claimed then we do not want to disable it anymore */
699 if (!mmc_try_claim_host(host))
700 return;
701 mmc_host_do_disable(host, 1);
702 mmc_do_release_host(host);
703}
704
705/**
706 * mmc_host_lazy_disable - lazily disable a host.
707 * @host: mmc host to disable
708 *
709 * Hosts that support power saving can use the 'enable' and 'disable'
710 * methods to exit and enter power saving states. For more information
711 * see comments for struct mmc_host_ops.
712 */
713int mmc_host_lazy_disable(struct mmc_host *host)
714{
715 if (!(host->caps & MMC_CAP_DISABLE))
716 return 0;
717
718 if (host->en_dis_recurs)
719 return 0;
720
721 if (--host->nesting_cnt)
722 return 0;
723
724 if (!host->enabled)
725 return 0;
726
727 if (host->disable_delay) {
728 mmc_schedule_delayed_work(&host->disable,
729 msecs_to_jiffies(host->disable_delay));
730 return 0;
731 } else
732 return mmc_host_do_disable(host, 1);
733}
734EXPORT_SYMBOL(mmc_host_lazy_disable);
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/**
737 * mmc_release_host - release a host
738 * @host: mmc host to release
739 *
740 * Release a MMC host, allowing others to claim the host
741 * for their operations.
742 */
743void mmc_release_host(struct mmc_host *host)
744{
Pierre Ossmand84075c82007-08-09 13:23:56 +0200745 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700747 mmc_host_lazy_disable(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700749 mmc_do_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752EXPORT_SYMBOL(mmc_release_host);
753
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100754/*
755 * Internal function that does the actual ios call to the host driver,
756 * optionally printing some debug output.
757 */
Russell King920e70c2006-05-04 18:22:51 +0100758static inline void mmc_set_ios(struct mmc_host *host)
759{
760 struct mmc_ios *ios = &host->ios;
761
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100762 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
763 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100764 mmc_hostname(host), ios->clock, ios->bus_mode,
765 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100766 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800767
Linus Walleij04566832010-11-08 21:36:50 -0500768 if (ios->clock > 0)
769 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +0100770 host->ops->set_ios(host, ios);
771}
772
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100773/*
774 * Control chip select pin on a host.
775 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100776void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700777{
Mika Westerberg778e2772011-08-18 15:23:48 +0300778 mmc_host_clk_hold(host);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100779 host->ios.chip_select = mode;
780 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300781 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100785 * Sets the host clock to the highest possible frequency that
786 * is below "hz".
787 */
Mika Westerberg778e2772011-08-18 15:23:48 +0300788static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100789{
790 WARN_ON(hz < host->f_min);
791
792 if (hz > host->f_max)
793 hz = host->f_max;
794
795 host->ios.clock = hz;
796 mmc_set_ios(host);
797}
798
Mika Westerberg778e2772011-08-18 15:23:48 +0300799void mmc_set_clock(struct mmc_host *host, unsigned int hz)
800{
801 mmc_host_clk_hold(host);
802 __mmc_set_clock(host, hz);
803 mmc_host_clk_release(host);
804}
805
Linus Walleij04566832010-11-08 21:36:50 -0500806#ifdef CONFIG_MMC_CLKGATE
807/*
808 * This gates the clock by setting it to 0 Hz.
809 */
810void mmc_gate_clock(struct mmc_host *host)
811{
812 unsigned long flags;
813
814 spin_lock_irqsave(&host->clk_lock, flags);
815 host->clk_old = host->ios.clock;
816 host->ios.clock = 0;
817 host->clk_gated = true;
818 spin_unlock_irqrestore(&host->clk_lock, flags);
819 mmc_set_ios(host);
820}
821
822/*
823 * This restores the clock from gating by using the cached
824 * clock value.
825 */
826void mmc_ungate_clock(struct mmc_host *host)
827{
828 /*
829 * We should previously have gated the clock, so the clock shall
830 * be 0 here! The clock may however be 0 during initialization,
831 * when some request operations are performed before setting
832 * the frequency. When ungate is requested in that situation
833 * we just ignore the call.
834 */
835 if (host->clk_old) {
836 BUG_ON(host->ios.clock);
837 /* This call will also set host->clk_gated to false */
Mika Westerberg778e2772011-08-18 15:23:48 +0300838 __mmc_set_clock(host, host->clk_old);
Linus Walleij04566832010-11-08 21:36:50 -0500839 }
840}
841
842void mmc_set_ungated(struct mmc_host *host)
843{
844 unsigned long flags;
845
846 /*
847 * We've been given a new frequency while the clock is gated,
848 * so make sure we regard this as ungating it.
849 */
850 spin_lock_irqsave(&host->clk_lock, flags);
851 host->clk_gated = false;
852 spin_unlock_irqrestore(&host->clk_lock, flags);
853}
854
855#else
856void mmc_set_ungated(struct mmc_host *host)
857{
858}
859#endif
860
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100861/*
862 * Change the bus mode (open drain/push-pull) of a host.
863 */
864void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
865{
Mika Westerberg778e2772011-08-18 15:23:48 +0300866 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100867 host->ios.bus_mode = mode;
868 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300869 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100870}
871
872/*
873 * Change data bus width of a host.
874 */
875void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
876{
Mika Westerberg778e2772011-08-18 15:23:48 +0300877 mmc_host_clk_hold(host);
Philip Rakity4c4cb172011-05-13 11:17:18 +0530878 host->ios.bus_width = width;
879 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +0300880 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100881}
882
Anton Vorontsov86e82862008-11-26 22:54:17 +0300883/**
884 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
885 * @vdd: voltage (mV)
886 * @low_bits: prefer low bits in boundary cases
887 *
888 * This function returns the OCR bit number according to the provided @vdd
889 * value. If conversion is not possible a negative errno value returned.
890 *
891 * Depending on the @low_bits flag the function prefers low or high OCR bits
892 * on boundary voltages. For example,
893 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
894 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
895 *
896 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
897 */
898static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
899{
900 const int max_bit = ilog2(MMC_VDD_35_36);
901 int bit;
902
903 if (vdd < 1650 || vdd > 3600)
904 return -EINVAL;
905
906 if (vdd >= 1650 && vdd <= 1950)
907 return ilog2(MMC_VDD_165_195);
908
909 if (low_bits)
910 vdd -= 1;
911
912 /* Base 2000 mV, step 100 mV, bit's base 8. */
913 bit = (vdd - 2000) / 100 + 8;
914 if (bit > max_bit)
915 return max_bit;
916 return bit;
917}
918
919/**
920 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
921 * @vdd_min: minimum voltage value (mV)
922 * @vdd_max: maximum voltage value (mV)
923 *
924 * This function returns the OCR mask bits according to the provided @vdd_min
925 * and @vdd_max values. If conversion is not possible the function returns 0.
926 *
927 * Notes wrt boundary cases:
928 * This function sets the OCR bits for all boundary voltages, for example
929 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
930 * MMC_VDD_34_35 mask.
931 */
932u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
933{
934 u32 mask = 0;
935
936 if (vdd_max < vdd_min)
937 return 0;
938
939 /* Prefer high bits for the boundary vdd_max values. */
940 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
941 if (vdd_max < 0)
942 return 0;
943
944 /* Prefer low bits for the boundary vdd_min values. */
945 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
946 if (vdd_min < 0)
947 return 0;
948
949 /* Fill the mask, from max bit to min bit. */
950 while (vdd_max >= vdd_min)
951 mask |= 1 << vdd_max--;
952
953 return mask;
954}
955EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
956
David Brownell5c139412009-03-11 03:30:43 -0800957#ifdef CONFIG_REGULATOR
958
959/**
960 * mmc_regulator_get_ocrmask - return mask of supported voltages
961 * @supply: regulator to use
962 *
963 * This returns either a negative errno, or a mask of voltages that
964 * can be provided to MMC/SD/SDIO devices using the specified voltage
965 * regulator. This would normally be called before registering the
966 * MMC host adapter.
967 */
968int mmc_regulator_get_ocrmask(struct regulator *supply)
969{
970 int result = 0;
971 int count;
972 int i;
973
974 count = regulator_count_voltages(supply);
975 if (count < 0)
976 return count;
977
978 for (i = 0; i < count; i++) {
979 int vdd_uV;
980 int vdd_mV;
981
982 vdd_uV = regulator_list_voltage(supply, i);
983 if (vdd_uV <= 0)
984 continue;
985
986 vdd_mV = vdd_uV / 1000;
987 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
988 }
989
990 return result;
991}
992EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
993
994/**
995 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -0400996 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -0800997 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -0400998 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -0800999 *
1000 * Returns zero on success, else negative errno.
1001 *
1002 * MMC host drivers may use this to enable or disable a regulator using
1003 * a particular supply voltage. This would normally be called from the
1004 * set_ios() method.
1005 */
Linus Walleij99fc5132010-09-29 01:08:27 -04001006int mmc_regulator_set_ocr(struct mmc_host *mmc,
1007 struct regulator *supply,
1008 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -08001009{
1010 int result = 0;
1011 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -08001012
1013 if (vdd_bit) {
1014 int tmp;
1015 int voltage;
1016
1017 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
1018 * bits this regulator doesn't quite support ... don't
1019 * be too picky, most cards and regulators are OK with
1020 * a 0.1V range goof (it's a small error percentage).
1021 */
1022 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
1023 if (tmp == 0) {
1024 min_uV = 1650 * 1000;
1025 max_uV = 1950 * 1000;
1026 } else {
1027 min_uV = 1900 * 1000 + tmp * 100 * 1000;
1028 max_uV = min_uV + 100 * 1000;
1029 }
1030
1031 /* avoid needless changes to this voltage; the regulator
1032 * might not allow this operation
1033 */
1034 voltage = regulator_get_voltage(supply);
1035 if (voltage < 0)
1036 result = voltage;
1037 else if (voltage < min_uV || voltage > max_uV)
1038 result = regulator_set_voltage(supply, min_uV, max_uV);
1039 else
1040 result = 0;
1041
Linus Walleij99fc5132010-09-29 01:08:27 -04001042 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001043 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001044 if (!result)
1045 mmc->regulator_enabled = true;
1046 }
1047 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08001048 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04001049 if (result == 0)
1050 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -08001051 }
1052
Linus Walleij99fc5132010-09-29 01:08:27 -04001053 if (result)
1054 dev_err(mmc_dev(mmc),
1055 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -08001056 return result;
1057}
1058EXPORT_SYMBOL(mmc_regulator_set_ocr);
1059
Linus Walleij99fc5132010-09-29 01:08:27 -04001060#endif /* CONFIG_REGULATOR */
David Brownell5c139412009-03-11 03:30:43 -08001061
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001062/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 * Mask off any voltages we don't support and select
1064 * the lowest voltage
1065 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001066u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 int bit;
1069
1070 ocr &= host->ocr_avail;
1071
1072 bit = ffs(ocr);
1073 if (bit) {
1074 bit -= 1;
1075
Timo Teras63ef7312006-11-02 19:43:27 +01001076 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Mika Westerberg778e2772011-08-18 15:23:48 +03001078 mmc_host_clk_hold(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +01001080 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001081 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 } else {
David Brownellf6e10b82008-12-31 09:50:30 -08001083 pr_warning("%s: host doesn't support card's voltages\n",
1084 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 ocr = 0;
1086 }
1087
1088 return ocr;
1089}
1090
Philip Rakity261bbd42011-05-13 11:17:17 +05301091int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
Arindam Nathf2119df2011-05-05 12:18:57 +05301092{
1093 struct mmc_command cmd = {0};
1094 int err = 0;
1095
1096 BUG_ON(!host);
1097
1098 /*
1099 * Send CMD11 only if the request is to switch the card to
1100 * 1.8V signalling.
1101 */
Philip Rakity261bbd42011-05-13 11:17:17 +05301102 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
Arindam Nathf2119df2011-05-05 12:18:57 +05301103 cmd.opcode = SD_SWITCH_VOLTAGE;
1104 cmd.arg = 0;
1105 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1106
1107 err = mmc_wait_for_cmd(host, &cmd, 0);
1108 if (err)
1109 return err;
1110
1111 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
1112 return -EIO;
1113 }
1114
1115 host->ios.signal_voltage = signal_voltage;
1116
1117 if (host->ops->start_signal_voltage_switch)
1118 err = host->ops->start_signal_voltage_switch(host, &host->ios);
1119
1120 return err;
1121}
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001124 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001126void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Mika Westerberg778e2772011-08-18 15:23:48 +03001128 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001129 host->ios.timing = timing;
1130 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001131 mmc_host_clk_release(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07001132}
1133
1134/*
Arindam Nathd6d50a12011-05-05 12:18:59 +05301135 * Select appropriate driver type for host.
1136 */
1137void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
1138{
Mika Westerberg778e2772011-08-18 15:23:48 +03001139 mmc_host_clk_hold(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301140 host->ios.drv_type = drv_type;
1141 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001142 mmc_host_clk_release(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301143}
1144
1145/*
Russell King45f82452005-12-14 14:57:35 +00001146 * Apply power to the MMC stack. This is a two-stage process.
1147 * First, we enable power to the card without the clock running.
1148 * We then wait a bit for the power to stabilise. Finally,
1149 * enable the bus drivers and clock to the card.
1150 *
1151 * We must _NOT_ enable the clock prior to power stablising.
1152 *
1153 * If a host does all the power sequencing itself, ignore the
1154 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
1156static void mmc_power_up(struct mmc_host *host)
1157{
Balaji Rao500f3562009-09-22 16:44:18 -07001158 int bit;
1159
Mika Westerberg778e2772011-08-18 15:23:48 +03001160 mmc_host_clk_hold(host);
1161
Balaji Rao500f3562009-09-22 16:44:18 -07001162 /* If ocr is set, we use it */
1163 if (host->ocr)
1164 bit = ffs(host->ocr) - 1;
1165 else
1166 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
1168 host->ios.vdd = bit;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001169 if (mmc_host_is_spi(host))
David Brownellaf517152007-08-08 09:11:32 -07001170 host->ios.chip_select = MMC_CS_HIGH;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001171 else
David Brownellaf517152007-08-08 09:11:32 -07001172 host->ios.chip_select = MMC_CS_DONTCARE;
Stefan Nilsson XK44669032011-09-15 17:50:38 +02001173 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001175 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001176 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001177 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001179 /*
1180 * This delay should be sufficient to allow the power supply
1181 * to reach the minimum voltage.
1182 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001183 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001185 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01001188 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001190 /*
1191 * This delay must be at least 74 clock sizes, or 1 ms, or the
1192 * time required to reach a stable voltage.
1193 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001194 mmc_delay(10);
Mika Westerberg778e2772011-08-18 15:23:48 +03001195
1196 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001199void mmc_power_off(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200{
Mika Westerberg778e2772011-08-18 15:23:48 +03001201 mmc_host_clk_hold(host);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 host->ios.clock = 0;
1204 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001205
1206 /*
1207 * Reset ocr mask to be the highest possible voltage supported for
1208 * this mmc host. This value will be used at next power up.
1209 */
1210 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1211
David Brownellaf517152007-08-08 09:11:32 -07001212 if (!mmc_host_is_spi(host)) {
1213 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1214 host->ios.chip_select = MMC_CS_DONTCARE;
1215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001217 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001218 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001219 mmc_set_ios(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03001220
Daniel Drake041beb12011-09-07 10:22:09 +01001221 /*
1222 * Some configurations, such as the 802.11 SDIO card in the OLPC
1223 * XO-1.5, require a short delay after poweroff before the card
1224 * can be successfully turned on again.
1225 */
1226 mmc_delay(1);
1227
Mika Westerberg778e2772011-08-18 15:23:48 +03001228 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231/*
Adrian Bunk39361852007-07-25 00:40:58 +02001232 * Cleanup when the last reference to the bus operator is dropped.
1233 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001234static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001235{
1236 BUG_ON(!host);
1237 BUG_ON(host->bus_refs);
1238 BUG_ON(!host->bus_dead);
1239
1240 host->bus_ops = NULL;
1241}
1242
1243/*
1244 * Increase reference count of bus operator
1245 */
1246static inline void mmc_bus_get(struct mmc_host *host)
1247{
1248 unsigned long flags;
1249
1250 spin_lock_irqsave(&host->lock, flags);
1251 host->bus_refs++;
1252 spin_unlock_irqrestore(&host->lock, flags);
1253}
1254
1255/*
1256 * Decrease reference count of bus operator and free it if
1257 * it is the last reference.
1258 */
1259static inline void mmc_bus_put(struct mmc_host *host)
1260{
1261 unsigned long flags;
1262
1263 spin_lock_irqsave(&host->lock, flags);
1264 host->bus_refs--;
1265 if ((host->bus_refs == 0) && host->bus_ops)
1266 __mmc_release_bus(host);
1267 spin_unlock_irqrestore(&host->lock, flags);
1268}
1269
1270/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001271 * Assign a mmc bus handler to a host. Only one bus handler may control a
1272 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001274void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001276 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001278 BUG_ON(!host);
1279 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001280
Pierre Ossmand84075c82007-08-09 13:23:56 +02001281 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001282
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001283 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001284
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001285 BUG_ON(host->bus_ops);
1286 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001287
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001288 host->bus_ops = ops;
1289 host->bus_refs = 1;
1290 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001292 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295/*
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001296 * Remove the current bus handler from a host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001298void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001300 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001302 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303
Pierre Ossmand84075c82007-08-09 13:23:56 +02001304 WARN_ON(!host->claimed);
1305 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001307 spin_lock_irqsave(&host->lock, flags);
1308
1309 host->bus_dead = 1;
1310
1311 spin_unlock_irqrestore(&host->lock, flags);
1312
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001313 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314}
1315
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316/**
1317 * mmc_detect_change - process change of state on a MMC socket
1318 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001319 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001321 * MMC drivers should call this when they detect a card has been
1322 * inserted or removed. The MMC layer will confirm that any
1323 * present card is still functional, and initialize any newly
1324 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001326void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001328#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001329 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001330 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001331 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001332 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001333#endif
1334
David Howellsc4028952006-11-22 14:57:56 +00001335 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336}
1337
1338EXPORT_SYMBOL(mmc_detect_change);
1339
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001340void mmc_init_erase(struct mmc_card *card)
1341{
1342 unsigned int sz;
1343
1344 if (is_power_of_2(card->erase_size))
1345 card->erase_shift = ffs(card->erase_size) - 1;
1346 else
1347 card->erase_shift = 0;
1348
1349 /*
1350 * It is possible to erase an arbitrarily large area of an SD or MMC
1351 * card. That is not desirable because it can take a long time
1352 * (minutes) potentially delaying more important I/O, and also the
1353 * timeout calculations become increasingly hugely over-estimated.
1354 * Consequently, 'pref_erase' is defined as a guide to limit erases
1355 * to that size and alignment.
1356 *
1357 * For SD cards that define Allocation Unit size, limit erases to one
1358 * Allocation Unit at a time. For MMC cards that define High Capacity
1359 * Erase Size, whether it is switched on or not, limit to that size.
1360 * Otherwise just have a stab at a good value. For modern cards it
1361 * will end up being 4MiB. Note that if the value is too small, it
1362 * can end up taking longer to erase.
1363 */
1364 if (mmc_card_sd(card) && card->ssr.au) {
1365 card->pref_erase = card->ssr.au;
1366 card->erase_shift = ffs(card->ssr.au) - 1;
1367 } else if (card->ext_csd.hc_erase_size) {
1368 card->pref_erase = card->ext_csd.hc_erase_size;
1369 } else {
1370 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1371 if (sz < 128)
1372 card->pref_erase = 512 * 1024 / 512;
1373 else if (sz < 512)
1374 card->pref_erase = 1024 * 1024 / 512;
1375 else if (sz < 1024)
1376 card->pref_erase = 2 * 1024 * 1024 / 512;
1377 else
1378 card->pref_erase = 4 * 1024 * 1024 / 512;
1379 if (card->pref_erase < card->erase_size)
1380 card->pref_erase = card->erase_size;
1381 else {
1382 sz = card->pref_erase % card->erase_size;
1383 if (sz)
1384 card->pref_erase += card->erase_size - sz;
1385 }
1386 }
1387}
1388
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001389static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1390 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001391{
1392 unsigned int erase_timeout;
1393
1394 if (card->ext_csd.erase_group_def & 1) {
1395 /* High Capacity Erase Group Size uses HC timeouts */
1396 if (arg == MMC_TRIM_ARG)
1397 erase_timeout = card->ext_csd.trim_timeout;
1398 else
1399 erase_timeout = card->ext_csd.hc_erase_timeout;
1400 } else {
1401 /* CSD Erase Group Size uses write timeout */
1402 unsigned int mult = (10 << card->csd.r2w_factor);
1403 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1404 unsigned int timeout_us;
1405
1406 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1407 if (card->csd.tacc_ns < 1000000)
1408 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1409 else
1410 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1411
1412 /*
1413 * ios.clock is only a target. The real clock rate might be
1414 * less but not that much less, so fudge it by multiplying by 2.
1415 */
1416 timeout_clks <<= 1;
1417 timeout_us += (timeout_clks * 1000) /
Adrian Hunter4cf8c6d2011-06-23 13:40:27 +03001418 (mmc_host_clk_rate(card->host) / 1000);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001419
1420 erase_timeout = timeout_us / 1000;
1421
1422 /*
1423 * Theoretically, the calculation could underflow so round up
1424 * to 1ms in that case.
1425 */
1426 if (!erase_timeout)
1427 erase_timeout = 1;
1428 }
1429
1430 /* Multiplier for secure operations */
1431 if (arg & MMC_SECURE_ARGS) {
1432 if (arg == MMC_SECURE_ERASE_ARG)
1433 erase_timeout *= card->ext_csd.sec_erase_mult;
1434 else
1435 erase_timeout *= card->ext_csd.sec_trim_mult;
1436 }
1437
1438 erase_timeout *= qty;
1439
1440 /*
1441 * Ensure at least a 1 second timeout for SPI as per
1442 * 'mmc_set_data_timeout()'
1443 */
1444 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1445 erase_timeout = 1000;
1446
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001447 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001448}
1449
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001450static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1451 unsigned int arg,
1452 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001453{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001454 unsigned int erase_timeout;
1455
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001456 if (card->ssr.erase_timeout) {
1457 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001458 erase_timeout = card->ssr.erase_timeout * qty +
1459 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001460 } else {
1461 /*
1462 * Erase timeout not specified in SD Status Register (SSR) so
1463 * use 250ms per write block.
1464 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001465 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001466 }
1467
1468 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001469 if (erase_timeout < 1000)
1470 erase_timeout = 1000;
1471
1472 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001473}
1474
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001475static unsigned int mmc_erase_timeout(struct mmc_card *card,
1476 unsigned int arg,
1477 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001478{
1479 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001480 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001481 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001482 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001483}
1484
1485static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1486 unsigned int to, unsigned int arg)
1487{
Chris Ball1278dba2011-04-13 23:40:30 -04001488 struct mmc_command cmd = {0};
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001489 unsigned int qty = 0;
1490 int err;
1491
1492 /*
1493 * qty is used to calculate the erase timeout which depends on how many
1494 * erase groups (or allocation units in SD terminology) are affected.
1495 * We count erasing part of an erase group as one erase group.
1496 * For SD, the allocation units are always a power of 2. For MMC, the
1497 * erase group size is almost certainly also power of 2, but it does not
1498 * seem to insist on that in the JEDEC standard, so we fall back to
1499 * division in that case. SD may not specify an allocation unit size,
1500 * in which case the timeout is based on the number of write blocks.
1501 *
1502 * Note that the timeout for secure trim 2 will only be correct if the
1503 * number of erase groups specified is the same as the total of all
1504 * preceding secure trim 1 commands. Since the power may have been
1505 * lost since the secure trim 1 commands occurred, it is generally
1506 * impossible to calculate the secure trim 2 timeout correctly.
1507 */
1508 if (card->erase_shift)
1509 qty += ((to >> card->erase_shift) -
1510 (from >> card->erase_shift)) + 1;
1511 else if (mmc_card_sd(card))
1512 qty += to - from + 1;
1513 else
1514 qty += ((to / card->erase_size) -
1515 (from / card->erase_size)) + 1;
1516
1517 if (!mmc_card_blockaddr(card)) {
1518 from <<= 9;
1519 to <<= 9;
1520 }
1521
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001522 if (mmc_card_sd(card))
1523 cmd.opcode = SD_ERASE_WR_BLK_START;
1524 else
1525 cmd.opcode = MMC_ERASE_GROUP_START;
1526 cmd.arg = from;
1527 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1528 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1529 if (err) {
1530 printk(KERN_ERR "mmc_erase: group start error %d, "
1531 "status %#x\n", err, cmd.resp[0]);
1532 err = -EINVAL;
1533 goto out;
1534 }
1535
1536 memset(&cmd, 0, sizeof(struct mmc_command));
1537 if (mmc_card_sd(card))
1538 cmd.opcode = SD_ERASE_WR_BLK_END;
1539 else
1540 cmd.opcode = MMC_ERASE_GROUP_END;
1541 cmd.arg = to;
1542 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1543 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1544 if (err) {
1545 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1546 err, cmd.resp[0]);
1547 err = -EINVAL;
1548 goto out;
1549 }
1550
1551 memset(&cmd, 0, sizeof(struct mmc_command));
1552 cmd.opcode = MMC_ERASE;
1553 cmd.arg = arg;
1554 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001555 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001556 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1557 if (err) {
1558 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1559 err, cmd.resp[0]);
1560 err = -EIO;
1561 goto out;
1562 }
1563
1564 if (mmc_host_is_spi(card->host))
1565 goto out;
1566
1567 do {
1568 memset(&cmd, 0, sizeof(struct mmc_command));
1569 cmd.opcode = MMC_SEND_STATUS;
1570 cmd.arg = card->rca << 16;
1571 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1572 /* Do not retry else we can't see errors */
1573 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1574 if (err || (cmd.resp[0] & 0xFDF92000)) {
1575 printk(KERN_ERR "error %d requesting status %#x\n",
1576 err, cmd.resp[0]);
1577 err = -EIO;
1578 goto out;
1579 }
1580 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
Jaehoon Chung7435bb72011-08-10 18:46:28 +09001581 R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001582out:
1583 return err;
1584}
1585
1586/**
1587 * mmc_erase - erase sectors.
1588 * @card: card to erase
1589 * @from: first sector to erase
1590 * @nr: number of sectors to erase
1591 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1592 *
1593 * Caller must claim host before calling this function.
1594 */
1595int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1596 unsigned int arg)
1597{
1598 unsigned int rem, to = from + nr;
1599
1600 if (!(card->host->caps & MMC_CAP_ERASE) ||
1601 !(card->csd.cmdclass & CCC_ERASE))
1602 return -EOPNOTSUPP;
1603
1604 if (!card->erase_size)
1605 return -EOPNOTSUPP;
1606
1607 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1608 return -EOPNOTSUPP;
1609
1610 if ((arg & MMC_SECURE_ARGS) &&
1611 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1612 return -EOPNOTSUPP;
1613
1614 if ((arg & MMC_TRIM_ARGS) &&
1615 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1616 return -EOPNOTSUPP;
1617
1618 if (arg == MMC_SECURE_ERASE_ARG) {
1619 if (from % card->erase_size || nr % card->erase_size)
1620 return -EINVAL;
1621 }
1622
1623 if (arg == MMC_ERASE_ARG) {
1624 rem = from % card->erase_size;
1625 if (rem) {
1626 rem = card->erase_size - rem;
1627 from += rem;
1628 if (nr > rem)
1629 nr -= rem;
1630 else
1631 return 0;
1632 }
1633 rem = nr % card->erase_size;
1634 if (rem)
1635 nr -= rem;
1636 }
1637
1638 if (nr == 0)
1639 return 0;
1640
1641 to = from + nr;
1642
1643 if (to <= from)
1644 return -EINVAL;
1645
1646 /* 'from' and 'to' are inclusive */
1647 to -= 1;
1648
1649 return mmc_do_erase(card, from, to, arg);
1650}
1651EXPORT_SYMBOL(mmc_erase);
1652
1653int mmc_can_erase(struct mmc_card *card)
1654{
1655 if ((card->host->caps & MMC_CAP_ERASE) &&
1656 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1657 return 1;
1658 return 0;
1659}
1660EXPORT_SYMBOL(mmc_can_erase);
1661
1662int mmc_can_trim(struct mmc_card *card)
1663{
1664 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1665 return 1;
1666 return 0;
1667}
1668EXPORT_SYMBOL(mmc_can_trim);
1669
1670int mmc_can_secure_erase_trim(struct mmc_card *card)
1671{
1672 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1673 return 1;
1674 return 0;
1675}
1676EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1677
1678int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1679 unsigned int nr)
1680{
1681 if (!card->erase_size)
1682 return 0;
1683 if (from % card->erase_size || nr % card->erase_size)
1684 return 0;
1685 return 1;
1686}
1687EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Adrian Huntere056a1b2011-06-28 17:16:02 +03001689static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
1690 unsigned int arg)
1691{
1692 struct mmc_host *host = card->host;
1693 unsigned int max_discard, x, y, qty = 0, max_qty, timeout;
1694 unsigned int last_timeout = 0;
1695
1696 if (card->erase_shift)
1697 max_qty = UINT_MAX >> card->erase_shift;
1698 else if (mmc_card_sd(card))
1699 max_qty = UINT_MAX;
1700 else
1701 max_qty = UINT_MAX / card->erase_size;
1702
1703 /* Find the largest qty with an OK timeout */
1704 do {
1705 y = 0;
1706 for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) {
1707 timeout = mmc_erase_timeout(card, arg, qty + x);
1708 if (timeout > host->max_discard_to)
1709 break;
1710 if (timeout < last_timeout)
1711 break;
1712 last_timeout = timeout;
1713 y = x;
1714 }
1715 qty += y;
1716 } while (y);
1717
1718 if (!qty)
1719 return 0;
1720
1721 if (qty == 1)
1722 return 1;
1723
1724 /* Convert qty to sectors */
1725 if (card->erase_shift)
1726 max_discard = --qty << card->erase_shift;
1727 else if (mmc_card_sd(card))
1728 max_discard = qty;
1729 else
1730 max_discard = --qty * card->erase_size;
1731
1732 return max_discard;
1733}
1734
1735unsigned int mmc_calc_max_discard(struct mmc_card *card)
1736{
1737 struct mmc_host *host = card->host;
1738 unsigned int max_discard, max_trim;
1739
1740 if (!host->max_discard_to)
1741 return UINT_MAX;
1742
1743 /*
1744 * Without erase_group_def set, MMC erase timeout depends on clock
1745 * frequence which can change. In that case, the best choice is
1746 * just the preferred erase size.
1747 */
1748 if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1))
1749 return card->pref_erase;
1750
1751 max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG);
1752 if (mmc_can_trim(card)) {
1753 max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG);
1754 if (max_trim < max_discard)
1755 max_discard = max_trim;
1756 } else if (max_discard < card->erase_size) {
1757 max_discard = 0;
1758 }
1759 pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n",
1760 mmc_hostname(host), max_discard, host->max_discard_to);
1761 return max_discard;
1762}
1763EXPORT_SYMBOL(mmc_calc_max_discard);
1764
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001765int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1766{
Chris Ball1278dba2011-04-13 23:40:30 -04001767 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001768
1769 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1770 return 0;
1771
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001772 cmd.opcode = MMC_SET_BLOCKLEN;
1773 cmd.arg = blocklen;
1774 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1775 return mmc_wait_for_cmd(card->host, &cmd, 5);
1776}
1777EXPORT_SYMBOL(mmc_set_blocklen);
1778
Andy Ross807e8e42011-01-03 10:36:56 -08001779static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1780{
1781 host->f_init = freq;
1782
1783#ifdef CONFIG_MMC_DEBUG
1784 pr_info("%s: %s: trying to init card at %u Hz\n",
1785 mmc_hostname(host), __func__, host->f_init);
1786#endif
1787 mmc_power_up(host);
Philip Rakity2f94e552011-02-13 23:12:28 -08001788
1789 /*
1790 * sdio_reset sends CMD52 to reset card. Since we do not know
1791 * if the card is being re-initialized, just send it. CMD52
1792 * should be ignored by SD/eMMC cards.
1793 */
Andy Ross807e8e42011-01-03 10:36:56 -08001794 sdio_reset(host);
1795 mmc_go_idle(host);
1796
1797 mmc_send_if_cond(host, host->ocr_avail);
1798
1799 /* Order's important: probe SDIO, then SD, then MMC */
1800 if (!mmc_attach_sdio(host))
1801 return 0;
1802 if (!mmc_attach_sd(host))
1803 return 0;
1804 if (!mmc_attach_mmc(host))
1805 return 0;
1806
1807 mmc_power_off(host);
1808 return -EIO;
1809}
1810
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001811void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Andy Ross807e8e42011-01-03 10:36:56 -08001813 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
David Howellsc4028952006-11-22 14:57:56 +00001814 struct mmc_host *host =
1815 container_of(work, struct mmc_host, detect.work);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001816 int i;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001817
Andy Ross807e8e42011-01-03 10:36:56 -08001818 if (host->rescan_disable)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001819 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001821 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02001823 /*
1824 * if there is a _removable_ card registered, check whether it is
1825 * still present
1826 */
1827 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
Ohad Ben-Cohenbad3bab2011-03-08 23:32:02 +02001828 && !(host->caps & MMC_CAP_NONREMOVABLE))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001829 host->bus_ops->detect(host);
1830
Chris Ballc5841792011-01-04 12:20:22 -05001831 /*
1832 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1833 * the card is no longer present.
1834 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001835 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001836 mmc_bus_get(host);
1837
1838 /* if there still is a card present, stop here */
1839 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001840 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001841 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001843
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001844 /*
1845 * Only we can add a new handler, so it's safe to
1846 * release the lock here.
1847 */
1848 mmc_bus_put(host);
1849
1850 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1851 goto out;
1852
Andy Ross807e8e42011-01-03 10:36:56 -08001853 mmc_claim_host(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001854 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
Andy Ross807e8e42011-01-03 10:36:56 -08001855 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1856 break;
Jaehoon Chung06b22332011-05-12 17:18:59 +09001857 if (freqs[i] <= host->f_min)
Andy Ross807e8e42011-01-03 10:36:56 -08001858 break;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001859 }
Andy Ross807e8e42011-01-03 10:36:56 -08001860 mmc_release_host(host);
1861
1862 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04001863 if (host->caps & MMC_CAP_NEEDS_POLL)
1864 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001867void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001869 mmc_power_off(host);
1870 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871}
1872
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001873void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001875#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001876 unsigned long flags;
1877 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001878 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001879 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001880#endif
1881
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001882 if (host->caps & MMC_CAP_DISABLE)
1883 cancel_delayed_work(&host->disable);
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01001884 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001885 mmc_flush_scheduled_work();
1886
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001887 /* clear pm flags now and let card drivers set them as needed */
1888 host->pm_flags = 0;
1889
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001890 mmc_bus_get(host);
1891 if (host->bus_ops && !host->bus_dead) {
1892 if (host->bus_ops->remove)
1893 host->bus_ops->remove(host);
1894
1895 mmc_claim_host(host);
1896 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04001897 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001898 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07001899 mmc_bus_put(host);
1900 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001902 mmc_bus_put(host);
1903
1904 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905
1906 mmc_power_off(host);
1907}
1908
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001909int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001910{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001911 int ret = 0;
1912
Daniel Drakebb9cab92011-07-17 16:38:41 +01001913#ifdef CONFIG_MMC_DEBUG
1914 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
1915#endif
1916
Adrian Huntereae1aee2009-09-22 16:44:33 -07001917 mmc_bus_get(host);
1918
1919 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1920 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001921 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001922 }
1923
1924 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001925 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001926
1927 mmc_bus_put(host);
1928
1929 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001930
1931 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001932}
1933EXPORT_SYMBOL(mmc_power_save_host);
1934
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001935int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001936{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001937 int ret;
1938
Daniel Drakebb9cab92011-07-17 16:38:41 +01001939#ifdef CONFIG_MMC_DEBUG
1940 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
1941#endif
1942
Adrian Huntereae1aee2009-09-22 16:44:33 -07001943 mmc_bus_get(host);
1944
1945 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1946 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001947 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001948 }
1949
1950 mmc_power_up(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001951 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001952
1953 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001954
1955 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001956}
1957EXPORT_SYMBOL(mmc_power_restore_host);
1958
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001959int mmc_card_awake(struct mmc_host *host)
1960{
1961 int err = -ENOSYS;
1962
1963 mmc_bus_get(host);
1964
1965 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1966 err = host->bus_ops->awake(host);
1967
1968 mmc_bus_put(host);
1969
1970 return err;
1971}
1972EXPORT_SYMBOL(mmc_card_awake);
1973
1974int mmc_card_sleep(struct mmc_host *host)
1975{
1976 int err = -ENOSYS;
1977
1978 mmc_bus_get(host);
1979
1980 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1981 err = host->bus_ops->sleep(host);
1982
1983 mmc_bus_put(host);
1984
1985 return err;
1986}
1987EXPORT_SYMBOL(mmc_card_sleep);
1988
1989int mmc_card_can_sleep(struct mmc_host *host)
1990{
1991 struct mmc_card *card = host->card;
1992
1993 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1994 return 1;
1995 return 0;
1996}
1997EXPORT_SYMBOL(mmc_card_can_sleep);
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999#ifdef CONFIG_PM
2000
2001/**
2002 * mmc_suspend_host - suspend a host
2003 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07002005int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002007 int err = 0;
2008
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002009 if (host->caps & MMC_CAP_DISABLE)
2010 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02002011 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002012 mmc_flush_scheduled_work();
2013
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002014 mmc_bus_get(host);
2015 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002016 if (host->bus_ops->suspend)
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002017 err = host->bus_ops->suspend(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02002018 if (err == -ENOSYS || !host->bus_ops->resume) {
2019 /*
2020 * We simply "remove" the card in this case.
2021 * It will be redetected on resume.
2022 */
2023 if (host->bus_ops->remove)
2024 host->bus_ops->remove(host);
2025 mmc_claim_host(host);
2026 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002027 mmc_power_off(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02002028 mmc_release_host(host);
2029 host->pm_flags = 0;
2030 err = 0;
2031 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02002032 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002033 mmc_bus_put(host);
2034
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002035 if (!err && !mmc_card_keep_power(host))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002036 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002038 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039}
2040
2041EXPORT_SYMBOL(mmc_suspend_host);
2042
2043/**
2044 * mmc_resume_host - resume a previously suspended host
2045 * @host: mmc host
2046 */
2047int mmc_resume_host(struct mmc_host *host)
2048{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002049 int err = 0;
2050
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002051 mmc_bus_get(host);
2052 if (host->bus_ops && !host->bus_dead) {
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03002053 if (!mmc_card_keep_power(host)) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002054 mmc_power_up(host);
2055 mmc_select_voltage(host, host->ocr);
Ohad Ben-Cohene5945732010-11-28 07:21:30 +02002056 /*
2057 * Tell runtime PM core we just powered up the card,
2058 * since it still believes the card is powered off.
2059 * Note that currently runtime PM is only enabled
2060 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
2061 */
2062 if (mmc_card_sdio(host->card) &&
2063 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
2064 pm_runtime_disable(&host->card->dev);
2065 pm_runtime_set_active(&host->card->dev);
2066 pm_runtime_enable(&host->card->dev);
2067 }
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08002068 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002069 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002070 err = host->bus_ops->resume(host);
2071 if (err) {
2072 printk(KERN_WARNING "%s: error %d during resume "
2073 "(card was removed?)\n",
2074 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002075 err = 0;
2076 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002077 }
Eliad Pellera8e6df72011-05-09 11:32:31 +03002078 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02002079 mmc_bus_put(host);
2080
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07002081 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083EXPORT_SYMBOL(mmc_resume_host);
2084
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002085/* Do the card removal on suspend if card is assumed removeable
2086 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
2087 to sync the card.
2088*/
2089int mmc_pm_notify(struct notifier_block *notify_block,
2090 unsigned long mode, void *unused)
2091{
2092 struct mmc_host *host = container_of(
2093 notify_block, struct mmc_host, pm_notify);
2094 unsigned long flags;
2095
2096
2097 switch (mode) {
2098 case PM_HIBERNATION_PREPARE:
2099 case PM_SUSPEND_PREPARE:
2100
2101 spin_lock_irqsave(&host->lock, flags);
2102 host->rescan_disable = 1;
2103 spin_unlock_irqrestore(&host->lock, flags);
2104 cancel_delayed_work_sync(&host->detect);
2105
2106 if (!host->bus_ops || host->bus_ops->suspend)
2107 break;
2108
2109 mmc_claim_host(host);
2110
2111 if (host->bus_ops->remove)
2112 host->bus_ops->remove(host);
2113
2114 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04002115 mmc_power_off(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002116 mmc_release_host(host);
2117 host->pm_flags = 0;
2118 break;
2119
2120 case PM_POST_SUSPEND:
2121 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01002122 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07002123
2124 spin_lock_irqsave(&host->lock, flags);
2125 host->rescan_disable = 0;
2126 spin_unlock_irqrestore(&host->lock, flags);
2127 mmc_detect_change(host, 0);
2128
2129 }
2130
2131 return 0;
2132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133#endif
2134
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002135static int __init mmc_init(void)
2136{
2137 int ret;
2138
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01002139 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002140 if (!workqueue)
2141 return -ENOMEM;
2142
2143 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002144 if (ret)
2145 goto destroy_workqueue;
2146
2147 ret = mmc_register_host_class();
2148 if (ret)
2149 goto unregister_bus;
2150
2151 ret = sdio_register_bus();
2152 if (ret)
2153 goto unregister_host_class;
2154
2155 return 0;
2156
2157unregister_host_class:
2158 mmc_unregister_host_class();
2159unregister_bus:
2160 mmc_unregister_bus();
2161destroy_workqueue:
2162 destroy_workqueue(workqueue);
2163
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002164 return ret;
2165}
2166
2167static void __exit mmc_exit(void)
2168{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02002169 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002170 mmc_unregister_host_class();
2171 mmc_unregister_bus();
2172 destroy_workqueue(workqueue);
2173}
2174
Nicolas Pitre26074962007-06-16 02:07:53 -04002175subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02002176module_exit(mmc_exit);
2177
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178MODULE_LICENSE("GPL");