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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/mmc/card.h> |
| 26 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 27 | #include <linux/mmc/mmc.h> |
| 28 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 30 | #include "core.h" |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 31 | #include "bus.h" |
| 32 | #include "host.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 33 | #include "sdio_bus.h" |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 34 | |
| 35 | #include "mmc_ops.h" |
| 36 | #include "sd_ops.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 37 | #include "sdio_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 39 | static struct workqueue_struct *workqueue; |
| 40 | |
| 41 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 42 | * Enabling software CRCs on the data blocks can be a significant (30%) |
| 43 | * performance cost, and for other reasons may not always be desired. |
| 44 | * So we allow it it to be disabled. |
| 45 | */ |
| 46 | int use_spi_crc = 1; |
| 47 | module_param(use_spi_crc, bool, 0); |
| 48 | |
| 49 | /* |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 50 | * Internal function. Schedule delayed work in the MMC work queue. |
| 51 | */ |
| 52 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 53 | unsigned long delay) |
| 54 | { |
| 55 | return queue_delayed_work(workqueue, work, delay); |
| 56 | } |
| 57 | |
| 58 | /* |
| 59 | * Internal function. Flush all scheduled work from the MMC work queue. |
| 60 | */ |
| 61 | static void mmc_flush_scheduled_work(void) |
| 62 | { |
| 63 | flush_workqueue(workqueue); |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 67 | * mmc_request_done - finish processing an MMC request |
| 68 | * @host: MMC host which completed request |
| 69 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | * |
| 71 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 72 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 75 | { |
| 76 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 77 | int err = cmd->error; |
| 78 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 79 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 80 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 81 | cmd->retries = 0; |
| 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | if (err && cmd->retries) { |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 85 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 86 | mmc_hostname(host), cmd->opcode, err); |
| 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | cmd->retries--; |
| 89 | cmd->error = 0; |
| 90 | host->ops->request(host, mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 91 | } else { |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 92 | led_trigger_event(host->led, LED_OFF); |
| 93 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 94 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 95 | mmc_hostname(host), cmd->opcode, err, |
| 96 | cmd->resp[0], cmd->resp[1], |
| 97 | cmd->resp[2], cmd->resp[3]); |
| 98 | |
| 99 | if (mrq->data) { |
| 100 | pr_debug("%s: %d bytes transferred: %d\n", |
| 101 | mmc_hostname(host), |
| 102 | mrq->data->bytes_xfered, mrq->data->error); |
| 103 | } |
| 104 | |
| 105 | if (mrq->stop) { |
| 106 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 107 | mmc_hostname(host), mrq->stop->opcode, |
| 108 | mrq->stop->error, |
| 109 | mrq->stop->resp[0], mrq->stop->resp[1], |
| 110 | mrq->stop->resp[2], mrq->stop->resp[3]); |
| 111 | } |
| 112 | |
| 113 | if (mrq->done) |
| 114 | mrq->done(mrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | EXPORT_SYMBOL(mmc_request_done); |
| 119 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 120 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 122 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 123 | #ifdef CONFIG_MMC_DEBUG |
| 124 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 125 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 126 | #endif |
| 127 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 128 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 129 | mmc_hostname(host), mrq->cmd->opcode, |
| 130 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 132 | if (mrq->data) { |
| 133 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 134 | "tsac %d ms nsac %d\n", |
| 135 | mmc_hostname(host), mrq->data->blksz, |
| 136 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 137 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 138 | mrq->data->timeout_clks); |
| 139 | } |
| 140 | |
| 141 | if (mrq->stop) { |
| 142 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 143 | mmc_hostname(host), mrq->stop->opcode, |
| 144 | mrq->stop->arg, mrq->stop->flags); |
| 145 | } |
| 146 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 147 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 149 | led_trigger_event(host->led, LED_FULL); |
| 150 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | mrq->cmd->error = 0; |
| 152 | mrq->cmd->mrq = mrq; |
| 153 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 154 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 155 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 156 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 157 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 158 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 159 | #ifdef CONFIG_MMC_DEBUG |
| 160 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 161 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 162 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 163 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 164 | #endif |
| 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | mrq->cmd->data = mrq->data; |
| 167 | mrq->data->error = 0; |
| 168 | mrq->data->mrq = mrq; |
| 169 | if (mrq->stop) { |
| 170 | mrq->data->stop = mrq->stop; |
| 171 | mrq->stop->error = 0; |
| 172 | mrq->stop->mrq = mrq; |
| 173 | } |
| 174 | } |
| 175 | host->ops->request(host, mrq); |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | static void mmc_wait_done(struct mmc_request *mrq) |
| 179 | { |
| 180 | complete(mrq->done_data); |
| 181 | } |
| 182 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 183 | /** |
| 184 | * mmc_wait_for_req - start a request and wait for completion |
| 185 | * @host: MMC host to start command |
| 186 | * @mrq: MMC request to start |
| 187 | * |
| 188 | * Start a new MMC custom command request for a host, and wait |
| 189 | * for the command to complete. Does not attempt to parse the |
| 190 | * response. |
| 191 | */ |
| 192 | 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] | 193 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 194 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
| 196 | mrq->done_data = &complete; |
| 197 | mrq->done = mmc_wait_done; |
| 198 | |
| 199 | mmc_start_request(host, mrq); |
| 200 | |
| 201 | wait_for_completion(&complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 205 | |
| 206 | /** |
| 207 | * mmc_wait_for_cmd - start a command and wait for completion |
| 208 | * @host: MMC host to start command |
| 209 | * @cmd: MMC command to start |
| 210 | * @retries: maximum number of retries |
| 211 | * |
| 212 | * Start a new MMC command for a host, and wait for the command |
| 213 | * to complete. Return any error that occurred while the command |
| 214 | * was executing. Do not attempt to parse the response. |
| 215 | */ |
| 216 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 217 | { |
| 218 | struct mmc_request mrq; |
| 219 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 220 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
| 222 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 223 | |
| 224 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 225 | cmd->retries = retries; |
| 226 | |
| 227 | mrq.cmd = cmd; |
| 228 | cmd->data = NULL; |
| 229 | |
| 230 | mmc_wait_for_req(host, &mrq); |
| 231 | |
| 232 | return cmd->error; |
| 233 | } |
| 234 | |
| 235 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 236 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 237 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 238 | * mmc_set_data_timeout - set the timeout for a data command |
| 239 | * @data: data phase for command |
| 240 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 241 | * |
| 242 | * Computes the data timeout parameters according to the |
| 243 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 244 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 245 | 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] | 246 | { |
| 247 | unsigned int mult; |
| 248 | |
| 249 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 250 | * SDIO cards only define an upper 1 s limit on access. |
| 251 | */ |
| 252 | if (mmc_card_sdio(card)) { |
| 253 | data->timeout_ns = 1000000000; |
| 254 | data->timeout_clks = 0; |
| 255 | return; |
| 256 | } |
| 257 | |
| 258 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 259 | * SD cards use a 100 multiplier rather than 10 |
| 260 | */ |
| 261 | mult = mmc_card_sd(card) ? 100 : 10; |
| 262 | |
| 263 | /* |
| 264 | * Scale up the multiplier (and therefore the timeout) by |
| 265 | * the r2w factor for writes. |
| 266 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 267 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 268 | mult <<= card->csd.r2w_factor; |
| 269 | |
| 270 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 271 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 272 | |
| 273 | /* |
| 274 | * SD cards also have an upper limit on the timeout. |
| 275 | */ |
| 276 | if (mmc_card_sd(card)) { |
| 277 | unsigned int timeout_us, limit_us; |
| 278 | |
| 279 | timeout_us = data->timeout_ns / 1000; |
| 280 | timeout_us += data->timeout_clks * 1000 / |
| 281 | (card->host->ios.clock / 1000); |
| 282 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 283 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 284 | /* |
| 285 | * The limit is really 250 ms, but that is |
| 286 | * insufficient for some crappy cards. |
| 287 | */ |
| 288 | limit_us = 300000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 289 | else |
| 290 | limit_us = 100000; |
| 291 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 292 | /* |
| 293 | * SDHC cards always use these fixed values. |
| 294 | */ |
| 295 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 296 | data->timeout_ns = limit_us * 1000; |
| 297 | data->timeout_clks = 0; |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 302 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 304 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 305 | * @card: the MMC card associated with the data transfer |
| 306 | * @sz: original transfer size |
| 307 | * |
| 308 | * Pads the original data size with a number of extra bytes in |
| 309 | * order to avoid controller bugs and/or performance hits |
| 310 | * (e.g. some controllers revert to PIO for certain sizes). |
| 311 | * |
| 312 | * Returns the improved size, which might be unmodified. |
| 313 | * |
| 314 | * Note that this function is only relevant when issuing a |
| 315 | * single scatter gather entry. |
| 316 | */ |
| 317 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 318 | { |
| 319 | /* |
| 320 | * FIXME: We don't have a system for the controller to tell |
| 321 | * the core about its problems yet, so for now we just 32-bit |
| 322 | * align the size. |
| 323 | */ |
| 324 | sz = ((sz + 3) / 4) * 4; |
| 325 | |
| 326 | return sz; |
| 327 | } |
| 328 | EXPORT_SYMBOL(mmc_align_data_size); |
| 329 | |
| 330 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 331 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 333 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 335 | * Claim a host for a set of operations. If @abort is non null and |
| 336 | * dereference a non-zero value then this will return prematurely with |
| 337 | * that non-zero value without acquiring the lock. Returns zero |
| 338 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 340 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
| 342 | DECLARE_WAITQUEUE(wait, current); |
| 343 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 344 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 346 | might_sleep(); |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | add_wait_queue(&host->wq, &wait); |
| 349 | spin_lock_irqsave(&host->lock, flags); |
| 350 | while (1) { |
| 351 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 352 | stop = abort ? atomic_read(abort) : 0; |
| 353 | if (stop || !host->claimed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | break; |
| 355 | spin_unlock_irqrestore(&host->lock, flags); |
| 356 | schedule(); |
| 357 | spin_lock_irqsave(&host->lock, flags); |
| 358 | } |
| 359 | set_current_state(TASK_RUNNING); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 360 | if (!stop) |
| 361 | host->claimed = 1; |
| 362 | else |
| 363 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | spin_unlock_irqrestore(&host->lock, flags); |
| 365 | remove_wait_queue(&host->wq, &wait); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 366 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 369 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * mmc_release_host - release a host |
| 373 | * @host: mmc host to release |
| 374 | * |
| 375 | * Release a MMC host, allowing others to claim the host |
| 376 | * for their operations. |
| 377 | */ |
| 378 | void mmc_release_host(struct mmc_host *host) |
| 379 | { |
| 380 | unsigned long flags; |
| 381 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 382 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 385 | host->claimed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | spin_unlock_irqrestore(&host->lock, flags); |
| 387 | |
| 388 | wake_up(&host->wq); |
| 389 | } |
| 390 | |
| 391 | EXPORT_SYMBOL(mmc_release_host); |
| 392 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 393 | /* |
| 394 | * Internal function that does the actual ios call to the host driver, |
| 395 | * optionally printing some debug output. |
| 396 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 397 | static inline void mmc_set_ios(struct mmc_host *host) |
| 398 | { |
| 399 | struct mmc_ios *ios = &host->ios; |
| 400 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 401 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 402 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 403 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 404 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 405 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 406 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 407 | host->ops->set_ios(host, ios); |
| 408 | } |
| 409 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 410 | /* |
| 411 | * Control chip select pin on a host. |
| 412 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 413 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 414 | { |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 415 | host->ios.chip_select = mode; |
| 416 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 420 | * Sets the host clock to the highest possible frequency that |
| 421 | * is below "hz". |
| 422 | */ |
| 423 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 424 | { |
| 425 | WARN_ON(hz < host->f_min); |
| 426 | |
| 427 | if (hz > host->f_max) |
| 428 | hz = host->f_max; |
| 429 | |
| 430 | host->ios.clock = hz; |
| 431 | mmc_set_ios(host); |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * Change the bus mode (open drain/push-pull) of a host. |
| 436 | */ |
| 437 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 438 | { |
| 439 | host->ios.bus_mode = mode; |
| 440 | mmc_set_ios(host); |
| 441 | } |
| 442 | |
| 443 | /* |
| 444 | * Change data bus width of a host. |
| 445 | */ |
| 446 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 447 | { |
| 448 | host->ios.bus_width = width; |
| 449 | mmc_set_ios(host); |
| 450 | } |
| 451 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame^] | 452 | /** |
| 453 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 454 | * @vdd: voltage (mV) |
| 455 | * @low_bits: prefer low bits in boundary cases |
| 456 | * |
| 457 | * This function returns the OCR bit number according to the provided @vdd |
| 458 | * value. If conversion is not possible a negative errno value returned. |
| 459 | * |
| 460 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 461 | * on boundary voltages. For example, |
| 462 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 463 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 464 | * |
| 465 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 466 | */ |
| 467 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 468 | { |
| 469 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 470 | int bit; |
| 471 | |
| 472 | if (vdd < 1650 || vdd > 3600) |
| 473 | return -EINVAL; |
| 474 | |
| 475 | if (vdd >= 1650 && vdd <= 1950) |
| 476 | return ilog2(MMC_VDD_165_195); |
| 477 | |
| 478 | if (low_bits) |
| 479 | vdd -= 1; |
| 480 | |
| 481 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 482 | bit = (vdd - 2000) / 100 + 8; |
| 483 | if (bit > max_bit) |
| 484 | return max_bit; |
| 485 | return bit; |
| 486 | } |
| 487 | |
| 488 | /** |
| 489 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 490 | * @vdd_min: minimum voltage value (mV) |
| 491 | * @vdd_max: maximum voltage value (mV) |
| 492 | * |
| 493 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 494 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 495 | * |
| 496 | * Notes wrt boundary cases: |
| 497 | * This function sets the OCR bits for all boundary voltages, for example |
| 498 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 499 | * MMC_VDD_34_35 mask. |
| 500 | */ |
| 501 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 502 | { |
| 503 | u32 mask = 0; |
| 504 | |
| 505 | if (vdd_max < vdd_min) |
| 506 | return 0; |
| 507 | |
| 508 | /* Prefer high bits for the boundary vdd_max values. */ |
| 509 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 510 | if (vdd_max < 0) |
| 511 | return 0; |
| 512 | |
| 513 | /* Prefer low bits for the boundary vdd_min values. */ |
| 514 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 515 | if (vdd_min < 0) |
| 516 | return 0; |
| 517 | |
| 518 | /* Fill the mask, from max bit to min bit. */ |
| 519 | while (vdd_max >= vdd_min) |
| 520 | mask |= 1 << vdd_max--; |
| 521 | |
| 522 | return mask; |
| 523 | } |
| 524 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 525 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 526 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | * Mask off any voltages we don't support and select |
| 528 | * the lowest voltage |
| 529 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 530 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
| 532 | int bit; |
| 533 | |
| 534 | ocr &= host->ocr_avail; |
| 535 | |
| 536 | bit = ffs(ocr); |
| 537 | if (bit) { |
| 538 | bit -= 1; |
| 539 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 540 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | |
| 542 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 543 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | } else { |
| 545 | ocr = 0; |
| 546 | } |
| 547 | |
| 548 | return ocr; |
| 549 | } |
| 550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 552 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 554 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 556 | host->ios.timing = timing; |
| 557 | mmc_set_ios(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 561 | * Apply power to the MMC stack. This is a two-stage process. |
| 562 | * First, we enable power to the card without the clock running. |
| 563 | * We then wait a bit for the power to stabilise. Finally, |
| 564 | * enable the bus drivers and clock to the card. |
| 565 | * |
| 566 | * We must _NOT_ enable the clock prior to power stablising. |
| 567 | * |
| 568 | * If a host does all the power sequencing itself, ignore the |
| 569 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | */ |
| 571 | static void mmc_power_up(struct mmc_host *host) |
| 572 | { |
| 573 | int bit = fls(host->ocr_avail) - 1; |
| 574 | |
| 575 | host->ios.vdd = bit; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 576 | if (mmc_host_is_spi(host)) { |
| 577 | host->ios.chip_select = MMC_CS_HIGH; |
| 578 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 579 | } else { |
| 580 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 581 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 582 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 584 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 585 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 586 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 588 | /* |
| 589 | * This delay should be sufficient to allow the power supply |
| 590 | * to reach the minimum voltage. |
| 591 | */ |
| 592 | mmc_delay(2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | |
| 594 | host->ios.clock = host->f_min; |
| 595 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 596 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 598 | /* |
| 599 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 600 | * time required to reach a stable voltage. |
| 601 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | mmc_delay(2); |
| 603 | } |
| 604 | |
| 605 | static void mmc_power_off(struct mmc_host *host) |
| 606 | { |
| 607 | host->ios.clock = 0; |
| 608 | host->ios.vdd = 0; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 609 | if (!mmc_host_is_spi(host)) { |
| 610 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 611 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 614 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 615 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 616 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | } |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 620 | * Cleanup when the last reference to the bus operator is dropped. |
| 621 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 622 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 623 | { |
| 624 | BUG_ON(!host); |
| 625 | BUG_ON(host->bus_refs); |
| 626 | BUG_ON(!host->bus_dead); |
| 627 | |
| 628 | host->bus_ops = NULL; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Increase reference count of bus operator |
| 633 | */ |
| 634 | static inline void mmc_bus_get(struct mmc_host *host) |
| 635 | { |
| 636 | unsigned long flags; |
| 637 | |
| 638 | spin_lock_irqsave(&host->lock, flags); |
| 639 | host->bus_refs++; |
| 640 | spin_unlock_irqrestore(&host->lock, flags); |
| 641 | } |
| 642 | |
| 643 | /* |
| 644 | * Decrease reference count of bus operator and free it if |
| 645 | * it is the last reference. |
| 646 | */ |
| 647 | static inline void mmc_bus_put(struct mmc_host *host) |
| 648 | { |
| 649 | unsigned long flags; |
| 650 | |
| 651 | spin_lock_irqsave(&host->lock, flags); |
| 652 | host->bus_refs--; |
| 653 | if ((host->bus_refs == 0) && host->bus_ops) |
| 654 | __mmc_release_bus(host); |
| 655 | spin_unlock_irqrestore(&host->lock, flags); |
| 656 | } |
| 657 | |
| 658 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 659 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 660 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 661 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 662 | 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] | 663 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 664 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 665 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 666 | BUG_ON(!host); |
| 667 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 668 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 669 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 670 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 671 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 672 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 673 | BUG_ON(host->bus_ops); |
| 674 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 675 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 676 | host->bus_ops = ops; |
| 677 | host->bus_refs = 1; |
| 678 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 680 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | } |
| 682 | |
| 683 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 684 | * Remove the current bus handler from a host. Assumes that there are |
| 685 | * no interesting cards left, so the bus is powered down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 687 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 689 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 691 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 693 | WARN_ON(!host->claimed); |
| 694 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 696 | spin_lock_irqsave(&host->lock, flags); |
| 697 | |
| 698 | host->bus_dead = 1; |
| 699 | |
| 700 | spin_unlock_irqrestore(&host->lock, flags); |
| 701 | |
| 702 | mmc_power_off(host); |
| 703 | |
| 704 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
| 706 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | /** |
| 708 | * mmc_detect_change - process change of state on a MMC socket |
| 709 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 710 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 712 | * MMC drivers should call this when they detect a card has been |
| 713 | * inserted or removed. The MMC layer will confirm that any |
| 714 | * present card is still functional, and initialize any newly |
| 715 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 717 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 718 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 719 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 720 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 721 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 722 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 723 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 724 | #endif |
| 725 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 726 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | EXPORT_SYMBOL(mmc_detect_change); |
| 730 | |
| 731 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 732 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 734 | struct mmc_host *host = |
| 735 | container_of(work, struct mmc_host, detect.work); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 736 | u32 ocr; |
| 737 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 739 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 741 | if (host->bus_ops == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 743 | * Only we can add a new handler, so it's safe to |
| 744 | * release the lock here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 745 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 746 | mmc_bus_put(host); |
| 747 | |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 748 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
| 749 | goto out; |
| 750 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 751 | mmc_claim_host(host); |
| 752 | |
| 753 | mmc_power_up(host); |
| 754 | mmc_go_idle(host); |
| 755 | |
| 756 | mmc_send_if_cond(host, host->ocr_avail); |
| 757 | |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 758 | /* |
| 759 | * First we search for SDIO... |
| 760 | */ |
| 761 | err = mmc_send_io_op_cond(host, 0, &ocr); |
| 762 | if (!err) { |
| 763 | if (mmc_attach_sdio(host, ocr)) |
| 764 | mmc_power_off(host); |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 765 | goto out; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 766 | } |
| 767 | |
| 768 | /* |
| 769 | * ...then normal SD... |
| 770 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 771 | err = mmc_send_app_op_cond(host, 0, &ocr); |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 772 | if (!err) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 773 | if (mmc_attach_sd(host, ocr)) |
| 774 | mmc_power_off(host); |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 775 | goto out; |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 776 | } |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 777 | |
| 778 | /* |
| 779 | * ...and finally MMC. |
| 780 | */ |
| 781 | err = mmc_send_op_cond(host, 0, &ocr); |
| 782 | if (!err) { |
| 783 | if (mmc_attach_mmc(host, ocr)) |
| 784 | mmc_power_off(host); |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 785 | goto out; |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | mmc_release_host(host); |
| 789 | mmc_power_off(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 790 | } else { |
| 791 | if (host->bus_ops->detect && !host->bus_dead) |
| 792 | host->bus_ops->detect(host); |
| 793 | |
| 794 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 796 | out: |
| 797 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 798 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 801 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 803 | mmc_power_off(host); |
| 804 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | } |
| 806 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 807 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 809 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 810 | unsigned long flags; |
| 811 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 812 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 813 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 814 | #endif |
| 815 | |
| 816 | mmc_flush_scheduled_work(); |
| 817 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 818 | mmc_bus_get(host); |
| 819 | if (host->bus_ops && !host->bus_dead) { |
| 820 | if (host->bus_ops->remove) |
| 821 | host->bus_ops->remove(host); |
| 822 | |
| 823 | mmc_claim_host(host); |
| 824 | mmc_detach_bus(host); |
| 825 | mmc_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 827 | mmc_bus_put(host); |
| 828 | |
| 829 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | |
| 831 | mmc_power_off(host); |
| 832 | } |
| 833 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | #ifdef CONFIG_PM |
| 835 | |
| 836 | /** |
| 837 | * mmc_suspend_host - suspend a host |
| 838 | * @host: mmc host |
| 839 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 840 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 841 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 842 | { |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 843 | mmc_flush_scheduled_work(); |
| 844 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 845 | mmc_bus_get(host); |
| 846 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 847 | if (host->bus_ops->suspend) |
| 848 | host->bus_ops->suspend(host); |
| 849 | if (!host->bus_ops->resume) { |
| 850 | if (host->bus_ops->remove) |
| 851 | host->bus_ops->remove(host); |
| 852 | |
| 853 | mmc_claim_host(host); |
| 854 | mmc_detach_bus(host); |
| 855 | mmc_release_host(host); |
| 856 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 857 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 858 | mmc_bus_put(host); |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | EXPORT_SYMBOL(mmc_suspend_host); |
| 866 | |
| 867 | /** |
| 868 | * mmc_resume_host - resume a previously suspended host |
| 869 | * @host: mmc host |
| 870 | */ |
| 871 | int mmc_resume_host(struct mmc_host *host) |
| 872 | { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 873 | mmc_bus_get(host); |
| 874 | if (host->bus_ops && !host->bus_dead) { |
| 875 | mmc_power_up(host); |
| 876 | BUG_ON(!host->bus_ops->resume); |
| 877 | host->bus_ops->resume(host); |
| 878 | } |
| 879 | mmc_bus_put(host); |
| 880 | |
| 881 | /* |
| 882 | * We add a slight delay here so that resume can progress |
| 883 | * in parallel. |
| 884 | */ |
| 885 | mmc_detect_change(host, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | EXPORT_SYMBOL(mmc_resume_host); |
| 891 | |
| 892 | #endif |
| 893 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 894 | static int __init mmc_init(void) |
| 895 | { |
| 896 | int ret; |
| 897 | |
| 898 | workqueue = create_singlethread_workqueue("kmmcd"); |
| 899 | if (!workqueue) |
| 900 | return -ENOMEM; |
| 901 | |
| 902 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 903 | if (ret) |
| 904 | goto destroy_workqueue; |
| 905 | |
| 906 | ret = mmc_register_host_class(); |
| 907 | if (ret) |
| 908 | goto unregister_bus; |
| 909 | |
| 910 | ret = sdio_register_bus(); |
| 911 | if (ret) |
| 912 | goto unregister_host_class; |
| 913 | |
| 914 | return 0; |
| 915 | |
| 916 | unregister_host_class: |
| 917 | mmc_unregister_host_class(); |
| 918 | unregister_bus: |
| 919 | mmc_unregister_bus(); |
| 920 | destroy_workqueue: |
| 921 | destroy_workqueue(workqueue); |
| 922 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 923 | return ret; |
| 924 | } |
| 925 | |
| 926 | static void __exit mmc_exit(void) |
| 927 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 928 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 929 | mmc_unregister_host_class(); |
| 930 | mmc_unregister_bus(); |
| 931 | destroy_workqueue(workqueue); |
| 932 | } |
| 933 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 934 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 935 | module_exit(mmc_exit); |
| 936 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | MODULE_LICENSE("GPL"); |