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