Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 2 | * linux/drivers/mmc/core/core.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2003-2004 Russell King, All Rights Reserved. |
Pierre Ossman | 5b4fd9a | 2005-09-06 15:18:56 -0700 | [diff] [blame] | 5 | * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved. |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 6 | * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved. |
Philip Langdale | bce40a3 | 2006-10-21 12:35:02 +0200 | [diff] [blame] | 7 | * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License version 2 as |
| 11 | * published by the Free Software Foundation. |
| 12 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/completion.h> |
| 17 | #include <linux/device.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/pagemap.h> |
| 20 | #include <linux/err.h> |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 21 | #include <linux/leds.h> |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 22 | #include <linux/scatterlist.h> |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 23 | #include <linux/log2.h> |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 24 | #include <linux/regulator/consumer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <linux/mmc/card.h> |
| 27 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 28 | #include <linux/mmc/mmc.h> |
| 29 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 31 | #include "core.h" |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 32 | #include "bus.h" |
| 33 | #include "host.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 34 | #include "sdio_bus.h" |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 35 | |
| 36 | #include "mmc_ops.h" |
| 37 | #include "sd_ops.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 38 | #include "sdio_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 40 | static struct workqueue_struct *workqueue; |
| 41 | |
| 42 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 43 | * Enabling software CRCs on the data blocks can be a significant (30%) |
| 44 | * performance cost, and for other reasons may not always be desired. |
| 45 | * So we allow it it to be disabled. |
| 46 | */ |
| 47 | int use_spi_crc = 1; |
| 48 | module_param(use_spi_crc, bool, 0); |
| 49 | |
| 50 | /* |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 51 | * We normally treat cards as removed during suspend if they are not |
| 52 | * known to be on a non-removable bus, to avoid the risk of writing |
| 53 | * back data to a different card after resume. Allow this to be |
| 54 | * overridden if necessary. |
| 55 | */ |
| 56 | #ifdef CONFIG_MMC_UNSAFE_RESUME |
| 57 | int mmc_assume_removable; |
| 58 | #else |
| 59 | int mmc_assume_removable = 1; |
| 60 | #endif |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 61 | EXPORT_SYMBOL(mmc_assume_removable); |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 62 | module_param_named(removable, mmc_assume_removable, bool, 0644); |
| 63 | MODULE_PARM_DESC( |
| 64 | removable, |
| 65 | "MMC/SD cards are removable and may be removed during suspend"); |
| 66 | |
| 67 | /* |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 68 | * Internal function. Schedule delayed work in the MMC work queue. |
| 69 | */ |
| 70 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 71 | unsigned long delay) |
| 72 | { |
| 73 | return queue_delayed_work(workqueue, work, delay); |
| 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Internal function. Flush all scheduled work from the MMC work queue. |
| 78 | */ |
| 79 | static void mmc_flush_scheduled_work(void) |
| 80 | { |
| 81 | flush_workqueue(workqueue); |
| 82 | } |
| 83 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 85 | * mmc_request_done - finish processing an MMC request |
| 86 | * @host: MMC host which completed request |
| 87 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | * |
| 89 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 90 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | */ |
| 92 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 93 | { |
| 94 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 95 | int err = cmd->error; |
| 96 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 97 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 98 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 99 | cmd->retries = 0; |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | if (err && cmd->retries) { |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 103 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 104 | mmc_hostname(host), cmd->opcode, err); |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | cmd->retries--; |
| 107 | cmd->error = 0; |
| 108 | host->ops->request(host, mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 109 | } else { |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 110 | led_trigger_event(host->led, LED_OFF); |
| 111 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 112 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 113 | mmc_hostname(host), cmd->opcode, err, |
| 114 | cmd->resp[0], cmd->resp[1], |
| 115 | cmd->resp[2], cmd->resp[3]); |
| 116 | |
| 117 | if (mrq->data) { |
| 118 | pr_debug("%s: %d bytes transferred: %d\n", |
| 119 | mmc_hostname(host), |
| 120 | mrq->data->bytes_xfered, mrq->data->error); |
| 121 | } |
| 122 | |
| 123 | if (mrq->stop) { |
| 124 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 125 | mmc_hostname(host), mrq->stop->opcode, |
| 126 | mrq->stop->error, |
| 127 | mrq->stop->resp[0], mrq->stop->resp[1], |
| 128 | mrq->stop->resp[2], mrq->stop->resp[3]); |
| 129 | } |
| 130 | |
| 131 | if (mrq->done) |
| 132 | mrq->done(mrq); |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame^] | 133 | |
| 134 | mmc_host_clk_gate(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
| 138 | EXPORT_SYMBOL(mmc_request_done); |
| 139 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 140 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 142 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 143 | #ifdef CONFIG_MMC_DEBUG |
| 144 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 145 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 146 | #endif |
| 147 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 148 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 149 | mmc_hostname(host), mrq->cmd->opcode, |
| 150 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 152 | if (mrq->data) { |
| 153 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 154 | "tsac %d ms nsac %d\n", |
| 155 | mmc_hostname(host), mrq->data->blksz, |
| 156 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 157 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 158 | mrq->data->timeout_clks); |
| 159 | } |
| 160 | |
| 161 | if (mrq->stop) { |
| 162 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 163 | mmc_hostname(host), mrq->stop->opcode, |
| 164 | mrq->stop->arg, mrq->stop->flags); |
| 165 | } |
| 166 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 167 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 169 | led_trigger_event(host->led, LED_FULL); |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | mrq->cmd->error = 0; |
| 172 | mrq->cmd->mrq = mrq; |
| 173 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 174 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 175 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 176 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 177 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 178 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 179 | #ifdef CONFIG_MMC_DEBUG |
| 180 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 181 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 182 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 183 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 184 | #endif |
| 185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | mrq->cmd->data = mrq->data; |
| 187 | mrq->data->error = 0; |
| 188 | mrq->data->mrq = mrq; |
| 189 | if (mrq->stop) { |
| 190 | mrq->data->stop = mrq->stop; |
| 191 | mrq->stop->error = 0; |
| 192 | mrq->stop->mrq = mrq; |
| 193 | } |
| 194 | } |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame^] | 195 | mmc_host_clk_ungate(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | host->ops->request(host, mrq); |
| 197 | } |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | static void mmc_wait_done(struct mmc_request *mrq) |
| 200 | { |
| 201 | complete(mrq->done_data); |
| 202 | } |
| 203 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 204 | /** |
| 205 | * mmc_wait_for_req - start a request and wait for completion |
| 206 | * @host: MMC host to start command |
| 207 | * @mrq: MMC request to start |
| 208 | * |
| 209 | * Start a new MMC custom command request for a host, and wait |
| 210 | * for the command to complete. Does not attempt to parse the |
| 211 | * response. |
| 212 | */ |
| 213 | 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] | 214 | { |
Ingo Molnar | 0afffc7 | 2006-07-03 00:25:35 -0700 | [diff] [blame] | 215 | DECLARE_COMPLETION_ONSTACK(complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | mrq->done_data = &complete; |
| 218 | mrq->done = mmc_wait_done; |
| 219 | |
| 220 | mmc_start_request(host, mrq); |
| 221 | |
| 222 | wait_for_completion(&complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 226 | |
| 227 | /** |
| 228 | * mmc_wait_for_cmd - start a command and wait for completion |
| 229 | * @host: MMC host to start command |
| 230 | * @cmd: MMC command to start |
| 231 | * @retries: maximum number of retries |
| 232 | * |
| 233 | * Start a new MMC command for a host, and wait for the command |
| 234 | * to complete. Return any error that occurred while the command |
| 235 | * was executing. Do not attempt to parse the response. |
| 236 | */ |
| 237 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 238 | { |
| 239 | struct mmc_request mrq; |
| 240 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 241 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
| 243 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 244 | |
| 245 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 246 | cmd->retries = retries; |
| 247 | |
| 248 | mrq.cmd = cmd; |
| 249 | cmd->data = NULL; |
| 250 | |
| 251 | mmc_wait_for_req(host, &mrq); |
| 252 | |
| 253 | return cmd->error; |
| 254 | } |
| 255 | |
| 256 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 257 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 258 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 259 | * mmc_set_data_timeout - set the timeout for a data command |
| 260 | * @data: data phase for command |
| 261 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 262 | * |
| 263 | * Computes the data timeout parameters according to the |
| 264 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 265 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 266 | 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] | 267 | { |
| 268 | unsigned int mult; |
| 269 | |
| 270 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 271 | * SDIO cards only define an upper 1 s limit on access. |
| 272 | */ |
| 273 | if (mmc_card_sdio(card)) { |
| 274 | data->timeout_ns = 1000000000; |
| 275 | data->timeout_clks = 0; |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 280 | * SD cards use a 100 multiplier rather than 10 |
| 281 | */ |
| 282 | mult = mmc_card_sd(card) ? 100 : 10; |
| 283 | |
| 284 | /* |
| 285 | * Scale up the multiplier (and therefore the timeout) by |
| 286 | * the r2w factor for writes. |
| 287 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 288 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 289 | mult <<= card->csd.r2w_factor; |
| 290 | |
| 291 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 292 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 293 | |
| 294 | /* |
| 295 | * SD cards also have an upper limit on the timeout. |
| 296 | */ |
| 297 | if (mmc_card_sd(card)) { |
| 298 | unsigned int timeout_us, limit_us; |
| 299 | |
| 300 | timeout_us = data->timeout_ns / 1000; |
| 301 | timeout_us += data->timeout_clks * 1000 / |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame^] | 302 | (mmc_host_clk_rate(card->host) / 1000); |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 303 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 304 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 305 | /* |
| 306 | * The limit is really 250 ms, but that is |
| 307 | * insufficient for some crappy cards. |
| 308 | */ |
| 309 | limit_us = 300000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 310 | else |
| 311 | limit_us = 100000; |
| 312 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 313 | /* |
| 314 | * SDHC cards always use these fixed values. |
| 315 | */ |
| 316 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 317 | data->timeout_ns = limit_us * 1000; |
| 318 | data->timeout_clks = 0; |
| 319 | } |
| 320 | } |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 321 | /* |
| 322 | * Some cards need very high timeouts if driven in SPI mode. |
| 323 | * The worst observed timeout was 900ms after writing a |
| 324 | * continuous stream of data until the internal logic |
| 325 | * overflowed. |
| 326 | */ |
| 327 | if (mmc_host_is_spi(card->host)) { |
| 328 | if (data->flags & MMC_DATA_WRITE) { |
| 329 | if (data->timeout_ns < 1000000000) |
| 330 | data->timeout_ns = 1000000000; /* 1s */ |
| 331 | } else { |
| 332 | if (data->timeout_ns < 100000000) |
| 333 | data->timeout_ns = 100000000; /* 100ms */ |
| 334 | } |
| 335 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 336 | } |
| 337 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 338 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 340 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 341 | * @card: the MMC card associated with the data transfer |
| 342 | * @sz: original transfer size |
| 343 | * |
| 344 | * Pads the original data size with a number of extra bytes in |
| 345 | * order to avoid controller bugs and/or performance hits |
| 346 | * (e.g. some controllers revert to PIO for certain sizes). |
| 347 | * |
| 348 | * Returns the improved size, which might be unmodified. |
| 349 | * |
| 350 | * Note that this function is only relevant when issuing a |
| 351 | * single scatter gather entry. |
| 352 | */ |
| 353 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 354 | { |
| 355 | /* |
| 356 | * FIXME: We don't have a system for the controller to tell |
| 357 | * the core about its problems yet, so for now we just 32-bit |
| 358 | * align the size. |
| 359 | */ |
| 360 | sz = ((sz + 3) / 4) * 4; |
| 361 | |
| 362 | return sz; |
| 363 | } |
| 364 | EXPORT_SYMBOL(mmc_align_data_size); |
| 365 | |
| 366 | /** |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 367 | * mmc_host_enable - enable a host. |
| 368 | * @host: mmc host to enable |
| 369 | * |
| 370 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 371 | * methods to exit and enter power saving states. For more information |
| 372 | * see comments for struct mmc_host_ops. |
| 373 | */ |
| 374 | int mmc_host_enable(struct mmc_host *host) |
| 375 | { |
| 376 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 377 | return 0; |
| 378 | |
| 379 | if (host->en_dis_recurs) |
| 380 | return 0; |
| 381 | |
| 382 | if (host->nesting_cnt++) |
| 383 | return 0; |
| 384 | |
| 385 | cancel_delayed_work_sync(&host->disable); |
| 386 | |
| 387 | if (host->enabled) |
| 388 | return 0; |
| 389 | |
| 390 | if (host->ops->enable) { |
| 391 | int err; |
| 392 | |
| 393 | host->en_dis_recurs = 1; |
| 394 | err = host->ops->enable(host); |
| 395 | host->en_dis_recurs = 0; |
| 396 | |
| 397 | if (err) { |
| 398 | pr_debug("%s: enable error %d\n", |
| 399 | mmc_hostname(host), err); |
| 400 | return err; |
| 401 | } |
| 402 | } |
| 403 | host->enabled = 1; |
| 404 | return 0; |
| 405 | } |
| 406 | EXPORT_SYMBOL(mmc_host_enable); |
| 407 | |
| 408 | static int mmc_host_do_disable(struct mmc_host *host, int lazy) |
| 409 | { |
| 410 | if (host->ops->disable) { |
| 411 | int err; |
| 412 | |
| 413 | host->en_dis_recurs = 1; |
| 414 | err = host->ops->disable(host, lazy); |
| 415 | host->en_dis_recurs = 0; |
| 416 | |
| 417 | if (err < 0) { |
| 418 | pr_debug("%s: disable error %d\n", |
| 419 | mmc_hostname(host), err); |
| 420 | return err; |
| 421 | } |
| 422 | if (err > 0) { |
| 423 | unsigned long delay = msecs_to_jiffies(err); |
| 424 | |
| 425 | mmc_schedule_delayed_work(&host->disable, delay); |
| 426 | } |
| 427 | } |
| 428 | host->enabled = 0; |
| 429 | return 0; |
| 430 | } |
| 431 | |
| 432 | /** |
| 433 | * mmc_host_disable - disable a host. |
| 434 | * @host: mmc host to disable |
| 435 | * |
| 436 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 437 | * methods to exit and enter power saving states. For more information |
| 438 | * see comments for struct mmc_host_ops. |
| 439 | */ |
| 440 | int mmc_host_disable(struct mmc_host *host) |
| 441 | { |
| 442 | int err; |
| 443 | |
| 444 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 445 | return 0; |
| 446 | |
| 447 | if (host->en_dis_recurs) |
| 448 | return 0; |
| 449 | |
| 450 | if (--host->nesting_cnt) |
| 451 | return 0; |
| 452 | |
| 453 | if (!host->enabled) |
| 454 | return 0; |
| 455 | |
| 456 | err = mmc_host_do_disable(host, 0); |
| 457 | return err; |
| 458 | } |
| 459 | EXPORT_SYMBOL(mmc_host_disable); |
| 460 | |
| 461 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 462 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 464 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 466 | * Claim a host for a set of operations. If @abort is non null and |
| 467 | * dereference a non-zero value then this will return prematurely with |
| 468 | * that non-zero value without acquiring the lock. Returns zero |
| 469 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 471 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { |
| 473 | DECLARE_WAITQUEUE(wait, current); |
| 474 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 475 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 477 | might_sleep(); |
| 478 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | add_wait_queue(&host->wq, &wait); |
| 480 | spin_lock_irqsave(&host->lock, flags); |
| 481 | while (1) { |
| 482 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 483 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 484 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | break; |
| 486 | spin_unlock_irqrestore(&host->lock, flags); |
| 487 | schedule(); |
| 488 | spin_lock_irqsave(&host->lock, flags); |
| 489 | } |
| 490 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 491 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 492 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 493 | host->claimer = current; |
| 494 | host->claim_cnt += 1; |
| 495 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 496 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | spin_unlock_irqrestore(&host->lock, flags); |
| 498 | remove_wait_queue(&host->wq, &wait); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 499 | if (!stop) |
| 500 | mmc_host_enable(host); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 501 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 504 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 506 | /** |
| 507 | * mmc_try_claim_host - try exclusively to claim a host |
| 508 | * @host: mmc host to claim |
| 509 | * |
| 510 | * Returns %1 if the host is claimed, %0 otherwise. |
| 511 | */ |
| 512 | int mmc_try_claim_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 513 | { |
| 514 | int claimed_host = 0; |
| 515 | unsigned long flags; |
| 516 | |
| 517 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 518 | if (!host->claimed || host->claimer == current) { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 519 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 520 | host->claimer = current; |
| 521 | host->claim_cnt += 1; |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 522 | claimed_host = 1; |
| 523 | } |
| 524 | spin_unlock_irqrestore(&host->lock, flags); |
| 525 | return claimed_host; |
| 526 | } |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 527 | EXPORT_SYMBOL(mmc_try_claim_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 528 | |
| 529 | static void mmc_do_release_host(struct mmc_host *host) |
| 530 | { |
| 531 | unsigned long flags; |
| 532 | |
| 533 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 534 | if (--host->claim_cnt) { |
| 535 | /* Release for nested claim */ |
| 536 | spin_unlock_irqrestore(&host->lock, flags); |
| 537 | } else { |
| 538 | host->claimed = 0; |
| 539 | host->claimer = NULL; |
| 540 | spin_unlock_irqrestore(&host->lock, flags); |
| 541 | wake_up(&host->wq); |
| 542 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | void mmc_host_deeper_disable(struct work_struct *work) |
| 546 | { |
| 547 | struct mmc_host *host = |
| 548 | container_of(work, struct mmc_host, disable.work); |
| 549 | |
| 550 | /* If the host is claimed then we do not want to disable it anymore */ |
| 551 | if (!mmc_try_claim_host(host)) |
| 552 | return; |
| 553 | mmc_host_do_disable(host, 1); |
| 554 | mmc_do_release_host(host); |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * mmc_host_lazy_disable - lazily disable a host. |
| 559 | * @host: mmc host to disable |
| 560 | * |
| 561 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 562 | * methods to exit and enter power saving states. For more information |
| 563 | * see comments for struct mmc_host_ops. |
| 564 | */ |
| 565 | int mmc_host_lazy_disable(struct mmc_host *host) |
| 566 | { |
| 567 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 568 | return 0; |
| 569 | |
| 570 | if (host->en_dis_recurs) |
| 571 | return 0; |
| 572 | |
| 573 | if (--host->nesting_cnt) |
| 574 | return 0; |
| 575 | |
| 576 | if (!host->enabled) |
| 577 | return 0; |
| 578 | |
| 579 | if (host->disable_delay) { |
| 580 | mmc_schedule_delayed_work(&host->disable, |
| 581 | msecs_to_jiffies(host->disable_delay)); |
| 582 | return 0; |
| 583 | } else |
| 584 | return mmc_host_do_disable(host, 1); |
| 585 | } |
| 586 | EXPORT_SYMBOL(mmc_host_lazy_disable); |
| 587 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | /** |
| 589 | * mmc_release_host - release a host |
| 590 | * @host: mmc host to release |
| 591 | * |
| 592 | * Release a MMC host, allowing others to claim the host |
| 593 | * for their operations. |
| 594 | */ |
| 595 | void mmc_release_host(struct mmc_host *host) |
| 596 | { |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 597 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 599 | mmc_host_lazy_disable(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 600 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 601 | mmc_do_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | EXPORT_SYMBOL(mmc_release_host); |
| 605 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 606 | /* |
| 607 | * Internal function that does the actual ios call to the host driver, |
| 608 | * optionally printing some debug output. |
| 609 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 610 | static inline void mmc_set_ios(struct mmc_host *host) |
| 611 | { |
| 612 | struct mmc_ios *ios = &host->ios; |
| 613 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 614 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 615 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 616 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 617 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 618 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 619 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame^] | 620 | if (ios->clock > 0) |
| 621 | mmc_set_ungated(host); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 622 | host->ops->set_ios(host, ios); |
| 623 | } |
| 624 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 625 | /* |
| 626 | * Control chip select pin on a host. |
| 627 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 628 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 629 | { |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 630 | host->ios.chip_select = mode; |
| 631 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 635 | * Sets the host clock to the highest possible frequency that |
| 636 | * is below "hz". |
| 637 | */ |
| 638 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 639 | { |
| 640 | WARN_ON(hz < host->f_min); |
| 641 | |
| 642 | if (hz > host->f_max) |
| 643 | hz = host->f_max; |
| 644 | |
| 645 | host->ios.clock = hz; |
| 646 | mmc_set_ios(host); |
| 647 | } |
| 648 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame^] | 649 | #ifdef CONFIG_MMC_CLKGATE |
| 650 | /* |
| 651 | * This gates the clock by setting it to 0 Hz. |
| 652 | */ |
| 653 | void mmc_gate_clock(struct mmc_host *host) |
| 654 | { |
| 655 | unsigned long flags; |
| 656 | |
| 657 | spin_lock_irqsave(&host->clk_lock, flags); |
| 658 | host->clk_old = host->ios.clock; |
| 659 | host->ios.clock = 0; |
| 660 | host->clk_gated = true; |
| 661 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 662 | mmc_set_ios(host); |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * This restores the clock from gating by using the cached |
| 667 | * clock value. |
| 668 | */ |
| 669 | void mmc_ungate_clock(struct mmc_host *host) |
| 670 | { |
| 671 | /* |
| 672 | * We should previously have gated the clock, so the clock shall |
| 673 | * be 0 here! The clock may however be 0 during initialization, |
| 674 | * when some request operations are performed before setting |
| 675 | * the frequency. When ungate is requested in that situation |
| 676 | * we just ignore the call. |
| 677 | */ |
| 678 | if (host->clk_old) { |
| 679 | BUG_ON(host->ios.clock); |
| 680 | /* This call will also set host->clk_gated to false */ |
| 681 | mmc_set_clock(host, host->clk_old); |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | void mmc_set_ungated(struct mmc_host *host) |
| 686 | { |
| 687 | unsigned long flags; |
| 688 | |
| 689 | /* |
| 690 | * We've been given a new frequency while the clock is gated, |
| 691 | * so make sure we regard this as ungating it. |
| 692 | */ |
| 693 | spin_lock_irqsave(&host->clk_lock, flags); |
| 694 | host->clk_gated = false; |
| 695 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 696 | } |
| 697 | |
| 698 | #else |
| 699 | void mmc_set_ungated(struct mmc_host *host) |
| 700 | { |
| 701 | } |
| 702 | #endif |
| 703 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 704 | /* |
| 705 | * Change the bus mode (open drain/push-pull) of a host. |
| 706 | */ |
| 707 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 708 | { |
| 709 | host->ios.bus_mode = mode; |
| 710 | mmc_set_ios(host); |
| 711 | } |
| 712 | |
| 713 | /* |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 714 | * Change data bus width and DDR mode of a host. |
| 715 | */ |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 716 | void mmc_set_bus_width_ddr(struct mmc_host *host, unsigned int width, |
| 717 | unsigned int ddr) |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 718 | { |
| 719 | host->ios.bus_width = width; |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 720 | host->ios.ddr = ddr; |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 721 | mmc_set_ios(host); |
| 722 | } |
| 723 | |
| 724 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 725 | * Change data bus width of a host. |
| 726 | */ |
| 727 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 728 | { |
Adrian Hunter | 49e3b5a | 2010-10-11 12:43:50 +0300 | [diff] [blame] | 729 | mmc_set_bus_width_ddr(host, width, MMC_SDR_MODE); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 730 | } |
| 731 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 732 | /** |
| 733 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 734 | * @vdd: voltage (mV) |
| 735 | * @low_bits: prefer low bits in boundary cases |
| 736 | * |
| 737 | * This function returns the OCR bit number according to the provided @vdd |
| 738 | * value. If conversion is not possible a negative errno value returned. |
| 739 | * |
| 740 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 741 | * on boundary voltages. For example, |
| 742 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 743 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 744 | * |
| 745 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 746 | */ |
| 747 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 748 | { |
| 749 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 750 | int bit; |
| 751 | |
| 752 | if (vdd < 1650 || vdd > 3600) |
| 753 | return -EINVAL; |
| 754 | |
| 755 | if (vdd >= 1650 && vdd <= 1950) |
| 756 | return ilog2(MMC_VDD_165_195); |
| 757 | |
| 758 | if (low_bits) |
| 759 | vdd -= 1; |
| 760 | |
| 761 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 762 | bit = (vdd - 2000) / 100 + 8; |
| 763 | if (bit > max_bit) |
| 764 | return max_bit; |
| 765 | return bit; |
| 766 | } |
| 767 | |
| 768 | /** |
| 769 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 770 | * @vdd_min: minimum voltage value (mV) |
| 771 | * @vdd_max: maximum voltage value (mV) |
| 772 | * |
| 773 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 774 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 775 | * |
| 776 | * Notes wrt boundary cases: |
| 777 | * This function sets the OCR bits for all boundary voltages, for example |
| 778 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 779 | * MMC_VDD_34_35 mask. |
| 780 | */ |
| 781 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 782 | { |
| 783 | u32 mask = 0; |
| 784 | |
| 785 | if (vdd_max < vdd_min) |
| 786 | return 0; |
| 787 | |
| 788 | /* Prefer high bits for the boundary vdd_max values. */ |
| 789 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 790 | if (vdd_max < 0) |
| 791 | return 0; |
| 792 | |
| 793 | /* Prefer low bits for the boundary vdd_min values. */ |
| 794 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 795 | if (vdd_min < 0) |
| 796 | return 0; |
| 797 | |
| 798 | /* Fill the mask, from max bit to min bit. */ |
| 799 | while (vdd_max >= vdd_min) |
| 800 | mask |= 1 << vdd_max--; |
| 801 | |
| 802 | return mask; |
| 803 | } |
| 804 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 805 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 806 | #ifdef CONFIG_REGULATOR |
| 807 | |
| 808 | /** |
| 809 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 810 | * @supply: regulator to use |
| 811 | * |
| 812 | * This returns either a negative errno, or a mask of voltages that |
| 813 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 814 | * regulator. This would normally be called before registering the |
| 815 | * MMC host adapter. |
| 816 | */ |
| 817 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 818 | { |
| 819 | int result = 0; |
| 820 | int count; |
| 821 | int i; |
| 822 | |
| 823 | count = regulator_count_voltages(supply); |
| 824 | if (count < 0) |
| 825 | return count; |
| 826 | |
| 827 | for (i = 0; i < count; i++) { |
| 828 | int vdd_uV; |
| 829 | int vdd_mV; |
| 830 | |
| 831 | vdd_uV = regulator_list_voltage(supply, i); |
| 832 | if (vdd_uV <= 0) |
| 833 | continue; |
| 834 | |
| 835 | vdd_mV = vdd_uV / 1000; |
| 836 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 837 | } |
| 838 | |
| 839 | return result; |
| 840 | } |
| 841 | EXPORT_SYMBOL(mmc_regulator_get_ocrmask); |
| 842 | |
| 843 | /** |
| 844 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 845 | * @mmc: the host to regulate |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 846 | * @supply: regulator to use |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 847 | * @vdd_bit: zero for power off, else a bit number (host->ios.vdd) |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 848 | * |
| 849 | * Returns zero on success, else negative errno. |
| 850 | * |
| 851 | * MMC host drivers may use this to enable or disable a regulator using |
| 852 | * a particular supply voltage. This would normally be called from the |
| 853 | * set_ios() method. |
| 854 | */ |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 855 | int mmc_regulator_set_ocr(struct mmc_host *mmc, |
| 856 | struct regulator *supply, |
| 857 | unsigned short vdd_bit) |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 858 | { |
| 859 | int result = 0; |
| 860 | int min_uV, max_uV; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 861 | |
| 862 | if (vdd_bit) { |
| 863 | int tmp; |
| 864 | int voltage; |
| 865 | |
| 866 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 867 | * bits this regulator doesn't quite support ... don't |
| 868 | * be too picky, most cards and regulators are OK with |
| 869 | * a 0.1V range goof (it's a small error percentage). |
| 870 | */ |
| 871 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 872 | if (tmp == 0) { |
| 873 | min_uV = 1650 * 1000; |
| 874 | max_uV = 1950 * 1000; |
| 875 | } else { |
| 876 | min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 877 | max_uV = min_uV + 100 * 1000; |
| 878 | } |
| 879 | |
| 880 | /* avoid needless changes to this voltage; the regulator |
| 881 | * might not allow this operation |
| 882 | */ |
| 883 | voltage = regulator_get_voltage(supply); |
| 884 | if (voltage < 0) |
| 885 | result = voltage; |
| 886 | else if (voltage < min_uV || voltage > max_uV) |
| 887 | result = regulator_set_voltage(supply, min_uV, max_uV); |
| 888 | else |
| 889 | result = 0; |
| 890 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 891 | if (result == 0 && !mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 892 | result = regulator_enable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 893 | if (!result) |
| 894 | mmc->regulator_enabled = true; |
| 895 | } |
| 896 | } else if (mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 897 | result = regulator_disable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 898 | if (result == 0) |
| 899 | mmc->regulator_enabled = false; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 900 | } |
| 901 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 902 | if (result) |
| 903 | dev_err(mmc_dev(mmc), |
| 904 | "could not set regulator OCR (%d)\n", result); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 905 | return result; |
| 906 | } |
| 907 | EXPORT_SYMBOL(mmc_regulator_set_ocr); |
| 908 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 909 | #endif /* CONFIG_REGULATOR */ |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 910 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 911 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | * Mask off any voltages we don't support and select |
| 913 | * the lowest voltage |
| 914 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 915 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
| 917 | int bit; |
| 918 | |
| 919 | ocr &= host->ocr_avail; |
| 920 | |
| 921 | bit = ffs(ocr); |
| 922 | if (bit) { |
| 923 | bit -= 1; |
| 924 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 925 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
| 927 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 928 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | } else { |
David Brownell | f6e10b8 | 2008-12-31 09:50:30 -0800 | [diff] [blame] | 930 | pr_warning("%s: host doesn't support card's voltages\n", |
| 931 | mmc_hostname(host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | ocr = 0; |
| 933 | } |
| 934 | |
| 935 | return ocr; |
| 936 | } |
| 937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 939 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 941 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 943 | host->ios.timing = timing; |
| 944 | mmc_set_ios(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 948 | * Apply power to the MMC stack. This is a two-stage process. |
| 949 | * First, we enable power to the card without the clock running. |
| 950 | * We then wait a bit for the power to stabilise. Finally, |
| 951 | * enable the bus drivers and clock to the card. |
| 952 | * |
| 953 | * We must _NOT_ enable the clock prior to power stablising. |
| 954 | * |
| 955 | * If a host does all the power sequencing itself, ignore the |
| 956 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | */ |
| 958 | static void mmc_power_up(struct mmc_host *host) |
| 959 | { |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 960 | int bit; |
| 961 | |
| 962 | /* If ocr is set, we use it */ |
| 963 | if (host->ocr) |
| 964 | bit = ffs(host->ocr) - 1; |
| 965 | else |
| 966 | bit = fls(host->ocr_avail) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | |
| 968 | host->ios.vdd = bit; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 969 | if (mmc_host_is_spi(host)) { |
| 970 | host->ios.chip_select = MMC_CS_HIGH; |
| 971 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 972 | } else { |
| 973 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 974 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 975 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 977 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 978 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 979 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 981 | /* |
| 982 | * This delay should be sufficient to allow the power supply |
| 983 | * to reach the minimum voltage. |
| 984 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 985 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 987 | host->ios.clock = host->f_init; |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 988 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 989 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 990 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 992 | /* |
| 993 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 994 | * time required to reach a stable voltage. |
| 995 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 996 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | static void mmc_power_off(struct mmc_host *host) |
| 1000 | { |
| 1001 | host->ios.clock = 0; |
| 1002 | host->ios.vdd = 0; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1003 | if (!mmc_host_is_spi(host)) { |
| 1004 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 1005 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 1006 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 1008 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1009 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1010 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1014 | * Cleanup when the last reference to the bus operator is dropped. |
| 1015 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 1016 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1017 | { |
| 1018 | BUG_ON(!host); |
| 1019 | BUG_ON(host->bus_refs); |
| 1020 | BUG_ON(!host->bus_dead); |
| 1021 | |
| 1022 | host->bus_ops = NULL; |
| 1023 | } |
| 1024 | |
| 1025 | /* |
| 1026 | * Increase reference count of bus operator |
| 1027 | */ |
| 1028 | static inline void mmc_bus_get(struct mmc_host *host) |
| 1029 | { |
| 1030 | unsigned long flags; |
| 1031 | |
| 1032 | spin_lock_irqsave(&host->lock, flags); |
| 1033 | host->bus_refs++; |
| 1034 | spin_unlock_irqrestore(&host->lock, flags); |
| 1035 | } |
| 1036 | |
| 1037 | /* |
| 1038 | * Decrease reference count of bus operator and free it if |
| 1039 | * it is the last reference. |
| 1040 | */ |
| 1041 | static inline void mmc_bus_put(struct mmc_host *host) |
| 1042 | { |
| 1043 | unsigned long flags; |
| 1044 | |
| 1045 | spin_lock_irqsave(&host->lock, flags); |
| 1046 | host->bus_refs--; |
| 1047 | if ((host->bus_refs == 0) && host->bus_ops) |
| 1048 | __mmc_release_bus(host); |
| 1049 | spin_unlock_irqrestore(&host->lock, flags); |
| 1050 | } |
| 1051 | |
| 1052 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1053 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 1054 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1056 | 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] | 1057 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1058 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1060 | BUG_ON(!host); |
| 1061 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1062 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1063 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1064 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1065 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1066 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1067 | BUG_ON(host->bus_ops); |
| 1068 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1069 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1070 | host->bus_ops = ops; |
| 1071 | host->bus_refs = 1; |
| 1072 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1074 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | } |
| 1076 | |
| 1077 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1078 | * Remove the current bus handler from a host. Assumes that there are |
| 1079 | * no interesting cards left, so the bus is powered down. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1081 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1083 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1085 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1087 | WARN_ON(!host->claimed); |
| 1088 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1089 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1090 | spin_lock_irqsave(&host->lock, flags); |
| 1091 | |
| 1092 | host->bus_dead = 1; |
| 1093 | |
| 1094 | spin_unlock_irqrestore(&host->lock, flags); |
| 1095 | |
| 1096 | mmc_power_off(host); |
| 1097 | |
| 1098 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | /** |
| 1102 | * mmc_detect_change - process change of state on a MMC socket |
| 1103 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1104 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1106 | * MMC drivers should call this when they detect a card has been |
| 1107 | * inserted or removed. The MMC layer will confirm that any |
| 1108 | * present card is still functional, and initialize any newly |
| 1109 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1111 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1113 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1114 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1115 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1116 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1117 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1118 | #endif |
| 1119 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1120 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | EXPORT_SYMBOL(mmc_detect_change); |
| 1124 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1125 | void mmc_init_erase(struct mmc_card *card) |
| 1126 | { |
| 1127 | unsigned int sz; |
| 1128 | |
| 1129 | if (is_power_of_2(card->erase_size)) |
| 1130 | card->erase_shift = ffs(card->erase_size) - 1; |
| 1131 | else |
| 1132 | card->erase_shift = 0; |
| 1133 | |
| 1134 | /* |
| 1135 | * It is possible to erase an arbitrarily large area of an SD or MMC |
| 1136 | * card. That is not desirable because it can take a long time |
| 1137 | * (minutes) potentially delaying more important I/O, and also the |
| 1138 | * timeout calculations become increasingly hugely over-estimated. |
| 1139 | * Consequently, 'pref_erase' is defined as a guide to limit erases |
| 1140 | * to that size and alignment. |
| 1141 | * |
| 1142 | * For SD cards that define Allocation Unit size, limit erases to one |
| 1143 | * Allocation Unit at a time. For MMC cards that define High Capacity |
| 1144 | * Erase Size, whether it is switched on or not, limit to that size. |
| 1145 | * Otherwise just have a stab at a good value. For modern cards it |
| 1146 | * will end up being 4MiB. Note that if the value is too small, it |
| 1147 | * can end up taking longer to erase. |
| 1148 | */ |
| 1149 | if (mmc_card_sd(card) && card->ssr.au) { |
| 1150 | card->pref_erase = card->ssr.au; |
| 1151 | card->erase_shift = ffs(card->ssr.au) - 1; |
| 1152 | } else if (card->ext_csd.hc_erase_size) { |
| 1153 | card->pref_erase = card->ext_csd.hc_erase_size; |
| 1154 | } else { |
| 1155 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 1156 | if (sz < 128) |
| 1157 | card->pref_erase = 512 * 1024 / 512; |
| 1158 | else if (sz < 512) |
| 1159 | card->pref_erase = 1024 * 1024 / 512; |
| 1160 | else if (sz < 1024) |
| 1161 | card->pref_erase = 2 * 1024 * 1024 / 512; |
| 1162 | else |
| 1163 | card->pref_erase = 4 * 1024 * 1024 / 512; |
| 1164 | if (card->pref_erase < card->erase_size) |
| 1165 | card->pref_erase = card->erase_size; |
| 1166 | else { |
| 1167 | sz = card->pref_erase % card->erase_size; |
| 1168 | if (sz) |
| 1169 | card->pref_erase += card->erase_size - sz; |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | static void mmc_set_mmc_erase_timeout(struct mmc_card *card, |
| 1175 | struct mmc_command *cmd, |
| 1176 | unsigned int arg, unsigned int qty) |
| 1177 | { |
| 1178 | unsigned int erase_timeout; |
| 1179 | |
| 1180 | if (card->ext_csd.erase_group_def & 1) { |
| 1181 | /* High Capacity Erase Group Size uses HC timeouts */ |
| 1182 | if (arg == MMC_TRIM_ARG) |
| 1183 | erase_timeout = card->ext_csd.trim_timeout; |
| 1184 | else |
| 1185 | erase_timeout = card->ext_csd.hc_erase_timeout; |
| 1186 | } else { |
| 1187 | /* CSD Erase Group Size uses write timeout */ |
| 1188 | unsigned int mult = (10 << card->csd.r2w_factor); |
| 1189 | unsigned int timeout_clks = card->csd.tacc_clks * mult; |
| 1190 | unsigned int timeout_us; |
| 1191 | |
| 1192 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ |
| 1193 | if (card->csd.tacc_ns < 1000000) |
| 1194 | timeout_us = (card->csd.tacc_ns * mult) / 1000; |
| 1195 | else |
| 1196 | timeout_us = (card->csd.tacc_ns / 1000) * mult; |
| 1197 | |
| 1198 | /* |
| 1199 | * ios.clock is only a target. The real clock rate might be |
| 1200 | * less but not that much less, so fudge it by multiplying by 2. |
| 1201 | */ |
| 1202 | timeout_clks <<= 1; |
| 1203 | timeout_us += (timeout_clks * 1000) / |
| 1204 | (card->host->ios.clock / 1000); |
| 1205 | |
| 1206 | erase_timeout = timeout_us / 1000; |
| 1207 | |
| 1208 | /* |
| 1209 | * Theoretically, the calculation could underflow so round up |
| 1210 | * to 1ms in that case. |
| 1211 | */ |
| 1212 | if (!erase_timeout) |
| 1213 | erase_timeout = 1; |
| 1214 | } |
| 1215 | |
| 1216 | /* Multiplier for secure operations */ |
| 1217 | if (arg & MMC_SECURE_ARGS) { |
| 1218 | if (arg == MMC_SECURE_ERASE_ARG) |
| 1219 | erase_timeout *= card->ext_csd.sec_erase_mult; |
| 1220 | else |
| 1221 | erase_timeout *= card->ext_csd.sec_trim_mult; |
| 1222 | } |
| 1223 | |
| 1224 | erase_timeout *= qty; |
| 1225 | |
| 1226 | /* |
| 1227 | * Ensure at least a 1 second timeout for SPI as per |
| 1228 | * 'mmc_set_data_timeout()' |
| 1229 | */ |
| 1230 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 1231 | erase_timeout = 1000; |
| 1232 | |
| 1233 | cmd->erase_timeout = erase_timeout; |
| 1234 | } |
| 1235 | |
| 1236 | static void mmc_set_sd_erase_timeout(struct mmc_card *card, |
| 1237 | struct mmc_command *cmd, unsigned int arg, |
| 1238 | unsigned int qty) |
| 1239 | { |
| 1240 | if (card->ssr.erase_timeout) { |
| 1241 | /* Erase timeout specified in SD Status Register (SSR) */ |
| 1242 | cmd->erase_timeout = card->ssr.erase_timeout * qty + |
| 1243 | card->ssr.erase_offset; |
| 1244 | } else { |
| 1245 | /* |
| 1246 | * Erase timeout not specified in SD Status Register (SSR) so |
| 1247 | * use 250ms per write block. |
| 1248 | */ |
| 1249 | cmd->erase_timeout = 250 * qty; |
| 1250 | } |
| 1251 | |
| 1252 | /* Must not be less than 1 second */ |
| 1253 | if (cmd->erase_timeout < 1000) |
| 1254 | cmd->erase_timeout = 1000; |
| 1255 | } |
| 1256 | |
| 1257 | static void mmc_set_erase_timeout(struct mmc_card *card, |
| 1258 | struct mmc_command *cmd, unsigned int arg, |
| 1259 | unsigned int qty) |
| 1260 | { |
| 1261 | if (mmc_card_sd(card)) |
| 1262 | mmc_set_sd_erase_timeout(card, cmd, arg, qty); |
| 1263 | else |
| 1264 | mmc_set_mmc_erase_timeout(card, cmd, arg, qty); |
| 1265 | } |
| 1266 | |
| 1267 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, |
| 1268 | unsigned int to, unsigned int arg) |
| 1269 | { |
| 1270 | struct mmc_command cmd; |
| 1271 | unsigned int qty = 0; |
| 1272 | int err; |
| 1273 | |
| 1274 | /* |
| 1275 | * qty is used to calculate the erase timeout which depends on how many |
| 1276 | * erase groups (or allocation units in SD terminology) are affected. |
| 1277 | * We count erasing part of an erase group as one erase group. |
| 1278 | * For SD, the allocation units are always a power of 2. For MMC, the |
| 1279 | * erase group size is almost certainly also power of 2, but it does not |
| 1280 | * seem to insist on that in the JEDEC standard, so we fall back to |
| 1281 | * division in that case. SD may not specify an allocation unit size, |
| 1282 | * in which case the timeout is based on the number of write blocks. |
| 1283 | * |
| 1284 | * Note that the timeout for secure trim 2 will only be correct if the |
| 1285 | * number of erase groups specified is the same as the total of all |
| 1286 | * preceding secure trim 1 commands. Since the power may have been |
| 1287 | * lost since the secure trim 1 commands occurred, it is generally |
| 1288 | * impossible to calculate the secure trim 2 timeout correctly. |
| 1289 | */ |
| 1290 | if (card->erase_shift) |
| 1291 | qty += ((to >> card->erase_shift) - |
| 1292 | (from >> card->erase_shift)) + 1; |
| 1293 | else if (mmc_card_sd(card)) |
| 1294 | qty += to - from + 1; |
| 1295 | else |
| 1296 | qty += ((to / card->erase_size) - |
| 1297 | (from / card->erase_size)) + 1; |
| 1298 | |
| 1299 | if (!mmc_card_blockaddr(card)) { |
| 1300 | from <<= 9; |
| 1301 | to <<= 9; |
| 1302 | } |
| 1303 | |
| 1304 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1305 | if (mmc_card_sd(card)) |
| 1306 | cmd.opcode = SD_ERASE_WR_BLK_START; |
| 1307 | else |
| 1308 | cmd.opcode = MMC_ERASE_GROUP_START; |
| 1309 | cmd.arg = from; |
| 1310 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1311 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1312 | if (err) { |
| 1313 | printk(KERN_ERR "mmc_erase: group start error %d, " |
| 1314 | "status %#x\n", err, cmd.resp[0]); |
| 1315 | err = -EINVAL; |
| 1316 | goto out; |
| 1317 | } |
| 1318 | |
| 1319 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1320 | if (mmc_card_sd(card)) |
| 1321 | cmd.opcode = SD_ERASE_WR_BLK_END; |
| 1322 | else |
| 1323 | cmd.opcode = MMC_ERASE_GROUP_END; |
| 1324 | cmd.arg = to; |
| 1325 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1326 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1327 | if (err) { |
| 1328 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", |
| 1329 | err, cmd.resp[0]); |
| 1330 | err = -EINVAL; |
| 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1335 | cmd.opcode = MMC_ERASE; |
| 1336 | cmd.arg = arg; |
| 1337 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1338 | mmc_set_erase_timeout(card, &cmd, arg, qty); |
| 1339 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1340 | if (err) { |
| 1341 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", |
| 1342 | err, cmd.resp[0]); |
| 1343 | err = -EIO; |
| 1344 | goto out; |
| 1345 | } |
| 1346 | |
| 1347 | if (mmc_host_is_spi(card->host)) |
| 1348 | goto out; |
| 1349 | |
| 1350 | do { |
| 1351 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1352 | cmd.opcode = MMC_SEND_STATUS; |
| 1353 | cmd.arg = card->rca << 16; |
| 1354 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1355 | /* Do not retry else we can't see errors */ |
| 1356 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1357 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
| 1358 | printk(KERN_ERR "error %d requesting status %#x\n", |
| 1359 | err, cmd.resp[0]); |
| 1360 | err = -EIO; |
| 1361 | goto out; |
| 1362 | } |
| 1363 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 1364 | R1_CURRENT_STATE(cmd.resp[0]) == 7); |
| 1365 | out: |
| 1366 | return err; |
| 1367 | } |
| 1368 | |
| 1369 | /** |
| 1370 | * mmc_erase - erase sectors. |
| 1371 | * @card: card to erase |
| 1372 | * @from: first sector to erase |
| 1373 | * @nr: number of sectors to erase |
| 1374 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
| 1375 | * |
| 1376 | * Caller must claim host before calling this function. |
| 1377 | */ |
| 1378 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, |
| 1379 | unsigned int arg) |
| 1380 | { |
| 1381 | unsigned int rem, to = from + nr; |
| 1382 | |
| 1383 | if (!(card->host->caps & MMC_CAP_ERASE) || |
| 1384 | !(card->csd.cmdclass & CCC_ERASE)) |
| 1385 | return -EOPNOTSUPP; |
| 1386 | |
| 1387 | if (!card->erase_size) |
| 1388 | return -EOPNOTSUPP; |
| 1389 | |
| 1390 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
| 1391 | return -EOPNOTSUPP; |
| 1392 | |
| 1393 | if ((arg & MMC_SECURE_ARGS) && |
| 1394 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 1395 | return -EOPNOTSUPP; |
| 1396 | |
| 1397 | if ((arg & MMC_TRIM_ARGS) && |
| 1398 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 1399 | return -EOPNOTSUPP; |
| 1400 | |
| 1401 | if (arg == MMC_SECURE_ERASE_ARG) { |
| 1402 | if (from % card->erase_size || nr % card->erase_size) |
| 1403 | return -EINVAL; |
| 1404 | } |
| 1405 | |
| 1406 | if (arg == MMC_ERASE_ARG) { |
| 1407 | rem = from % card->erase_size; |
| 1408 | if (rem) { |
| 1409 | rem = card->erase_size - rem; |
| 1410 | from += rem; |
| 1411 | if (nr > rem) |
| 1412 | nr -= rem; |
| 1413 | else |
| 1414 | return 0; |
| 1415 | } |
| 1416 | rem = nr % card->erase_size; |
| 1417 | if (rem) |
| 1418 | nr -= rem; |
| 1419 | } |
| 1420 | |
| 1421 | if (nr == 0) |
| 1422 | return 0; |
| 1423 | |
| 1424 | to = from + nr; |
| 1425 | |
| 1426 | if (to <= from) |
| 1427 | return -EINVAL; |
| 1428 | |
| 1429 | /* 'from' and 'to' are inclusive */ |
| 1430 | to -= 1; |
| 1431 | |
| 1432 | return mmc_do_erase(card, from, to, arg); |
| 1433 | } |
| 1434 | EXPORT_SYMBOL(mmc_erase); |
| 1435 | |
| 1436 | int mmc_can_erase(struct mmc_card *card) |
| 1437 | { |
| 1438 | if ((card->host->caps & MMC_CAP_ERASE) && |
| 1439 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
| 1440 | return 1; |
| 1441 | return 0; |
| 1442 | } |
| 1443 | EXPORT_SYMBOL(mmc_can_erase); |
| 1444 | |
| 1445 | int mmc_can_trim(struct mmc_card *card) |
| 1446 | { |
| 1447 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) |
| 1448 | return 1; |
| 1449 | return 0; |
| 1450 | } |
| 1451 | EXPORT_SYMBOL(mmc_can_trim); |
| 1452 | |
| 1453 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
| 1454 | { |
| 1455 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) |
| 1456 | return 1; |
| 1457 | return 0; |
| 1458 | } |
| 1459 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); |
| 1460 | |
| 1461 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, |
| 1462 | unsigned int nr) |
| 1463 | { |
| 1464 | if (!card->erase_size) |
| 1465 | return 0; |
| 1466 | if (from % card->erase_size || nr % card->erase_size) |
| 1467 | return 0; |
| 1468 | return 1; |
| 1469 | } |
| 1470 | EXPORT_SYMBOL(mmc_erase_group_aligned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1472 | int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) |
| 1473 | { |
| 1474 | struct mmc_command cmd; |
| 1475 | |
| 1476 | if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card)) |
| 1477 | return 0; |
| 1478 | |
| 1479 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1480 | cmd.opcode = MMC_SET_BLOCKLEN; |
| 1481 | cmd.arg = blocklen; |
| 1482 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1483 | return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 1484 | } |
| 1485 | EXPORT_SYMBOL(mmc_set_blocklen); |
| 1486 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1487 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1489 | struct mmc_host *host = |
| 1490 | container_of(work, struct mmc_host, detect.work); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1491 | u32 ocr; |
| 1492 | int err; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1493 | unsigned long flags; |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1494 | int i; |
| 1495 | const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1496 | |
| 1497 | spin_lock_irqsave(&host->lock, flags); |
| 1498 | |
| 1499 | if (host->rescan_disable) { |
| 1500 | spin_unlock_irqrestore(&host->lock, flags); |
| 1501 | return; |
| 1502 | } |
| 1503 | |
| 1504 | spin_unlock_irqrestore(&host->lock, flags); |
| 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1507 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1509 | /* if there is a card registered, check whether it is still present */ |
| 1510 | if ((host->bus_ops != NULL) && host->bus_ops->detect && !host->bus_dead) |
| 1511 | host->bus_ops->detect(host); |
| 1512 | |
| 1513 | mmc_bus_put(host); |
| 1514 | |
| 1515 | |
| 1516 | mmc_bus_get(host); |
| 1517 | |
| 1518 | /* if there still is a card present, stop here */ |
| 1519 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1520 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1521 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1523 | |
| 1524 | /* detect a newly inserted card */ |
| 1525 | |
| 1526 | /* |
| 1527 | * Only we can add a new handler, so it's safe to |
| 1528 | * release the lock here. |
| 1529 | */ |
| 1530 | mmc_bus_put(host); |
| 1531 | |
| 1532 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
| 1533 | goto out; |
| 1534 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1535 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
| 1536 | mmc_claim_host(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1537 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1538 | if (freqs[i] >= host->f_min) |
| 1539 | host->f_init = freqs[i]; |
| 1540 | else if (!i || freqs[i-1] > host->f_min) |
| 1541 | host->f_init = host->f_min; |
| 1542 | else { |
| 1543 | mmc_release_host(host); |
| 1544 | goto out; |
| 1545 | } |
Eric Bénard | 4d0b861 | 2010-10-12 09:29:37 +0200 | [diff] [blame] | 1546 | #ifdef CONFIG_MMC_DEBUG |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1547 | pr_info("%s: %s: trying to init card at %u Hz\n", |
| 1548 | mmc_hostname(host), __func__, host->f_init); |
Eric Bénard | 4d0b861 | 2010-10-12 09:29:37 +0200 | [diff] [blame] | 1549 | #endif |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1550 | mmc_power_up(host); |
| 1551 | sdio_reset(host); |
| 1552 | mmc_go_idle(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1553 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1554 | mmc_send_if_cond(host, host->ocr_avail); |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1555 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1556 | /* |
| 1557 | * First we search for SDIO... |
| 1558 | */ |
| 1559 | err = mmc_send_io_op_cond(host, 0, &ocr); |
| 1560 | if (!err) { |
| 1561 | if (mmc_attach_sdio(host, ocr)) { |
| 1562 | mmc_claim_host(host); |
| 1563 | /* |
| 1564 | * Try SDMEM (but not MMC) even if SDIO |
| 1565 | * is broken. |
| 1566 | */ |
| 1567 | if (mmc_send_app_op_cond(host, 0, &ocr)) |
| 1568 | goto out_fail; |
| 1569 | |
| 1570 | if (mmc_attach_sd(host, ocr)) |
| 1571 | mmc_power_off(host); |
| 1572 | } |
| 1573 | goto out; |
| 1574 | } |
| 1575 | |
| 1576 | /* |
| 1577 | * ...then normal SD... |
| 1578 | */ |
| 1579 | err = mmc_send_app_op_cond(host, 0, &ocr); |
| 1580 | if (!err) { |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1581 | if (mmc_attach_sd(host, ocr)) |
| 1582 | mmc_power_off(host); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1583 | goto out; |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1584 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1585 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1586 | /* |
| 1587 | * ...and finally MMC. |
| 1588 | */ |
| 1589 | err = mmc_send_op_cond(host, 0, &ocr); |
| 1590 | if (!err) { |
| 1591 | if (mmc_attach_mmc(host, ocr)) |
| 1592 | mmc_power_off(host); |
| 1593 | goto out; |
| 1594 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1595 | |
Michal Miroslaw | 7310ece8 | 2010-08-10 18:01:40 -0700 | [diff] [blame] | 1596 | out_fail: |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1597 | mmc_release_host(host); |
| 1598 | mmc_power_off(host); |
| 1599 | } |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 1600 | out: |
| 1601 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 1602 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1603 | } |
| 1604 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1605 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1606 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1607 | mmc_power_off(host); |
| 1608 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1611 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1613 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1614 | unsigned long flags; |
| 1615 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1616 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1617 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1618 | #endif |
| 1619 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1620 | if (host->caps & MMC_CAP_DISABLE) |
| 1621 | cancel_delayed_work(&host->disable); |
Guennadi Liakhovetski | d9bcbf3 | 2010-11-11 17:32:25 +0100 | [diff] [blame] | 1622 | cancel_delayed_work_sync(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1623 | mmc_flush_scheduled_work(); |
| 1624 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1625 | /* clear pm flags now and let card drivers set them as needed */ |
| 1626 | host->pm_flags = 0; |
| 1627 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1628 | mmc_bus_get(host); |
| 1629 | if (host->bus_ops && !host->bus_dead) { |
| 1630 | if (host->bus_ops->remove) |
| 1631 | host->bus_ops->remove(host); |
| 1632 | |
| 1633 | mmc_claim_host(host); |
| 1634 | mmc_detach_bus(host); |
| 1635 | mmc_release_host(host); |
Denis Karpov | 53509f0 | 2009-09-22 16:44:36 -0700 | [diff] [blame] | 1636 | mmc_bus_put(host); |
| 1637 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1639 | mmc_bus_put(host); |
| 1640 | |
| 1641 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
| 1643 | mmc_power_off(host); |
| 1644 | } |
| 1645 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1646 | int mmc_power_save_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1647 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1648 | int ret = 0; |
| 1649 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1650 | mmc_bus_get(host); |
| 1651 | |
| 1652 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1653 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1654 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1655 | } |
| 1656 | |
| 1657 | if (host->bus_ops->power_save) |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1658 | ret = host->bus_ops->power_save(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1659 | |
| 1660 | mmc_bus_put(host); |
| 1661 | |
| 1662 | mmc_power_off(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1663 | |
| 1664 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1665 | } |
| 1666 | EXPORT_SYMBOL(mmc_power_save_host); |
| 1667 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1668 | int mmc_power_restore_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1669 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1670 | int ret; |
| 1671 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1672 | mmc_bus_get(host); |
| 1673 | |
| 1674 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1675 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1676 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1677 | } |
| 1678 | |
| 1679 | mmc_power_up(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1680 | ret = host->bus_ops->power_restore(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1681 | |
| 1682 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1683 | |
| 1684 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1685 | } |
| 1686 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 1687 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1688 | int mmc_card_awake(struct mmc_host *host) |
| 1689 | { |
| 1690 | int err = -ENOSYS; |
| 1691 | |
| 1692 | mmc_bus_get(host); |
| 1693 | |
| 1694 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1695 | err = host->bus_ops->awake(host); |
| 1696 | |
| 1697 | mmc_bus_put(host); |
| 1698 | |
| 1699 | return err; |
| 1700 | } |
| 1701 | EXPORT_SYMBOL(mmc_card_awake); |
| 1702 | |
| 1703 | int mmc_card_sleep(struct mmc_host *host) |
| 1704 | { |
| 1705 | int err = -ENOSYS; |
| 1706 | |
| 1707 | mmc_bus_get(host); |
| 1708 | |
| 1709 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1710 | err = host->bus_ops->sleep(host); |
| 1711 | |
| 1712 | mmc_bus_put(host); |
| 1713 | |
| 1714 | return err; |
| 1715 | } |
| 1716 | EXPORT_SYMBOL(mmc_card_sleep); |
| 1717 | |
| 1718 | int mmc_card_can_sleep(struct mmc_host *host) |
| 1719 | { |
| 1720 | struct mmc_card *card = host->card; |
| 1721 | |
| 1722 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) |
| 1723 | return 1; |
| 1724 | return 0; |
| 1725 | } |
| 1726 | EXPORT_SYMBOL(mmc_card_can_sleep); |
| 1727 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | #ifdef CONFIG_PM |
| 1729 | |
| 1730 | /** |
| 1731 | * mmc_suspend_host - suspend a host |
| 1732 | * @host: mmc host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | */ |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 1734 | int mmc_suspend_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1736 | int err = 0; |
| 1737 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1738 | if (host->caps & MMC_CAP_DISABLE) |
| 1739 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 1740 | cancel_delayed_work(&host->detect); |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1741 | mmc_flush_scheduled_work(); |
| 1742 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1743 | mmc_bus_get(host); |
| 1744 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1745 | if (host->bus_ops->suspend) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1746 | err = host->bus_ops->suspend(host); |
Ohad Ben-Cohen | 1c8cf9c | 2010-10-13 09:31:56 +0200 | [diff] [blame] | 1747 | if (err == -ENOSYS || !host->bus_ops->resume) { |
| 1748 | /* |
| 1749 | * We simply "remove" the card in this case. |
| 1750 | * It will be redetected on resume. |
| 1751 | */ |
| 1752 | if (host->bus_ops->remove) |
| 1753 | host->bus_ops->remove(host); |
| 1754 | mmc_claim_host(host); |
| 1755 | mmc_detach_bus(host); |
| 1756 | mmc_release_host(host); |
| 1757 | host->pm_flags = 0; |
| 1758 | err = 0; |
| 1759 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 1760 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1761 | mmc_bus_put(host); |
| 1762 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1763 | if (!err && !(host->pm_flags & MMC_PM_KEEP_POWER)) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1764 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1766 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | EXPORT_SYMBOL(mmc_suspend_host); |
| 1770 | |
| 1771 | /** |
| 1772 | * mmc_resume_host - resume a previously suspended host |
| 1773 | * @host: mmc host |
| 1774 | */ |
| 1775 | int mmc_resume_host(struct mmc_host *host) |
| 1776 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1777 | int err = 0; |
| 1778 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1779 | mmc_bus_get(host); |
| 1780 | if (host->bus_ops && !host->bus_dead) { |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1781 | if (!(host->pm_flags & MMC_PM_KEEP_POWER)) { |
| 1782 | mmc_power_up(host); |
| 1783 | mmc_select_voltage(host, host->ocr); |
| 1784 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1785 | BUG_ON(!host->bus_ops->resume); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1786 | err = host->bus_ops->resume(host); |
| 1787 | if (err) { |
| 1788 | printk(KERN_WARNING "%s: error %d during resume " |
| 1789 | "(card was removed?)\n", |
| 1790 | mmc_hostname(host), err); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1791 | err = 0; |
| 1792 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 1793 | } |
| 1794 | mmc_bus_put(host); |
| 1795 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 1796 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | EXPORT_SYMBOL(mmc_resume_host); |
| 1799 | |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1800 | /* Do the card removal on suspend if card is assumed removeable |
| 1801 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
| 1802 | to sync the card. |
| 1803 | */ |
| 1804 | int mmc_pm_notify(struct notifier_block *notify_block, |
| 1805 | unsigned long mode, void *unused) |
| 1806 | { |
| 1807 | struct mmc_host *host = container_of( |
| 1808 | notify_block, struct mmc_host, pm_notify); |
| 1809 | unsigned long flags; |
| 1810 | |
| 1811 | |
| 1812 | switch (mode) { |
| 1813 | case PM_HIBERNATION_PREPARE: |
| 1814 | case PM_SUSPEND_PREPARE: |
| 1815 | |
| 1816 | spin_lock_irqsave(&host->lock, flags); |
| 1817 | host->rescan_disable = 1; |
| 1818 | spin_unlock_irqrestore(&host->lock, flags); |
| 1819 | cancel_delayed_work_sync(&host->detect); |
| 1820 | |
| 1821 | if (!host->bus_ops || host->bus_ops->suspend) |
| 1822 | break; |
| 1823 | |
| 1824 | mmc_claim_host(host); |
| 1825 | |
| 1826 | if (host->bus_ops->remove) |
| 1827 | host->bus_ops->remove(host); |
| 1828 | |
| 1829 | mmc_detach_bus(host); |
| 1830 | mmc_release_host(host); |
| 1831 | host->pm_flags = 0; |
| 1832 | break; |
| 1833 | |
| 1834 | case PM_POST_SUSPEND: |
| 1835 | case PM_POST_HIBERNATION: |
Takashi Iwai | 274476f | 2010-12-10 08:40:31 +0100 | [diff] [blame] | 1836 | case PM_POST_RESTORE: |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1837 | |
| 1838 | spin_lock_irqsave(&host->lock, flags); |
| 1839 | host->rescan_disable = 0; |
| 1840 | spin_unlock_irqrestore(&host->lock, flags); |
| 1841 | mmc_detect_change(host, 0); |
| 1842 | |
| 1843 | } |
| 1844 | |
| 1845 | return 0; |
| 1846 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1847 | #endif |
| 1848 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1849 | static int __init mmc_init(void) |
| 1850 | { |
| 1851 | int ret; |
| 1852 | |
Tejun Heo | 0d9ee5b | 2010-12-24 16:00:17 +0100 | [diff] [blame] | 1853 | workqueue = alloc_ordered_workqueue("kmmcd", 0); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1854 | if (!workqueue) |
| 1855 | return -ENOMEM; |
| 1856 | |
| 1857 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1858 | if (ret) |
| 1859 | goto destroy_workqueue; |
| 1860 | |
| 1861 | ret = mmc_register_host_class(); |
| 1862 | if (ret) |
| 1863 | goto unregister_bus; |
| 1864 | |
| 1865 | ret = sdio_register_bus(); |
| 1866 | if (ret) |
| 1867 | goto unregister_host_class; |
| 1868 | |
| 1869 | return 0; |
| 1870 | |
| 1871 | unregister_host_class: |
| 1872 | mmc_unregister_host_class(); |
| 1873 | unregister_bus: |
| 1874 | mmc_unregister_bus(); |
| 1875 | destroy_workqueue: |
| 1876 | destroy_workqueue(workqueue); |
| 1877 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1878 | return ret; |
| 1879 | } |
| 1880 | |
| 1881 | static void __exit mmc_exit(void) |
| 1882 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 1883 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1884 | mmc_unregister_host_class(); |
| 1885 | mmc_unregister_bus(); |
| 1886 | destroy_workqueue(workqueue); |
| 1887 | } |
| 1888 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 1889 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 1890 | module_exit(mmc_exit); |
| 1891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | MODULE_LICENSE("GPL"); |