Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 2 | * linux/drivers/mmc/core/core.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
Pierre Ossman | 5b4fd9a | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 5 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 7 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #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 Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 21 | #include <linux/leds.h> |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 22 | #include <linux/scatterlist.h> |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 23 | #include <linux/log2.h> |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 24 | #include <linux/regulator/consumer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <linux/mmc/card.h> |
| 27 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 28 | #include <linux/mmc/mmc.h> |
| 29 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 31 | #include "core.h" |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 32 | #include "bus.h" |
| 33 | #include "host.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 34 | #include "sdio_bus.h" |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 35 | |
| 36 | #include "mmc_ops.h" |
| 37 | #include "sd_ops.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 38 | #include "sdio_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 40 | static struct workqueue_struct *workqueue; |
| 41 | |
| 42 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 43 | * 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 | */ |
| 47 | int use_spi_crc = 1; |
| 48 | module_param(use_spi_crc, bool, 0); |
| 49 | |
| 50 | /* |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 51 | * 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 |
| 57 | int mmc_assume_removable; |
| 58 | #else |
| 59 | int mmc_assume_removable = 1; |
| 60 | #endif |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 61 | EXPORT_SYMBOL(mmc_assume_removable); |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 62 | module_param_named(removable, mmc_assume_removable, bool, 0644); |
| 63 | MODULE_PARM_DESC( |
| 64 | removable, |
| 65 | "MMC/SD cards are removable and may be removed during suspend"); |
| 66 | |
| 67 | /* |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 68 | * Internal function. Schedule delayed work in the MMC work queue. |
| 69 | */ |
| 70 | static 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 | */ |
| 79 | static void mmc_flush_scheduled_work(void) |
| 80 | { |
| 81 | flush_workqueue(workqueue); |
| 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 85 | * mmc_request_done - finish processing an MMC request |
| 86 | * @host: MMC host which completed request |
| 87 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
| 89 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 90 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
| 92 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 93 | { |
| 94 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 95 | int err = cmd->error; |
| 96 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 97 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (err && cmd->retries) { |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 103 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 104 | mmc_hostname(host), cmd->opcode, err); |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | cmd->retries--; |
| 107 | cmd->error = 0; |
| 108 | host->ops->request(host, mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 109 | } else { |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 110 | led_trigger_event(host->led, LED_OFF); |
| 111 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 112 | 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL(mmc_request_done); |
| 137 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 138 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 140 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 141 | #ifdef CONFIG_MMC_DEBUG |
| 142 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 143 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 144 | #endif |
| 145 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 146 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 147 | mmc_hostname(host), mrq->cmd->opcode, |
| 148 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 150 | if (mrq->data) { |
| 151 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 152 | "tsac %d ms nsac %d\n", |
| 153 | mmc_hostname(host), mrq->data->blksz, |
| 154 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 155 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 156 | mrq->data->timeout_clks); |
| 157 | } |
| 158 | |
| 159 | if (mrq->stop) { |
| 160 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 161 | mmc_hostname(host), mrq->stop->opcode, |
| 162 | mrq->stop->arg, mrq->stop->flags); |
| 163 | } |
| 164 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 165 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 167 | led_trigger_event(host->led, LED_FULL); |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | mrq->cmd->error = 0; |
| 170 | mrq->cmd->mrq = mrq; |
| 171 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 172 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 173 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 174 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 175 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 176 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_MMC_DEBUG |
| 178 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 179 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 180 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 181 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 182 | #endif |
| 183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | mrq->cmd->data = mrq->data; |
| 185 | mrq->data->error = 0; |
| 186 | mrq->data->mrq = mrq; |
| 187 | if (mrq->stop) { |
| 188 | mrq->data->stop = mrq->stop; |
| 189 | mrq->stop->error = 0; |
| 190 | mrq->stop->mrq = mrq; |
| 191 | } |
| 192 | } |
| 193 | host->ops->request(host, mrq); |
| 194 | } |
| 195 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | static void mmc_wait_done(struct mmc_request *mrq) |
| 197 | { |
| 198 | complete(mrq->done_data); |
| 199 | } |
| 200 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 201 | /** |
| 202 | * mmc_wait_for_req - start a request and wait for completion |
| 203 | * @host: MMC host to start command |
| 204 | * @mrq: MMC request to start |
| 205 | * |
| 206 | * Start a new MMC custom command request for a host, and wait |
| 207 | * for the command to complete. Does not attempt to parse the |
| 208 | * response. |
| 209 | */ |
| 210 | void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 212 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
| 214 | mrq->done_data = &complete; |
| 215 | mrq->done = mmc_wait_done; |
| 216 | |
| 217 | mmc_start_request(host, mrq); |
| 218 | |
| 219 | wait_for_completion(&complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 223 | |
| 224 | /** |
| 225 | * mmc_wait_for_cmd - start a command and wait for completion |
| 226 | * @host: MMC host to start command |
| 227 | * @cmd: MMC command to start |
| 228 | * @retries: maximum number of retries |
| 229 | * |
| 230 | * Start a new MMC command for a host, and wait for the command |
| 231 | * to complete. Return any error that occurred while the command |
| 232 | * was executing. Do not attempt to parse the response. |
| 233 | */ |
| 234 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 235 | { |
| 236 | struct mmc_request mrq; |
| 237 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 238 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | |
| 240 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 241 | |
| 242 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 243 | cmd->retries = retries; |
| 244 | |
| 245 | mrq.cmd = cmd; |
| 246 | cmd->data = NULL; |
| 247 | |
| 248 | mmc_wait_for_req(host, &mrq); |
| 249 | |
| 250 | return cmd->error; |
| 251 | } |
| 252 | |
| 253 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 254 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 255 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 256 | * mmc_set_data_timeout - set the timeout for a data command |
| 257 | * @data: data phase for command |
| 258 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 259 | * |
| 260 | * Computes the data timeout parameters according to the |
| 261 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 262 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 263 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 264 | { |
| 265 | unsigned int mult; |
| 266 | |
| 267 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 268 | * SDIO cards only define an upper 1 s limit on access. |
| 269 | */ |
| 270 | if (mmc_card_sdio(card)) { |
| 271 | data->timeout_ns = 1000000000; |
| 272 | data->timeout_clks = 0; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 277 | * SD cards use a 100 multiplier rather than 10 |
| 278 | */ |
| 279 | mult = mmc_card_sd(card) ? 100 : 10; |
| 280 | |
| 281 | /* |
| 282 | * Scale up the multiplier (and therefore the timeout) by |
| 283 | * the r2w factor for writes. |
| 284 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 285 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 286 | mult <<= card->csd.r2w_factor; |
| 287 | |
| 288 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 289 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 290 | |
| 291 | /* |
| 292 | * SD cards also have an upper limit on the timeout. |
| 293 | */ |
| 294 | if (mmc_card_sd(card)) { |
| 295 | unsigned int timeout_us, limit_us; |
| 296 | |
| 297 | timeout_us = data->timeout_ns / 1000; |
| 298 | timeout_us += data->timeout_clks * 1000 / |
| 299 | (card->host->ios.clock / 1000); |
| 300 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 301 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 302 | /* |
| 303 | * The limit is really 250 ms, but that is |
| 304 | * insufficient for some crappy cards. |
| 305 | */ |
| 306 | limit_us = 300000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 307 | else |
| 308 | limit_us = 100000; |
| 309 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 310 | /* |
| 311 | * SDHC cards always use these fixed values. |
| 312 | */ |
| 313 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 314 | data->timeout_ns = limit_us * 1000; |
| 315 | data->timeout_clks = 0; |
| 316 | } |
| 317 | } |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 318 | /* |
| 319 | * Some cards need very high timeouts if driven in SPI mode. |
| 320 | * The worst observed timeout was 900ms after writing a |
| 321 | * continuous stream of data until the internal logic |
| 322 | * overflowed. |
| 323 | */ |
| 324 | if (mmc_host_is_spi(card->host)) { |
| 325 | if (data->flags & MMC_DATA_WRITE) { |
| 326 | if (data->timeout_ns < 1000000000) |
| 327 | data->timeout_ns = 1000000000; /* 1s */ |
| 328 | } else { |
| 329 | if (data->timeout_ns < 100000000) |
| 330 | data->timeout_ns = 100000000; /* 100ms */ |
| 331 | } |
| 332 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 333 | } |
| 334 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 335 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 337 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 338 | * @card: the MMC card associated with the data transfer |
| 339 | * @sz: original transfer size |
| 340 | * |
| 341 | * Pads the original data size with a number of extra bytes in |
| 342 | * order to avoid controller bugs and/or performance hits |
| 343 | * (e.g. some controllers revert to PIO for certain sizes). |
| 344 | * |
| 345 | * Returns the improved size, which might be unmodified. |
| 346 | * |
| 347 | * Note that this function is only relevant when issuing a |
| 348 | * single scatter gather entry. |
| 349 | */ |
| 350 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 351 | { |
| 352 | /* |
| 353 | * FIXME: We don't have a system for the controller to tell |
| 354 | * the core about its problems yet, so for now we just 32-bit |
| 355 | * align the size. |
| 356 | */ |
| 357 | sz = ((sz + 3) / 4) * 4; |
| 358 | |
| 359 | return sz; |
| 360 | } |
| 361 | EXPORT_SYMBOL(mmc_align_data_size); |
| 362 | |
| 363 | /** |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 364 | * mmc_host_enable - enable a host. |
| 365 | * @host: mmc host to enable |
| 366 | * |
| 367 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 368 | * methods to exit and enter power saving states. For more information |
| 369 | * see comments for struct mmc_host_ops. |
| 370 | */ |
| 371 | int mmc_host_enable(struct mmc_host *host) |
| 372 | { |
| 373 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 374 | return 0; |
| 375 | |
| 376 | if (host->en_dis_recurs) |
| 377 | return 0; |
| 378 | |
| 379 | if (host->nesting_cnt++) |
| 380 | return 0; |
| 381 | |
| 382 | cancel_delayed_work_sync(&host->disable); |
| 383 | |
| 384 | if (host->enabled) |
| 385 | return 0; |
| 386 | |
| 387 | if (host->ops->enable) { |
| 388 | int err; |
| 389 | |
| 390 | host->en_dis_recurs = 1; |
| 391 | err = host->ops->enable(host); |
| 392 | host->en_dis_recurs = 0; |
| 393 | |
| 394 | if (err) { |
| 395 | pr_debug("%s: enable error %d\n", |
| 396 | mmc_hostname(host), err); |
| 397 | return err; |
| 398 | } |
| 399 | } |
| 400 | host->enabled = 1; |
| 401 | return 0; |
| 402 | } |
| 403 | EXPORT_SYMBOL(mmc_host_enable); |
| 404 | |
| 405 | static int mmc_host_do_disable(struct mmc_host *host, int lazy) |
| 406 | { |
| 407 | if (host->ops->disable) { |
| 408 | int err; |
| 409 | |
| 410 | host->en_dis_recurs = 1; |
| 411 | err = host->ops->disable(host, lazy); |
| 412 | host->en_dis_recurs = 0; |
| 413 | |
| 414 | if (err < 0) { |
| 415 | pr_debug("%s: disable error %d\n", |
| 416 | mmc_hostname(host), err); |
| 417 | return err; |
| 418 | } |
| 419 | if (err > 0) { |
| 420 | unsigned long delay = msecs_to_jiffies(err); |
| 421 | |
| 422 | mmc_schedule_delayed_work(&host->disable, delay); |
| 423 | } |
| 424 | } |
| 425 | host->enabled = 0; |
| 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * mmc_host_disable - disable a host. |
| 431 | * @host: mmc host to disable |
| 432 | * |
| 433 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 434 | * methods to exit and enter power saving states. For more information |
| 435 | * see comments for struct mmc_host_ops. |
| 436 | */ |
| 437 | int mmc_host_disable(struct mmc_host *host) |
| 438 | { |
| 439 | int err; |
| 440 | |
| 441 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 442 | return 0; |
| 443 | |
| 444 | if (host->en_dis_recurs) |
| 445 | return 0; |
| 446 | |
| 447 | if (--host->nesting_cnt) |
| 448 | return 0; |
| 449 | |
| 450 | if (!host->enabled) |
| 451 | return 0; |
| 452 | |
| 453 | err = mmc_host_do_disable(host, 0); |
| 454 | return err; |
| 455 | } |
| 456 | EXPORT_SYMBOL(mmc_host_disable); |
| 457 | |
| 458 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 459 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 461 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 463 | * Claim a host for a set of operations. If @abort is non null and |
| 464 | * dereference a non-zero value then this will return prematurely with |
| 465 | * that non-zero value without acquiring the lock. Returns zero |
| 466 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 468 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | DECLARE_WAITQUEUE(wait, current); |
| 471 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 472 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 474 | might_sleep(); |
| 475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | add_wait_queue(&host->wq, &wait); |
| 477 | spin_lock_irqsave(&host->lock, flags); |
| 478 | while (1) { |
| 479 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 480 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 481 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | break; |
| 483 | spin_unlock_irqrestore(&host->lock, flags); |
| 484 | schedule(); |
| 485 | spin_lock_irqsave(&host->lock, flags); |
| 486 | } |
| 487 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 488 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 489 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 490 | host->claimer = current; |
| 491 | host->claim_cnt += 1; |
| 492 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 493 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | spin_unlock_irqrestore(&host->lock, flags); |
| 495 | remove_wait_queue(&host->wq, &wait); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 496 | if (!stop) |
| 497 | mmc_host_enable(host); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 498 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 501 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 503 | /** |
| 504 | * mmc_try_claim_host - try exclusively to claim a host |
| 505 | * @host: mmc host to claim |
| 506 | * |
| 507 | * Returns %1 if the host is claimed, %0 otherwise. |
| 508 | */ |
| 509 | int mmc_try_claim_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 510 | { |
| 511 | int claimed_host = 0; |
| 512 | unsigned long flags; |
| 513 | |
| 514 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 515 | if (!host->claimed || host->claimer == current) { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 516 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 517 | host->claimer = current; |
| 518 | host->claim_cnt += 1; |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 519 | claimed_host = 1; |
| 520 | } |
| 521 | spin_unlock_irqrestore(&host->lock, flags); |
| 522 | return claimed_host; |
| 523 | } |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 524 | EXPORT_SYMBOL(mmc_try_claim_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 525 | |
| 526 | static void mmc_do_release_host(struct mmc_host *host) |
| 527 | { |
| 528 | unsigned long flags; |
| 529 | |
| 530 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 531 | if (--host->claim_cnt) { |
| 532 | /* Release for nested claim */ |
| 533 | spin_unlock_irqrestore(&host->lock, flags); |
| 534 | } else { |
| 535 | host->claimed = 0; |
| 536 | host->claimer = NULL; |
| 537 | spin_unlock_irqrestore(&host->lock, flags); |
| 538 | wake_up(&host->wq); |
| 539 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void mmc_host_deeper_disable(struct work_struct *work) |
| 543 | { |
| 544 | struct mmc_host *host = |
| 545 | container_of(work, struct mmc_host, disable.work); |
| 546 | |
| 547 | /* If the host is claimed then we do not want to disable it anymore */ |
| 548 | if (!mmc_try_claim_host(host)) |
| 549 | return; |
| 550 | mmc_host_do_disable(host, 1); |
| 551 | mmc_do_release_host(host); |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * mmc_host_lazy_disable - lazily disable a host. |
| 556 | * @host: mmc host to disable |
| 557 | * |
| 558 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 559 | * methods to exit and enter power saving states. For more information |
| 560 | * see comments for struct mmc_host_ops. |
| 561 | */ |
| 562 | int mmc_host_lazy_disable(struct mmc_host *host) |
| 563 | { |
| 564 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 565 | return 0; |
| 566 | |
| 567 | if (host->en_dis_recurs) |
| 568 | return 0; |
| 569 | |
| 570 | if (--host->nesting_cnt) |
| 571 | return 0; |
| 572 | |
| 573 | if (!host->enabled) |
| 574 | return 0; |
| 575 | |
| 576 | if (host->disable_delay) { |
| 577 | mmc_schedule_delayed_work(&host->disable, |
| 578 | msecs_to_jiffies(host->disable_delay)); |
| 579 | return 0; |
| 580 | } else |
| 581 | return mmc_host_do_disable(host, 1); |
| 582 | } |
| 583 | EXPORT_SYMBOL(mmc_host_lazy_disable); |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | /** |
| 586 | * mmc_release_host - release a host |
| 587 | * @host: mmc host to release |
| 588 | * |
| 589 | * Release a MMC host, allowing others to claim the host |
| 590 | * for their operations. |
| 591 | */ |
| 592 | void mmc_release_host(struct mmc_host *host) |
| 593 | { |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 594 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 596 | mmc_host_lazy_disable(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 598 | mmc_do_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | } |
| 600 | |
| 601 | EXPORT_SYMBOL(mmc_release_host); |
| 602 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 603 | /* |
| 604 | * Internal function that does the actual ios call to the host driver, |
| 605 | * optionally printing some debug output. |
| 606 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 607 | static inline void mmc_set_ios(struct mmc_host *host) |
| 608 | { |
| 609 | struct mmc_ios *ios = &host->ios; |
| 610 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 611 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 612 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 613 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 614 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 615 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 616 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 617 | host->ops->set_ios(host, ios); |
| 618 | } |
| 619 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 620 | /* |
| 621 | * Control chip select pin on a host. |
| 622 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 623 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 624 | { |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 625 | host->ios.chip_select = mode; |
| 626 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 630 | * Sets the host clock to the highest possible frequency that |
| 631 | * is below "hz". |
| 632 | */ |
| 633 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 634 | { |
| 635 | WARN_ON(hz < host->f_min); |
| 636 | |
| 637 | if (hz > host->f_max) |
| 638 | hz = host->f_max; |
| 639 | |
| 640 | host->ios.clock = hz; |
| 641 | mmc_set_ios(host); |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * Change the bus mode (open drain/push-pull) of a host. |
| 646 | */ |
| 647 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 648 | { |
| 649 | host->ios.bus_mode = mode; |
| 650 | mmc_set_ios(host); |
| 651 | } |
| 652 | |
| 653 | /* |
| 654 | * Change data bus width of a host. |
| 655 | */ |
| 656 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 657 | { |
| 658 | host->ios.bus_width = width; |
| 659 | mmc_set_ios(host); |
| 660 | } |
| 661 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 662 | /** |
| 663 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 664 | * @vdd: voltage (mV) |
| 665 | * @low_bits: prefer low bits in boundary cases |
| 666 | * |
| 667 | * This function returns the OCR bit number according to the provided @vdd |
| 668 | * value. If conversion is not possible a negative errno value returned. |
| 669 | * |
| 670 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 671 | * on boundary voltages. For example, |
| 672 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 673 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 674 | * |
| 675 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 676 | */ |
| 677 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 678 | { |
| 679 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 680 | int bit; |
| 681 | |
| 682 | if (vdd < 1650 || vdd > 3600) |
| 683 | return -EINVAL; |
| 684 | |
| 685 | if (vdd >= 1650 && vdd <= 1950) |
| 686 | return ilog2(MMC_VDD_165_195); |
| 687 | |
| 688 | if (low_bits) |
| 689 | vdd -= 1; |
| 690 | |
| 691 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 692 | bit = (vdd - 2000) / 100 + 8; |
| 693 | if (bit > max_bit) |
| 694 | return max_bit; |
| 695 | return bit; |
| 696 | } |
| 697 | |
| 698 | /** |
| 699 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 700 | * @vdd_min: minimum voltage value (mV) |
| 701 | * @vdd_max: maximum voltage value (mV) |
| 702 | * |
| 703 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 704 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 705 | * |
| 706 | * Notes wrt boundary cases: |
| 707 | * This function sets the OCR bits for all boundary voltages, for example |
| 708 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 709 | * MMC_VDD_34_35 mask. |
| 710 | */ |
| 711 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 712 | { |
| 713 | u32 mask = 0; |
| 714 | |
| 715 | if (vdd_max < vdd_min) |
| 716 | return 0; |
| 717 | |
| 718 | /* Prefer high bits for the boundary vdd_max values. */ |
| 719 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 720 | if (vdd_max < 0) |
| 721 | return 0; |
| 722 | |
| 723 | /* Prefer low bits for the boundary vdd_min values. */ |
| 724 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 725 | if (vdd_min < 0) |
| 726 | return 0; |
| 727 | |
| 728 | /* Fill the mask, from max bit to min bit. */ |
| 729 | while (vdd_max >= vdd_min) |
| 730 | mask |= 1 << vdd_max--; |
| 731 | |
| 732 | return mask; |
| 733 | } |
| 734 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 735 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 736 | #ifdef CONFIG_REGULATOR |
| 737 | |
| 738 | /** |
| 739 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 740 | * @supply: regulator to use |
| 741 | * |
| 742 | * This returns either a negative errno, or a mask of voltages that |
| 743 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 744 | * regulator. This would normally be called before registering the |
| 745 | * MMC host adapter. |
| 746 | */ |
| 747 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 748 | { |
| 749 | int result = 0; |
| 750 | int count; |
| 751 | int i; |
| 752 | |
| 753 | count = regulator_count_voltages(supply); |
| 754 | if (count < 0) |
| 755 | return count; |
| 756 | |
| 757 | for (i = 0; i < count; i++) { |
| 758 | int vdd_uV; |
| 759 | int vdd_mV; |
| 760 | |
| 761 | vdd_uV = regulator_list_voltage(supply, i); |
| 762 | if (vdd_uV <= 0) |
| 763 | continue; |
| 764 | |
| 765 | vdd_mV = vdd_uV / 1000; |
| 766 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 767 | } |
| 768 | |
| 769 | return result; |
| 770 | } |
| 771 | EXPORT_SYMBOL(mmc_regulator_get_ocrmask); |
| 772 | |
| 773 | /** |
| 774 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
| 775 | * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) |
| 776 | * @supply: regulator to use |
| 777 | * |
| 778 | * Returns zero on success, else negative errno. |
| 779 | * |
| 780 | * MMC host drivers may use this to enable or disable a regulator using |
| 781 | * a particular supply voltage. This would normally be called from the |
| 782 | * set_ios() method. |
| 783 | */ |
| 784 | int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit) |
| 785 | { |
| 786 | int result = 0; |
| 787 | int min_uV, max_uV; |
| 788 | int enabled; |
| 789 | |
| 790 | enabled = regulator_is_enabled(supply); |
| 791 | if (enabled < 0) |
| 792 | return enabled; |
| 793 | |
| 794 | if (vdd_bit) { |
| 795 | int tmp; |
| 796 | int voltage; |
| 797 | |
| 798 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 799 | * bits this regulator doesn't quite support ... don't |
| 800 | * be too picky, most cards and regulators are OK with |
| 801 | * a 0.1V range goof (it's a small error percentage). |
| 802 | */ |
| 803 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 804 | if (tmp == 0) { |
| 805 | min_uV = 1650 * 1000; |
| 806 | max_uV = 1950 * 1000; |
| 807 | } else { |
| 808 | min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 809 | max_uV = min_uV + 100 * 1000; |
| 810 | } |
| 811 | |
| 812 | /* avoid needless changes to this voltage; the regulator |
| 813 | * might not allow this operation |
| 814 | */ |
| 815 | voltage = regulator_get_voltage(supply); |
| 816 | if (voltage < 0) |
| 817 | result = voltage; |
| 818 | else if (voltage < min_uV || voltage > max_uV) |
| 819 | result = regulator_set_voltage(supply, min_uV, max_uV); |
| 820 | else |
| 821 | result = 0; |
| 822 | |
| 823 | if (result == 0 && !enabled) |
| 824 | result = regulator_enable(supply); |
| 825 | } else if (enabled) { |
| 826 | result = regulator_disable(supply); |
| 827 | } |
| 828 | |
| 829 | return result; |
| 830 | } |
| 831 | EXPORT_SYMBOL(mmc_regulator_set_ocr); |
| 832 | |
| 833 | #endif |
| 834 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 835 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | * Mask off any voltages we don't support and select |
| 837 | * the lowest voltage |
| 838 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 839 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | { |
| 841 | int bit; |
| 842 | |
| 843 | ocr &= host->ocr_avail; |
| 844 | |
| 845 | bit = ffs(ocr); |
| 846 | if (bit) { |
| 847 | bit -= 1; |
| 848 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 849 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 850 | |
| 851 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 852 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | } else { |
David Brownell | f6e10b8 | 2008-12-31 09:50:30 -0800 | [diff] [blame] | 854 | pr_warning("%s: host doesn't support card's voltages\n", |
| 855 | mmc_hostname(host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | ocr = 0; |
| 857 | } |
| 858 | |
| 859 | return ocr; |
| 860 | } |
| 861 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 863 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 865 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 867 | host->ios.timing = timing; |
| 868 | mmc_set_ios(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 872 | * Apply power to the MMC stack. This is a two-stage process. |
| 873 | * First, we enable power to the card without the clock running. |
| 874 | * We then wait a bit for the power to stabilise. Finally, |
| 875 | * enable the bus drivers and clock to the card. |
| 876 | * |
| 877 | * We must _NOT_ enable the clock prior to power stablising. |
| 878 | * |
| 879 | * If a host does all the power sequencing itself, ignore the |
| 880 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | */ |
| 882 | static void mmc_power_up(struct mmc_host *host) |
| 883 | { |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 884 | int bit; |
| 885 | |
| 886 | /* If ocr is set, we use it */ |
| 887 | if (host->ocr) |
| 888 | bit = ffs(host->ocr) - 1; |
| 889 | else |
| 890 | bit = fls(host->ocr_avail) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | |
| 892 | host->ios.vdd = bit; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 893 | if (mmc_host_is_spi(host)) { |
| 894 | host->ios.chip_select = MMC_CS_HIGH; |
| 895 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 896 | } else { |
| 897 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 898 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 899 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 901 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 902 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 903 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 904 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 905 | /* |
| 906 | * This delay should be sufficient to allow the power supply |
| 907 | * to reach the minimum voltage. |
| 908 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 909 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 911 | host->ios.clock = host->f_init; |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 912 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 914 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 916 | /* |
| 917 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 918 | * time required to reach a stable voltage. |
| 919 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 920 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | static void mmc_power_off(struct mmc_host *host) |
| 924 | { |
| 925 | host->ios.clock = 0; |
| 926 | host->ios.vdd = 0; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 927 | if (!mmc_host_is_spi(host)) { |
| 928 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 929 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 931 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 932 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 933 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 934 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } |
| 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 938 | * Cleanup when the last reference to the bus operator is dropped. |
| 939 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 940 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 941 | { |
| 942 | BUG_ON(!host); |
| 943 | BUG_ON(host->bus_refs); |
| 944 | BUG_ON(!host->bus_dead); |
| 945 | |
| 946 | host->bus_ops = NULL; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Increase reference count of bus operator |
| 951 | */ |
| 952 | static inline void mmc_bus_get(struct mmc_host *host) |
| 953 | { |
| 954 | unsigned long flags; |
| 955 | |
| 956 | spin_lock_irqsave(&host->lock, flags); |
| 957 | host->bus_refs++; |
| 958 | spin_unlock_irqrestore(&host->lock, flags); |
| 959 | } |
| 960 | |
| 961 | /* |
| 962 | * Decrease reference count of bus operator and free it if |
| 963 | * it is the last reference. |
| 964 | */ |
| 965 | static inline void mmc_bus_put(struct mmc_host *host) |
| 966 | { |
| 967 | unsigned long flags; |
| 968 | |
| 969 | spin_lock_irqsave(&host->lock, flags); |
| 970 | host->bus_refs--; |
| 971 | if ((host->bus_refs == 0) && host->bus_ops) |
| 972 | __mmc_release_bus(host); |
| 973 | spin_unlock_irqrestore(&host->lock, flags); |
| 974 | } |
| 975 | |
| 976 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 977 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 978 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 980 | void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 982 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 984 | BUG_ON(!host); |
| 985 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 986 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 987 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 988 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 989 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 990 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 991 | BUG_ON(host->bus_ops); |
| 992 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 993 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 994 | host->bus_ops = ops; |
| 995 | host->bus_refs = 1; |
| 996 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 998 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1002 | * Remove the current bus handler from a host. Assumes that there are |
| 1003 | * no interesting cards left, so the bus is powered down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1004 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1005 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1007 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1009 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1011 | WARN_ON(!host->claimed); |
| 1012 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1014 | spin_lock_irqsave(&host->lock, flags); |
| 1015 | |
| 1016 | host->bus_dead = 1; |
| 1017 | |
| 1018 | spin_unlock_irqrestore(&host->lock, flags); |
| 1019 | |
| 1020 | mmc_power_off(host); |
| 1021 | |
| 1022 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | } |
| 1024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1025 | /** |
| 1026 | * mmc_detect_change - process change of state on a MMC socket |
| 1027 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1028 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1030 | * MMC drivers should call this when they detect a card has been |
| 1031 | * inserted or removed. The MMC layer will confirm that any |
| 1032 | * present card is still functional, and initialize any newly |
| 1033 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1035 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1037 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1038 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1039 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1040 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1041 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1042 | #endif |
| 1043 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1044 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | EXPORT_SYMBOL(mmc_detect_change); |
| 1048 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1049 | void mmc_init_erase(struct mmc_card *card) |
| 1050 | { |
| 1051 | unsigned int sz; |
| 1052 | |
| 1053 | if (is_power_of_2(card->erase_size)) |
| 1054 | card->erase_shift = ffs(card->erase_size) - 1; |
| 1055 | else |
| 1056 | card->erase_shift = 0; |
| 1057 | |
| 1058 | /* |
| 1059 | * It is possible to erase an arbitrarily large area of an SD or MMC |
| 1060 | * card. That is not desirable because it can take a long time |
| 1061 | * (minutes) potentially delaying more important I/O, and also the |
| 1062 | * timeout calculations become increasingly hugely over-estimated. |
| 1063 | * Consequently, 'pref_erase' is defined as a guide to limit erases |
| 1064 | * to that size and alignment. |
| 1065 | * |
| 1066 | * For SD cards that define Allocation Unit size, limit erases to one |
| 1067 | * Allocation Unit at a time. For MMC cards that define High Capacity |
| 1068 | * Erase Size, whether it is switched on or not, limit to that size. |
| 1069 | * Otherwise just have a stab at a good value. For modern cards it |
| 1070 | * will end up being 4MiB. Note that if the value is too small, it |
| 1071 | * can end up taking longer to erase. |
| 1072 | */ |
| 1073 | if (mmc_card_sd(card) && card->ssr.au) { |
| 1074 | card->pref_erase = card->ssr.au; |
| 1075 | card->erase_shift = ffs(card->ssr.au) - 1; |
| 1076 | } else if (card->ext_csd.hc_erase_size) { |
| 1077 | card->pref_erase = card->ext_csd.hc_erase_size; |
| 1078 | } else { |
| 1079 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 1080 | if (sz < 128) |
| 1081 | card->pref_erase = 512 * 1024 / 512; |
| 1082 | else if (sz < 512) |
| 1083 | card->pref_erase = 1024 * 1024 / 512; |
| 1084 | else if (sz < 1024) |
| 1085 | card->pref_erase = 2 * 1024 * 1024 / 512; |
| 1086 | else |
| 1087 | card->pref_erase = 4 * 1024 * 1024 / 512; |
| 1088 | if (card->pref_erase < card->erase_size) |
| 1089 | card->pref_erase = card->erase_size; |
| 1090 | else { |
| 1091 | sz = card->pref_erase % card->erase_size; |
| 1092 | if (sz) |
| 1093 | card->pref_erase += card->erase_size - sz; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | static void mmc_set_mmc_erase_timeout(struct mmc_card *card, |
| 1099 | struct mmc_command *cmd, |
| 1100 | unsigned int arg, unsigned int qty) |
| 1101 | { |
| 1102 | unsigned int erase_timeout; |
| 1103 | |
| 1104 | if (card->ext_csd.erase_group_def & 1) { |
| 1105 | /* High Capacity Erase Group Size uses HC timeouts */ |
| 1106 | if (arg == MMC_TRIM_ARG) |
| 1107 | erase_timeout = card->ext_csd.trim_timeout; |
| 1108 | else |
| 1109 | erase_timeout = card->ext_csd.hc_erase_timeout; |
| 1110 | } else { |
| 1111 | /* CSD Erase Group Size uses write timeout */ |
| 1112 | unsigned int mult = (10 << card->csd.r2w_factor); |
| 1113 | unsigned int timeout_clks = card->csd.tacc_clks * mult; |
| 1114 | unsigned int timeout_us; |
| 1115 | |
| 1116 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ |
| 1117 | if (card->csd.tacc_ns < 1000000) |
| 1118 | timeout_us = (card->csd.tacc_ns * mult) / 1000; |
| 1119 | else |
| 1120 | timeout_us = (card->csd.tacc_ns / 1000) * mult; |
| 1121 | |
| 1122 | /* |
| 1123 | * ios.clock is only a target. The real clock rate might be |
| 1124 | * less but not that much less, so fudge it by multiplying by 2. |
| 1125 | */ |
| 1126 | timeout_clks <<= 1; |
| 1127 | timeout_us += (timeout_clks * 1000) / |
| 1128 | (card->host->ios.clock / 1000); |
| 1129 | |
| 1130 | erase_timeout = timeout_us / 1000; |
| 1131 | |
| 1132 | /* |
| 1133 | * Theoretically, the calculation could underflow so round up |
| 1134 | * to 1ms in that case. |
| 1135 | */ |
| 1136 | if (!erase_timeout) |
| 1137 | erase_timeout = 1; |
| 1138 | } |
| 1139 | |
| 1140 | /* Multiplier for secure operations */ |
| 1141 | if (arg & MMC_SECURE_ARGS) { |
| 1142 | if (arg == MMC_SECURE_ERASE_ARG) |
| 1143 | erase_timeout *= card->ext_csd.sec_erase_mult; |
| 1144 | else |
| 1145 | erase_timeout *= card->ext_csd.sec_trim_mult; |
| 1146 | } |
| 1147 | |
| 1148 | erase_timeout *= qty; |
| 1149 | |
| 1150 | /* |
| 1151 | * Ensure at least a 1 second timeout for SPI as per |
| 1152 | * 'mmc_set_data_timeout()' |
| 1153 | */ |
| 1154 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 1155 | erase_timeout = 1000; |
| 1156 | |
| 1157 | cmd->erase_timeout = erase_timeout; |
| 1158 | } |
| 1159 | |
| 1160 | static void mmc_set_sd_erase_timeout(struct mmc_card *card, |
| 1161 | struct mmc_command *cmd, unsigned int arg, |
| 1162 | unsigned int qty) |
| 1163 | { |
| 1164 | if (card->ssr.erase_timeout) { |
| 1165 | /* Erase timeout specified in SD Status Register (SSR) */ |
| 1166 | cmd->erase_timeout = card->ssr.erase_timeout * qty + |
| 1167 | card->ssr.erase_offset; |
| 1168 | } else { |
| 1169 | /* |
| 1170 | * Erase timeout not specified in SD Status Register (SSR) so |
| 1171 | * use 250ms per write block. |
| 1172 | */ |
| 1173 | cmd->erase_timeout = 250 * qty; |
| 1174 | } |
| 1175 | |
| 1176 | /* Must not be less than 1 second */ |
| 1177 | if (cmd->erase_timeout < 1000) |
| 1178 | cmd->erase_timeout = 1000; |
| 1179 | } |
| 1180 | |
| 1181 | static void mmc_set_erase_timeout(struct mmc_card *card, |
| 1182 | struct mmc_command *cmd, unsigned int arg, |
| 1183 | unsigned int qty) |
| 1184 | { |
| 1185 | if (mmc_card_sd(card)) |
| 1186 | mmc_set_sd_erase_timeout(card, cmd, arg, qty); |
| 1187 | else |
| 1188 | mmc_set_mmc_erase_timeout(card, cmd, arg, qty); |
| 1189 | } |
| 1190 | |
| 1191 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, |
| 1192 | unsigned int to, unsigned int arg) |
| 1193 | { |
| 1194 | struct mmc_command cmd; |
| 1195 | unsigned int qty = 0; |
| 1196 | int err; |
| 1197 | |
| 1198 | /* |
| 1199 | * qty is used to calculate the erase timeout which depends on how many |
| 1200 | * erase groups (or allocation units in SD terminology) are affected. |
| 1201 | * We count erasing part of an erase group as one erase group. |
| 1202 | * For SD, the allocation units are always a power of 2. For MMC, the |
| 1203 | * erase group size is almost certainly also power of 2, but it does not |
| 1204 | * seem to insist on that in the JEDEC standard, so we fall back to |
| 1205 | * division in that case. SD may not specify an allocation unit size, |
| 1206 | * in which case the timeout is based on the number of write blocks. |
| 1207 | * |
| 1208 | * Note that the timeout for secure trim 2 will only be correct if the |
| 1209 | * number of erase groups specified is the same as the total of all |
| 1210 | * preceding secure trim 1 commands. Since the power may have been |
| 1211 | * lost since the secure trim 1 commands occurred, it is generally |
| 1212 | * impossible to calculate the secure trim 2 timeout correctly. |
| 1213 | */ |
| 1214 | if (card->erase_shift) |
| 1215 | qty += ((to >> card->erase_shift) - |
| 1216 | (from >> card->erase_shift)) + 1; |
| 1217 | else if (mmc_card_sd(card)) |
| 1218 | qty += to - from + 1; |
| 1219 | else |
| 1220 | qty += ((to / card->erase_size) - |
| 1221 | (from / card->erase_size)) + 1; |
| 1222 | |
| 1223 | if (!mmc_card_blockaddr(card)) { |
| 1224 | from <<= 9; |
| 1225 | to <<= 9; |
| 1226 | } |
| 1227 | |
| 1228 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1229 | if (mmc_card_sd(card)) |
| 1230 | cmd.opcode = SD_ERASE_WR_BLK_START; |
| 1231 | else |
| 1232 | cmd.opcode = MMC_ERASE_GROUP_START; |
| 1233 | cmd.arg = from; |
| 1234 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1235 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1236 | if (err) { |
| 1237 | printk(KERN_ERR "mmc_erase: group start error %d, " |
| 1238 | "status %#x\n", err, cmd.resp[0]); |
| 1239 | err = -EINVAL; |
| 1240 | goto out; |
| 1241 | } |
| 1242 | |
| 1243 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1244 | if (mmc_card_sd(card)) |
| 1245 | cmd.opcode = SD_ERASE_WR_BLK_END; |
| 1246 | else |
| 1247 | cmd.opcode = MMC_ERASE_GROUP_END; |
| 1248 | cmd.arg = to; |
| 1249 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1250 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1251 | if (err) { |
| 1252 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", |
| 1253 | err, cmd.resp[0]); |
| 1254 | err = -EINVAL; |
| 1255 | goto out; |
| 1256 | } |
| 1257 | |
| 1258 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1259 | cmd.opcode = MMC_ERASE; |
| 1260 | cmd.arg = arg; |
| 1261 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1262 | mmc_set_erase_timeout(card, &cmd, arg, qty); |
| 1263 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1264 | if (err) { |
| 1265 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", |
| 1266 | err, cmd.resp[0]); |
| 1267 | err = -EIO; |
| 1268 | goto out; |
| 1269 | } |
| 1270 | |
| 1271 | if (mmc_host_is_spi(card->host)) |
| 1272 | goto out; |
| 1273 | |
| 1274 | do { |
| 1275 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1276 | cmd.opcode = MMC_SEND_STATUS; |
| 1277 | cmd.arg = card->rca << 16; |
| 1278 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1279 | /* Do not retry else we can't see errors */ |
| 1280 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1281 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
| 1282 | printk(KERN_ERR "error %d requesting status %#x\n", |
| 1283 | err, cmd.resp[0]); |
| 1284 | err = -EIO; |
| 1285 | goto out; |
| 1286 | } |
| 1287 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 1288 | R1_CURRENT_STATE(cmd.resp[0]) == 7); |
| 1289 | out: |
| 1290 | return err; |
| 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * mmc_erase - erase sectors. |
| 1295 | * @card: card to erase |
| 1296 | * @from: first sector to erase |
| 1297 | * @nr: number of sectors to erase |
| 1298 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
| 1299 | * |
| 1300 | * Caller must claim host before calling this function. |
| 1301 | */ |
| 1302 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, |
| 1303 | unsigned int arg) |
| 1304 | { |
| 1305 | unsigned int rem, to = from + nr; |
| 1306 | |
| 1307 | if (!(card->host->caps & MMC_CAP_ERASE) || |
| 1308 | !(card->csd.cmdclass & CCC_ERASE)) |
| 1309 | return -EOPNOTSUPP; |
| 1310 | |
| 1311 | if (!card->erase_size) |
| 1312 | return -EOPNOTSUPP; |
| 1313 | |
| 1314 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
| 1315 | return -EOPNOTSUPP; |
| 1316 | |
| 1317 | if ((arg & MMC_SECURE_ARGS) && |
| 1318 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 1319 | return -EOPNOTSUPP; |
| 1320 | |
| 1321 | if ((arg & MMC_TRIM_ARGS) && |
| 1322 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 1323 | return -EOPNOTSUPP; |
| 1324 | |
| 1325 | if (arg == MMC_SECURE_ERASE_ARG) { |
| 1326 | if (from % card->erase_size || nr % card->erase_size) |
| 1327 | return -EINVAL; |
| 1328 | } |
| 1329 | |
| 1330 | if (arg == MMC_ERASE_ARG) { |
| 1331 | rem = from % card->erase_size; |
| 1332 | if (rem) { |
| 1333 | rem = card->erase_size - rem; |
| 1334 | from += rem; |
| 1335 | if (nr > rem) |
| 1336 | nr -= rem; |
| 1337 | else |
| 1338 | return 0; |
| 1339 | } |
| 1340 | rem = nr % card->erase_size; |
| 1341 | if (rem) |
| 1342 | nr -= rem; |
| 1343 | } |
| 1344 | |
| 1345 | if (nr == 0) |
| 1346 | return 0; |
| 1347 | |
| 1348 | to = from + nr; |
| 1349 | |
| 1350 | if (to <= from) |
| 1351 | return -EINVAL; |
| 1352 | |
| 1353 | /* 'from' and 'to' are inclusive */ |
| 1354 | to -= 1; |
| 1355 | |
| 1356 | return mmc_do_erase(card, from, to, arg); |
| 1357 | } |
| 1358 | EXPORT_SYMBOL(mmc_erase); |
| 1359 | |
| 1360 | int mmc_can_erase(struct mmc_card *card) |
| 1361 | { |
| 1362 | if ((card->host->caps & MMC_CAP_ERASE) && |
| 1363 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
| 1364 | return 1; |
| 1365 | return 0; |
| 1366 | } |
| 1367 | EXPORT_SYMBOL(mmc_can_erase); |
| 1368 | |
| 1369 | int mmc_can_trim(struct mmc_card *card) |
| 1370 | { |
| 1371 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) |
| 1372 | return 1; |
| 1373 | return 0; |
| 1374 | } |
| 1375 | EXPORT_SYMBOL(mmc_can_trim); |
| 1376 | |
| 1377 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
| 1378 | { |
| 1379 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) |
| 1380 | return 1; |
| 1381 | return 0; |
| 1382 | } |
| 1383 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); |
| 1384 | |
| 1385 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, |
| 1386 | unsigned int nr) |
| 1387 | { |
| 1388 | if (!card->erase_size) |
| 1389 | return 0; |
| 1390 | if (from % card->erase_size || nr % card->erase_size) |
| 1391 | return 0; |
| 1392 | return 1; |
| 1393 | } |
| 1394 | EXPORT_SYMBOL(mmc_erase_group_aligned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1395 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1396 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1398 | struct mmc_host *host = |
| 1399 | container_of(work, struct mmc_host, detect.work); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1400 | u32 ocr; |
| 1401 | int err; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1402 | unsigned long flags; |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1403 | int i; |
| 1404 | const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1405 | |
| 1406 | spin_lock_irqsave(&host->lock, flags); |
| 1407 | |
| 1408 | if (host->rescan_disable) { |
| 1409 | spin_unlock_irqrestore(&host->lock, flags); |
| 1410 | return; |
| 1411 | } |
| 1412 | |
| 1413 | spin_unlock_irqrestore(&host->lock, flags); |
| 1414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1416 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1417 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1418 | /* if there is a card registered, check whether it is still present */ |
| 1419 | if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead) |
| 1420 | host->bus_ops->detect(host); |
| 1421 | |
| 1422 | mmc_bus_put(host); |
| 1423 | |
| 1424 | |
| 1425 | mmc_bus_get(host); |
| 1426 | |
| 1427 | /* if there still is a card present, stop here */ |
| 1428 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1429 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1430 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1431 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1432 | |
| 1433 | /* detect a newly inserted card */ |
| 1434 | |
| 1435 | /* |
| 1436 | * Only we can add a new handler, so it's safe to |
| 1437 | * release the lock here. |
| 1438 | */ |
| 1439 | mmc_bus_put(host); |
| 1440 | |
| 1441 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
| 1442 | goto out; |
| 1443 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1444 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
| 1445 | mmc_claim_host(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1446 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1447 | if (freqs[i] >= host->f_min) |
| 1448 | host->f_init = freqs[i]; |
| 1449 | else if (!i || freqs[i-1] > host->f_min) |
| 1450 | host->f_init = host->f_min; |
| 1451 | else { |
| 1452 | mmc_release_host(host); |
| 1453 | goto out; |
| 1454 | } |
Eric Bénard | 4d0b861 | 2010-10-12 09:29:37 +0200 | [diff] [blame^] | 1455 | #ifdef CONFIG_MMC_DEBUG |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1456 | pr_info("%s: %s: trying to init card at %u Hz\n", |
| 1457 | mmc_hostname(host), __func__, host->f_init); |
Eric Bénard | 4d0b861 | 2010-10-12 09:29:37 +0200 | [diff] [blame^] | 1458 | #endif |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1459 | mmc_power_up(host); |
| 1460 | sdio_reset(host); |
| 1461 | mmc_go_idle(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1462 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1463 | mmc_send_if_cond(host, host->ocr_avail); |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1464 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1465 | /* |
| 1466 | * First we search for SDIO... |
| 1467 | */ |
| 1468 | err = mmc_send_io_op_cond(host, 0, &ocr); |
| 1469 | if (!err) { |
| 1470 | if (mmc_attach_sdio(host, ocr)) { |
| 1471 | mmc_claim_host(host); |
| 1472 | /* |
| 1473 | * Try SDMEM (but not MMC) even if SDIO |
| 1474 | * is broken. |
| 1475 | */ |
| 1476 | if (mmc_send_app_op_cond(host, 0, &ocr)) |
| 1477 | goto out_fail; |
| 1478 | |
| 1479 | if (mmc_attach_sd(host, ocr)) |
| 1480 | mmc_power_off(host); |
| 1481 | } |
| 1482 | goto out; |
| 1483 | } |
| 1484 | |
| 1485 | /* |
| 1486 | * ...then normal SD... |
| 1487 | */ |
| 1488 | err = mmc_send_app_op_cond(host, 0, &ocr); |
| 1489 | if (!err) { |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1490 | if (mmc_attach_sd(host, ocr)) |
| 1491 | mmc_power_off(host); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1492 | goto out; |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1493 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1494 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1495 | /* |
| 1496 | * ...and finally MMC. |
| 1497 | */ |
| 1498 | err = mmc_send_op_cond(host, 0, &ocr); |
| 1499 | if (!err) { |
| 1500 | if (mmc_attach_mmc(host, ocr)) |
| 1501 | mmc_power_off(host); |
| 1502 | goto out; |
| 1503 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1504 | |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1505 | out_fail: |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1506 | mmc_release_host(host); |
| 1507 | mmc_power_off(host); |
| 1508 | } |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 1509 | out: |
| 1510 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 1511 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | } |
| 1513 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1514 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1516 | mmc_power_off(host); |
| 1517 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
| 1519 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1520 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1522 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1523 | unsigned long flags; |
| 1524 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1525 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1526 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1527 | #endif |
| 1528 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1529 | if (host->caps & MMC_CAP_DISABLE) |
| 1530 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 1531 | cancel_delayed_work(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1532 | mmc_flush_scheduled_work(); |
| 1533 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1534 | /* clear pm flags now and let card drivers set them as needed */ |
| 1535 | host->pm_flags = 0; |
| 1536 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1537 | mmc_bus_get(host); |
| 1538 | if (host->bus_ops && !host->bus_dead) { |
| 1539 | if (host->bus_ops->remove) |
| 1540 | host->bus_ops->remove(host); |
| 1541 | |
| 1542 | mmc_claim_host(host); |
| 1543 | mmc_detach_bus(host); |
| 1544 | mmc_release_host(host); |
Denis Karpov | 53509f0 | 2009-09-22 16:44:36 -0700 | [diff] [blame] | 1545 | mmc_bus_put(host); |
| 1546 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1548 | mmc_bus_put(host); |
| 1549 | |
| 1550 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
| 1552 | mmc_power_off(host); |
| 1553 | } |
| 1554 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1555 | void mmc_power_save_host(struct mmc_host *host) |
| 1556 | { |
| 1557 | mmc_bus_get(host); |
| 1558 | |
| 1559 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1560 | mmc_bus_put(host); |
| 1561 | return; |
| 1562 | } |
| 1563 | |
| 1564 | if (host->bus_ops->power_save) |
| 1565 | host->bus_ops->power_save(host); |
| 1566 | |
| 1567 | mmc_bus_put(host); |
| 1568 | |
| 1569 | mmc_power_off(host); |
| 1570 | } |
| 1571 | EXPORT_SYMBOL(mmc_power_save_host); |
| 1572 | |
| 1573 | void mmc_power_restore_host(struct mmc_host *host) |
| 1574 | { |
| 1575 | mmc_bus_get(host); |
| 1576 | |
| 1577 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1578 | mmc_bus_put(host); |
| 1579 | return; |
| 1580 | } |
| 1581 | |
| 1582 | mmc_power_up(host); |
| 1583 | host->bus_ops->power_restore(host); |
| 1584 | |
| 1585 | mmc_bus_put(host); |
| 1586 | } |
| 1587 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 1588 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1589 | int mmc_card_awake(struct mmc_host *host) |
| 1590 | { |
| 1591 | int err = -ENOSYS; |
| 1592 | |
| 1593 | mmc_bus_get(host); |
| 1594 | |
| 1595 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1596 | err = host->bus_ops->awake(host); |
| 1597 | |
| 1598 | mmc_bus_put(host); |
| 1599 | |
| 1600 | return err; |
| 1601 | } |
| 1602 | EXPORT_SYMBOL(mmc_card_awake); |
| 1603 | |
| 1604 | int mmc_card_sleep(struct mmc_host *host) |
| 1605 | { |
| 1606 | int err = -ENOSYS; |
| 1607 | |
| 1608 | mmc_bus_get(host); |
| 1609 | |
| 1610 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1611 | err = host->bus_ops->sleep(host); |
| 1612 | |
| 1613 | mmc_bus_put(host); |
| 1614 | |
| 1615 | return err; |
| 1616 | } |
| 1617 | EXPORT_SYMBOL(mmc_card_sleep); |
| 1618 | |
| 1619 | int mmc_card_can_sleep(struct mmc_host *host) |
| 1620 | { |
| 1621 | struct mmc_card *card = host->card; |
| 1622 | |
| 1623 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) |
| 1624 | return 1; |
| 1625 | return 0; |
| 1626 | } |
| 1627 | EXPORT_SYMBOL(mmc_card_can_sleep); |
| 1628 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | #ifdef CONFIG_PM |
| 1630 | |
| 1631 | /** |
| 1632 | * mmc_suspend_host - suspend a host |
| 1633 | * @host: mmc host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1634 | */ |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 1635 | int mmc_suspend_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1636 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1637 | int err = 0; |
| 1638 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1639 | if (host->caps & MMC_CAP_DISABLE) |
| 1640 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 1641 | cancel_delayed_work(&host->detect); |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1642 | mmc_flush_scheduled_work(); |
| 1643 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1644 | mmc_bus_get(host); |
| 1645 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1646 | if (host->bus_ops->suspend) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1647 | err = host->bus_ops->suspend(host); |
Ohad Ben-Cohen | 1c8cf9c | 2010-10-13 09:31:56 +0200 | [diff] [blame] | 1648 | if (err == -ENOSYS || !host->bus_ops->resume) { |
| 1649 | /* |
| 1650 | * We simply "remove" the card in this case. |
| 1651 | * It will be redetected on resume. |
| 1652 | */ |
| 1653 | if (host->bus_ops->remove) |
| 1654 | host->bus_ops->remove(host); |
| 1655 | mmc_claim_host(host); |
| 1656 | mmc_detach_bus(host); |
| 1657 | mmc_release_host(host); |
| 1658 | host->pm_flags = 0; |
| 1659 | err = 0; |
| 1660 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1661 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1662 | mmc_bus_put(host); |
| 1663 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1664 | if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER)) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1665 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1667 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | EXPORT_SYMBOL(mmc_suspend_host); |
| 1671 | |
| 1672 | /** |
| 1673 | * mmc_resume_host - resume a previously suspended host |
| 1674 | * @host: mmc host |
| 1675 | */ |
| 1676 | int mmc_resume_host(struct mmc_host *host) |
| 1677 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1678 | int err = 0; |
| 1679 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1680 | mmc_bus_get(host); |
| 1681 | if (host->bus_ops && !host->bus_dead) { |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1682 | if (!(host->pm_flags & MMC_PM_KEEP_POWER)) { |
| 1683 | mmc_power_up(host); |
| 1684 | mmc_select_voltage(host, host->ocr); |
| 1685 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1686 | BUG_ON(!host->bus_ops->resume); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1687 | err = host->bus_ops->resume(host); |
| 1688 | if (err) { |
| 1689 | printk(KERN_WARNING "%s: error %d during resume " |
| 1690 | "(card was removed?)\n", |
| 1691 | mmc_hostname(host), err); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1692 | err = 0; |
| 1693 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1694 | } |
| 1695 | mmc_bus_put(host); |
| 1696 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1697 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1699 | EXPORT_SYMBOL(mmc_resume_host); |
| 1700 | |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1701 | /* Do the card removal on suspend if card is assumed removeable |
| 1702 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
| 1703 | to sync the card. |
| 1704 | */ |
| 1705 | int mmc_pm_notify(struct notifier_block *notify_block, |
| 1706 | unsigned long mode, void *unused) |
| 1707 | { |
| 1708 | struct mmc_host *host = container_of( |
| 1709 | notify_block, struct mmc_host, pm_notify); |
| 1710 | unsigned long flags; |
| 1711 | |
| 1712 | |
| 1713 | switch (mode) { |
| 1714 | case PM_HIBERNATION_PREPARE: |
| 1715 | case PM_SUSPEND_PREPARE: |
| 1716 | |
| 1717 | spin_lock_irqsave(&host->lock, flags); |
| 1718 | host->rescan_disable = 1; |
| 1719 | spin_unlock_irqrestore(&host->lock, flags); |
| 1720 | cancel_delayed_work_sync(&host->detect); |
| 1721 | |
| 1722 | if (!host->bus_ops || host->bus_ops->suspend) |
| 1723 | break; |
| 1724 | |
| 1725 | mmc_claim_host(host); |
| 1726 | |
| 1727 | if (host->bus_ops->remove) |
| 1728 | host->bus_ops->remove(host); |
| 1729 | |
| 1730 | mmc_detach_bus(host); |
| 1731 | mmc_release_host(host); |
| 1732 | host->pm_flags = 0; |
| 1733 | break; |
| 1734 | |
| 1735 | case PM_POST_SUSPEND: |
| 1736 | case PM_POST_HIBERNATION: |
| 1737 | |
| 1738 | spin_lock_irqsave(&host->lock, flags); |
| 1739 | host->rescan_disable = 0; |
| 1740 | spin_unlock_irqrestore(&host->lock, flags); |
| 1741 | mmc_detect_change(host, 0); |
| 1742 | |
| 1743 | } |
| 1744 | |
| 1745 | return 0; |
| 1746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | #endif |
| 1748 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1749 | static int __init mmc_init(void) |
| 1750 | { |
| 1751 | int ret; |
| 1752 | |
| 1753 | workqueue = create_singlethread_workqueue("kmmcd"); |
| 1754 | if (!workqueue) |
| 1755 | return -ENOMEM; |
| 1756 | |
| 1757 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1758 | if (ret) |
| 1759 | goto destroy_workqueue; |
| 1760 | |
| 1761 | ret = mmc_register_host_class(); |
| 1762 | if (ret) |
| 1763 | goto unregister_bus; |
| 1764 | |
| 1765 | ret = sdio_register_bus(); |
| 1766 | if (ret) |
| 1767 | goto unregister_host_class; |
| 1768 | |
| 1769 | return 0; |
| 1770 | |
| 1771 | unregister_host_class: |
| 1772 | mmc_unregister_host_class(); |
| 1773 | unregister_bus: |
| 1774 | mmc_unregister_bus(); |
| 1775 | destroy_workqueue: |
| 1776 | destroy_workqueue(workqueue); |
| 1777 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1778 | return ret; |
| 1779 | } |
| 1780 | |
| 1781 | static void __exit mmc_exit(void) |
| 1782 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1783 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1784 | mmc_unregister_host_class(); |
| 1785 | mmc_unregister_bus(); |
| 1786 | destroy_workqueue(workqueue); |
| 1787 | } |
| 1788 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 1789 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1790 | module_exit(mmc_exit); |
| 1791 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | MODULE_LICENSE("GPL"); |