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