blob: c5e3c9bf6fddd26b7fe0269a8024f430dab5236c [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <linux/mmc/card.h>
27#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010028#include <linux/mmc/mmc.h>
29#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010031#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020032#include "bus.h"
33#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020034#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010035
36#include "mmc_ops.h"
37#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020038#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Pierre Ossmanffce2e72007-05-19 14:32:22 +020040static struct workqueue_struct *workqueue;
41
42/*
David Brownellaf517152007-08-08 09:11:32 -070043 * Enabling software CRCs on the data blocks can be a significant (30%)
44 * performance cost, and for other reasons may not always be desired.
45 * So we allow it it to be disabled.
46 */
47int use_spi_crc = 1;
48module_param(use_spi_crc, bool, 0);
49
50/*
Ben Hutchingsbd68e082009-12-14 18:01:29 -080051 * We normally treat cards as removed during suspend if they are not
52 * known to be on a non-removable bus, to avoid the risk of writing
53 * back data to a different card after resume. Allow this to be
54 * overridden if necessary.
55 */
56#ifdef CONFIG_MMC_UNSAFE_RESUME
57int mmc_assume_removable;
58#else
59int mmc_assume_removable = 1;
60#endif
Matt Fleming71d7d3d2010-09-27 09:42:19 +010061EXPORT_SYMBOL(mmc_assume_removable);
Ben Hutchingsbd68e082009-12-14 18:01:29 -080062module_param_named(removable, mmc_assume_removable, bool, 0644);
63MODULE_PARM_DESC(
64 removable,
65 "MMC/SD cards are removable and may be removed during suspend");
66
67/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020068 * Internal function. Schedule delayed work in the MMC work queue.
69 */
70static int mmc_schedule_delayed_work(struct delayed_work *work,
71 unsigned long delay)
72{
73 return queue_delayed_work(workqueue, work, delay);
74}
75
76/*
77 * Internal function. Flush all scheduled work from the MMC work queue.
78 */
79static void mmc_flush_scheduled_work(void)
80{
81 flush_workqueue(workqueue);
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/**
Russell Kingfe10c6a2006-05-04 13:51:45 +010085 * mmc_request_done - finish processing an MMC request
86 * @host: MMC host which completed request
87 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 *
89 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010090 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 */
92void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
93{
94 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010095 int err = cmd->error;
96
David Brownellaf517152007-08-08 09:11:32 -070097 if (err && cmd->retries && mmc_host_is_spi(host)) {
98 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
99 cmd->retries = 0;
100 }
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (err && cmd->retries) {
Pierre Ossmane4d21702007-07-24 21:46:49 +0200103 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
104 mmc_hostname(host), cmd->opcode, err);
105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 cmd->retries--;
107 cmd->error = 0;
108 host->ops->request(host, mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +0200109 } else {
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200110 led_trigger_event(host->led, LED_OFF);
111
Pierre Ossmane4d21702007-07-24 21:46:49 +0200112 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
113 mmc_hostname(host), cmd->opcode, err,
114 cmd->resp[0], cmd->resp[1],
115 cmd->resp[2], cmd->resp[3]);
116
117 if (mrq->data) {
118 pr_debug("%s: %d bytes transferred: %d\n",
119 mmc_hostname(host),
120 mrq->data->bytes_xfered, mrq->data->error);
121 }
122
123 if (mrq->stop) {
124 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
125 mmc_hostname(host), mrq->stop->opcode,
126 mrq->stop->error,
127 mrq->stop->resp[0], mrq->stop->resp[1],
128 mrq->stop->resp[2], mrq->stop->resp[3]);
129 }
130
131 if (mrq->done)
132 mrq->done(mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134}
135
136EXPORT_SYMBOL(mmc_request_done);
137
Adrian Bunk39361852007-07-25 00:40:58 +0200138static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
140{
Pierre Ossman976d9272007-04-13 22:47:01 +0200141#ifdef CONFIG_MMC_DEBUG
142 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200143 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200144#endif
145
Russell King920e70c2006-05-04 18:22:51 +0100146 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
147 mmc_hostname(host), mrq->cmd->opcode,
148 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Pierre Ossmane4d21702007-07-24 21:46:49 +0200150 if (mrq->data) {
151 pr_debug("%s: blksz %d blocks %d flags %08x "
152 "tsac %d ms nsac %d\n",
153 mmc_hostname(host), mrq->data->blksz,
154 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200155 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200156 mrq->data->timeout_clks);
157 }
158
159 if (mrq->stop) {
160 pr_debug("%s: CMD%u arg %08x flags %08x\n",
161 mmc_hostname(host), mrq->stop->opcode,
162 mrq->stop->arg, mrq->stop->flags);
163 }
164
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100165 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200167 led_trigger_event(host->led, LED_FULL);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 mrq->cmd->error = 0;
170 mrq->cmd->mrq = mrq;
171 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100172 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100173 BUG_ON(mrq->data->blocks > host->max_blk_count);
174 BUG_ON(mrq->data->blocks * mrq->data->blksz >
175 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100176
Pierre Ossman976d9272007-04-13 22:47:01 +0200177#ifdef CONFIG_MMC_DEBUG
178 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200179 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
180 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200181 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
182#endif
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 mrq->cmd->data = mrq->data;
185 mrq->data->error = 0;
186 mrq->data->mrq = mrq;
187 if (mrq->stop) {
188 mrq->data->stop = mrq->stop;
189 mrq->stop->error = 0;
190 mrq->stop->mrq = mrq;
191 }
192 }
193 host->ops->request(host, mrq);
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static void mmc_wait_done(struct mmc_request *mrq)
197{
198 complete(mrq->done_data);
199}
200
Pierre Ossman67a61c42007-07-11 20:22:11 +0200201/**
202 * mmc_wait_for_req - start a request and wait for completion
203 * @host: MMC host to start command
204 * @mrq: MMC request to start
205 *
206 * Start a new MMC custom command request for a host, and wait
207 * for the command to complete. Does not attempt to parse the
208 * response.
209 */
210void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700212 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 mrq->done_data = &complete;
215 mrq->done = mmc_wait_done;
216
217 mmc_start_request(host, mrq);
218
219 wait_for_completion(&complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
222EXPORT_SYMBOL(mmc_wait_for_req);
223
224/**
225 * mmc_wait_for_cmd - start a command and wait for completion
226 * @host: MMC host to start command
227 * @cmd: MMC command to start
228 * @retries: maximum number of retries
229 *
230 * Start a new MMC command for a host, and wait for the command
231 * to complete. Return any error that occurred while the command
232 * was executing. Do not attempt to parse the response.
233 */
234int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
235{
236 struct mmc_request mrq;
237
Pierre Ossmand84075c82007-08-09 13:23:56 +0200238 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 memset(&mrq, 0, sizeof(struct mmc_request));
241
242 memset(cmd->resp, 0, sizeof(cmd->resp));
243 cmd->retries = retries;
244
245 mrq.cmd = cmd;
246 cmd->data = NULL;
247
248 mmc_wait_for_req(host, &mrq);
249
250 return cmd->error;
251}
252
253EXPORT_SYMBOL(mmc_wait_for_cmd);
254
Pierre Ossman335eadf2005-09-06 15:18:50 -0700255/**
Russell Kingd773d722006-09-07 15:57:12 +0100256 * mmc_set_data_timeout - set the timeout for a data command
257 * @data: data phase for command
258 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200259 *
260 * Computes the data timeout parameters according to the
261 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100262 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200263void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100264{
265 unsigned int mult;
266
267 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200268 * SDIO cards only define an upper 1 s limit on access.
269 */
270 if (mmc_card_sdio(card)) {
271 data->timeout_ns = 1000000000;
272 data->timeout_clks = 0;
273 return;
274 }
275
276 /*
Russell Kingd773d722006-09-07 15:57:12 +0100277 * SD cards use a 100 multiplier rather than 10
278 */
279 mult = mmc_card_sd(card) ? 100 : 10;
280
281 /*
282 * Scale up the multiplier (and therefore the timeout) by
283 * the r2w factor for writes.
284 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200285 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100286 mult <<= card->csd.r2w_factor;
287
288 data->timeout_ns = card->csd.tacc_ns * mult;
289 data->timeout_clks = card->csd.tacc_clks * mult;
290
291 /*
292 * SD cards also have an upper limit on the timeout.
293 */
294 if (mmc_card_sd(card)) {
295 unsigned int timeout_us, limit_us;
296
297 timeout_us = data->timeout_ns / 1000;
298 timeout_us += data->timeout_clks * 1000 /
299 (card->host->ios.clock / 1000);
300
Pierre Ossmanb146d262007-07-24 19:16:54 +0200301 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100302 /*
303 * The limit is really 250 ms, but that is
304 * insufficient for some crappy cards.
305 */
306 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100307 else
308 limit_us = 100000;
309
Philip Langdalefba68bd2007-01-04 06:57:32 -0800310 /*
311 * SDHC cards always use these fixed values.
312 */
313 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100314 data->timeout_ns = limit_us * 1000;
315 data->timeout_clks = 0;
316 }
317 }
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100318 /*
319 * Some cards need very high timeouts if driven in SPI mode.
320 * The worst observed timeout was 900ms after writing a
321 * continuous stream of data until the internal logic
322 * overflowed.
323 */
324 if (mmc_host_is_spi(card->host)) {
325 if (data->flags & MMC_DATA_WRITE) {
326 if (data->timeout_ns < 1000000000)
327 data->timeout_ns = 1000000000; /* 1s */
328 } else {
329 if (data->timeout_ns < 100000000)
330 data->timeout_ns = 100000000; /* 100ms */
331 }
332 }
Russell Kingd773d722006-09-07 15:57:12 +0100333}
334EXPORT_SYMBOL(mmc_set_data_timeout);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200337 * mmc_align_data_size - pads a transfer size to a more optimal value
338 * @card: the MMC card associated with the data transfer
339 * @sz: original transfer size
340 *
341 * Pads the original data size with a number of extra bytes in
342 * order to avoid controller bugs and/or performance hits
343 * (e.g. some controllers revert to PIO for certain sizes).
344 *
345 * Returns the improved size, which might be unmodified.
346 *
347 * Note that this function is only relevant when issuing a
348 * single scatter gather entry.
349 */
350unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
351{
352 /*
353 * FIXME: We don't have a system for the controller to tell
354 * the core about its problems yet, so for now we just 32-bit
355 * align the size.
356 */
357 sz = ((sz + 3) / 4) * 4;
358
359 return sz;
360}
361EXPORT_SYMBOL(mmc_align_data_size);
362
363/**
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700364 * mmc_host_enable - enable a host.
365 * @host: mmc host to enable
366 *
367 * Hosts that support power saving can use the 'enable' and 'disable'
368 * methods to exit and enter power saving states. For more information
369 * see comments for struct mmc_host_ops.
370 */
371int mmc_host_enable(struct mmc_host *host)
372{
373 if (!(host->caps & MMC_CAP_DISABLE))
374 return 0;
375
376 if (host->en_dis_recurs)
377 return 0;
378
379 if (host->nesting_cnt++)
380 return 0;
381
382 cancel_delayed_work_sync(&host->disable);
383
384 if (host->enabled)
385 return 0;
386
387 if (host->ops->enable) {
388 int err;
389
390 host->en_dis_recurs = 1;
391 err = host->ops->enable(host);
392 host->en_dis_recurs = 0;
393
394 if (err) {
395 pr_debug("%s: enable error %d\n",
396 mmc_hostname(host), err);
397 return err;
398 }
399 }
400 host->enabled = 1;
401 return 0;
402}
403EXPORT_SYMBOL(mmc_host_enable);
404
405static int mmc_host_do_disable(struct mmc_host *host, int lazy)
406{
407 if (host->ops->disable) {
408 int err;
409
410 host->en_dis_recurs = 1;
411 err = host->ops->disable(host, lazy);
412 host->en_dis_recurs = 0;
413
414 if (err < 0) {
415 pr_debug("%s: disable error %d\n",
416 mmc_hostname(host), err);
417 return err;
418 }
419 if (err > 0) {
420 unsigned long delay = msecs_to_jiffies(err);
421
422 mmc_schedule_delayed_work(&host->disable, delay);
423 }
424 }
425 host->enabled = 0;
426 return 0;
427}
428
429/**
430 * mmc_host_disable - disable a host.
431 * @host: mmc host to disable
432 *
433 * Hosts that support power saving can use the 'enable' and 'disable'
434 * methods to exit and enter power saving states. For more information
435 * see comments for struct mmc_host_ops.
436 */
437int mmc_host_disable(struct mmc_host *host)
438{
439 int err;
440
441 if (!(host->caps & MMC_CAP_DISABLE))
442 return 0;
443
444 if (host->en_dis_recurs)
445 return 0;
446
447 if (--host->nesting_cnt)
448 return 0;
449
450 if (!host->enabled)
451 return 0;
452
453 err = mmc_host_do_disable(host, 0);
454 return err;
455}
456EXPORT_SYMBOL(mmc_host_disable);
457
458/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200459 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200461 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200463 * Claim a host for a set of operations. If @abort is non null and
464 * dereference a non-zero value then this will return prematurely with
465 * that non-zero value without acquiring the lock. Returns zero
466 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200468int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 DECLARE_WAITQUEUE(wait, current);
471 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200472 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200474 might_sleep();
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 add_wait_queue(&host->wq, &wait);
477 spin_lock_irqsave(&host->lock, flags);
478 while (1) {
479 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200480 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700481 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 break;
483 spin_unlock_irqrestore(&host->lock, flags);
484 schedule();
485 spin_lock_irqsave(&host->lock, flags);
486 }
487 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700488 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200489 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700490 host->claimer = current;
491 host->claim_cnt += 1;
492 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200493 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 spin_unlock_irqrestore(&host->lock, flags);
495 remove_wait_queue(&host->wq, &wait);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700496 if (!stop)
497 mmc_host_enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200498 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499}
500
Nicolas Pitre2342f332007-06-30 16:21:52 +0200501EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Adrian Hunter319a3f12009-09-22 16:44:30 -0700503/**
504 * mmc_try_claim_host - try exclusively to claim a host
505 * @host: mmc host to claim
506 *
507 * Returns %1 if the host is claimed, %0 otherwise.
508 */
509int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700510{
511 int claimed_host = 0;
512 unsigned long flags;
513
514 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700515 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700516 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700517 host->claimer = current;
518 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700519 claimed_host = 1;
520 }
521 spin_unlock_irqrestore(&host->lock, flags);
522 return claimed_host;
523}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700524EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700525
526static void mmc_do_release_host(struct mmc_host *host)
527{
528 unsigned long flags;
529
530 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700531 if (--host->claim_cnt) {
532 /* Release for nested claim */
533 spin_unlock_irqrestore(&host->lock, flags);
534 } else {
535 host->claimed = 0;
536 host->claimer = NULL;
537 spin_unlock_irqrestore(&host->lock, flags);
538 wake_up(&host->wq);
539 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700540}
541
542void mmc_host_deeper_disable(struct work_struct *work)
543{
544 struct mmc_host *host =
545 container_of(work, struct mmc_host, disable.work);
546
547 /* If the host is claimed then we do not want to disable it anymore */
548 if (!mmc_try_claim_host(host))
549 return;
550 mmc_host_do_disable(host, 1);
551 mmc_do_release_host(host);
552}
553
554/**
555 * mmc_host_lazy_disable - lazily disable a host.
556 * @host: mmc host to disable
557 *
558 * Hosts that support power saving can use the 'enable' and 'disable'
559 * methods to exit and enter power saving states. For more information
560 * see comments for struct mmc_host_ops.
561 */
562int mmc_host_lazy_disable(struct mmc_host *host)
563{
564 if (!(host->caps & MMC_CAP_DISABLE))
565 return 0;
566
567 if (host->en_dis_recurs)
568 return 0;
569
570 if (--host->nesting_cnt)
571 return 0;
572
573 if (!host->enabled)
574 return 0;
575
576 if (host->disable_delay) {
577 mmc_schedule_delayed_work(&host->disable,
578 msecs_to_jiffies(host->disable_delay));
579 return 0;
580 } else
581 return mmc_host_do_disable(host, 1);
582}
583EXPORT_SYMBOL(mmc_host_lazy_disable);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585/**
586 * mmc_release_host - release a host
587 * @host: mmc host to release
588 *
589 * Release a MMC host, allowing others to claim the host
590 * for their operations.
591 */
592void mmc_release_host(struct mmc_host *host)
593{
Pierre Ossmand84075c82007-08-09 13:23:56 +0200594 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700596 mmc_host_lazy_disable(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700598 mmc_do_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599}
600
601EXPORT_SYMBOL(mmc_release_host);
602
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100603/*
604 * Internal function that does the actual ios call to the host driver,
605 * optionally printing some debug output.
606 */
Russell King920e70c2006-05-04 18:22:51 +0100607static inline void mmc_set_ios(struct mmc_host *host)
608{
609 struct mmc_ios *ios = &host->ios;
610
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100611 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
612 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100613 mmc_hostname(host), ios->clock, ios->bus_mode,
614 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100615 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800616
Russell King920e70c2006-05-04 18:22:51 +0100617 host->ops->set_ios(host, ios);
618}
619
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100620/*
621 * Control chip select pin on a host.
622 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100623void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700624{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100625 host->ios.chip_select = mode;
626 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}
628
629/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100630 * Sets the host clock to the highest possible frequency that
631 * is below "hz".
632 */
633void mmc_set_clock(struct mmc_host *host, unsigned int hz)
634{
635 WARN_ON(hz < host->f_min);
636
637 if (hz > host->f_max)
638 hz = host->f_max;
639
640 host->ios.clock = hz;
641 mmc_set_ios(host);
642}
643
644/*
645 * Change the bus mode (open drain/push-pull) of a host.
646 */
647void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
648{
649 host->ios.bus_mode = mode;
650 mmc_set_ios(host);
651}
652
653/*
654 * Change data bus width of a host.
655 */
656void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
657{
658 host->ios.bus_width = width;
659 mmc_set_ios(host);
660}
661
Anton Vorontsov86e82862008-11-26 22:54:17 +0300662/**
663 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
664 * @vdd: voltage (mV)
665 * @low_bits: prefer low bits in boundary cases
666 *
667 * This function returns the OCR bit number according to the provided @vdd
668 * value. If conversion is not possible a negative errno value returned.
669 *
670 * Depending on the @low_bits flag the function prefers low or high OCR bits
671 * on boundary voltages. For example,
672 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
673 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
674 *
675 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
676 */
677static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
678{
679 const int max_bit = ilog2(MMC_VDD_35_36);
680 int bit;
681
682 if (vdd < 1650 || vdd > 3600)
683 return -EINVAL;
684
685 if (vdd >= 1650 && vdd <= 1950)
686 return ilog2(MMC_VDD_165_195);
687
688 if (low_bits)
689 vdd -= 1;
690
691 /* Base 2000 mV, step 100 mV, bit's base 8. */
692 bit = (vdd - 2000) / 100 + 8;
693 if (bit > max_bit)
694 return max_bit;
695 return bit;
696}
697
698/**
699 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
700 * @vdd_min: minimum voltage value (mV)
701 * @vdd_max: maximum voltage value (mV)
702 *
703 * This function returns the OCR mask bits according to the provided @vdd_min
704 * and @vdd_max values. If conversion is not possible the function returns 0.
705 *
706 * Notes wrt boundary cases:
707 * This function sets the OCR bits for all boundary voltages, for example
708 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
709 * MMC_VDD_34_35 mask.
710 */
711u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
712{
713 u32 mask = 0;
714
715 if (vdd_max < vdd_min)
716 return 0;
717
718 /* Prefer high bits for the boundary vdd_max values. */
719 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
720 if (vdd_max < 0)
721 return 0;
722
723 /* Prefer low bits for the boundary vdd_min values. */
724 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
725 if (vdd_min < 0)
726 return 0;
727
728 /* Fill the mask, from max bit to min bit. */
729 while (vdd_max >= vdd_min)
730 mask |= 1 << vdd_max--;
731
732 return mask;
733}
734EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
735
David Brownell5c139412009-03-11 03:30:43 -0800736#ifdef CONFIG_REGULATOR
737
738/**
739 * mmc_regulator_get_ocrmask - return mask of supported voltages
740 * @supply: regulator to use
741 *
742 * This returns either a negative errno, or a mask of voltages that
743 * can be provided to MMC/SD/SDIO devices using the specified voltage
744 * regulator. This would normally be called before registering the
745 * MMC host adapter.
746 */
747int mmc_regulator_get_ocrmask(struct regulator *supply)
748{
749 int result = 0;
750 int count;
751 int i;
752
753 count = regulator_count_voltages(supply);
754 if (count < 0)
755 return count;
756
757 for (i = 0; i < count; i++) {
758 int vdd_uV;
759 int vdd_mV;
760
761 vdd_uV = regulator_list_voltage(supply, i);
762 if (vdd_uV <= 0)
763 continue;
764
765 vdd_mV = vdd_uV / 1000;
766 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
767 }
768
769 return result;
770}
771EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
772
773/**
774 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
775 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
776 * @supply: regulator to use
777 *
778 * Returns zero on success, else negative errno.
779 *
780 * MMC host drivers may use this to enable or disable a regulator using
781 * a particular supply voltage. This would normally be called from the
782 * set_ios() method.
783 */
784int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
785{
786 int result = 0;
787 int min_uV, max_uV;
788 int enabled;
789
790 enabled = regulator_is_enabled(supply);
791 if (enabled < 0)
792 return enabled;
793
794 if (vdd_bit) {
795 int tmp;
796 int voltage;
797
798 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
799 * bits this regulator doesn't quite support ... don't
800 * be too picky, most cards and regulators are OK with
801 * a 0.1V range goof (it's a small error percentage).
802 */
803 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
804 if (tmp == 0) {
805 min_uV = 1650 * 1000;
806 max_uV = 1950 * 1000;
807 } else {
808 min_uV = 1900 * 1000 + tmp * 100 * 1000;
809 max_uV = min_uV + 100 * 1000;
810 }
811
812 /* avoid needless changes to this voltage; the regulator
813 * might not allow this operation
814 */
815 voltage = regulator_get_voltage(supply);
816 if (voltage < 0)
817 result = voltage;
818 else if (voltage < min_uV || voltage > max_uV)
819 result = regulator_set_voltage(supply, min_uV, max_uV);
820 else
821 result = 0;
822
823 if (result == 0 && !enabled)
824 result = regulator_enable(supply);
825 } else if (enabled) {
826 result = regulator_disable(supply);
827 }
828
829 return result;
830}
831EXPORT_SYMBOL(mmc_regulator_set_ocr);
832
833#endif
834
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100835/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 * Mask off any voltages we don't support and select
837 * the lowest voltage
838 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100839u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 int bit;
842
843 ocr &= host->ocr_avail;
844
845 bit = ffs(ocr);
846 if (bit) {
847 bit -= 1;
848
Timo Teras63ef7312006-11-02 19:43:27 +0100849 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100852 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 } else {
David Brownellf6e10b82008-12-31 09:50:30 -0800854 pr_warning("%s: host doesn't support card's voltages\n",
855 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 ocr = 0;
857 }
858
859 return ocr;
860}
861
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100863 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100865void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100867 host->ios.timing = timing;
868 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700869}
870
871/*
Russell King45f82452005-12-14 14:57:35 +0000872 * Apply power to the MMC stack. This is a two-stage process.
873 * First, we enable power to the card without the clock running.
874 * We then wait a bit for the power to stabilise. Finally,
875 * enable the bus drivers and clock to the card.
876 *
877 * We must _NOT_ enable the clock prior to power stablising.
878 *
879 * If a host does all the power sequencing itself, ignore the
880 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 */
882static void mmc_power_up(struct mmc_host *host)
883{
Balaji Rao500f3562009-09-22 16:44:18 -0700884 int bit;
885
886 /* If ocr is set, we use it */
887 if (host->ocr)
888 bit = ffs(host->ocr) - 1;
889 else
890 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
892 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -0700893 if (mmc_host_is_spi(host)) {
894 host->ios.chip_select = MMC_CS_HIGH;
895 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
896 } else {
897 host->ios.chip_select = MMC_CS_DONTCARE;
898 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
899 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700901 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100902 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100903 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200905 /*
906 * This delay should be sufficient to allow the power supply
907 * to reach the minimum voltage.
908 */
José M. Fernández79bccc52009-03-10 02:21:21 +0100909 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Hein Tibosch88ae8b82010-09-06 09:37:19 +0800911 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +0200912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100914 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200916 /*
917 * This delay must be at least 74 clock sizes, or 1 ms, or the
918 * time required to reach a stable voltage.
919 */
José M. Fernández79bccc52009-03-10 02:21:21 +0100920 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921}
922
923static void mmc_power_off(struct mmc_host *host)
924{
925 host->ios.clock = 0;
926 host->ios.vdd = 0;
David Brownellaf517152007-08-08 09:11:32 -0700927 if (!mmc_host_is_spi(host)) {
928 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
929 host->ios.chip_select = MMC_CS_DONTCARE;
930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700932 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100933 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100934 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937/*
Adrian Bunk39361852007-07-25 00:40:58 +0200938 * Cleanup when the last reference to the bus operator is dropped.
939 */
Adrian Bunk261172f2008-04-13 21:15:47 +0300940static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +0200941{
942 BUG_ON(!host);
943 BUG_ON(host->bus_refs);
944 BUG_ON(!host->bus_dead);
945
946 host->bus_ops = NULL;
947}
948
949/*
950 * Increase reference count of bus operator
951 */
952static inline void mmc_bus_get(struct mmc_host *host)
953{
954 unsigned long flags;
955
956 spin_lock_irqsave(&host->lock, flags);
957 host->bus_refs++;
958 spin_unlock_irqrestore(&host->lock, flags);
959}
960
961/*
962 * Decrease reference count of bus operator and free it if
963 * it is the last reference.
964 */
965static inline void mmc_bus_put(struct mmc_host *host)
966{
967 unsigned long flags;
968
969 spin_lock_irqsave(&host->lock, flags);
970 host->bus_refs--;
971 if ((host->bus_refs == 0) && host->bus_ops)
972 __mmc_release_bus(host);
973 spin_unlock_irqrestore(&host->lock, flags);
974}
975
976/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100977 * Assign a mmc bus handler to a host. Only one bus handler may control a
978 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100980void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100982 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100984 BUG_ON(!host);
985 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100986
Pierre Ossmand84075c82007-08-09 13:23:56 +0200987 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100988
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100989 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100990
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100991 BUG_ON(host->bus_ops);
992 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100993
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100994 host->bus_ops = ops;
995 host->bus_refs = 1;
996 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100998 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
1001/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001002 * Remove the current bus handler from a host. Assumes that there are
1003 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001005void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001007 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001009 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
Pierre Ossmand84075c82007-08-09 13:23:56 +02001011 WARN_ON(!host->claimed);
1012 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001014 spin_lock_irqsave(&host->lock, flags);
1015
1016 host->bus_dead = 1;
1017
1018 spin_unlock_irqrestore(&host->lock, flags);
1019
1020 mmc_power_off(host);
1021
1022 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023}
1024
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025/**
1026 * mmc_detect_change - process change of state on a MMC socket
1027 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001028 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001030 * MMC drivers should call this when they detect a card has been
1031 * inserted or removed. The MMC layer will confirm that any
1032 * present card is still functional, and initialize any newly
1033 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001035void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001037#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001038 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001039 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001040 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001041 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001042#endif
1043
David Howellsc4028952006-11-22 14:57:56 +00001044 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047EXPORT_SYMBOL(mmc_detect_change);
1048
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001049void mmc_init_erase(struct mmc_card *card)
1050{
1051 unsigned int sz;
1052
1053 if (is_power_of_2(card->erase_size))
1054 card->erase_shift = ffs(card->erase_size) - 1;
1055 else
1056 card->erase_shift = 0;
1057
1058 /*
1059 * It is possible to erase an arbitrarily large area of an SD or MMC
1060 * card. That is not desirable because it can take a long time
1061 * (minutes) potentially delaying more important I/O, and also the
1062 * timeout calculations become increasingly hugely over-estimated.
1063 * Consequently, 'pref_erase' is defined as a guide to limit erases
1064 * to that size and alignment.
1065 *
1066 * For SD cards that define Allocation Unit size, limit erases to one
1067 * Allocation Unit at a time. For MMC cards that define High Capacity
1068 * Erase Size, whether it is switched on or not, limit to that size.
1069 * Otherwise just have a stab at a good value. For modern cards it
1070 * will end up being 4MiB. Note that if the value is too small, it
1071 * can end up taking longer to erase.
1072 */
1073 if (mmc_card_sd(card) && card->ssr.au) {
1074 card->pref_erase = card->ssr.au;
1075 card->erase_shift = ffs(card->ssr.au) - 1;
1076 } else if (card->ext_csd.hc_erase_size) {
1077 card->pref_erase = card->ext_csd.hc_erase_size;
1078 } else {
1079 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1080 if (sz < 128)
1081 card->pref_erase = 512 * 1024 / 512;
1082 else if (sz < 512)
1083 card->pref_erase = 1024 * 1024 / 512;
1084 else if (sz < 1024)
1085 card->pref_erase = 2 * 1024 * 1024 / 512;
1086 else
1087 card->pref_erase = 4 * 1024 * 1024 / 512;
1088 if (card->pref_erase < card->erase_size)
1089 card->pref_erase = card->erase_size;
1090 else {
1091 sz = card->pref_erase % card->erase_size;
1092 if (sz)
1093 card->pref_erase += card->erase_size - sz;
1094 }
1095 }
1096}
1097
1098static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1099 struct mmc_command *cmd,
1100 unsigned int arg, unsigned int qty)
1101{
1102 unsigned int erase_timeout;
1103
1104 if (card->ext_csd.erase_group_def & 1) {
1105 /* High Capacity Erase Group Size uses HC timeouts */
1106 if (arg == MMC_TRIM_ARG)
1107 erase_timeout = card->ext_csd.trim_timeout;
1108 else
1109 erase_timeout = card->ext_csd.hc_erase_timeout;
1110 } else {
1111 /* CSD Erase Group Size uses write timeout */
1112 unsigned int mult = (10 << card->csd.r2w_factor);
1113 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1114 unsigned int timeout_us;
1115
1116 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1117 if (card->csd.tacc_ns < 1000000)
1118 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1119 else
1120 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1121
1122 /*
1123 * ios.clock is only a target. The real clock rate might be
1124 * less but not that much less, so fudge it by multiplying by 2.
1125 */
1126 timeout_clks <<= 1;
1127 timeout_us += (timeout_clks * 1000) /
1128 (card->host->ios.clock / 1000);
1129
1130 erase_timeout = timeout_us / 1000;
1131
1132 /*
1133 * Theoretically, the calculation could underflow so round up
1134 * to 1ms in that case.
1135 */
1136 if (!erase_timeout)
1137 erase_timeout = 1;
1138 }
1139
1140 /* Multiplier for secure operations */
1141 if (arg & MMC_SECURE_ARGS) {
1142 if (arg == MMC_SECURE_ERASE_ARG)
1143 erase_timeout *= card->ext_csd.sec_erase_mult;
1144 else
1145 erase_timeout *= card->ext_csd.sec_trim_mult;
1146 }
1147
1148 erase_timeout *= qty;
1149
1150 /*
1151 * Ensure at least a 1 second timeout for SPI as per
1152 * 'mmc_set_data_timeout()'
1153 */
1154 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1155 erase_timeout = 1000;
1156
1157 cmd->erase_timeout = erase_timeout;
1158}
1159
1160static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1161 struct mmc_command *cmd, unsigned int arg,
1162 unsigned int qty)
1163{
1164 if (card->ssr.erase_timeout) {
1165 /* Erase timeout specified in SD Status Register (SSR) */
1166 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1167 card->ssr.erase_offset;
1168 } else {
1169 /*
1170 * Erase timeout not specified in SD Status Register (SSR) so
1171 * use 250ms per write block.
1172 */
1173 cmd->erase_timeout = 250 * qty;
1174 }
1175
1176 /* Must not be less than 1 second */
1177 if (cmd->erase_timeout < 1000)
1178 cmd->erase_timeout = 1000;
1179}
1180
1181static void mmc_set_erase_timeout(struct mmc_card *card,
1182 struct mmc_command *cmd, unsigned int arg,
1183 unsigned int qty)
1184{
1185 if (mmc_card_sd(card))
1186 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1187 else
1188 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1189}
1190
1191static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1192 unsigned int to, unsigned int arg)
1193{
1194 struct mmc_command cmd;
1195 unsigned int qty = 0;
1196 int err;
1197
1198 /*
1199 * qty is used to calculate the erase timeout which depends on how many
1200 * erase groups (or allocation units in SD terminology) are affected.
1201 * We count erasing part of an erase group as one erase group.
1202 * For SD, the allocation units are always a power of 2. For MMC, the
1203 * erase group size is almost certainly also power of 2, but it does not
1204 * seem to insist on that in the JEDEC standard, so we fall back to
1205 * division in that case. SD may not specify an allocation unit size,
1206 * in which case the timeout is based on the number of write blocks.
1207 *
1208 * Note that the timeout for secure trim 2 will only be correct if the
1209 * number of erase groups specified is the same as the total of all
1210 * preceding secure trim 1 commands. Since the power may have been
1211 * lost since the secure trim 1 commands occurred, it is generally
1212 * impossible to calculate the secure trim 2 timeout correctly.
1213 */
1214 if (card->erase_shift)
1215 qty += ((to >> card->erase_shift) -
1216 (from >> card->erase_shift)) + 1;
1217 else if (mmc_card_sd(card))
1218 qty += to - from + 1;
1219 else
1220 qty += ((to / card->erase_size) -
1221 (from / card->erase_size)) + 1;
1222
1223 if (!mmc_card_blockaddr(card)) {
1224 from <<= 9;
1225 to <<= 9;
1226 }
1227
1228 memset(&cmd, 0, sizeof(struct mmc_command));
1229 if (mmc_card_sd(card))
1230 cmd.opcode = SD_ERASE_WR_BLK_START;
1231 else
1232 cmd.opcode = MMC_ERASE_GROUP_START;
1233 cmd.arg = from;
1234 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1235 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1236 if (err) {
1237 printk(KERN_ERR "mmc_erase: group start error %d, "
1238 "status %#x\n", err, cmd.resp[0]);
1239 err = -EINVAL;
1240 goto out;
1241 }
1242
1243 memset(&cmd, 0, sizeof(struct mmc_command));
1244 if (mmc_card_sd(card))
1245 cmd.opcode = SD_ERASE_WR_BLK_END;
1246 else
1247 cmd.opcode = MMC_ERASE_GROUP_END;
1248 cmd.arg = to;
1249 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1250 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1251 if (err) {
1252 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1253 err, cmd.resp[0]);
1254 err = -EINVAL;
1255 goto out;
1256 }
1257
1258 memset(&cmd, 0, sizeof(struct mmc_command));
1259 cmd.opcode = MMC_ERASE;
1260 cmd.arg = arg;
1261 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1262 mmc_set_erase_timeout(card, &cmd, arg, qty);
1263 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1264 if (err) {
1265 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1266 err, cmd.resp[0]);
1267 err = -EIO;
1268 goto out;
1269 }
1270
1271 if (mmc_host_is_spi(card->host))
1272 goto out;
1273
1274 do {
1275 memset(&cmd, 0, sizeof(struct mmc_command));
1276 cmd.opcode = MMC_SEND_STATUS;
1277 cmd.arg = card->rca << 16;
1278 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1279 /* Do not retry else we can't see errors */
1280 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1281 if (err || (cmd.resp[0] & 0xFDF92000)) {
1282 printk(KERN_ERR "error %d requesting status %#x\n",
1283 err, cmd.resp[0]);
1284 err = -EIO;
1285 goto out;
1286 }
1287 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1288 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1289out:
1290 return err;
1291}
1292
1293/**
1294 * mmc_erase - erase sectors.
1295 * @card: card to erase
1296 * @from: first sector to erase
1297 * @nr: number of sectors to erase
1298 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1299 *
1300 * Caller must claim host before calling this function.
1301 */
1302int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1303 unsigned int arg)
1304{
1305 unsigned int rem, to = from + nr;
1306
1307 if (!(card->host->caps & MMC_CAP_ERASE) ||
1308 !(card->csd.cmdclass & CCC_ERASE))
1309 return -EOPNOTSUPP;
1310
1311 if (!card->erase_size)
1312 return -EOPNOTSUPP;
1313
1314 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1315 return -EOPNOTSUPP;
1316
1317 if ((arg & MMC_SECURE_ARGS) &&
1318 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1319 return -EOPNOTSUPP;
1320
1321 if ((arg & MMC_TRIM_ARGS) &&
1322 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1323 return -EOPNOTSUPP;
1324
1325 if (arg == MMC_SECURE_ERASE_ARG) {
1326 if (from % card->erase_size || nr % card->erase_size)
1327 return -EINVAL;
1328 }
1329
1330 if (arg == MMC_ERASE_ARG) {
1331 rem = from % card->erase_size;
1332 if (rem) {
1333 rem = card->erase_size - rem;
1334 from += rem;
1335 if (nr > rem)
1336 nr -= rem;
1337 else
1338 return 0;
1339 }
1340 rem = nr % card->erase_size;
1341 if (rem)
1342 nr -= rem;
1343 }
1344
1345 if (nr == 0)
1346 return 0;
1347
1348 to = from + nr;
1349
1350 if (to <= from)
1351 return -EINVAL;
1352
1353 /* 'from' and 'to' are inclusive */
1354 to -= 1;
1355
1356 return mmc_do_erase(card, from, to, arg);
1357}
1358EXPORT_SYMBOL(mmc_erase);
1359
1360int mmc_can_erase(struct mmc_card *card)
1361{
1362 if ((card->host->caps & MMC_CAP_ERASE) &&
1363 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1364 return 1;
1365 return 0;
1366}
1367EXPORT_SYMBOL(mmc_can_erase);
1368
1369int mmc_can_trim(struct mmc_card *card)
1370{
1371 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1372 return 1;
1373 return 0;
1374}
1375EXPORT_SYMBOL(mmc_can_trim);
1376
1377int mmc_can_secure_erase_trim(struct mmc_card *card)
1378{
1379 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1380 return 1;
1381 return 0;
1382}
1383EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1384
1385int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1386 unsigned int nr)
1387{
1388 if (!card->erase_size)
1389 return 0;
1390 if (from % card->erase_size || nr % card->erase_size)
1391 return 0;
1392 return 1;
1393}
1394EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001396void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397{
David Howellsc4028952006-11-22 14:57:56 +00001398 struct mmc_host *host =
1399 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001400 u32 ocr;
1401 int err;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001402 unsigned long flags;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001403 int i;
1404 const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001405
1406 spin_lock_irqsave(&host->lock, flags);
1407
1408 if (host->rescan_disable) {
1409 spin_unlock_irqrestore(&host->lock, flags);
1410 return;
1411 }
1412
1413 spin_unlock_irqrestore(&host->lock, flags);
1414
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001416 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001418 /* if there is a card registered, check whether it is still present */
1419 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1420 host->bus_ops->detect(host);
1421
1422 mmc_bus_put(host);
1423
1424
1425 mmc_bus_get(host);
1426
1427 /* if there still is a card present, stop here */
1428 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001429 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001430 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001432
1433 /* detect a newly inserted card */
1434
1435 /*
1436 * Only we can add a new handler, so it's safe to
1437 * release the lock here.
1438 */
1439 mmc_bus_put(host);
1440
1441 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1442 goto out;
1443
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001444 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1445 mmc_claim_host(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001446
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001447 if (freqs[i] >= host->f_min)
1448 host->f_init = freqs[i];
1449 else if (!i || freqs[i-1] > host->f_min)
1450 host->f_init = host->f_min;
1451 else {
1452 mmc_release_host(host);
1453 goto out;
1454 }
Eric Bénard4d0b8612010-10-12 09:29:37 +02001455#ifdef CONFIG_MMC_DEBUG
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001456 pr_info("%s: %s: trying to init card at %u Hz\n",
1457 mmc_hostname(host), __func__, host->f_init);
Eric Bénard4d0b8612010-10-12 09:29:37 +02001458#endif
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001459 mmc_power_up(host);
1460 sdio_reset(host);
1461 mmc_go_idle(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001462
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001463 mmc_send_if_cond(host, host->ocr_avail);
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001464
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001465 /*
1466 * First we search for SDIO...
1467 */
1468 err = mmc_send_io_op_cond(host, 0, &ocr);
1469 if (!err) {
1470 if (mmc_attach_sdio(host, ocr)) {
1471 mmc_claim_host(host);
1472 /*
1473 * Try SDMEM (but not MMC) even if SDIO
1474 * is broken.
1475 */
1476 if (mmc_send_app_op_cond(host, 0, &ocr))
1477 goto out_fail;
1478
1479 if (mmc_attach_sd(host, ocr))
1480 mmc_power_off(host);
1481 }
1482 goto out;
1483 }
1484
1485 /*
1486 * ...then normal SD...
1487 */
1488 err = mmc_send_app_op_cond(host, 0, &ocr);
1489 if (!err) {
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001490 if (mmc_attach_sd(host, ocr))
1491 mmc_power_off(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001492 goto out;
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001493 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001494
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001495 /*
1496 * ...and finally MMC.
1497 */
1498 err = mmc_send_op_cond(host, 0, &ocr);
1499 if (!err) {
1500 if (mmc_attach_mmc(host, ocr))
1501 mmc_power_off(host);
1502 goto out;
1503 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001504
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001505out_fail:
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001506 mmc_release_host(host);
1507 mmc_power_off(host);
1508 }
Anton Vorontsov28f52482008-06-17 18:17:15 +04001509out:
1510 if (host->caps & MMC_CAP_NEEDS_POLL)
1511 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001514void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001516 mmc_power_off(host);
1517 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518}
1519
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001520void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001522#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001523 unsigned long flags;
1524 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001525 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001526 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001527#endif
1528
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001529 if (host->caps & MMC_CAP_DISABLE)
1530 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02001531 cancel_delayed_work(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001532 mmc_flush_scheduled_work();
1533
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001534 /* clear pm flags now and let card drivers set them as needed */
1535 host->pm_flags = 0;
1536
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001537 mmc_bus_get(host);
1538 if (host->bus_ops && !host->bus_dead) {
1539 if (host->bus_ops->remove)
1540 host->bus_ops->remove(host);
1541
1542 mmc_claim_host(host);
1543 mmc_detach_bus(host);
1544 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07001545 mmc_bus_put(host);
1546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001548 mmc_bus_put(host);
1549
1550 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551
1552 mmc_power_off(host);
1553}
1554
Adrian Huntereae1aee2009-09-22 16:44:33 -07001555void mmc_power_save_host(struct mmc_host *host)
1556{
1557 mmc_bus_get(host);
1558
1559 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1560 mmc_bus_put(host);
1561 return;
1562 }
1563
1564 if (host->bus_ops->power_save)
1565 host->bus_ops->power_save(host);
1566
1567 mmc_bus_put(host);
1568
1569 mmc_power_off(host);
1570}
1571EXPORT_SYMBOL(mmc_power_save_host);
1572
1573void mmc_power_restore_host(struct mmc_host *host)
1574{
1575 mmc_bus_get(host);
1576
1577 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1578 mmc_bus_put(host);
1579 return;
1580 }
1581
1582 mmc_power_up(host);
1583 host->bus_ops->power_restore(host);
1584
1585 mmc_bus_put(host);
1586}
1587EXPORT_SYMBOL(mmc_power_restore_host);
1588
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001589int mmc_card_awake(struct mmc_host *host)
1590{
1591 int err = -ENOSYS;
1592
1593 mmc_bus_get(host);
1594
1595 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1596 err = host->bus_ops->awake(host);
1597
1598 mmc_bus_put(host);
1599
1600 return err;
1601}
1602EXPORT_SYMBOL(mmc_card_awake);
1603
1604int mmc_card_sleep(struct mmc_host *host)
1605{
1606 int err = -ENOSYS;
1607
1608 mmc_bus_get(host);
1609
1610 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1611 err = host->bus_ops->sleep(host);
1612
1613 mmc_bus_put(host);
1614
1615 return err;
1616}
1617EXPORT_SYMBOL(mmc_card_sleep);
1618
1619int mmc_card_can_sleep(struct mmc_host *host)
1620{
1621 struct mmc_card *card = host->card;
1622
1623 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1624 return 1;
1625 return 0;
1626}
1627EXPORT_SYMBOL(mmc_card_can_sleep);
1628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629#ifdef CONFIG_PM
1630
1631/**
1632 * mmc_suspend_host - suspend a host
1633 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001635int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001637 int err = 0;
1638
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001639 if (host->caps & MMC_CAP_DISABLE)
1640 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02001641 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001642 mmc_flush_scheduled_work();
1643
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001644 mmc_bus_get(host);
1645 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001646 if (host->bus_ops->suspend)
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001647 err = host->bus_ops->suspend(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02001648 if (err == -ENOSYS || !host->bus_ops->resume) {
1649 /*
1650 * We simply "remove" the card in this case.
1651 * It will be redetected on resume.
1652 */
1653 if (host->bus_ops->remove)
1654 host->bus_ops->remove(host);
1655 mmc_claim_host(host);
1656 mmc_detach_bus(host);
1657 mmc_release_host(host);
1658 host->pm_flags = 0;
1659 err = 0;
1660 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001661 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001662 mmc_bus_put(host);
1663
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001664 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001665 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001667 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668}
1669
1670EXPORT_SYMBOL(mmc_suspend_host);
1671
1672/**
1673 * mmc_resume_host - resume a previously suspended host
1674 * @host: mmc host
1675 */
1676int mmc_resume_host(struct mmc_host *host)
1677{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001678 int err = 0;
1679
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001680 mmc_bus_get(host);
1681 if (host->bus_ops && !host->bus_dead) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001682 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1683 mmc_power_up(host);
1684 mmc_select_voltage(host, host->ocr);
1685 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001686 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001687 err = host->bus_ops->resume(host);
1688 if (err) {
1689 printk(KERN_WARNING "%s: error %d during resume "
1690 "(card was removed?)\n",
1691 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001692 err = 0;
1693 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001694 }
1695 mmc_bus_put(host);
1696
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001697 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699EXPORT_SYMBOL(mmc_resume_host);
1700
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001701/* Do the card removal on suspend if card is assumed removeable
1702 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1703 to sync the card.
1704*/
1705int mmc_pm_notify(struct notifier_block *notify_block,
1706 unsigned long mode, void *unused)
1707{
1708 struct mmc_host *host = container_of(
1709 notify_block, struct mmc_host, pm_notify);
1710 unsigned long flags;
1711
1712
1713 switch (mode) {
1714 case PM_HIBERNATION_PREPARE:
1715 case PM_SUSPEND_PREPARE:
1716
1717 spin_lock_irqsave(&host->lock, flags);
1718 host->rescan_disable = 1;
1719 spin_unlock_irqrestore(&host->lock, flags);
1720 cancel_delayed_work_sync(&host->detect);
1721
1722 if (!host->bus_ops || host->bus_ops->suspend)
1723 break;
1724
1725 mmc_claim_host(host);
1726
1727 if (host->bus_ops->remove)
1728 host->bus_ops->remove(host);
1729
1730 mmc_detach_bus(host);
1731 mmc_release_host(host);
1732 host->pm_flags = 0;
1733 break;
1734
1735 case PM_POST_SUSPEND:
1736 case PM_POST_HIBERNATION:
1737
1738 spin_lock_irqsave(&host->lock, flags);
1739 host->rescan_disable = 0;
1740 spin_unlock_irqrestore(&host->lock, flags);
1741 mmc_detect_change(host, 0);
1742
1743 }
1744
1745 return 0;
1746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747#endif
1748
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001749static int __init mmc_init(void)
1750{
1751 int ret;
1752
1753 workqueue = create_singlethread_workqueue("kmmcd");
1754 if (!workqueue)
1755 return -ENOMEM;
1756
1757 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001758 if (ret)
1759 goto destroy_workqueue;
1760
1761 ret = mmc_register_host_class();
1762 if (ret)
1763 goto unregister_bus;
1764
1765 ret = sdio_register_bus();
1766 if (ret)
1767 goto unregister_host_class;
1768
1769 return 0;
1770
1771unregister_host_class:
1772 mmc_unregister_host_class();
1773unregister_bus:
1774 mmc_unregister_bus();
1775destroy_workqueue:
1776 destroy_workqueue(workqueue);
1777
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001778 return ret;
1779}
1780
1781static void __exit mmc_exit(void)
1782{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001783 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001784 mmc_unregister_host_class();
1785 mmc_unregister_bus();
1786 destroy_workqueue(workqueue);
1787}
1788
Nicolas Pitre26074962007-06-16 02:07:53 -04001789subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001790module_exit(mmc_exit);
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792MODULE_LICENSE("GPL");