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 | */ |
| 51 | int use_spi_crc = 1; |
| 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 |
| 61 | int mmc_assume_removable; |
| 62 | #else |
| 63 | int mmc_assume_removable = 1; |
| 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (err && cmd->retries) { |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 144 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 145 | mmc_hostname(host), cmd->opcode, err); |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | cmd->retries--; |
| 148 | cmd->error = 0; |
| 149 | host->ops->request(host, 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; |
| 250 | mmc_start_request(host, mrq); |
| 251 | } |
| 252 | |
| 253 | static void mmc_wait_for_req_done(struct mmc_host *host, |
| 254 | struct mmc_request *mrq) |
| 255 | { |
| 256 | wait_for_completion(&mrq->completion); |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * mmc_pre_req - Prepare for a new request |
| 261 | * @host: MMC host to prepare command |
| 262 | * @mrq: MMC request to prepare for |
| 263 | * @is_first_req: true if there is no previous started request |
| 264 | * that may run in parellel to this call, otherwise false |
| 265 | * |
| 266 | * mmc_pre_req() is called in prior to mmc_start_req() to let |
| 267 | * host prepare for the new request. Preparation of a request may be |
| 268 | * performed while another request is running on the host. |
| 269 | */ |
| 270 | static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq, |
| 271 | bool is_first_req) |
| 272 | { |
| 273 | if (host->ops->pre_req) |
| 274 | host->ops->pre_req(host, mrq, is_first_req); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * mmc_post_req - Post process a completed request |
| 279 | * @host: MMC host to post process command |
| 280 | * @mrq: MMC request to post process for |
| 281 | * @err: Error, if non zero, clean up any resources made in pre_req |
| 282 | * |
| 283 | * Let the host post process a completed request. Post processing of |
| 284 | * a request may be performed while another reuqest is running. |
| 285 | */ |
| 286 | static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq, |
| 287 | int err) |
| 288 | { |
| 289 | if (host->ops->post_req) |
| 290 | host->ops->post_req(host, mrq, err); |
| 291 | } |
| 292 | |
| 293 | /** |
| 294 | * mmc_start_req - start a non-blocking request |
| 295 | * @host: MMC host to start command |
| 296 | * @areq: async request to start |
| 297 | * @error: out parameter returns 0 for success, otherwise non zero |
| 298 | * |
| 299 | * Start a new MMC custom command request for a host. |
| 300 | * If there is on ongoing async request wait for completion |
| 301 | * of that request and start the new one and return. |
| 302 | * Does not wait for the new request to complete. |
| 303 | * |
| 304 | * Returns the completed request, NULL in case of none completed. |
| 305 | * Wait for the an ongoing request (previoulsy started) to complete and |
| 306 | * return the completed request. If there is no ongoing request, NULL |
| 307 | * is returned without waiting. NULL is not an error condition. |
| 308 | */ |
| 309 | struct mmc_async_req *mmc_start_req(struct mmc_host *host, |
| 310 | struct mmc_async_req *areq, int *error) |
| 311 | { |
| 312 | int err = 0; |
| 313 | struct mmc_async_req *data = host->areq; |
| 314 | |
| 315 | /* Prepare a new request */ |
| 316 | if (areq) |
| 317 | mmc_pre_req(host, areq->mrq, !host->areq); |
| 318 | |
| 319 | if (host->areq) { |
| 320 | mmc_wait_for_req_done(host, host->areq->mrq); |
| 321 | err = host->areq->err_check(host->card, host->areq); |
| 322 | if (err) { |
| 323 | mmc_post_req(host, host->areq->mrq, 0); |
| 324 | if (areq) |
| 325 | mmc_post_req(host, areq->mrq, -EINVAL); |
| 326 | |
| 327 | host->areq = NULL; |
| 328 | goto out; |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | if (areq) |
| 333 | __mmc_start_req(host, areq->mrq); |
| 334 | |
| 335 | if (host->areq) |
| 336 | mmc_post_req(host, host->areq->mrq, 0); |
| 337 | |
| 338 | host->areq = areq; |
| 339 | out: |
| 340 | if (error) |
| 341 | *error = err; |
| 342 | return data; |
| 343 | } |
| 344 | EXPORT_SYMBOL(mmc_start_req); |
| 345 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 346 | /** |
| 347 | * mmc_wait_for_req - start a request and wait for completion |
| 348 | * @host: MMC host to start command |
| 349 | * @mrq: MMC request to start |
| 350 | * |
| 351 | * Start a new MMC custom command request for a host, and wait |
| 352 | * for the command to complete. Does not attempt to parse the |
| 353 | * response. |
| 354 | */ |
| 355 | 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] | 356 | { |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 357 | __mmc_start_req(host, mrq); |
| 358 | mmc_wait_for_req_done(host, mrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 361 | |
| 362 | /** |
| 363 | * mmc_wait_for_cmd - start a command and wait for completion |
| 364 | * @host: MMC host to start command |
| 365 | * @cmd: MMC command to start |
| 366 | * @retries: maximum number of retries |
| 367 | * |
| 368 | * Start a new MMC command for a host, and wait for the command |
| 369 | * to complete. Return any error that occurred while the command |
| 370 | * was executing. Do not attempt to parse the response. |
| 371 | */ |
| 372 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 373 | { |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 374 | struct mmc_request mrq = {NULL}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 376 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 379 | cmd->retries = retries; |
| 380 | |
| 381 | mrq.cmd = cmd; |
| 382 | cmd->data = NULL; |
| 383 | |
| 384 | mmc_wait_for_req(host, &mrq); |
| 385 | |
| 386 | return cmd->error; |
| 387 | } |
| 388 | |
| 389 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 390 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 391 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 392 | * mmc_set_data_timeout - set the timeout for a data command |
| 393 | * @data: data phase for command |
| 394 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 395 | * |
| 396 | * Computes the data timeout parameters according to the |
| 397 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 398 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 399 | 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] | 400 | { |
| 401 | unsigned int mult; |
| 402 | |
| 403 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 404 | * SDIO cards only define an upper 1 s limit on access. |
| 405 | */ |
| 406 | if (mmc_card_sdio(card)) { |
| 407 | data->timeout_ns = 1000000000; |
| 408 | data->timeout_clks = 0; |
| 409 | return; |
| 410 | } |
| 411 | |
| 412 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 413 | * SD cards use a 100 multiplier rather than 10 |
| 414 | */ |
| 415 | mult = mmc_card_sd(card) ? 100 : 10; |
| 416 | |
| 417 | /* |
| 418 | * Scale up the multiplier (and therefore the timeout) by |
| 419 | * the r2w factor for writes. |
| 420 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 421 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 422 | mult <<= card->csd.r2w_factor; |
| 423 | |
| 424 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 425 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 426 | |
| 427 | /* |
| 428 | * SD cards also have an upper limit on the timeout. |
| 429 | */ |
| 430 | if (mmc_card_sd(card)) { |
| 431 | unsigned int timeout_us, limit_us; |
| 432 | |
| 433 | timeout_us = data->timeout_ns / 1000; |
Linus Walleij | e9b8684 | 2011-01-05 00:44:32 +0100 | [diff] [blame] | 434 | if (mmc_host_clk_rate(card->host)) |
| 435 | timeout_us += data->timeout_clks * 1000 / |
| 436 | (mmc_host_clk_rate(card->host) / 1000); |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 437 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 438 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 439 | /* |
| 440 | * The limit is really 250 ms, but that is |
| 441 | * insufficient for some crappy cards. |
| 442 | */ |
| 443 | limit_us = 300000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 444 | else |
| 445 | limit_us = 100000; |
| 446 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 447 | /* |
| 448 | * SDHC cards always use these fixed values. |
| 449 | */ |
| 450 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 451 | data->timeout_ns = limit_us * 1000; |
| 452 | data->timeout_clks = 0; |
| 453 | } |
| 454 | } |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 455 | /* |
| 456 | * Some cards need very high timeouts if driven in SPI mode. |
| 457 | * The worst observed timeout was 900ms after writing a |
| 458 | * continuous stream of data until the internal logic |
| 459 | * overflowed. |
| 460 | */ |
| 461 | if (mmc_host_is_spi(card->host)) { |
| 462 | if (data->flags & MMC_DATA_WRITE) { |
| 463 | if (data->timeout_ns < 1000000000) |
| 464 | data->timeout_ns = 1000000000; /* 1s */ |
| 465 | } else { |
| 466 | if (data->timeout_ns < 100000000) |
| 467 | data->timeout_ns = 100000000; /* 100ms */ |
| 468 | } |
| 469 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 470 | } |
| 471 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 472 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 474 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 475 | * @card: the MMC card associated with the data transfer |
| 476 | * @sz: original transfer size |
| 477 | * |
| 478 | * Pads the original data size with a number of extra bytes in |
| 479 | * order to avoid controller bugs and/or performance hits |
| 480 | * (e.g. some controllers revert to PIO for certain sizes). |
| 481 | * |
| 482 | * Returns the improved size, which might be unmodified. |
| 483 | * |
| 484 | * Note that this function is only relevant when issuing a |
| 485 | * single scatter gather entry. |
| 486 | */ |
| 487 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 488 | { |
| 489 | /* |
| 490 | * FIXME: We don't have a system for the controller to tell |
| 491 | * the core about its problems yet, so for now we just 32-bit |
| 492 | * align the size. |
| 493 | */ |
| 494 | sz = ((sz + 3) / 4) * 4; |
| 495 | |
| 496 | return sz; |
| 497 | } |
| 498 | EXPORT_SYMBOL(mmc_align_data_size); |
| 499 | |
| 500 | /** |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 501 | * mmc_host_enable - enable a host. |
| 502 | * @host: mmc host to enable |
| 503 | * |
| 504 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 505 | * methods to exit and enter power saving states. For more information |
| 506 | * see comments for struct mmc_host_ops. |
| 507 | */ |
| 508 | int mmc_host_enable(struct mmc_host *host) |
| 509 | { |
| 510 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 511 | return 0; |
| 512 | |
| 513 | if (host->en_dis_recurs) |
| 514 | return 0; |
| 515 | |
| 516 | if (host->nesting_cnt++) |
| 517 | return 0; |
| 518 | |
| 519 | cancel_delayed_work_sync(&host->disable); |
| 520 | |
| 521 | if (host->enabled) |
| 522 | return 0; |
| 523 | |
| 524 | if (host->ops->enable) { |
| 525 | int err; |
| 526 | |
| 527 | host->en_dis_recurs = 1; |
| 528 | err = host->ops->enable(host); |
| 529 | host->en_dis_recurs = 0; |
| 530 | |
| 531 | if (err) { |
| 532 | pr_debug("%s: enable error %d\n", |
| 533 | mmc_hostname(host), err); |
| 534 | return err; |
| 535 | } |
| 536 | } |
| 537 | host->enabled = 1; |
| 538 | return 0; |
| 539 | } |
| 540 | EXPORT_SYMBOL(mmc_host_enable); |
| 541 | |
| 542 | static int mmc_host_do_disable(struct mmc_host *host, int lazy) |
| 543 | { |
| 544 | if (host->ops->disable) { |
| 545 | int err; |
| 546 | |
| 547 | host->en_dis_recurs = 1; |
| 548 | err = host->ops->disable(host, lazy); |
| 549 | host->en_dis_recurs = 0; |
| 550 | |
| 551 | if (err < 0) { |
| 552 | pr_debug("%s: disable error %d\n", |
| 553 | mmc_hostname(host), err); |
| 554 | return err; |
| 555 | } |
| 556 | if (err > 0) { |
| 557 | unsigned long delay = msecs_to_jiffies(err); |
| 558 | |
| 559 | mmc_schedule_delayed_work(&host->disable, delay); |
| 560 | } |
| 561 | } |
| 562 | host->enabled = 0; |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | /** |
| 567 | * mmc_host_disable - disable a host. |
| 568 | * @host: mmc host to disable |
| 569 | * |
| 570 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 571 | * methods to exit and enter power saving states. For more information |
| 572 | * see comments for struct mmc_host_ops. |
| 573 | */ |
| 574 | int mmc_host_disable(struct mmc_host *host) |
| 575 | { |
| 576 | int err; |
| 577 | |
| 578 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 579 | return 0; |
| 580 | |
| 581 | if (host->en_dis_recurs) |
| 582 | return 0; |
| 583 | |
| 584 | if (--host->nesting_cnt) |
| 585 | return 0; |
| 586 | |
| 587 | if (!host->enabled) |
| 588 | return 0; |
| 589 | |
| 590 | err = mmc_host_do_disable(host, 0); |
| 591 | return err; |
| 592 | } |
| 593 | EXPORT_SYMBOL(mmc_host_disable); |
| 594 | |
| 595 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 596 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 598 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 600 | * Claim a host for a set of operations. If @abort is non null and |
| 601 | * dereference a non-zero value then this will return prematurely with |
| 602 | * that non-zero value without acquiring the lock. Returns zero |
| 603 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 605 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | DECLARE_WAITQUEUE(wait, current); |
| 608 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 609 | int stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 611 | might_sleep(); |
| 612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | add_wait_queue(&host->wq, &wait); |
| 614 | spin_lock_irqsave(&host->lock, flags); |
| 615 | while (1) { |
| 616 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 617 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 618 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | break; |
| 620 | spin_unlock_irqrestore(&host->lock, flags); |
| 621 | schedule(); |
| 622 | spin_lock_irqsave(&host->lock, flags); |
| 623 | } |
| 624 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 625 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 626 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 627 | host->claimer = current; |
| 628 | host->claim_cnt += 1; |
| 629 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 630 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | spin_unlock_irqrestore(&host->lock, flags); |
| 632 | remove_wait_queue(&host->wq, &wait); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 633 | if (!stop) |
| 634 | mmc_host_enable(host); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 635 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 638 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 640 | /** |
| 641 | * mmc_try_claim_host - try exclusively to claim a host |
| 642 | * @host: mmc host to claim |
| 643 | * |
| 644 | * Returns %1 if the host is claimed, %0 otherwise. |
| 645 | */ |
| 646 | int mmc_try_claim_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 647 | { |
| 648 | int claimed_host = 0; |
| 649 | unsigned long flags; |
| 650 | |
| 651 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 652 | if (!host->claimed || host->claimer == current) { |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 653 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 654 | host->claimer = current; |
| 655 | host->claim_cnt += 1; |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 656 | claimed_host = 1; |
| 657 | } |
| 658 | spin_unlock_irqrestore(&host->lock, flags); |
| 659 | return claimed_host; |
| 660 | } |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 661 | EXPORT_SYMBOL(mmc_try_claim_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 662 | |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 663 | /** |
| 664 | * mmc_do_release_host - release a claimed host |
| 665 | * @host: mmc host to release |
| 666 | * |
| 667 | * If you successfully claimed a host, this function will |
| 668 | * release it again. |
| 669 | */ |
| 670 | void mmc_do_release_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 671 | { |
| 672 | unsigned long flags; |
| 673 | |
| 674 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 675 | if (--host->claim_cnt) { |
| 676 | /* Release for nested claim */ |
| 677 | spin_unlock_irqrestore(&host->lock, flags); |
| 678 | } else { |
| 679 | host->claimed = 0; |
| 680 | host->claimer = NULL; |
| 681 | spin_unlock_irqrestore(&host->lock, flags); |
| 682 | wake_up(&host->wq); |
| 683 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 684 | } |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 685 | EXPORT_SYMBOL(mmc_do_release_host); |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 686 | |
| 687 | void mmc_host_deeper_disable(struct work_struct *work) |
| 688 | { |
| 689 | struct mmc_host *host = |
| 690 | container_of(work, struct mmc_host, disable.work); |
| 691 | |
| 692 | /* If the host is claimed then we do not want to disable it anymore */ |
| 693 | if (!mmc_try_claim_host(host)) |
| 694 | return; |
| 695 | mmc_host_do_disable(host, 1); |
| 696 | mmc_do_release_host(host); |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * mmc_host_lazy_disable - lazily disable a host. |
| 701 | * @host: mmc host to disable |
| 702 | * |
| 703 | * Hosts that support power saving can use the 'enable' and 'disable' |
| 704 | * methods to exit and enter power saving states. For more information |
| 705 | * see comments for struct mmc_host_ops. |
| 706 | */ |
| 707 | int mmc_host_lazy_disable(struct mmc_host *host) |
| 708 | { |
| 709 | if (!(host->caps & MMC_CAP_DISABLE)) |
| 710 | return 0; |
| 711 | |
| 712 | if (host->en_dis_recurs) |
| 713 | return 0; |
| 714 | |
| 715 | if (--host->nesting_cnt) |
| 716 | return 0; |
| 717 | |
| 718 | if (!host->enabled) |
| 719 | return 0; |
| 720 | |
| 721 | if (host->disable_delay) { |
| 722 | mmc_schedule_delayed_work(&host->disable, |
| 723 | msecs_to_jiffies(host->disable_delay)); |
| 724 | return 0; |
| 725 | } else |
| 726 | return mmc_host_do_disable(host, 1); |
| 727 | } |
| 728 | EXPORT_SYMBOL(mmc_host_lazy_disable); |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | /** |
| 731 | * mmc_release_host - release a host |
| 732 | * @host: mmc host to release |
| 733 | * |
| 734 | * Release a MMC host, allowing others to claim the host |
| 735 | * for their operations. |
| 736 | */ |
| 737 | void mmc_release_host(struct mmc_host *host) |
| 738 | { |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 739 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 740 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 741 | mmc_host_lazy_disable(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 743 | mmc_do_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | EXPORT_SYMBOL(mmc_release_host); |
| 747 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 748 | /* |
| 749 | * Internal function that does the actual ios call to the host driver, |
| 750 | * optionally printing some debug output. |
| 751 | */ |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 752 | static inline void mmc_set_ios(struct mmc_host *host) |
| 753 | { |
| 754 | struct mmc_ios *ios = &host->ios; |
| 755 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 756 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 757 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 758 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 759 | ios->power_mode, ios->chip_select, ios->vdd, |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 760 | ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 761 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 762 | if (ios->clock > 0) |
| 763 | mmc_set_ungated(host); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 764 | host->ops->set_ios(host, ios); |
| 765 | } |
| 766 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 767 | /* |
| 768 | * Control chip select pin on a host. |
| 769 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 770 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 771 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 772 | mmc_host_clk_hold(host); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 773 | host->ios.chip_select = mode; |
| 774 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 775 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | } |
| 777 | |
| 778 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 779 | * Sets the host clock to the highest possible frequency that |
| 780 | * is below "hz". |
| 781 | */ |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 782 | static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 783 | { |
| 784 | WARN_ON(hz < host->f_min); |
| 785 | |
| 786 | if (hz > host->f_max) |
| 787 | hz = host->f_max; |
| 788 | |
| 789 | host->ios.clock = hz; |
| 790 | mmc_set_ios(host); |
| 791 | } |
| 792 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 793 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 794 | { |
| 795 | mmc_host_clk_hold(host); |
| 796 | __mmc_set_clock(host, hz); |
| 797 | mmc_host_clk_release(host); |
| 798 | } |
| 799 | |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 800 | #ifdef CONFIG_MMC_CLKGATE |
| 801 | /* |
| 802 | * This gates the clock by setting it to 0 Hz. |
| 803 | */ |
| 804 | void mmc_gate_clock(struct mmc_host *host) |
| 805 | { |
| 806 | unsigned long flags; |
| 807 | |
| 808 | spin_lock_irqsave(&host->clk_lock, flags); |
| 809 | host->clk_old = host->ios.clock; |
| 810 | host->ios.clock = 0; |
| 811 | host->clk_gated = true; |
| 812 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 813 | mmc_set_ios(host); |
| 814 | } |
| 815 | |
| 816 | /* |
| 817 | * This restores the clock from gating by using the cached |
| 818 | * clock value. |
| 819 | */ |
| 820 | void mmc_ungate_clock(struct mmc_host *host) |
| 821 | { |
| 822 | /* |
| 823 | * We should previously have gated the clock, so the clock shall |
| 824 | * be 0 here! The clock may however be 0 during initialization, |
| 825 | * when some request operations are performed before setting |
| 826 | * the frequency. When ungate is requested in that situation |
| 827 | * we just ignore the call. |
| 828 | */ |
| 829 | if (host->clk_old) { |
| 830 | BUG_ON(host->ios.clock); |
| 831 | /* This call will also set host->clk_gated to false */ |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 832 | __mmc_set_clock(host, host->clk_old); |
Linus Walleij | 0456683 | 2010-11-08 21:36:50 -0500 | [diff] [blame] | 833 | } |
| 834 | } |
| 835 | |
| 836 | void mmc_set_ungated(struct mmc_host *host) |
| 837 | { |
| 838 | unsigned long flags; |
| 839 | |
| 840 | /* |
| 841 | * We've been given a new frequency while the clock is gated, |
| 842 | * so make sure we regard this as ungating it. |
| 843 | */ |
| 844 | spin_lock_irqsave(&host->clk_lock, flags); |
| 845 | host->clk_gated = false; |
| 846 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 847 | } |
| 848 | |
| 849 | #else |
| 850 | void mmc_set_ungated(struct mmc_host *host) |
| 851 | { |
| 852 | } |
| 853 | #endif |
| 854 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 855 | /* |
| 856 | * Change the bus mode (open drain/push-pull) of a host. |
| 857 | */ |
| 858 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 859 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 860 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 861 | host->ios.bus_mode = mode; |
| 862 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 863 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | /* |
| 867 | * Change data bus width of a host. |
| 868 | */ |
| 869 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 870 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 871 | mmc_host_clk_hold(host); |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 872 | host->ios.bus_width = width; |
| 873 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 874 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 877 | /** |
| 878 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 879 | * @vdd: voltage (mV) |
| 880 | * @low_bits: prefer low bits in boundary cases |
| 881 | * |
| 882 | * This function returns the OCR bit number according to the provided @vdd |
| 883 | * value. If conversion is not possible a negative errno value returned. |
| 884 | * |
| 885 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 886 | * on boundary voltages. For example, |
| 887 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 888 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 889 | * |
| 890 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 891 | */ |
| 892 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 893 | { |
| 894 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 895 | int bit; |
| 896 | |
| 897 | if (vdd < 1650 || vdd > 3600) |
| 898 | return -EINVAL; |
| 899 | |
| 900 | if (vdd >= 1650 && vdd <= 1950) |
| 901 | return ilog2(MMC_VDD_165_195); |
| 902 | |
| 903 | if (low_bits) |
| 904 | vdd -= 1; |
| 905 | |
| 906 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 907 | bit = (vdd - 2000) / 100 + 8; |
| 908 | if (bit > max_bit) |
| 909 | return max_bit; |
| 910 | return bit; |
| 911 | } |
| 912 | |
| 913 | /** |
| 914 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 915 | * @vdd_min: minimum voltage value (mV) |
| 916 | * @vdd_max: maximum voltage value (mV) |
| 917 | * |
| 918 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 919 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 920 | * |
| 921 | * Notes wrt boundary cases: |
| 922 | * This function sets the OCR bits for all boundary voltages, for example |
| 923 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 924 | * MMC_VDD_34_35 mask. |
| 925 | */ |
| 926 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 927 | { |
| 928 | u32 mask = 0; |
| 929 | |
| 930 | if (vdd_max < vdd_min) |
| 931 | return 0; |
| 932 | |
| 933 | /* Prefer high bits for the boundary vdd_max values. */ |
| 934 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 935 | if (vdd_max < 0) |
| 936 | return 0; |
| 937 | |
| 938 | /* Prefer low bits for the boundary vdd_min values. */ |
| 939 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 940 | if (vdd_min < 0) |
| 941 | return 0; |
| 942 | |
| 943 | /* Fill the mask, from max bit to min bit. */ |
| 944 | while (vdd_max >= vdd_min) |
| 945 | mask |= 1 << vdd_max--; |
| 946 | |
| 947 | return mask; |
| 948 | } |
| 949 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 950 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 951 | #ifdef CONFIG_REGULATOR |
| 952 | |
| 953 | /** |
| 954 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 955 | * @supply: regulator to use |
| 956 | * |
| 957 | * This returns either a negative errno, or a mask of voltages that |
| 958 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 959 | * regulator. This would normally be called before registering the |
| 960 | * MMC host adapter. |
| 961 | */ |
| 962 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 963 | { |
| 964 | int result = 0; |
| 965 | int count; |
| 966 | int i; |
| 967 | |
| 968 | count = regulator_count_voltages(supply); |
| 969 | if (count < 0) |
| 970 | return count; |
| 971 | |
| 972 | for (i = 0; i < count; i++) { |
| 973 | int vdd_uV; |
| 974 | int vdd_mV; |
| 975 | |
| 976 | vdd_uV = regulator_list_voltage(supply, i); |
| 977 | if (vdd_uV <= 0) |
| 978 | continue; |
| 979 | |
| 980 | vdd_mV = vdd_uV / 1000; |
| 981 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 982 | } |
| 983 | |
| 984 | return result; |
| 985 | } |
| 986 | EXPORT_SYMBOL(mmc_regulator_get_ocrmask); |
| 987 | |
| 988 | /** |
| 989 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 990 | * @mmc: the host to regulate |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 991 | * @supply: regulator to use |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 992 | * @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] | 993 | * |
| 994 | * Returns zero on success, else negative errno. |
| 995 | * |
| 996 | * MMC host drivers may use this to enable or disable a regulator using |
| 997 | * a particular supply voltage. This would normally be called from the |
| 998 | * set_ios() method. |
| 999 | */ |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1000 | int mmc_regulator_set_ocr(struct mmc_host *mmc, |
| 1001 | struct regulator *supply, |
| 1002 | unsigned short vdd_bit) |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1003 | { |
| 1004 | int result = 0; |
| 1005 | int min_uV, max_uV; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1006 | |
| 1007 | if (vdd_bit) { |
| 1008 | int tmp; |
| 1009 | int voltage; |
| 1010 | |
| 1011 | /* REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 1012 | * bits this regulator doesn't quite support ... don't |
| 1013 | * be too picky, most cards and regulators are OK with |
| 1014 | * a 0.1V range goof (it's a small error percentage). |
| 1015 | */ |
| 1016 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 1017 | if (tmp == 0) { |
| 1018 | min_uV = 1650 * 1000; |
| 1019 | max_uV = 1950 * 1000; |
| 1020 | } else { |
| 1021 | min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 1022 | max_uV = min_uV + 100 * 1000; |
| 1023 | } |
| 1024 | |
| 1025 | /* avoid needless changes to this voltage; the regulator |
| 1026 | * might not allow this operation |
| 1027 | */ |
| 1028 | voltage = regulator_get_voltage(supply); |
| 1029 | if (voltage < 0) |
| 1030 | result = voltage; |
| 1031 | else if (voltage < min_uV || voltage > max_uV) |
| 1032 | result = regulator_set_voltage(supply, min_uV, max_uV); |
| 1033 | else |
| 1034 | result = 0; |
| 1035 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1036 | if (result == 0 && !mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1037 | result = regulator_enable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1038 | if (!result) |
| 1039 | mmc->regulator_enabled = true; |
| 1040 | } |
| 1041 | } else if (mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1042 | result = regulator_disable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1043 | if (result == 0) |
| 1044 | mmc->regulator_enabled = false; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1045 | } |
| 1046 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1047 | if (result) |
| 1048 | dev_err(mmc_dev(mmc), |
| 1049 | "could not set regulator OCR (%d)\n", result); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1050 | return result; |
| 1051 | } |
| 1052 | EXPORT_SYMBOL(mmc_regulator_set_ocr); |
| 1053 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 1054 | #endif /* CONFIG_REGULATOR */ |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 1055 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1056 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | * Mask off any voltages we don't support and select |
| 1058 | * the lowest voltage |
| 1059 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1060 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1061 | { |
| 1062 | int bit; |
| 1063 | |
| 1064 | ocr &= host->ocr_avail; |
| 1065 | |
| 1066 | bit = ffs(ocr); |
| 1067 | if (bit) { |
| 1068 | bit -= 1; |
| 1069 | |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 1070 | ocr &= 3 << bit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1072 | mmc_host_clk_hold(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1073 | host->ios.vdd = bit; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1074 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1075 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } else { |
David Brownell | f6e10b8 | 2008-12-31 09:50:30 -0800 | [diff] [blame] | 1077 | pr_warning("%s: host doesn't support card's voltages\n", |
| 1078 | mmc_hostname(host)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | ocr = 0; |
| 1080 | } |
| 1081 | |
| 1082 | return ocr; |
| 1083 | } |
| 1084 | |
Philip Rakity | 261bbd4 | 2011-05-13 11:17:17 +0530 | [diff] [blame] | 1085 | 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] | 1086 | { |
| 1087 | struct mmc_command cmd = {0}; |
| 1088 | int err = 0; |
| 1089 | |
| 1090 | BUG_ON(!host); |
| 1091 | |
| 1092 | /* |
| 1093 | * Send CMD11 only if the request is to switch the card to |
| 1094 | * 1.8V signalling. |
| 1095 | */ |
Philip Rakity | 261bbd4 | 2011-05-13 11:17:17 +0530 | [diff] [blame] | 1096 | if ((signal_voltage != MMC_SIGNAL_VOLTAGE_330) && cmd11) { |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 1097 | cmd.opcode = SD_SWITCH_VOLTAGE; |
| 1098 | cmd.arg = 0; |
| 1099 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1100 | |
| 1101 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 1102 | if (err) |
| 1103 | return err; |
| 1104 | |
| 1105 | if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) |
| 1106 | return -EIO; |
| 1107 | } |
| 1108 | |
| 1109 | host->ios.signal_voltage = signal_voltage; |
| 1110 | |
| 1111 | if (host->ops->start_signal_voltage_switch) |
| 1112 | err = host->ops->start_signal_voltage_switch(host, &host->ios); |
| 1113 | |
| 1114 | return err; |
| 1115 | } |
| 1116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1118 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1119 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1120 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1122 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1123 | host->ios.timing = timing; |
| 1124 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1125 | mmc_host_clk_release(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | /* |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1129 | * Select appropriate driver type for host. |
| 1130 | */ |
| 1131 | void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) |
| 1132 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1133 | mmc_host_clk_hold(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1134 | host->ios.drv_type = drv_type; |
| 1135 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1136 | mmc_host_clk_release(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 1140 | * Apply power to the MMC stack. This is a two-stage process. |
| 1141 | * First, we enable power to the card without the clock running. |
| 1142 | * We then wait a bit for the power to stabilise. Finally, |
| 1143 | * enable the bus drivers and clock to the card. |
| 1144 | * |
| 1145 | * We must _NOT_ enable the clock prior to power stablising. |
| 1146 | * |
| 1147 | * If a host does all the power sequencing itself, ignore the |
| 1148 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | */ |
| 1150 | static void mmc_power_up(struct mmc_host *host) |
| 1151 | { |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 1152 | int bit; |
| 1153 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1154 | mmc_host_clk_hold(host); |
| 1155 | |
Balaji Rao | 500f356 | 2009-09-22 16:44:18 -0700 | [diff] [blame] | 1156 | /* If ocr is set, we use it */ |
| 1157 | if (host->ocr) |
| 1158 | bit = ffs(host->ocr) - 1; |
| 1159 | else |
| 1160 | bit = fls(host->ocr_avail) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | |
| 1162 | host->ios.vdd = bit; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame^] | 1163 | if (mmc_host_is_spi(host)) |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1164 | host->ios.chip_select = MMC_CS_HIGH; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame^] | 1165 | else |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1166 | host->ios.chip_select = MMC_CS_DONTCARE; |
Stefan Nilsson XK | 4466903 | 2011-09-15 17:50:38 +0200 | [diff] [blame^] | 1167 | host->ios.bus_mode = MMC_BUSMODE_PUSHPULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | host->ios.power_mode = MMC_POWER_UP; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 1169 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1170 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1171 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 1173 | /* |
| 1174 | * This delay should be sufficient to allow the power supply |
| 1175 | * to reach the minimum voltage. |
| 1176 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 1177 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1179 | host->ios.clock = host->f_init; |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 1180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1181 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1182 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 1184 | /* |
| 1185 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 1186 | * time required to reach a stable voltage. |
| 1187 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 1188 | mmc_delay(10); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1189 | |
| 1190 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | } |
| 1192 | |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1193 | void mmc_power_off(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | { |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1195 | mmc_host_clk_hold(host); |
| 1196 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | host->ios.clock = 0; |
| 1198 | host->ios.vdd = 0; |
Ulf Hansson | b33d46c | 2011-03-05 14:36:24 +0100 | [diff] [blame] | 1199 | |
| 1200 | /* |
| 1201 | * Reset ocr mask to be the highest possible voltage supported for |
| 1202 | * this mmc host. This value will be used at next power up. |
| 1203 | */ |
| 1204 | host->ocr = 1 << (fls(host->ocr_avail) - 1); |
| 1205 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 1206 | if (!mmc_host_is_spi(host)) { |
| 1207 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 1208 | host->ios.chip_select = MMC_CS_DONTCARE; |
| 1209 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | host->ios.power_mode = MMC_POWER_OFF; |
Pierre Ossman | f218278 | 2005-09-06 15:18:55 -0700 | [diff] [blame] | 1211 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 1212 | host->ios.timing = MMC_TIMING_LEGACY; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1213 | mmc_set_ios(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1214 | |
Daniel Drake | 041beb1 | 2011-09-07 10:22:09 +0100 | [diff] [blame] | 1215 | /* |
| 1216 | * Some configurations, such as the 802.11 SDIO card in the OLPC |
| 1217 | * XO-1.5, require a short delay after poweroff before the card |
| 1218 | * can be successfully turned on again. |
| 1219 | */ |
| 1220 | mmc_delay(1); |
| 1221 | |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 1222 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1226 | * Cleanup when the last reference to the bus operator is dropped. |
| 1227 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 1228 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 1229 | { |
| 1230 | BUG_ON(!host); |
| 1231 | BUG_ON(host->bus_refs); |
| 1232 | BUG_ON(!host->bus_dead); |
| 1233 | |
| 1234 | host->bus_ops = NULL; |
| 1235 | } |
| 1236 | |
| 1237 | /* |
| 1238 | * Increase reference count of bus operator |
| 1239 | */ |
| 1240 | static inline void mmc_bus_get(struct mmc_host *host) |
| 1241 | { |
| 1242 | unsigned long flags; |
| 1243 | |
| 1244 | spin_lock_irqsave(&host->lock, flags); |
| 1245 | host->bus_refs++; |
| 1246 | spin_unlock_irqrestore(&host->lock, flags); |
| 1247 | } |
| 1248 | |
| 1249 | /* |
| 1250 | * Decrease reference count of bus operator and free it if |
| 1251 | * it is the last reference. |
| 1252 | */ |
| 1253 | static inline void mmc_bus_put(struct mmc_host *host) |
| 1254 | { |
| 1255 | unsigned long flags; |
| 1256 | |
| 1257 | spin_lock_irqsave(&host->lock, flags); |
| 1258 | host->bus_refs--; |
| 1259 | if ((host->bus_refs == 0) && host->bus_ops) |
| 1260 | __mmc_release_bus(host); |
| 1261 | spin_unlock_irqrestore(&host->lock, flags); |
| 1262 | } |
| 1263 | |
| 1264 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1265 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 1266 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1268 | 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] | 1269 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1270 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1272 | BUG_ON(!host); |
| 1273 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1274 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1275 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1276 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1277 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1278 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1279 | BUG_ON(host->bus_ops); |
| 1280 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 1281 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1282 | host->bus_ops = ops; |
| 1283 | host->bus_refs = 1; |
| 1284 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1286 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
| 1289 | /* |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1290 | * Remove the current bus handler from a host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1292 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1294 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1296 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1298 | WARN_ON(!host->claimed); |
| 1299 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1301 | spin_lock_irqsave(&host->lock, flags); |
| 1302 | |
| 1303 | host->bus_dead = 1; |
| 1304 | |
| 1305 | spin_unlock_irqrestore(&host->lock, flags); |
| 1306 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1307 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1310 | /** |
| 1311 | * mmc_detect_change - process change of state on a MMC socket |
| 1312 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1313 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1315 | * MMC drivers should call this when they detect a card has been |
| 1316 | * inserted or removed. The MMC layer will confirm that any |
| 1317 | * present card is still functional, and initialize any newly |
| 1318 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 1320 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1322 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1323 | unsigned long flags; |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1324 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 1325 | WARN_ON(host->removed); |
Andrew Morton | 01f41ec | 2007-05-09 02:32:34 -0700 | [diff] [blame] | 1326 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1327 | #endif |
| 1328 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1329 | mmc_schedule_delayed_work(&host->detect, delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | EXPORT_SYMBOL(mmc_detect_change); |
| 1333 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1334 | void mmc_init_erase(struct mmc_card *card) |
| 1335 | { |
| 1336 | unsigned int sz; |
| 1337 | |
| 1338 | if (is_power_of_2(card->erase_size)) |
| 1339 | card->erase_shift = ffs(card->erase_size) - 1; |
| 1340 | else |
| 1341 | card->erase_shift = 0; |
| 1342 | |
| 1343 | /* |
| 1344 | * It is possible to erase an arbitrarily large area of an SD or MMC |
| 1345 | * card. That is not desirable because it can take a long time |
| 1346 | * (minutes) potentially delaying more important I/O, and also the |
| 1347 | * timeout calculations become increasingly hugely over-estimated. |
| 1348 | * Consequently, 'pref_erase' is defined as a guide to limit erases |
| 1349 | * to that size and alignment. |
| 1350 | * |
| 1351 | * For SD cards that define Allocation Unit size, limit erases to one |
| 1352 | * Allocation Unit at a time. For MMC cards that define High Capacity |
| 1353 | * Erase Size, whether it is switched on or not, limit to that size. |
| 1354 | * Otherwise just have a stab at a good value. For modern cards it |
| 1355 | * will end up being 4MiB. Note that if the value is too small, it |
| 1356 | * can end up taking longer to erase. |
| 1357 | */ |
| 1358 | if (mmc_card_sd(card) && card->ssr.au) { |
| 1359 | card->pref_erase = card->ssr.au; |
| 1360 | card->erase_shift = ffs(card->ssr.au) - 1; |
| 1361 | } else if (card->ext_csd.hc_erase_size) { |
| 1362 | card->pref_erase = card->ext_csd.hc_erase_size; |
| 1363 | } else { |
| 1364 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 1365 | if (sz < 128) |
| 1366 | card->pref_erase = 512 * 1024 / 512; |
| 1367 | else if (sz < 512) |
| 1368 | card->pref_erase = 1024 * 1024 / 512; |
| 1369 | else if (sz < 1024) |
| 1370 | card->pref_erase = 2 * 1024 * 1024 / 512; |
| 1371 | else |
| 1372 | card->pref_erase = 4 * 1024 * 1024 / 512; |
| 1373 | if (card->pref_erase < card->erase_size) |
| 1374 | card->pref_erase = card->erase_size; |
| 1375 | else { |
| 1376 | sz = card->pref_erase % card->erase_size; |
| 1377 | if (sz) |
| 1378 | card->pref_erase += card->erase_size - sz; |
| 1379 | } |
| 1380 | } |
| 1381 | } |
| 1382 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1383 | static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, |
| 1384 | unsigned int arg, unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1385 | { |
| 1386 | unsigned int erase_timeout; |
| 1387 | |
| 1388 | if (card->ext_csd.erase_group_def & 1) { |
| 1389 | /* High Capacity Erase Group Size uses HC timeouts */ |
| 1390 | if (arg == MMC_TRIM_ARG) |
| 1391 | erase_timeout = card->ext_csd.trim_timeout; |
| 1392 | else |
| 1393 | erase_timeout = card->ext_csd.hc_erase_timeout; |
| 1394 | } else { |
| 1395 | /* CSD Erase Group Size uses write timeout */ |
| 1396 | unsigned int mult = (10 << card->csd.r2w_factor); |
| 1397 | unsigned int timeout_clks = card->csd.tacc_clks * mult; |
| 1398 | unsigned int timeout_us; |
| 1399 | |
| 1400 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ |
| 1401 | if (card->csd.tacc_ns < 1000000) |
| 1402 | timeout_us = (card->csd.tacc_ns * mult) / 1000; |
| 1403 | else |
| 1404 | timeout_us = (card->csd.tacc_ns / 1000) * mult; |
| 1405 | |
| 1406 | /* |
| 1407 | * ios.clock is only a target. The real clock rate might be |
| 1408 | * less but not that much less, so fudge it by multiplying by 2. |
| 1409 | */ |
| 1410 | timeout_clks <<= 1; |
| 1411 | timeout_us += (timeout_clks * 1000) / |
Adrian Hunter | 4cf8c6d | 2011-06-23 13:40:27 +0300 | [diff] [blame] | 1412 | (mmc_host_clk_rate(card->host) / 1000); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1413 | |
| 1414 | erase_timeout = timeout_us / 1000; |
| 1415 | |
| 1416 | /* |
| 1417 | * Theoretically, the calculation could underflow so round up |
| 1418 | * to 1ms in that case. |
| 1419 | */ |
| 1420 | if (!erase_timeout) |
| 1421 | erase_timeout = 1; |
| 1422 | } |
| 1423 | |
| 1424 | /* Multiplier for secure operations */ |
| 1425 | if (arg & MMC_SECURE_ARGS) { |
| 1426 | if (arg == MMC_SECURE_ERASE_ARG) |
| 1427 | erase_timeout *= card->ext_csd.sec_erase_mult; |
| 1428 | else |
| 1429 | erase_timeout *= card->ext_csd.sec_trim_mult; |
| 1430 | } |
| 1431 | |
| 1432 | erase_timeout *= qty; |
| 1433 | |
| 1434 | /* |
| 1435 | * Ensure at least a 1 second timeout for SPI as per |
| 1436 | * 'mmc_set_data_timeout()' |
| 1437 | */ |
| 1438 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 1439 | erase_timeout = 1000; |
| 1440 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1441 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1442 | } |
| 1443 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1444 | static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, |
| 1445 | unsigned int arg, |
| 1446 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1447 | { |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1448 | unsigned int erase_timeout; |
| 1449 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1450 | if (card->ssr.erase_timeout) { |
| 1451 | /* Erase timeout specified in SD Status Register (SSR) */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1452 | erase_timeout = card->ssr.erase_timeout * qty + |
| 1453 | card->ssr.erase_offset; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1454 | } else { |
| 1455 | /* |
| 1456 | * Erase timeout not specified in SD Status Register (SSR) so |
| 1457 | * use 250ms per write block. |
| 1458 | */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1459 | erase_timeout = 250 * qty; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | /* Must not be less than 1 second */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1463 | if (erase_timeout < 1000) |
| 1464 | erase_timeout = 1000; |
| 1465 | |
| 1466 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1467 | } |
| 1468 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1469 | static unsigned int mmc_erase_timeout(struct mmc_card *card, |
| 1470 | unsigned int arg, |
| 1471 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1472 | { |
| 1473 | if (mmc_card_sd(card)) |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1474 | return mmc_sd_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1475 | else |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1476 | return mmc_mmc_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, |
| 1480 | unsigned int to, unsigned int arg) |
| 1481 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1482 | struct mmc_command cmd = {0}; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1483 | unsigned int qty = 0; |
| 1484 | int err; |
| 1485 | |
| 1486 | /* |
| 1487 | * qty is used to calculate the erase timeout which depends on how many |
| 1488 | * erase groups (or allocation units in SD terminology) are affected. |
| 1489 | * We count erasing part of an erase group as one erase group. |
| 1490 | * For SD, the allocation units are always a power of 2. For MMC, the |
| 1491 | * erase group size is almost certainly also power of 2, but it does not |
| 1492 | * seem to insist on that in the JEDEC standard, so we fall back to |
| 1493 | * division in that case. SD may not specify an allocation unit size, |
| 1494 | * in which case the timeout is based on the number of write blocks. |
| 1495 | * |
| 1496 | * Note that the timeout for secure trim 2 will only be correct if the |
| 1497 | * number of erase groups specified is the same as the total of all |
| 1498 | * preceding secure trim 1 commands. Since the power may have been |
| 1499 | * lost since the secure trim 1 commands occurred, it is generally |
| 1500 | * impossible to calculate the secure trim 2 timeout correctly. |
| 1501 | */ |
| 1502 | if (card->erase_shift) |
| 1503 | qty += ((to >> card->erase_shift) - |
| 1504 | (from >> card->erase_shift)) + 1; |
| 1505 | else if (mmc_card_sd(card)) |
| 1506 | qty += to - from + 1; |
| 1507 | else |
| 1508 | qty += ((to / card->erase_size) - |
| 1509 | (from / card->erase_size)) + 1; |
| 1510 | |
| 1511 | if (!mmc_card_blockaddr(card)) { |
| 1512 | from <<= 9; |
| 1513 | to <<= 9; |
| 1514 | } |
| 1515 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1516 | if (mmc_card_sd(card)) |
| 1517 | cmd.opcode = SD_ERASE_WR_BLK_START; |
| 1518 | else |
| 1519 | cmd.opcode = MMC_ERASE_GROUP_START; |
| 1520 | cmd.arg = from; |
| 1521 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1522 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1523 | if (err) { |
| 1524 | printk(KERN_ERR "mmc_erase: group start error %d, " |
| 1525 | "status %#x\n", err, cmd.resp[0]); |
| 1526 | err = -EINVAL; |
| 1527 | goto out; |
| 1528 | } |
| 1529 | |
| 1530 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1531 | if (mmc_card_sd(card)) |
| 1532 | cmd.opcode = SD_ERASE_WR_BLK_END; |
| 1533 | else |
| 1534 | cmd.opcode = MMC_ERASE_GROUP_END; |
| 1535 | cmd.arg = to; |
| 1536 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1537 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1538 | if (err) { |
| 1539 | printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", |
| 1540 | err, cmd.resp[0]); |
| 1541 | err = -EINVAL; |
| 1542 | goto out; |
| 1543 | } |
| 1544 | |
| 1545 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1546 | cmd.opcode = MMC_ERASE; |
| 1547 | cmd.arg = arg; |
| 1548 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 1549 | cmd.cmd_timeout_ms = mmc_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1550 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1551 | if (err) { |
| 1552 | printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", |
| 1553 | err, cmd.resp[0]); |
| 1554 | err = -EIO; |
| 1555 | goto out; |
| 1556 | } |
| 1557 | |
| 1558 | if (mmc_host_is_spi(card->host)) |
| 1559 | goto out; |
| 1560 | |
| 1561 | do { |
| 1562 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1563 | cmd.opcode = MMC_SEND_STATUS; |
| 1564 | cmd.arg = card->rca << 16; |
| 1565 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1566 | /* Do not retry else we can't see errors */ |
| 1567 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 1568 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
| 1569 | printk(KERN_ERR "error %d requesting status %#x\n", |
| 1570 | err, cmd.resp[0]); |
| 1571 | err = -EIO; |
| 1572 | goto out; |
| 1573 | } |
| 1574 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
Jaehoon Chung | 7435bb7 | 2011-08-10 18:46:28 +0900 | [diff] [blame] | 1575 | R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 1576 | out: |
| 1577 | return err; |
| 1578 | } |
| 1579 | |
| 1580 | /** |
| 1581 | * mmc_erase - erase sectors. |
| 1582 | * @card: card to erase |
| 1583 | * @from: first sector to erase |
| 1584 | * @nr: number of sectors to erase |
| 1585 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
| 1586 | * |
| 1587 | * Caller must claim host before calling this function. |
| 1588 | */ |
| 1589 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, |
| 1590 | unsigned int arg) |
| 1591 | { |
| 1592 | unsigned int rem, to = from + nr; |
| 1593 | |
| 1594 | if (!(card->host->caps & MMC_CAP_ERASE) || |
| 1595 | !(card->csd.cmdclass & CCC_ERASE)) |
| 1596 | return -EOPNOTSUPP; |
| 1597 | |
| 1598 | if (!card->erase_size) |
| 1599 | return -EOPNOTSUPP; |
| 1600 | |
| 1601 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
| 1602 | return -EOPNOTSUPP; |
| 1603 | |
| 1604 | if ((arg & MMC_SECURE_ARGS) && |
| 1605 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 1606 | return -EOPNOTSUPP; |
| 1607 | |
| 1608 | if ((arg & MMC_TRIM_ARGS) && |
| 1609 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 1610 | return -EOPNOTSUPP; |
| 1611 | |
| 1612 | if (arg == MMC_SECURE_ERASE_ARG) { |
| 1613 | if (from % card->erase_size || nr % card->erase_size) |
| 1614 | return -EINVAL; |
| 1615 | } |
| 1616 | |
| 1617 | if (arg == MMC_ERASE_ARG) { |
| 1618 | rem = from % card->erase_size; |
| 1619 | if (rem) { |
| 1620 | rem = card->erase_size - rem; |
| 1621 | from += rem; |
| 1622 | if (nr > rem) |
| 1623 | nr -= rem; |
| 1624 | else |
| 1625 | return 0; |
| 1626 | } |
| 1627 | rem = nr % card->erase_size; |
| 1628 | if (rem) |
| 1629 | nr -= rem; |
| 1630 | } |
| 1631 | |
| 1632 | if (nr == 0) |
| 1633 | return 0; |
| 1634 | |
| 1635 | to = from + nr; |
| 1636 | |
| 1637 | if (to <= from) |
| 1638 | return -EINVAL; |
| 1639 | |
| 1640 | /* 'from' and 'to' are inclusive */ |
| 1641 | to -= 1; |
| 1642 | |
| 1643 | return mmc_do_erase(card, from, to, arg); |
| 1644 | } |
| 1645 | EXPORT_SYMBOL(mmc_erase); |
| 1646 | |
| 1647 | int mmc_can_erase(struct mmc_card *card) |
| 1648 | { |
| 1649 | if ((card->host->caps & MMC_CAP_ERASE) && |
| 1650 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
| 1651 | return 1; |
| 1652 | return 0; |
| 1653 | } |
| 1654 | EXPORT_SYMBOL(mmc_can_erase); |
| 1655 | |
| 1656 | int mmc_can_trim(struct mmc_card *card) |
| 1657 | { |
| 1658 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) |
| 1659 | return 1; |
| 1660 | return 0; |
| 1661 | } |
| 1662 | EXPORT_SYMBOL(mmc_can_trim); |
| 1663 | |
| 1664 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
| 1665 | { |
| 1666 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) |
| 1667 | return 1; |
| 1668 | return 0; |
| 1669 | } |
| 1670 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); |
| 1671 | |
| 1672 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, |
| 1673 | unsigned int nr) |
| 1674 | { |
| 1675 | if (!card->erase_size) |
| 1676 | return 0; |
| 1677 | if (from % card->erase_size || nr % card->erase_size) |
| 1678 | return 0; |
| 1679 | return 1; |
| 1680 | } |
| 1681 | EXPORT_SYMBOL(mmc_erase_group_aligned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 1683 | static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, |
| 1684 | unsigned int arg) |
| 1685 | { |
| 1686 | struct mmc_host *host = card->host; |
| 1687 | unsigned int max_discard, x, y, qty = 0, max_qty, timeout; |
| 1688 | unsigned int last_timeout = 0; |
| 1689 | |
| 1690 | if (card->erase_shift) |
| 1691 | max_qty = UINT_MAX >> card->erase_shift; |
| 1692 | else if (mmc_card_sd(card)) |
| 1693 | max_qty = UINT_MAX; |
| 1694 | else |
| 1695 | max_qty = UINT_MAX / card->erase_size; |
| 1696 | |
| 1697 | /* Find the largest qty with an OK timeout */ |
| 1698 | do { |
| 1699 | y = 0; |
| 1700 | for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { |
| 1701 | timeout = mmc_erase_timeout(card, arg, qty + x); |
| 1702 | if (timeout > host->max_discard_to) |
| 1703 | break; |
| 1704 | if (timeout < last_timeout) |
| 1705 | break; |
| 1706 | last_timeout = timeout; |
| 1707 | y = x; |
| 1708 | } |
| 1709 | qty += y; |
| 1710 | } while (y); |
| 1711 | |
| 1712 | if (!qty) |
| 1713 | return 0; |
| 1714 | |
| 1715 | if (qty == 1) |
| 1716 | return 1; |
| 1717 | |
| 1718 | /* Convert qty to sectors */ |
| 1719 | if (card->erase_shift) |
| 1720 | max_discard = --qty << card->erase_shift; |
| 1721 | else if (mmc_card_sd(card)) |
| 1722 | max_discard = qty; |
| 1723 | else |
| 1724 | max_discard = --qty * card->erase_size; |
| 1725 | |
| 1726 | return max_discard; |
| 1727 | } |
| 1728 | |
| 1729 | unsigned int mmc_calc_max_discard(struct mmc_card *card) |
| 1730 | { |
| 1731 | struct mmc_host *host = card->host; |
| 1732 | unsigned int max_discard, max_trim; |
| 1733 | |
| 1734 | if (!host->max_discard_to) |
| 1735 | return UINT_MAX; |
| 1736 | |
| 1737 | /* |
| 1738 | * Without erase_group_def set, MMC erase timeout depends on clock |
| 1739 | * frequence which can change. In that case, the best choice is |
| 1740 | * just the preferred erase size. |
| 1741 | */ |
| 1742 | if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) |
| 1743 | return card->pref_erase; |
| 1744 | |
| 1745 | max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); |
| 1746 | if (mmc_can_trim(card)) { |
| 1747 | max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); |
| 1748 | if (max_trim < max_discard) |
| 1749 | max_discard = max_trim; |
| 1750 | } else if (max_discard < card->erase_size) { |
| 1751 | max_discard = 0; |
| 1752 | } |
| 1753 | pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", |
| 1754 | mmc_hostname(host), max_discard, host->max_discard_to); |
| 1755 | return max_discard; |
| 1756 | } |
| 1757 | EXPORT_SYMBOL(mmc_calc_max_discard); |
| 1758 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1759 | int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) |
| 1760 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1761 | struct mmc_command cmd = {0}; |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1762 | |
| 1763 | if (mmc_card_blockaddr(card) || mmc_card_ddr_mode(card)) |
| 1764 | return 0; |
| 1765 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 1766 | cmd.opcode = MMC_SET_BLOCKLEN; |
| 1767 | cmd.arg = blocklen; |
| 1768 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1769 | return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 1770 | } |
| 1771 | EXPORT_SYMBOL(mmc_set_blocklen); |
| 1772 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1773 | static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) |
| 1774 | { |
| 1775 | host->f_init = freq; |
| 1776 | |
| 1777 | #ifdef CONFIG_MMC_DEBUG |
| 1778 | pr_info("%s: %s: trying to init card at %u Hz\n", |
| 1779 | mmc_hostname(host), __func__, host->f_init); |
| 1780 | #endif |
| 1781 | mmc_power_up(host); |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 1782 | |
| 1783 | /* |
| 1784 | * sdio_reset sends CMD52 to reset card. Since we do not know |
| 1785 | * if the card is being re-initialized, just send it. CMD52 |
| 1786 | * should be ignored by SD/eMMC cards. |
| 1787 | */ |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1788 | sdio_reset(host); |
| 1789 | mmc_go_idle(host); |
| 1790 | |
| 1791 | mmc_send_if_cond(host, host->ocr_avail); |
| 1792 | |
| 1793 | /* Order's important: probe SDIO, then SD, then MMC */ |
| 1794 | if (!mmc_attach_sdio(host)) |
| 1795 | return 0; |
| 1796 | if (!mmc_attach_sd(host)) |
| 1797 | return 0; |
| 1798 | if (!mmc_attach_mmc(host)) |
| 1799 | return 0; |
| 1800 | |
| 1801 | mmc_power_off(host); |
| 1802 | return -EIO; |
| 1803 | } |
| 1804 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1805 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | { |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1807 | static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1808 | struct mmc_host *host = |
| 1809 | container_of(work, struct mmc_host, detect.work); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1810 | int i; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1811 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1812 | if (host->rescan_disable) |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 1813 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1815 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | |
Ohad Ben-Cohen | 30201e7 | 2010-11-28 07:21:28 +0200 | [diff] [blame] | 1817 | /* |
| 1818 | * if there is a _removable_ card registered, check whether it is |
| 1819 | * still present |
| 1820 | */ |
| 1821 | if (host->bus_ops && host->bus_ops->detect && !host->bus_dead |
Ohad Ben-Cohen | bad3bab | 2011-03-08 23:32:02 +0200 | [diff] [blame] | 1822 | && !(host->caps & MMC_CAP_NONREMOVABLE)) |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1823 | host->bus_ops->detect(host); |
| 1824 | |
Chris Ball | c584179 | 2011-01-04 12:20:22 -0500 | [diff] [blame] | 1825 | /* |
| 1826 | * Let mmc_bus_put() free the bus/bus_ops if we've found that |
| 1827 | * the card is no longer present. |
| 1828 | */ |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1829 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1830 | mmc_bus_get(host); |
| 1831 | |
| 1832 | /* if there still is a card present, stop here */ |
| 1833 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1834 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1835 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1837 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 1838 | /* |
| 1839 | * Only we can add a new handler, so it's safe to |
| 1840 | * release the lock here. |
| 1841 | */ |
| 1842 | mmc_bus_put(host); |
| 1843 | |
| 1844 | if (host->ops->get_cd && host->ops->get_cd(host) == 0) |
| 1845 | goto out; |
| 1846 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1847 | mmc_claim_host(host); |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1848 | for (i = 0; i < ARRAY_SIZE(freqs); i++) { |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1849 | if (!mmc_rescan_try_freq(host, max(freqs[i], host->f_min))) |
| 1850 | break; |
Jaehoon Chung | 06b2233 | 2011-05-12 17:18:59 +0900 | [diff] [blame] | 1851 | if (freqs[i] <= host->f_min) |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1852 | break; |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 1853 | } |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 1854 | mmc_release_host(host); |
| 1855 | |
| 1856 | out: |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 1857 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 1858 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } |
| 1860 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1861 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1862 | { |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1863 | mmc_power_off(host); |
| 1864 | mmc_detect_change(host, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 1867 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1869 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1870 | unsigned long flags; |
| 1871 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1872 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 1873 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1874 | #endif |
| 1875 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 1876 | if (host->caps & MMC_CAP_DISABLE) |
| 1877 | cancel_delayed_work(&host->disable); |
Guennadi Liakhovetski | d9bcbf3 | 2010-11-11 17:32:25 +0100 | [diff] [blame] | 1878 | cancel_delayed_work_sync(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 1879 | mmc_flush_scheduled_work(); |
| 1880 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 1881 | /* clear pm flags now and let card drivers set them as needed */ |
| 1882 | host->pm_flags = 0; |
| 1883 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1884 | mmc_bus_get(host); |
| 1885 | if (host->bus_ops && !host->bus_dead) { |
| 1886 | if (host->bus_ops->remove) |
| 1887 | host->bus_ops->remove(host); |
| 1888 | |
| 1889 | mmc_claim_host(host); |
| 1890 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 1891 | mmc_power_off(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1892 | mmc_release_host(host); |
Denis Karpov | 53509f0 | 2009-09-22 16:44:36 -0700 | [diff] [blame] | 1893 | mmc_bus_put(host); |
| 1894 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 1896 | mmc_bus_put(host); |
| 1897 | |
| 1898 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | mmc_power_off(host); |
| 1901 | } |
| 1902 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1903 | int mmc_power_save_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1904 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1905 | int ret = 0; |
| 1906 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 1907 | #ifdef CONFIG_MMC_DEBUG |
| 1908 | pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__); |
| 1909 | #endif |
| 1910 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1911 | mmc_bus_get(host); |
| 1912 | |
| 1913 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1914 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1915 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | if (host->bus_ops->power_save) |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1919 | ret = host->bus_ops->power_save(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1920 | |
| 1921 | mmc_bus_put(host); |
| 1922 | |
| 1923 | mmc_power_off(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1924 | |
| 1925 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1926 | } |
| 1927 | EXPORT_SYMBOL(mmc_power_save_host); |
| 1928 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1929 | int mmc_power_restore_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1930 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1931 | int ret; |
| 1932 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 1933 | #ifdef CONFIG_MMC_DEBUG |
| 1934 | pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__); |
| 1935 | #endif |
| 1936 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1937 | mmc_bus_get(host); |
| 1938 | |
| 1939 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->power_restore) { |
| 1940 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1941 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1942 | } |
| 1943 | |
| 1944 | mmc_power_up(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1945 | ret = host->bus_ops->power_restore(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1946 | |
| 1947 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 1948 | |
| 1949 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 1950 | } |
| 1951 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 1952 | |
Jarkko Lavinen | b1ebe38 | 2009-09-22 16:44:34 -0700 | [diff] [blame] | 1953 | int mmc_card_awake(struct mmc_host *host) |
| 1954 | { |
| 1955 | int err = -ENOSYS; |
| 1956 | |
| 1957 | mmc_bus_get(host); |
| 1958 | |
| 1959 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1960 | err = host->bus_ops->awake(host); |
| 1961 | |
| 1962 | mmc_bus_put(host); |
| 1963 | |
| 1964 | return err; |
| 1965 | } |
| 1966 | EXPORT_SYMBOL(mmc_card_awake); |
| 1967 | |
| 1968 | int mmc_card_sleep(struct mmc_host *host) |
| 1969 | { |
| 1970 | int err = -ENOSYS; |
| 1971 | |
| 1972 | mmc_bus_get(host); |
| 1973 | |
| 1974 | if (host->bus_ops && !host->bus_dead && host->bus_ops->awake) |
| 1975 | err = host->bus_ops->sleep(host); |
| 1976 | |
| 1977 | mmc_bus_put(host); |
| 1978 | |
| 1979 | return err; |
| 1980 | } |
| 1981 | EXPORT_SYMBOL(mmc_card_sleep); |
| 1982 | |
| 1983 | int mmc_card_can_sleep(struct mmc_host *host) |
| 1984 | { |
| 1985 | struct mmc_card *card = host->card; |
| 1986 | |
| 1987 | if (card && mmc_card_mmc(card) && card->ext_csd.rev >= 3) |
| 1988 | return 1; |
| 1989 | return 0; |
| 1990 | } |
| 1991 | EXPORT_SYMBOL(mmc_card_can_sleep); |
| 1992 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | #ifdef CONFIG_PM |
| 1994 | |
| 1995 | /** |
| 1996 | * mmc_suspend_host - suspend a host |
| 1997 | * @host: mmc host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | */ |
Matt Fleming | 1a13f8f | 2010-05-26 14:42:08 -0700 | [diff] [blame] | 1999 | int mmc_suspend_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2001 | int err = 0; |
| 2002 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 2003 | if (host->caps & MMC_CAP_DISABLE) |
| 2004 | cancel_delayed_work(&host->disable); |
Jorg Schummer | 7de427d | 2009-02-19 13:17:03 +0200 | [diff] [blame] | 2005 | cancel_delayed_work(&host->detect); |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 2006 | mmc_flush_scheduled_work(); |
| 2007 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2008 | mmc_bus_get(host); |
| 2009 | if (host->bus_ops && !host->bus_dead) { |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2010 | if (host->bus_ops->suspend) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2011 | err = host->bus_ops->suspend(host); |
Ohad Ben-Cohen | 1c8cf9c | 2010-10-13 09:31:56 +0200 | [diff] [blame] | 2012 | if (err == -ENOSYS || !host->bus_ops->resume) { |
| 2013 | /* |
| 2014 | * We simply "remove" the card in this case. |
| 2015 | * It will be redetected on resume. |
| 2016 | */ |
| 2017 | if (host->bus_ops->remove) |
| 2018 | host->bus_ops->remove(host); |
| 2019 | mmc_claim_host(host); |
| 2020 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 2021 | mmc_power_off(host); |
Ohad Ben-Cohen | 1c8cf9c | 2010-10-13 09:31:56 +0200 | [diff] [blame] | 2022 | mmc_release_host(host); |
| 2023 | host->pm_flags = 0; |
| 2024 | err = 0; |
| 2025 | } |
Pierre Ossman | b5af25b | 2007-04-28 17:30:50 +0200 | [diff] [blame] | 2026 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2027 | mmc_bus_put(host); |
| 2028 | |
Ohad Ben-Cohen | a5e9425 | 2011-04-05 17:43:20 +0300 | [diff] [blame] | 2029 | if (!err && !mmc_card_keep_power(host)) |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2030 | mmc_power_off(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2032 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | } |
| 2034 | |
| 2035 | EXPORT_SYMBOL(mmc_suspend_host); |
| 2036 | |
| 2037 | /** |
| 2038 | * mmc_resume_host - resume a previously suspended host |
| 2039 | * @host: mmc host |
| 2040 | */ |
| 2041 | int mmc_resume_host(struct mmc_host *host) |
| 2042 | { |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2043 | int err = 0; |
| 2044 | |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2045 | mmc_bus_get(host); |
| 2046 | if (host->bus_ops && !host->bus_dead) { |
Ohad Ben-Cohen | a5e9425 | 2011-04-05 17:43:20 +0300 | [diff] [blame] | 2047 | if (!mmc_card_keep_power(host)) { |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 2048 | mmc_power_up(host); |
| 2049 | mmc_select_voltage(host, host->ocr); |
Ohad Ben-Cohen | e594573 | 2010-11-28 07:21:30 +0200 | [diff] [blame] | 2050 | /* |
| 2051 | * Tell runtime PM core we just powered up the card, |
| 2052 | * since it still believes the card is powered off. |
| 2053 | * Note that currently runtime PM is only enabled |
| 2054 | * for SDIO cards that are MMC_CAP_POWER_OFF_CARD |
| 2055 | */ |
| 2056 | if (mmc_card_sdio(host->card) && |
| 2057 | (host->caps & MMC_CAP_POWER_OFF_CARD)) { |
| 2058 | pm_runtime_disable(&host->card->dev); |
| 2059 | pm_runtime_set_active(&host->card->dev); |
| 2060 | pm_runtime_enable(&host->card->dev); |
| 2061 | } |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 2062 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2063 | BUG_ON(!host->bus_ops->resume); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2064 | err = host->bus_ops->resume(host); |
| 2065 | if (err) { |
| 2066 | printk(KERN_WARNING "%s: error %d during resume " |
| 2067 | "(card was removed?)\n", |
| 2068 | mmc_hostname(host), err); |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2069 | err = 0; |
| 2070 | } |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2071 | } |
Eliad Peller | a8e6df7 | 2011-05-09 11:32:31 +0300 | [diff] [blame] | 2072 | host->pm_flags &= ~MMC_PM_KEEP_POWER; |
Pierre Ossman | 6abaa0c | 2007-05-01 16:00:02 +0200 | [diff] [blame] | 2073 | mmc_bus_put(host); |
| 2074 | |
Nicolas Pitre | 95cdfb7 | 2009-09-22 16:45:29 -0700 | [diff] [blame] | 2075 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | EXPORT_SYMBOL(mmc_resume_host); |
| 2078 | |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2079 | /* Do the card removal on suspend if card is assumed removeable |
| 2080 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
| 2081 | to sync the card. |
| 2082 | */ |
| 2083 | int mmc_pm_notify(struct notifier_block *notify_block, |
| 2084 | unsigned long mode, void *unused) |
| 2085 | { |
| 2086 | struct mmc_host *host = container_of( |
| 2087 | notify_block, struct mmc_host, pm_notify); |
| 2088 | unsigned long flags; |
| 2089 | |
| 2090 | |
| 2091 | switch (mode) { |
| 2092 | case PM_HIBERNATION_PREPARE: |
| 2093 | case PM_SUSPEND_PREPARE: |
| 2094 | |
| 2095 | spin_lock_irqsave(&host->lock, flags); |
| 2096 | host->rescan_disable = 1; |
| 2097 | spin_unlock_irqrestore(&host->lock, flags); |
| 2098 | cancel_delayed_work_sync(&host->detect); |
| 2099 | |
| 2100 | if (!host->bus_ops || host->bus_ops->suspend) |
| 2101 | break; |
| 2102 | |
| 2103 | mmc_claim_host(host); |
| 2104 | |
| 2105 | if (host->bus_ops->remove) |
| 2106 | host->bus_ops->remove(host); |
| 2107 | |
| 2108 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 2109 | mmc_power_off(host); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2110 | mmc_release_host(host); |
| 2111 | host->pm_flags = 0; |
| 2112 | break; |
| 2113 | |
| 2114 | case PM_POST_SUSPEND: |
| 2115 | case PM_POST_HIBERNATION: |
Takashi Iwai | 274476f | 2010-12-10 08:40:31 +0100 | [diff] [blame] | 2116 | case PM_POST_RESTORE: |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 2117 | |
| 2118 | spin_lock_irqsave(&host->lock, flags); |
| 2119 | host->rescan_disable = 0; |
| 2120 | spin_unlock_irqrestore(&host->lock, flags); |
| 2121 | mmc_detect_change(host, 0); |
| 2122 | |
| 2123 | } |
| 2124 | |
| 2125 | return 0; |
| 2126 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2127 | #endif |
| 2128 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2129 | static int __init mmc_init(void) |
| 2130 | { |
| 2131 | int ret; |
| 2132 | |
Tejun Heo | 0d9ee5b | 2010-12-24 16:00:17 +0100 | [diff] [blame] | 2133 | workqueue = alloc_ordered_workqueue("kmmcd", 0); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2134 | if (!workqueue) |
| 2135 | return -ENOMEM; |
| 2136 | |
| 2137 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 2138 | if (ret) |
| 2139 | goto destroy_workqueue; |
| 2140 | |
| 2141 | ret = mmc_register_host_class(); |
| 2142 | if (ret) |
| 2143 | goto unregister_bus; |
| 2144 | |
| 2145 | ret = sdio_register_bus(); |
| 2146 | if (ret) |
| 2147 | goto unregister_host_class; |
| 2148 | |
| 2149 | return 0; |
| 2150 | |
| 2151 | unregister_host_class: |
| 2152 | mmc_unregister_host_class(); |
| 2153 | unregister_bus: |
| 2154 | mmc_unregister_bus(); |
| 2155 | destroy_workqueue: |
| 2156 | destroy_workqueue(workqueue); |
| 2157 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2158 | return ret; |
| 2159 | } |
| 2160 | |
| 2161 | static void __exit mmc_exit(void) |
| 2162 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 2163 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2164 | mmc_unregister_host_class(); |
| 2165 | mmc_unregister_bus(); |
| 2166 | destroy_workqueue(workqueue); |
| 2167 | } |
| 2168 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 2169 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 2170 | module_exit(mmc_exit); |
| 2171 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | MODULE_LICENSE("GPL"); |