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