blob: 94c8d5a9ecae50f68e643b8e31dce528a85a2349 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossmanaaac1b42007-02-28 15:33:10 +01002 * linux/drivers/mmc/core/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
Pierre Ossman5b4fd9a2005-09-06 15:18:56 -07005 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
Pierre Ossmanad3868b2008-06-28 12:52:45 +02006 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Philip Langdalebce40a32006-10-21 12:35:02 +02007 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/completion.h>
17#include <linux/device.h>
18#include <linux/delay.h>
19#include <linux/pagemap.h>
20#include <linux/err.h>
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020021#include <linux/leds.h>
Pierre Ossmanb57c43a2005-09-06 15:18:53 -070022#include <linux/scatterlist.h>
Anton Vorontsov86e82862008-11-26 22:54:17 +030023#include <linux/log2.h>
David Brownell5c139412009-03-11 03:30:43 -080024#include <linux/regulator/consumer.h>
Ohad Ben-Cohene5945732010-11-28 07:21:30 +020025#include <linux/pm_runtime.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include <linux/mmc/card.h>
28#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010029#include <linux/mmc/mmc.h>
30#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010032#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020033#include "bus.h"
34#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020035#include "sdio_bus.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010036
37#include "mmc_ops.h"
38#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020039#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Pierre Ossmanffce2e72007-05-19 14:32:22 +020041static struct workqueue_struct *workqueue;
42
43/*
David Brownellaf517152007-08-08 09:11:32 -070044 * Enabling software CRCs on the data blocks can be a significant (30%)
45 * performance cost, and for other reasons may not always be desired.
46 * So we allow it it to be disabled.
47 */
48int use_spi_crc = 1;
49module_param(use_spi_crc, bool, 0);
50
51/*
Ben Hutchingsbd68e082009-12-14 18:01:29 -080052 * We normally treat cards as removed during suspend if they are not
53 * known to be on a non-removable bus, to avoid the risk of writing
54 * back data to a different card after resume. Allow this to be
55 * overridden if necessary.
56 */
57#ifdef CONFIG_MMC_UNSAFE_RESUME
58int mmc_assume_removable;
59#else
60int mmc_assume_removable = 1;
61#endif
Matt Fleming71d7d3d2010-09-27 09:42:19 +010062EXPORT_SYMBOL(mmc_assume_removable);
Ben Hutchingsbd68e082009-12-14 18:01:29 -080063module_param_named(removable, mmc_assume_removable, bool, 0644);
64MODULE_PARM_DESC(
65 removable,
66 "MMC/SD cards are removable and may be removed during suspend");
67
68/*
Pierre Ossmanffce2e72007-05-19 14:32:22 +020069 * Internal function. Schedule delayed work in the MMC work queue.
70 */
71static int mmc_schedule_delayed_work(struct delayed_work *work,
72 unsigned long delay)
73{
74 return queue_delayed_work(workqueue, work, delay);
75}
76
77/*
78 * Internal function. Flush all scheduled work from the MMC work queue.
79 */
80static void mmc_flush_scheduled_work(void)
81{
82 flush_workqueue(workqueue);
83}
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085/**
Russell Kingfe10c6a2006-05-04 13:51:45 +010086 * mmc_request_done - finish processing an MMC request
87 * @host: MMC host which completed request
88 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *
90 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +010091 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
93void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
94{
95 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +010096 int err = cmd->error;
97
David Brownellaf517152007-08-08 09:11:32 -070098 if (err && cmd->retries && mmc_host_is_spi(host)) {
99 if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND)
100 cmd->retries = 0;
101 }
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 if (err && cmd->retries) {
Pierre Ossmane4d21702007-07-24 21:46:49 +0200104 pr_debug("%s: req failed (CMD%u): %d, retrying...\n",
105 mmc_hostname(host), cmd->opcode, err);
106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 cmd->retries--;
108 cmd->error = 0;
109 host->ops->request(host, mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +0200110 } else {
Pierre Ossmanaf8350c2007-09-24 07:15:48 +0200111 led_trigger_event(host->led, LED_OFF);
112
Pierre Ossmane4d21702007-07-24 21:46:49 +0200113 pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n",
114 mmc_hostname(host), cmd->opcode, err,
115 cmd->resp[0], cmd->resp[1],
116 cmd->resp[2], cmd->resp[3]);
117
118 if (mrq->data) {
119 pr_debug("%s: %d bytes transferred: %d\n",
120 mmc_hostname(host),
121 mrq->data->bytes_xfered, mrq->data->error);
122 }
123
124 if (mrq->stop) {
125 pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n",
126 mmc_hostname(host), mrq->stop->opcode,
127 mrq->stop->error,
128 mrq->stop->resp[0], mrq->stop->resp[1],
129 mrq->stop->resp[2], mrq->stop->resp[3]);
130 }
131
132 if (mrq->done)
133 mrq->done(mrq);
Linus Walleij04566832010-11-08 21:36:50 -0500134
135 mmc_host_clk_gate(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137}
138
139EXPORT_SYMBOL(mmc_request_done);
140
Adrian Bunk39361852007-07-25 00:40:58 +0200141static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
143{
Pierre Ossman976d9272007-04-13 22:47:01 +0200144#ifdef CONFIG_MMC_DEBUG
145 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200146 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +0200147#endif
148
Russell King920e70c2006-05-04 18:22:51 +0100149 pr_debug("%s: starting CMD%u arg %08x flags %08x\n",
150 mmc_hostname(host), mrq->cmd->opcode,
151 mrq->cmd->arg, mrq->cmd->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Pierre Ossmane4d21702007-07-24 21:46:49 +0200153 if (mrq->data) {
154 pr_debug("%s: blksz %d blocks %d flags %08x "
155 "tsac %d ms nsac %d\n",
156 mmc_hostname(host), mrq->data->blksz,
157 mrq->data->blocks, mrq->data->flags,
Pierre Ossmance252ed2007-08-07 14:06:18 +0200158 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +0200159 mrq->data->timeout_clks);
160 }
161
162 if (mrq->stop) {
163 pr_debug("%s: CMD%u arg %08x flags %08x\n",
164 mmc_hostname(host), mrq->stop->opcode,
165 mrq->stop->arg, mrq->stop->flags);
166 }
167
Pierre Ossmanf22ee4e2006-12-26 15:11:23 +0100168 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 mrq->cmd->error = 0;
171 mrq->cmd->mrq = mrq;
172 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100173 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +0100174 BUG_ON(mrq->data->blocks > host->max_blk_count);
175 BUG_ON(mrq->data->blocks * mrq->data->blksz >
176 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100177
Pierre Ossman976d9272007-04-13 22:47:01 +0200178#ifdef CONFIG_MMC_DEBUG
179 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +0200180 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
181 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +0200182 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
183#endif
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 mrq->cmd->data = mrq->data;
186 mrq->data->error = 0;
187 mrq->data->mrq = mrq;
188 if (mrq->stop) {
189 mrq->data->stop = mrq->stop;
190 mrq->stop->error = 0;
191 mrq->stop->mrq = mrq;
192 }
193 }
Linus Walleij04566832010-11-08 21:36:50 -0500194 mmc_host_clk_ungate(host);
Pierre Tardy66c036e2011-02-06 19:02:48 +0100195 led_trigger_event(host->led, LED_FULL);
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{
Chris Ball24f5b532011-04-13 23:49:45 -0400239 struct mmc_request mrq = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Pierre Ossmand84075c82007-08-09 13:23:56 +0200241 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 memset(cmd->resp, 0, sizeof(cmd->resp));
244 cmd->retries = retries;
245
246 mrq.cmd = cmd;
247 cmd->data = NULL;
248
249 mmc_wait_for_req(host, &mrq);
250
251 return cmd->error;
252}
253
254EXPORT_SYMBOL(mmc_wait_for_cmd);
255
Pierre Ossman335eadf2005-09-06 15:18:50 -0700256/**
Russell Kingd773d722006-09-07 15:57:12 +0100257 * mmc_set_data_timeout - set the timeout for a data command
258 * @data: data phase for command
259 * @card: the MMC card associated with the data transfer
Pierre Ossman67a61c42007-07-11 20:22:11 +0200260 *
261 * Computes the data timeout parameters according to the
262 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +0100263 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200264void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +0100265{
266 unsigned int mult;
267
268 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +0200269 * SDIO cards only define an upper 1 s limit on access.
270 */
271 if (mmc_card_sdio(card)) {
272 data->timeout_ns = 1000000000;
273 data->timeout_clks = 0;
274 return;
275 }
276
277 /*
Russell Kingd773d722006-09-07 15:57:12 +0100278 * SD cards use a 100 multiplier rather than 10
279 */
280 mult = mmc_card_sd(card) ? 100 : 10;
281
282 /*
283 * Scale up the multiplier (and therefore the timeout) by
284 * the r2w factor for writes.
285 */
Pierre Ossmanb146d262007-07-24 19:16:54 +0200286 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +0100287 mult <<= card->csd.r2w_factor;
288
289 data->timeout_ns = card->csd.tacc_ns * mult;
290 data->timeout_clks = card->csd.tacc_clks * mult;
291
292 /*
293 * SD cards also have an upper limit on the timeout.
294 */
295 if (mmc_card_sd(card)) {
296 unsigned int timeout_us, limit_us;
297
298 timeout_us = data->timeout_ns / 1000;
Linus Walleije9b86842011-01-05 00:44:32 +0100299 if (mmc_host_clk_rate(card->host))
300 timeout_us += data->timeout_clks * 1000 /
301 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +0100302
Pierre Ossmanb146d262007-07-24 19:16:54 +0200303 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +0100304 /*
305 * The limit is really 250 ms, but that is
306 * insufficient for some crappy cards.
307 */
308 limit_us = 300000;
Russell Kingd773d722006-09-07 15:57:12 +0100309 else
310 limit_us = 100000;
311
Philip Langdalefba68bd2007-01-04 06:57:32 -0800312 /*
313 * SDHC cards always use these fixed values.
314 */
315 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +0100316 data->timeout_ns = limit_us * 1000;
317 data->timeout_clks = 0;
318 }
319 }
Wolfgang Mueesc0c88872009-03-11 14:28:39 +0100320 /*
321 * Some cards need very high timeouts if driven in SPI mode.
322 * The worst observed timeout was 900ms after writing a
323 * continuous stream of data until the internal logic
324 * overflowed.
325 */
326 if (mmc_host_is_spi(card->host)) {
327 if (data->flags & MMC_DATA_WRITE) {
328 if (data->timeout_ns < 1000000000)
329 data->timeout_ns = 1000000000; /* 1s */
330 } else {
331 if (data->timeout_ns < 100000000)
332 data->timeout_ns = 100000000; /* 100ms */
333 }
334 }
Russell Kingd773d722006-09-07 15:57:12 +0100335}
336EXPORT_SYMBOL(mmc_set_data_timeout);
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +0200339 * mmc_align_data_size - pads a transfer size to a more optimal value
340 * @card: the MMC card associated with the data transfer
341 * @sz: original transfer size
342 *
343 * Pads the original data size with a number of extra bytes in
344 * order to avoid controller bugs and/or performance hits
345 * (e.g. some controllers revert to PIO for certain sizes).
346 *
347 * Returns the improved size, which might be unmodified.
348 *
349 * Note that this function is only relevant when issuing a
350 * single scatter gather entry.
351 */
352unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz)
353{
354 /*
355 * FIXME: We don't have a system for the controller to tell
356 * the core about its problems yet, so for now we just 32-bit
357 * align the size.
358 */
359 sz = ((sz + 3) / 4) * 4;
360
361 return sz;
362}
363EXPORT_SYMBOL(mmc_align_data_size);
364
365/**
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700366 * mmc_host_enable - enable a host.
367 * @host: mmc host to enable
368 *
369 * Hosts that support power saving can use the 'enable' and 'disable'
370 * methods to exit and enter power saving states. For more information
371 * see comments for struct mmc_host_ops.
372 */
373int mmc_host_enable(struct mmc_host *host)
374{
375 if (!(host->caps & MMC_CAP_DISABLE))
376 return 0;
377
378 if (host->en_dis_recurs)
379 return 0;
380
381 if (host->nesting_cnt++)
382 return 0;
383
384 cancel_delayed_work_sync(&host->disable);
385
386 if (host->enabled)
387 return 0;
388
389 if (host->ops->enable) {
390 int err;
391
392 host->en_dis_recurs = 1;
393 err = host->ops->enable(host);
394 host->en_dis_recurs = 0;
395
396 if (err) {
397 pr_debug("%s: enable error %d\n",
398 mmc_hostname(host), err);
399 return err;
400 }
401 }
402 host->enabled = 1;
403 return 0;
404}
405EXPORT_SYMBOL(mmc_host_enable);
406
407static int mmc_host_do_disable(struct mmc_host *host, int lazy)
408{
409 if (host->ops->disable) {
410 int err;
411
412 host->en_dis_recurs = 1;
413 err = host->ops->disable(host, lazy);
414 host->en_dis_recurs = 0;
415
416 if (err < 0) {
417 pr_debug("%s: disable error %d\n",
418 mmc_hostname(host), err);
419 return err;
420 }
421 if (err > 0) {
422 unsigned long delay = msecs_to_jiffies(err);
423
424 mmc_schedule_delayed_work(&host->disable, delay);
425 }
426 }
427 host->enabled = 0;
428 return 0;
429}
430
431/**
432 * mmc_host_disable - disable a host.
433 * @host: mmc host to disable
434 *
435 * Hosts that support power saving can use the 'enable' and 'disable'
436 * methods to exit and enter power saving states. For more information
437 * see comments for struct mmc_host_ops.
438 */
439int mmc_host_disable(struct mmc_host *host)
440{
441 int err;
442
443 if (!(host->caps & MMC_CAP_DISABLE))
444 return 0;
445
446 if (host->en_dis_recurs)
447 return 0;
448
449 if (--host->nesting_cnt)
450 return 0;
451
452 if (!host->enabled)
453 return 0;
454
455 err = mmc_host_do_disable(host, 0);
456 return err;
457}
458EXPORT_SYMBOL(mmc_host_disable);
459
460/**
Nicolas Pitre2342f332007-06-30 16:21:52 +0200461 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +0200463 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
Nicolas Pitre2342f332007-06-30 16:21:52 +0200465 * Claim a host for a set of operations. If @abort is non null and
466 * dereference a non-zero value then this will return prematurely with
467 * that non-zero value without acquiring the lock. Returns zero
468 * with the lock held otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 */
Nicolas Pitre2342f332007-06-30 16:21:52 +0200470int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 DECLARE_WAITQUEUE(wait, current);
473 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +0200474 int stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
Pierre Ossmancf795bf2007-07-11 20:28:02 +0200476 might_sleep();
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 add_wait_queue(&host->wq, &wait);
479 spin_lock_irqsave(&host->lock, flags);
480 while (1) {
481 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200482 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700483 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 break;
485 spin_unlock_irqrestore(&host->lock, flags);
486 schedule();
487 spin_lock_irqsave(&host->lock, flags);
488 }
489 set_current_state(TASK_RUNNING);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700490 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +0200491 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700492 host->claimer = current;
493 host->claim_cnt += 1;
494 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +0200495 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 spin_unlock_irqrestore(&host->lock, flags);
497 remove_wait_queue(&host->wq, &wait);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700498 if (!stop)
499 mmc_host_enable(host);
Nicolas Pitre2342f332007-06-30 16:21:52 +0200500 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Nicolas Pitre2342f332007-06-30 16:21:52 +0200503EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Adrian Hunter319a3f12009-09-22 16:44:30 -0700505/**
506 * mmc_try_claim_host - try exclusively to claim a host
507 * @host: mmc host to claim
508 *
509 * Returns %1 if the host is claimed, %0 otherwise.
510 */
511int mmc_try_claim_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700512{
513 int claimed_host = 0;
514 unsigned long flags;
515
516 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700517 if (!host->claimed || host->claimer == current) {
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700518 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -0700519 host->claimer = current;
520 host->claim_cnt += 1;
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700521 claimed_host = 1;
522 }
523 spin_unlock_irqrestore(&host->lock, flags);
524 return claimed_host;
525}
Adrian Hunter319a3f12009-09-22 16:44:30 -0700526EXPORT_SYMBOL(mmc_try_claim_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700527
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100528/**
529 * mmc_do_release_host - release a claimed host
530 * @host: mmc host to release
531 *
532 * If you successfully claimed a host, this function will
533 * release it again.
534 */
535void mmc_do_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700536{
537 unsigned long flags;
538
539 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -0700540 if (--host->claim_cnt) {
541 /* Release for nested claim */
542 spin_unlock_irqrestore(&host->lock, flags);
543 } else {
544 host->claimed = 0;
545 host->claimer = NULL;
546 spin_unlock_irqrestore(&host->lock, flags);
547 wake_up(&host->wq);
548 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700549}
Ulf Hanssonab1efd22011-03-09 09:11:02 +0100550EXPORT_SYMBOL(mmc_do_release_host);
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700551
552void mmc_host_deeper_disable(struct work_struct *work)
553{
554 struct mmc_host *host =
555 container_of(work, struct mmc_host, disable.work);
556
557 /* If the host is claimed then we do not want to disable it anymore */
558 if (!mmc_try_claim_host(host))
559 return;
560 mmc_host_do_disable(host, 1);
561 mmc_do_release_host(host);
562}
563
564/**
565 * mmc_host_lazy_disable - lazily disable a host.
566 * @host: mmc host to disable
567 *
568 * Hosts that support power saving can use the 'enable' and 'disable'
569 * methods to exit and enter power saving states. For more information
570 * see comments for struct mmc_host_ops.
571 */
572int mmc_host_lazy_disable(struct mmc_host *host)
573{
574 if (!(host->caps & MMC_CAP_DISABLE))
575 return 0;
576
577 if (host->en_dis_recurs)
578 return 0;
579
580 if (--host->nesting_cnt)
581 return 0;
582
583 if (!host->enabled)
584 return 0;
585
586 if (host->disable_delay) {
587 mmc_schedule_delayed_work(&host->disable,
588 msecs_to_jiffies(host->disable_delay));
589 return 0;
590 } else
591 return mmc_host_do_disable(host, 1);
592}
593EXPORT_SYMBOL(mmc_host_lazy_disable);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595/**
596 * mmc_release_host - release a host
597 * @host: mmc host to release
598 *
599 * Release a MMC host, allowing others to claim the host
600 * for their operations.
601 */
602void mmc_release_host(struct mmc_host *host)
603{
Pierre Ossmand84075c82007-08-09 13:23:56 +0200604 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700606 mmc_host_lazy_disable(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Adrian Hunter8ea926b2009-09-22 16:44:29 -0700608 mmc_do_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611EXPORT_SYMBOL(mmc_release_host);
612
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100613/*
614 * Internal function that does the actual ios call to the host driver,
615 * optionally printing some debug output.
616 */
Russell King920e70c2006-05-04 18:22:51 +0100617static inline void mmc_set_ios(struct mmc_host *host)
618{
619 struct mmc_ios *ios = &host->ios;
620
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100621 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
622 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +0100623 mmc_hostname(host), ios->clock, ios->bus_mode,
624 ios->power_mode, ios->chip_select, ios->vdd,
Pierre Ossmancd9277c2007-02-18 12:07:47 +0100625 ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800626
Linus Walleij04566832010-11-08 21:36:50 -0500627 if (ios->clock > 0)
628 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +0100629 host->ops->set_ios(host, ios);
630}
631
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100632/*
633 * Control chip select pin on a host.
634 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100635void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700636{
Pierre Ossmanda7fbe52006-12-24 22:46:55 +0100637 host->ios.chip_select = mode;
638 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
641/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100642 * Sets the host clock to the highest possible frequency that
643 * is below "hz".
644 */
645void mmc_set_clock(struct mmc_host *host, unsigned int hz)
646{
647 WARN_ON(hz < host->f_min);
648
649 if (hz > host->f_max)
650 hz = host->f_max;
651
652 host->ios.clock = hz;
653 mmc_set_ios(host);
654}
655
Linus Walleij04566832010-11-08 21:36:50 -0500656#ifdef CONFIG_MMC_CLKGATE
657/*
658 * This gates the clock by setting it to 0 Hz.
659 */
660void mmc_gate_clock(struct mmc_host *host)
661{
662 unsigned long flags;
663
664 spin_lock_irqsave(&host->clk_lock, flags);
665 host->clk_old = host->ios.clock;
666 host->ios.clock = 0;
667 host->clk_gated = true;
668 spin_unlock_irqrestore(&host->clk_lock, flags);
669 mmc_set_ios(host);
670}
671
672/*
673 * This restores the clock from gating by using the cached
674 * clock value.
675 */
676void mmc_ungate_clock(struct mmc_host *host)
677{
678 /*
679 * We should previously have gated the clock, so the clock shall
680 * be 0 here! The clock may however be 0 during initialization,
681 * when some request operations are performed before setting
682 * the frequency. When ungate is requested in that situation
683 * we just ignore the call.
684 */
685 if (host->clk_old) {
686 BUG_ON(host->ios.clock);
687 /* This call will also set host->clk_gated to false */
688 mmc_set_clock(host, host->clk_old);
689 }
690}
691
692void mmc_set_ungated(struct mmc_host *host)
693{
694 unsigned long flags;
695
696 /*
697 * We've been given a new frequency while the clock is gated,
698 * so make sure we regard this as ungating it.
699 */
700 spin_lock_irqsave(&host->clk_lock, flags);
701 host->clk_gated = false;
702 spin_unlock_irqrestore(&host->clk_lock, flags);
703}
704
705#else
706void mmc_set_ungated(struct mmc_host *host)
707{
708}
709#endif
710
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100711/*
712 * Change the bus mode (open drain/push-pull) of a host.
713 */
714void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
715{
716 host->ios.bus_mode = mode;
717 mmc_set_ios(host);
718}
719
720/*
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300721 * Change data bus width and DDR mode of a host.
722 */
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300723void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width,
724 unsigned int ddr)
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300725{
726 host->ios.bus_width = width;
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300727 host->ios.ddr = ddr;
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +0300728 mmc_set_ios(host);
729}
730
731/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100732 * Change data bus width of a host.
733 */
734void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
735{
Adrian Hunter49e3b5a2010-10-11 12:43:50 +0300736 mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE);
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100737}
738
Anton Vorontsov86e82862008-11-26 22:54:17 +0300739/**
740 * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number
741 * @vdd: voltage (mV)
742 * @low_bits: prefer low bits in boundary cases
743 *
744 * This function returns the OCR bit number according to the provided @vdd
745 * value. If conversion is not possible a negative errno value returned.
746 *
747 * Depending on the @low_bits flag the function prefers low or high OCR bits
748 * on boundary voltages. For example,
749 * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33);
750 * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34);
751 *
752 * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21).
753 */
754static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits)
755{
756 const int max_bit = ilog2(MMC_VDD_35_36);
757 int bit;
758
759 if (vdd < 1650 || vdd > 3600)
760 return -EINVAL;
761
762 if (vdd >= 1650 && vdd <= 1950)
763 return ilog2(MMC_VDD_165_195);
764
765 if (low_bits)
766 vdd -= 1;
767
768 /* Base 2000 mV, step 100 mV, bit's base 8. */
769 bit = (vdd - 2000) / 100 + 8;
770 if (bit > max_bit)
771 return max_bit;
772 return bit;
773}
774
775/**
776 * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask
777 * @vdd_min: minimum voltage value (mV)
778 * @vdd_max: maximum voltage value (mV)
779 *
780 * This function returns the OCR mask bits according to the provided @vdd_min
781 * and @vdd_max values. If conversion is not possible the function returns 0.
782 *
783 * Notes wrt boundary cases:
784 * This function sets the OCR bits for all boundary voltages, for example
785 * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 |
786 * MMC_VDD_34_35 mask.
787 */
788u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max)
789{
790 u32 mask = 0;
791
792 if (vdd_max < vdd_min)
793 return 0;
794
795 /* Prefer high bits for the boundary vdd_max values. */
796 vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false);
797 if (vdd_max < 0)
798 return 0;
799
800 /* Prefer low bits for the boundary vdd_min values. */
801 vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true);
802 if (vdd_min < 0)
803 return 0;
804
805 /* Fill the mask, from max bit to min bit. */
806 while (vdd_max >= vdd_min)
807 mask |= 1 << vdd_max--;
808
809 return mask;
810}
811EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
812
David Brownell5c139412009-03-11 03:30:43 -0800813#ifdef CONFIG_REGULATOR
814
815/**
816 * mmc_regulator_get_ocrmask - return mask of supported voltages
817 * @supply: regulator to use
818 *
819 * This returns either a negative errno, or a mask of voltages that
820 * can be provided to MMC/SD/SDIO devices using the specified voltage
821 * regulator. This would normally be called before registering the
822 * MMC host adapter.
823 */
824int mmc_regulator_get_ocrmask(struct regulator *supply)
825{
826 int result = 0;
827 int count;
828 int i;
829
830 count = regulator_count_voltages(supply);
831 if (count < 0)
832 return count;
833
834 for (i = 0; i < count; i++) {
835 int vdd_uV;
836 int vdd_mV;
837
838 vdd_uV = regulator_list_voltage(supply, i);
839 if (vdd_uV <= 0)
840 continue;
841
842 vdd_mV = vdd_uV / 1000;
843 result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV);
844 }
845
846 return result;
847}
848EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
849
850/**
851 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -0400852 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -0800853 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -0400854 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -0800855 *
856 * Returns zero on success, else negative errno.
857 *
858 * MMC host drivers may use this to enable or disable a regulator using
859 * a particular supply voltage. This would normally be called from the
860 * set_ios() method.
861 */
Linus Walleij99fc5132010-09-29 01:08:27 -0400862int mmc_regulator_set_ocr(struct mmc_host *mmc,
863 struct regulator *supply,
864 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -0800865{
866 int result = 0;
867 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -0800868
869 if (vdd_bit) {
870 int tmp;
871 int voltage;
872
873 /* REVISIT mmc_vddrange_to_ocrmask() may have set some
874 * bits this regulator doesn't quite support ... don't
875 * be too picky, most cards and regulators are OK with
876 * a 0.1V range goof (it's a small error percentage).
877 */
878 tmp = vdd_bit - ilog2(MMC_VDD_165_195);
879 if (tmp == 0) {
880 min_uV = 1650 * 1000;
881 max_uV = 1950 * 1000;
882 } else {
883 min_uV = 1900 * 1000 + tmp * 100 * 1000;
884 max_uV = min_uV + 100 * 1000;
885 }
886
887 /* avoid needless changes to this voltage; the regulator
888 * might not allow this operation
889 */
890 voltage = regulator_get_voltage(supply);
891 if (voltage < 0)
892 result = voltage;
893 else if (voltage < min_uV || voltage > max_uV)
894 result = regulator_set_voltage(supply, min_uV, max_uV);
895 else
896 result = 0;
897
Linus Walleij99fc5132010-09-29 01:08:27 -0400898 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -0800899 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -0400900 if (!result)
901 mmc->regulator_enabled = true;
902 }
903 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -0800904 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -0400905 if (result == 0)
906 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -0800907 }
908
Linus Walleij99fc5132010-09-29 01:08:27 -0400909 if (result)
910 dev_err(mmc_dev(mmc),
911 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -0800912 return result;
913}
914EXPORT_SYMBOL(mmc_regulator_set_ocr);
915
Linus Walleij99fc5132010-09-29 01:08:27 -0400916#endif /* CONFIG_REGULATOR */
David Brownell5c139412009-03-11 03:30:43 -0800917
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100918/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 * Mask off any voltages we don't support and select
920 * the lowest voltage
921 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100922u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 int bit;
925
926 ocr &= host->ocr_avail;
927
928 bit = ffs(ocr);
929 if (bit) {
930 bit -= 1;
931
Timo Teras63ef7312006-11-02 19:43:27 +0100932 ocr &= 3 << bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 host->ios.vdd = bit;
Russell King920e70c2006-05-04 18:22:51 +0100935 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 } else {
David Brownellf6e10b82008-12-31 09:50:30 -0800937 pr_warning("%s: host doesn't support card's voltages\n",
938 mmc_hostname(host));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 ocr = 0;
940 }
941
942 return ocr;
943}
944
Philip Rakity261bbd42011-05-13 11:17:17 +0530945int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11)
Arindam Nathf2119df2011-05-05 12:18:57 +0530946{
947 struct mmc_command cmd = {0};
948 int err = 0;
949
950 BUG_ON(!host);
951
952 /*
953 * Send CMD11 only if the request is to switch the card to
954 * 1.8V signalling.
955 */
Philip Rakity261bbd42011-05-13 11:17:17 +0530956 if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) {
Arindam Nathf2119df2011-05-05 12:18:57 +0530957 cmd.opcode = SD_SWITCH_VOLTAGE;
958 cmd.arg = 0;
959 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
960
961 err = mmc_wait_for_cmd(host, &cmd, 0);
962 if (err)
963 return err;
964
965 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR))
966 return -EIO;
967 }
968
969 host->ios.signal_voltage = signal_voltage;
970
971 if (host->ops->start_signal_voltage_switch)
972 err = host->ops->start_signal_voltage_switch(host, &host->ios);
973
974 return err;
975}
976
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100978 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100980void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Pierre Ossman7ea239d2006-12-31 00:11:32 +0100982 host->ios.timing = timing;
983 mmc_set_ios(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -0700984}
985
986/*
Arindam Nathd6d50a12011-05-05 12:18:59 +0530987 * Select appropriate driver type for host.
988 */
989void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
990{
991 host->ios.drv_type = drv_type;
992 mmc_set_ios(host);
993}
994
995/*
Russell King45f82452005-12-14 14:57:35 +0000996 * Apply power to the MMC stack. This is a two-stage process.
997 * First, we enable power to the card without the clock running.
998 * We then wait a bit for the power to stabilise. Finally,
999 * enable the bus drivers and clock to the card.
1000 *
1001 * We must _NOT_ enable the clock prior to power stablising.
1002 *
1003 * If a host does all the power sequencing itself, ignore the
1004 * initial MMC_POWER_UP stage.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 */
1006static void mmc_power_up(struct mmc_host *host)
1007{
Balaji Rao500f3562009-09-22 16:44:18 -07001008 int bit;
1009
1010 /* If ocr is set, we use it */
1011 if (host->ocr)
1012 bit = ffs(host->ocr) - 1;
1013 else
1014 bit = fls(host->ocr_avail) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 host->ios.vdd = bit;
David Brownellaf517152007-08-08 09:11:32 -07001017 if (mmc_host_is_spi(host)) {
1018 host->ios.chip_select = MMC_CS_HIGH;
1019 host->ios.bus_mode = MMC_BUSMODE_PUSHPULL;
1020 } else {
1021 host->ios.chip_select = MMC_CS_DONTCARE;
1022 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 host->ios.power_mode = MMC_POWER_UP;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001025 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001026 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001027 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001029 /*
1030 * This delay should be sufficient to allow the power supply
1031 * to reach the minimum voltage.
1032 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001033 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001035 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02001036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01001038 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02001040 /*
1041 * This delay must be at least 74 clock sizes, or 1 ms, or the
1042 * time required to reach a stable voltage.
1043 */
José M. Fernández79bccc52009-03-10 02:21:21 +01001044 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045}
1046
1047static void mmc_power_off(struct mmc_host *host)
1048{
1049 host->ios.clock = 0;
1050 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01001051
1052 /*
1053 * Reset ocr mask to be the highest possible voltage supported for
1054 * this mmc host. This value will be used at next power up.
1055 */
1056 host->ocr = 1 << (fls(host->ocr_avail) - 1);
1057
David Brownellaf517152007-08-08 09:11:32 -07001058 if (!mmc_host_is_spi(host)) {
1059 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
1060 host->ios.chip_select = MMC_CS_DONTCARE;
1061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 host->ios.power_mode = MMC_POWER_OFF;
Pierre Ossmanf2182782005-09-06 15:18:55 -07001063 host->ios.bus_width = MMC_BUS_WIDTH_1;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001064 host->ios.timing = MMC_TIMING_LEGACY;
Russell King920e70c2006-05-04 18:22:51 +01001065 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068/*
Adrian Bunk39361852007-07-25 00:40:58 +02001069 * Cleanup when the last reference to the bus operator is dropped.
1070 */
Adrian Bunk261172f2008-04-13 21:15:47 +03001071static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02001072{
1073 BUG_ON(!host);
1074 BUG_ON(host->bus_refs);
1075 BUG_ON(!host->bus_dead);
1076
1077 host->bus_ops = NULL;
1078}
1079
1080/*
1081 * Increase reference count of bus operator
1082 */
1083static inline void mmc_bus_get(struct mmc_host *host)
1084{
1085 unsigned long flags;
1086
1087 spin_lock_irqsave(&host->lock, flags);
1088 host->bus_refs++;
1089 spin_unlock_irqrestore(&host->lock, flags);
1090}
1091
1092/*
1093 * Decrease reference count of bus operator and free it if
1094 * it is the last reference.
1095 */
1096static inline void mmc_bus_put(struct mmc_host *host)
1097{
1098 unsigned long flags;
1099
1100 spin_lock_irqsave(&host->lock, flags);
1101 host->bus_refs--;
1102 if ((host->bus_refs == 0) && host->bus_ops)
1103 __mmc_release_bus(host);
1104 spin_unlock_irqrestore(&host->lock, flags);
1105}
1106
1107/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001108 * Assign a mmc bus handler to a host. Only one bus handler may control a
1109 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001111void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001113 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001115 BUG_ON(!host);
1116 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001117
Pierre Ossmand84075c82007-08-09 13:23:56 +02001118 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001119
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001120 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001121
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001122 BUG_ON(host->bus_ops);
1123 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01001124
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001125 host->bus_ops = ops;
1126 host->bus_refs = 1;
1127 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001129 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001133 * Remove the current bus handler from a host. Assumes that there are
1134 * no interesting cards left, so the bus is powered down.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001136void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001138 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001140 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Pierre Ossmand84075c82007-08-09 13:23:56 +02001142 WARN_ON(!host->claimed);
1143 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001145 spin_lock_irqsave(&host->lock, flags);
1146
1147 host->bus_dead = 1;
1148
1149 spin_unlock_irqrestore(&host->lock, flags);
1150
1151 mmc_power_off(host);
1152
1153 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154}
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156/**
1157 * mmc_detect_change - process change of state on a MMC socket
1158 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01001159 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02001161 * MMC drivers should call this when they detect a card has been
1162 * inserted or removed. The MMC layer will confirm that any
1163 * present card is still functional, and initialize any newly
1164 * inserted.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Richard Purdie8dc00332005-09-08 17:53:01 +01001166void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001168#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001169 unsigned long flags;
Andrew Morton01f41ec2007-05-09 02:32:34 -07001170 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmand84075c82007-08-09 13:23:56 +02001171 WARN_ON(host->removed);
Andrew Morton01f41ec2007-05-09 02:32:34 -07001172 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001173#endif
1174
David Howellsc4028952006-11-22 14:57:56 +00001175 mmc_schedule_delayed_work(&host->detect, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176}
1177
1178EXPORT_SYMBOL(mmc_detect_change);
1179
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001180void mmc_init_erase(struct mmc_card *card)
1181{
1182 unsigned int sz;
1183
1184 if (is_power_of_2(card->erase_size))
1185 card->erase_shift = ffs(card->erase_size) - 1;
1186 else
1187 card->erase_shift = 0;
1188
1189 /*
1190 * It is possible to erase an arbitrarily large area of an SD or MMC
1191 * card. That is not desirable because it can take a long time
1192 * (minutes) potentially delaying more important I/O, and also the
1193 * timeout calculations become increasingly hugely over-estimated.
1194 * Consequently, 'pref_erase' is defined as a guide to limit erases
1195 * to that size and alignment.
1196 *
1197 * For SD cards that define Allocation Unit size, limit erases to one
1198 * Allocation Unit at a time. For MMC cards that define High Capacity
1199 * Erase Size, whether it is switched on or not, limit to that size.
1200 * Otherwise just have a stab at a good value. For modern cards it
1201 * will end up being 4MiB. Note that if the value is too small, it
1202 * can end up taking longer to erase.
1203 */
1204 if (mmc_card_sd(card) && card->ssr.au) {
1205 card->pref_erase = card->ssr.au;
1206 card->erase_shift = ffs(card->ssr.au) - 1;
1207 } else if (card->ext_csd.hc_erase_size) {
1208 card->pref_erase = card->ext_csd.hc_erase_size;
1209 } else {
1210 sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
1211 if (sz < 128)
1212 card->pref_erase = 512 * 1024 / 512;
1213 else if (sz < 512)
1214 card->pref_erase = 1024 * 1024 / 512;
1215 else if (sz < 1024)
1216 card->pref_erase = 2 * 1024 * 1024 / 512;
1217 else
1218 card->pref_erase = 4 * 1024 * 1024 / 512;
1219 if (card->pref_erase < card->erase_size)
1220 card->pref_erase = card->erase_size;
1221 else {
1222 sz = card->pref_erase % card->erase_size;
1223 if (sz)
1224 card->pref_erase += card->erase_size - sz;
1225 }
1226 }
1227}
1228
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001229static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
1230 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001231{
1232 unsigned int erase_timeout;
1233
1234 if (card->ext_csd.erase_group_def & 1) {
1235 /* High Capacity Erase Group Size uses HC timeouts */
1236 if (arg == MMC_TRIM_ARG)
1237 erase_timeout = card->ext_csd.trim_timeout;
1238 else
1239 erase_timeout = card->ext_csd.hc_erase_timeout;
1240 } else {
1241 /* CSD Erase Group Size uses write timeout */
1242 unsigned int mult = (10 << card->csd.r2w_factor);
1243 unsigned int timeout_clks = card->csd.tacc_clks * mult;
1244 unsigned int timeout_us;
1245
1246 /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
1247 if (card->csd.tacc_ns < 1000000)
1248 timeout_us = (card->csd.tacc_ns * mult) / 1000;
1249 else
1250 timeout_us = (card->csd.tacc_ns / 1000) * mult;
1251
1252 /*
1253 * ios.clock is only a target. The real clock rate might be
1254 * less but not that much less, so fudge it by multiplying by 2.
1255 */
1256 timeout_clks <<= 1;
1257 timeout_us += (timeout_clks * 1000) /
1258 (card->host->ios.clock / 1000);
1259
1260 erase_timeout = timeout_us / 1000;
1261
1262 /*
1263 * Theoretically, the calculation could underflow so round up
1264 * to 1ms in that case.
1265 */
1266 if (!erase_timeout)
1267 erase_timeout = 1;
1268 }
1269
1270 /* Multiplier for secure operations */
1271 if (arg & MMC_SECURE_ARGS) {
1272 if (arg == MMC_SECURE_ERASE_ARG)
1273 erase_timeout *= card->ext_csd.sec_erase_mult;
1274 else
1275 erase_timeout *= card->ext_csd.sec_trim_mult;
1276 }
1277
1278 erase_timeout *= qty;
1279
1280 /*
1281 * Ensure at least a 1 second timeout for SPI as per
1282 * 'mmc_set_data_timeout()'
1283 */
1284 if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
1285 erase_timeout = 1000;
1286
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001287 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001288}
1289
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001290static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
1291 unsigned int arg,
1292 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001293{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001294 unsigned int erase_timeout;
1295
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001296 if (card->ssr.erase_timeout) {
1297 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001298 erase_timeout = card->ssr.erase_timeout * qty +
1299 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001300 } else {
1301 /*
1302 * Erase timeout not specified in SD Status Register (SSR) so
1303 * use 250ms per write block.
1304 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001305 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001306 }
1307
1308 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001309 if (erase_timeout < 1000)
1310 erase_timeout = 1000;
1311
1312 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001313}
1314
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001315static unsigned int mmc_erase_timeout(struct mmc_card *card,
1316 unsigned int arg,
1317 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001318{
1319 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001320 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001321 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001322 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001323}
1324
1325static int mmc_do_erase(struct mmc_card *card, unsigned int from,
1326 unsigned int to, unsigned int arg)
1327{
Chris Ball1278dba2011-04-13 23:40:30 -04001328 struct mmc_command cmd = {0};
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001329 unsigned int qty = 0;
1330 int err;
1331
1332 /*
1333 * qty is used to calculate the erase timeout which depends on how many
1334 * erase groups (or allocation units in SD terminology) are affected.
1335 * We count erasing part of an erase group as one erase group.
1336 * For SD, the allocation units are always a power of 2. For MMC, the
1337 * erase group size is almost certainly also power of 2, but it does not
1338 * seem to insist on that in the JEDEC standard, so we fall back to
1339 * division in that case. SD may not specify an allocation unit size,
1340 * in which case the timeout is based on the number of write blocks.
1341 *
1342 * Note that the timeout for secure trim 2 will only be correct if the
1343 * number of erase groups specified is the same as the total of all
1344 * preceding secure trim 1 commands. Since the power may have been
1345 * lost since the secure trim 1 commands occurred, it is generally
1346 * impossible to calculate the secure trim 2 timeout correctly.
1347 */
1348 if (card->erase_shift)
1349 qty += ((to >> card->erase_shift) -
1350 (from >> card->erase_shift)) + 1;
1351 else if (mmc_card_sd(card))
1352 qty += to - from + 1;
1353 else
1354 qty += ((to / card->erase_size) -
1355 (from / card->erase_size)) + 1;
1356
1357 if (!mmc_card_blockaddr(card)) {
1358 from <<= 9;
1359 to <<= 9;
1360 }
1361
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001362 if (mmc_card_sd(card))
1363 cmd.opcode = SD_ERASE_WR_BLK_START;
1364 else
1365 cmd.opcode = MMC_ERASE_GROUP_START;
1366 cmd.arg = from;
1367 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1368 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1369 if (err) {
1370 printk(KERN_ERR "mmc_erase: group start error %d, "
1371 "status %#x\n", err, cmd.resp[0]);
1372 err = -EINVAL;
1373 goto out;
1374 }
1375
1376 memset(&cmd, 0, sizeof(struct mmc_command));
1377 if (mmc_card_sd(card))
1378 cmd.opcode = SD_ERASE_WR_BLK_END;
1379 else
1380 cmd.opcode = MMC_ERASE_GROUP_END;
1381 cmd.arg = to;
1382 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1383 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1384 if (err) {
1385 printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
1386 err, cmd.resp[0]);
1387 err = -EINVAL;
1388 goto out;
1389 }
1390
1391 memset(&cmd, 0, sizeof(struct mmc_command));
1392 cmd.opcode = MMC_ERASE;
1393 cmd.arg = arg;
1394 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Andrei Warkentineaa02f72011-04-11 16:13:41 -05001395 cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07001396 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1397 if (err) {
1398 printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
1399 err, cmd.resp[0]);
1400 err = -EIO;
1401 goto out;
1402 }
1403
1404 if (mmc_host_is_spi(card->host))
1405 goto out;
1406
1407 do {
1408 memset(&cmd, 0, sizeof(struct mmc_command));
1409 cmd.opcode = MMC_SEND_STATUS;
1410 cmd.arg = card->rca << 16;
1411 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
1412 /* Do not retry else we can't see errors */
1413 err = mmc_wait_for_cmd(card->host, &cmd, 0);
1414 if (err || (cmd.resp[0] & 0xFDF92000)) {
1415 printk(KERN_ERR "error %d requesting status %#x\n",
1416 err, cmd.resp[0]);
1417 err = -EIO;
1418 goto out;
1419 }
1420 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
1421 R1_CURRENT_STATE(cmd.resp[0]) == 7);
1422out:
1423 return err;
1424}
1425
1426/**
1427 * mmc_erase - erase sectors.
1428 * @card: card to erase
1429 * @from: first sector to erase
1430 * @nr: number of sectors to erase
1431 * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
1432 *
1433 * Caller must claim host before calling this function.
1434 */
1435int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
1436 unsigned int arg)
1437{
1438 unsigned int rem, to = from + nr;
1439
1440 if (!(card->host->caps & MMC_CAP_ERASE) ||
1441 !(card->csd.cmdclass & CCC_ERASE))
1442 return -EOPNOTSUPP;
1443
1444 if (!card->erase_size)
1445 return -EOPNOTSUPP;
1446
1447 if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
1448 return -EOPNOTSUPP;
1449
1450 if ((arg & MMC_SECURE_ARGS) &&
1451 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
1452 return -EOPNOTSUPP;
1453
1454 if ((arg & MMC_TRIM_ARGS) &&
1455 !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
1456 return -EOPNOTSUPP;
1457
1458 if (arg == MMC_SECURE_ERASE_ARG) {
1459 if (from % card->erase_size || nr % card->erase_size)
1460 return -EINVAL;
1461 }
1462
1463 if (arg == MMC_ERASE_ARG) {
1464 rem = from % card->erase_size;
1465 if (rem) {
1466 rem = card->erase_size - rem;
1467 from += rem;
1468 if (nr > rem)
1469 nr -= rem;
1470 else
1471 return 0;
1472 }
1473 rem = nr % card->erase_size;
1474 if (rem)
1475 nr -= rem;
1476 }
1477
1478 if (nr == 0)
1479 return 0;
1480
1481 to = from + nr;
1482
1483 if (to <= from)
1484 return -EINVAL;
1485
1486 /* 'from' and 'to' are inclusive */
1487 to -= 1;
1488
1489 return mmc_do_erase(card, from, to, arg);
1490}
1491EXPORT_SYMBOL(mmc_erase);
1492
1493int mmc_can_erase(struct mmc_card *card)
1494{
1495 if ((card->host->caps & MMC_CAP_ERASE) &&
1496 (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
1497 return 1;
1498 return 0;
1499}
1500EXPORT_SYMBOL(mmc_can_erase);
1501
1502int mmc_can_trim(struct mmc_card *card)
1503{
1504 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
1505 return 1;
1506 return 0;
1507}
1508EXPORT_SYMBOL(mmc_can_trim);
1509
1510int mmc_can_secure_erase_trim(struct mmc_card *card)
1511{
1512 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
1513 return 1;
1514 return 0;
1515}
1516EXPORT_SYMBOL(mmc_can_secure_erase_trim);
1517
1518int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
1519 unsigned int nr)
1520{
1521 if (!card->erase_size)
1522 return 0;
1523 if (from % card->erase_size || nr % card->erase_size)
1524 return 0;
1525 return 1;
1526}
1527EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001529int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
1530{
Chris Ball1278dba2011-04-13 23:40:30 -04001531 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001532
1533 if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card))
1534 return 0;
1535
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03001536 cmd.opcode = MMC_SET_BLOCKLEN;
1537 cmd.arg = blocklen;
1538 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1539 return mmc_wait_for_cmd(card->host, &cmd, 5);
1540}
1541EXPORT_SYMBOL(mmc_set_blocklen);
1542
Andy Ross807e8e42011-01-03 10:36:56 -08001543static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq)
1544{
1545 host->f_init = freq;
1546
1547#ifdef CONFIG_MMC_DEBUG
1548 pr_info("%s: %s: trying to init card at %u Hz\n",
1549 mmc_hostname(host), __func__, host->f_init);
1550#endif
1551 mmc_power_up(host);
Philip Rakity2f94e552011-02-13 23:12:28 -08001552
1553 /*
1554 * sdio_reset sends CMD52 to reset card. Since we do not know
1555 * if the card is being re-initialized, just send it. CMD52
1556 * should be ignored by SD/eMMC cards.
1557 */
Andy Ross807e8e42011-01-03 10:36:56 -08001558 sdio_reset(host);
1559 mmc_go_idle(host);
1560
1561 mmc_send_if_cond(host, host->ocr_avail);
1562
1563 /* Order's important: probe SDIO, then SD, then MMC */
1564 if (!mmc_attach_sdio(host))
1565 return 0;
1566 if (!mmc_attach_sd(host))
1567 return 0;
1568 if (!mmc_attach_mmc(host))
1569 return 0;
1570
1571 mmc_power_off(host);
1572 return -EIO;
1573}
1574
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001575void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Andy Ross807e8e42011-01-03 10:36:56 -08001577 static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
David Howellsc4028952006-11-22 14:57:56 +00001578 struct mmc_host *host =
1579 container_of(work, struct mmc_host, detect.work);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001580 int i;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001581
Andy Ross807e8e42011-01-03 10:36:56 -08001582 if (host->rescan_disable)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001583 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001585 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02001587 /*
1588 * if there is a _removable_ card registered, check whether it is
1589 * still present
1590 */
1591 if (host->bus_ops && host->bus_ops->detect && !host->bus_dead
Ohad Ben-Cohenbad3bab2011-03-08 23:32:02 +02001592 && !(host->caps & MMC_CAP_NONREMOVABLE))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001593 host->bus_ops->detect(host);
1594
Chris Ballc5841792011-01-04 12:20:22 -05001595 /*
1596 * Let mmc_bus_put() free the bus/bus_ops if we've found that
1597 * the card is no longer present.
1598 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001599 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001600 mmc_bus_get(host);
1601
1602 /* if there still is a card present, stop here */
1603 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001604 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001607
Jorg Schummer94d89ef2009-03-31 17:51:21 +03001608 /*
1609 * Only we can add a new handler, so it's safe to
1610 * release the lock here.
1611 */
1612 mmc_bus_put(host);
1613
1614 if (host->ops->get_cd && host->ops->get_cd(host) == 0)
1615 goto out;
1616
Andy Ross807e8e42011-01-03 10:36:56 -08001617 mmc_claim_host(host);
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001618 for (i = 0; i < ARRAY_SIZE(freqs); i++) {
Andy Ross807e8e42011-01-03 10:36:56 -08001619 if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min)))
1620 break;
1621 if (freqs[i] < host->f_min)
1622 break;
Hein Tibosch88ae8b82010-09-06 09:37:19 +08001623 }
Andy Ross807e8e42011-01-03 10:36:56 -08001624 mmc_release_host(host);
1625
1626 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04001627 if (host->caps & MMC_CAP_NEEDS_POLL)
1628 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629}
1630
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001631void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001633 mmc_power_off(host);
1634 mmc_detect_change(host, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
Pierre Ossmanb93931a2007-05-19 14:06:24 +02001637void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Pierre Ossman3b91e552007-02-11 20:43:19 +01001639#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001640 unsigned long flags;
1641 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001642 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02001643 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001644#endif
1645
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001646 if (host->caps & MMC_CAP_DISABLE)
1647 cancel_delayed_work(&host->disable);
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01001648 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01001649 mmc_flush_scheduled_work();
1650
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001651 /* clear pm flags now and let card drivers set them as needed */
1652 host->pm_flags = 0;
1653
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001654 mmc_bus_get(host);
1655 if (host->bus_ops && !host->bus_dead) {
1656 if (host->bus_ops->remove)
1657 host->bus_ops->remove(host);
1658
1659 mmc_claim_host(host);
1660 mmc_detach_bus(host);
1661 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07001662 mmc_bus_put(host);
1663 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001665 mmc_bus_put(host);
1666
1667 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
1669 mmc_power_off(host);
1670}
1671
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001672int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001673{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001674 int ret = 0;
1675
Adrian Huntereae1aee2009-09-22 16:44:33 -07001676 mmc_bus_get(host);
1677
1678 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1679 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001680 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001681 }
1682
1683 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001684 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001685
1686 mmc_bus_put(host);
1687
1688 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001689
1690 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001691}
1692EXPORT_SYMBOL(mmc_power_save_host);
1693
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001694int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07001695{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001696 int ret;
1697
Adrian Huntereae1aee2009-09-22 16:44:33 -07001698 mmc_bus_get(host);
1699
1700 if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) {
1701 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001702 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001703 }
1704
1705 mmc_power_up(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001706 ret = host->bus_ops->power_restore(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07001707
1708 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02001709
1710 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07001711}
1712EXPORT_SYMBOL(mmc_power_restore_host);
1713
Jarkko Lavinenb1ebe382009-09-22 16:44:34 -07001714int mmc_card_awake(struct mmc_host *host)
1715{
1716 int err = -ENOSYS;
1717
1718 mmc_bus_get(host);
1719
1720 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1721 err = host->bus_ops->awake(host);
1722
1723 mmc_bus_put(host);
1724
1725 return err;
1726}
1727EXPORT_SYMBOL(mmc_card_awake);
1728
1729int mmc_card_sleep(struct mmc_host *host)
1730{
1731 int err = -ENOSYS;
1732
1733 mmc_bus_get(host);
1734
1735 if (host->bus_ops && !host->bus_dead && host->bus_ops->awake)
1736 err = host->bus_ops->sleep(host);
1737
1738 mmc_bus_put(host);
1739
1740 return err;
1741}
1742EXPORT_SYMBOL(mmc_card_sleep);
1743
1744int mmc_card_can_sleep(struct mmc_host *host)
1745{
1746 struct mmc_card *card = host->card;
1747
1748 if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3)
1749 return 1;
1750 return 0;
1751}
1752EXPORT_SYMBOL(mmc_card_can_sleep);
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754#ifdef CONFIG_PM
1755
1756/**
1757 * mmc_suspend_host - suspend a host
1758 * @host: mmc host
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 */
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001760int mmc_suspend_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001762 int err = 0;
1763
Adrian Hunter8ea926b2009-09-22 16:44:29 -07001764 if (host->caps & MMC_CAP_DISABLE)
1765 cancel_delayed_work(&host->disable);
Jorg Schummer7de427d2009-02-19 13:17:03 +02001766 cancel_delayed_work(&host->detect);
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001767 mmc_flush_scheduled_work();
1768
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001769 mmc_bus_get(host);
1770 if (host->bus_ops && !host->bus_dead) {
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001771 if (host->bus_ops->suspend)
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001772 err = host->bus_ops->suspend(host);
Ohad Ben-Cohen1c8cf9c2010-10-13 09:31:56 +02001773 if (err == -ENOSYS || !host->bus_ops->resume) {
1774 /*
1775 * We simply "remove" the card in this case.
1776 * It will be redetected on resume.
1777 */
1778 if (host->bus_ops->remove)
1779 host->bus_ops->remove(host);
1780 mmc_claim_host(host);
1781 mmc_detach_bus(host);
1782 mmc_release_host(host);
1783 host->pm_flags = 0;
1784 err = 0;
1785 }
Pierre Ossmanb5af25b2007-04-28 17:30:50 +02001786 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01001787 mmc_bus_put(host);
1788
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001789 if (!err && !mmc_card_keep_power(host))
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001790 mmc_power_off(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001792 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795EXPORT_SYMBOL(mmc_suspend_host);
1796
1797/**
1798 * mmc_resume_host - resume a previously suspended host
1799 * @host: mmc host
1800 */
1801int mmc_resume_host(struct mmc_host *host)
1802{
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001803 int err = 0;
1804
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001805 mmc_bus_get(host);
1806 if (host->bus_ops && !host->bus_dead) {
Ohad Ben-Cohena5e94252011-04-05 17:43:20 +03001807 if (!mmc_card_keep_power(host)) {
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001808 mmc_power_up(host);
1809 mmc_select_voltage(host, host->ocr);
Ohad Ben-Cohene5945732010-11-28 07:21:30 +02001810 /*
1811 * Tell runtime PM core we just powered up the card,
1812 * since it still believes the card is powered off.
1813 * Note that currently runtime PM is only enabled
1814 * for SDIO cards that are MMC_CAP_POWER_OFF_CARD
1815 */
1816 if (mmc_card_sdio(host->card) &&
1817 (host->caps & MMC_CAP_POWER_OFF_CARD)) {
1818 pm_runtime_disable(&host->card->dev);
1819 pm_runtime_set_active(&host->card->dev);
1820 pm_runtime_enable(&host->card->dev);
1821 }
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08001822 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001823 BUG_ON(!host->bus_ops->resume);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001824 err = host->bus_ops->resume(host);
1825 if (err) {
1826 printk(KERN_WARNING "%s: error %d during resume "
1827 "(card was removed?)\n",
1828 mmc_hostname(host), err);
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001829 err = 0;
1830 }
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001831 }
Eliad Pellera8e6df72011-05-09 11:32:31 +03001832 host->pm_flags &= ~MMC_PM_KEEP_POWER;
Pierre Ossman6abaa0c2007-05-01 16:00:02 +02001833 mmc_bus_put(host);
1834
Nicolas Pitre95cdfb72009-09-22 16:45:29 -07001835 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837EXPORT_SYMBOL(mmc_resume_host);
1838
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001839/* Do the card removal on suspend if card is assumed removeable
1840 * Do that in pm notifier while userspace isn't yet frozen, so we will be able
1841 to sync the card.
1842*/
1843int mmc_pm_notify(struct notifier_block *notify_block,
1844 unsigned long mode, void *unused)
1845{
1846 struct mmc_host *host = container_of(
1847 notify_block, struct mmc_host, pm_notify);
1848 unsigned long flags;
1849
1850
1851 switch (mode) {
1852 case PM_HIBERNATION_PREPARE:
1853 case PM_SUSPEND_PREPARE:
1854
1855 spin_lock_irqsave(&host->lock, flags);
1856 host->rescan_disable = 1;
1857 spin_unlock_irqrestore(&host->lock, flags);
1858 cancel_delayed_work_sync(&host->detect);
1859
1860 if (!host->bus_ops || host->bus_ops->suspend)
1861 break;
1862
1863 mmc_claim_host(host);
1864
1865 if (host->bus_ops->remove)
1866 host->bus_ops->remove(host);
1867
1868 mmc_detach_bus(host);
1869 mmc_release_host(host);
1870 host->pm_flags = 0;
1871 break;
1872
1873 case PM_POST_SUSPEND:
1874 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01001875 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07001876
1877 spin_lock_irqsave(&host->lock, flags);
1878 host->rescan_disable = 0;
1879 spin_unlock_irqrestore(&host->lock, flags);
1880 mmc_detect_change(host, 0);
1881
1882 }
1883
1884 return 0;
1885}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886#endif
1887
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001888static int __init mmc_init(void)
1889{
1890 int ret;
1891
Tejun Heo0d9ee5b2010-12-24 16:00:17 +01001892 workqueue = alloc_ordered_workqueue("kmmcd", 0);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001893 if (!workqueue)
1894 return -ENOMEM;
1895
1896 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001897 if (ret)
1898 goto destroy_workqueue;
1899
1900 ret = mmc_register_host_class();
1901 if (ret)
1902 goto unregister_bus;
1903
1904 ret = sdio_register_bus();
1905 if (ret)
1906 goto unregister_host_class;
1907
1908 return 0;
1909
1910unregister_host_class:
1911 mmc_unregister_host_class();
1912unregister_bus:
1913 mmc_unregister_bus();
1914destroy_workqueue:
1915 destroy_workqueue(workqueue);
1916
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001917 return ret;
1918}
1919
1920static void __exit mmc_exit(void)
1921{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02001922 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001923 mmc_unregister_host_class();
1924 mmc_unregister_bus();
1925 destroy_workqueue(workqueue);
1926}
1927
Nicolas Pitre26074962007-06-16 02:07:53 -04001928subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02001929module_exit(mmc_exit);
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931MODULE_LICENSE("GPL");