blob: 722af2dce3bbbed1eb971db09e38e8fc49445c5a [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 Walleij04566832010-11-08 21:36:50 -0500133
134 mmc_host_clk_gate(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136}
137
138EXPORT_SYMBOL(mmc_request_done);
139
Adrian Bunk39361852007-07-25 00:40:58 +0200140static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
142{
Pierre Ossman976d9272007-04-13 22:47:01 +0200143#ifdef CONFIG_MMC_DEBUG
144 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200145 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200146#endif
147
Russell King920e70c2006-05-04 18:22:51 +0100148 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
149 mmc_hostname(host), mrq->cmd->opcode,
150 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Pierre Ossmane4d21702007-07-24 21:46:49 +0200152 if (mrq->data) {
153 pr_debug("%s: blksz %d blocks %d flags %08x "
154 "tsac %d ms nsac %d\n",
155 mmc_hostname(host), mrq->data->blksz,
156 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200157 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200158 mrq->data->timeout_clks);
159 }
160
161 if (mrq->stop) {
162 pr_debug("%s: CMD%u arg %08x flags %08x\n",
163 mmc_hostname(host), mrq->stop->opcode,
164 mrq->stop->arg, mrq->stop->flags);
165 }
166
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100167 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200169 led_trigger_event(host->led, LED_FULL);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 mrq->cmd->error = 0;
172 mrq->cmd->mrq = mrq;
173 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100174 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100175 BUG_ON(mrq->data->blocks > host->max_blk_count);
176 BUG_ON(mrq->data->blocks * mrq->data->blksz >
177 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100178
Pierre Ossman976d9272007-04-13 22:47:01 +0200179#ifdef CONFIG_MMC_DEBUG
180 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200181 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
182 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200183 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
184#endif
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 mrq->cmd->data = mrq->data;
187 mrq->data->error = 0;
188 mrq->data->mrq = mrq;
189 if (mrq->stop) {
190 mrq->data->stop = mrq->stop;
191 mrq->stop->error = 0;
192 mrq->stop->mrq = mrq;
193 }
194 }
Linus Walleij04566832010-11-08 21:36:50 -0500195 mmc_host_clk_ungate(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 host->ops->request(host, mrq);
197}
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void mmc_wait_done(struct mmc_request *mrq)
200{
201 complete(mrq->done_data);
202}
203
Pierre Ossman67a61c42007-07-11 20:22:11 +0200204/**
205 * mmc_wait_for_req - start a request and wait for completion
206 * @host: MMC host to start command
207 * @mrq: MMC request to start
208 *
209 * Start a new MMC custom command request for a host, and wait
210 * for the command to complete. Does not attempt to parse the
211 * response.
212 */
213void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Ingo Molnar0afffc72006-07-03 00:25:35 -0700215 DECLARE_COMPLETION_ONSTACK(complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 mrq->done_data = &complete;
218 mrq->done = mmc_wait_done;
219
220 mmc_start_request(host, mrq);
221
222 wait_for_completion(&complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
225EXPORT_SYMBOL(mmc_wait_for_req);
226
227/**
228 * mmc_wait_for_cmd - start a command and wait for completion
229 * @host: MMC host to start command
230 * @cmd: MMC command to start
231 * @retries: maximum number of retries
232 *
233 * Start a new MMC command for a host, and wait for the command
234 * to complete. Return any error that occurred while the command
235 * was executing. Do not attempt to parse the response.
236 */
237int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
238{
239 struct mmc_request mrq;
240
Pierre Ossmand84075c82007-08-09 13:23:56 +0200241 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 memset(&mrq, 0, sizeof(struct mmc_request));
244
245 memset(cmd->resp, 0, sizeof(cmd->resp));
246 cmd->retries = retries;
247
248 mrq.cmd = cmd;
249 cmd->data = NULL;
250
251 mmc_wait_for_req(host, &mrq);
252
253 return cmd->error;
254}
255
256EXPORT_SYMBOL(mmc_wait_for_cmd);
257
Pierre Ossman335eadf2005-09-06 15:18:50 -0700258/**
Russell Kingd773d722006-09-07 15:57:12 +0100259 * mmc_set_data_timeout - set the timeout for a data command
260 * @data: data phase for command
261 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200262 *
263 * Computes the data timeout parameters according to the
264 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100265 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200266void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100267{
268 unsigned int mult;
269
270 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200271 * SDIO cards only define an upper 1 s limit on access.
272 */
273 if (mmc_card_sdio(card)) {
274 data->timeout_ns = 1000000000;
275 data->timeout_clks = 0;
276 return;
277 }
278
279 /*
Russell Kingd773d722006-09-07 15:57:12 +0100280 * SD cards use a 100 multiplier rather than 10
281 */
282 mult = mmc_card_sd(card) ? 100 : 10;
283
284 /*
285 * Scale up the multiplier (and therefore the timeout) by
286 * the r2w factor for writes.
287 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200288 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100289 mult <<= card->csd.r2w_factor;
290
291 data->timeout_ns = card->csd.tacc_ns * mult;
292 data->timeout_clks = card->csd.tacc_clks * mult;
293
294 /*
295 * SD cards also have an upper limit on the timeout.
296 */
297 if (mmc_card_sd(card)) {
298 unsigned int timeout_us, limit_us;
299
300 timeout_us = data->timeout_ns / 1000;
301 timeout_us += data->timeout_clks * 1000 /
Linus Walleij04566832010-11-08 21:36:50 -0500302 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100303
Pierre Ossmanb146d262007-07-24 19:16:54 +0200304 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100305 /*
306 * The limit is really 250 ms, but that is
307 * insufficient for some crappy cards.
308 */
309 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100310 else
311 limit_us = 100000;
312
Philip Langdalefba68bd2007-01-04 06:57:32 -0800313 /*
314 * SDHC cards always use these fixed values.
315 */
316 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100317 data->timeout_ns = limit_us * 1000;
318 data->timeout_clks = 0;
319 }
320 }
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100321 /*
322 * Some cards need very high timeouts if driven in SPI mode.
323 * The worst observed timeout was 900ms after writing a
324 * continuous stream of data until the internal logic
325 * overflowed.
326 */
327 if (mmc_host_is_spi(card->host)) {
328 if (data->flags & MMC_DATA_WRITE) {
329 if (data->timeout_ns < 1000000000)
330 data->timeout_ns = 1000000000; /* 1s */
331 } else {
332 if (data->timeout_ns < 100000000)
333 data->timeout_ns = 100000000; /* 100ms */
334 }
335 }
Russell Kingd773d722006-09-07 15:57:12 +0100336}
337EXPORT_SYMBOL(mmc_set_data_timeout);
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200340 * mmc_align_data_size - pads a transfer size to a more optimal value
341 * @card: the MMC card associated with the data transfer
342 * @sz: original transfer size
343 *
344 * Pads the original data size with a number of extra bytes in
345 * order to avoid controller bugs and/or performance hits
346 * (e.g. some controllers revert to PIO for certain sizes).
347 *
348 * Returns the improved size, which might be unmodified.
349 *
350 * Note that this function is only relevant when issuing a
351 * single scatter gather entry.
352 */
353unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
354{
355 /*
356 * FIXME: We don't have a system for the controller to tell
357 * the core about its problems yet, so for now we just 32-bit
358 * align the size.
359 */
360 sz = ((sz + 3) / 4) * 4;
361
362 return sz;
363}
364EXPORT_SYMBOL(mmc_align_data_size);
365
366/**
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700367 * mmc_host_enable - enable a host.
368 * @host: mmc host to enable
369 *
370 * Hosts that support power saving can use the 'enable' and 'disable'
371 * methods to exit and enter power saving states. For more information
372 * see comments for struct mmc_host_ops.
373 */
374int mmc_host_enable(struct mmc_host *host)
375{
376 if (!(host->caps & MMC_CAP_DISABLE))
377 return 0;
378
379 if (host->en_dis_recurs)
380 return 0;
381
382 if (host->nesting_cnt++)
383 return 0;
384
385 cancel_delayed_work_sync(&host->disable);
386
387 if (host->enabled)
388 return 0;
389
390 if (host->ops->enable) {
391 int err;
392
393 host->en_dis_recurs = 1;
394 err = host->ops->enable(host);
395 host->en_dis_recurs = 0;
396
397 if (err) {
398 pr_debug("%s: enable error %d\n",
399 mmc_hostname(host), err);
400 return err;
401 }
402 }
403 host->enabled = 1;
404 return 0;
405}
406EXPORT_SYMBOL(mmc_host_enable);
407
408static int mmc_host_do_disable(struct mmc_host *host, int lazy)
409{
410 if (host->ops->disable) {
411 int err;
412
413 host->en_dis_recurs = 1;
414 err = host->ops->disable(host, lazy);
415 host->en_dis_recurs = 0;
416
417 if (err < 0) {
418 pr_debug("%s: disable error %d\n",
419 mmc_hostname(host), err);
420 return err;
421 }
422 if (err > 0) {
423 unsigned long delay = msecs_to_jiffies(err);
424
425 mmc_schedule_delayed_work(&host->disable, delay);
426 }
427 }
428 host->enabled = 0;
429 return 0;
430}
431
432/**
433 * mmc_host_disable - disable a host.
434 * @host: mmc host to disable
435 *
436 * Hosts that support power saving can use the 'enable' and 'disable'
437 * methods to exit and enter power saving states. For more information
438 * see comments for struct mmc_host_ops.
439 */
440int mmc_host_disable(struct mmc_host *host)
441{
442 int err;
443
444 if (!(host->caps & MMC_CAP_DISABLE))
445 return 0;
446
447 if (host->en_dis_recurs)
448 return 0;
449
450 if (--host->nesting_cnt)
451 return 0;
452
453 if (!host->enabled)
454 return 0;
455
456 err = mmc_host_do_disable(host, 0);
457 return err;
458}
459EXPORT_SYMBOL(mmc_host_disable);
460
461/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200462 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200464 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200466 * Claim a host for a set of operations. If @abort is non null and
467 * dereference a non-zero value then this will return prematurely with
468 * that non-zero value without acquiring the lock. Returns zero
469 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200471int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472{
473 DECLARE_WAITQUEUE(wait, current);
474 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200475 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200477 might_sleep();
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 add_wait_queue(&host->wq, &wait);
480 spin_lock_irqsave(&host->lock, flags);
481 while (1) {
482 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200483 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700484 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486 spin_unlock_irqrestore(&host->lock, flags);
487 schedule();
488 spin_lock_irqsave(&host->lock, flags);
489 }
490 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700491 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200492 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700493 host->claimer = current;
494 host->claim_cnt += 1;
495 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200496 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 spin_unlock_irqrestore(&host->lock, flags);
498 remove_wait_queue(&host->wq, &wait);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700499 if (!stop)
500 mmc_host_enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200501 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Nicolas Pitre2342f332007-06-30 16:21:52 +0200504EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
Adrian Hunter319a3f12009-09-22 16:44:30 -0700506/**
507 * mmc_try_claim_host - try exclusively to claim a host
508 * @host: mmc host to claim
509 *
510 * Returns %1 if the host is claimed, %0 otherwise.
511 */
512int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700513{
514 int claimed_host = 0;
515 unsigned long flags;
516
517 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700518 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700519 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700520 host->claimer = current;
521 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700522 claimed_host = 1;
523 }
524 spin_unlock_irqrestore(&host->lock, flags);
525 return claimed_host;
526}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700527EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700528
529static void mmc_do_release_host(struct mmc_host *host)
530{
531 unsigned long flags;
532
533 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700534 if (--host->claim_cnt) {
535 /* Release for nested claim */
536 spin_unlock_irqrestore(&host->lock, flags);
537 } else {
538 host->claimed = 0;
539 host->claimer = NULL;
540 spin_unlock_irqrestore(&host->lock, flags);
541 wake_up(&host->wq);
542 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700543}
544
545void mmc_host_deeper_disable(struct work_struct *work)
546{
547 struct mmc_host *host =
548 container_of(work, struct mmc_host, disable.work);
549
550 /* If the host is claimed then we do not want to disable it anymore */
551 if (!mmc_try_claim_host(host))
552 return;
553 mmc_host_do_disable(host, 1);
554 mmc_do_release_host(host);
555}
556
557/**
558 * mmc_host_lazy_disable - lazily disable a host.
559 * @host: mmc host to disable
560 *
561 * Hosts that support power saving can use the 'enable' and 'disable'
562 * methods to exit and enter power saving states. For more information
563 * see comments for struct mmc_host_ops.
564 */
565int mmc_host_lazy_disable(struct mmc_host *host)
566{
567 if (!(host->caps & MMC_CAP_DISABLE))
568 return 0;
569
570 if (host->en_dis_recurs)
571 return 0;
572
573 if (--host->nesting_cnt)
574 return 0;
575
576 if (!host->enabled)
577 return 0;
578
579 if (host->disable_delay) {
580 mmc_schedule_delayed_work(&host->disable,
581 msecs_to_jiffies(host->disable_delay));
582 return 0;
583 } else
584 return mmc_host_do_disable(host, 1);
585}
586EXPORT_SYMBOL(mmc_host_lazy_disable);
587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
589 * mmc_release_host - release a host
590 * @host: mmc host to release
591 *
592 * Release a MMC host, allowing others to claim the host
593 * for their operations.
594 */
595void mmc_release_host(struct mmc_host *host)
596{
Pierre Ossmand84075c82007-08-09 13:23:56 +0200597 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700599 mmc_host_lazy_disable(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700601 mmc_do_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
604EXPORT_SYMBOL(mmc_release_host);
605
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100606/*
607 * Internal function that does the actual ios call to the host driver,
608 * optionally printing some debug output.
609 */
Russell King920e70c2006-05-04 18:22:51 +0100610static inline void mmc_set_ios(struct mmc_host *host)
611{
612 struct mmc_ios *ios = &host->ios;
613
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100614 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
615 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100616 mmc_hostname(host), ios->clock, ios->bus_mode,
617 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100618 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800619
Linus Walleij04566832010-11-08 21:36:50 -0500620 if (ios->clock > 0)
621 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +0100622 host->ops->set_ios(host, ios);
623}
624
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100625/*
626 * Control chip select pin on a host.
627 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100628void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700629{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100630 host->ios.chip_select = mode;
631 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632}
633
634/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100635 * Sets the host clock to the highest possible frequency that
636 * is below "hz".
637 */
638void mmc_set_clock(struct mmc_host *host, unsigned int hz)
639{
640 WARN_ON(hz < host->f_min);
641
642 if (hz > host->f_max)
643 hz = host->f_max;
644
645 host->ios.clock = hz;
646 mmc_set_ios(host);
647}
648
Linus Walleij04566832010-11-08 21:36:50 -0500649#ifdef CONFIG_MMC_CLKGATE
650/*
651 * This gates the clock by setting it to 0 Hz.
652 */
653void mmc_gate_clock(struct mmc_host *host)
654{
655 unsigned long flags;
656
657 spin_lock_irqsave(&host->clk_lock, flags);
658 host->clk_old = host->ios.clock;
659 host->ios.clock = 0;
660 host->clk_gated = true;
661 spin_unlock_irqrestore(&host->clk_lock, flags);
662 mmc_set_ios(host);
663}
664
665/*
666 * This restores the clock from gating by using the cached
667 * clock value.
668 */
669void mmc_ungate_clock(struct mmc_host *host)
670{
671 /*
672 * We should previously have gated the clock, so the clock shall
673 * be 0 here! The clock may however be 0 during initialization,
674 * when some request operations are performed before setting
675 * the frequency. When ungate is requested in that situation
676 * we just ignore the call.
677 */
678 if (host->clk_old) {
679 BUG_ON(host->ios.clock);
680 /* This call will also set host->clk_gated to false */
681 mmc_set_clock(host, host->clk_old);
682 }
683}
684
685void mmc_set_ungated(struct mmc_host *host)
686{
687 unsigned long flags;
688
689 /*
690 * We've been given a new frequency while the clock is gated,
691 * so make sure we regard this as ungating it.
692 */
693 spin_lock_irqsave(&host->clk_lock, flags);
694 host->clk_gated = false;
695 spin_unlock_irqrestore(&host->clk_lock, flags);
696}
697
698#else
699void mmc_set_ungated(struct mmc_host *host)
700{
701}
702#endif
703
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100704/*
705 * Change the bus mode (open drain/push-pull) of a host.
706 */
707void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
708{
709 host->ios.bus_mode = mode;
710 mmc_set_ios(host);
711}
712
713/*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300714 * Change data bus width and DDR mode of a host.
715 */
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300716void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
717 unsigned int ddr)
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300718{
719 host->ios.bus_width = width;
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300720 host->ios.ddr = ddr;
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300721 mmc_set_ios(host);
722}
723
724/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100725 * Change data bus width of a host.
726 */
727void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
728{
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300729 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100730}
731
Anton Vorontsov86e82862008-11-26 22:54:17 +0300732/**
733 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
734 * @vdd: voltage (mV)
735 * @low_bits: prefer low bits in boundary cases
736 *
737 * This function returns the OCR bit number according to the provided @vdd
738 * value. If conversion is not possible a negative errno value returned.
739 *
740 * Depending on the @low_bits flag the function prefers low or high OCR bits
741 * on boundary voltages. For example,
742 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
743 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
744 *
745 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
746 */
747static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
748{
749 const int max_bit = ilog2(MMC_VDD_35_36);
750 int bit;
751
752 if (vdd < 1650 || vdd > 3600)
753 return -EINVAL;
754
755 if (vdd >= 1650 && vdd <= 1950)
756 return ilog2(MMC_VDD_165_195);
757
758 if (low_bits)
759 vdd -= 1;
760
761 /* Base 2000 mV, step 100 mV, bit's base 8. */
762 bit = (vdd - 2000) / 100 + 8;
763 if (bit > max_bit)
764 return max_bit;
765 return bit;
766}
767
768/**
769 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
770 * @vdd_min: minimum voltage value (mV)
771 * @vdd_max: maximum voltage value (mV)
772 *
773 * This function returns the OCR mask bits according to the provided @vdd_min
774 * and @vdd_max values. If conversion is not possible the function returns 0.
775 *
776 * Notes wrt boundary cases:
777 * This function sets the OCR bits for all boundary voltages, for example
778 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
779 * MMC_VDD_34_35 mask.
780 */
781u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
782{
783 u32 mask = 0;
784
785 if (vdd_max < vdd_min)
786 return 0;
787
788 /* Prefer high bits for the boundary vdd_max values. */
789 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
790 if (vdd_max < 0)
791 return 0;
792
793 /* Prefer low bits for the boundary vdd_min values. */
794 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
795 if (vdd_min < 0)
796 return 0;
797
798 /* Fill the mask, from max bit to min bit. */
799 while (vdd_max >= vdd_min)
800 mask |= 1 << vdd_max--;
801
802 return mask;
803}
804EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
805
David Brownell5c139412009-03-11 03:30:43 -0800806#ifdef CONFIG_REGULATOR
807
808/**
809 * mmc_regulator_get_ocrmask - return mask of supported voltages
810 * @supply: regulator to use
811 *
812 * This returns either a negative errno, or a mask of voltages that
813 * can be provided to MMC/SD/SDIO devices using the specified voltage
814 * regulator. This would normally be called before registering the
815 * MMC host adapter.
816 */
817int mmc_regulator_get_ocrmask(struct regulator *supply)
818{
819 int result = 0;
820 int count;
821 int i;
822
823 count = regulator_count_voltages(supply);
824 if (count < 0)
825 return count;
826
827 for (i = 0; i < count; i++) {
828 int vdd_uV;
829 int vdd_mV;
830
831 vdd_uV = regulator_list_voltage(supply, i);
832 if (vdd_uV <= 0)
833 continue;
834
835 vdd_mV = vdd_uV / 1000;
836 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
837 }
838
839 return result;
840}
841EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
842
843/**
844 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -0400845 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -0800846 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -0400847 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -0800848 *
849 * Returns zero on success, else negative errno.
850 *
851 * MMC host drivers may use this to enable or disable a regulator using
852 * a particular supply voltage. This would normally be called from the
853 * set_ios() method.
854 */
Linus Walleij99fc5132010-09-29 01:08:27 -0400855int mmc_regulator_set_ocr(struct mmc_host *mmc,
856 struct regulator *supply,
857 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -0800858{
859 int result = 0;
860 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -0800861
862 if (vdd_bit) {
863 int tmp;
864 int voltage;
865
866 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
867 * bits this regulator doesn't quite support ... don't
868 * be too picky, most cards and regulators are OK with
869 * a 0.1V range goof (it's a small error percentage).
870 */
871 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
872 if (tmp == 0) {
873 min_uV = 1650 * 1000;
874 max_uV = 1950 * 1000;
875 } else {
876 min_uV = 1900 * 1000 + tmp * 100 * 1000;
877 max_uV = min_uV + 100 * 1000;
878 }
879
880 /* avoid needless changes to this voltage; the regulator
881 * might not allow this operation
882 */
883 voltage = regulator_get_voltage(supply);
884 if (voltage < 0)
885 result = voltage;
886 else if (voltage < min_uV || voltage > max_uV)
887 result = regulator_set_voltage(supply, min_uV, max_uV);
888 else
889 result = 0;
890
Linus Walleij99fc5132010-09-29 01:08:27 -0400891 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -0800892 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -0400893 if (!result)
894 mmc->regulator_enabled = true;
895 }
896 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -0800897 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -0400898 if (result == 0)
899 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -0800900 }
901
Linus Walleij99fc5132010-09-29 01:08:27 -0400902 if (result)
903 dev_err(mmc_dev(mmc),
904 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -0800905 return result;
906}
907EXPORT_SYMBOL(mmc_regulator_set_ocr);
908
Linus Walleij99fc5132010-09-29 01:08:27 -0400909#endif /* CONFIG_REGULATOR */
David Brownell5c139412009-03-11 03:30:43 -0800910
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100911/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 * Mask off any voltages we don't support and select
913 * the lowest voltage
914 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100915u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916{
917 int bit;
918
919 ocr &= host->ocr_avail;
920
921 bit = ffs(ocr);
922 if (bit) {
923 bit -= 1;
924
Timo Teras63ef7312006-11-02 19:43:27 +0100925 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926
927 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100928 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 } else {
David Brownellf6e10b82008-12-31 09:50:30 -0800930 pr_warning("%s: host doesn't support card's voltages\n",
931 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 ocr = 0;
933 }
934
935 return ocr;
936}
937
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100939 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100941void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100943 host->ios.timing = timing;
944 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700945}
946
947/*
Russell King45f82452005-12-14 14:57:35 +0000948 * Apply power to the MMC stack. This is a two-stage process.
949 * First, we enable power to the card without the clock running.
950 * We then wait a bit for the power to stabilise. Finally,
951 * enable the bus drivers and clock to the card.
952 *
953 * We must _NOT_ enable the clock prior to power stablising.
954 *
955 * If a host does all the power sequencing itself, ignore the
956 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958static void mmc_power_up(struct mmc_host *host)
959{
Balaji Rao500f3562009-09-22 16:44:18 -0700960 int bit;
961
962 /* If ocr is set, we use it */
963 if (host->ocr)
964 bit = ffs(host->ocr) - 1;
965 else
966 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -0700969 if (mmc_host_is_spi(host)) {
970 host->ios.chip_select = MMC_CS_HIGH;
971 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
972 } else {
973 host->ios.chip_select = MMC_CS_DONTCARE;
974 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -0700977 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100978 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +0100979 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200981 /*
982 * This delay should be sufficient to allow the power supply
983 * to reach the minimum voltage.
984 */
José M. Fernández79bccc52009-03-10 02:21:21 +0100985 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Hein Tibosch88ae8b82010-09-06 09:37:19 +0800987 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +0200988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +0100990 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
Pierre Ossmanf9996ae2007-09-19 18:38:50 +0200992 /*
993 * This delay must be at least 74 clock sizes, or 1 ms, or the
994 * time required to reach a stable voltage.
995 */
José M. Fernández79bccc52009-03-10 02:21:21 +0100996 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999static void mmc_power_off(struct mmc_host *host)
1000{
1001 host->ios.clock = 0;
1002 host->ios.vdd = 0;
David Brownellaf517152007-08-08 09:11:32 -07001003 if (!mmc_host_is_spi(host)) {
1004 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1005 host->ios.chip_select = MMC_CS_DONTCARE;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001008 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001009 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001010 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013/*
Adrian Bunk39361852007-07-25 00:40:58 +02001014 * Cleanup when the last reference to the bus operator is dropped.
1015 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001016static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001017{
1018 BUG_ON(!host);
1019 BUG_ON(host->bus_refs);
1020 BUG_ON(!host->bus_dead);
1021
1022 host->bus_ops = NULL;
1023}
1024
1025/*
1026 * Increase reference count of bus operator
1027 */
1028static inline void mmc_bus_get(struct mmc_host *host)
1029{
1030 unsigned long flags;
1031
1032 spin_lock_irqsave(&host->lock, flags);
1033 host->bus_refs++;
1034 spin_unlock_irqrestore(&host->lock, flags);
1035}
1036
1037/*
1038 * Decrease reference count of bus operator and free it if
1039 * it is the last reference.
1040 */
1041static inline void mmc_bus_put(struct mmc_host *host)
1042{
1043 unsigned long flags;
1044
1045 spin_lock_irqsave(&host->lock, flags);
1046 host->bus_refs--;
1047 if ((host->bus_refs == 0) && host->bus_ops)
1048 __mmc_release_bus(host);
1049 spin_unlock_irqrestore(&host->lock, flags);
1050}
1051
1052/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001053 * Assign a mmc bus handler to a host. Only one bus handler may control a
1054 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001056void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001058 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001060 BUG_ON(!host);
1061 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001062
Pierre Ossmand84075c82007-08-09 13:23:56 +02001063 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001064
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001065 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001066
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001067 BUG_ON(host->bus_ops);
1068 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001069
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001070 host->bus_ops = ops;
1071 host->bus_refs = 1;
1072 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001074 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075}
1076
1077/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001078 * Remove the current bus handler from a host. Assumes that there are
1079 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001081void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001083 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001085 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Pierre Ossmand84075c82007-08-09 13:23:56 +02001087 WARN_ON(!host->claimed);
1088 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001090 spin_lock_irqsave(&host->lock, flags);
1091
1092 host->bus_dead = 1;
1093
1094 spin_unlock_irqrestore(&host->lock, flags);
1095
1096 mmc_power_off(host);
1097
1098 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101/**
1102 * mmc_detect_change - process change of state on a MMC socket
1103 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001104 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001106 * MMC drivers should call this when they detect a card has been
1107 * inserted or removed. The MMC layer will confirm that any
1108 * present card is still functional, and initialize any newly
1109 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001111void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001113#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001114 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001115 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001116 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001117 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001118#endif
1119
David Howellsc4028952006-11-22 14:57:56 +00001120 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121}
1122
1123EXPORT_SYMBOL(mmc_detect_change);
1124
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001125void mmc_init_erase(struct mmc_card *card)
1126{
1127 unsigned int sz;
1128
1129 if (is_power_of_2(card->erase_size))
1130 card->erase_shift = ffs(card->erase_size) - 1;
1131 else
1132 card->erase_shift = 0;
1133
1134 /*
1135 * It is possible to erase an arbitrarily large area of an SD or MMC
1136 * card. That is not desirable because it can take a long time
1137 * (minutes) potentially delaying more important I/O, and also the
1138 * timeout calculations become increasingly hugely over-estimated.
1139 * Consequently, 'pref_erase' is defined as a guide to limit erases
1140 * to that size and alignment.
1141 *
1142 * For SD cards that define Allocation Unit size, limit erases to one
1143 * Allocation Unit at a time. For MMC cards that define High Capacity
1144 * Erase Size, whether it is switched on or not, limit to that size.
1145 * Otherwise just have a stab at a good value. For modern cards it
1146 * will end up being 4MiB. Note that if the value is too small, it
1147 * can end up taking longer to erase.
1148 */
1149 if (mmc_card_sd(card) && card->ssr.au) {
1150 card->pref_erase = card->ssr.au;
1151 card->erase_shift = ffs(card->ssr.au) - 1;
1152 } else if (card->ext_csd.hc_erase_size) {
1153 card->pref_erase = card->ext_csd.hc_erase_size;
1154 } else {
1155 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1156 if (sz < 128)
1157 card->pref_erase = 512 * 1024 / 512;
1158 else if (sz < 512)
1159 card->pref_erase = 1024 * 1024 / 512;
1160 else if (sz < 1024)
1161 card->pref_erase = 2 * 1024 * 1024 / 512;
1162 else
1163 card->pref_erase = 4 * 1024 * 1024 / 512;
1164 if (card->pref_erase < card->erase_size)
1165 card->pref_erase = card->erase_size;
1166 else {
1167 sz = card->pref_erase % card->erase_size;
1168 if (sz)
1169 card->pref_erase += card->erase_size - sz;
1170 }
1171 }
1172}
1173
1174static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
1175 struct mmc_command *cmd,
1176 unsigned int arg, unsigned int qty)
1177{
1178 unsigned int erase_timeout;
1179
1180 if (card->ext_csd.erase_group_def & 1) {
1181 /* High Capacity Erase Group Size uses HC timeouts */
1182 if (arg == MMC_TRIM_ARG)
1183 erase_timeout = card->ext_csd.trim_timeout;
1184 else
1185 erase_timeout = card->ext_csd.hc_erase_timeout;
1186 } else {
1187 /* CSD Erase Group Size uses write timeout */
1188 unsigned int mult = (10 << card->csd.r2w_factor);
1189 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1190 unsigned int timeout_us;
1191
1192 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1193 if (card->csd.tacc_ns < 1000000)
1194 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1195 else
1196 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1197
1198 /*
1199 * ios.clock is only a target. The real clock rate might be
1200 * less but not that much less, so fudge it by multiplying by 2.
1201 */
1202 timeout_clks <<= 1;
1203 timeout_us += (timeout_clks * 1000) /
1204 (card->host->ios.clock / 1000);
1205
1206 erase_timeout = timeout_us / 1000;
1207
1208 /*
1209 * Theoretically, the calculation could underflow so round up
1210 * to 1ms in that case.
1211 */
1212 if (!erase_timeout)
1213 erase_timeout = 1;
1214 }
1215
1216 /* Multiplier for secure operations */
1217 if (arg & MMC_SECURE_ARGS) {
1218 if (arg == MMC_SECURE_ERASE_ARG)
1219 erase_timeout *= card->ext_csd.sec_erase_mult;
1220 else
1221 erase_timeout *= card->ext_csd.sec_trim_mult;
1222 }
1223
1224 erase_timeout *= qty;
1225
1226 /*
1227 * Ensure at least a 1 second timeout for SPI as per
1228 * 'mmc_set_data_timeout()'
1229 */
1230 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1231 erase_timeout = 1000;
1232
1233 cmd->erase_timeout = erase_timeout;
1234}
1235
1236static void mmc_set_sd_erase_timeout(struct mmc_card *card,
1237 struct mmc_command *cmd, unsigned int arg,
1238 unsigned int qty)
1239{
1240 if (card->ssr.erase_timeout) {
1241 /* Erase timeout specified in SD Status Register (SSR) */
1242 cmd->erase_timeout = card->ssr.erase_timeout * qty +
1243 card->ssr.erase_offset;
1244 } else {
1245 /*
1246 * Erase timeout not specified in SD Status Register (SSR) so
1247 * use 250ms per write block.
1248 */
1249 cmd->erase_timeout = 250 * qty;
1250 }
1251
1252 /* Must not be less than 1 second */
1253 if (cmd->erase_timeout < 1000)
1254 cmd->erase_timeout = 1000;
1255}
1256
1257static void mmc_set_erase_timeout(struct mmc_card *card,
1258 struct mmc_command *cmd, unsigned int arg,
1259 unsigned int qty)
1260{
1261 if (mmc_card_sd(card))
1262 mmc_set_sd_erase_timeout(card, cmd, arg, qty);
1263 else
1264 mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
1265}
1266
1267static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1268 unsigned int to, unsigned int arg)
1269{
1270 struct mmc_command cmd;
1271 unsigned int qty = 0;
1272 int err;
1273
1274 /*
1275 * qty is used to calculate the erase timeout which depends on how many
1276 * erase groups (or allocation units in SD terminology) are affected.
1277 * We count erasing part of an erase group as one erase group.
1278 * For SD, the allocation units are always a power of 2. For MMC, the
1279 * erase group size is almost certainly also power of 2, but it does not
1280 * seem to insist on that in the JEDEC standard, so we fall back to
1281 * division in that case. SD may not specify an allocation unit size,
1282 * in which case the timeout is based on the number of write blocks.
1283 *
1284 * Note that the timeout for secure trim 2 will only be correct if the
1285 * number of erase groups specified is the same as the total of all
1286 * preceding secure trim 1 commands. Since the power may have been
1287 * lost since the secure trim 1 commands occurred, it is generally
1288 * impossible to calculate the secure trim 2 timeout correctly.
1289 */
1290 if (card->erase_shift)
1291 qty += ((to >> card->erase_shift) -
1292 (from >> card->erase_shift)) + 1;
1293 else if (mmc_card_sd(card))
1294 qty += to - from + 1;
1295 else
1296 qty += ((to / card->erase_size) -
1297 (from / card->erase_size)) + 1;
1298
1299 if (!mmc_card_blockaddr(card)) {
1300 from <<= 9;
1301 to <<= 9;
1302 }
1303
1304 memset(&cmd, 0, sizeof(struct mmc_command));
1305 if (mmc_card_sd(card))
1306 cmd.opcode = SD_ERASE_WR_BLK_START;
1307 else
1308 cmd.opcode = MMC_ERASE_GROUP_START;
1309 cmd.arg = from;
1310 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1311 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1312 if (err) {
1313 printk(KERN_ERR "mmc_erase: group start error %d, "
1314 "status %#x\n", err, cmd.resp[0]);
1315 err = -EINVAL;
1316 goto out;
1317 }
1318
1319 memset(&cmd, 0, sizeof(struct mmc_command));
1320 if (mmc_card_sd(card))
1321 cmd.opcode = SD_ERASE_WR_BLK_END;
1322 else
1323 cmd.opcode = MMC_ERASE_GROUP_END;
1324 cmd.arg = to;
1325 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1326 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1327 if (err) {
1328 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1329 err, cmd.resp[0]);
1330 err = -EINVAL;
1331 goto out;
1332 }
1333
1334 memset(&cmd, 0, sizeof(struct mmc_command));
1335 cmd.opcode = MMC_ERASE;
1336 cmd.arg = arg;
1337 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1338 mmc_set_erase_timeout(card, &cmd, arg, qty);
1339 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1340 if (err) {
1341 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1342 err, cmd.resp[0]);
1343 err = -EIO;
1344 goto out;
1345 }
1346
1347 if (mmc_host_is_spi(card->host))
1348 goto out;
1349
1350 do {
1351 memset(&cmd, 0, sizeof(struct mmc_command));
1352 cmd.opcode = MMC_SEND_STATUS;
1353 cmd.arg = card->rca << 16;
1354 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1355 /* Do not retry else we can't see errors */
1356 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1357 if (err || (cmd.resp[0] & 0xFDF92000)) {
1358 printk(KERN_ERR "error %d requesting status %#x\n",
1359 err, cmd.resp[0]);
1360 err = -EIO;
1361 goto out;
1362 }
1363 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1364 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1365out:
1366 return err;
1367}
1368
1369/**
1370 * mmc_erase - erase sectors.
1371 * @card: card to erase
1372 * @from: first sector to erase
1373 * @nr: number of sectors to erase
1374 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1375 *
1376 * Caller must claim host before calling this function.
1377 */
1378int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1379 unsigned int arg)
1380{
1381 unsigned int rem, to = from + nr;
1382
1383 if (!(card->host->caps & MMC_CAP_ERASE) ||
1384 !(card->csd.cmdclass & CCC_ERASE))
1385 return -EOPNOTSUPP;
1386
1387 if (!card->erase_size)
1388 return -EOPNOTSUPP;
1389
1390 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1391 return -EOPNOTSUPP;
1392
1393 if ((arg & MMC_SECURE_ARGS) &&
1394 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1395 return -EOPNOTSUPP;
1396
1397 if ((arg & MMC_TRIM_ARGS) &&
1398 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1399 return -EOPNOTSUPP;
1400
1401 if (arg == MMC_SECURE_ERASE_ARG) {
1402 if (from % card->erase_size || nr % card->erase_size)
1403 return -EINVAL;
1404 }
1405
1406 if (arg == MMC_ERASE_ARG) {
1407 rem = from % card->erase_size;
1408 if (rem) {
1409 rem = card->erase_size - rem;
1410 from += rem;
1411 if (nr > rem)
1412 nr -= rem;
1413 else
1414 return 0;
1415 }
1416 rem = nr % card->erase_size;
1417 if (rem)
1418 nr -= rem;
1419 }
1420
1421 if (nr == 0)
1422 return 0;
1423
1424 to = from + nr;
1425
1426 if (to <= from)
1427 return -EINVAL;
1428
1429 /* 'from' and 'to' are inclusive */
1430 to -= 1;
1431
1432 return mmc_do_erase(card, from, to, arg);
1433}
1434EXPORT_SYMBOL(mmc_erase);
1435
1436int mmc_can_erase(struct mmc_card *card)
1437{
1438 if ((card->host->caps & MMC_CAP_ERASE) &&
1439 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1440 return 1;
1441 return 0;
1442}
1443EXPORT_SYMBOL(mmc_can_erase);
1444
1445int mmc_can_trim(struct mmc_card *card)
1446{
1447 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1448 return 1;
1449 return 0;
1450}
1451EXPORT_SYMBOL(mmc_can_trim);
1452
1453int mmc_can_secure_erase_trim(struct mmc_card *card)
1454{
1455 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1456 return 1;
1457 return 0;
1458}
1459EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1460
1461int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1462 unsigned int nr)
1463{
1464 if (!card->erase_size)
1465 return 0;
1466 if (from % card->erase_size || nr % card->erase_size)
1467 return 0;
1468 return 1;
1469}
1470EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001472int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1473{
1474 struct mmc_command cmd;
1475
1476 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1477 return 0;
1478
1479 memset(&cmd, 0, sizeof(struct mmc_command));
1480 cmd.opcode = MMC_SET_BLOCKLEN;
1481 cmd.arg = blocklen;
1482 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1483 return mmc_wait_for_cmd(card->host, &cmd, 5);
1484}
1485EXPORT_SYMBOL(mmc_set_blocklen);
1486
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001487void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488{
David Howellsc4028952006-11-22 14:57:56 +00001489 struct mmc_host *host =
1490 container_of(work, struct mmc_host, detect.work);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001491 u32 ocr;
1492 int err;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001493 unsigned long flags;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001494 int i;
1495 const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001496
1497 spin_lock_irqsave(&host->lock, flags);
1498
1499 if (host->rescan_disable) {
1500 spin_unlock_irqrestore(&host->lock, flags);
1501 return;
1502 }
1503
1504 spin_unlock_irqrestore(&host->lock, flags);
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001507 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001509 /* if there is a card registered, check whether it is still present */
1510 if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead)
1511 host->bus_ops->detect(host);
1512
1513 mmc_bus_put(host);
1514
1515
1516 mmc_bus_get(host);
1517
1518 /* if there still is a card present, stop here */
1519 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001520 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001521 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001523
1524 /* detect a newly inserted card */
1525
1526 /*
1527 * Only we can add a new handler, so it's safe to
1528 * release the lock here.
1529 */
1530 mmc_bus_put(host);
1531
1532 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1533 goto out;
1534
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001535 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
1536 mmc_claim_host(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001537
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001538 if (freqs[i] >= host->f_min)
1539 host->f_init = freqs[i];
1540 else if (!i || freqs[i-1] > host->f_min)
1541 host->f_init = host->f_min;
1542 else {
1543 mmc_release_host(host);
1544 goto out;
1545 }
Eric Bénard4d0b8612010-10-12 09:29:37 +02001546#ifdef CONFIG_MMC_DEBUG
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001547 pr_info("%s: %s: trying to init card at %u Hz\n",
1548 mmc_hostname(host), __func__, host->f_init);
Eric Bénard4d0b8612010-10-12 09:29:37 +02001549#endif
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001550 mmc_power_up(host);
1551 sdio_reset(host);
1552 mmc_go_idle(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001553
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001554 mmc_send_if_cond(host, host->ocr_avail);
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001555
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001556 /*
1557 * First we search for SDIO...
1558 */
1559 err = mmc_send_io_op_cond(host, 0, &ocr);
1560 if (!err) {
1561 if (mmc_attach_sdio(host, ocr)) {
1562 mmc_claim_host(host);
1563 /*
1564 * Try SDMEM (but not MMC) even if SDIO
1565 * is broken.
1566 */
1567 if (mmc_send_app_op_cond(host, 0, &ocr))
1568 goto out_fail;
1569
1570 if (mmc_attach_sd(host, ocr))
1571 mmc_power_off(host);
1572 }
1573 goto out;
1574 }
1575
1576 /*
1577 * ...then normal SD...
1578 */
1579 err = mmc_send_app_op_cond(host, 0, &ocr);
1580 if (!err) {
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001581 if (mmc_attach_sd(host, ocr))
1582 mmc_power_off(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001583 goto out;
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001584 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001585
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001586 /*
1587 * ...and finally MMC.
1588 */
1589 err = mmc_send_op_cond(host, 0, &ocr);
1590 if (!err) {
1591 if (mmc_attach_mmc(host, ocr))
1592 mmc_power_off(host);
1593 goto out;
1594 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001595
Michal Miroslaw7310ece82010-08-10 18:01:40 -07001596out_fail:
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001597 mmc_release_host(host);
1598 mmc_power_off(host);
1599 }
Anton Vorontsov28f52482008-06-17 18:17:15 +04001600out:
1601 if (host->caps & MMC_CAP_NEEDS_POLL)
1602 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603}
1604
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001605void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606{
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001607 mmc_power_off(host);
1608 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609}
1610
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001611void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001613#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001614 unsigned long flags;
1615 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001616 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001617 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001618#endif
1619
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001620 if (host->caps & MMC_CAP_DISABLE)
1621 cancel_delayed_work(&host->disable);
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01001622 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001623 mmc_flush_scheduled_work();
1624
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001625 /* clear pm flags now and let card drivers set them as needed */
1626 host->pm_flags = 0;
1627
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001628 mmc_bus_get(host);
1629 if (host->bus_ops && !host->bus_dead) {
1630 if (host->bus_ops->remove)
1631 host->bus_ops->remove(host);
1632
1633 mmc_claim_host(host);
1634 mmc_detach_bus(host);
1635 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07001636 mmc_bus_put(host);
1637 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001639 mmc_bus_put(host);
1640
1641 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 mmc_power_off(host);
1644}
1645
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001646int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001647{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001648 int ret = 0;
1649
Adrian Huntereae1aee2009-09-22 16:44:33 -07001650 mmc_bus_get(host);
1651
1652 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1653 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001654 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001655 }
1656
1657 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001658 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001659
1660 mmc_bus_put(host);
1661
1662 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001663
1664 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001665}
1666EXPORT_SYMBOL(mmc_power_save_host);
1667
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001668int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001669{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001670 int ret;
1671
Adrian Huntereae1aee2009-09-22 16:44:33 -07001672 mmc_bus_get(host);
1673
1674 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1675 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001676 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001677 }
1678
1679 mmc_power_up(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001680 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001681
1682 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001683
1684 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001685}
1686EXPORT_SYMBOL(mmc_power_restore_host);
1687
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001688int mmc_card_awake(struct mmc_host *host)
1689{
1690 int err = -ENOSYS;
1691
1692 mmc_bus_get(host);
1693
1694 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1695 err = host->bus_ops->awake(host);
1696
1697 mmc_bus_put(host);
1698
1699 return err;
1700}
1701EXPORT_SYMBOL(mmc_card_awake);
1702
1703int mmc_card_sleep(struct mmc_host *host)
1704{
1705 int err = -ENOSYS;
1706
1707 mmc_bus_get(host);
1708
1709 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1710 err = host->bus_ops->sleep(host);
1711
1712 mmc_bus_put(host);
1713
1714 return err;
1715}
1716EXPORT_SYMBOL(mmc_card_sleep);
1717
1718int mmc_card_can_sleep(struct mmc_host *host)
1719{
1720 struct mmc_card *card = host->card;
1721
1722 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1723 return 1;
1724 return 0;
1725}
1726EXPORT_SYMBOL(mmc_card_can_sleep);
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728#ifdef CONFIG_PM
1729
1730/**
1731 * mmc_suspend_host - suspend a host
1732 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001734int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001736 int err = 0;
1737
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001738 if (host->caps & MMC_CAP_DISABLE)
1739 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02001740 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001741 mmc_flush_scheduled_work();
1742
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001743 mmc_bus_get(host);
1744 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001745 if (host->bus_ops->suspend)
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001746 err = host->bus_ops->suspend(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02001747 if (err == -ENOSYS || !host->bus_ops->resume) {
1748 /*
1749 * We simply "remove" the card in this case.
1750 * It will be redetected on resume.
1751 */
1752 if (host->bus_ops->remove)
1753 host->bus_ops->remove(host);
1754 mmc_claim_host(host);
1755 mmc_detach_bus(host);
1756 mmc_release_host(host);
1757 host->pm_flags = 0;
1758 err = 0;
1759 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001760 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001761 mmc_bus_put(host);
1762
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001763 if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001764 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001766 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
1769EXPORT_SYMBOL(mmc_suspend_host);
1770
1771/**
1772 * mmc_resume_host - resume a previously suspended host
1773 * @host: mmc host
1774 */
1775int mmc_resume_host(struct mmc_host *host)
1776{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001777 int err = 0;
1778
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001779 mmc_bus_get(host);
1780 if (host->bus_ops && !host->bus_dead) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001781 if (!(host->pm_flags & MMC_PM_KEEP_POWER)) {
1782 mmc_power_up(host);
1783 mmc_select_voltage(host, host->ocr);
1784 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001785 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001786 err = host->bus_ops->resume(host);
1787 if (err) {
1788 printk(KERN_WARNING "%s: error %d during resume "
1789 "(card was removed?)\n",
1790 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001791 err = 0;
1792 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001793 }
1794 mmc_bus_put(host);
1795
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001796 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798EXPORT_SYMBOL(mmc_resume_host);
1799
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001800/* Do the card removal on suspend if card is assumed removeable
1801 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1802 to sync the card.
1803*/
1804int mmc_pm_notify(struct notifier_block *notify_block,
1805 unsigned long mode, void *unused)
1806{
1807 struct mmc_host *host = container_of(
1808 notify_block, struct mmc_host, pm_notify);
1809 unsigned long flags;
1810
1811
1812 switch (mode) {
1813 case PM_HIBERNATION_PREPARE:
1814 case PM_SUSPEND_PREPARE:
1815
1816 spin_lock_irqsave(&host->lock, flags);
1817 host->rescan_disable = 1;
1818 spin_unlock_irqrestore(&host->lock, flags);
1819 cancel_delayed_work_sync(&host->detect);
1820
1821 if (!host->bus_ops || host->bus_ops->suspend)
1822 break;
1823
1824 mmc_claim_host(host);
1825
1826 if (host->bus_ops->remove)
1827 host->bus_ops->remove(host);
1828
1829 mmc_detach_bus(host);
1830 mmc_release_host(host);
1831 host->pm_flags = 0;
1832 break;
1833
1834 case PM_POST_SUSPEND:
1835 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01001836 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001837
1838 spin_lock_irqsave(&host->lock, flags);
1839 host->rescan_disable = 0;
1840 spin_unlock_irqrestore(&host->lock, flags);
1841 mmc_detect_change(host, 0);
1842
1843 }
1844
1845 return 0;
1846}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847#endif
1848
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001849static int __init mmc_init(void)
1850{
1851 int ret;
1852
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01001853 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001854 if (!workqueue)
1855 return -ENOMEM;
1856
1857 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001858 if (ret)
1859 goto destroy_workqueue;
1860
1861 ret = mmc_register_host_class();
1862 if (ret)
1863 goto unregister_bus;
1864
1865 ret = sdio_register_bus();
1866 if (ret)
1867 goto unregister_host_class;
1868
1869 return 0;
1870
1871unregister_host_class:
1872 mmc_unregister_host_class();
1873unregister_bus:
1874 mmc_unregister_bus();
1875destroy_workqueue:
1876 destroy_workqueue(workqueue);
1877
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001878 return ret;
1879}
1880
1881static void __exit mmc_exit(void)
1882{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001883 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001884 mmc_unregister_host_class();
1885 mmc_unregister_bus();
1886 destroy_workqueue(workqueue);
1887}
1888
Nicolas Pitre26074962007-06-16 02:07:53 -04001889subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001890module_exit(mmc_exit);
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892MODULE_LICENSE("GPL");