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