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 | /* |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 51 | * Internal function. Schedule delayed work in the MMC work queue. |
| 52 | */ |
| 53 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 54 | unsigned long delay) |
| 55 | { |
| 56 | return queue_delayed_work(workqueue, work, delay); |
| 57 | } |
| 58 | |
| 59 | /* |
| 60 | * Internal function. Flush all scheduled work from the MMC work queue. |
| 61 | */ |
| 62 | static void mmc_flush_scheduled_work(void) |
| 63 | { |
| 64 | flush_workqueue(workqueue); |
| 65 | } |
| 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 68 | * mmc_request_done - finish processing an MMC request |
| 69 | * @host: MMC host which completed request |
| 70 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * |
| 72 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 73 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 75 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 76 | { |
| 77 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 78 | int err = cmd->error; |
| 79 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 80 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 81 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 82 | cmd->retries = 0; |
| 83 | } |
| 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | if (err && cmd->retries) { |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 86 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 87 | mmc_hostname(host), cmd->opcode, err); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | cmd->retries--; |
| 90 | cmd->error = 0; |
| 91 | host->ops->request(host, mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 92 | } else { |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 93 | led_trigger_event(host->led, LED_OFF); |
| 94 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 95 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 96 | mmc_hostname(host), cmd->opcode, err, |
| 97 | cmd->resp[0], cmd->resp[1], |
| 98 | cmd->resp[2], cmd->resp[3]); |
| 99 | |
| 100 | if (mrq->data) { |
| 101 | pr_debug("%s: %d bytes transferred: %d\n", |
| 102 | mmc_hostname(host), |
| 103 | mrq->data->bytes_xfered, mrq->data->error); |
| 104 | } |
| 105 | |
| 106 | if (mrq->stop) { |
| 107 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 108 | mmc_hostname(host), mrq->stop->opcode, |
| 109 | mrq->stop->error, |
| 110 | mrq->stop->resp[0], mrq->stop->resp[1], |
| 111 | mrq->stop->resp[2], mrq->stop->resp[3]); |
| 112 | } |
| 113 | |
| 114 | if (mrq->done) |
| 115 | mrq->done(mrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | EXPORT_SYMBOL(mmc_request_done); |
| 120 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 121 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 123 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 124 | #ifdef CONFIG_MMC_DEBUG |
| 125 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 126 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 127 | #endif |
| 128 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 129 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 130 | mmc_hostname(host), mrq->cmd->opcode, |
| 131 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 133 | if (mrq->data) { |
| 134 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 135 | "tsac %d ms nsac %d\n", |
| 136 | mmc_hostname(host), mrq->data->blksz, |
| 137 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 138 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 139 | mrq->data->timeout_clks); |
| 140 | } |
| 141 | |
| 142 | if (mrq->stop) { |
| 143 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 144 | mmc_hostname(host), mrq->stop->opcode, |
| 145 | mrq->stop->arg, mrq->stop->flags); |
| 146 | } |
| 147 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 148 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 150 | led_trigger_event(host->led, LED_FULL); |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | mrq->cmd->error = 0; |
| 153 | mrq->cmd->mrq = mrq; |
| 154 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 155 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 156 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 157 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 158 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 159 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 160 | #ifdef CONFIG_MMC_DEBUG |
| 161 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 162 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 163 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 164 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 165 | #endif |
| 166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | mrq->cmd->data = mrq->data; |
| 168 | mrq->data->error = 0; |
| 169 | mrq->data->mrq = mrq; |
| 170 | if (mrq->stop) { |
| 171 | mrq->data->stop = mrq->stop; |
| 172 | mrq->stop->error = 0; |
| 173 | mrq->stop->mrq = mrq; |
| 174 | } |
| 175 | } |
| 176 | host->ops->request(host, mrq); |
| 177 | } |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | static void mmc_wait_done(struct mmc_request *mrq) |
| 180 | { |
| 181 | complete(mrq->done_data); |
| 182 | } |
| 183 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 184 | /** |
| 185 | * mmc_wait_for_req - start a request and wait for completion |
| 186 | * @host: MMC host to start command |
| 187 | * @mrq: MMC request to start |
| 188 | * |
| 189 | * Start a new MMC custom command request for a host, and wait |
| 190 | * for the command to complete. Does not attempt to parse the |
| 191 | * response. |
| 192 | */ |
| 193 | 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] | 194 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 195 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | mrq->done_data = &complete; |
| 198 | mrq->done = mmc_wait_done; |
| 199 | |
| 200 | mmc_start_request(host, mrq); |
| 201 | |
| 202 | wait_for_completion(&complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 206 | |
| 207 | /** |
| 208 | * mmc_wait_for_cmd - start a command and wait for completion |
| 209 | * @host: MMC host to start command |
| 210 | * @cmd: MMC command to start |
| 211 | * @retries: maximum number of retries |
| 212 | * |
| 213 | * Start a new MMC command for a host, and wait for the command |
| 214 | * to complete. Return any error that occurred while the command |
| 215 | * was executing. Do not attempt to parse the response. |
| 216 | */ |
| 217 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 218 | { |
| 219 | struct mmc_request mrq; |
| 220 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 221 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 224 | |
| 225 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 226 | cmd->retries = retries; |
| 227 | |
| 228 | mrq.cmd = cmd; |
| 229 | cmd->data = NULL; |
| 230 | |
| 231 | mmc_wait_for_req(host, &mrq); |
| 232 | |
| 233 | return cmd->error; |
| 234 | } |
| 235 | |
| 236 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 237 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 238 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 239 | * mmc_set_data_timeout - set the timeout for a data command |
| 240 | * @data: data phase for command |
| 241 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 242 | * |
| 243 | * Computes the data timeout parameters according to the |
| 244 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 245 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 246 | 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] | 247 | { |
| 248 | unsigned int mult; |
| 249 | |
| 250 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 251 | * SDIO cards only define an upper 1 s limit on access. |
| 252 | */ |
| 253 | if (mmc_card_sdio(card)) { |
| 254 | data->timeout_ns = 1000000000; |
| 255 | data->timeout_clks = 0; |
| 256 | return; |
| 257 | } |
| 258 | |
| 259 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 260 | * SD cards use a 100 multiplier rather than 10 |
| 261 | */ |
| 262 | mult = mmc_card_sd(card) ? 100 : 10; |
| 263 | |
| 264 | /* |
| 265 | * Scale up the multiplier (and therefore the timeout) by |
| 266 | * the r2w factor for writes. |
| 267 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 268 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 269 | mult <<= card->csd.r2w_factor; |
| 270 | |
| 271 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 272 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 273 | |
| 274 | /* |
| 275 | * SD cards also have an upper limit on the timeout. |
| 276 | */ |
| 277 | if (mmc_card_sd(card)) { |
| 278 | unsigned int timeout_us, limit_us; |
| 279 | |
| 280 | timeout_us = data->timeout_ns / 1000; |
| 281 | timeout_us += data->timeout_clks * 1000 / |
| 282 | (card->host->ios.clock / 1000); |
| 283 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 284 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 285 | /* |
| 286 | * The limit is really 250 ms, but that is |
| 287 | * insufficient for some crappy cards. |
| 288 | */ |
| 289 | limit_us = 300000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 290 | else |
| 291 | limit_us = 100000; |
| 292 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 293 | /* |
| 294 | * SDHC cards always use these fixed values. |
| 295 | */ |
| 296 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 297 | data->timeout_ns = limit_us * 1000; |
| 298 | data->timeout_clks = 0; |
| 299 | } |
| 300 | } |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 301 | /* |
| 302 | * Some cards need very high timeouts if driven in SPI mode. |
| 303 | * The worst observed timeout was 900ms after writing a |
| 304 | * continuous stream of data until the internal logic |
| 305 | * overflowed. |
| 306 | */ |
| 307 | if (mmc_host_is_spi(card->host)) { |
| 308 | if (data->flags & MMC_DATA_WRITE) { |
| 309 | if (data->timeout_ns < 1000000000) |
| 310 | data->timeout_ns = 1000000000; /* 1s */ |
| 311 | } else { |
| 312 | if (data->timeout_ns < 100000000) |
| 313 | data->timeout_ns = 100000000; /* 100ms */ |
| 314 | } |
| 315 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 316 | } |
| 317 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 320 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 321 | * @card: the MMC card associated with the data transfer |
| 322 | * @sz: original transfer size |
| 323 | * |
| 324 | * Pads the original data size with a number of extra bytes in |
| 325 | * order to avoid controller bugs and/or performance hits |
| 326 | * (e.g. some controllers revert to PIO for certain sizes). |
| 327 | * |
| 328 | * Returns the improved size, which might be unmodified. |
| 329 | * |
| 330 | * Note that this function is only relevant when issuing a |
| 331 | * single scatter gather entry. |
| 332 | */ |
| 333 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 334 | { |
| 335 | /* |
| 336 | * FIXME: We don't have a system for the controller to tell |
| 337 | * the core about its problems yet, so for now we just 32-bit |
| 338 | * align the size. |
| 339 | */ |
| 340 | sz = ((sz + 3) / 4) * 4; |
| 341 | |
| 342 | return sz; |
| 343 | } |
| 344 | EXPORT_SYMBOL(mmc_align_data_size); |
| 345 | |
| 346 | /** |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 347 | * mmc_host_enable - enable a host. |
| 348 | * @host: mmc host to enable |
| 349 | * |
| 350 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 351 | * methods to exit and enter power saving states. For more information |
| 352 | * see comments for struct mmc_host_ops. |
| 353 | */ |
| 354 | int mmc_host_enable(struct mmc_host *host) |
| 355 | { |
| 356 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 357 | return 0; |
| 358 | |
| 359 | if (host->en_dis_recurs) |
| 360 | return 0; |
| 361 | |
| 362 | if (host->nesting_cnt++) |
| 363 | return 0; |
| 364 | |
| 365 | cancel_delayed_work_sync(&host->disable); |
| 366 | |
| 367 | if (host->enabled) |
| 368 | return 0; |
| 369 | |
| 370 | if (host->ops->enable) { |
| 371 | int err; |
| 372 | |
| 373 | host->en_dis_recurs = 1; |
| 374 | err = host->ops->enable(host); |
| 375 | host->en_dis_recurs = 0; |
| 376 | |
| 377 | if (err) { |
| 378 | pr_debug("%s: enable error %d\n", |
| 379 | mmc_hostname(host), err); |
| 380 | return err; |
| 381 | } |
| 382 | } |
| 383 | host->enabled = 1; |
| 384 | return 0; |
| 385 | } |
| 386 | EXPORT_SYMBOL(mmc_host_enable); |
| 387 | |
| 388 | static int mmc_host_do_disable(struct mmc_host *host, int lazy) |
| 389 | { |
| 390 | if (host->ops->disable) { |
| 391 | int err; |
| 392 | |
| 393 | host->en_dis_recurs = 1; |
| 394 | err = host->ops->disable(host, lazy); |
| 395 | host->en_dis_recurs = 0; |
| 396 | |
| 397 | if (err < 0) { |
| 398 | pr_debug("%s: disable error %d\n", |
| 399 | mmc_hostname(host), err); |
| 400 | return err; |
| 401 | } |
| 402 | if (err > 0) { |
| 403 | unsigned long delay = msecs_to_jiffies(err); |
| 404 | |
| 405 | mmc_schedule_delayed_work(&host->disable, delay); |
| 406 | } |
| 407 | } |
| 408 | host->enabled = 0; |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * mmc_host_disable - disable a host. |
| 414 | * @host: mmc host to disable |
| 415 | * |
| 416 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 417 | * methods to exit and enter power saving states. For more information |
| 418 | * see comments for struct mmc_host_ops. |
| 419 | */ |
| 420 | int mmc_host_disable(struct mmc_host *host) |
| 421 | { |
| 422 | int err; |
| 423 | |
| 424 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 425 | return 0; |
| 426 | |
| 427 | if (host->en_dis_recurs) |
| 428 | return 0; |
| 429 | |
| 430 | if (--host->nesting_cnt) |
| 431 | return 0; |
| 432 | |
| 433 | if (!host->enabled) |
| 434 | return 0; |
| 435 | |
| 436 | err = mmc_host_do_disable(host, 0); |
| 437 | return err; |
| 438 | } |
| 439 | EXPORT_SYMBOL(mmc_host_disable); |
| 440 | |
| 441 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 442 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 444 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 446 | * Claim a host for a set of operations. If @abort is non null and |
| 447 | * dereference a non-zero value then this will return prematurely with |
| 448 | * that non-zero value without acquiring the lock. Returns zero |
| 449 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 451 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
| 453 | DECLARE_WAITQUEUE(wait, current); |
| 454 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 455 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 457 | might_sleep(); |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | add_wait_queue(&host->wq, &wait); |
| 460 | spin_lock_irqsave(&host->lock, flags); |
| 461 | while (1) { |
| 462 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 463 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 464 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | break; |
| 466 | spin_unlock_irqrestore(&host->lock, flags); |
| 467 | schedule(); |
| 468 | spin_lock_irqsave(&host->lock, flags); |
| 469 | } |
| 470 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 471 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 472 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 473 | host->claimer = current; |
| 474 | host->claim_cnt += 1; |
| 475 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 476 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | spin_unlock_irqrestore(&host->lock, flags); |
| 478 | remove_wait_queue(&host->wq, &wait); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 479 | if (!stop) |
| 480 | mmc_host_enable(host); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 481 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 484 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 486 | /** |
| 487 | * mmc_try_claim_host - try exclusively to claim a host |
| 488 | * @host: mmc host to claim |
| 489 | * |
| 490 | * Returns %1 if the host is claimed, %0 otherwise. |
| 491 | */ |
| 492 | int mmc_try_claim_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 493 | { |
| 494 | int claimed_host = 0; |
| 495 | unsigned long flags; |
| 496 | |
| 497 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 498 | if (!host->claimed || host->claimer == current) { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 499 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 500 | host->claimer = current; |
| 501 | host->claim_cnt += 1; |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 502 | claimed_host = 1; |
| 503 | } |
| 504 | spin_unlock_irqrestore(&host->lock, flags); |
| 505 | return claimed_host; |
| 506 | } |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 507 | EXPORT_SYMBOL(mmc_try_claim_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 508 | |
| 509 | static void mmc_do_release_host(struct mmc_host *host) |
| 510 | { |
| 511 | unsigned long flags; |
| 512 | |
| 513 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 514 | if (--host->claim_cnt) { |
| 515 | /* Release for nested claim */ |
| 516 | spin_unlock_irqrestore(&host->lock, flags); |
| 517 | } else { |
| 518 | host->claimed = 0; |
| 519 | host->claimer = NULL; |
| 520 | spin_unlock_irqrestore(&host->lock, flags); |
| 521 | wake_up(&host->wq); |
| 522 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | void mmc_host_deeper_disable(struct work_struct *work) |
| 526 | { |
| 527 | struct mmc_host *host = |
| 528 | container_of(work, struct mmc_host, disable.work); |
| 529 | |
| 530 | /* If the host is claimed then we do not want to disable it anymore */ |
| 531 | if (!mmc_try_claim_host(host)) |
| 532 | return; |
| 533 | mmc_host_do_disable(host, 1); |
| 534 | mmc_do_release_host(host); |
| 535 | } |
| 536 | |
| 537 | /** |
| 538 | * mmc_host_lazy_disable - lazily disable a host. |
| 539 | * @host: mmc host to disable |
| 540 | * |
| 541 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 542 | * methods to exit and enter power saving states. For more information |
| 543 | * see comments for struct mmc_host_ops. |
| 544 | */ |
| 545 | int mmc_host_lazy_disable(struct mmc_host *host) |
| 546 | { |
| 547 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 548 | return 0; |
| 549 | |
| 550 | if (host->en_dis_recurs) |
| 551 | return 0; |
| 552 | |
| 553 | if (--host->nesting_cnt) |
| 554 | return 0; |
| 555 | |
| 556 | if (!host->enabled) |
| 557 | return 0; |
| 558 | |
| 559 | if (host->disable_delay) { |
| 560 | mmc_schedule_delayed_work(&host->disable, |
| 561 | msecs_to_jiffies(host->disable_delay)); |
| 562 | return 0; |
| 563 | } else |
| 564 | return mmc_host_do_disable(host, 1); |
| 565 | } |
| 566 | EXPORT_SYMBOL(mmc_host_lazy_disable); |
| 567 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | /** |
| 569 | * mmc_release_host - release a host |
| 570 | * @host: mmc host to release |
| 571 | * |
| 572 | * Release a MMC host, allowing others to claim the host |
| 573 | * for their operations. |
| 574 | */ |
| 575 | void mmc_release_host(struct mmc_host *host) |
| 576 | { |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 577 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 579 | mmc_host_lazy_disable(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 581 | mmc_do_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | EXPORT_SYMBOL(mmc_release_host); |
| 585 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 586 | /* |
| 587 | * Internal function that does the actual ios call to the host driver, |
| 588 | * optionally printing some debug output. |
| 589 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 590 | static inline void mmc_set_ios(struct mmc_host *host) |
| 591 | { |
| 592 | struct mmc_ios *ios = &host->ios; |
| 593 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 594 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 595 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 596 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 597 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 598 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 599 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 600 | host->ops->set_ios(host, ios); |
| 601 | } |
| 602 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 603 | /* |
| 604 | * Control chip select pin on a host. |
| 605 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 606 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 607 | { |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 608 | host->ios.chip_select = mode; |
| 609 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 613 | * Sets the host clock to the highest possible frequency that |
| 614 | * is below "hz". |
| 615 | */ |
| 616 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 617 | { |
| 618 | WARN_ON(hz < host->f_min); |
| 619 | |
| 620 | if (hz > host->f_max) |
| 621 | hz = host->f_max; |
| 622 | |
| 623 | host->ios.clock = hz; |
| 624 | mmc_set_ios(host); |
| 625 | } |
| 626 | |
| 627 | /* |
| 628 | * Change the bus mode (open drain/push-pull) of a host. |
| 629 | */ |
| 630 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 631 | { |
| 632 | host->ios.bus_mode = mode; |
| 633 | mmc_set_ios(host); |
| 634 | } |
| 635 | |
| 636 | /* |
| 637 | * Change data bus width of a host. |
| 638 | */ |
| 639 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 640 | { |
| 641 | host->ios.bus_width = width; |
| 642 | mmc_set_ios(host); |
| 643 | } |
| 644 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 645 | /** |
| 646 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 647 | * @vdd: voltage (mV) |
| 648 | * @low_bits: prefer low bits in boundary cases |
| 649 | * |
| 650 | * This function returns the OCR bit number according to the provided @vdd |
| 651 | * value. If conversion is not possible a negative errno value returned. |
| 652 | * |
| 653 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 654 | * on boundary voltages. For example, |
| 655 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 656 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 657 | * |
| 658 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 659 | */ |
| 660 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 661 | { |
| 662 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 663 | int bit; |
| 664 | |
| 665 | if (vdd < 1650 || vdd > 3600) |
| 666 | return -EINVAL; |
| 667 | |
| 668 | if (vdd >= 1650 && vdd <= 1950) |
| 669 | return ilog2(MMC_VDD_165_195); |
| 670 | |
| 671 | if (low_bits) |
| 672 | vdd -= 1; |
| 673 | |
| 674 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 675 | bit = (vdd - 2000) / 100 + 8; |
| 676 | if (bit > max_bit) |
| 677 | return max_bit; |
| 678 | return bit; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 683 | * @vdd_min: minimum voltage value (mV) |
| 684 | * @vdd_max: maximum voltage value (mV) |
| 685 | * |
| 686 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 687 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 688 | * |
| 689 | * Notes wrt boundary cases: |
| 690 | * This function sets the OCR bits for all boundary voltages, for example |
| 691 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 692 | * MMC_VDD_34_35 mask. |
| 693 | */ |
| 694 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 695 | { |
| 696 | u32 mask = 0; |
| 697 | |
| 698 | if (vdd_max < vdd_min) |
| 699 | return 0; |
| 700 | |
| 701 | /* Prefer high bits for the boundary vdd_max values. */ |
| 702 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 703 | if (vdd_max < 0) |
| 704 | return 0; |
| 705 | |
| 706 | /* Prefer low bits for the boundary vdd_min values. */ |
| 707 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 708 | if (vdd_min < 0) |
| 709 | return 0; |
| 710 | |
| 711 | /* Fill the mask, from max bit to min bit. */ |
| 712 | while (vdd_max >= vdd_min) |
| 713 | mask |= 1 << vdd_max--; |
| 714 | |
| 715 | return mask; |
| 716 | } |
| 717 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 718 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 719 | #ifdef CONFIG_REGULATOR |
| 720 | |
| 721 | /** |
| 722 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 723 | * @supply: regulator to use |
| 724 | * |
| 725 | * This returns either a negative errno, or a mask of voltages that |
| 726 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 727 | * regulator. This would normally be called before registering the |
| 728 | * MMC host adapter. |
| 729 | */ |
| 730 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 731 | { |
| 732 | int result = 0; |
| 733 | int count; |
| 734 | int i; |
| 735 | |
| 736 | count = regulator_count_voltages(supply); |
| 737 | if (count < 0) |
| 738 | return count; |
| 739 | |
| 740 | for (i = 0; i < count; i++) { |
| 741 | int vdd_uV; |
| 742 | int vdd_mV; |
| 743 | |
| 744 | vdd_uV = regulator_list_voltage(supply, i); |
| 745 | if (vdd_uV <= 0) |
| 746 | continue; |
| 747 | |
| 748 | vdd_mV = vdd_uV / 1000; |
| 749 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 750 | } |
| 751 | |
| 752 | return result; |
| 753 | } |
| 754 | EXPORT_SYMBOL(mmc_regulator_get_ocrmask); |
| 755 | |
| 756 | /** |
| 757 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
| 758 | * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) |
| 759 | * @supply: regulator to use |
| 760 | * |
| 761 | * Returns zero on success, else negative errno. |
| 762 | * |
| 763 | * MMC host drivers may use this to enable or disable a regulator using |
| 764 | * a particular supply voltage. This would normally be called from the |
| 765 | * set_ios() method. |
| 766 | */ |
| 767 | int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit) |
| 768 | { |
| 769 | int result = 0; |
| 770 | int min_uV, max_uV; |
| 771 | int enabled; |
| 772 | |
| 773 | enabled = regulator_is_enabled(supply); |
| 774 | if (enabled < 0) |
| 775 | return enabled; |
| 776 | |
| 777 | if (vdd_bit) { |
| 778 | int tmp; |
| 779 | int voltage; |
| 780 | |
| 781 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 782 | * bits this regulator doesn't quite support ... don't |
| 783 | * be too picky, most cards and regulators are OK with |
| 784 | * a 0.1V range goof (it's a small error percentage). |
| 785 | */ |
| 786 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 787 | if (tmp == 0) { |
| 788 | min_uV = 1650 * 1000; |
| 789 | max_uV = 1950 * 1000; |
| 790 | } else { |
| 791 | min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 792 | max_uV = min_uV + 100 * 1000; |
| 793 | } |
| 794 | |
| 795 | /* avoid needless changes to this voltage; the regulator |
| 796 | * might not allow this operation |
| 797 | */ |
| 798 | voltage = regulator_get_voltage(supply); |
| 799 | if (voltage < 0) |
| 800 | result = voltage; |
| 801 | else if (voltage < min_uV || voltage > max_uV) |
| 802 | result = regulator_set_voltage(supply, min_uV, max_uV); |
| 803 | else |
| 804 | result = 0; |
| 805 | |
| 806 | if (result == 0 && !enabled) |
| 807 | result = regulator_enable(supply); |
| 808 | } else if (enabled) { |
| 809 | result = regulator_disable(supply); |
| 810 | } |
| 811 | |
| 812 | return result; |
| 813 | } |
| 814 | EXPORT_SYMBOL(mmc_regulator_set_ocr); |
| 815 | |
| 816 | #endif |
| 817 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 818 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | * Mask off any voltages we don't support and select |
| 820 | * the lowest voltage |
| 821 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 822 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | { |
| 824 | int bit; |
| 825 | |
| 826 | ocr &= host->ocr_avail; |
| 827 | |
| 828 | bit = ffs(ocr); |
| 829 | if (bit) { |
| 830 | bit -= 1; |
| 831 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 832 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 835 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 836 | } else { |
David Brownell | f6e10b8 | 2008-12-31 09:50:30 -0800 | [diff] [blame] | 837 | pr_warning("%s: host doesn't support card's voltages\n", |
| 838 | mmc_hostname(host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | ocr = 0; |
| 840 | } |
| 841 | |
| 842 | return ocr; |
| 843 | } |
| 844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 846 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 847 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 848 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 850 | host->ios.timing = timing; |
| 851 | mmc_set_ios(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 855 | * Apply power to the MMC stack. This is a two-stage process. |
| 856 | * First, we enable power to the card without the clock running. |
| 857 | * We then wait a bit for the power to stabilise. Finally, |
| 858 | * enable the bus drivers and clock to the card. |
| 859 | * |
| 860 | * We must _NOT_ enable the clock prior to power stablising. |
| 861 | * |
| 862 | * If a host does all the power sequencing itself, ignore the |
| 863 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | */ |
| 865 | static void mmc_power_up(struct mmc_host *host) |
| 866 | { |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 867 | int bit; |
| 868 | |
| 869 | /* If ocr is set, we use it */ |
| 870 | if (host->ocr) |
| 871 | bit = ffs(host->ocr) - 1; |
| 872 | else |
| 873 | bit = fls(host->ocr_avail) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
| 875 | host->ios.vdd = bit; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 876 | if (mmc_host_is_spi(host)) { |
| 877 | host->ios.chip_select = MMC_CS_HIGH; |
| 878 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 879 | } else { |
| 880 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 881 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 882 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 884 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 885 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 886 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 888 | /* |
| 889 | * This delay should be sufficient to allow the power supply |
| 890 | * to reach the minimum voltage. |
| 891 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 892 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 894 | if (host->f_min > 400000) { |
| 895 | pr_warning("%s: Minimum clock frequency too high for " |
| 896 | "identification mode\n", mmc_hostname(host)); |
| 897 | host->ios.clock = host->f_min; |
| 898 | } else |
| 899 | host->ios.clock = 400000; |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 902 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 904 | /* |
| 905 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 906 | * time required to reach a stable voltage. |
| 907 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 908 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | static void mmc_power_off(struct mmc_host *host) |
| 912 | { |
| 913 | host->ios.clock = 0; |
| 914 | host->ios.vdd = 0; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 915 | if (!mmc_host_is_spi(host)) { |
| 916 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 917 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 918 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 920 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 921 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 922 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 926 | * Cleanup when the last reference to the bus operator is dropped. |
| 927 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 928 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 929 | { |
| 930 | BUG_ON(!host); |
| 931 | BUG_ON(host->bus_refs); |
| 932 | BUG_ON(!host->bus_dead); |
| 933 | |
| 934 | host->bus_ops = NULL; |
| 935 | } |
| 936 | |
| 937 | /* |
| 938 | * Increase reference count of bus operator |
| 939 | */ |
| 940 | static inline void mmc_bus_get(struct mmc_host *host) |
| 941 | { |
| 942 | unsigned long flags; |
| 943 | |
| 944 | spin_lock_irqsave(&host->lock, flags); |
| 945 | host->bus_refs++; |
| 946 | spin_unlock_irqrestore(&host->lock, flags); |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Decrease reference count of bus operator and free it if |
| 951 | * it is the last reference. |
| 952 | */ |
| 953 | static inline void mmc_bus_put(struct mmc_host *host) |
| 954 | { |
| 955 | unsigned long flags; |
| 956 | |
| 957 | spin_lock_irqsave(&host->lock, flags); |
| 958 | host->bus_refs--; |
| 959 | if ((host->bus_refs == 0) && host->bus_ops) |
| 960 | __mmc_release_bus(host); |
| 961 | spin_unlock_irqrestore(&host->lock, flags); |
| 962 | } |
| 963 | |
| 964 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 965 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 966 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 968 | 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] | 969 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 970 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 972 | BUG_ON(!host); |
| 973 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 974 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 975 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 976 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 977 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 978 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 979 | BUG_ON(host->bus_ops); |
| 980 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 981 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 982 | host->bus_ops = ops; |
| 983 | host->bus_refs = 1; |
| 984 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 986 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 990 | * Remove the current bus handler from a host. Assumes that there are |
| 991 | * no interesting cards left, so the bus is powered down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 993 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 995 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 996 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 997 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 999 | WARN_ON(!host->claimed); |
| 1000 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1002 | spin_lock_irqsave(&host->lock, flags); |
| 1003 | |
| 1004 | host->bus_dead = 1; |
| 1005 | |
| 1006 | spin_unlock_irqrestore(&host->lock, flags); |
| 1007 | |
| 1008 | mmc_power_off(host); |
| 1009 | |
| 1010 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | /** |
| 1014 | * mmc_detect_change - process change of state on a MMC socket |
| 1015 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1016 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1018 | * MMC drivers should call this when they detect a card has been |
| 1019 | * inserted or removed. The MMC layer will confirm that any |
| 1020 | * present card is still functional, and initialize any newly |
| 1021 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1023 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1025 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1026 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1027 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1028 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1029 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1030 | #endif |
| 1031 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1032 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | EXPORT_SYMBOL(mmc_detect_change); |
| 1036 | |
| 1037 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1038 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1040 | struct mmc_host *host = |
| 1041 | container_of(work, struct mmc_host, detect.work); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1042 | u32 ocr; |
| 1043 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1045 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1047 | /* if there is a card registered, check whether it is still present */ |
| 1048 | if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead) |
| 1049 | host->bus_ops->detect(host); |
| 1050 | |
| 1051 | mmc_bus_put(host); |
| 1052 | |
| 1053 | |
| 1054 | mmc_bus_get(host); |
| 1055 | |
| 1056 | /* if there still is a card present, stop here */ |
| 1057 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1058 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1059 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1061 | |
| 1062 | /* detect a newly inserted card */ |
| 1063 | |
| 1064 | /* |
| 1065 | * Only we can add a new handler, so it's safe to |
| 1066 | * release the lock here. |
| 1067 | */ |
| 1068 | mmc_bus_put(host); |
| 1069 | |
| 1070 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
| 1071 | goto out; |
| 1072 | |
| 1073 | mmc_claim_host(host); |
| 1074 | |
| 1075 | mmc_power_up(host); |
| 1076 | mmc_go_idle(host); |
| 1077 | |
| 1078 | mmc_send_if_cond(host, host->ocr_avail); |
| 1079 | |
| 1080 | /* |
| 1081 | * First we search for SDIO... |
| 1082 | */ |
| 1083 | err = mmc_send_io_op_cond(host, 0, &ocr); |
| 1084 | if (!err) { |
| 1085 | if (mmc_attach_sdio(host, ocr)) |
| 1086 | mmc_power_off(host); |
| 1087 | goto out; |
| 1088 | } |
| 1089 | |
| 1090 | /* |
| 1091 | * ...then normal SD... |
| 1092 | */ |
| 1093 | err = mmc_send_app_op_cond(host, 0, &ocr); |
| 1094 | if (!err) { |
| 1095 | if (mmc_attach_sd(host, ocr)) |
| 1096 | mmc_power_off(host); |
| 1097 | goto out; |
| 1098 | } |
| 1099 | |
| 1100 | /* |
| 1101 | * ...and finally MMC. |
| 1102 | */ |
| 1103 | err = mmc_send_op_cond(host, 0, &ocr); |
| 1104 | if (!err) { |
| 1105 | if (mmc_attach_mmc(host, ocr)) |
| 1106 | mmc_power_off(host); |
| 1107 | goto out; |
| 1108 | } |
| 1109 | |
| 1110 | mmc_release_host(host); |
| 1111 | mmc_power_off(host); |
| 1112 | |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 1113 | out: |
| 1114 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 1115 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1118 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1120 | mmc_power_off(host); |
| 1121 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1124 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1126 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1127 | unsigned long flags; |
| 1128 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1129 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1130 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1131 | #endif |
| 1132 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1133 | if (host->caps & MMC_CAP_DISABLE) |
| 1134 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 1135 | cancel_delayed_work(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1136 | mmc_flush_scheduled_work(); |
| 1137 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1138 | mmc_bus_get(host); |
| 1139 | if (host->bus_ops && !host->bus_dead) { |
| 1140 | if (host->bus_ops->remove) |
| 1141 | host->bus_ops->remove(host); |
| 1142 | |
| 1143 | mmc_claim_host(host); |
| 1144 | mmc_detach_bus(host); |
| 1145 | mmc_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1147 | mmc_bus_put(host); |
| 1148 | |
| 1149 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
| 1151 | mmc_power_off(host); |
| 1152 | } |
| 1153 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1154 | void mmc_power_save_host(struct mmc_host *host) |
| 1155 | { |
| 1156 | mmc_bus_get(host); |
| 1157 | |
| 1158 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1159 | mmc_bus_put(host); |
| 1160 | return; |
| 1161 | } |
| 1162 | |
| 1163 | if (host->bus_ops->power_save) |
| 1164 | host->bus_ops->power_save(host); |
| 1165 | |
| 1166 | mmc_bus_put(host); |
| 1167 | |
| 1168 | mmc_power_off(host); |
| 1169 | } |
| 1170 | EXPORT_SYMBOL(mmc_power_save_host); |
| 1171 | |
| 1172 | void mmc_power_restore_host(struct mmc_host *host) |
| 1173 | { |
| 1174 | mmc_bus_get(host); |
| 1175 | |
| 1176 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1177 | mmc_bus_put(host); |
| 1178 | return; |
| 1179 | } |
| 1180 | |
| 1181 | mmc_power_up(host); |
| 1182 | host->bus_ops->power_restore(host); |
| 1183 | |
| 1184 | mmc_bus_put(host); |
| 1185 | } |
| 1186 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 1187 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame^] | 1188 | int mmc_card_awake(struct mmc_host *host) |
| 1189 | { |
| 1190 | int err = -ENOSYS; |
| 1191 | |
| 1192 | mmc_bus_get(host); |
| 1193 | |
| 1194 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1195 | err = host->bus_ops->awake(host); |
| 1196 | |
| 1197 | mmc_bus_put(host); |
| 1198 | |
| 1199 | return err; |
| 1200 | } |
| 1201 | EXPORT_SYMBOL(mmc_card_awake); |
| 1202 | |
| 1203 | int mmc_card_sleep(struct mmc_host *host) |
| 1204 | { |
| 1205 | int err = -ENOSYS; |
| 1206 | |
| 1207 | mmc_bus_get(host); |
| 1208 | |
| 1209 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1210 | err = host->bus_ops->sleep(host); |
| 1211 | |
| 1212 | mmc_bus_put(host); |
| 1213 | |
| 1214 | return err; |
| 1215 | } |
| 1216 | EXPORT_SYMBOL(mmc_card_sleep); |
| 1217 | |
| 1218 | int mmc_card_can_sleep(struct mmc_host *host) |
| 1219 | { |
| 1220 | struct mmc_card *card = host->card; |
| 1221 | |
| 1222 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) |
| 1223 | return 1; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | EXPORT_SYMBOL(mmc_card_can_sleep); |
| 1227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | #ifdef CONFIG_PM |
| 1229 | |
| 1230 | /** |
| 1231 | * mmc_suspend_host - suspend a host |
| 1232 | * @host: mmc host |
| 1233 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 1234 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 1235 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1237 | if (host->caps & MMC_CAP_DISABLE) |
| 1238 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 1239 | cancel_delayed_work(&host->detect); |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1240 | mmc_flush_scheduled_work(); |
| 1241 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1242 | mmc_bus_get(host); |
| 1243 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1244 | if (host->bus_ops->suspend) |
| 1245 | host->bus_ops->suspend(host); |
| 1246 | if (!host->bus_ops->resume) { |
| 1247 | if (host->bus_ops->remove) |
| 1248 | host->bus_ops->remove(host); |
| 1249 | |
| 1250 | mmc_claim_host(host); |
| 1251 | mmc_detach_bus(host); |
| 1252 | mmc_release_host(host); |
| 1253 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1254 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1255 | mmc_bus_put(host); |
| 1256 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | EXPORT_SYMBOL(mmc_suspend_host); |
| 1263 | |
| 1264 | /** |
| 1265 | * mmc_resume_host - resume a previously suspended host |
| 1266 | * @host: mmc host |
| 1267 | */ |
| 1268 | int mmc_resume_host(struct mmc_host *host) |
| 1269 | { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1270 | mmc_bus_get(host); |
| 1271 | if (host->bus_ops && !host->bus_dead) { |
| 1272 | mmc_power_up(host); |
Balaji Rao | d3096f8 | 2009-02-25 15:28:09 +0530 | [diff] [blame] | 1273 | mmc_select_voltage(host, host->ocr); |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1274 | BUG_ON(!host->bus_ops->resume); |
| 1275 | host->bus_ops->resume(host); |
| 1276 | } |
| 1277 | mmc_bus_put(host); |
| 1278 | |
| 1279 | /* |
| 1280 | * We add a slight delay here so that resume can progress |
| 1281 | * in parallel. |
| 1282 | */ |
| 1283 | mmc_detect_change(host, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | EXPORT_SYMBOL(mmc_resume_host); |
| 1289 | |
| 1290 | #endif |
| 1291 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1292 | static int __init mmc_init(void) |
| 1293 | { |
| 1294 | int ret; |
| 1295 | |
| 1296 | workqueue = create_singlethread_workqueue("kmmcd"); |
| 1297 | if (!workqueue) |
| 1298 | return -ENOMEM; |
| 1299 | |
| 1300 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1301 | if (ret) |
| 1302 | goto destroy_workqueue; |
| 1303 | |
| 1304 | ret = mmc_register_host_class(); |
| 1305 | if (ret) |
| 1306 | goto unregister_bus; |
| 1307 | |
| 1308 | ret = sdio_register_bus(); |
| 1309 | if (ret) |
| 1310 | goto unregister_host_class; |
| 1311 | |
| 1312 | return 0; |
| 1313 | |
| 1314 | unregister_host_class: |
| 1315 | mmc_unregister_host_class(); |
| 1316 | unregister_bus: |
| 1317 | mmc_unregister_bus(); |
| 1318 | destroy_workqueue: |
| 1319 | destroy_workqueue(workqueue); |
| 1320 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | static void __exit mmc_exit(void) |
| 1325 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1326 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1327 | mmc_unregister_host_class(); |
| 1328 | mmc_unregister_bus(); |
| 1329 | destroy_workqueue(workqueue); |
| 1330 | } |
| 1331 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 1332 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1333 | module_exit(mmc_exit); |
| 1334 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1335 | MODULE_LICENSE("GPL"); |