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> |
Amerigo Wang | 35eb6db | 2011-07-25 17:13:11 -0700 | [diff] [blame] | 26 | #include <linux/suspend.h> |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 27 | #include <linux/fault-inject.h> |
| 28 | #include <linux/random.h> |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 29 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | #include <linux/mmc/card.h> |
| 32 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 33 | #include <linux/mmc/mmc.h> |
| 34 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 36 | #include "core.h" |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 37 | #include "bus.h" |
| 38 | #include "host.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 39 | #include "sdio_bus.h" |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 40 | |
| 41 | #include "mmc_ops.h" |
| 42 | #include "sd_ops.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 43 | #include "sdio_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 45 | /* |
| 46 | * Background operations can take a long time, depending on the housekeeping |
| 47 | * operations the card has to perform. |
| 48 | */ |
| 49 | #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */ |
| 50 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 51 | static struct workqueue_struct *workqueue; |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 52 | static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 53 | |
| 54 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 55 | * Enabling software CRCs on the data blocks can be a significant (30%) |
| 56 | * performance cost, and for other reasons may not always be desired. |
| 57 | * So we allow it it to be disabled. |
| 58 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 59 | bool use_spi_crc = 1; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 60 | module_param(use_spi_crc, bool, 0); |
| 61 | |
| 62 | /* |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 63 | * We normally treat cards as removed during suspend if they are not |
| 64 | * known to be on a non-removable bus, to avoid the risk of writing |
| 65 | * back data to a different card after resume. Allow this to be |
| 66 | * overridden if necessary. |
| 67 | */ |
| 68 | #ifdef CONFIG_MMC_UNSAFE_RESUME |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 69 | bool mmc_assume_removable; |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 70 | #else |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 71 | bool mmc_assume_removable = 1; |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 72 | #endif |
Matt Fleming | 71d7d3d | 2010-09-27 09:42:19 +0100 | [diff] [blame] | 73 | EXPORT_SYMBOL(mmc_assume_removable); |
Ben Hutchings | bd68e08 | 2009-12-14 18:01:29 -0800 | [diff] [blame] | 74 | module_param_named(removable, mmc_assume_removable, bool, 0644); |
| 75 | MODULE_PARM_DESC( |
| 76 | removable, |
| 77 | "MMC/SD cards are removable and may be removed during suspend"); |
| 78 | |
| 79 | /* |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 80 | * Internal function. Schedule delayed work in the MMC work queue. |
| 81 | */ |
| 82 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 83 | unsigned long delay) |
| 84 | { |
| 85 | return queue_delayed_work(workqueue, work, delay); |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Internal function. Flush all scheduled work from the MMC work queue. |
| 90 | */ |
| 91 | static void mmc_flush_scheduled_work(void) |
| 92 | { |
| 93 | flush_workqueue(workqueue); |
| 94 | } |
| 95 | |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_FAIL_MMC_REQUEST |
| 97 | |
| 98 | /* |
| 99 | * Internal function. Inject random data errors. |
| 100 | * If mmc_data is NULL no errors are injected. |
| 101 | */ |
| 102 | static void mmc_should_fail_request(struct mmc_host *host, |
| 103 | struct mmc_request *mrq) |
| 104 | { |
| 105 | struct mmc_command *cmd = mrq->cmd; |
| 106 | struct mmc_data *data = mrq->data; |
| 107 | static const int data_errors[] = { |
| 108 | -ETIMEDOUT, |
| 109 | -EILSEQ, |
| 110 | -EIO, |
| 111 | }; |
| 112 | |
| 113 | if (!data) |
| 114 | return; |
| 115 | |
| 116 | if (cmd->error || data->error || |
| 117 | !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) |
| 118 | return; |
| 119 | |
| 120 | data->error = data_errors[random32() % ARRAY_SIZE(data_errors)]; |
| 121 | data->bytes_xfered = (random32() % (data->bytes_xfered >> 9)) << 9; |
| 122 | } |
| 123 | |
| 124 | #else /* CONFIG_FAIL_MMC_REQUEST */ |
| 125 | |
| 126 | static inline void mmc_should_fail_request(struct mmc_host *host, |
| 127 | struct mmc_request *mrq) |
| 128 | { |
| 129 | } |
| 130 | |
| 131 | #endif /* CONFIG_FAIL_MMC_REQUEST */ |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 134 | * mmc_request_done - finish processing an MMC request |
| 135 | * @host: MMC host which completed request |
| 136 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * |
| 138 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 139 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | */ |
| 141 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 142 | { |
| 143 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 144 | int err = cmd->error; |
| 145 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 146 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 147 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 148 | cmd->retries = 0; |
| 149 | } |
| 150 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 151 | if (err && cmd->retries && !mmc_card_removed(host->card)) { |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 152 | /* |
| 153 | * Request starter must handle retries - see |
| 154 | * mmc_wait_for_req_done(). |
| 155 | */ |
| 156 | if (mrq->done) |
| 157 | mrq->done(mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 158 | } else { |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 159 | mmc_should_fail_request(host, mrq); |
| 160 | |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 161 | led_trigger_event(host->led, LED_OFF); |
| 162 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 163 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 164 | mmc_hostname(host), cmd->opcode, err, |
| 165 | cmd->resp[0], cmd->resp[1], |
| 166 | cmd->resp[2], cmd->resp[3]); |
| 167 | |
| 168 | if (mrq->data) { |
| 169 | pr_debug("%s: %d bytes transferred: %d\n", |
| 170 | mmc_hostname(host), |
| 171 | mrq->data->bytes_xfered, mrq->data->error); |
| 172 | } |
| 173 | |
| 174 | if (mrq->stop) { |
| 175 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 176 | mmc_hostname(host), mrq->stop->opcode, |
| 177 | mrq->stop->error, |
| 178 | mrq->stop->resp[0], mrq->stop->resp[1], |
| 179 | mrq->stop->resp[2], mrq->stop->resp[3]); |
| 180 | } |
| 181 | |
| 182 | if (mrq->done) |
| 183 | mrq->done(mrq); |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 184 | |
Mika Westerberg | 08c1407 | 2011-08-18 15:23:47 +0300 | [diff] [blame] | 185 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
| 189 | EXPORT_SYMBOL(mmc_request_done); |
| 190 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 191 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 193 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 194 | #ifdef CONFIG_MMC_DEBUG |
| 195 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 196 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 197 | #endif |
| 198 | |
Jaehoon Chung | 7b2fd4f | 2012-02-07 14:13:10 +0900 | [diff] [blame] | 199 | if (mrq->sbc) { |
| 200 | pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n", |
| 201 | mmc_hostname(host), mrq->sbc->opcode, |
| 202 | mrq->sbc->arg, mrq->sbc->flags); |
| 203 | } |
| 204 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 205 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 206 | mmc_hostname(host), mrq->cmd->opcode, |
| 207 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 209 | if (mrq->data) { |
| 210 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 211 | "tsac %d ms nsac %d\n", |
| 212 | mmc_hostname(host), mrq->data->blksz, |
| 213 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 214 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 215 | mrq->data->timeout_clks); |
| 216 | } |
| 217 | |
| 218 | if (mrq->stop) { |
| 219 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 220 | mmc_hostname(host), mrq->stop->opcode, |
| 221 | mrq->stop->arg, mrq->stop->flags); |
| 222 | } |
| 223 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 224 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | |
| 226 | mrq->cmd->error = 0; |
| 227 | mrq->cmd->mrq = mrq; |
| 228 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 229 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 230 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 231 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 232 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 233 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 234 | #ifdef CONFIG_MMC_DEBUG |
| 235 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 236 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 237 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 238 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 239 | #endif |
| 240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | mrq->cmd->data = mrq->data; |
| 242 | mrq->data->error = 0; |
| 243 | mrq->data->mrq = mrq; |
| 244 | if (mrq->stop) { |
| 245 | mrq->data->stop = mrq->stop; |
| 246 | mrq->stop->error = 0; |
| 247 | mrq->stop->mrq = mrq; |
| 248 | } |
| 249 | } |
Mika Westerberg | 08c1407 | 2011-08-18 15:23:47 +0300 | [diff] [blame] | 250 | mmc_host_clk_hold(host); |
Pierre Tardy | 66c036e | 2011-02-06 19:02:48 +0100 | [diff] [blame] | 251 | led_trigger_event(host->led, LED_FULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | host->ops->request(host, mrq); |
| 253 | } |
| 254 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 255 | /** |
| 256 | * mmc_start_bkops - start BKOPS for supported cards |
| 257 | * @card: MMC card to start BKOPS |
| 258 | * @form_exception: A flag to indicate if this function was |
| 259 | * called due to an exception raised by the card |
| 260 | * |
| 261 | * Start background operations whenever requested. |
| 262 | * When the urgent BKOPS bit is set in a R1 command response |
| 263 | * then background operations should be started immediately. |
| 264 | */ |
| 265 | void mmc_start_bkops(struct mmc_card *card, bool from_exception) |
| 266 | { |
| 267 | int err; |
| 268 | int timeout; |
| 269 | bool use_busy_signal; |
| 270 | |
| 271 | BUG_ON(!card); |
| 272 | |
| 273 | if (!card->ext_csd.bkops_en || mmc_card_doing_bkops(card)) |
| 274 | return; |
| 275 | |
| 276 | err = mmc_read_bkops_status(card); |
| 277 | if (err) { |
| 278 | pr_err("%s: Failed to read bkops status: %d\n", |
| 279 | mmc_hostname(card->host), err); |
| 280 | return; |
| 281 | } |
| 282 | |
| 283 | if (!card->ext_csd.raw_bkops_status) |
| 284 | return; |
| 285 | |
| 286 | if (card->ext_csd.raw_bkops_status < EXT_CSD_BKOPS_LEVEL_2 && |
| 287 | from_exception) |
| 288 | return; |
| 289 | |
| 290 | mmc_claim_host(card->host); |
| 291 | if (card->ext_csd.raw_bkops_status >= EXT_CSD_BKOPS_LEVEL_2) { |
| 292 | timeout = MMC_BKOPS_MAX_TIMEOUT; |
| 293 | use_busy_signal = true; |
| 294 | } else { |
| 295 | timeout = 0; |
| 296 | use_busy_signal = false; |
| 297 | } |
| 298 | |
| 299 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 300 | EXT_CSD_BKOPS_START, 1, timeout, use_busy_signal); |
| 301 | if (err) { |
| 302 | pr_warn("%s: Error %d starting bkops\n", |
| 303 | mmc_hostname(card->host), err); |
| 304 | goto out; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * For urgent bkops status (LEVEL_2 and more) |
| 309 | * bkops executed synchronously, otherwise |
| 310 | * the operation is in progress |
| 311 | */ |
| 312 | if (!use_busy_signal) |
| 313 | mmc_card_set_doing_bkops(card); |
| 314 | out: |
| 315 | mmc_release_host(card->host); |
| 316 | } |
| 317 | EXPORT_SYMBOL(mmc_start_bkops); |
| 318 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | static void mmc_wait_done(struct mmc_request *mrq) |
| 320 | { |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 321 | complete(&mrq->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | } |
| 323 | |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 324 | static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 325 | { |
| 326 | init_completion(&mrq->completion); |
| 327 | mrq->done = mmc_wait_done; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 328 | if (mmc_card_removed(host->card)) { |
| 329 | mrq->cmd->error = -ENOMEDIUM; |
| 330 | complete(&mrq->completion); |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 331 | return -ENOMEDIUM; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 332 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 333 | mmc_start_request(host, mrq); |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 334 | return 0; |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | static void mmc_wait_for_req_done(struct mmc_host *host, |
| 338 | struct mmc_request *mrq) |
| 339 | { |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 340 | struct mmc_command *cmd; |
| 341 | |
| 342 | while (1) { |
| 343 | wait_for_completion(&mrq->completion); |
| 344 | |
| 345 | cmd = mrq->cmd; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 346 | if (!cmd->error || !cmd->retries || |
| 347 | mmc_card_removed(host->card)) |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 348 | break; |
| 349 | |
| 350 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 351 | mmc_hostname(host), cmd->opcode, cmd->error); |
| 352 | cmd->retries--; |
| 353 | cmd->error = 0; |
| 354 | host->ops->request(host, mrq); |
| 355 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | /** |
| 359 | * mmc_pre_req - Prepare for a new request |
| 360 | * @host: MMC host to prepare command |
| 361 | * @mrq: MMC request to prepare for |
| 362 | * @is_first_req: true if there is no previous started request |
| 363 | * that may run in parellel to this call, otherwise false |
| 364 | * |
| 365 | * mmc_pre_req() is called in prior to mmc_start_req() to let |
| 366 | * host prepare for the new request. Preparation of a request may be |
| 367 | * performed while another request is running on the host. |
| 368 | */ |
| 369 | static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq, |
| 370 | bool is_first_req) |
| 371 | { |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 372 | if (host->ops->pre_req) { |
| 373 | mmc_host_clk_hold(host); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 374 | host->ops->pre_req(host, mrq, is_first_req); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 375 | mmc_host_clk_release(host); |
| 376 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /** |
| 380 | * mmc_post_req - Post process a completed request |
| 381 | * @host: MMC host to post process command |
| 382 | * @mrq: MMC request to post process for |
| 383 | * @err: Error, if non zero, clean up any resources made in pre_req |
| 384 | * |
| 385 | * Let the host post process a completed request. Post processing of |
| 386 | * a request may be performed while another reuqest is running. |
| 387 | */ |
| 388 | static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq, |
| 389 | int err) |
| 390 | { |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 391 | if (host->ops->post_req) { |
| 392 | mmc_host_clk_hold(host); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 393 | host->ops->post_req(host, mrq, err); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 394 | mmc_host_clk_release(host); |
| 395 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | /** |
| 399 | * mmc_start_req - start a non-blocking request |
| 400 | * @host: MMC host to start command |
| 401 | * @areq: async request to start |
| 402 | * @error: out parameter returns 0 for success, otherwise non zero |
| 403 | * |
| 404 | * Start a new MMC custom command request for a host. |
| 405 | * If there is on ongoing async request wait for completion |
| 406 | * of that request and start the new one and return. |
| 407 | * Does not wait for the new request to complete. |
| 408 | * |
| 409 | * Returns the completed request, NULL in case of none completed. |
| 410 | * Wait for the an ongoing request (previoulsy started) to complete and |
| 411 | * return the completed request. If there is no ongoing request, NULL |
| 412 | * is returned without waiting. NULL is not an error condition. |
| 413 | */ |
| 414 | struct mmc_async_req *mmc_start_req(struct mmc_host *host, |
| 415 | struct mmc_async_req *areq, int *error) |
| 416 | { |
| 417 | int err = 0; |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 418 | int start_err = 0; |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 419 | struct mmc_async_req *data = host->areq; |
| 420 | |
| 421 | /* Prepare a new request */ |
| 422 | if (areq) |
| 423 | mmc_pre_req(host, areq->mrq, !host->areq); |
| 424 | |
| 425 | if (host->areq) { |
| 426 | mmc_wait_for_req_done(host, host->areq->mrq); |
| 427 | err = host->areq->err_check(host->card, host->areq); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 428 | /* |
| 429 | * Check BKOPS urgency for each R1 response |
| 430 | */ |
| 431 | if (host->card && mmc_card_mmc(host->card) && |
| 432 | ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) || |
| 433 | (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) && |
| 434 | (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) |
| 435 | mmc_start_bkops(host->card, true); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 436 | } |
| 437 | |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 438 | if (!err && areq) |
| 439 | start_err = __mmc_start_req(host, areq->mrq); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 440 | |
| 441 | if (host->areq) |
| 442 | mmc_post_req(host, host->areq->mrq, 0); |
| 443 | |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 444 | /* Cancel a prepared request if it was not started. */ |
| 445 | if ((err || start_err) && areq) |
| 446 | mmc_post_req(host, areq->mrq, -EINVAL); |
| 447 | |
| 448 | if (err) |
| 449 | host->areq = NULL; |
| 450 | else |
| 451 | host->areq = areq; |
| 452 | |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 453 | if (error) |
| 454 | *error = err; |
| 455 | return data; |
| 456 | } |
| 457 | EXPORT_SYMBOL(mmc_start_req); |
| 458 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 459 | /** |
| 460 | * mmc_wait_for_req - start a request and wait for completion |
| 461 | * @host: MMC host to start command |
| 462 | * @mrq: MMC request to start |
| 463 | * |
| 464 | * Start a new MMC custom command request for a host, and wait |
| 465 | * for the command to complete. Does not attempt to parse the |
| 466 | * response. |
| 467 | */ |
| 468 | 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] | 469 | { |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 470 | __mmc_start_req(host, mrq); |
| 471 | mmc_wait_for_req_done(host, mrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 474 | |
| 475 | /** |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 476 | * mmc_interrupt_hpi - Issue for High priority Interrupt |
| 477 | * @card: the MMC card associated with the HPI transfer |
| 478 | * |
| 479 | * Issued High Priority Interrupt, and check for card status |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 480 | * until out-of prg-state. |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 481 | */ |
| 482 | int mmc_interrupt_hpi(struct mmc_card *card) |
| 483 | { |
| 484 | int err; |
| 485 | u32 status; |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 486 | unsigned long prg_wait; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 487 | |
| 488 | BUG_ON(!card); |
| 489 | |
| 490 | if (!card->ext_csd.hpi_en) { |
| 491 | pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host)); |
| 492 | return 1; |
| 493 | } |
| 494 | |
| 495 | mmc_claim_host(card->host); |
| 496 | err = mmc_send_status(card, &status); |
| 497 | if (err) { |
| 498 | pr_err("%s: Get card status fail\n", mmc_hostname(card->host)); |
| 499 | goto out; |
| 500 | } |
| 501 | |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 502 | switch (R1_CURRENT_STATE(status)) { |
| 503 | case R1_STATE_IDLE: |
| 504 | case R1_STATE_READY: |
| 505 | case R1_STATE_STBY: |
Venkatraman S | 211d4fe | 2012-08-07 19:24:45 +0530 | [diff] [blame] | 506 | case R1_STATE_TRAN: |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 507 | /* |
Venkatraman S | 211d4fe | 2012-08-07 19:24:45 +0530 | [diff] [blame] | 508 | * In idle and transfer states, HPI is not needed and the caller |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 509 | * can issue the next intended command immediately |
| 510 | */ |
| 511 | goto out; |
| 512 | case R1_STATE_PRG: |
| 513 | break; |
| 514 | default: |
| 515 | /* In all other states, it's illegal to issue HPI */ |
| 516 | pr_debug("%s: HPI cannot be sent. Card state=%d\n", |
| 517 | mmc_hostname(card->host), R1_CURRENT_STATE(status)); |
| 518 | err = -EINVAL; |
| 519 | goto out; |
| 520 | } |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 521 | |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 522 | err = mmc_send_hpi_cmd(card, &status); |
| 523 | if (err) |
| 524 | goto out; |
| 525 | |
| 526 | prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time); |
| 527 | do { |
| 528 | err = mmc_send_status(card, &status); |
| 529 | |
| 530 | if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN) |
| 531 | break; |
| 532 | if (time_after(jiffies, prg_wait)) |
| 533 | err = -ETIMEDOUT; |
| 534 | } while (!err); |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 535 | |
| 536 | out: |
| 537 | mmc_release_host(card->host); |
| 538 | return err; |
| 539 | } |
| 540 | EXPORT_SYMBOL(mmc_interrupt_hpi); |
| 541 | |
| 542 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | * mmc_wait_for_cmd - start a command and wait for completion |
| 544 | * @host: MMC host to start command |
| 545 | * @cmd: MMC command to start |
| 546 | * @retries: maximum number of retries |
| 547 | * |
| 548 | * Start a new MMC command for a host, and wait for the command |
| 549 | * to complete. Return any error that occurred while the command |
| 550 | * was executing. Do not attempt to parse the response. |
| 551 | */ |
| 552 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 553 | { |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 554 | struct mmc_request mrq = {NULL}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 556 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 559 | cmd->retries = retries; |
| 560 | |
| 561 | mrq.cmd = cmd; |
| 562 | cmd->data = NULL; |
| 563 | |
| 564 | mmc_wait_for_req(host, &mrq); |
| 565 | |
| 566 | return cmd->error; |
| 567 | } |
| 568 | |
| 569 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 570 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 571 | /** |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 572 | * mmc_stop_bkops - stop ongoing BKOPS |
| 573 | * @card: MMC card to check BKOPS |
| 574 | * |
| 575 | * Send HPI command to stop ongoing background operations to |
| 576 | * allow rapid servicing of foreground operations, e.g. read/ |
| 577 | * writes. Wait until the card comes out of the programming state |
| 578 | * to avoid errors in servicing read/write requests. |
| 579 | */ |
| 580 | int mmc_stop_bkops(struct mmc_card *card) |
| 581 | { |
| 582 | int err = 0; |
| 583 | |
| 584 | BUG_ON(!card); |
| 585 | err = mmc_interrupt_hpi(card); |
| 586 | |
| 587 | /* |
| 588 | * If err is EINVAL, we can't issue an HPI. |
| 589 | * It should complete the BKOPS. |
| 590 | */ |
| 591 | if (!err || (err == -EINVAL)) { |
| 592 | mmc_card_clr_doing_bkops(card); |
| 593 | err = 0; |
| 594 | } |
| 595 | |
| 596 | return err; |
| 597 | } |
| 598 | EXPORT_SYMBOL(mmc_stop_bkops); |
| 599 | |
| 600 | int mmc_read_bkops_status(struct mmc_card *card) |
| 601 | { |
| 602 | int err; |
| 603 | u8 *ext_csd; |
| 604 | |
| 605 | /* |
| 606 | * In future work, we should consider storing the entire ext_csd. |
| 607 | */ |
| 608 | ext_csd = kmalloc(512, GFP_KERNEL); |
| 609 | if (!ext_csd) { |
| 610 | pr_err("%s: could not allocate buffer to receive the ext_csd.\n", |
| 611 | mmc_hostname(card->host)); |
| 612 | return -ENOMEM; |
| 613 | } |
| 614 | |
| 615 | mmc_claim_host(card->host); |
| 616 | err = mmc_send_ext_csd(card, ext_csd); |
| 617 | mmc_release_host(card->host); |
| 618 | if (err) |
| 619 | goto out; |
| 620 | |
| 621 | card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS]; |
| 622 | card->ext_csd.raw_exception_status = ext_csd[EXT_CSD_EXP_EVENTS_STATUS]; |
| 623 | out: |
| 624 | kfree(ext_csd); |
| 625 | return err; |
| 626 | } |
| 627 | EXPORT_SYMBOL(mmc_read_bkops_status); |
| 628 | |
| 629 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 630 | * mmc_set_data_timeout - set the timeout for a data command |
| 631 | * @data: data phase for command |
| 632 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 633 | * |
| 634 | * Computes the data timeout parameters according to the |
| 635 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 636 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 637 | 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] | 638 | { |
| 639 | unsigned int mult; |
| 640 | |
| 641 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 642 | * SDIO cards only define an upper 1 s limit on access. |
| 643 | */ |
| 644 | if (mmc_card_sdio(card)) { |
| 645 | data->timeout_ns = 1000000000; |
| 646 | data->timeout_clks = 0; |
| 647 | return; |
| 648 | } |
| 649 | |
| 650 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 651 | * SD cards use a 100 multiplier rather than 10 |
| 652 | */ |
| 653 | mult = mmc_card_sd(card) ? 100 : 10; |
| 654 | |
| 655 | /* |
| 656 | * Scale up the multiplier (and therefore the timeout) by |
| 657 | * the r2w factor for writes. |
| 658 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 659 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 660 | mult <<= card->csd.r2w_factor; |
| 661 | |
| 662 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 663 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 664 | |
| 665 | /* |
| 666 | * SD cards also have an upper limit on the timeout. |
| 667 | */ |
| 668 | if (mmc_card_sd(card)) { |
| 669 | unsigned int timeout_us, limit_us; |
| 670 | |
| 671 | timeout_us = data->timeout_ns / 1000; |
Linus Walleij | e9b8684 | 2011-01-05 00:44:32 +0100 | [diff] [blame] | 672 | if (mmc_host_clk_rate(card->host)) |
| 673 | timeout_us += data->timeout_clks * 1000 / |
| 674 | (mmc_host_clk_rate(card->host) / 1000); |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 675 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 676 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 677 | /* |
Paul Walmsley | 3bdc9ba | 2012-03-12 04:58:00 -0600 | [diff] [blame] | 678 | * The MMC spec "It is strongly recommended |
| 679 | * for hosts to implement more than 500ms |
| 680 | * timeout value even if the card indicates |
| 681 | * the 250ms maximum busy length." Even the |
| 682 | * previous value of 300ms is known to be |
| 683 | * insufficient for some cards. |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 684 | */ |
Paul Walmsley | 3bdc9ba | 2012-03-12 04:58:00 -0600 | [diff] [blame] | 685 | limit_us = 3000000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 686 | else |
| 687 | limit_us = 100000; |
| 688 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 689 | /* |
| 690 | * SDHC cards always use these fixed values. |
| 691 | */ |
| 692 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 693 | data->timeout_ns = limit_us * 1000; |
| 694 | data->timeout_clks = 0; |
| 695 | } |
| 696 | } |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 697 | |
| 698 | /* |
| 699 | * Some cards require longer data read timeout than indicated in CSD. |
| 700 | * Address this by setting the read timeout to a "reasonably high" |
| 701 | * value. For the cards tested, 300ms has proven enough. If necessary, |
| 702 | * this value can be increased if other problematic cards require this. |
| 703 | */ |
| 704 | if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) { |
| 705 | data->timeout_ns = 300000000; |
| 706 | data->timeout_clks = 0; |
| 707 | } |
| 708 | |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 709 | /* |
| 710 | * Some cards need very high timeouts if driven in SPI mode. |
| 711 | * The worst observed timeout was 900ms after writing a |
| 712 | * continuous stream of data until the internal logic |
| 713 | * overflowed. |
| 714 | */ |
| 715 | if (mmc_host_is_spi(card->host)) { |
| 716 | if (data->flags & MMC_DATA_WRITE) { |
| 717 | if (data->timeout_ns < 1000000000) |
| 718 | data->timeout_ns = 1000000000; /* 1s */ |
| 719 | } else { |
| 720 | if (data->timeout_ns < 100000000) |
| 721 | data->timeout_ns = 100000000; /* 100ms */ |
| 722 | } |
| 723 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 724 | } |
| 725 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 726 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 728 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 729 | * @card: the MMC card associated with the data transfer |
| 730 | * @sz: original transfer size |
| 731 | * |
| 732 | * Pads the original data size with a number of extra bytes in |
| 733 | * order to avoid controller bugs and/or performance hits |
| 734 | * (e.g. some controllers revert to PIO for certain sizes). |
| 735 | * |
| 736 | * Returns the improved size, which might be unmodified. |
| 737 | * |
| 738 | * Note that this function is only relevant when issuing a |
| 739 | * single scatter gather entry. |
| 740 | */ |
| 741 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 742 | { |
| 743 | /* |
| 744 | * FIXME: We don't have a system for the controller to tell |
| 745 | * the core about its problems yet, so for now we just 32-bit |
| 746 | * align the size. |
| 747 | */ |
| 748 | sz = ((sz + 3) / 4) * 4; |
| 749 | |
| 750 | return sz; |
| 751 | } |
| 752 | EXPORT_SYMBOL(mmc_align_data_size); |
| 753 | |
| 754 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 755 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 757 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 759 | * Claim a host for a set of operations. If @abort is non null and |
| 760 | * dereference a non-zero value then this will return prematurely with |
| 761 | * that non-zero value without acquiring the lock. Returns zero |
| 762 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 764 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | { |
| 766 | DECLARE_WAITQUEUE(wait, current); |
| 767 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 768 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 770 | might_sleep(); |
| 771 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | add_wait_queue(&host->wq, &wait); |
| 773 | spin_lock_irqsave(&host->lock, flags); |
| 774 | while (1) { |
| 775 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 776 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 777 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | break; |
| 779 | spin_unlock_irqrestore(&host->lock, flags); |
| 780 | schedule(); |
| 781 | spin_lock_irqsave(&host->lock, flags); |
| 782 | } |
| 783 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 784 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 785 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 786 | host->claimer = current; |
| 787 | host->claim_cnt += 1; |
| 788 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 789 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | spin_unlock_irqrestore(&host->lock, flags); |
| 791 | remove_wait_queue(&host->wq, &wait); |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 792 | if (host->ops->enable && !stop && host->claim_cnt == 1) |
| 793 | host->ops->enable(host); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 794 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 797 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 799 | /** |
| 800 | * mmc_try_claim_host - try exclusively to claim a host |
| 801 | * @host: mmc host to claim |
| 802 | * |
| 803 | * Returns %1 if the host is claimed, %0 otherwise. |
| 804 | */ |
| 805 | int mmc_try_claim_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 806 | { |
| 807 | int claimed_host = 0; |
| 808 | unsigned long flags; |
| 809 | |
| 810 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 811 | if (!host->claimed || host->claimer == current) { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 812 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 813 | host->claimer = current; |
| 814 | host->claim_cnt += 1; |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 815 | claimed_host = 1; |
| 816 | } |
| 817 | spin_unlock_irqrestore(&host->lock, flags); |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 818 | if (host->ops->enable && claimed_host && host->claim_cnt == 1) |
| 819 | host->ops->enable(host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 820 | return claimed_host; |
| 821 | } |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 822 | EXPORT_SYMBOL(mmc_try_claim_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 823 | |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 824 | /** |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 825 | * mmc_release_host - release a host |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 826 | * @host: mmc host to release |
| 827 | * |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 828 | * Release a MMC host, allowing others to claim the host |
| 829 | * for their operations. |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 830 | */ |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 831 | void mmc_release_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 832 | { |
| 833 | unsigned long flags; |
| 834 | |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 835 | WARN_ON(!host->claimed); |
| 836 | |
| 837 | if (host->ops->disable && host->claim_cnt == 1) |
| 838 | host->ops->disable(host); |
| 839 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 840 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 841 | if (--host->claim_cnt) { |
| 842 | /* Release for nested claim */ |
| 843 | spin_unlock_irqrestore(&host->lock, flags); |
| 844 | } else { |
| 845 | host->claimed = 0; |
| 846 | host->claimer = NULL; |
| 847 | spin_unlock_irqrestore(&host->lock, flags); |
| 848 | wake_up(&host->wq); |
| 849 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | EXPORT_SYMBOL(mmc_release_host); |
| 852 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 853 | /* |
| 854 | * Internal function that does the actual ios call to the host driver, |
| 855 | * optionally printing some debug output. |
| 856 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 857 | static inline void mmc_set_ios(struct mmc_host *host) |
| 858 | { |
| 859 | struct mmc_ios *ios = &host->ios; |
| 860 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 861 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 862 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 863 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 864 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 865 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 866 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 867 | if (ios->clock > 0) |
| 868 | mmc_set_ungated(host); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 869 | host->ops->set_ios(host, ios); |
| 870 | } |
| 871 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 872 | /* |
| 873 | * Control chip select pin on a host. |
| 874 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 875 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 876 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 877 | mmc_host_clk_hold(host); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 878 | host->ios.chip_select = mode; |
| 879 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 880 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 884 | * Sets the host clock to the highest possible frequency that |
| 885 | * is below "hz". |
| 886 | */ |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 887 | static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 888 | { |
| 889 | WARN_ON(hz < host->f_min); |
| 890 | |
| 891 | if (hz > host->f_max) |
| 892 | hz = host->f_max; |
| 893 | |
| 894 | host->ios.clock = hz; |
| 895 | mmc_set_ios(host); |
| 896 | } |
| 897 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 898 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 899 | { |
| 900 | mmc_host_clk_hold(host); |
| 901 | __mmc_set_clock(host, hz); |
| 902 | mmc_host_clk_release(host); |
| 903 | } |
| 904 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 905 | #ifdef CONFIG_MMC_CLKGATE |
| 906 | /* |
| 907 | * This gates the clock by setting it to 0 Hz. |
| 908 | */ |
| 909 | void mmc_gate_clock(struct mmc_host *host) |
| 910 | { |
| 911 | unsigned long flags; |
| 912 | |
| 913 | spin_lock_irqsave(&host->clk_lock, flags); |
| 914 | host->clk_old = host->ios.clock; |
| 915 | host->ios.clock = 0; |
| 916 | host->clk_gated = true; |
| 917 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 918 | mmc_set_ios(host); |
| 919 | } |
| 920 | |
| 921 | /* |
| 922 | * This restores the clock from gating by using the cached |
| 923 | * clock value. |
| 924 | */ |
| 925 | void mmc_ungate_clock(struct mmc_host *host) |
| 926 | { |
| 927 | /* |
| 928 | * We should previously have gated the clock, so the clock shall |
| 929 | * be 0 here! The clock may however be 0 during initialization, |
| 930 | * when some request operations are performed before setting |
| 931 | * the frequency. When ungate is requested in that situation |
| 932 | * we just ignore the call. |
| 933 | */ |
| 934 | if (host->clk_old) { |
| 935 | BUG_ON(host->ios.clock); |
| 936 | /* This call will also set host->clk_gated to false */ |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 937 | __mmc_set_clock(host, host->clk_old); |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
| 941 | void mmc_set_ungated(struct mmc_host *host) |
| 942 | { |
| 943 | unsigned long flags; |
| 944 | |
| 945 | /* |
| 946 | * We've been given a new frequency while the clock is gated, |
| 947 | * so make sure we regard this as ungating it. |
| 948 | */ |
| 949 | spin_lock_irqsave(&host->clk_lock, flags); |
| 950 | host->clk_gated = false; |
| 951 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 952 | } |
| 953 | |
| 954 | #else |
| 955 | void mmc_set_ungated(struct mmc_host *host) |
| 956 | { |
| 957 | } |
| 958 | #endif |
| 959 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 960 | /* |
| 961 | * Change the bus mode (open drain/push-pull) of a host. |
| 962 | */ |
| 963 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 964 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 965 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 966 | host->ios.bus_mode = mode; |
| 967 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 968 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* |
| 972 | * Change data bus width of a host. |
| 973 | */ |
| 974 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 975 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 976 | mmc_host_clk_hold(host); |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 977 | host->ios.bus_width = width; |
| 978 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 979 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 980 | } |
| 981 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 982 | /** |
| 983 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 984 | * @vdd: voltage (mV) |
| 985 | * @low_bits: prefer low bits in boundary cases |
| 986 | * |
| 987 | * This function returns the OCR bit number according to the provided @vdd |
| 988 | * value. If conversion is not possible a negative errno value returned. |
| 989 | * |
| 990 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 991 | * on boundary voltages. For example, |
| 992 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 993 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 994 | * |
| 995 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 996 | */ |
| 997 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 998 | { |
| 999 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 1000 | int bit; |
| 1001 | |
| 1002 | if (vdd < 1650 || vdd > 3600) |
| 1003 | return -EINVAL; |
| 1004 | |
| 1005 | if (vdd >= 1650 && vdd <= 1950) |
| 1006 | return ilog2(MMC_VDD_165_195); |
| 1007 | |
| 1008 | if (low_bits) |
| 1009 | vdd -= 1; |
| 1010 | |
| 1011 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 1012 | bit = (vdd - 2000) / 100 + 8; |
| 1013 | if (bit > max_bit) |
| 1014 | return max_bit; |
| 1015 | return bit; |
| 1016 | } |
| 1017 | |
| 1018 | /** |
| 1019 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 1020 | * @vdd_min: minimum voltage value (mV) |
| 1021 | * @vdd_max: maximum voltage value (mV) |
| 1022 | * |
| 1023 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 1024 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 1025 | * |
| 1026 | * Notes wrt boundary cases: |
| 1027 | * This function sets the OCR bits for all boundary voltages, for example |
| 1028 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 1029 | * MMC_VDD_34_35 mask. |
| 1030 | */ |
| 1031 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 1032 | { |
| 1033 | u32 mask = 0; |
| 1034 | |
| 1035 | if (vdd_max < vdd_min) |
| 1036 | return 0; |
| 1037 | |
| 1038 | /* Prefer high bits for the boundary vdd_max values. */ |
| 1039 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 1040 | if (vdd_max < 0) |
| 1041 | return 0; |
| 1042 | |
| 1043 | /* Prefer low bits for the boundary vdd_min values. */ |
| 1044 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 1045 | if (vdd_min < 0) |
| 1046 | return 0; |
| 1047 | |
| 1048 | /* Fill the mask, from max bit to min bit. */ |
| 1049 | while (vdd_max >= vdd_min) |
| 1050 | mask |= 1 << vdd_max--; |
| 1051 | |
| 1052 | return mask; |
| 1053 | } |
| 1054 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 1055 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1056 | #ifdef CONFIG_REGULATOR |
| 1057 | |
| 1058 | /** |
| 1059 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 1060 | * @supply: regulator to use |
| 1061 | * |
| 1062 | * This returns either a negative errno, or a mask of voltages that |
| 1063 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 1064 | * regulator. This would normally be called before registering the |
| 1065 | * MMC host adapter. |
| 1066 | */ |
| 1067 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 1068 | { |
| 1069 | int result = 0; |
| 1070 | int count; |
| 1071 | int i; |
| 1072 | |
| 1073 | count = regulator_count_voltages(supply); |
| 1074 | if (count < 0) |
| 1075 | return count; |
| 1076 | |
| 1077 | for (i = 0; i < count; i++) { |
| 1078 | int vdd_uV; |
| 1079 | int vdd_mV; |
| 1080 | |
| 1081 | vdd_uV = regulator_list_voltage(supply, i); |
| 1082 | if (vdd_uV <= 0) |
| 1083 | continue; |
| 1084 | |
| 1085 | vdd_mV = vdd_uV / 1000; |
| 1086 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 1087 | } |
| 1088 | |
| 1089 | return result; |
| 1090 | } |
Chris Ball | 45a6b32 | 2012-06-11 09:39:12 -0400 | [diff] [blame] | 1091 | EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1092 | |
| 1093 | /** |
| 1094 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1095 | * @mmc: the host to regulate |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1096 | * @supply: regulator to use |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1097 | * @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] | 1098 | * |
| 1099 | * Returns zero on success, else negative errno. |
| 1100 | * |
| 1101 | * MMC host drivers may use this to enable or disable a regulator using |
| 1102 | * a particular supply voltage. This would normally be called from the |
| 1103 | * set_ios() method. |
| 1104 | */ |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1105 | int mmc_regulator_set_ocr(struct mmc_host *mmc, |
| 1106 | struct regulator *supply, |
| 1107 | unsigned short vdd_bit) |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1108 | { |
| 1109 | int result = 0; |
| 1110 | int min_uV, max_uV; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1111 | |
| 1112 | if (vdd_bit) { |
| 1113 | int tmp; |
| 1114 | int voltage; |
| 1115 | |
| 1116 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 1117 | * bits this regulator doesn't quite support ... don't |
| 1118 | * be too picky, most cards and regulators are OK with |
| 1119 | * a 0.1V range goof (it's a small error percentage). |
| 1120 | */ |
| 1121 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 1122 | if (tmp == 0) { |
| 1123 | min_uV = 1650 * 1000; |
| 1124 | max_uV = 1950 * 1000; |
| 1125 | } else { |
| 1126 | min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 1127 | max_uV = min_uV + 100 * 1000; |
| 1128 | } |
| 1129 | |
| 1130 | /* avoid needless changes to this voltage; the regulator |
| 1131 | * might not allow this operation |
| 1132 | */ |
| 1133 | voltage = regulator_get_voltage(supply); |
Jaehoon Chung | 6e8201f | 2012-01-16 17:49:01 +0900 | [diff] [blame] | 1134 | |
| 1135 | if (mmc->caps2 & MMC_CAP2_BROKEN_VOLTAGE) |
| 1136 | min_uV = max_uV = voltage; |
| 1137 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1138 | if (voltage < 0) |
| 1139 | result = voltage; |
| 1140 | else if (voltage < min_uV || voltage > max_uV) |
| 1141 | result = regulator_set_voltage(supply, min_uV, max_uV); |
| 1142 | else |
| 1143 | result = 0; |
| 1144 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1145 | if (result == 0 && !mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1146 | result = regulator_enable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1147 | if (!result) |
| 1148 | mmc->regulator_enabled = true; |
| 1149 | } |
| 1150 | } else if (mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1151 | result = regulator_disable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1152 | if (result == 0) |
| 1153 | mmc->regulator_enabled = false; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1156 | if (result) |
| 1157 | dev_err(mmc_dev(mmc), |
| 1158 | "could not set regulator OCR (%d)\n", result); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1159 | return result; |
| 1160 | } |
Chris Ball | 45a6b32 | 2012-06-11 09:39:12 -0400 | [diff] [blame] | 1161 | EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1162 | |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 1163 | int mmc_regulator_get_supply(struct mmc_host *mmc) |
| 1164 | { |
| 1165 | struct device *dev = mmc_dev(mmc); |
| 1166 | struct regulator *supply; |
| 1167 | int ret; |
| 1168 | |
| 1169 | supply = devm_regulator_get(dev, "vmmc"); |
| 1170 | mmc->supply.vmmc = supply; |
| 1171 | mmc->supply.vqmmc = devm_regulator_get(dev, "vqmmc"); |
| 1172 | |
| 1173 | if (IS_ERR(supply)) |
| 1174 | return PTR_ERR(supply); |
| 1175 | |
| 1176 | ret = mmc_regulator_get_ocrmask(supply); |
| 1177 | if (ret > 0) |
| 1178 | mmc->ocr_avail = ret; |
| 1179 | else |
| 1180 | dev_warn(mmc_dev(mmc), "Failed getting OCR mask: %d\n", ret); |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | EXPORT_SYMBOL_GPL(mmc_regulator_get_supply); |
| 1185 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1186 | #endif /* CONFIG_REGULATOR */ |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1187 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1188 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1189 | * Mask off any voltages we don't support and select |
| 1190 | * the lowest voltage |
| 1191 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1192 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | { |
| 1194 | int bit; |
| 1195 | |
| 1196 | ocr &= host->ocr_avail; |
| 1197 | |
| 1198 | bit = ffs(ocr); |
| 1199 | if (bit) { |
| 1200 | bit -= 1; |
| 1201 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 1202 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1204 | mmc_host_clk_hold(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1206 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1207 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1208 | } else { |
David Brownell | f6e10b8 | 2008-12-31 09:50:30 -0800 | [diff] [blame] | 1209 | pr_warning("%s: host doesn't support card's voltages\n", |
| 1210 | mmc_hostname(host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | ocr = 0; |
| 1212 | } |
| 1213 | |
| 1214 | return ocr; |
| 1215 | } |
| 1216 | |
Philip Rakity | 261bbd4 | 2011-05-13 11:17:17 +0530 | [diff] [blame] | 1217 | int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, bool cmd11) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 1218 | { |
| 1219 | struct mmc_command cmd = {0}; |
| 1220 | int err = 0; |
| 1221 | |
| 1222 | BUG_ON(!host); |
| 1223 | |
| 1224 | /* |
| 1225 | * Send CMD11 only if the request is to switch the card to |
| 1226 | * 1.8V signalling. |
| 1227 | */ |
Philip Rakity | 261bbd4 | 2011-05-13 11:17:17 +0530 | [diff] [blame] | 1228 | if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 1229 | cmd.opcode = SD_SWITCH_VOLTAGE; |
| 1230 | cmd.arg = 0; |
| 1231 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1232 | |
| 1233 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 1234 | if (err) |
| 1235 | return err; |
| 1236 | |
| 1237 | if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) |
| 1238 | return -EIO; |
| 1239 | } |
| 1240 | |
| 1241 | host->ios.signal_voltage = signal_voltage; |
| 1242 | |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1243 | if (host->ops->start_signal_voltage_switch) { |
| 1244 | mmc_host_clk_hold(host); |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 1245 | err = host->ops->start_signal_voltage_switch(host, &host->ios); |
Sujit Reddy Thumma | 2c4967f74 | 2012-02-04 16:14:50 -0500 | [diff] [blame] | 1246 | mmc_host_clk_release(host); |
| 1247 | } |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 1248 | |
| 1249 | return err; |
| 1250 | } |
| 1251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1252 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1253 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1254 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1255 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1257 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1258 | host->ios.timing = timing; |
| 1259 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1260 | mmc_host_clk_release(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1261 | } |
| 1262 | |
| 1263 | /* |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1264 | * Select appropriate driver type for host. |
| 1265 | */ |
| 1266 | void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) |
| 1267 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1268 | mmc_host_clk_hold(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1269 | host->ios.drv_type = drv_type; |
| 1270 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1271 | mmc_host_clk_release(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1272 | } |
| 1273 | |
Girish K S | a80f162 | 2011-11-15 11:55:46 +0530 | [diff] [blame] | 1274 | static void mmc_poweroff_notify(struct mmc_host *host) |
| 1275 | { |
| 1276 | struct mmc_card *card; |
| 1277 | unsigned int timeout; |
| 1278 | unsigned int notify_type = EXT_CSD_NO_POWER_NOTIFICATION; |
| 1279 | int err = 0; |
| 1280 | |
| 1281 | card = host->card; |
Girish K S | 3e73c36 | 2012-01-31 15:44:03 +0530 | [diff] [blame] | 1282 | mmc_claim_host(host); |
Girish K S | a80f162 | 2011-11-15 11:55:46 +0530 | [diff] [blame] | 1283 | |
| 1284 | /* |
| 1285 | * Send power notify command only if card |
| 1286 | * is mmc and notify state is powered ON |
| 1287 | */ |
| 1288 | if (card && mmc_card_mmc(card) && |
| 1289 | (card->poweroff_notify_state == MMC_POWERED_ON)) { |
| 1290 | |
| 1291 | if (host->power_notify_type == MMC_HOST_PW_NOTIFY_SHORT) { |
| 1292 | notify_type = EXT_CSD_POWER_OFF_SHORT; |
| 1293 | timeout = card->ext_csd.generic_cmd6_time; |
| 1294 | card->poweroff_notify_state = MMC_POWEROFF_SHORT; |
| 1295 | } else { |
| 1296 | notify_type = EXT_CSD_POWER_OFF_LONG; |
| 1297 | timeout = card->ext_csd.power_off_longtime; |
| 1298 | card->poweroff_notify_state = MMC_POWEROFF_LONG; |
| 1299 | } |
| 1300 | |
| 1301 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1302 | EXT_CSD_POWER_OFF_NOTIFICATION, |
| 1303 | notify_type, timeout); |
| 1304 | |
| 1305 | if (err && err != -EBADMSG) |
| 1306 | pr_err("Device failed to respond within %d poweroff " |
| 1307 | "time. Forcefully powering down the device\n", |
| 1308 | timeout); |
| 1309 | |
| 1310 | /* Set the card state to no notification after the poweroff */ |
| 1311 | card->poweroff_notify_state = MMC_NO_POWER_NOTIFICATION; |
| 1312 | } |
Girish K S | 3e73c36 | 2012-01-31 15:44:03 +0530 | [diff] [blame] | 1313 | mmc_release_host(host); |
Girish K S | a80f162 | 2011-11-15 11:55:46 +0530 | [diff] [blame] | 1314 | } |
| 1315 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1316 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 1317 | * Apply power to the MMC stack. This is a two-stage process. |
| 1318 | * First, we enable power to the card without the clock running. |
| 1319 | * We then wait a bit for the power to stabilise. Finally, |
| 1320 | * enable the bus drivers and clock to the card. |
| 1321 | * |
| 1322 | * We must _NOT_ enable the clock prior to power stablising. |
| 1323 | * |
| 1324 | * If a host does all the power sequencing itself, ignore the |
| 1325 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | */ |
| 1327 | static void mmc_power_up(struct mmc_host *host) |
| 1328 | { |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 1329 | int bit; |
| 1330 | |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 1331 | if (host->ios.power_mode == MMC_POWER_ON) |
| 1332 | return; |
| 1333 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1334 | mmc_host_clk_hold(host); |
| 1335 | |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 1336 | /* If ocr is set, we use it */ |
| 1337 | if (host->ocr) |
| 1338 | bit = ffs(host->ocr) - 1; |
| 1339 | else |
| 1340 | bit = fls(host->ocr_avail) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | host->ios.vdd = bit; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1343 | if (mmc_host_is_spi(host)) |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1344 | host->ios.chip_select = MMC_CS_HIGH; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1345 | else |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1346 | host->ios.chip_select = MMC_CS_DONTCARE; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame] | 1347 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 1349 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1350 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1351 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | |
Aaron Lu | 108ecc4 | 2012-07-10 16:55:37 +0800 | [diff] [blame] | 1353 | /* Set signal voltage to 3.3V */ |
| 1354 | mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330, false); |
| 1355 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 1356 | /* |
| 1357 | * This delay should be sufficient to allow the power supply |
| 1358 | * to reach the minimum voltage. |
| 1359 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 1360 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1362 | host->ios.clock = host->f_init; |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 1363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1365 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 1367 | /* |
| 1368 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 1369 | * time required to reach a stable voltage. |
| 1370 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 1371 | mmc_delay(10); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1372 | |
| 1373 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } |
| 1375 | |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1376 | void mmc_power_off(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | { |
Girish K S | 3e73c36 | 2012-01-31 15:44:03 +0530 | [diff] [blame] | 1378 | int err = 0; |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 1379 | |
| 1380 | if (host->ios.power_mode == MMC_POWER_OFF) |
| 1381 | return; |
| 1382 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1383 | mmc_host_clk_hold(host); |
| 1384 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | host->ios.clock = 0; |
| 1386 | host->ios.vdd = 0; |
Ulf Hansson | b33d46c | 2011-03-05 14:36:24 +0100 | [diff] [blame] | 1387 | |
Girish K S | 3e73c36 | 2012-01-31 15:44:03 +0530 | [diff] [blame] | 1388 | /* |
| 1389 | * For eMMC 4.5 device send AWAKE command before |
| 1390 | * POWER_OFF_NOTIFY command, because in sleep state |
| 1391 | * eMMC 4.5 devices respond to only RESET and AWAKE cmd |
| 1392 | */ |
| 1393 | if (host->card && mmc_card_is_sleep(host->card) && |
| 1394 | host->bus_ops->resume) { |
| 1395 | err = host->bus_ops->resume(host); |
| 1396 | |
| 1397 | if (!err) |
| 1398 | mmc_poweroff_notify(host); |
| 1399 | else |
| 1400 | pr_warning("%s: error %d during resume " |
| 1401 | "(continue with poweroff sequence)\n", |
| 1402 | mmc_hostname(host), err); |
| 1403 | } |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 1404 | |
Ulf Hansson | b33d46c | 2011-03-05 14:36:24 +0100 | [diff] [blame] | 1405 | /* |
| 1406 | * Reset ocr mask to be the highest possible voltage supported for |
| 1407 | * this mmc host. This value will be used at next power up. |
| 1408 | */ |
| 1409 | host->ocr = 1 << (fls(host->ocr_avail) - 1); |
| 1410 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1411 | if (!mmc_host_is_spi(host)) { |
| 1412 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 1413 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 1414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 1416 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1417 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1418 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1419 | |
Daniel Drake | 041beb1 | 2011-09-07 10:22:09 +0100 | [diff] [blame] | 1420 | /* |
| 1421 | * Some configurations, such as the 802.11 SDIO card in the OLPC |
| 1422 | * XO-1.5, require a short delay after poweroff before the card |
| 1423 | * can be successfully turned on again. |
| 1424 | */ |
| 1425 | mmc_delay(1); |
| 1426 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1427 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | } |
| 1429 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1431 | * Cleanup when the last reference to the bus operator is dropped. |
| 1432 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 1433 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1434 | { |
| 1435 | BUG_ON(!host); |
| 1436 | BUG_ON(host->bus_refs); |
| 1437 | BUG_ON(!host->bus_dead); |
| 1438 | |
| 1439 | host->bus_ops = NULL; |
| 1440 | } |
| 1441 | |
| 1442 | /* |
| 1443 | * Increase reference count of bus operator |
| 1444 | */ |
| 1445 | static inline void mmc_bus_get(struct mmc_host *host) |
| 1446 | { |
| 1447 | unsigned long flags; |
| 1448 | |
| 1449 | spin_lock_irqsave(&host->lock, flags); |
| 1450 | host->bus_refs++; |
| 1451 | spin_unlock_irqrestore(&host->lock, flags); |
| 1452 | } |
| 1453 | |
| 1454 | /* |
| 1455 | * Decrease reference count of bus operator and free it if |
| 1456 | * it is the last reference. |
| 1457 | */ |
| 1458 | static inline void mmc_bus_put(struct mmc_host *host) |
| 1459 | { |
| 1460 | unsigned long flags; |
| 1461 | |
| 1462 | spin_lock_irqsave(&host->lock, flags); |
| 1463 | host->bus_refs--; |
| 1464 | if ((host->bus_refs == 0) && host->bus_ops) |
| 1465 | __mmc_release_bus(host); |
| 1466 | spin_unlock_irqrestore(&host->lock, flags); |
| 1467 | } |
| 1468 | |
| 1469 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1470 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 1471 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1473 | 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] | 1474 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1475 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1477 | BUG_ON(!host); |
| 1478 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1479 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1480 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1481 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1482 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1483 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1484 | BUG_ON(host->bus_ops); |
| 1485 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1486 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1487 | host->bus_ops = ops; |
| 1488 | host->bus_refs = 1; |
| 1489 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1491 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | } |
| 1493 | |
| 1494 | /* |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1495 | * Remove the current bus handler from a host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1496 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1497 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1498 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1499 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1501 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1503 | WARN_ON(!host->claimed); |
| 1504 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1506 | spin_lock_irqsave(&host->lock, flags); |
| 1507 | |
| 1508 | host->bus_dead = 1; |
| 1509 | |
| 1510 | spin_unlock_irqrestore(&host->lock, flags); |
| 1511 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1512 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | } |
| 1514 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | /** |
| 1516 | * mmc_detect_change - process change of state on a MMC socket |
| 1517 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1518 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1520 | * MMC drivers should call this when they detect a card has been |
| 1521 | * inserted or removed. The MMC layer will confirm that any |
| 1522 | * present card is still functional, and initialize any newly |
| 1523 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1525 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1527 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1528 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1529 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1530 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1531 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1532 | #endif |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1533 | host->detect_change = 1; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1534 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | EXPORT_SYMBOL(mmc_detect_change); |
| 1538 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1539 | void mmc_init_erase(struct mmc_card *card) |
| 1540 | { |
| 1541 | unsigned int sz; |
| 1542 | |
| 1543 | if (is_power_of_2(card->erase_size)) |
| 1544 | card->erase_shift = ffs(card->erase_size) - 1; |
| 1545 | else |
| 1546 | card->erase_shift = 0; |
| 1547 | |
| 1548 | /* |
| 1549 | * It is possible to erase an arbitrarily large area of an SD or MMC |
| 1550 | * card. That is not desirable because it can take a long time |
| 1551 | * (minutes) potentially delaying more important I/O, and also the |
| 1552 | * timeout calculations become increasingly hugely over-estimated. |
| 1553 | * Consequently, 'pref_erase' is defined as a guide to limit erases |
| 1554 | * to that size and alignment. |
| 1555 | * |
| 1556 | * For SD cards that define Allocation Unit size, limit erases to one |
| 1557 | * Allocation Unit at a time. For MMC cards that define High Capacity |
| 1558 | * Erase Size, whether it is switched on or not, limit to that size. |
| 1559 | * Otherwise just have a stab at a good value. For modern cards it |
| 1560 | * will end up being 4MiB. Note that if the value is too small, it |
| 1561 | * can end up taking longer to erase. |
| 1562 | */ |
| 1563 | if (mmc_card_sd(card) && card->ssr.au) { |
| 1564 | card->pref_erase = card->ssr.au; |
| 1565 | card->erase_shift = ffs(card->ssr.au) - 1; |
| 1566 | } else if (card->ext_csd.hc_erase_size) { |
| 1567 | card->pref_erase = card->ext_csd.hc_erase_size; |
| 1568 | } else { |
| 1569 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 1570 | if (sz < 128) |
| 1571 | card->pref_erase = 512 * 1024 / 512; |
| 1572 | else if (sz < 512) |
| 1573 | card->pref_erase = 1024 * 1024 / 512; |
| 1574 | else if (sz < 1024) |
| 1575 | card->pref_erase = 2 * 1024 * 1024 / 512; |
| 1576 | else |
| 1577 | card->pref_erase = 4 * 1024 * 1024 / 512; |
| 1578 | if (card->pref_erase < card->erase_size) |
| 1579 | card->pref_erase = card->erase_size; |
| 1580 | else { |
| 1581 | sz = card->pref_erase % card->erase_size; |
| 1582 | if (sz) |
| 1583 | card->pref_erase += card->erase_size - sz; |
| 1584 | } |
| 1585 | } |
| 1586 | } |
| 1587 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1588 | static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, |
| 1589 | unsigned int arg, unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1590 | { |
| 1591 | unsigned int erase_timeout; |
| 1592 | |
Adrian Hunter | 7194efb | 2012-04-05 14:45:47 +0300 | [diff] [blame] | 1593 | if (arg == MMC_DISCARD_ARG || |
| 1594 | (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) { |
| 1595 | erase_timeout = card->ext_csd.trim_timeout; |
| 1596 | } else if (card->ext_csd.erase_group_def & 1) { |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1597 | /* High Capacity Erase Group Size uses HC timeouts */ |
| 1598 | if (arg == MMC_TRIM_ARG) |
| 1599 | erase_timeout = card->ext_csd.trim_timeout; |
| 1600 | else |
| 1601 | erase_timeout = card->ext_csd.hc_erase_timeout; |
| 1602 | } else { |
| 1603 | /* CSD Erase Group Size uses write timeout */ |
| 1604 | unsigned int mult = (10 << card->csd.r2w_factor); |
| 1605 | unsigned int timeout_clks = card->csd.tacc_clks * mult; |
| 1606 | unsigned int timeout_us; |
| 1607 | |
| 1608 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ |
| 1609 | if (card->csd.tacc_ns < 1000000) |
| 1610 | timeout_us = (card->csd.tacc_ns * mult) / 1000; |
| 1611 | else |
| 1612 | timeout_us = (card->csd.tacc_ns / 1000) * mult; |
| 1613 | |
| 1614 | /* |
| 1615 | * ios.clock is only a target. The real clock rate might be |
| 1616 | * less but not that much less, so fudge it by multiplying by 2. |
| 1617 | */ |
| 1618 | timeout_clks <<= 1; |
| 1619 | timeout_us += (timeout_clks * 1000) / |
Adrian Hunter | 4cf8c6d | 2011-06-23 13:40:27 +0300 | [diff] [blame] | 1620 | (mmc_host_clk_rate(card->host) / 1000); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1621 | |
| 1622 | erase_timeout = timeout_us / 1000; |
| 1623 | |
| 1624 | /* |
| 1625 | * Theoretically, the calculation could underflow so round up |
| 1626 | * to 1ms in that case. |
| 1627 | */ |
| 1628 | if (!erase_timeout) |
| 1629 | erase_timeout = 1; |
| 1630 | } |
| 1631 | |
| 1632 | /* Multiplier for secure operations */ |
| 1633 | if (arg & MMC_SECURE_ARGS) { |
| 1634 | if (arg == MMC_SECURE_ERASE_ARG) |
| 1635 | erase_timeout *= card->ext_csd.sec_erase_mult; |
| 1636 | else |
| 1637 | erase_timeout *= card->ext_csd.sec_trim_mult; |
| 1638 | } |
| 1639 | |
| 1640 | erase_timeout *= qty; |
| 1641 | |
| 1642 | /* |
| 1643 | * Ensure at least a 1 second timeout for SPI as per |
| 1644 | * 'mmc_set_data_timeout()' |
| 1645 | */ |
| 1646 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 1647 | erase_timeout = 1000; |
| 1648 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1649 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1652 | static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, |
| 1653 | unsigned int arg, |
| 1654 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1655 | { |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1656 | unsigned int erase_timeout; |
| 1657 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1658 | if (card->ssr.erase_timeout) { |
| 1659 | /* Erase timeout specified in SD Status Register (SSR) */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1660 | erase_timeout = card->ssr.erase_timeout * qty + |
| 1661 | card->ssr.erase_offset; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1662 | } else { |
| 1663 | /* |
| 1664 | * Erase timeout not specified in SD Status Register (SSR) so |
| 1665 | * use 250ms per write block. |
| 1666 | */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1667 | erase_timeout = 250 * qty; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1668 | } |
| 1669 | |
| 1670 | /* Must not be less than 1 second */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1671 | if (erase_timeout < 1000) |
| 1672 | erase_timeout = 1000; |
| 1673 | |
| 1674 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1675 | } |
| 1676 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1677 | static unsigned int mmc_erase_timeout(struct mmc_card *card, |
| 1678 | unsigned int arg, |
| 1679 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1680 | { |
| 1681 | if (mmc_card_sd(card)) |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1682 | return mmc_sd_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1683 | else |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1684 | return mmc_mmc_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, |
| 1688 | unsigned int to, unsigned int arg) |
| 1689 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1690 | struct mmc_command cmd = {0}; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1691 | unsigned int qty = 0; |
| 1692 | int err; |
| 1693 | |
| 1694 | /* |
| 1695 | * qty is used to calculate the erase timeout which depends on how many |
| 1696 | * erase groups (or allocation units in SD terminology) are affected. |
| 1697 | * We count erasing part of an erase group as one erase group. |
| 1698 | * For SD, the allocation units are always a power of 2. For MMC, the |
| 1699 | * erase group size is almost certainly also power of 2, but it does not |
| 1700 | * seem to insist on that in the JEDEC standard, so we fall back to |
| 1701 | * division in that case. SD may not specify an allocation unit size, |
| 1702 | * in which case the timeout is based on the number of write blocks. |
| 1703 | * |
| 1704 | * Note that the timeout for secure trim 2 will only be correct if the |
| 1705 | * number of erase groups specified is the same as the total of all |
| 1706 | * preceding secure trim 1 commands. Since the power may have been |
| 1707 | * lost since the secure trim 1 commands occurred, it is generally |
| 1708 | * impossible to calculate the secure trim 2 timeout correctly. |
| 1709 | */ |
| 1710 | if (card->erase_shift) |
| 1711 | qty += ((to >> card->erase_shift) - |
| 1712 | (from >> card->erase_shift)) + 1; |
| 1713 | else if (mmc_card_sd(card)) |
| 1714 | qty += to - from + 1; |
| 1715 | else |
| 1716 | qty += ((to / card->erase_size) - |
| 1717 | (from / card->erase_size)) + 1; |
| 1718 | |
| 1719 | if (!mmc_card_blockaddr(card)) { |
| 1720 | from <<= 9; |
| 1721 | to <<= 9; |
| 1722 | } |
| 1723 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1724 | if (mmc_card_sd(card)) |
| 1725 | cmd.opcode = SD_ERASE_WR_BLK_START; |
| 1726 | else |
| 1727 | cmd.opcode = MMC_ERASE_GROUP_START; |
| 1728 | cmd.arg = from; |
| 1729 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1730 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1731 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1732 | pr_err("mmc_erase: group start error %d, " |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1733 | "status %#x\n", err, cmd.resp[0]); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1734 | err = -EIO; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1735 | goto out; |
| 1736 | } |
| 1737 | |
| 1738 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1739 | if (mmc_card_sd(card)) |
| 1740 | cmd.opcode = SD_ERASE_WR_BLK_END; |
| 1741 | else |
| 1742 | cmd.opcode = MMC_ERASE_GROUP_END; |
| 1743 | cmd.arg = to; |
| 1744 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1745 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1746 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1747 | pr_err("mmc_erase: group end error %d, status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1748 | err, cmd.resp[0]); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1749 | err = -EIO; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1750 | goto out; |
| 1751 | } |
| 1752 | |
| 1753 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1754 | cmd.opcode = MMC_ERASE; |
| 1755 | cmd.arg = arg; |
| 1756 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1757 | cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1758 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1759 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1760 | pr_err("mmc_erase: erase error %d, status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1761 | err, cmd.resp[0]); |
| 1762 | err = -EIO; |
| 1763 | goto out; |
| 1764 | } |
| 1765 | |
| 1766 | if (mmc_host_is_spi(card->host)) |
| 1767 | goto out; |
| 1768 | |
| 1769 | do { |
| 1770 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1771 | cmd.opcode = MMC_SEND_STATUS; |
| 1772 | cmd.arg = card->rca << 16; |
| 1773 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1774 | /* Do not retry else we can't see errors */ |
| 1775 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1776 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1777 | pr_err("error %d requesting status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1778 | err, cmd.resp[0]); |
| 1779 | err = -EIO; |
| 1780 | goto out; |
| 1781 | } |
| 1782 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
Jaehoon Chung | 7435bb7 | 2011-08-10 18:46:28 +0900 | [diff] [blame] | 1783 | R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1784 | out: |
| 1785 | return err; |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * mmc_erase - erase sectors. |
| 1790 | * @card: card to erase |
| 1791 | * @from: first sector to erase |
| 1792 | * @nr: number of sectors to erase |
| 1793 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
| 1794 | * |
| 1795 | * Caller must claim host before calling this function. |
| 1796 | */ |
| 1797 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, |
| 1798 | unsigned int arg) |
| 1799 | { |
| 1800 | unsigned int rem, to = from + nr; |
| 1801 | |
| 1802 | if (!(card->host->caps & MMC_CAP_ERASE) || |
| 1803 | !(card->csd.cmdclass & CCC_ERASE)) |
| 1804 | return -EOPNOTSUPP; |
| 1805 | |
| 1806 | if (!card->erase_size) |
| 1807 | return -EOPNOTSUPP; |
| 1808 | |
| 1809 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
| 1810 | return -EOPNOTSUPP; |
| 1811 | |
| 1812 | if ((arg & MMC_SECURE_ARGS) && |
| 1813 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 1814 | return -EOPNOTSUPP; |
| 1815 | |
| 1816 | if ((arg & MMC_TRIM_ARGS) && |
| 1817 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 1818 | return -EOPNOTSUPP; |
| 1819 | |
| 1820 | if (arg == MMC_SECURE_ERASE_ARG) { |
| 1821 | if (from % card->erase_size || nr % card->erase_size) |
| 1822 | return -EINVAL; |
| 1823 | } |
| 1824 | |
| 1825 | if (arg == MMC_ERASE_ARG) { |
| 1826 | rem = from % card->erase_size; |
| 1827 | if (rem) { |
| 1828 | rem = card->erase_size - rem; |
| 1829 | from += rem; |
| 1830 | if (nr > rem) |
| 1831 | nr -= rem; |
| 1832 | else |
| 1833 | return 0; |
| 1834 | } |
| 1835 | rem = nr % card->erase_size; |
| 1836 | if (rem) |
| 1837 | nr -= rem; |
| 1838 | } |
| 1839 | |
| 1840 | if (nr == 0) |
| 1841 | return 0; |
| 1842 | |
| 1843 | to = from + nr; |
| 1844 | |
| 1845 | if (to <= from) |
| 1846 | return -EINVAL; |
| 1847 | |
| 1848 | /* 'from' and 'to' are inclusive */ |
| 1849 | to -= 1; |
| 1850 | |
| 1851 | return mmc_do_erase(card, from, to, arg); |
| 1852 | } |
| 1853 | EXPORT_SYMBOL(mmc_erase); |
| 1854 | |
| 1855 | int mmc_can_erase(struct mmc_card *card) |
| 1856 | { |
| 1857 | if ((card->host->caps & MMC_CAP_ERASE) && |
| 1858 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
| 1859 | return 1; |
| 1860 | return 0; |
| 1861 | } |
| 1862 | EXPORT_SYMBOL(mmc_can_erase); |
| 1863 | |
| 1864 | int mmc_can_trim(struct mmc_card *card) |
| 1865 | { |
| 1866 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) |
| 1867 | return 1; |
| 1868 | return 0; |
| 1869 | } |
| 1870 | EXPORT_SYMBOL(mmc_can_trim); |
| 1871 | |
Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 1872 | int mmc_can_discard(struct mmc_card *card) |
| 1873 | { |
| 1874 | /* |
| 1875 | * As there's no way to detect the discard support bit at v4.5 |
| 1876 | * use the s/w feature support filed. |
| 1877 | */ |
| 1878 | if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE) |
| 1879 | return 1; |
| 1880 | return 0; |
| 1881 | } |
| 1882 | EXPORT_SYMBOL(mmc_can_discard); |
| 1883 | |
Kyungmin Park | d9ddd62 | 2011-10-14 14:15:48 +0900 | [diff] [blame] | 1884 | int mmc_can_sanitize(struct mmc_card *card) |
| 1885 | { |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1886 | if (!mmc_can_trim(card) && !mmc_can_erase(card)) |
| 1887 | return 0; |
Kyungmin Park | d9ddd62 | 2011-10-14 14:15:48 +0900 | [diff] [blame] | 1888 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) |
| 1889 | return 1; |
| 1890 | return 0; |
| 1891 | } |
| 1892 | EXPORT_SYMBOL(mmc_can_sanitize); |
| 1893 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1894 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
| 1895 | { |
| 1896 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) |
| 1897 | return 1; |
| 1898 | return 0; |
| 1899 | } |
| 1900 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); |
| 1901 | |
| 1902 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, |
| 1903 | unsigned int nr) |
| 1904 | { |
| 1905 | if (!card->erase_size) |
| 1906 | return 0; |
| 1907 | if (from % card->erase_size || nr % card->erase_size) |
| 1908 | return 0; |
| 1909 | return 1; |
| 1910 | } |
| 1911 | EXPORT_SYMBOL(mmc_erase_group_aligned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 1913 | static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, |
| 1914 | unsigned int arg) |
| 1915 | { |
| 1916 | struct mmc_host *host = card->host; |
| 1917 | unsigned int max_discard, x, y, qty = 0, max_qty, timeout; |
| 1918 | unsigned int last_timeout = 0; |
| 1919 | |
| 1920 | if (card->erase_shift) |
| 1921 | max_qty = UINT_MAX >> card->erase_shift; |
| 1922 | else if (mmc_card_sd(card)) |
| 1923 | max_qty = UINT_MAX; |
| 1924 | else |
| 1925 | max_qty = UINT_MAX / card->erase_size; |
| 1926 | |
| 1927 | /* Find the largest qty with an OK timeout */ |
| 1928 | do { |
| 1929 | y = 0; |
| 1930 | for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { |
| 1931 | timeout = mmc_erase_timeout(card, arg, qty + x); |
| 1932 | if (timeout > host->max_discard_to) |
| 1933 | break; |
| 1934 | if (timeout < last_timeout) |
| 1935 | break; |
| 1936 | last_timeout = timeout; |
| 1937 | y = x; |
| 1938 | } |
| 1939 | qty += y; |
| 1940 | } while (y); |
| 1941 | |
| 1942 | if (!qty) |
| 1943 | return 0; |
| 1944 | |
| 1945 | if (qty == 1) |
| 1946 | return 1; |
| 1947 | |
| 1948 | /* Convert qty to sectors */ |
| 1949 | if (card->erase_shift) |
| 1950 | max_discard = --qty << card->erase_shift; |
| 1951 | else if (mmc_card_sd(card)) |
| 1952 | max_discard = qty; |
| 1953 | else |
| 1954 | max_discard = --qty * card->erase_size; |
| 1955 | |
| 1956 | return max_discard; |
| 1957 | } |
| 1958 | |
| 1959 | unsigned int mmc_calc_max_discard(struct mmc_card *card) |
| 1960 | { |
| 1961 | struct mmc_host *host = card->host; |
| 1962 | unsigned int max_discard, max_trim; |
| 1963 | |
| 1964 | if (!host->max_discard_to) |
| 1965 | return UINT_MAX; |
| 1966 | |
| 1967 | /* |
| 1968 | * Without erase_group_def set, MMC erase timeout depends on clock |
| 1969 | * frequence which can change. In that case, the best choice is |
| 1970 | * just the preferred erase size. |
| 1971 | */ |
| 1972 | if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) |
| 1973 | return card->pref_erase; |
| 1974 | |
| 1975 | max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); |
| 1976 | if (mmc_can_trim(card)) { |
| 1977 | max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); |
| 1978 | if (max_trim < max_discard) |
| 1979 | max_discard = max_trim; |
| 1980 | } else if (max_discard < card->erase_size) { |
| 1981 | max_discard = 0; |
| 1982 | } |
| 1983 | pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", |
| 1984 | mmc_hostname(host), max_discard, host->max_discard_to); |
| 1985 | return max_discard; |
| 1986 | } |
| 1987 | EXPORT_SYMBOL(mmc_calc_max_discard); |
| 1988 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1989 | int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) |
| 1990 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1991 | struct mmc_command cmd = {0}; |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1992 | |
| 1993 | if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card)) |
| 1994 | return 0; |
| 1995 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1996 | cmd.opcode = MMC_SET_BLOCKLEN; |
| 1997 | cmd.arg = blocklen; |
| 1998 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1999 | return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 2000 | } |
| 2001 | EXPORT_SYMBOL(mmc_set_blocklen); |
| 2002 | |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 2003 | static void mmc_hw_reset_for_init(struct mmc_host *host) |
| 2004 | { |
| 2005 | if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) |
| 2006 | return; |
| 2007 | mmc_host_clk_hold(host); |
| 2008 | host->ops->hw_reset(host); |
| 2009 | mmc_host_clk_release(host); |
| 2010 | } |
| 2011 | |
| 2012 | int mmc_can_reset(struct mmc_card *card) |
| 2013 | { |
| 2014 | u8 rst_n_function; |
| 2015 | |
| 2016 | if (!mmc_card_mmc(card)) |
| 2017 | return 0; |
| 2018 | rst_n_function = card->ext_csd.rst_n_function; |
| 2019 | if ((rst_n_function & EXT_CSD_RST_N_EN_MASK) != EXT_CSD_RST_N_ENABLED) |
| 2020 | return 0; |
| 2021 | return 1; |
| 2022 | } |
| 2023 | EXPORT_SYMBOL(mmc_can_reset); |
| 2024 | |
| 2025 | static int mmc_do_hw_reset(struct mmc_host *host, int check) |
| 2026 | { |
| 2027 | struct mmc_card *card = host->card; |
| 2028 | |
| 2029 | if (!host->bus_ops->power_restore) |
| 2030 | return -EOPNOTSUPP; |
| 2031 | |
| 2032 | if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) |
| 2033 | return -EOPNOTSUPP; |
| 2034 | |
| 2035 | if (!card) |
| 2036 | return -EINVAL; |
| 2037 | |
| 2038 | if (!mmc_can_reset(card)) |
| 2039 | return -EOPNOTSUPP; |
| 2040 | |
| 2041 | mmc_host_clk_hold(host); |
| 2042 | mmc_set_clock(host, host->f_init); |
| 2043 | |
| 2044 | host->ops->hw_reset(host); |
| 2045 | |
| 2046 | /* If the reset has happened, then a status command will fail */ |
| 2047 | if (check) { |
| 2048 | struct mmc_command cmd = {0}; |
| 2049 | int err; |
| 2050 | |
| 2051 | cmd.opcode = MMC_SEND_STATUS; |
| 2052 | if (!mmc_host_is_spi(card->host)) |
| 2053 | cmd.arg = card->rca << 16; |
| 2054 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 2055 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 2056 | if (!err) { |
| 2057 | mmc_host_clk_release(host); |
| 2058 | return -ENOSYS; |
| 2059 | } |
| 2060 | } |
| 2061 | |
| 2062 | host->card->state &= ~(MMC_STATE_HIGHSPEED | MMC_STATE_HIGHSPEED_DDR); |
| 2063 | if (mmc_host_is_spi(host)) { |
| 2064 | host->ios.chip_select = MMC_CS_HIGH; |
| 2065 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
| 2066 | } else { |
| 2067 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 2068 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 2069 | } |
| 2070 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
| 2071 | host->ios.timing = MMC_TIMING_LEGACY; |
| 2072 | mmc_set_ios(host); |
| 2073 | |
| 2074 | mmc_host_clk_release(host); |
| 2075 | |
| 2076 | return host->bus_ops->power_restore(host); |
| 2077 | } |
| 2078 | |
| 2079 | int mmc_hw_reset(struct mmc_host *host) |
| 2080 | { |
| 2081 | return mmc_do_hw_reset(host, 0); |
| 2082 | } |
| 2083 | EXPORT_SYMBOL(mmc_hw_reset); |
| 2084 | |
| 2085 | int mmc_hw_reset_check(struct mmc_host *host) |
| 2086 | { |
| 2087 | return mmc_do_hw_reset(host, 1); |
| 2088 | } |
| 2089 | EXPORT_SYMBOL(mmc_hw_reset_check); |
| 2090 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2091 | static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) |
| 2092 | { |
| 2093 | host->f_init = freq; |
| 2094 | |
| 2095 | #ifdef CONFIG_MMC_DEBUG |
| 2096 | pr_info("%s: %s: trying to init card at %u Hz\n", |
| 2097 | mmc_hostname(host), __func__, host->f_init); |
| 2098 | #endif |
| 2099 | mmc_power_up(host); |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 2100 | |
| 2101 | /* |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 2102 | * Some eMMCs (with VCCQ always on) may not be reset after power up, so |
| 2103 | * do a hardware reset if possible. |
| 2104 | */ |
| 2105 | mmc_hw_reset_for_init(host); |
| 2106 | |
| 2107 | /* |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 2108 | * sdio_reset sends CMD52 to reset card. Since we do not know |
| 2109 | * if the card is being re-initialized, just send it. CMD52 |
| 2110 | * should be ignored by SD/eMMC cards. |
| 2111 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2112 | sdio_reset(host); |
| 2113 | mmc_go_idle(host); |
| 2114 | |
| 2115 | mmc_send_if_cond(host, host->ocr_avail); |
| 2116 | |
| 2117 | /* Order's important: probe SDIO, then SD, then MMC */ |
| 2118 | if (!mmc_attach_sdio(host)) |
| 2119 | return 0; |
| 2120 | if (!mmc_attach_sd(host)) |
| 2121 | return 0; |
| 2122 | if (!mmc_attach_mmc(host)) |
| 2123 | return 0; |
| 2124 | |
| 2125 | mmc_power_off(host); |
| 2126 | return -EIO; |
| 2127 | } |
| 2128 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2129 | int _mmc_detect_card_removed(struct mmc_host *host) |
| 2130 | { |
| 2131 | int ret; |
| 2132 | |
| 2133 | if ((host->caps & MMC_CAP_NONREMOVABLE) || !host->bus_ops->alive) |
| 2134 | return 0; |
| 2135 | |
| 2136 | if (!host->card || mmc_card_removed(host->card)) |
| 2137 | return 1; |
| 2138 | |
| 2139 | ret = host->bus_ops->alive(host); |
| 2140 | if (ret) { |
| 2141 | mmc_card_set_removed(host->card); |
| 2142 | pr_debug("%s: card remove detected\n", mmc_hostname(host)); |
| 2143 | } |
| 2144 | |
| 2145 | return ret; |
| 2146 | } |
| 2147 | |
| 2148 | int mmc_detect_card_removed(struct mmc_host *host) |
| 2149 | { |
| 2150 | struct mmc_card *card = host->card; |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 2151 | int ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2152 | |
| 2153 | WARN_ON(!host->claimed); |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 2154 | |
| 2155 | if (!card) |
| 2156 | return 1; |
| 2157 | |
| 2158 | ret = mmc_card_removed(card); |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2159 | /* |
| 2160 | * The card will be considered unchanged unless we have been asked to |
| 2161 | * detect a change or host requires polling to provide card detection. |
| 2162 | */ |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 2163 | if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL) && |
| 2164 | !(host->caps2 & MMC_CAP2_DETECT_ON_ERR)) |
| 2165 | return ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2166 | |
| 2167 | host->detect_change = 0; |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 2168 | if (!ret) { |
| 2169 | ret = _mmc_detect_card_removed(host); |
| 2170 | if (ret && (host->caps2 & MMC_CAP2_DETECT_ON_ERR)) { |
| 2171 | /* |
| 2172 | * Schedule a detect work as soon as possible to let a |
| 2173 | * rescan handle the card removal. |
| 2174 | */ |
| 2175 | cancel_delayed_work(&host->detect); |
| 2176 | mmc_detect_change(host, 0); |
| 2177 | } |
| 2178 | } |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2179 | |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 2180 | return ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2181 | } |
| 2182 | EXPORT_SYMBOL(mmc_detect_card_removed); |
| 2183 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 2184 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2186 | struct mmc_host *host = |
| 2187 | container_of(work, struct mmc_host, detect.work); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 2188 | int i; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2189 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2190 | if (host->rescan_disable) |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2191 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Johan Rudholm | 3339d1e | 2012-08-23 13:40:55 +0200 | [diff] [blame] | 2193 | /* If there is a non-removable card registered, only scan once */ |
| 2194 | if ((host->caps & MMC_CAP_NONREMOVABLE) && host->rescan_entered) |
| 2195 | return; |
| 2196 | host->rescan_entered = 1; |
| 2197 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2198 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | |
Ohad Ben-Cohen | 30201e7 | 2010-11-28 07:21:28 +0200 | [diff] [blame] | 2200 | /* |
| 2201 | * if there is a _removable_ card registered, check whether it is |
| 2202 | * still present |
| 2203 | */ |
| 2204 | if (host->bus_ops && host->bus_ops->detect && !host->bus_dead |
Ohad Ben-Cohen | bad3bab | 2011-03-08 23:32:02 +0200 | [diff] [blame] | 2205 | && !(host->caps & MMC_CAP_NONREMOVABLE)) |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2206 | host->bus_ops->detect(host); |
| 2207 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 2208 | host->detect_change = 0; |
| 2209 | |
Chris Ball | c584179 | 2011-01-04 12:20:22 -0500 | [diff] [blame] | 2210 | /* |
| 2211 | * Let mmc_bus_put() free the bus/bus_ops if we've found that |
| 2212 | * the card is no longer present. |
| 2213 | */ |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2214 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2215 | mmc_bus_get(host); |
| 2216 | |
| 2217 | /* if there still is a card present, stop here */ |
| 2218 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2219 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2220 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2222 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2223 | /* |
| 2224 | * Only we can add a new handler, so it's safe to |
| 2225 | * release the lock here. |
| 2226 | */ |
| 2227 | mmc_bus_put(host); |
| 2228 | |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 2229 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) { |
| 2230 | mmc_claim_host(host); |
| 2231 | mmc_power_off(host); |
| 2232 | mmc_release_host(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2233 | goto out; |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 2234 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 2235 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2236 | mmc_claim_host(host); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 2237 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2238 | if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) |
| 2239 | break; |
Jaehoon Chung | 06b2233 | 2011-05-12 17:18:59 +0900 | [diff] [blame] | 2240 | if (freqs[i] <= host->f_min) |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2241 | break; |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 2242 | } |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 2243 | mmc_release_host(host); |
| 2244 | |
| 2245 | out: |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 2246 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 2247 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 2250 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | { |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 2252 | host->f_init = max(freqs[0], host->f_min); |
Guennadi Liakhovetski | d9adcc1 | 2012-06-14 10:17:39 +0200 | [diff] [blame] | 2253 | host->rescan_disable = 0; |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 2254 | mmc_power_up(host); |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 2255 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | } |
| 2257 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 2258 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2259 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 2260 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 2261 | unsigned long flags; |
| 2262 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 2263 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 2264 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 2265 | #endif |
| 2266 | |
Guennadi Liakhovetski | d9adcc1 | 2012-06-14 10:17:39 +0200 | [diff] [blame] | 2267 | host->rescan_disable = 1; |
Guennadi Liakhovetski | d9bcbf3 | 2010-11-11 17:32:25 +0100 | [diff] [blame] | 2268 | cancel_delayed_work_sync(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 2269 | mmc_flush_scheduled_work(); |
| 2270 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 2271 | /* clear pm flags now and let card drivers set them as needed */ |
| 2272 | host->pm_flags = 0; |
| 2273 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2274 | mmc_bus_get(host); |
| 2275 | if (host->bus_ops && !host->bus_dead) { |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 2276 | /* Calling bus_ops->remove() with a claimed host can deadlock */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2277 | if (host->bus_ops->remove) |
| 2278 | host->bus_ops->remove(host); |
| 2279 | |
| 2280 | mmc_claim_host(host); |
| 2281 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 2282 | mmc_power_off(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2283 | mmc_release_host(host); |
Denis Karpov | 53509f0 | 2009-09-22 16:44:36 -0700 | [diff] [blame] | 2284 | mmc_bus_put(host); |
| 2285 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2287 | mmc_bus_put(host); |
| 2288 | |
| 2289 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | |
| 2291 | mmc_power_off(host); |
| 2292 | } |
| 2293 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2294 | int mmc_power_save_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2295 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2296 | int ret = 0; |
| 2297 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 2298 | #ifdef CONFIG_MMC_DEBUG |
| 2299 | pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__); |
| 2300 | #endif |
| 2301 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2302 | mmc_bus_get(host); |
| 2303 | |
| 2304 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 2305 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2306 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | if (host->bus_ops->power_save) |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2310 | ret = host->bus_ops->power_save(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2311 | |
| 2312 | mmc_bus_put(host); |
| 2313 | |
| 2314 | mmc_power_off(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2315 | |
| 2316 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2317 | } |
| 2318 | EXPORT_SYMBOL(mmc_power_save_host); |
| 2319 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2320 | int mmc_power_restore_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2321 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2322 | int ret; |
| 2323 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 2324 | #ifdef CONFIG_MMC_DEBUG |
| 2325 | pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__); |
| 2326 | #endif |
| 2327 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2328 | mmc_bus_get(host); |
| 2329 | |
| 2330 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 2331 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2332 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | mmc_power_up(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2336 | ret = host->bus_ops->power_restore(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2337 | |
| 2338 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 2339 | |
| 2340 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 2341 | } |
| 2342 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 2343 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 2344 | int mmc_card_awake(struct mmc_host *host) |
| 2345 | { |
| 2346 | int err = -ENOSYS; |
| 2347 | |
Ulf Hansson | aa9df4f | 2011-12-19 16:24:19 +0100 | [diff] [blame] | 2348 | if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD) |
| 2349 | return 0; |
| 2350 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 2351 | mmc_bus_get(host); |
| 2352 | |
| 2353 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 2354 | err = host->bus_ops->awake(host); |
| 2355 | |
| 2356 | mmc_bus_put(host); |
| 2357 | |
| 2358 | return err; |
| 2359 | } |
| 2360 | EXPORT_SYMBOL(mmc_card_awake); |
| 2361 | |
| 2362 | int mmc_card_sleep(struct mmc_host *host) |
| 2363 | { |
| 2364 | int err = -ENOSYS; |
| 2365 | |
Ulf Hansson | aa9df4f | 2011-12-19 16:24:19 +0100 | [diff] [blame] | 2366 | if (host->caps2 & MMC_CAP2_NO_SLEEP_CMD) |
| 2367 | return 0; |
| 2368 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 2369 | mmc_bus_get(host); |
| 2370 | |
Kyungmin Park | c99872a | 2011-11-17 13:34:33 +0900 | [diff] [blame] | 2371 | if (host->bus_ops && !host->bus_dead && host->bus_ops->sleep) |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 2372 | err = host->bus_ops->sleep(host); |
| 2373 | |
| 2374 | mmc_bus_put(host); |
| 2375 | |
| 2376 | return err; |
| 2377 | } |
| 2378 | EXPORT_SYMBOL(mmc_card_sleep); |
| 2379 | |
| 2380 | int mmc_card_can_sleep(struct mmc_host *host) |
| 2381 | { |
| 2382 | struct mmc_card *card = host->card; |
| 2383 | |
| 2384 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) |
| 2385 | return 1; |
| 2386 | return 0; |
| 2387 | } |
| 2388 | EXPORT_SYMBOL(mmc_card_can_sleep); |
| 2389 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2390 | /* |
| 2391 | * Flush the cache to the non-volatile storage. |
| 2392 | */ |
| 2393 | int mmc_flush_cache(struct mmc_card *card) |
| 2394 | { |
| 2395 | struct mmc_host *host = card->host; |
| 2396 | int err = 0; |
| 2397 | |
| 2398 | if (!(host->caps2 & MMC_CAP2_CACHE_CTRL)) |
| 2399 | return err; |
| 2400 | |
| 2401 | if (mmc_card_mmc(card) && |
| 2402 | (card->ext_csd.cache_size > 0) && |
| 2403 | (card->ext_csd.cache_ctrl & 1)) { |
| 2404 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 2405 | EXT_CSD_FLUSH_CACHE, 1, 0); |
| 2406 | if (err) |
| 2407 | pr_err("%s: cache flush error %d\n", |
| 2408 | mmc_hostname(card->host), err); |
| 2409 | } |
| 2410 | |
| 2411 | return err; |
| 2412 | } |
| 2413 | EXPORT_SYMBOL(mmc_flush_cache); |
| 2414 | |
| 2415 | /* |
| 2416 | * Turn the cache ON/OFF. |
| 2417 | * Turning the cache OFF shall trigger flushing of the data |
| 2418 | * to the non-volatile storage. |
| 2419 | */ |
| 2420 | int mmc_cache_ctrl(struct mmc_host *host, u8 enable) |
| 2421 | { |
| 2422 | struct mmc_card *card = host->card; |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 2423 | unsigned int timeout; |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2424 | int err = 0; |
| 2425 | |
| 2426 | if (!(host->caps2 & MMC_CAP2_CACHE_CTRL) || |
| 2427 | mmc_card_is_removable(host)) |
| 2428 | return err; |
| 2429 | |
Ulf Hansson | 7c57091 | 2012-04-19 11:55:25 +0200 | [diff] [blame] | 2430 | mmc_claim_host(host); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2431 | if (card && mmc_card_mmc(card) && |
| 2432 | (card->ext_csd.cache_size > 0)) { |
| 2433 | enable = !!enable; |
| 2434 | |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 2435 | if (card->ext_csd.cache_ctrl ^ enable) { |
| 2436 | timeout = enable ? card->ext_csd.generic_cmd6_time : 0; |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2437 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Seungwon Jeon | 8bc0678 | 2011-12-09 17:47:17 +0900 | [diff] [blame] | 2438 | EXT_CSD_CACHE_CTRL, enable, timeout); |
| 2439 | if (err) |
| 2440 | pr_err("%s: cache %s error %d\n", |
| 2441 | mmc_hostname(card->host), |
| 2442 | enable ? "on" : "off", |
| 2443 | err); |
| 2444 | else |
| 2445 | card->ext_csd.cache_ctrl = enable; |
| 2446 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2447 | } |
Ulf Hansson | 7c57091 | 2012-04-19 11:55:25 +0200 | [diff] [blame] | 2448 | mmc_release_host(host); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2449 | |
| 2450 | return err; |
| 2451 | } |
| 2452 | EXPORT_SYMBOL(mmc_cache_ctrl); |
| 2453 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2454 | #ifdef CONFIG_PM |
| 2455 | |
| 2456 | /** |
| 2457 | * mmc_suspend_host - suspend a host |
| 2458 | * @host: mmc host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | */ |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 2460 | int mmc_suspend_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2461 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2462 | int err = 0; |
| 2463 | |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 2464 | cancel_delayed_work(&host->detect); |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 2465 | mmc_flush_scheduled_work(); |
Seungwon Jeon | 17e9ff5 | 2011-12-26 18:03:05 +0900 | [diff] [blame] | 2466 | |
Ulf Hansson | 7c57091 | 2012-04-19 11:55:25 +0200 | [diff] [blame] | 2467 | err = mmc_cache_ctrl(host, 0); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2468 | if (err) |
| 2469 | goto out; |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 2470 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2471 | mmc_bus_get(host); |
| 2472 | if (host->bus_ops && !host->bus_dead) { |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 2473 | if (host->bus_ops->suspend) { |
| 2474 | if (mmc_card_doing_bkops(host->card)) { |
| 2475 | err = mmc_stop_bkops(host->card); |
| 2476 | if (err) |
| 2477 | goto out; |
| 2478 | } |
Ulf Hansson | 7c57091 | 2012-04-19 11:55:25 +0200 | [diff] [blame] | 2479 | err = host->bus_ops->suspend(host); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 2480 | } |
Sujit Reddy Thumma | 49df780 | 2011-11-23 08:43:18 +0530 | [diff] [blame] | 2481 | |
Ulf Hansson | 7c57091 | 2012-04-19 11:55:25 +0200 | [diff] [blame] | 2482 | if (err == -ENOSYS || !host->bus_ops->resume) { |
| 2483 | /* |
| 2484 | * We simply "remove" the card in this case. |
| 2485 | * It will be redetected on resume. (Calling |
| 2486 | * bus_ops->remove() with a claimed host can |
| 2487 | * deadlock.) |
| 2488 | */ |
| 2489 | if (host->bus_ops->remove) |
| 2490 | host->bus_ops->remove(host); |
| 2491 | mmc_claim_host(host); |
| 2492 | mmc_detach_bus(host); |
| 2493 | mmc_power_off(host); |
| 2494 | mmc_release_host(host); |
| 2495 | host->pm_flags = 0; |
| 2496 | err = 0; |
Ohad Ben-Cohen | 1c8cf9c | 2010-10-13 09:31:56 +0200 | [diff] [blame] | 2497 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 2498 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2499 | mmc_bus_put(host); |
| 2500 | |
Ohad Ben-Cohen | a5e9425 | 2011-04-05 17:43:20 +0300 | [diff] [blame] | 2501 | if (!err && !mmc_card_keep_power(host)) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2502 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 2504 | out: |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2505 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
| 2508 | EXPORT_SYMBOL(mmc_suspend_host); |
| 2509 | |
| 2510 | /** |
| 2511 | * mmc_resume_host - resume a previously suspended host |
| 2512 | * @host: mmc host |
| 2513 | */ |
| 2514 | int mmc_resume_host(struct mmc_host *host) |
| 2515 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2516 | int err = 0; |
| 2517 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2518 | mmc_bus_get(host); |
| 2519 | if (host->bus_ops && !host->bus_dead) { |
Ohad Ben-Cohen | a5e9425 | 2011-04-05 17:43:20 +0300 | [diff] [blame] | 2520 | if (!mmc_card_keep_power(host)) { |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 2521 | mmc_power_up(host); |
| 2522 | mmc_select_voltage(host, host->ocr); |
Ohad Ben-Cohen | e594573 | 2010-11-28 07:21:30 +0200 | [diff] [blame] | 2523 | /* |
| 2524 | * Tell runtime PM core we just powered up the card, |
| 2525 | * since it still believes the card is powered off. |
| 2526 | * Note that currently runtime PM is only enabled |
| 2527 | * for SDIO cards that are MMC_CAP_POWER_OFF_CARD |
| 2528 | */ |
| 2529 | if (mmc_card_sdio(host->card) && |
| 2530 | (host->caps & MMC_CAP_POWER_OFF_CARD)) { |
| 2531 | pm_runtime_disable(&host->card->dev); |
| 2532 | pm_runtime_set_active(&host->card->dev); |
| 2533 | pm_runtime_enable(&host->card->dev); |
| 2534 | } |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 2535 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2536 | BUG_ON(!host->bus_ops->resume); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2537 | err = host->bus_ops->resume(host); |
| 2538 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2539 | pr_warning("%s: error %d during resume " |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2540 | "(card was removed?)\n", |
| 2541 | mmc_hostname(host), err); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2542 | err = 0; |
| 2543 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2544 | } |
Eliad Peller | a8e6df7 | 2011-05-09 11:32:31 +0300 | [diff] [blame] | 2545 | host->pm_flags &= ~MMC_PM_KEEP_POWER; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2546 | mmc_bus_put(host); |
| 2547 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2548 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | EXPORT_SYMBOL(mmc_resume_host); |
| 2551 | |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2552 | /* Do the card removal on suspend if card is assumed removeable |
| 2553 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
| 2554 | to sync the card. |
| 2555 | */ |
| 2556 | int mmc_pm_notify(struct notifier_block *notify_block, |
| 2557 | unsigned long mode, void *unused) |
| 2558 | { |
| 2559 | struct mmc_host *host = container_of( |
| 2560 | notify_block, struct mmc_host, pm_notify); |
| 2561 | unsigned long flags; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 2562 | int err = 0; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2563 | |
| 2564 | switch (mode) { |
| 2565 | case PM_HIBERNATION_PREPARE: |
| 2566 | case PM_SUSPEND_PREPARE: |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame^] | 2567 | if (host->card && mmc_card_mmc(host->card) && |
| 2568 | mmc_card_doing_bkops(host->card)) { |
| 2569 | err = mmc_stop_bkops(host->card); |
| 2570 | if (err) { |
| 2571 | pr_err("%s: didn't stop bkops\n", |
| 2572 | mmc_hostname(host)); |
| 2573 | return err; |
| 2574 | } |
| 2575 | mmc_card_clr_doing_bkops(host->card); |
| 2576 | } |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2577 | |
| 2578 | spin_lock_irqsave(&host->lock, flags); |
| 2579 | host->rescan_disable = 1; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 2580 | host->power_notify_type = MMC_HOST_PW_NOTIFY_SHORT; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2581 | spin_unlock_irqrestore(&host->lock, flags); |
| 2582 | cancel_delayed_work_sync(&host->detect); |
| 2583 | |
| 2584 | if (!host->bus_ops || host->bus_ops->suspend) |
| 2585 | break; |
| 2586 | |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 2587 | /* Calling bus_ops->remove() with a claimed host can deadlock */ |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2588 | if (host->bus_ops->remove) |
| 2589 | host->bus_ops->remove(host); |
| 2590 | |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 2591 | mmc_claim_host(host); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2592 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 2593 | mmc_power_off(host); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2594 | mmc_release_host(host); |
| 2595 | host->pm_flags = 0; |
| 2596 | break; |
| 2597 | |
| 2598 | case PM_POST_SUSPEND: |
| 2599 | case PM_POST_HIBERNATION: |
Takashi Iwai | 274476f | 2010-12-10 08:40:31 +0100 | [diff] [blame] | 2600 | case PM_POST_RESTORE: |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2601 | |
| 2602 | spin_lock_irqsave(&host->lock, flags); |
| 2603 | host->rescan_disable = 0; |
Girish K S | bec8726 | 2011-10-13 12:04:16 +0530 | [diff] [blame] | 2604 | host->power_notify_type = MMC_HOST_PW_NOTIFY_LONG; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2605 | spin_unlock_irqrestore(&host->lock, flags); |
| 2606 | mmc_detect_change(host, 0); |
| 2607 | |
| 2608 | } |
| 2609 | |
| 2610 | return 0; |
| 2611 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | #endif |
| 2613 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2614 | static int __init mmc_init(void) |
| 2615 | { |
| 2616 | int ret; |
| 2617 | |
Tejun Heo | 0d9ee5b | 2010-12-24 16:00:17 +0100 | [diff] [blame] | 2618 | workqueue = alloc_ordered_workqueue("kmmcd", 0); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2619 | if (!workqueue) |
| 2620 | return -ENOMEM; |
| 2621 | |
| 2622 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 2623 | if (ret) |
| 2624 | goto destroy_workqueue; |
| 2625 | |
| 2626 | ret = mmc_register_host_class(); |
| 2627 | if (ret) |
| 2628 | goto unregister_bus; |
| 2629 | |
| 2630 | ret = sdio_register_bus(); |
| 2631 | if (ret) |
| 2632 | goto unregister_host_class; |
| 2633 | |
| 2634 | return 0; |
| 2635 | |
| 2636 | unregister_host_class: |
| 2637 | mmc_unregister_host_class(); |
| 2638 | unregister_bus: |
| 2639 | mmc_unregister_bus(); |
| 2640 | destroy_workqueue: |
| 2641 | destroy_workqueue(workqueue); |
| 2642 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2643 | return ret; |
| 2644 | } |
| 2645 | |
| 2646 | static void __exit mmc_exit(void) |
| 2647 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 2648 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2649 | mmc_unregister_host_class(); |
| 2650 | mmc_unregister_bus(); |
| 2651 | destroy_workqueue(workqueue); |
| 2652 | } |
| 2653 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 2654 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2655 | module_exit(mmc_exit); |
| 2656 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2657 | MODULE_LICENSE("GPL"); |