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 | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 21 | #include <asm/scatterlist.h> |
| 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 | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 32 | |
| 33 | #include "mmc_ops.h" |
| 34 | #include "sd_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 36 | extern int mmc_attach_mmc(struct mmc_host *host, u32 ocr); |
| 37 | extern int mmc_attach_sd(struct mmc_host *host, u32 ocr); |
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 | /* |
| 42 | * Internal function. Schedule delayed work in the MMC work queue. |
| 43 | */ |
| 44 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 45 | unsigned long delay) |
| 46 | { |
| 47 | return queue_delayed_work(workqueue, work, delay); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Internal function. Flush all scheduled work from the MMC work queue. |
| 52 | */ |
| 53 | static void mmc_flush_scheduled_work(void) |
| 54 | { |
| 55 | flush_workqueue(workqueue); |
| 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 59 | * mmc_request_done - finish processing an MMC request |
| 60 | * @host: MMC host which completed request |
| 61 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | * |
| 63 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 64 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | */ |
| 66 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 67 | { |
| 68 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 69 | int err = cmd->error; |
| 70 | |
| 71 | pr_debug("%s: req done (CMD%u): %d/%d/%d: %08x %08x %08x %08x\n", |
| 72 | mmc_hostname(host), cmd->opcode, err, |
| 73 | mrq->data ? mrq->data->error : 0, |
| 74 | mrq->stop ? mrq->stop->error : 0, |
| 75 | cmd->resp[0], cmd->resp[1], cmd->resp[2], cmd->resp[3]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
| 77 | if (err && cmd->retries) { |
| 78 | cmd->retries--; |
| 79 | cmd->error = 0; |
| 80 | host->ops->request(host, mrq); |
| 81 | } else if (mrq->done) { |
| 82 | mrq->done(mrq); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | EXPORT_SYMBOL(mmc_request_done); |
| 87 | |
| 88 | /** |
| 89 | * mmc_start_request - start a command on a host |
| 90 | * @host: MMC host to start command on |
| 91 | * @mrq: MMC request to start |
| 92 | * |
| 93 | * Queue a command on the specified host. We expect the |
| 94 | * caller to be holding the host lock with interrupts disabled. |
| 95 | */ |
| 96 | void |
| 97 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 98 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 99 | #ifdef CONFIG_MMC_DEBUG |
| 100 | unsigned int i, sz; |
| 101 | #endif |
| 102 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 103 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 104 | mmc_hostname(host), mrq->cmd->opcode, |
| 105 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 107 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
| 109 | mrq->cmd->error = 0; |
| 110 | mrq->cmd->mrq = mrq; |
| 111 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 112 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 113 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 114 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 115 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 116 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 117 | #ifdef CONFIG_MMC_DEBUG |
| 118 | sz = 0; |
| 119 | for (i = 0;i < mrq->data->sg_len;i++) |
| 120 | sz += mrq->data->sg[i].length; |
| 121 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 122 | #endif |
| 123 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | mrq->cmd->data = mrq->data; |
| 125 | mrq->data->error = 0; |
| 126 | mrq->data->mrq = mrq; |
| 127 | if (mrq->stop) { |
| 128 | mrq->data->stop = mrq->stop; |
| 129 | mrq->stop->error = 0; |
| 130 | mrq->stop->mrq = mrq; |
| 131 | } |
| 132 | } |
| 133 | host->ops->request(host, mrq); |
| 134 | } |
| 135 | |
| 136 | EXPORT_SYMBOL(mmc_start_request); |
| 137 | |
| 138 | static void mmc_wait_done(struct mmc_request *mrq) |
| 139 | { |
| 140 | complete(mrq->done_data); |
| 141 | } |
| 142 | |
| 143 | int mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq) |
| 144 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 145 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
| 147 | mrq->done_data = &complete; |
| 148 | mrq->done = mmc_wait_done; |
| 149 | |
| 150 | mmc_start_request(host, mrq); |
| 151 | |
| 152 | wait_for_completion(&complete); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 158 | |
| 159 | /** |
| 160 | * mmc_wait_for_cmd - start a command and wait for completion |
| 161 | * @host: MMC host to start command |
| 162 | * @cmd: MMC command to start |
| 163 | * @retries: maximum number of retries |
| 164 | * |
| 165 | * Start a new MMC command for a host, and wait for the command |
| 166 | * to complete. Return any error that occurred while the command |
| 167 | * was executing. Do not attempt to parse the response. |
| 168 | */ |
| 169 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 170 | { |
| 171 | struct mmc_request mrq; |
| 172 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 173 | BUG_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | |
| 175 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 176 | |
| 177 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 178 | cmd->retries = retries; |
| 179 | |
| 180 | mrq.cmd = cmd; |
| 181 | cmd->data = NULL; |
| 182 | |
| 183 | mmc_wait_for_req(host, &mrq); |
| 184 | |
| 185 | return cmd->error; |
| 186 | } |
| 187 | |
| 188 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 189 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 190 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 191 | * mmc_set_data_timeout - set the timeout for a data command |
| 192 | * @data: data phase for command |
| 193 | * @card: the MMC card associated with the data transfer |
| 194 | * @write: flag to differentiate reads from writes |
| 195 | */ |
| 196 | void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card, |
| 197 | int write) |
| 198 | { |
| 199 | unsigned int mult; |
| 200 | |
| 201 | /* |
| 202 | * SD cards use a 100 multiplier rather than 10 |
| 203 | */ |
| 204 | mult = mmc_card_sd(card) ? 100 : 10; |
| 205 | |
| 206 | /* |
| 207 | * Scale up the multiplier (and therefore the timeout) by |
| 208 | * the r2w factor for writes. |
| 209 | */ |
| 210 | if (write) |
| 211 | mult <<= card->csd.r2w_factor; |
| 212 | |
| 213 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 214 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 215 | |
| 216 | /* |
| 217 | * SD cards also have an upper limit on the timeout. |
| 218 | */ |
| 219 | if (mmc_card_sd(card)) { |
| 220 | unsigned int timeout_us, limit_us; |
| 221 | |
| 222 | timeout_us = data->timeout_ns / 1000; |
| 223 | timeout_us += data->timeout_clks * 1000 / |
| 224 | (card->host->ios.clock / 1000); |
| 225 | |
| 226 | if (write) |
| 227 | limit_us = 250000; |
| 228 | else |
| 229 | limit_us = 100000; |
| 230 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 231 | /* |
| 232 | * SDHC cards always use these fixed values. |
| 233 | */ |
| 234 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 235 | data->timeout_ns = limit_us * 1000; |
| 236 | data->timeout_clks = 0; |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | /** |
| 243 | * __mmc_claim_host - exclusively claim a host |
| 244 | * @host: mmc host to claim |
| 245 | * @card: mmc card to claim host for |
| 246 | * |
| 247 | * Claim a host for a set of operations. If a valid card |
| 248 | * is passed and this wasn't the last card selected, select |
| 249 | * the card before returning. |
| 250 | * |
| 251 | * Note: you should use mmc_card_claim_host or mmc_claim_host. |
| 252 | */ |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 253 | void mmc_claim_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
| 255 | DECLARE_WAITQUEUE(wait, current); |
| 256 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
| 258 | add_wait_queue(&host->wq, &wait); |
| 259 | spin_lock_irqsave(&host->lock, flags); |
| 260 | while (1) { |
| 261 | set_current_state(TASK_UNINTERRUPTIBLE); |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 262 | if (!host->claimed) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | break; |
| 264 | spin_unlock_irqrestore(&host->lock, flags); |
| 265 | schedule(); |
| 266 | spin_lock_irqsave(&host->lock, flags); |
| 267 | } |
| 268 | set_current_state(TASK_RUNNING); |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 269 | host->claimed = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | spin_unlock_irqrestore(&host->lock, flags); |
| 271 | remove_wait_queue(&host->wq, &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 274 | EXPORT_SYMBOL(mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
| 276 | /** |
| 277 | * mmc_release_host - release a host |
| 278 | * @host: mmc host to release |
| 279 | * |
| 280 | * Release a MMC host, allowing others to claim the host |
| 281 | * for their operations. |
| 282 | */ |
| 283 | void mmc_release_host(struct mmc_host *host) |
| 284 | { |
| 285 | unsigned long flags; |
| 286 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 287 | BUG_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | |
| 289 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 290 | host->claimed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | spin_unlock_irqrestore(&host->lock, flags); |
| 292 | |
| 293 | wake_up(&host->wq); |
| 294 | } |
| 295 | |
| 296 | EXPORT_SYMBOL(mmc_release_host); |
| 297 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 298 | /* |
| 299 | * Internal function that does the actual ios call to the host driver, |
| 300 | * optionally printing some debug output. |
| 301 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 302 | static inline void mmc_set_ios(struct mmc_host *host) |
| 303 | { |
| 304 | struct mmc_ios *ios = &host->ios; |
| 305 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 306 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 307 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 308 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 309 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 310 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 311 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 312 | host->ops->set_ios(host, ios); |
| 313 | } |
| 314 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 315 | /* |
| 316 | * Control chip select pin on a host. |
| 317 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 318 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 319 | { |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 320 | host->ios.chip_select = mode; |
| 321 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 325 | * Sets the host clock to the highest possible frequency that |
| 326 | * is below "hz". |
| 327 | */ |
| 328 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 329 | { |
| 330 | WARN_ON(hz < host->f_min); |
| 331 | |
| 332 | if (hz > host->f_max) |
| 333 | hz = host->f_max; |
| 334 | |
| 335 | host->ios.clock = hz; |
| 336 | mmc_set_ios(host); |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Change the bus mode (open drain/push-pull) of a host. |
| 341 | */ |
| 342 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 343 | { |
| 344 | host->ios.bus_mode = mode; |
| 345 | mmc_set_ios(host); |
| 346 | } |
| 347 | |
| 348 | /* |
| 349 | * Change data bus width of a host. |
| 350 | */ |
| 351 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 352 | { |
| 353 | host->ios.bus_width = width; |
| 354 | mmc_set_ios(host); |
| 355 | } |
| 356 | |
| 357 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | * Mask off any voltages we don't support and select |
| 359 | * the lowest voltage |
| 360 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 361 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | { |
| 363 | int bit; |
| 364 | |
| 365 | ocr &= host->ocr_avail; |
| 366 | |
| 367 | bit = ffs(ocr); |
| 368 | if (bit) { |
| 369 | bit -= 1; |
| 370 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 371 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | |
| 373 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 374 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | } else { |
| 376 | ocr = 0; |
| 377 | } |
| 378 | |
| 379 | return ocr; |
| 380 | } |
| 381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 383 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 385 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 387 | host->ios.timing = timing; |
| 388 | mmc_set_ios(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 392 | * Apply power to the MMC stack. This is a two-stage process. |
| 393 | * First, we enable power to the card without the clock running. |
| 394 | * We then wait a bit for the power to stabilise. Finally, |
| 395 | * enable the bus drivers and clock to the card. |
| 396 | * |
| 397 | * We must _NOT_ enable the clock prior to power stablising. |
| 398 | * |
| 399 | * If a host does all the power sequencing itself, ignore the |
| 400 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | */ |
| 402 | static void mmc_power_up(struct mmc_host *host) |
| 403 | { |
| 404 | int bit = fls(host->ocr_avail) - 1; |
| 405 | |
| 406 | host->ios.vdd = bit; |
| 407 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 408 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 410 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 411 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 412 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
| 414 | mmc_delay(1); |
| 415 | |
| 416 | host->ios.clock = host->f_min; |
| 417 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 418 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | mmc_delay(2); |
| 421 | } |
| 422 | |
| 423 | static void mmc_power_off(struct mmc_host *host) |
| 424 | { |
| 425 | host->ios.clock = 0; |
| 426 | host->ios.vdd = 0; |
| 427 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
Pierre Ossman | 865e9f1 | 2005-09-03 16:45:02 +0100 | [diff] [blame] | 428 | host->ios.chip_select = MMC_CS_DONTCARE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 430 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 431 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 432 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 436 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 437 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 439 | 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] | 440 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 441 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 443 | BUG_ON(!host); |
| 444 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 445 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 446 | BUG_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 447 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 448 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 449 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 450 | BUG_ON(host->bus_ops); |
| 451 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 452 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 453 | host->bus_ops = ops; |
| 454 | host->bus_refs = 1; |
| 455 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 457 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 461 | * Remove the current bus handler from a host. Assumes that there are |
| 462 | * no interesting cards left, so the bus is powered down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 464 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 466 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 468 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 470 | BUG_ON(!host->claimed); |
| 471 | BUG_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 473 | spin_lock_irqsave(&host->lock, flags); |
| 474 | |
| 475 | host->bus_dead = 1; |
| 476 | |
| 477 | spin_unlock_irqrestore(&host->lock, flags); |
| 478 | |
| 479 | mmc_power_off(host); |
| 480 | |
| 481 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 484 | /* |
| 485 | * Cleanup when the last reference to the bus operator is dropped. |
| 486 | */ |
| 487 | void __mmc_release_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 489 | BUG_ON(!host); |
| 490 | BUG_ON(host->bus_refs); |
| 491 | BUG_ON(!host->bus_dead); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 493 | host->bus_ops = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /** |
| 497 | * mmc_detect_change - process change of state on a MMC socket |
| 498 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 499 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | * |
| 501 | * All we know is that card(s) have been inserted or removed |
| 502 | * from the socket(s). We don't know which socket or cards. |
| 503 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 504 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 506 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 507 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 508 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 509 | BUG_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 510 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 511 | #endif |
| 512 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 513 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | EXPORT_SYMBOL(mmc_detect_change); |
| 517 | |
| 518 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 519 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 521 | struct mmc_host *host = |
| 522 | container_of(work, struct mmc_host, detect.work); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 523 | u32 ocr; |
| 524 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 526 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 528 | if (host->bus_ops == NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 530 | * Only we can add a new handler, so it's safe to |
| 531 | * release the lock here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 533 | mmc_bus_put(host); |
| 534 | |
| 535 | mmc_claim_host(host); |
| 536 | |
| 537 | mmc_power_up(host); |
| 538 | mmc_go_idle(host); |
| 539 | |
| 540 | mmc_send_if_cond(host, host->ocr_avail); |
| 541 | |
| 542 | err = mmc_send_app_op_cond(host, 0, &ocr); |
| 543 | if (err == MMC_ERR_NONE) { |
| 544 | if (mmc_attach_sd(host, ocr)) |
| 545 | mmc_power_off(host); |
| 546 | } else { |
| 547 | /* |
| 548 | * If we fail to detect any SD cards then try |
| 549 | * searching for MMC cards. |
| 550 | */ |
| 551 | err = mmc_send_op_cond(host, 0, &ocr); |
| 552 | if (err == MMC_ERR_NONE) { |
| 553 | if (mmc_attach_mmc(host, ocr)) |
| 554 | mmc_power_off(host); |
| 555 | } else { |
| 556 | mmc_power_off(host); |
| 557 | mmc_release_host(host); |
| 558 | } |
| 559 | } |
| 560 | } else { |
| 561 | if (host->bus_ops->detect && !host->bus_dead) |
| 562 | host->bus_ops->detect(host); |
| 563 | |
| 564 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 568 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 570 | mmc_power_off(host); |
| 571 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | } |
| 573 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 574 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 576 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 577 | unsigned long flags; |
| 578 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 579 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 580 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 581 | #endif |
| 582 | |
| 583 | mmc_flush_scheduled_work(); |
| 584 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 585 | mmc_bus_get(host); |
| 586 | if (host->bus_ops && !host->bus_dead) { |
| 587 | if (host->bus_ops->remove) |
| 588 | host->bus_ops->remove(host); |
| 589 | |
| 590 | mmc_claim_host(host); |
| 591 | mmc_detach_bus(host); |
| 592 | mmc_release_host(host); |
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 | mmc_bus_put(host); |
| 595 | |
| 596 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
| 598 | mmc_power_off(host); |
| 599 | } |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | #ifdef CONFIG_PM |
| 602 | |
| 603 | /** |
| 604 | * mmc_suspend_host - suspend a host |
| 605 | * @host: mmc host |
| 606 | * @state: suspend mode (PM_SUSPEND_xxx) |
| 607 | */ |
Pavel Machek | e5378ca | 2005-04-16 15:25:29 -0700 | [diff] [blame] | 608 | int mmc_suspend_host(struct mmc_host *host, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 610 | mmc_flush_scheduled_work(); |
| 611 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 612 | mmc_bus_get(host); |
| 613 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 614 | if (host->bus_ops->suspend) |
| 615 | host->bus_ops->suspend(host); |
| 616 | if (!host->bus_ops->resume) { |
| 617 | if (host->bus_ops->remove) |
| 618 | host->bus_ops->remove(host); |
| 619 | |
| 620 | mmc_claim_host(host); |
| 621 | mmc_detach_bus(host); |
| 622 | mmc_release_host(host); |
| 623 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 624 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 625 | mmc_bus_put(host); |
| 626 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | EXPORT_SYMBOL(mmc_suspend_host); |
| 633 | |
| 634 | /** |
| 635 | * mmc_resume_host - resume a previously suspended host |
| 636 | * @host: mmc host |
| 637 | */ |
| 638 | int mmc_resume_host(struct mmc_host *host) |
| 639 | { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 640 | mmc_bus_get(host); |
| 641 | if (host->bus_ops && !host->bus_dead) { |
| 642 | mmc_power_up(host); |
| 643 | BUG_ON(!host->bus_ops->resume); |
| 644 | host->bus_ops->resume(host); |
| 645 | } |
| 646 | mmc_bus_put(host); |
| 647 | |
| 648 | /* |
| 649 | * We add a slight delay here so that resume can progress |
| 650 | * in parallel. |
| 651 | */ |
| 652 | mmc_detect_change(host, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | EXPORT_SYMBOL(mmc_resume_host); |
| 658 | |
| 659 | #endif |
| 660 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 661 | static int __init mmc_init(void) |
| 662 | { |
| 663 | int ret; |
| 664 | |
| 665 | workqueue = create_singlethread_workqueue("kmmcd"); |
| 666 | if (!workqueue) |
| 667 | return -ENOMEM; |
| 668 | |
| 669 | ret = mmc_register_bus(); |
| 670 | if (ret == 0) { |
| 671 | ret = mmc_register_host_class(); |
| 672 | if (ret) |
| 673 | mmc_unregister_bus(); |
| 674 | } |
| 675 | return ret; |
| 676 | } |
| 677 | |
| 678 | static void __exit mmc_exit(void) |
| 679 | { |
| 680 | mmc_unregister_host_class(); |
| 681 | mmc_unregister_bus(); |
| 682 | destroy_workqueue(workqueue); |
| 683 | } |
| 684 | |
| 685 | module_init(mmc_init); |
| 686 | module_exit(mmc_exit); |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | MODULE_LICENSE("GPL"); |