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> |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 17 | #include <linux/devfreq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include <linux/device.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/pagemap.h> |
| 21 | #include <linux/err.h> |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 22 | #include <linux/leds.h> |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 23 | #include <linux/scatterlist.h> |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 24 | #include <linux/log2.h> |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 25 | #include <linux/regulator/consumer.h> |
Ohad Ben-Cohen | e594573 | 2010-11-28 07:21:30 +0200 | [diff] [blame] | 26 | #include <linux/pm_runtime.h> |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 27 | #include <linux/pm_wakeup.h> |
Amerigo Wang | 35eb6db | 2011-07-25 17:13:11 -0700 | [diff] [blame] | 28 | #include <linux/suspend.h> |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 29 | #include <linux/fault-inject.h> |
| 30 | #include <linux/random.h> |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 31 | #include <linux/slab.h> |
Haijun Zhang | 6e9e318 | 2013-08-26 09:19:22 +0800 | [diff] [blame] | 32 | #include <linux/of.h> |
Sujith Reddy Thumma | 1547ccb | 2011-02-17 21:37:48 +0530 | [diff] [blame] | 33 | #include <linux/pm.h> |
Sujit Reddy Thumma | cb18d85 | 2014-12-04 09:57:23 +0200 | [diff] [blame] | 34 | #include <linux/jiffies.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | |
| 36 | #include <linux/mmc/card.h> |
| 37 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 38 | #include <linux/mmc/mmc.h> |
| 39 | #include <linux/mmc/sd.h> |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 40 | #include <linux/mmc/slot-gpio.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
Baolin Wang | 7962fc3 | 2016-03-31 11:16:27 +0800 | [diff] [blame] | 42 | #define CREATE_TRACE_POINTS |
| 43 | #include <trace/events/mmc.h> |
| 44 | |
Pierre Ossman | aaac1b4 | 2007-02-28 15:33:10 +0100 | [diff] [blame] | 45 | #include "core.h" |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 46 | #include "bus.h" |
| 47 | #include "host.h" |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 48 | #include "sdio_bus.h" |
Ulf Hansson | 3aa8793 | 2014-11-28 14:38:36 +0100 | [diff] [blame] | 49 | #include "pwrseq.h" |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 50 | |
| 51 | #include "mmc_ops.h" |
| 52 | #include "sd_ops.h" |
Pierre Ossman | 5c4e6f1 | 2007-05-21 20:23:20 +0200 | [diff] [blame] | 53 | #include "sdio_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 55 | /* If the device is not responding */ |
| 56 | #define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */ |
| 57 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 58 | /* |
| 59 | * Background operations can take a long time, depending on the housekeeping |
| 60 | * operations the card has to perform. |
| 61 | */ |
| 62 | #define MMC_BKOPS_MAX_TIMEOUT (4 * 60 * 1000) /* max time to wait in ms */ |
| 63 | |
Ulf Hansson | 12182af | 2016-07-27 00:04:03 +0200 | [diff] [blame] | 64 | /* The max erase timeout, used when host->max_busy_timeout isn't specified */ |
| 65 | #define MMC_ERASE_TIMEOUT_MS (60 * 1000) /* 60 s */ |
| 66 | |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 67 | static const unsigned freqs[] = { 400000, 300000, 200000, 100000 }; |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 68 | |
| 69 | /* |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 70 | * Enabling software CRCs on the data blocks can be a significant (30%) |
| 71 | * performance cost, and for other reasons may not always be desired. |
| 72 | * So we allow it it to be disabled. |
| 73 | */ |
Rusty Russell | 90ab5ee | 2012-01-13 09:32:20 +1030 | [diff] [blame] | 74 | bool use_spi_crc = 1; |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 75 | module_param(use_spi_crc, bool, 0); |
| 76 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 77 | static int mmc_schedule_delayed_work(struct delayed_work *work, |
| 78 | unsigned long delay) |
| 79 | { |
Ulf Hansson | 520bd7a | 2015-12-01 10:35:29 +0100 | [diff] [blame] | 80 | /* |
| 81 | * We use the system_freezable_wq, because of two reasons. |
| 82 | * First, it allows several works (not the same work item) to be |
| 83 | * executed simultaneously. Second, the queue becomes frozen when |
| 84 | * userspace becomes frozen during system PM. |
| 85 | */ |
| 86 | return queue_delayed_work(system_freezable_wq, work, delay); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 89 | #ifdef CONFIG_FAIL_MMC_REQUEST |
| 90 | |
| 91 | /* |
| 92 | * Internal function. Inject random data errors. |
| 93 | * If mmc_data is NULL no errors are injected. |
| 94 | */ |
| 95 | static void mmc_should_fail_request(struct mmc_host *host, |
| 96 | struct mmc_request *mrq) |
| 97 | { |
| 98 | struct mmc_command *cmd = mrq->cmd; |
| 99 | struct mmc_data *data = mrq->data; |
| 100 | static const int data_errors[] = { |
| 101 | -ETIMEDOUT, |
| 102 | -EILSEQ, |
| 103 | -EIO, |
| 104 | }; |
| 105 | |
| 106 | if (!data) |
| 107 | return; |
| 108 | |
| 109 | if (cmd->error || data->error || |
| 110 | !should_fail(&host->fail_mmc_request, data->blksz * data->blocks)) |
| 111 | return; |
| 112 | |
Akinobu Mita | 2e744fc | 2013-04-29 16:21:31 -0700 | [diff] [blame] | 113 | data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)]; |
| 114 | data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 115 | data->fault_injected = true; |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | #else /* CONFIG_FAIL_MMC_REQUEST */ |
| 119 | |
| 120 | static inline void mmc_should_fail_request(struct mmc_host *host, |
| 121 | struct mmc_request *mrq) |
| 122 | { |
| 123 | } |
| 124 | |
| 125 | #endif /* CONFIG_FAIL_MMC_REQUEST */ |
| 126 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 127 | static bool mmc_is_data_request(struct mmc_request *mmc_request) |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 128 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 129 | switch (mmc_request->cmd->opcode) { |
| 130 | case MMC_READ_SINGLE_BLOCK: |
| 131 | case MMC_READ_MULTIPLE_BLOCK: |
| 132 | case MMC_WRITE_BLOCK: |
| 133 | case MMC_WRITE_MULTIPLE_BLOCK: |
| 134 | return true; |
| 135 | default: |
| 136 | return false; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static void mmc_clk_scaling_start_busy(struct mmc_host *host, bool lock_needed) |
| 141 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 142 | struct mmc_devfeq_clk_scaling *clk_scaling = &host->clk_scaling; |
| 143 | |
| 144 | if (!clk_scaling->enable) |
| 145 | return; |
| 146 | |
| 147 | if (lock_needed) |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 148 | spin_lock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 149 | |
| 150 | clk_scaling->start_busy = ktime_get(); |
| 151 | clk_scaling->is_busy_started = true; |
| 152 | |
| 153 | if (lock_needed) |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 154 | spin_unlock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | static void mmc_clk_scaling_stop_busy(struct mmc_host *host, bool lock_needed) |
| 158 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 159 | struct mmc_devfeq_clk_scaling *clk_scaling = &host->clk_scaling; |
| 160 | |
| 161 | if (!clk_scaling->enable) |
| 162 | return; |
| 163 | |
| 164 | if (lock_needed) |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 165 | spin_lock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 166 | |
| 167 | if (!clk_scaling->is_busy_started) { |
| 168 | WARN_ON(1); |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | clk_scaling->total_busy_time_us += |
| 173 | ktime_to_us(ktime_sub(ktime_get(), |
| 174 | clk_scaling->start_busy)); |
| 175 | pr_debug("%s: accumulated busy time is %lu usec\n", |
| 176 | mmc_hostname(host), clk_scaling->total_busy_time_us); |
| 177 | clk_scaling->is_busy_started = false; |
| 178 | |
| 179 | out: |
| 180 | if (lock_needed) |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 181 | spin_unlock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /** |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 185 | * mmc_cmdq_clk_scaling_start_busy() - start busy timer for data requests |
| 186 | * @host: pointer to mmc host structure |
| 187 | * @lock_needed: flag indication if locking is needed |
| 188 | * |
| 189 | * This function starts the busy timer in case it was not already started. |
| 190 | */ |
| 191 | void mmc_cmdq_clk_scaling_start_busy(struct mmc_host *host, |
| 192 | bool lock_needed) |
| 193 | { |
| 194 | if (!host->clk_scaling.enable) |
| 195 | return; |
| 196 | |
| 197 | if (lock_needed) |
| 198 | spin_lock_bh(&host->clk_scaling.lock); |
| 199 | |
| 200 | if (!host->clk_scaling.is_busy_started && |
| 201 | !test_bit(CMDQ_STATE_DCMD_ACTIVE, |
| 202 | &host->cmdq_ctx.curr_state)) { |
| 203 | host->clk_scaling.start_busy = ktime_get(); |
| 204 | host->clk_scaling.is_busy_started = true; |
| 205 | } |
| 206 | |
| 207 | if (lock_needed) |
| 208 | spin_unlock_bh(&host->clk_scaling.lock); |
| 209 | } |
| 210 | EXPORT_SYMBOL(mmc_cmdq_clk_scaling_start_busy); |
| 211 | |
| 212 | /** |
| 213 | * mmc_cmdq_clk_scaling_stop_busy() - stop busy timer for last data requests |
| 214 | * @host: pointer to mmc host structure |
| 215 | * @lock_needed: flag indication if locking is needed |
| 216 | * |
| 217 | * This function stops the busy timer in case it is the last data request. |
| 218 | * In case the current request is not the last one, the busy time till |
| 219 | * now will be accumulated and the counter will be restarted. |
| 220 | */ |
| 221 | void mmc_cmdq_clk_scaling_stop_busy(struct mmc_host *host, |
| 222 | bool lock_needed, bool is_cmdq_dcmd) |
| 223 | { |
| 224 | if (!host->clk_scaling.enable) |
| 225 | return; |
| 226 | |
| 227 | if (lock_needed) |
| 228 | spin_lock_bh(&host->clk_scaling.lock); |
| 229 | |
| 230 | /* |
| 231 | * For CQ mode: In completion of DCMD request, start busy time in |
| 232 | * case of pending data requests |
| 233 | */ |
| 234 | if (is_cmdq_dcmd) { |
| 235 | if (host->cmdq_ctx.data_active_reqs) { |
| 236 | host->clk_scaling.is_busy_started = true; |
| 237 | host->clk_scaling.start_busy = ktime_get(); |
| 238 | } |
| 239 | goto out; |
| 240 | } |
| 241 | |
| 242 | host->clk_scaling.total_busy_time_us += |
| 243 | ktime_to_us(ktime_sub(ktime_get(), |
| 244 | host->clk_scaling.start_busy)); |
| 245 | |
| 246 | if (host->cmdq_ctx.data_active_reqs) { |
| 247 | host->clk_scaling.is_busy_started = true; |
| 248 | host->clk_scaling.start_busy = ktime_get(); |
| 249 | } else { |
| 250 | host->clk_scaling.is_busy_started = false; |
| 251 | } |
| 252 | out: |
| 253 | if (lock_needed) |
| 254 | spin_unlock_bh(&host->clk_scaling.lock); |
| 255 | |
| 256 | } |
| 257 | EXPORT_SYMBOL(mmc_cmdq_clk_scaling_stop_busy); |
| 258 | |
| 259 | /** |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 260 | * mmc_can_scale_clk() - Check clock scaling capability |
| 261 | * @host: pointer to mmc host structure |
| 262 | */ |
| 263 | bool mmc_can_scale_clk(struct mmc_host *host) |
| 264 | { |
| 265 | if (!host) { |
| 266 | pr_err("bad host parameter\n"); |
| 267 | WARN_ON(1); |
| 268 | return false; |
| 269 | } |
| 270 | |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 271 | return host->caps2 & MMC_CAP2_CLK_SCALE; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 272 | } |
| 273 | EXPORT_SYMBOL(mmc_can_scale_clk); |
| 274 | |
| 275 | static int mmc_devfreq_get_dev_status(struct device *dev, |
| 276 | struct devfreq_dev_status *status) |
| 277 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 278 | struct mmc_host *host = container_of(dev, struct mmc_host, class_dev); |
| 279 | struct mmc_devfeq_clk_scaling *clk_scaling; |
| 280 | |
| 281 | if (!host) { |
| 282 | pr_err("bad host parameter\n"); |
| 283 | WARN_ON(1); |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | |
| 287 | clk_scaling = &host->clk_scaling; |
| 288 | |
| 289 | if (!clk_scaling->enable) |
| 290 | return 0; |
| 291 | |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 292 | spin_lock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 293 | |
| 294 | /* accumulate the busy time of ongoing work */ |
| 295 | memset(status, 0, sizeof(*status)); |
| 296 | if (clk_scaling->is_busy_started) { |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 297 | if (mmc_card_cmdq(host->card)) { |
| 298 | /* the "busy-timer" will be restarted in case there |
| 299 | * are pending data requests */ |
| 300 | mmc_cmdq_clk_scaling_stop_busy(host, false, false); |
| 301 | } else { |
| 302 | mmc_clk_scaling_stop_busy(host, false); |
| 303 | mmc_clk_scaling_start_busy(host, false); |
| 304 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | status->busy_time = clk_scaling->total_busy_time_us; |
| 308 | status->total_time = ktime_to_us(ktime_sub(ktime_get(), |
| 309 | clk_scaling->measure_interval_start)); |
| 310 | clk_scaling->total_busy_time_us = 0; |
| 311 | status->current_frequency = clk_scaling->curr_freq; |
| 312 | clk_scaling->measure_interval_start = ktime_get(); |
| 313 | |
| 314 | pr_debug("%s: status: load = %lu%% - total_time=%lu busy_time = %lu, clk=%lu\n", |
| 315 | mmc_hostname(host), |
| 316 | (status->busy_time*100)/status->total_time, |
| 317 | status->total_time, status->busy_time, |
| 318 | status->current_frequency); |
| 319 | |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 320 | spin_unlock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 321 | |
| 322 | return 0; |
| 323 | } |
| 324 | |
Talel Shenhar | 9591b99 | 2015-07-20 16:12:31 +0300 | [diff] [blame] | 325 | static bool mmc_is_valid_state_for_clk_scaling(struct mmc_host *host) |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 326 | { |
| 327 | struct mmc_card *card = host->card; |
| 328 | u32 status; |
| 329 | |
| 330 | /* |
| 331 | * If the current partition type is RPMB, clock switching may not |
| 332 | * work properly as sending tuning command (CMD21) is illegal in |
| 333 | * this mode. |
| 334 | */ |
| 335 | if (!card || (mmc_card_mmc(card) && |
Maya Erez | ee975d3 | 2015-09-17 16:07:39 +0300 | [diff] [blame] | 336 | (card->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB || |
| 337 | mmc_card_doing_bkops(card)))) |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 338 | return false; |
| 339 | |
| 340 | if (mmc_send_status(card, &status)) { |
| 341 | pr_err("%s: Get card status fail\n", mmc_hostname(card->host)); |
| 342 | return false; |
| 343 | } |
| 344 | |
| 345 | return R1_CURRENT_STATE(status) == R1_STATE_TRAN; |
| 346 | } |
| 347 | |
Ritesh Harjani | cd62f1b | 2015-07-13 10:52:36 +0530 | [diff] [blame] | 348 | int mmc_cmdq_halt_on_empty_queue(struct mmc_host *host) |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 349 | { |
| 350 | int err = 0; |
| 351 | |
| 352 | err = wait_event_interruptible(host->cmdq_ctx.queue_empty_wq, |
| 353 | (!host->cmdq_ctx.active_reqs)); |
| 354 | if (host->cmdq_ctx.active_reqs) { |
| 355 | pr_err("%s: %s: unexpected active requests (%lu)\n", |
| 356 | mmc_hostname(host), __func__, |
| 357 | host->cmdq_ctx.active_reqs); |
| 358 | return -EPERM; |
| 359 | } |
| 360 | |
| 361 | err = mmc_cmdq_halt(host, true); |
| 362 | if (err) { |
| 363 | pr_err("%s: %s: mmc_cmdq_halt failed (%d)\n", |
| 364 | mmc_hostname(host), __func__, err); |
| 365 | goto out; |
| 366 | } |
| 367 | |
| 368 | out: |
| 369 | return err; |
| 370 | } |
Ritesh Harjani | cd62f1b | 2015-07-13 10:52:36 +0530 | [diff] [blame] | 371 | EXPORT_SYMBOL(mmc_cmdq_halt_on_empty_queue); |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 372 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 373 | int mmc_clk_update_freq(struct mmc_host *host, |
| 374 | unsigned long freq, enum mmc_load state) |
| 375 | { |
| 376 | int err = 0; |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 377 | bool cmdq_mode; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 378 | |
| 379 | if (!host) { |
| 380 | pr_err("bad host parameter\n"); |
| 381 | WARN_ON(1); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 385 | mmc_host_clk_hold(host); |
| 386 | cmdq_mode = mmc_card_cmdq(host->card); |
| 387 | |
Talel Shenhar | a52f84e | 2015-05-27 14:20:34 +0300 | [diff] [blame] | 388 | /* make sure the card supports the frequency we want */ |
| 389 | if (unlikely(freq > host->card->clk_scaling_highest)) { |
| 390 | freq = host->card->clk_scaling_highest; |
| 391 | pr_warn("%s: %s: frequency was overridden to %lu\n", |
| 392 | mmc_hostname(host), __func__, |
| 393 | host->card->clk_scaling_highest); |
| 394 | } |
| 395 | |
| 396 | if (unlikely(freq < host->card->clk_scaling_lowest)) { |
| 397 | freq = host->card->clk_scaling_lowest; |
| 398 | pr_warn("%s: %s: frequency was overridden to %lu\n", |
| 399 | mmc_hostname(host), __func__, |
| 400 | host->card->clk_scaling_lowest); |
| 401 | } |
| 402 | |
Talel Shenhar | 9591b99 | 2015-07-20 16:12:31 +0300 | [diff] [blame] | 403 | if (freq == host->clk_scaling.curr_freq) |
| 404 | goto out; |
| 405 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 406 | if (host->ops->notify_load) { |
| 407 | err = host->ops->notify_load(host, state); |
| 408 | if (err) { |
| 409 | pr_err("%s: %s: fail on notify_load\n", |
| 410 | mmc_hostname(host), __func__); |
| 411 | goto out; |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 412 | } |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Talel Shenhar | 9591b99 | 2015-07-20 16:12:31 +0300 | [diff] [blame] | 415 | if (cmdq_mode) { |
| 416 | err = mmc_cmdq_halt_on_empty_queue(host); |
| 417 | if (err) { |
| 418 | pr_err("%s: %s: failed halting queue (%d)\n", |
| 419 | mmc_hostname(host), __func__, err); |
| 420 | goto halt_failed; |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 421 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 422 | } |
Talel Shenhar | 9591b99 | 2015-07-20 16:12:31 +0300 | [diff] [blame] | 423 | |
| 424 | if (!mmc_is_valid_state_for_clk_scaling(host)) { |
| 425 | pr_debug("%s: invalid state for clock scaling - skipping", |
| 426 | mmc_hostname(host)); |
| 427 | goto invalid_state; |
| 428 | } |
| 429 | |
| 430 | err = host->bus_ops->change_bus_speed(host, &freq); |
| 431 | if (!err) |
| 432 | host->clk_scaling.curr_freq = freq; |
| 433 | else |
| 434 | pr_err("%s: %s: failed (%d) at freq=%lu\n", |
| 435 | mmc_hostname(host), __func__, err, freq); |
| 436 | |
| 437 | invalid_state: |
| 438 | if (cmdq_mode) { |
| 439 | if (mmc_cmdq_halt(host, false)) |
| 440 | pr_err("%s: %s: cmdq unhalt failed\n", |
| 441 | mmc_hostname(host), __func__); |
| 442 | } |
| 443 | |
| 444 | halt_failed: |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 445 | if (err) { |
| 446 | /* restore previous state */ |
| 447 | if (host->ops->notify_load) |
| 448 | if (host->ops->notify_load(host, |
| 449 | host->clk_scaling.state)) |
| 450 | pr_err("%s: %s: fail on notify_load restore\n", |
| 451 | mmc_hostname(host), __func__); |
| 452 | } |
| 453 | out: |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 454 | mmc_host_clk_release(host); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 455 | return err; |
| 456 | } |
| 457 | EXPORT_SYMBOL(mmc_clk_update_freq); |
| 458 | |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 459 | int mmc_recovery_fallback_lower_speed(struct mmc_host *host) |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 460 | { |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 461 | int err = 0; |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 462 | if (!host->card) |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 463 | return -EINVAL; |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 464 | |
| 465 | if (host->sdr104_wa && mmc_card_sd(host->card) && |
| 466 | (host->ios.timing == MMC_TIMING_UHS_SDR104) && |
| 467 | !host->card->sdr104_blocked) { |
| 468 | pr_err("%s: %s: blocked SDR104, lower the bus-speed (SDR50 / DDR50)\n", |
| 469 | mmc_hostname(host), __func__); |
| 470 | mmc_host_clear_sdr104(host); |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 471 | err = mmc_hw_reset(host); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 472 | host->card->sdr104_blocked = true; |
| 473 | } |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 474 | if (err) |
| 475 | pr_err("%s: %s: Fallback to lower speed mode failed with err=%d\n", |
| 476 | mmc_hostname(host), __func__, err); |
| 477 | |
| 478 | return err; |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 479 | } |
| 480 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 481 | static int mmc_devfreq_set_target(struct device *dev, |
| 482 | unsigned long *freq, u32 devfreq_flags) |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 483 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 484 | struct mmc_host *host = container_of(dev, struct mmc_host, class_dev); |
| 485 | struct mmc_devfeq_clk_scaling *clk_scaling; |
| 486 | int err = 0; |
| 487 | int abort; |
Veerabhadrarao Badiganti | 2f3f4eb | 2017-06-23 19:18:45 +0530 | [diff] [blame] | 488 | unsigned long pflags = current->flags; |
| 489 | |
| 490 | /* Ensure scaling would happen even in memory pressure conditions */ |
| 491 | current->flags |= PF_MEMALLOC; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 492 | |
| 493 | if (!(host && freq)) { |
| 494 | pr_err("%s: unexpected host/freq parameter\n", __func__); |
| 495 | err = -EINVAL; |
| 496 | goto out; |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 497 | } |
Veerabhadrarao Badiganti | 2f3f4eb | 2017-06-23 19:18:45 +0530 | [diff] [blame] | 498 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 499 | clk_scaling = &host->clk_scaling; |
| 500 | |
| 501 | if (!clk_scaling->enable) |
| 502 | goto out; |
| 503 | |
| 504 | pr_debug("%s: target freq = %lu (%s)\n", mmc_hostname(host), |
| 505 | *freq, current->comm); |
| 506 | |
| 507 | if ((clk_scaling->curr_freq == *freq) || |
| 508 | clk_scaling->skip_clk_scale_freq_update) |
| 509 | goto out; |
| 510 | |
| 511 | /* No need to scale the clocks if they are gated */ |
| 512 | if (!host->ios.clock) |
| 513 | goto out; |
| 514 | |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 515 | spin_lock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 516 | if (clk_scaling->clk_scaling_in_progress) { |
| 517 | pr_debug("%s: clocks scaling is already in-progress by mmc thread\n", |
| 518 | mmc_hostname(host)); |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 519 | spin_unlock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 520 | goto out; |
| 521 | } |
| 522 | clk_scaling->need_freq_change = true; |
| 523 | clk_scaling->target_freq = *freq; |
| 524 | clk_scaling->state = *freq < clk_scaling->curr_freq ? |
| 525 | MMC_LOAD_LOW : MMC_LOAD_HIGH; |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 526 | spin_unlock_bh(&clk_scaling->lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 527 | |
| 528 | abort = __mmc_claim_host(host, &clk_scaling->devfreq_abort); |
| 529 | if (abort) |
| 530 | goto out; |
| 531 | |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 532 | if (mmc_card_sd(host->card) && host->card->sdr104_blocked) |
| 533 | goto rel_host; |
| 534 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 535 | /* |
| 536 | * In case we were able to claim host there is no need to |
| 537 | * defer the frequency change. It will be done now |
| 538 | */ |
| 539 | clk_scaling->need_freq_change = false; |
| 540 | |
| 541 | mmc_host_clk_hold(host); |
| 542 | err = mmc_clk_update_freq(host, *freq, clk_scaling->state); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 543 | if (err && err != -EAGAIN) { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 544 | pr_err("%s: clock scale to %lu failed with error %d\n", |
| 545 | mmc_hostname(host), *freq, err); |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 546 | err = mmc_recovery_fallback_lower_speed(host); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 547 | } else { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 548 | pr_debug("%s: clock change to %lu finished successfully (%s)\n", |
| 549 | mmc_hostname(host), *freq, current->comm); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 550 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 551 | |
Talel Shenhar | 3904067 | 2015-07-14 16:06:06 +0300 | [diff] [blame] | 552 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 553 | mmc_host_clk_release(host); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 554 | rel_host: |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 555 | mmc_release_host(host); |
| 556 | out: |
Veerabhadrarao Badiganti | 2f3f4eb | 2017-06-23 19:18:45 +0530 | [diff] [blame] | 557 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 558 | return err; |
Sujit Reddy Thumma | a12e92b | 2014-12-09 12:55:38 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 561 | /** |
| 562 | * mmc_deferred_scaling() - scale clocks from data path (mmc thread context) |
| 563 | * @host: pointer to mmc host structure |
| 564 | * |
| 565 | * This function does clock scaling in case "need_freq_change" flag was set |
| 566 | * by the clock scaling logic. |
| 567 | */ |
| 568 | void mmc_deferred_scaling(struct mmc_host *host) |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 569 | { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 570 | unsigned long target_freq; |
| 571 | int err; |
| 572 | |
| 573 | if (!host->clk_scaling.enable) |
| 574 | return; |
| 575 | |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 576 | if (mmc_card_sd(host->card) && host->card->sdr104_blocked) |
| 577 | return; |
| 578 | |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 579 | spin_lock_bh(&host->clk_scaling.lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 580 | |
| 581 | if (host->clk_scaling.clk_scaling_in_progress || |
| 582 | !(host->clk_scaling.need_freq_change)) { |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 583 | spin_unlock_bh(&host->clk_scaling.lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 584 | return; |
| 585 | } |
| 586 | |
| 587 | |
| 588 | atomic_inc(&host->clk_scaling.devfreq_abort); |
| 589 | target_freq = host->clk_scaling.target_freq; |
| 590 | host->clk_scaling.clk_scaling_in_progress = true; |
| 591 | host->clk_scaling.need_freq_change = false; |
Talel Shenhar | 87605ad | 2015-06-25 09:30:04 +0300 | [diff] [blame] | 592 | spin_unlock_bh(&host->clk_scaling.lock); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 593 | pr_debug("%s: doing deferred frequency change (%lu) (%s)\n", |
| 594 | mmc_hostname(host), |
| 595 | target_freq, current->comm); |
| 596 | |
| 597 | err = mmc_clk_update_freq(host, target_freq, |
| 598 | host->clk_scaling.state); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 599 | if (err && err != -EAGAIN) { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 600 | pr_err("%s: failed on deferred scale clocks (%d)\n", |
| 601 | mmc_hostname(host), err); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 602 | mmc_recovery_fallback_lower_speed(host); |
| 603 | } else { |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 604 | pr_debug("%s: clocks were successfully scaled to %lu (%s)\n", |
| 605 | mmc_hostname(host), |
| 606 | target_freq, current->comm); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 607 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 608 | host->clk_scaling.clk_scaling_in_progress = false; |
| 609 | atomic_dec(&host->clk_scaling.devfreq_abort); |
| 610 | } |
Talel Shenhar | 339a4e1 | 2015-06-29 10:50:19 +0300 | [diff] [blame] | 611 | EXPORT_SYMBOL(mmc_deferred_scaling); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 612 | |
| 613 | static int mmc_devfreq_create_freq_table(struct mmc_host *host) |
| 614 | { |
| 615 | int i; |
| 616 | struct mmc_devfeq_clk_scaling *clk_scaling = &host->clk_scaling; |
| 617 | |
| 618 | pr_debug("%s: supported: lowest=%lu, highest=%lu\n", |
| 619 | mmc_hostname(host), |
| 620 | host->card->clk_scaling_lowest, |
| 621 | host->card->clk_scaling_highest); |
| 622 | |
Veerabhadrarao Badiganti | 48887f3 | 2017-05-30 20:44:06 +0530 | [diff] [blame] | 623 | /* |
| 624 | * Create the frequency table and initialize it with default values. |
| 625 | * Initialize it with platform specific frequencies if the frequency |
| 626 | * table supplied by platform driver is present, otherwise initialize |
| 627 | * it with min and max frequencies supported by the card. |
| 628 | */ |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 629 | if (!clk_scaling->freq_table) { |
Veerabhadrarao Badiganti | 48887f3 | 2017-05-30 20:44:06 +0530 | [diff] [blame] | 630 | if (clk_scaling->pltfm_freq_table_sz) |
| 631 | clk_scaling->freq_table_sz = |
| 632 | clk_scaling->pltfm_freq_table_sz; |
| 633 | else |
| 634 | clk_scaling->freq_table_sz = 2; |
| 635 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 636 | clk_scaling->freq_table = kzalloc( |
Veerabhadrarao Badiganti | 48887f3 | 2017-05-30 20:44:06 +0530 | [diff] [blame] | 637 | (clk_scaling->freq_table_sz * |
| 638 | sizeof(*(clk_scaling->freq_table))), GFP_KERNEL); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 639 | if (!clk_scaling->freq_table) |
| 640 | return -ENOMEM; |
Veerabhadrarao Badiganti | 48887f3 | 2017-05-30 20:44:06 +0530 | [diff] [blame] | 641 | |
| 642 | if (clk_scaling->pltfm_freq_table) { |
| 643 | memcpy(clk_scaling->freq_table, |
| 644 | clk_scaling->pltfm_freq_table, |
| 645 | (clk_scaling->pltfm_freq_table_sz * |
| 646 | sizeof(*(clk_scaling->pltfm_freq_table)))); |
| 647 | } else { |
| 648 | pr_debug("%s: no frequency table defined - setting default\n", |
| 649 | mmc_hostname(host)); |
| 650 | clk_scaling->freq_table[0] = |
| 651 | host->card->clk_scaling_lowest; |
| 652 | clk_scaling->freq_table[1] = |
| 653 | host->card->clk_scaling_highest; |
| 654 | goto out; |
| 655 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | if (host->card->clk_scaling_lowest > |
| 659 | clk_scaling->freq_table[0]) |
| 660 | pr_debug("%s: frequency table undershot possible freq\n", |
| 661 | mmc_hostname(host)); |
| 662 | |
| 663 | for (i = 0; i < clk_scaling->freq_table_sz; i++) { |
| 664 | if (clk_scaling->freq_table[i] <= |
| 665 | host->card->clk_scaling_highest) |
| 666 | continue; |
| 667 | clk_scaling->freq_table[i] = |
| 668 | host->card->clk_scaling_highest; |
| 669 | clk_scaling->freq_table_sz = i + 1; |
| 670 | pr_debug("%s: frequency table overshot possible freq (%d)\n", |
| 671 | mmc_hostname(host), clk_scaling->freq_table[i]); |
| 672 | break; |
| 673 | } |
| 674 | |
| 675 | out: |
Xiaonian Wang | f3218eb | 2017-04-20 00:30:42 -0700 | [diff] [blame] | 676 | /** |
| 677 | * devfreq requires unsigned long type freq_table while the |
| 678 | * freq_table in clk_scaling is un32. Here allocates an individual |
| 679 | * memory space for it and release it when exit clock scaling. |
| 680 | */ |
| 681 | clk_scaling->devfreq_profile.freq_table = kzalloc( |
| 682 | clk_scaling->freq_table_sz * |
| 683 | sizeof(*(clk_scaling->devfreq_profile.freq_table)), |
| 684 | GFP_KERNEL); |
| 685 | if (!clk_scaling->devfreq_profile.freq_table) |
| 686 | return -ENOMEM; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 687 | clk_scaling->devfreq_profile.max_state = clk_scaling->freq_table_sz; |
| 688 | |
Xiaonian Wang | f3218eb | 2017-04-20 00:30:42 -0700 | [diff] [blame] | 689 | for (i = 0; i < clk_scaling->freq_table_sz; i++) { |
| 690 | clk_scaling->devfreq_profile.freq_table[i] = |
| 691 | clk_scaling->freq_table[i]; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 692 | pr_debug("%s: freq[%d] = %u\n", |
| 693 | mmc_hostname(host), i, clk_scaling->freq_table[i]); |
Xiaonian Wang | f3218eb | 2017-04-20 00:30:42 -0700 | [diff] [blame] | 694 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 695 | |
| 696 | return 0; |
| 697 | } |
| 698 | |
| 699 | /** |
| 700 | * mmc_init_devfreq_clk_scaling() - Initialize clock scaling |
| 701 | * @host: pointer to mmc host structure |
| 702 | * |
| 703 | * Initialize clock scaling for supported hosts. It is assumed that the caller |
| 704 | * ensure clock is running at maximum possible frequency before calling this |
| 705 | * function. Shall use struct devfreq_simple_ondemand_data to configure |
| 706 | * governor. |
| 707 | */ |
| 708 | int mmc_init_clk_scaling(struct mmc_host *host) |
| 709 | { |
| 710 | int err; |
| 711 | |
| 712 | if (!host || !host->card) { |
| 713 | pr_err("%s: unexpected host/card parameters\n", |
| 714 | __func__); |
| 715 | return -EINVAL; |
| 716 | } |
| 717 | |
| 718 | if (!mmc_can_scale_clk(host) || |
| 719 | !host->bus_ops->change_bus_speed) { |
| 720 | pr_debug("%s: clock scaling is not supported\n", |
| 721 | mmc_hostname(host)); |
| 722 | return 0; |
| 723 | } |
| 724 | |
| 725 | pr_debug("registering %s dev (%p) to devfreq", |
| 726 | mmc_hostname(host), |
| 727 | mmc_classdev(host)); |
| 728 | |
| 729 | if (host->clk_scaling.devfreq) { |
| 730 | pr_err("%s: dev is already registered for dev %p\n", |
| 731 | mmc_hostname(host), |
| 732 | mmc_dev(host)); |
| 733 | return -EPERM; |
| 734 | } |
| 735 | spin_lock_init(&host->clk_scaling.lock); |
| 736 | atomic_set(&host->clk_scaling.devfreq_abort, 0); |
| 737 | host->clk_scaling.curr_freq = host->ios.clock; |
| 738 | host->clk_scaling.clk_scaling_in_progress = false; |
| 739 | host->clk_scaling.need_freq_change = false; |
| 740 | host->clk_scaling.is_busy_started = false; |
| 741 | |
| 742 | host->clk_scaling.devfreq_profile.polling_ms = |
| 743 | host->clk_scaling.polling_delay_ms; |
| 744 | host->clk_scaling.devfreq_profile.get_dev_status = |
| 745 | mmc_devfreq_get_dev_status; |
| 746 | host->clk_scaling.devfreq_profile.target = mmc_devfreq_set_target; |
| 747 | host->clk_scaling.devfreq_profile.initial_freq = host->ios.clock; |
| 748 | |
| 749 | host->clk_scaling.ondemand_gov_data.simple_scaling = true; |
| 750 | host->clk_scaling.ondemand_gov_data.upthreshold = |
| 751 | host->clk_scaling.upthreshold; |
| 752 | host->clk_scaling.ondemand_gov_data.downdifferential = |
Subhash Jadavani | 164db7b | 2015-10-05 11:33:11 -0700 | [diff] [blame] | 753 | host->clk_scaling.upthreshold - host->clk_scaling.downthreshold; |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 754 | |
| 755 | err = mmc_devfreq_create_freq_table(host); |
| 756 | if (err) { |
| 757 | pr_err("%s: fail to create devfreq frequency table\n", |
| 758 | mmc_hostname(host)); |
| 759 | return err; |
| 760 | } |
| 761 | |
| 762 | pr_debug("%s: adding devfreq with: upthreshold=%u downthreshold=%u polling=%u\n", |
| 763 | mmc_hostname(host), |
| 764 | host->clk_scaling.ondemand_gov_data.upthreshold, |
| 765 | host->clk_scaling.ondemand_gov_data.downdifferential, |
| 766 | host->clk_scaling.devfreq_profile.polling_ms); |
| 767 | host->clk_scaling.devfreq = devfreq_add_device( |
| 768 | mmc_classdev(host), |
| 769 | &host->clk_scaling.devfreq_profile, |
| 770 | "simple_ondemand", |
| 771 | &host->clk_scaling.ondemand_gov_data); |
| 772 | if (!host->clk_scaling.devfreq) { |
| 773 | pr_err("%s: unable to register with devfreq\n", |
| 774 | mmc_hostname(host)); |
| 775 | return -EPERM; |
| 776 | } |
| 777 | |
| 778 | pr_debug("%s: clk scaling is enabled for device %s (%p) with devfreq %p (clock = %uHz)\n", |
| 779 | mmc_hostname(host), |
| 780 | dev_name(mmc_classdev(host)), |
| 781 | mmc_classdev(host), |
| 782 | host->clk_scaling.devfreq, |
| 783 | host->ios.clock); |
| 784 | |
| 785 | host->clk_scaling.enable = true; |
| 786 | |
| 787 | return err; |
| 788 | } |
| 789 | EXPORT_SYMBOL(mmc_init_clk_scaling); |
| 790 | |
| 791 | /** |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 792 | * mmc_suspend_clk_scaling() - suspend clock scaling |
| 793 | * @host: pointer to mmc host structure |
| 794 | * |
| 795 | * This API will suspend devfreq feature for the specific host. |
| 796 | * The statistics collected by mmc will be cleared. |
| 797 | * This function is intended to be called by the pm callbacks |
| 798 | * (e.g. runtime_suspend, suspend) of the mmc device |
| 799 | */ |
| 800 | int mmc_suspend_clk_scaling(struct mmc_host *host) |
| 801 | { |
| 802 | int err; |
| 803 | |
| 804 | if (!host) { |
| 805 | WARN(1, "bad host parameter\n"); |
| 806 | return -EINVAL; |
| 807 | } |
| 808 | |
| 809 | if (!mmc_can_scale_clk(host) || !host->clk_scaling.enable) |
| 810 | return 0; |
| 811 | |
| 812 | if (!host->clk_scaling.devfreq) { |
| 813 | pr_err("%s: %s: no devfreq is assosiated with this device\n", |
| 814 | mmc_hostname(host), __func__); |
| 815 | return -EPERM; |
| 816 | } |
| 817 | |
| 818 | atomic_inc(&host->clk_scaling.devfreq_abort); |
Konstantin Dorfman | 9379bf9 | 2015-12-20 15:16:40 +0200 | [diff] [blame] | 819 | wake_up(&host->wq); |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 820 | err = devfreq_suspend_device(host->clk_scaling.devfreq); |
| 821 | if (err) { |
| 822 | pr_err("%s: %s: failed to suspend devfreq\n", |
| 823 | mmc_hostname(host), __func__); |
| 824 | return err; |
| 825 | } |
| 826 | host->clk_scaling.enable = false; |
| 827 | |
| 828 | host->clk_scaling.total_busy_time_us = 0; |
| 829 | |
| 830 | pr_debug("%s: devfreq was removed\n", mmc_hostname(host)); |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | EXPORT_SYMBOL(mmc_suspend_clk_scaling); |
| 835 | |
| 836 | /** |
| 837 | * mmc_resume_clk_scaling() - resume clock scaling |
| 838 | * @host: pointer to mmc host structure |
| 839 | * |
| 840 | * This API will resume devfreq feature for the specific host. |
| 841 | * This API is intended to be called by the pm callbacks |
| 842 | * (e.g. runtime_suspend, suspend) of the mmc device |
| 843 | */ |
| 844 | int mmc_resume_clk_scaling(struct mmc_host *host) |
| 845 | { |
| 846 | int err = 0; |
Sahitya Tummala | 1a9cf8b | 2016-02-12 16:53:04 +0530 | [diff] [blame] | 847 | u32 max_clk_idx = 0; |
| 848 | u32 devfreq_max_clk = 0; |
| 849 | u32 devfreq_min_clk = 0; |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 850 | |
| 851 | if (!host) { |
| 852 | WARN(1, "bad host parameter\n"); |
| 853 | return -EINVAL; |
| 854 | } |
| 855 | |
| 856 | if (!mmc_can_scale_clk(host)) |
| 857 | return 0; |
| 858 | |
Vijay Viswanath | 450552a | 2017-05-19 16:26:33 +0530 | [diff] [blame] | 859 | /* |
| 860 | * If clock scaling is already exited when resume is called, like |
| 861 | * during mmc shutdown, it is not an error and should not fail the |
| 862 | * API calling this. |
| 863 | */ |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 864 | if (!host->clk_scaling.devfreq) { |
Vijay Viswanath | 450552a | 2017-05-19 16:26:33 +0530 | [diff] [blame] | 865 | pr_warn("%s: %s: no devfreq is assosiated with this device\n", |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 866 | mmc_hostname(host), __func__); |
Vijay Viswanath | 450552a | 2017-05-19 16:26:33 +0530 | [diff] [blame] | 867 | return 0; |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | atomic_set(&host->clk_scaling.devfreq_abort, 0); |
Sahitya Tummala | 1a9cf8b | 2016-02-12 16:53:04 +0530 | [diff] [blame] | 871 | |
| 872 | max_clk_idx = host->clk_scaling.freq_table_sz - 1; |
| 873 | devfreq_max_clk = host->clk_scaling.freq_table[max_clk_idx]; |
| 874 | devfreq_min_clk = host->clk_scaling.freq_table[0]; |
| 875 | |
| 876 | host->clk_scaling.curr_freq = devfreq_max_clk; |
Veerabhadrarao Badiganti | 4ef47bc | 2017-06-01 21:16:14 +0530 | [diff] [blame] | 877 | if (host->ios.clock < host->clk_scaling.freq_table[max_clk_idx]) |
Sahitya Tummala | 1a9cf8b | 2016-02-12 16:53:04 +0530 | [diff] [blame] | 878 | host->clk_scaling.curr_freq = devfreq_min_clk; |
| 879 | |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 880 | host->clk_scaling.clk_scaling_in_progress = false; |
| 881 | host->clk_scaling.need_freq_change = false; |
| 882 | |
| 883 | err = devfreq_resume_device(host->clk_scaling.devfreq); |
| 884 | if (err) { |
| 885 | pr_err("%s: %s: failed to resume devfreq (%d)\n", |
| 886 | mmc_hostname(host), __func__, err); |
| 887 | } else { |
| 888 | host->clk_scaling.enable = true; |
| 889 | pr_debug("%s: devfreq resumed\n", mmc_hostname(host)); |
| 890 | } |
| 891 | |
| 892 | return err; |
| 893 | } |
| 894 | EXPORT_SYMBOL(mmc_resume_clk_scaling); |
| 895 | |
| 896 | /** |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 897 | * mmc_exit_devfreq_clk_scaling() - Disable clock scaling |
| 898 | * @host: pointer to mmc host structure |
| 899 | * |
| 900 | * Disable clock scaling permanently. |
| 901 | */ |
| 902 | int mmc_exit_clk_scaling(struct mmc_host *host) |
| 903 | { |
| 904 | int err; |
| 905 | |
| 906 | if (!host) { |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 907 | pr_err("%s: bad host parameter\n", __func__); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 908 | WARN_ON(1); |
| 909 | return -EINVAL; |
| 910 | } |
| 911 | |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 912 | if (!mmc_can_scale_clk(host)) |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 913 | return 0; |
| 914 | |
| 915 | if (!host->clk_scaling.devfreq) { |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 916 | pr_err("%s: %s: no devfreq is assosiated with this device\n", |
| 917 | mmc_hostname(host), __func__); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 918 | return -EPERM; |
| 919 | } |
| 920 | |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 921 | err = mmc_suspend_clk_scaling(host); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 922 | if (err) { |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 923 | pr_err("%s: %s: fail to suspend clock scaling (%d)\n", |
| 924 | mmc_hostname(host), __func__, err); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 925 | return err; |
| 926 | } |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 927 | |
| 928 | err = devfreq_remove_device(host->clk_scaling.devfreq); |
| 929 | if (err) { |
Talel Shenhar | 999102c | 2015-09-06 16:38:42 +0300 | [diff] [blame] | 930 | pr_err("%s: remove devfreq failed (%d)\n", |
| 931 | mmc_hostname(host), err); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 932 | return err; |
| 933 | } |
| 934 | |
Xiaonian Wang | f3218eb | 2017-04-20 00:30:42 -0700 | [diff] [blame] | 935 | kfree(host->clk_scaling.devfreq_profile.freq_table); |
| 936 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 937 | host->clk_scaling.devfreq = NULL; |
| 938 | atomic_set(&host->clk_scaling.devfreq_abort, 1); |
Veerabhadrarao Badiganti | 48887f3 | 2017-05-30 20:44:06 +0530 | [diff] [blame] | 939 | |
| 940 | kfree(host->clk_scaling.freq_table); |
| 941 | host->clk_scaling.freq_table = NULL; |
| 942 | |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 943 | pr_debug("%s: devfreq was removed\n", mmc_hostname(host)); |
| 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | EXPORT_SYMBOL(mmc_exit_clk_scaling); |
| 948 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 949 | static inline void mmc_complete_cmd(struct mmc_request *mrq) |
| 950 | { |
| 951 | if (mrq->cap_cmd_during_tfr && !completion_done(&mrq->cmd_completion)) |
| 952 | complete_all(&mrq->cmd_completion); |
| 953 | } |
| 954 | |
| 955 | void mmc_command_done(struct mmc_host *host, struct mmc_request *mrq) |
| 956 | { |
| 957 | if (!mrq->cap_cmd_during_tfr) |
| 958 | return; |
| 959 | |
| 960 | mmc_complete_cmd(mrq); |
| 961 | |
| 962 | pr_debug("%s: cmd done, tfr ongoing (CMD%u)\n", |
| 963 | mmc_hostname(host), mrq->cmd->opcode); |
| 964 | } |
| 965 | EXPORT_SYMBOL(mmc_command_done); |
| 966 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | /** |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 968 | * mmc_request_done - finish processing an MMC request |
| 969 | * @host: MMC host which completed request |
| 970 | * @mrq: MMC request which request |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | * |
| 972 | * MMC drivers should call this function when they have completed |
Russell King | fe10c6a | 2006-05-04 13:51:45 +0100 | [diff] [blame] | 973 | * their processing of a request. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | */ |
| 975 | void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq) |
| 976 | { |
| 977 | struct mmc_command *cmd = mrq->cmd; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 978 | int err = cmd->error; |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 979 | #ifdef CONFIG_MMC_PERF_PROFILING |
| 980 | ktime_t diff; |
| 981 | #endif |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 982 | |
| 983 | if (host->clk_scaling.is_busy_started) |
| 984 | mmc_clk_scaling_stop_busy(host, true); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 985 | |
Adrian Hunter | bd11e8b | 2015-05-07 13:10:22 +0300 | [diff] [blame] | 986 | /* Flag re-tuning needed on CRC errors */ |
Chaotian Jing | 031277d | 2015-09-30 17:37:18 +0800 | [diff] [blame] | 987 | if ((cmd->opcode != MMC_SEND_TUNING_BLOCK && |
| 988 | cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200) && |
| 989 | (err == -EILSEQ || (mrq->sbc && mrq->sbc->error == -EILSEQ) || |
Adrian Hunter | bd11e8b | 2015-05-07 13:10:22 +0300 | [diff] [blame] | 990 | (mrq->data && mrq->data->error == -EILSEQ) || |
Chaotian Jing | 031277d | 2015-09-30 17:37:18 +0800 | [diff] [blame] | 991 | (mrq->stop && mrq->stop->error == -EILSEQ))) |
Adrian Hunter | bd11e8b | 2015-05-07 13:10:22 +0300 | [diff] [blame] | 992 | mmc_retune_needed(host); |
| 993 | |
David Brownell | af51715 | 2007-08-08 09:11:32 -0700 | [diff] [blame] | 994 | if (err && cmd->retries && mmc_host_is_spi(host)) { |
| 995 | if (cmd->resp[0] & R1_SPI_ILLEGAL_COMMAND) |
| 996 | cmd->retries = 0; |
| 997 | } |
| 998 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 999 | if (host->ongoing_mrq == mrq) |
| 1000 | host->ongoing_mrq = NULL; |
| 1001 | |
| 1002 | mmc_complete_cmd(mrq); |
| 1003 | |
Baolin Wang | 7962fc3 | 2016-03-31 11:16:27 +0800 | [diff] [blame] | 1004 | trace_mmc_request_done(host, mrq); |
| 1005 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1006 | if (err && cmd->retries && !mmc_card_removed(host->card)) { |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1007 | /* |
| 1008 | * Request starter must handle retries - see |
| 1009 | * mmc_wait_for_req_done(). |
| 1010 | */ |
| 1011 | if (mrq->done) |
| 1012 | mrq->done(mrq); |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1013 | } else { |
Per Forlin | 1b676f7 | 2011-08-19 14:52:37 +0200 | [diff] [blame] | 1014 | mmc_should_fail_request(host, mrq); |
| 1015 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1016 | if (!host->ongoing_mrq) |
| 1017 | led_trigger_event(host->led, LED_OFF); |
Pierre Ossman | af8350c | 2007-09-24 07:15:48 +0200 | [diff] [blame] | 1018 | |
Andrew Gabbasov | fc75b70 | 2014-10-01 07:14:10 -0500 | [diff] [blame] | 1019 | if (mrq->sbc) { |
| 1020 | pr_debug("%s: req done <CMD%u>: %d: %08x %08x %08x %08x\n", |
| 1021 | mmc_hostname(host), mrq->sbc->opcode, |
| 1022 | mrq->sbc->error, |
| 1023 | mrq->sbc->resp[0], mrq->sbc->resp[1], |
| 1024 | mrq->sbc->resp[2], mrq->sbc->resp[3]); |
| 1025 | } |
| 1026 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1027 | pr_debug("%s: req done (CMD%u): %d: %08x %08x %08x %08x\n", |
| 1028 | mmc_hostname(host), cmd->opcode, err, |
| 1029 | cmd->resp[0], cmd->resp[1], |
| 1030 | cmd->resp[2], cmd->resp[3]); |
| 1031 | |
| 1032 | if (mrq->data) { |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 1033 | #ifdef CONFIG_MMC_PERF_PROFILING |
Subhash Jadavani | ffb55e8 | 2012-03-25 11:15:41 +0530 | [diff] [blame] | 1034 | if (host->perf_enable) { |
| 1035 | diff = ktime_sub(ktime_get(), host->perf.start); |
| 1036 | if (mrq->data->flags == MMC_DATA_READ) { |
| 1037 | host->perf.rbytes_drv += |
| 1038 | mrq->data->bytes_xfered; |
| 1039 | host->perf.rtime_drv = |
| 1040 | ktime_add(host->perf.rtime_drv, |
| 1041 | diff); |
| 1042 | } else { |
| 1043 | host->perf.wbytes_drv += |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 1044 | mrq->data->bytes_xfered; |
Subhash Jadavani | ffb55e8 | 2012-03-25 11:15:41 +0530 | [diff] [blame] | 1045 | host->perf.wtime_drv = |
| 1046 | ktime_add(host->perf.wtime_drv, |
| 1047 | diff); |
| 1048 | } |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 1049 | } |
| 1050 | #endif |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1051 | pr_debug("%s: %d bytes transferred: %d\n", |
| 1052 | mmc_hostname(host), |
| 1053 | mrq->data->bytes_xfered, mrq->data->error); |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 1054 | #ifdef CONFIG_BLOCK |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 1055 | if (mrq->lat_hist_enabled) { |
| 1056 | ktime_t completion; |
| 1057 | u_int64_t delta_us; |
| 1058 | |
| 1059 | completion = ktime_get(); |
| 1060 | delta_us = ktime_us_delta(completion, |
| 1061 | mrq->io_start); |
| 1062 | blk_update_latency_hist(&host->io_lat_s, |
| 1063 | (mrq->data->flags & MMC_DATA_READ), |
| 1064 | delta_us); |
| 1065 | } |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 1066 | #endif |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | if (mrq->stop) { |
| 1070 | pr_debug("%s: (CMD%u): %d: %08x %08x %08x %08x\n", |
| 1071 | mmc_hostname(host), mrq->stop->opcode, |
| 1072 | mrq->stop->error, |
| 1073 | mrq->stop->resp[0], mrq->stop->resp[1], |
| 1074 | mrq->stop->resp[2], mrq->stop->resp[3]); |
| 1075 | } |
| 1076 | |
| 1077 | if (mrq->done) |
| 1078 | mrq->done(mrq); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1079 | |
| 1080 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | EXPORT_SYMBOL(mmc_request_done); |
| 1085 | |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1086 | static void __mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
| 1087 | { |
| 1088 | int err; |
| 1089 | |
| 1090 | /* Assumes host controller has been runtime resumed by mmc_claim_host */ |
| 1091 | err = mmc_retune(host); |
| 1092 | if (err) { |
| 1093 | mrq->cmd->error = err; |
| 1094 | mmc_request_done(host, mrq); |
| 1095 | return; |
| 1096 | } |
| 1097 | |
Hans de Goede | 5d3f6ef | 2015-09-22 17:30:25 +0200 | [diff] [blame] | 1098 | /* |
| 1099 | * For sdio rw commands we must wait for card busy otherwise some |
| 1100 | * sdio devices won't work properly. |
| 1101 | */ |
| 1102 | if (mmc_is_io_op(mrq->cmd->opcode) && host->ops->card_busy) { |
| 1103 | int tries = 500; /* Wait aprox 500ms at maximum */ |
| 1104 | |
| 1105 | while (host->ops->card_busy(host) && --tries) |
| 1106 | mmc_delay(1); |
| 1107 | |
| 1108 | if (tries == 0) { |
| 1109 | mrq->cmd->error = -EBUSY; |
| 1110 | mmc_request_done(host, mrq); |
| 1111 | return; |
| 1112 | } |
| 1113 | } |
| 1114 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1115 | if (mrq->cap_cmd_during_tfr) { |
| 1116 | host->ongoing_mrq = mrq; |
| 1117 | /* |
| 1118 | * Retry path could come through here without having waiting on |
| 1119 | * cmd_completion, so ensure it is reinitialised. |
| 1120 | */ |
| 1121 | reinit_completion(&mrq->cmd_completion); |
| 1122 | } |
| 1123 | |
Baolin Wang | 7962fc3 | 2016-03-31 11:16:27 +0800 | [diff] [blame] | 1124 | trace_mmc_request_start(host, mrq); |
| 1125 | |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1126 | host->ops->request(host, mrq); |
| 1127 | } |
| 1128 | |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1129 | static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | { |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 1131 | #ifdef CONFIG_MMC_DEBUG |
| 1132 | unsigned int i, sz; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 1133 | struct scatterlist *sg; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 1134 | #endif |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1135 | mmc_retune_hold(host); |
| 1136 | |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1137 | if (mmc_card_removed(host->card)) |
| 1138 | return -ENOMEDIUM; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 1139 | |
Jaehoon Chung | 7b2fd4f | 2012-02-07 14:13:10 +0900 | [diff] [blame] | 1140 | if (mrq->sbc) { |
| 1141 | pr_debug("<%s: starting CMD%u arg %08x flags %08x>\n", |
| 1142 | mmc_hostname(host), mrq->sbc->opcode, |
| 1143 | mrq->sbc->arg, mrq->sbc->flags); |
| 1144 | } |
| 1145 | |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 1146 | pr_debug("%s: starting CMD%u arg %08x flags %08x\n", |
| 1147 | mmc_hostname(host), mrq->cmd->opcode, |
| 1148 | mrq->cmd->arg, mrq->cmd->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1149 | |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1150 | if (mrq->data) { |
| 1151 | pr_debug("%s: blksz %d blocks %d flags %08x " |
| 1152 | "tsac %d ms nsac %d\n", |
| 1153 | mmc_hostname(host), mrq->data->blksz, |
| 1154 | mrq->data->blocks, mrq->data->flags, |
Pierre Ossman | ce252ed | 2007-08-07 14:06:18 +0200 | [diff] [blame] | 1155 | mrq->data->timeout_ns / 1000000, |
Pierre Ossman | e4d2170 | 2007-07-24 21:46:49 +0200 | [diff] [blame] | 1156 | mrq->data->timeout_clks); |
| 1157 | } |
| 1158 | |
| 1159 | if (mrq->stop) { |
| 1160 | pr_debug("%s: CMD%u arg %08x flags %08x\n", |
| 1161 | mmc_hostname(host), mrq->stop->opcode, |
| 1162 | mrq->stop->arg, mrq->stop->flags); |
| 1163 | } |
| 1164 | |
Pierre Ossman | f22ee4e | 2006-12-26 15:11:23 +0100 | [diff] [blame] | 1165 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | |
| 1167 | mrq->cmd->error = 0; |
| 1168 | mrq->cmd->mrq = mrq; |
Andrew Gabbasov | cce411e | 2014-10-01 07:14:09 -0500 | [diff] [blame] | 1169 | if (mrq->sbc) { |
| 1170 | mrq->sbc->error = 0; |
| 1171 | mrq->sbc->mrq = mrq; |
| 1172 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | if (mrq->data) { |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1174 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 1175 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 1176 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 1177 | host->max_req_size); |
Pierre Ossman | fe4a3c7 | 2006-11-21 17:54:23 +0100 | [diff] [blame] | 1178 | |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 1179 | #ifdef CONFIG_MMC_DEBUG |
| 1180 | sz = 0; |
Pierre Ossman | a84756c | 2008-07-29 01:09:37 +0200 | [diff] [blame] | 1181 | for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i) |
| 1182 | sz += sg->length; |
Pierre Ossman | 976d927 | 2007-04-13 22:47:01 +0200 | [diff] [blame] | 1183 | BUG_ON(sz != mrq->data->blocks * mrq->data->blksz); |
| 1184 | #endif |
| 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | mrq->cmd->data = mrq->data; |
| 1187 | mrq->data->error = 0; |
| 1188 | mrq->data->mrq = mrq; |
| 1189 | if (mrq->stop) { |
| 1190 | mrq->data->stop = mrq->stop; |
| 1191 | mrq->stop->error = 0; |
| 1192 | mrq->stop->mrq = mrq; |
| 1193 | } |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 1194 | #ifdef CONFIG_MMC_PERF_PROFILING |
Subhash Jadavani | ffb55e8 | 2012-03-25 11:15:41 +0530 | [diff] [blame] | 1195 | if (host->perf_enable) |
| 1196 | host->perf.start = ktime_get(); |
Aparna Mallavarapu | eb2bbc0 | 2010-06-11 18:13:05 +0530 | [diff] [blame] | 1197 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | } |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1199 | mmc_host_clk_hold(host); |
Pierre Tardy | 66c036e | 2011-02-06 19:02:48 +0100 | [diff] [blame] | 1200 | led_trigger_event(host->led, LED_FULL); |
Sujit Reddy Thumma | cb18d85 | 2014-12-04 09:57:23 +0200 | [diff] [blame] | 1201 | |
Talel Shenhar | 8f00420 | 2015-08-18 02:28:56 +0300 | [diff] [blame] | 1202 | if (mmc_is_data_request(mrq)) { |
| 1203 | mmc_deferred_scaling(host); |
Talel Shenhar | 7dc5f79 | 2015-05-18 12:12:48 +0300 | [diff] [blame] | 1204 | mmc_clk_scaling_start_busy(host, true); |
Talel Shenhar | 8f00420 | 2015-08-18 02:28:56 +0300 | [diff] [blame] | 1205 | } |
Sujit Reddy Thumma | cb18d85 | 2014-12-04 09:57:23 +0200 | [diff] [blame] | 1206 | |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1207 | __mmc_start_request(host, mrq); |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1208 | |
| 1209 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1210 | } |
| 1211 | |
Vijay Viswanath | 778491f | 2017-11-08 14:09:01 +0530 | [diff] [blame] | 1212 | static int mmc_cmdq_check_retune(struct mmc_host *host) |
| 1213 | { |
| 1214 | bool cmdq_mode; |
| 1215 | int err = 0; |
| 1216 | |
| 1217 | if (!host->need_retune || host->doing_retune || !host->card || |
| 1218 | mmc_card_hs400es(host->card) || |
| 1219 | (host->ios.clock <= MMC_HIGH_DDR_MAX_DTR)) |
| 1220 | return 0; |
| 1221 | |
| 1222 | cmdq_mode = mmc_card_cmdq(host->card); |
| 1223 | if (cmdq_mode) { |
| 1224 | err = mmc_cmdq_halt(host, true); |
| 1225 | if (err) { |
| 1226 | pr_err("%s: %s: failed halting queue (%d)\n", |
| 1227 | mmc_hostname(host), __func__, err); |
| 1228 | host->cmdq_ops->dumpstate(host); |
| 1229 | goto halt_failed; |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | mmc_retune_hold(host); |
| 1234 | err = mmc_retune(host); |
| 1235 | mmc_retune_release(host); |
| 1236 | |
| 1237 | if (cmdq_mode) { |
| 1238 | if (mmc_cmdq_halt(host, false)) { |
| 1239 | pr_err("%s: %s: cmdq unhalt failed\n", |
| 1240 | mmc_hostname(host), __func__); |
| 1241 | host->cmdq_ops->dumpstate(host); |
| 1242 | } |
| 1243 | } |
| 1244 | |
| 1245 | halt_failed: |
| 1246 | pr_debug("%s: %s: Retuning done err: %d\n", |
| 1247 | mmc_hostname(host), __func__, err); |
| 1248 | |
| 1249 | return err; |
| 1250 | } |
| 1251 | |
Veerabhadrarao Badiganti | 4efc0a6 | 2017-10-09 20:13:45 +0530 | [diff] [blame] | 1252 | static int mmc_start_cmdq_request(struct mmc_host *host, |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1253 | struct mmc_request *mrq) |
| 1254 | { |
Veerabhadrarao Badiganti | 4efc0a6 | 2017-10-09 20:13:45 +0530 | [diff] [blame] | 1255 | int ret = 0; |
| 1256 | |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1257 | if (mrq->data) { |
| 1258 | pr_debug("%s: blksz %d blocks %d flags %08x tsac %lu ms nsac %d\n", |
| 1259 | mmc_hostname(host), mrq->data->blksz, |
| 1260 | mrq->data->blocks, mrq->data->flags, |
| 1261 | mrq->data->timeout_ns / NSEC_PER_MSEC, |
| 1262 | mrq->data->timeout_clks); |
| 1263 | |
| 1264 | BUG_ON(mrq->data->blksz > host->max_blk_size); |
| 1265 | BUG_ON(mrq->data->blocks > host->max_blk_count); |
| 1266 | BUG_ON(mrq->data->blocks * mrq->data->blksz > |
| 1267 | host->max_req_size); |
| 1268 | mrq->data->error = 0; |
| 1269 | mrq->data->mrq = mrq; |
| 1270 | } |
| 1271 | |
| 1272 | if (mrq->cmd) { |
| 1273 | mrq->cmd->error = 0; |
| 1274 | mrq->cmd->mrq = mrq; |
| 1275 | } |
| 1276 | |
| 1277 | mmc_host_clk_hold(host); |
Vijay Viswanath | 778491f | 2017-11-08 14:09:01 +0530 | [diff] [blame] | 1278 | mmc_cmdq_check_retune(host); |
Veerabhadrarao Badiganti | 4efc0a6 | 2017-10-09 20:13:45 +0530 | [diff] [blame] | 1279 | if (likely(host->cmdq_ops->request)) { |
| 1280 | ret = host->cmdq_ops->request(host, mrq); |
| 1281 | } else { |
| 1282 | ret = -ENOENT; |
| 1283 | pr_err("%s: %s: cmdq request host op is not available\n", |
| 1284 | mmc_hostname(host), __func__); |
| 1285 | } |
| 1286 | |
| 1287 | if (ret) { |
| 1288 | mmc_host_clk_release(host); |
| 1289 | pr_err("%s: %s: issue request failed, err=%d\n", |
| 1290 | mmc_hostname(host), __func__, ret); |
| 1291 | } |
| 1292 | |
| 1293 | return ret; |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1294 | } |
| 1295 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1296 | /** |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1297 | * mmc_blk_init_bkops_statistics - initialize bkops statistics |
| 1298 | * @card: MMC card to start BKOPS |
| 1299 | * |
| 1300 | * Initialize and enable the bkops statistics |
| 1301 | */ |
| 1302 | void mmc_blk_init_bkops_statistics(struct mmc_card *card) |
| 1303 | { |
| 1304 | int i; |
| 1305 | struct mmc_bkops_stats *stats; |
| 1306 | |
| 1307 | if (!card) |
| 1308 | return; |
| 1309 | |
| 1310 | stats = &card->bkops.stats; |
| 1311 | spin_lock(&stats->lock); |
| 1312 | |
| 1313 | stats->manual_start = 0; |
| 1314 | stats->hpi = 0; |
| 1315 | stats->auto_start = 0; |
| 1316 | stats->auto_stop = 0; |
| 1317 | for (i = 0 ; i < MMC_BKOPS_NUM_SEVERITY_LEVELS ; i++) |
| 1318 | stats->level[i] = 0; |
| 1319 | stats->enabled = true; |
| 1320 | |
| 1321 | spin_unlock(&stats->lock); |
| 1322 | } |
| 1323 | EXPORT_SYMBOL(mmc_blk_init_bkops_statistics); |
| 1324 | |
| 1325 | static void mmc_update_bkops_hpi(struct mmc_bkops_stats *stats) |
| 1326 | { |
| 1327 | spin_lock_irq(&stats->lock); |
| 1328 | if (stats->enabled) |
| 1329 | stats->hpi++; |
| 1330 | spin_unlock_irq(&stats->lock); |
| 1331 | } |
| 1332 | |
| 1333 | static void mmc_update_bkops_start(struct mmc_bkops_stats *stats) |
| 1334 | { |
| 1335 | spin_lock_irq(&stats->lock); |
| 1336 | if (stats->enabled) |
| 1337 | stats->manual_start++; |
| 1338 | spin_unlock_irq(&stats->lock); |
| 1339 | } |
| 1340 | |
| 1341 | static void mmc_update_bkops_auto_on(struct mmc_bkops_stats *stats) |
| 1342 | { |
| 1343 | spin_lock_irq(&stats->lock); |
| 1344 | if (stats->enabled) |
| 1345 | stats->auto_start++; |
| 1346 | spin_unlock_irq(&stats->lock); |
| 1347 | } |
| 1348 | |
| 1349 | static void mmc_update_bkops_auto_off(struct mmc_bkops_stats *stats) |
| 1350 | { |
| 1351 | spin_lock_irq(&stats->lock); |
| 1352 | if (stats->enabled) |
| 1353 | stats->auto_stop++; |
| 1354 | spin_unlock_irq(&stats->lock); |
| 1355 | } |
| 1356 | |
| 1357 | static void mmc_update_bkops_level(struct mmc_bkops_stats *stats, |
| 1358 | unsigned level) |
| 1359 | { |
| 1360 | BUG_ON(level >= MMC_BKOPS_NUM_SEVERITY_LEVELS); |
| 1361 | spin_lock_irq(&stats->lock); |
| 1362 | if (stats->enabled) |
| 1363 | stats->level[level]++; |
| 1364 | spin_unlock_irq(&stats->lock); |
| 1365 | } |
| 1366 | |
| 1367 | /** |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1368 | * mmc_set_auto_bkops - set auto BKOPS for supported cards |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1369 | * @card: MMC card to start BKOPS |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1370 | * @enable: enable/disable flag |
| 1371 | * Configure the card to run automatic BKOPS. |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1372 | * |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1373 | * Should be called when host is claimed. |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1374 | */ |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1375 | int mmc_set_auto_bkops(struct mmc_card *card, bool enable) |
| 1376 | { |
| 1377 | int ret = 0; |
| 1378 | u8 bkops_en; |
| 1379 | |
| 1380 | BUG_ON(!card); |
| 1381 | enable = !!enable; |
| 1382 | |
| 1383 | if (unlikely(!mmc_card_support_auto_bkops(card))) { |
| 1384 | pr_err("%s: %s: card doesn't support auto bkops\n", |
| 1385 | mmc_hostname(card->host), __func__); |
| 1386 | return -EPERM; |
| 1387 | } |
| 1388 | |
| 1389 | if (enable) { |
| 1390 | if (mmc_card_doing_auto_bkops(card)) |
| 1391 | goto out; |
Subhash Jadavani | 0a3b83c | 2016-05-12 11:58:15 -0700 | [diff] [blame] | 1392 | bkops_en = card->ext_csd.bkops_en | EXT_CSD_BKOPS_AUTO_EN; |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1393 | } else { |
| 1394 | if (!mmc_card_doing_auto_bkops(card)) |
| 1395 | goto out; |
Subhash Jadavani | 0a3b83c | 2016-05-12 11:58:15 -0700 | [diff] [blame] | 1396 | bkops_en = card->ext_csd.bkops_en & ~EXT_CSD_BKOPS_AUTO_EN; |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_EN, |
| 1400 | bkops_en, 0); |
| 1401 | if (ret) { |
| 1402 | pr_err("%s: %s: error in setting auto bkops to %d (%d)\n", |
| 1403 | mmc_hostname(card->host), __func__, enable, ret); |
| 1404 | } else { |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1405 | if (enable) { |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1406 | mmc_card_set_auto_bkops(card); |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1407 | mmc_update_bkops_auto_on(&card->bkops.stats); |
| 1408 | } else { |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1409 | mmc_card_clr_auto_bkops(card); |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1410 | mmc_update_bkops_auto_off(&card->bkops.stats); |
| 1411 | } |
Subhash Jadavani | 0a3b83c | 2016-05-12 11:58:15 -0700 | [diff] [blame] | 1412 | card->ext_csd.bkops_en = bkops_en; |
Konstantin Dorfman | 56380b1 | 2015-12-31 14:45:48 +0200 | [diff] [blame] | 1413 | pr_debug("%s: %s: bkops state %x\n", |
| 1414 | mmc_hostname(card->host), __func__, bkops_en); |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1415 | } |
| 1416 | out: |
| 1417 | return ret; |
| 1418 | } |
| 1419 | EXPORT_SYMBOL(mmc_set_auto_bkops); |
| 1420 | |
| 1421 | /** |
| 1422 | * mmc_check_bkops - check BKOPS for supported cards |
| 1423 | * @card: MMC card to check BKOPS |
| 1424 | * |
| 1425 | * Read the BKOPS status in order to determine whether the |
| 1426 | * card requires bkops to be started. |
| 1427 | */ |
| 1428 | void mmc_check_bkops(struct mmc_card *card) |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1429 | { |
| 1430 | int err; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1431 | |
| 1432 | BUG_ON(!card); |
| 1433 | |
Konstantin Dorfman | 56380b1 | 2015-12-31 14:45:48 +0200 | [diff] [blame] | 1434 | if (mmc_card_doing_bkops(card)) |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1435 | return; |
| 1436 | |
| 1437 | err = mmc_read_bkops_status(card); |
| 1438 | if (err) { |
| 1439 | pr_err("%s: Failed to read bkops status: %d\n", |
| 1440 | mmc_hostname(card->host), err); |
| 1441 | return; |
| 1442 | } |
| 1443 | |
Konstantin Dorfman | 56380b1 | 2015-12-31 14:45:48 +0200 | [diff] [blame] | 1444 | card->bkops.needs_check = false; |
| 1445 | |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1446 | mmc_update_bkops_level(&card->bkops.stats, |
| 1447 | card->ext_csd.raw_bkops_status); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1448 | |
Konstantin Dorfman | 56380b1 | 2015-12-31 14:45:48 +0200 | [diff] [blame] | 1449 | card->bkops.needs_bkops = card->ext_csd.raw_bkops_status > 0; |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1450 | } |
| 1451 | EXPORT_SYMBOL(mmc_check_bkops); |
| 1452 | |
| 1453 | /** |
| 1454 | * mmc_start_manual_bkops - start BKOPS for supported cards |
| 1455 | * @card: MMC card to start BKOPS |
| 1456 | * |
Maya Erez | 7ffa163 | 2015-09-24 14:17:08 +0300 | [diff] [blame] | 1457 | * Send START_BKOPS to the card. |
| 1458 | * The function should be called with claimed host. |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1459 | */ |
| 1460 | void mmc_start_manual_bkops(struct mmc_card *card) |
| 1461 | { |
| 1462 | int err; |
| 1463 | |
| 1464 | BUG_ON(!card); |
| 1465 | |
| 1466 | if (unlikely(!mmc_card_configured_manual_bkops(card))) |
| 1467 | return; |
| 1468 | |
| 1469 | if (mmc_card_doing_bkops(card)) |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1470 | return; |
| 1471 | |
Adrian Hunter | 66073d8 | 2015-05-07 13:10:17 +0300 | [diff] [blame] | 1472 | mmc_retune_hold(card->host); |
| 1473 | |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1474 | err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_START, |
| 1475 | 1, 0, false, true, false); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1476 | if (err) { |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1477 | pr_err("%s: Error %d starting manual bkops\n", |
| 1478 | mmc_hostname(card->host), err); |
| 1479 | } else { |
| 1480 | mmc_card_set_doing_bkops(card); |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 1481 | mmc_update_bkops_start(&card->bkops.stats); |
Konstantin Dorfman | 56380b1 | 2015-12-31 14:45:48 +0200 | [diff] [blame] | 1482 | card->bkops.needs_bkops = false; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1485 | mmc_retune_release(card->host); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1486 | } |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1487 | EXPORT_SYMBOL(mmc_start_manual_bkops); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1488 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1489 | /* |
| 1490 | * mmc_wait_data_done() - done callback for data request |
| 1491 | * @mrq: done data request |
| 1492 | * |
| 1493 | * Wakes up mmc context, passed as a callback to host controller driver |
| 1494 | */ |
| 1495 | static void mmc_wait_data_done(struct mmc_request *mrq) |
| 1496 | { |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1497 | unsigned long flags; |
Jialing Fu | 71f8a4b | 2015-08-28 11:13:09 +0800 | [diff] [blame] | 1498 | struct mmc_context_info *context_info = &mrq->host->context_info; |
| 1499 | |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1500 | spin_lock_irqsave(&context_info->lock, flags); |
Jialing Fu | 71f8a4b | 2015-08-28 11:13:09 +0800 | [diff] [blame] | 1501 | context_info->is_done_rcv = true; |
| 1502 | wake_up_interruptible(&context_info->wait); |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1503 | spin_unlock_irqrestore(&context_info->lock, flags); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1504 | } |
| 1505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | static void mmc_wait_done(struct mmc_request *mrq) |
| 1507 | { |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1508 | complete(&mrq->completion); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | } |
| 1510 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1511 | static inline void mmc_wait_ongoing_tfr_cmd(struct mmc_host *host) |
| 1512 | { |
| 1513 | struct mmc_request *ongoing_mrq = READ_ONCE(host->ongoing_mrq); |
| 1514 | |
| 1515 | /* |
| 1516 | * If there is an ongoing transfer, wait for the command line to become |
| 1517 | * available. |
| 1518 | */ |
| 1519 | if (ongoing_mrq && !completion_done(&ongoing_mrq->cmd_completion)) |
| 1520 | wait_for_completion(&ongoing_mrq->cmd_completion); |
| 1521 | } |
| 1522 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1523 | /* |
| 1524 | *__mmc_start_data_req() - starts data request |
| 1525 | * @host: MMC host to start the request |
| 1526 | * @mrq: data request to start |
| 1527 | * |
| 1528 | * Sets the done callback to be called when request is completed by the card. |
| 1529 | * Starts data mmc request execution |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1530 | * If an ongoing transfer is already in progress, wait for the command line |
| 1531 | * to become available before sending another command. |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1532 | */ |
| 1533 | static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq) |
| 1534 | { |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1535 | int err; |
| 1536 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1537 | mmc_wait_ongoing_tfr_cmd(host); |
| 1538 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1539 | mrq->done = mmc_wait_data_done; |
| 1540 | mrq->host = host; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1541 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1542 | init_completion(&mrq->cmd_completion); |
| 1543 | |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1544 | err = mmc_start_request(host, mrq); |
| 1545 | if (err) { |
| 1546 | mrq->cmd->error = err; |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1547 | mmc_complete_cmd(mrq); |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1548 | mmc_wait_data_done(mrq); |
| 1549 | } |
| 1550 | |
| 1551 | return err; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1552 | } |
| 1553 | |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 1554 | static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq) |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1555 | { |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1556 | int err; |
| 1557 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1558 | mmc_wait_ongoing_tfr_cmd(host); |
| 1559 | |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1560 | init_completion(&mrq->completion); |
| 1561 | mrq->done = mmc_wait_done; |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1562 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1563 | init_completion(&mrq->cmd_completion); |
| 1564 | |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1565 | err = mmc_start_request(host, mrq); |
| 1566 | if (err) { |
| 1567 | mrq->cmd->error = err; |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1568 | mmc_complete_cmd(mrq); |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1569 | complete(&mrq->completion); |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1570 | } |
Adrian Hunter | f100c1c | 2014-12-05 19:41:02 +0200 | [diff] [blame] | 1571 | |
| 1572 | return err; |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1573 | } |
| 1574 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1575 | /* |
| 1576 | * mmc_wait_for_data_req_done() - wait for request completed |
| 1577 | * @host: MMC host to prepare the command. |
| 1578 | * @mrq: MMC request to wait for |
| 1579 | * |
| 1580 | * Blocks MMC context till host controller will ack end of data request |
| 1581 | * execution or new request notification arrives from the block layer. |
| 1582 | * Handles command retries. |
| 1583 | * |
| 1584 | * Returns enum mmc_blk_status after checking errors. |
| 1585 | */ |
| 1586 | static int mmc_wait_for_data_req_done(struct mmc_host *host, |
| 1587 | struct mmc_request *mrq, |
| 1588 | struct mmc_async_req *next_req) |
| 1589 | { |
| 1590 | struct mmc_command *cmd; |
| 1591 | struct mmc_context_info *context_info = &host->context_info; |
| 1592 | int err; |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1593 | bool is_done_rcv = false; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1594 | unsigned long flags; |
| 1595 | |
| 1596 | while (1) { |
| 1597 | wait_event_interruptible(context_info->wait, |
| 1598 | (context_info->is_done_rcv || |
| 1599 | context_info->is_new_req)); |
| 1600 | spin_lock_irqsave(&context_info->lock, flags); |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1601 | is_done_rcv = context_info->is_done_rcv; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1602 | context_info->is_waiting_last_req = false; |
| 1603 | spin_unlock_irqrestore(&context_info->lock, flags); |
Sujit Reddy Thumma | d67ca6b | 2014-12-09 20:44:41 +0200 | [diff] [blame] | 1604 | if (is_done_rcv) { |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1605 | context_info->is_done_rcv = false; |
| 1606 | context_info->is_new_req = false; |
| 1607 | cmd = mrq->cmd; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1608 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1609 | if (!cmd->error || !cmd->retries || |
| 1610 | mmc_card_removed(host->card)) { |
| 1611 | err = host->areq->err_check(host->card, |
| 1612 | host->areq); |
| 1613 | break; /* return err */ |
| 1614 | } else { |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1615 | mmc_retune_recheck(host); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1616 | pr_info("%s: req failed (CMD%u): %d, retrying...\n", |
| 1617 | mmc_hostname(host), |
| 1618 | cmd->opcode, cmd->error); |
| 1619 | cmd->retries--; |
| 1620 | cmd->error = 0; |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1621 | __mmc_start_request(host, mrq); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1622 | continue; /* wait for done/new event again */ |
| 1623 | } |
| 1624 | } else if (context_info->is_new_req) { |
| 1625 | context_info->is_new_req = false; |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1626 | if (!next_req) |
| 1627 | return MMC_BLK_NEW_REQUEST; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1628 | } |
| 1629 | } |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1630 | mmc_retune_release(host); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1631 | return err; |
| 1632 | } |
| 1633 | |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1634 | void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq) |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1635 | { |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1636 | struct mmc_command *cmd; |
| 1637 | |
| 1638 | while (1) { |
Subhash Jadavani | 9151c53 | 2012-07-13 10:51:37 +0530 | [diff] [blame] | 1639 | wait_for_completion_io(&mrq->completion); |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1640 | |
| 1641 | cmd = mrq->cmd; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1642 | |
| 1643 | /* |
Subhash Jadavani | 7ad7ea8 | 2014-12-04 15:07:32 +0200 | [diff] [blame] | 1644 | * If host has timed out waiting for the sanitize/bkops |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1645 | * to complete, card might be still in programming state |
| 1646 | * so let's try to bring the card out of programming |
| 1647 | * state. |
| 1648 | */ |
Subhash Jadavani | 7ad7ea8 | 2014-12-04 15:07:32 +0200 | [diff] [blame] | 1649 | if ((cmd->bkops_busy || cmd->sanitize_busy) && cmd->error == -ETIMEDOUT) { |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1650 | if (!mmc_interrupt_hpi(host->card)) { |
Subhash Jadavani | 7ad7ea8 | 2014-12-04 15:07:32 +0200 | [diff] [blame] | 1651 | pr_warn("%s: %s: Interrupted sanitize/bkops\n", |
| 1652 | mmc_hostname(host), __func__); |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1653 | cmd->error = 0; |
| 1654 | break; |
| 1655 | } else { |
| 1656 | pr_err("%s: %s: Failed to interrupt sanitize\n", |
| 1657 | mmc_hostname(host), __func__); |
| 1658 | } |
| 1659 | } |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 1660 | if (!cmd->error || !cmd->retries || |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 1661 | mmc_card_removed(host->card)) { |
| 1662 | if (cmd->error && !cmd->retries && |
| 1663 | cmd->opcode != MMC_SEND_STATUS && |
| 1664 | cmd->opcode != MMC_SEND_TUNING_BLOCK) |
| 1665 | mmc_recovery_fallback_lower_speed(host); |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1666 | break; |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 1667 | } |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1668 | |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1669 | mmc_retune_recheck(host); |
| 1670 | |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1671 | pr_debug("%s: req failed (CMD%u): %d, retrying...\n", |
| 1672 | mmc_hostname(host), cmd->opcode, cmd->error); |
| 1673 | cmd->retries--; |
| 1674 | cmd->error = 0; |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1675 | __mmc_start_request(host, mrq); |
Adrian Hunter | 08a7e1d | 2011-10-03 15:33:33 +0300 | [diff] [blame] | 1676 | } |
Adrian Hunter | 90a8148 | 2015-05-07 13:10:14 +0300 | [diff] [blame] | 1677 | |
| 1678 | mmc_retune_release(host); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1679 | } |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1680 | EXPORT_SYMBOL(mmc_wait_for_req_done); |
| 1681 | |
| 1682 | /** |
| 1683 | * mmc_is_req_done - Determine if a 'cap_cmd_during_tfr' request is done |
| 1684 | * @host: MMC host |
| 1685 | * @mrq: MMC request |
| 1686 | * |
| 1687 | * mmc_is_req_done() is used with requests that have |
| 1688 | * mrq->cap_cmd_during_tfr = true. mmc_is_req_done() must be called after |
| 1689 | * starting a request and before waiting for it to complete. That is, |
| 1690 | * either in between calls to mmc_start_req(), or after mmc_wait_for_req() |
| 1691 | * and before mmc_wait_for_req_done(). If it is called at other times the |
| 1692 | * result is not meaningful. |
| 1693 | */ |
| 1694 | bool mmc_is_req_done(struct mmc_host *host, struct mmc_request *mrq) |
| 1695 | { |
| 1696 | if (host->areq) |
| 1697 | return host->context_info.is_done_rcv; |
| 1698 | else |
| 1699 | return completion_done(&mrq->completion); |
| 1700 | } |
| 1701 | EXPORT_SYMBOL(mmc_is_req_done); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1702 | |
| 1703 | /** |
| 1704 | * mmc_pre_req - Prepare for a new request |
| 1705 | * @host: MMC host to prepare command |
| 1706 | * @mrq: MMC request to prepare for |
| 1707 | * @is_first_req: true if there is no previous started request |
| 1708 | * that may run in parellel to this call, otherwise false |
| 1709 | * |
| 1710 | * mmc_pre_req() is called in prior to mmc_start_req() to let |
| 1711 | * host prepare for the new request. Preparation of a request may be |
| 1712 | * performed while another request is running on the host. |
| 1713 | */ |
| 1714 | static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq, |
| 1715 | bool is_first_req) |
| 1716 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1717 | if (host->ops->pre_req) { |
| 1718 | mmc_host_clk_hold(host); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1719 | host->ops->pre_req(host, mrq, is_first_req); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1720 | mmc_host_clk_release(host); |
| 1721 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1722 | } |
| 1723 | |
| 1724 | /** |
| 1725 | * mmc_post_req - Post process a completed request |
| 1726 | * @host: MMC host to post process command |
| 1727 | * @mrq: MMC request to post process for |
| 1728 | * @err: Error, if non zero, clean up any resources made in pre_req |
| 1729 | * |
| 1730 | * Let the host post process a completed request. Post processing of |
| 1731 | * a request may be performed while another reuqest is running. |
| 1732 | */ |
| 1733 | static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq, |
| 1734 | int err) |
| 1735 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1736 | if (host->ops->post_req) { |
| 1737 | mmc_host_clk_hold(host); |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1738 | host->ops->post_req(host, mrq, err); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 1739 | mmc_host_clk_release(host); |
| 1740 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1741 | } |
| 1742 | |
| 1743 | /** |
Asutosh Das | 02e3086 | 2015-05-20 16:52:04 +0530 | [diff] [blame] | 1744 | * mmc_cmdq_discard_card_queue - discard the task[s] in the device |
| 1745 | * @host: host instance |
| 1746 | * @tasks: mask of tasks to be knocked off |
| 1747 | * 0: remove all queued tasks |
| 1748 | */ |
| 1749 | int mmc_cmdq_discard_queue(struct mmc_host *host, u32 tasks) |
| 1750 | { |
| 1751 | return mmc_discard_queue(host, tasks); |
| 1752 | } |
| 1753 | EXPORT_SYMBOL(mmc_cmdq_discard_queue); |
| 1754 | |
| 1755 | |
| 1756 | /** |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1757 | * mmc_cmdq_post_req - post process of a completed request |
| 1758 | * @host: host instance |
Ritesh Harjani | 121cdec | 2015-09-27 21:51:01 +0530 | [diff] [blame] | 1759 | * @tag: the request tag. |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1760 | * @err: non-zero is error, success otherwise |
| 1761 | */ |
Ritesh Harjani | 121cdec | 2015-09-27 21:51:01 +0530 | [diff] [blame] | 1762 | void mmc_cmdq_post_req(struct mmc_host *host, int tag, int err) |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1763 | { |
| 1764 | if (likely(host->cmdq_ops->post_req)) |
Ritesh Harjani | 121cdec | 2015-09-27 21:51:01 +0530 | [diff] [blame] | 1765 | host->cmdq_ops->post_req(host, tag, err); |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1766 | } |
| 1767 | EXPORT_SYMBOL(mmc_cmdq_post_req); |
| 1768 | |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1769 | /** |
| 1770 | * mmc_cmdq_halt - halt/un-halt the command queue engine |
| 1771 | * @host: host instance |
| 1772 | * @halt: true - halt, un-halt otherwise |
| 1773 | * |
| 1774 | * Host halts the command queue engine. It should complete |
| 1775 | * the ongoing transfer and release the bus. |
| 1776 | * All legacy commands can be sent upon successful |
| 1777 | * completion of this function. |
| 1778 | * Returns 0 on success, negative otherwise |
| 1779 | */ |
| 1780 | int mmc_cmdq_halt(struct mmc_host *host, bool halt) |
| 1781 | { |
| 1782 | int err = 0; |
| 1783 | |
Sahitya Tummala | e79f4f4 | 2016-04-26 15:00:04 +0530 | [diff] [blame] | 1784 | if (mmc_host_cq_disable(host)) { |
| 1785 | pr_debug("%s: %s: CQE is already disabled\n", |
| 1786 | mmc_hostname(host), __func__); |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1790 | if ((halt && mmc_host_halt(host)) || |
Konstantin Dorfman | 05a9a49 | 2015-06-28 10:25:55 +0300 | [diff] [blame] | 1791 | (!halt && !mmc_host_halt(host))) { |
| 1792 | pr_debug("%s: %s: CQE is already %s\n", mmc_hostname(host), |
| 1793 | __func__, halt ? "halted" : "un-halted"); |
| 1794 | return 0; |
| 1795 | } |
| 1796 | |
Ritesh Harjani | b431b3f | 2015-05-19 14:27:34 +0530 | [diff] [blame] | 1797 | mmc_host_clk_hold(host); |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1798 | if (host->cmdq_ops->halt) { |
| 1799 | err = host->cmdq_ops->halt(host, halt); |
Talel Shenhar | e68741c | 2015-06-25 09:33:24 +0300 | [diff] [blame] | 1800 | if (!err && host->ops->notify_halt) |
| 1801 | host->ops->notify_halt(host, halt); |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1802 | if (!err && halt) |
| 1803 | mmc_host_set_halt(host); |
Gilad Broner | 4df1896 | 2015-08-13 17:58:30 +0300 | [diff] [blame] | 1804 | else if (!err && !halt) { |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1805 | mmc_host_clr_halt(host); |
Subhash Jadavani | 987144c | 2015-08-12 17:53:21 -0700 | [diff] [blame] | 1806 | wake_up(&host->cmdq_ctx.wait); |
Gilad Broner | 4df1896 | 2015-08-13 17:58:30 +0300 | [diff] [blame] | 1807 | } |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1808 | } else { |
| 1809 | err = -ENOSYS; |
| 1810 | } |
Ritesh Harjani | b431b3f | 2015-05-19 14:27:34 +0530 | [diff] [blame] | 1811 | mmc_host_clk_release(host); |
Asutosh Das | 31150ae | 2015-05-21 17:21:07 +0530 | [diff] [blame] | 1812 | return err; |
| 1813 | } |
| 1814 | EXPORT_SYMBOL(mmc_cmdq_halt); |
| 1815 | |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1816 | int mmc_cmdq_start_req(struct mmc_host *host, struct mmc_cmdq_req *cmdq_req) |
| 1817 | { |
| 1818 | struct mmc_request *mrq = &cmdq_req->mrq; |
| 1819 | |
| 1820 | mrq->host = host; |
| 1821 | if (mmc_card_removed(host->card)) { |
| 1822 | mrq->cmd->error = -ENOMEDIUM; |
| 1823 | return -ENOMEDIUM; |
| 1824 | } |
Veerabhadrarao Badiganti | 4efc0a6 | 2017-10-09 20:13:45 +0530 | [diff] [blame] | 1825 | return mmc_start_cmdq_request(host, mrq); |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1826 | } |
| 1827 | EXPORT_SYMBOL(mmc_cmdq_start_req); |
| 1828 | |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 1829 | static void mmc_cmdq_dcmd_req_done(struct mmc_request *mrq) |
| 1830 | { |
Ritesh Harjani | b431b3f | 2015-05-19 14:27:34 +0530 | [diff] [blame] | 1831 | mmc_host_clk_release(mrq->host); |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 1832 | complete(&mrq->completion); |
| 1833 | } |
| 1834 | |
| 1835 | int mmc_cmdq_wait_for_dcmd(struct mmc_host *host, |
| 1836 | struct mmc_cmdq_req *cmdq_req) |
| 1837 | { |
| 1838 | struct mmc_request *mrq = &cmdq_req->mrq; |
| 1839 | struct mmc_command *cmd = mrq->cmd; |
| 1840 | int err = 0; |
| 1841 | |
| 1842 | init_completion(&mrq->completion); |
| 1843 | mrq->done = mmc_cmdq_dcmd_req_done; |
| 1844 | err = mmc_cmdq_start_req(host, cmdq_req); |
| 1845 | if (err) |
| 1846 | return err; |
| 1847 | |
| 1848 | wait_for_completion_io(&mrq->completion); |
| 1849 | if (cmd->error) { |
| 1850 | pr_err("%s: DCMD %d failed with err %d\n", |
| 1851 | mmc_hostname(host), cmd->opcode, |
| 1852 | cmd->error); |
| 1853 | err = cmd->error; |
Konstantin Dorfman | 9a9c9fd | 2015-06-30 17:39:43 +0300 | [diff] [blame] | 1854 | mmc_host_clk_hold(host); |
| 1855 | host->cmdq_ops->dumpstate(host); |
| 1856 | mmc_host_clk_release(host); |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 1857 | } |
| 1858 | return err; |
| 1859 | } |
| 1860 | EXPORT_SYMBOL(mmc_cmdq_wait_for_dcmd); |
| 1861 | |
Asutosh Das | 5238e02 | 2015-04-23 16:00:45 +0530 | [diff] [blame] | 1862 | int mmc_cmdq_prepare_flush(struct mmc_command *cmd) |
| 1863 | { |
| 1864 | return __mmc_switch_cmdq_mode(cmd, EXT_CSD_CMD_SET_NORMAL, |
| 1865 | EXT_CSD_FLUSH_CACHE, 1, |
| 1866 | 0, true, true); |
| 1867 | } |
| 1868 | EXPORT_SYMBOL(mmc_cmdq_prepare_flush); |
| 1869 | |
Venkat Gopalakrishnan | e95d7bf | 2015-05-29 16:51:43 -0700 | [diff] [blame] | 1870 | /** |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1871 | * mmc_start_req - start a non-blocking request |
| 1872 | * @host: MMC host to start command |
| 1873 | * @areq: async request to start |
| 1874 | * @error: out parameter returns 0 for success, otherwise non zero |
| 1875 | * |
| 1876 | * Start a new MMC custom command request for a host. |
| 1877 | * If there is on ongoing async request wait for completion |
| 1878 | * of that request and start the new one and return. |
| 1879 | * Does not wait for the new request to complete. |
| 1880 | * |
| 1881 | * Returns the completed request, NULL in case of none completed. |
| 1882 | * Wait for the an ongoing request (previoulsy started) to complete and |
| 1883 | * return the completed request. If there is no ongoing request, NULL |
| 1884 | * is returned without waiting. NULL is not an error condition. |
| 1885 | */ |
| 1886 | struct mmc_async_req *mmc_start_req(struct mmc_host *host, |
| 1887 | struct mmc_async_req *areq, int *error) |
| 1888 | { |
| 1889 | int err = 0; |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 1890 | int start_err = 0; |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1891 | struct mmc_async_req *data = host->areq; |
| 1892 | |
| 1893 | /* Prepare a new request */ |
| 1894 | if (areq) |
| 1895 | mmc_pre_req(host, areq->mrq, !host->areq); |
| 1896 | |
| 1897 | if (host->areq) { |
Jaehoon Chung | f5c2758 | 2013-02-01 14:32:22 +0900 | [diff] [blame] | 1898 | err = mmc_wait_for_data_req_done(host, host->areq->mrq, areq); |
| 1899 | if (err == MMC_BLK_NEW_REQUEST) { |
| 1900 | if (error) |
| 1901 | *error = err; |
| 1902 | /* |
| 1903 | * The previous request was not completed, |
| 1904 | * nothing to return |
| 1905 | */ |
| 1906 | return NULL; |
| 1907 | } |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1908 | /* |
| 1909 | * Check BKOPS urgency for each R1 response |
| 1910 | */ |
| 1911 | if (host->card && mmc_card_mmc(host->card) && |
| 1912 | ((mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1) || |
| 1913 | (mmc_resp_type(host->areq->mrq->cmd) == MMC_RSP_R1B)) && |
Srinivas Kandagatla | 64b12a6 | 2014-10-08 12:24:24 +0100 | [diff] [blame] | 1914 | (host->areq->mrq->cmd->resp[0] & R1_EXCEPTION_EVENT)) { |
| 1915 | |
| 1916 | /* Cancel the prepared request */ |
| 1917 | if (areq) |
| 1918 | mmc_post_req(host, areq->mrq, -EINVAL); |
| 1919 | |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 1920 | mmc_check_bkops(host->card); |
Srinivas Kandagatla | 64b12a6 | 2014-10-08 12:24:24 +0100 | [diff] [blame] | 1921 | |
| 1922 | /* prepare the request again */ |
| 1923 | if (areq) |
| 1924 | mmc_pre_req(host, areq->mrq, !host->areq); |
| 1925 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1926 | } |
| 1927 | |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 1928 | if (!err && areq) { |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 1929 | #ifdef CONFIG_BLOCK |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 1930 | if (host->latency_hist_enabled) { |
| 1931 | areq->mrq->io_start = ktime_get(); |
| 1932 | areq->mrq->lat_hist_enabled = 1; |
| 1933 | } else |
| 1934 | areq->mrq->lat_hist_enabled = 0; |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 1935 | #endif |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1936 | start_err = __mmc_start_data_req(host, areq->mrq); |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 1937 | } |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1938 | |
| 1939 | if (host->areq) |
| 1940 | mmc_post_req(host, host->areq->mrq, 0); |
| 1941 | |
Subhash Jadavani | a7b703a | 2015-10-14 19:04:26 -0700 | [diff] [blame] | 1942 | if (err && areq) |
Jaehoon Chung | f5c2758 | 2013-02-01 14:32:22 +0900 | [diff] [blame] | 1943 | mmc_post_req(host, areq->mrq, -EINVAL); |
Ulf Hansson | 956d9fd | 2012-03-05 15:52:43 +0100 | [diff] [blame] | 1944 | |
| 1945 | if (err) |
| 1946 | host->areq = NULL; |
| 1947 | else |
| 1948 | host->areq = areq; |
| 1949 | |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1950 | if (error) |
| 1951 | *error = err; |
| 1952 | return data; |
| 1953 | } |
| 1954 | EXPORT_SYMBOL(mmc_start_req); |
| 1955 | |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1956 | /** |
| 1957 | * mmc_wait_for_req - start a request and wait for completion |
| 1958 | * @host: MMC host to start command |
| 1959 | * @mrq: MMC request to start |
| 1960 | * |
| 1961 | * Start a new MMC custom command request for a host, and wait |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1962 | * for the command to complete. In the case of 'cap_cmd_during_tfr' |
| 1963 | * requests, the transfer is ongoing and the caller can issue further |
| 1964 | * commands that do not use the data lines, and then wait by calling |
| 1965 | * mmc_wait_for_req_done(). |
| 1966 | * Does not attempt to parse the response. |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 1967 | */ |
| 1968 | 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] | 1969 | { |
Asutosh Das | e1f8f97 | 2013-10-14 11:35:19 +0530 | [diff] [blame] | 1970 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 1971 | if (mmc_bus_needs_resume(host)) |
| 1972 | mmc_resume_bus(host); |
| 1973 | #endif |
Per Forlin | aa8b683 | 2011-07-01 18:55:22 +0200 | [diff] [blame] | 1974 | __mmc_start_req(host, mrq); |
Adrian Hunter | 5163af5 | 2016-08-16 13:44:11 +0300 | [diff] [blame] | 1975 | |
| 1976 | if (!mrq->cap_cmd_during_tfr) |
| 1977 | mmc_wait_for_req_done(host, mrq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | EXPORT_SYMBOL(mmc_wait_for_req); |
| 1980 | |
| 1981 | /** |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 1982 | * mmc_interrupt_hpi - Issue for High priority Interrupt |
| 1983 | * @card: the MMC card associated with the HPI transfer |
| 1984 | * |
| 1985 | * Issued High Priority Interrupt, and check for card status |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 1986 | * until out-of prg-state. |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 1987 | */ |
| 1988 | int mmc_interrupt_hpi(struct mmc_card *card) |
| 1989 | { |
| 1990 | int err; |
| 1991 | u32 status; |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 1992 | unsigned long prg_wait; |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 1993 | |
| 1994 | BUG_ON(!card); |
| 1995 | |
| 1996 | if (!card->ext_csd.hpi_en) { |
| 1997 | pr_info("%s: HPI enable bit unset\n", mmc_hostname(card->host)); |
| 1998 | return 1; |
| 1999 | } |
| 2000 | |
| 2001 | mmc_claim_host(card->host); |
| 2002 | err = mmc_send_status(card, &status); |
| 2003 | if (err) { |
| 2004 | pr_err("%s: Get card status fail\n", mmc_hostname(card->host)); |
| 2005 | goto out; |
| 2006 | } |
| 2007 | |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2008 | switch (R1_CURRENT_STATE(status)) { |
| 2009 | case R1_STATE_IDLE: |
| 2010 | case R1_STATE_READY: |
| 2011 | case R1_STATE_STBY: |
Venkatraman S | 211d4fe | 2012-08-07 19:24:45 +0530 | [diff] [blame] | 2012 | case R1_STATE_TRAN: |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2013 | /* |
Venkatraman S | 211d4fe | 2012-08-07 19:24:45 +0530 | [diff] [blame] | 2014 | * In idle and transfer states, HPI is not needed and the caller |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2015 | * can issue the next intended command immediately |
| 2016 | */ |
| 2017 | goto out; |
| 2018 | case R1_STATE_PRG: |
| 2019 | break; |
| 2020 | default: |
| 2021 | /* In all other states, it's illegal to issue HPI */ |
| 2022 | pr_debug("%s: HPI cannot be sent. Card state=%d\n", |
| 2023 | mmc_hostname(card->host), R1_CURRENT_STATE(status)); |
| 2024 | err = -EINVAL; |
| 2025 | goto out; |
| 2026 | } |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 2027 | |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2028 | err = mmc_send_hpi_cmd(card, &status); |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2029 | |
| 2030 | prg_wait = jiffies + msecs_to_jiffies(card->ext_csd.out_of_int_time); |
| 2031 | do { |
| 2032 | err = mmc_send_status(card, &status); |
| 2033 | |
| 2034 | if (!err && R1_CURRENT_STATE(status) == R1_STATE_TRAN) |
| 2035 | break; |
Konstantin Dorfman | cfad32e | 2013-05-13 15:00:21 +0300 | [diff] [blame] | 2036 | if (time_after(jiffies, prg_wait)) { |
| 2037 | err = mmc_send_status(card, &status); |
| 2038 | if (!err && R1_CURRENT_STATE(status) != R1_STATE_TRAN) |
| 2039 | err = -ETIMEDOUT; |
| 2040 | else |
| 2041 | break; |
| 2042 | } |
Venkatraman S | 6af9e96 | 2012-06-22 11:42:36 +0530 | [diff] [blame] | 2043 | } while (!err); |
Jaehoon Chung | eb0d8f1 | 2011-10-18 01:26:42 -0400 | [diff] [blame] | 2044 | |
| 2045 | out: |
| 2046 | mmc_release_host(card->host); |
| 2047 | return err; |
| 2048 | } |
| 2049 | EXPORT_SYMBOL(mmc_interrupt_hpi); |
| 2050 | |
| 2051 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2052 | * mmc_wait_for_cmd - start a command and wait for completion |
| 2053 | * @host: MMC host to start command |
| 2054 | * @cmd: MMC command to start |
| 2055 | * @retries: maximum number of retries |
| 2056 | * |
| 2057 | * Start a new MMC command for a host, and wait for the command |
| 2058 | * to complete. Return any error that occurred while the command |
| 2059 | * was executing. Do not attempt to parse the response. |
| 2060 | */ |
| 2061 | int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries) |
| 2062 | { |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 2063 | struct mmc_request mrq = {NULL}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 2065 | WARN_ON(!host->claimed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | memset(cmd->resp, 0, sizeof(cmd->resp)); |
| 2068 | cmd->retries = retries; |
| 2069 | |
| 2070 | mrq.cmd = cmd; |
| 2071 | cmd->data = NULL; |
| 2072 | |
| 2073 | mmc_wait_for_req(host, &mrq); |
| 2074 | |
| 2075 | return cmd->error; |
| 2076 | } |
| 2077 | |
| 2078 | EXPORT_SYMBOL(mmc_wait_for_cmd); |
| 2079 | |
Pierre Ossman | 335eadf | 2005-09-06 15:18:50 -0700 | [diff] [blame] | 2080 | /** |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2081 | * mmc_stop_bkops - stop ongoing BKOPS |
| 2082 | * @card: MMC card to check BKOPS |
| 2083 | * |
| 2084 | * Send HPI command to stop ongoing background operations to |
| 2085 | * allow rapid servicing of foreground operations, e.g. read/ |
| 2086 | * writes. Wait until the card comes out of the programming state |
| 2087 | * to avoid errors in servicing read/write requests. |
| 2088 | */ |
| 2089 | int mmc_stop_bkops(struct mmc_card *card) |
| 2090 | { |
| 2091 | int err = 0; |
| 2092 | |
| 2093 | BUG_ON(!card); |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 2094 | if (unlikely(!mmc_card_configured_manual_bkops(card))) |
| 2095 | goto out; |
| 2096 | if (!mmc_card_doing_bkops(card)) |
| 2097 | goto out; |
| 2098 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2099 | err = mmc_interrupt_hpi(card); |
| 2100 | |
| 2101 | /* |
| 2102 | * If err is EINVAL, we can't issue an HPI. |
| 2103 | * It should complete the BKOPS. |
| 2104 | */ |
| 2105 | if (!err || (err == -EINVAL)) { |
| 2106 | mmc_card_clr_doing_bkops(card); |
Dov Levenglick | ea221d4 | 2015-06-28 16:45:41 +0300 | [diff] [blame] | 2107 | mmc_update_bkops_hpi(&card->bkops.stats); |
Adrian Hunter | 66073d8 | 2015-05-07 13:10:17 +0300 | [diff] [blame] | 2108 | mmc_retune_release(card->host); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2109 | err = 0; |
| 2110 | } |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 2111 | out: |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2112 | return err; |
| 2113 | } |
| 2114 | EXPORT_SYMBOL(mmc_stop_bkops); |
| 2115 | |
| 2116 | int mmc_read_bkops_status(struct mmc_card *card) |
| 2117 | { |
| 2118 | int err; |
| 2119 | u8 *ext_csd; |
| 2120 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2121 | mmc_claim_host(card->host); |
Ulf Hansson | b2cada7 | 2014-10-17 11:48:23 +0200 | [diff] [blame] | 2122 | err = mmc_get_ext_csd(card, &ext_csd); |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2123 | mmc_release_host(card->host); |
| 2124 | if (err) |
Ulf Hansson | b2cada7 | 2014-10-17 11:48:23 +0200 | [diff] [blame] | 2125 | return err; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2126 | |
Asutosh Das | 79ccfa5 | 2016-04-21 14:15:15 +0530 | [diff] [blame] | 2127 | card->ext_csd.raw_bkops_status = ext_csd[EXT_CSD_BKOPS_STATUS] & |
| 2128 | MMC_BKOPS_URGENCY_MASK; |
| 2129 | card->ext_csd.raw_exception_status = |
| 2130 | ext_csd[EXT_CSD_EXP_EVENTS_STATUS] & (EXT_CSD_URGENT_BKOPS | |
| 2131 | EXT_CSD_DYNCAP_NEEDED | |
| 2132 | EXT_CSD_SYSPOOL_EXHAUSTED |
| 2133 | | EXT_CSD_PACKED_FAILURE); |
| 2134 | |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2135 | kfree(ext_csd); |
Ulf Hansson | b2cada7 | 2014-10-17 11:48:23 +0200 | [diff] [blame] | 2136 | return 0; |
Jaehoon Chung | 950d56a | 2012-09-17 08:42:02 +0000 | [diff] [blame] | 2137 | } |
| 2138 | EXPORT_SYMBOL(mmc_read_bkops_status); |
| 2139 | |
| 2140 | /** |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2141 | * mmc_set_data_timeout - set the timeout for a data command |
| 2142 | * @data: data phase for command |
| 2143 | * @card: the MMC card associated with the data transfer |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 2144 | * |
| 2145 | * Computes the data timeout parameters according to the |
| 2146 | * correct algorithm given the card type. |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2147 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 2148 | 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] | 2149 | { |
| 2150 | unsigned int mult; |
| 2151 | |
Asutosh Das | bbefab3 | 2013-10-07 14:53:32 +0530 | [diff] [blame] | 2152 | if (!card) { |
| 2153 | WARN_ON(1); |
| 2154 | return; |
| 2155 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2156 | /* |
Pierre Ossman | e6f918b | 2007-08-07 14:11:55 +0200 | [diff] [blame] | 2157 | * SDIO cards only define an upper 1 s limit on access. |
| 2158 | */ |
| 2159 | if (mmc_card_sdio(card)) { |
| 2160 | data->timeout_ns = 1000000000; |
| 2161 | data->timeout_clks = 0; |
| 2162 | return; |
| 2163 | } |
| 2164 | |
| 2165 | /* |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2166 | * SD cards use a 100 multiplier rather than 10 |
| 2167 | */ |
| 2168 | mult = mmc_card_sd(card) ? 100 : 10; |
| 2169 | |
| 2170 | /* |
| 2171 | * Scale up the multiplier (and therefore the timeout) by |
| 2172 | * the r2w factor for writes. |
| 2173 | */ |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 2174 | if (data->flags & MMC_DATA_WRITE) |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2175 | mult <<= card->csd.r2w_factor; |
| 2176 | |
| 2177 | data->timeout_ns = card->csd.tacc_ns * mult; |
| 2178 | data->timeout_clks = card->csd.tacc_clks * mult; |
| 2179 | |
| 2180 | /* |
| 2181 | * SD cards also have an upper limit on the timeout. |
| 2182 | */ |
| 2183 | if (mmc_card_sd(card)) { |
| 2184 | unsigned int timeout_us, limit_us; |
| 2185 | |
| 2186 | timeout_us = data->timeout_ns / 1000; |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2187 | if (mmc_host_clk_rate(card->host)) |
Linus Walleij | e9b8684 | 2011-01-05 00:44:32 +0100 | [diff] [blame] | 2188 | timeout_us += data->timeout_clks * 1000 / |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2189 | (mmc_host_clk_rate(card->host) / 1000); |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2190 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 2191 | if (data->flags & MMC_DATA_WRITE) |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 2192 | /* |
Paul Walmsley | 3bdc9ba | 2012-03-12 04:58:00 -0600 | [diff] [blame] | 2193 | * The MMC spec "It is strongly recommended |
| 2194 | * for hosts to implement more than 500ms |
| 2195 | * timeout value even if the card indicates |
| 2196 | * the 250ms maximum busy length." Even the |
| 2197 | * previous value of 300ms is known to be |
| 2198 | * insufficient for some cards. |
Pierre Ossman | 493890e | 2008-10-26 12:37:25 +0100 | [diff] [blame] | 2199 | */ |
Paul Walmsley | 3bdc9ba | 2012-03-12 04:58:00 -0600 | [diff] [blame] | 2200 | limit_us = 3000000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2201 | else |
| 2202 | limit_us = 100000; |
| 2203 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 2204 | /* |
| 2205 | * SDHC cards always use these fixed values. |
| 2206 | */ |
| 2207 | if (timeout_us > limit_us || mmc_card_blockaddr(card)) { |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2208 | data->timeout_ns = limit_us * 1000; |
| 2209 | data->timeout_clks = 0; |
| 2210 | } |
Stefan Wahren | f7bf11a | 2014-04-03 17:32:05 +0200 | [diff] [blame] | 2211 | |
| 2212 | /* assign limit value if invalid */ |
| 2213 | if (timeout_us == 0) |
| 2214 | data->timeout_ns = limit_us * 1000; |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2215 | } |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* |
| 2218 | * Some cards require longer data read timeout than indicated in CSD. |
| 2219 | * Address this by setting the read timeout to a "reasonably high" |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 2220 | * value. For the cards tested, 600ms has proven enough. If necessary, |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2221 | * this value can be increased if other problematic cards require this. |
Ritesh Harjani | e11a1b7 | 2016-02-25 17:54:58 +0530 | [diff] [blame] | 2222 | * Certain Hynix 5.x cards giving read timeout even with 300ms. |
| 2223 | * Increasing further to max value (4s). |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2224 | */ |
| 2225 | if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) { |
Ritesh Harjani | e11a1b7 | 2016-02-25 17:54:58 +0530 | [diff] [blame] | 2226 | data->timeout_ns = 4000000000u; |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2227 | data->timeout_clks = 0; |
| 2228 | } |
| 2229 | |
Wolfgang Muees | c0c8887 | 2009-03-11 14:28:39 +0100 | [diff] [blame] | 2230 | /* |
| 2231 | * Some cards need very high timeouts if driven in SPI mode. |
| 2232 | * The worst observed timeout was 900ms after writing a |
| 2233 | * continuous stream of data until the internal logic |
| 2234 | * overflowed. |
| 2235 | */ |
| 2236 | if (mmc_host_is_spi(card->host)) { |
| 2237 | if (data->flags & MMC_DATA_WRITE) { |
| 2238 | if (data->timeout_ns < 1000000000) |
| 2239 | data->timeout_ns = 1000000000; /* 1s */ |
| 2240 | } else { |
| 2241 | if (data->timeout_ns < 100000000) |
| 2242 | data->timeout_ns = 100000000; /* 100ms */ |
| 2243 | } |
| 2244 | } |
Pratibhasagar V | 8d664e3 | 2014-12-03 18:26:42 +0200 | [diff] [blame] | 2245 | /* Increase the timeout values for some bad INAND MCP devices */ |
| 2246 | if (card->quirks & MMC_QUIRK_INAND_DATA_TIMEOUT) { |
| 2247 | data->timeout_ns = 4000000000u; /* 4s */ |
| 2248 | data->timeout_clks = 0; |
| 2249 | } |
Russell King | d773d72 | 2006-09-07 15:57:12 +0100 | [diff] [blame] | 2250 | } |
| 2251 | EXPORT_SYMBOL(mmc_set_data_timeout); |
| 2252 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2253 | /** |
Pierre Ossman | ad3868b | 2008-06-28 12:52:45 +0200 | [diff] [blame] | 2254 | * mmc_align_data_size - pads a transfer size to a more optimal value |
| 2255 | * @card: the MMC card associated with the data transfer |
| 2256 | * @sz: original transfer size |
| 2257 | * |
| 2258 | * Pads the original data size with a number of extra bytes in |
| 2259 | * order to avoid controller bugs and/or performance hits |
| 2260 | * (e.g. some controllers revert to PIO for certain sizes). |
| 2261 | * |
| 2262 | * Returns the improved size, which might be unmodified. |
| 2263 | * |
| 2264 | * Note that this function is only relevant when issuing a |
| 2265 | * single scatter gather entry. |
| 2266 | */ |
| 2267 | unsigned int mmc_align_data_size(struct mmc_card *card, unsigned int sz) |
| 2268 | { |
| 2269 | /* |
| 2270 | * FIXME: We don't have a system for the controller to tell |
| 2271 | * the core about its problems yet, so for now we just 32-bit |
| 2272 | * align the size. |
| 2273 | */ |
| 2274 | sz = ((sz + 3) / 4) * 4; |
| 2275 | |
| 2276 | return sz; |
| 2277 | } |
| 2278 | EXPORT_SYMBOL(mmc_align_data_size); |
| 2279 | |
| 2280 | /** |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2281 | * __mmc_claim_host - exclusively claim a host |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2282 | * @host: mmc host to claim |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2283 | * @abort: whether or not the operation should be aborted |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | * |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2285 | * Claim a host for a set of operations. If @abort is non null and |
| 2286 | * dereference a non-zero value then this will return prematurely with |
| 2287 | * that non-zero value without acquiring the lock. Returns zero |
| 2288 | * with the lock held otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | */ |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2290 | int __mmc_claim_host(struct mmc_host *host, atomic_t *abort) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | { |
| 2292 | DECLARE_WAITQUEUE(wait, current); |
| 2293 | unsigned long flags; |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2294 | int stop; |
Ulf Hansson | 9250aea | 2015-03-27 12:15:15 +0100 | [diff] [blame] | 2295 | bool pm = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | |
Pierre Ossman | cf795bf | 2007-07-11 20:28:02 +0200 | [diff] [blame] | 2297 | might_sleep(); |
| 2298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2299 | add_wait_queue(&host->wq, &wait); |
Sujith Reddy Thumma | 1547ccb | 2011-02-17 21:37:48 +0530 | [diff] [blame] | 2300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | spin_lock_irqsave(&host->lock, flags); |
| 2302 | while (1) { |
| 2303 | set_current_state(TASK_UNINTERRUPTIBLE); |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2304 | stop = abort ? atomic_read(abort) : 0; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2305 | if (stop || !host->claimed || host->claimer == current) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | break; |
| 2307 | spin_unlock_irqrestore(&host->lock, flags); |
| 2308 | schedule(); |
| 2309 | spin_lock_irqsave(&host->lock, flags); |
| 2310 | } |
| 2311 | set_current_state(TASK_RUNNING); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2312 | if (!stop) { |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2313 | host->claimed = 1; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2314 | host->claimer = current; |
| 2315 | host->claim_cnt += 1; |
Ulf Hansson | 9250aea | 2015-03-27 12:15:15 +0100 | [diff] [blame] | 2316 | if (host->claim_cnt == 1) |
| 2317 | pm = true; |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2318 | } else |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2319 | wake_up(&host->wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | spin_unlock_irqrestore(&host->lock, flags); |
| 2321 | remove_wait_queue(&host->wq, &wait); |
Ulf Hansson | 9250aea | 2015-03-27 12:15:15 +0100 | [diff] [blame] | 2322 | |
| 2323 | if (pm) |
| 2324 | pm_runtime_get_sync(mmc_dev(host)); |
| 2325 | |
Subhash Jadavani | 480a79e | 2016-04-27 17:19:29 -0700 | [diff] [blame] | 2326 | if (host->ops->enable && !stop && host->claim_cnt == 1) |
| 2327 | host->ops->enable(host); |
| 2328 | |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2329 | return stop; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | } |
Nicolas Pitre | 2342f33 | 2007-06-30 16:21:52 +0200 | [diff] [blame] | 2331 | EXPORT_SYMBOL(__mmc_claim_host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2333 | /** |
Veerabhadrarao Badiganti | bf795c5 | 2017-02-09 20:23:17 +0530 | [diff] [blame] | 2334 | * mmc_try_claim_host - try exclusively to claim a host |
| 2335 | * and keep trying for given time, with a gap of 10ms |
| 2336 | * @host: mmc host to claim |
| 2337 | * @dealy_ms: delay in ms |
| 2338 | * |
| 2339 | * Returns %1 if the host is claimed, %0 otherwise. |
| 2340 | */ |
| 2341 | int mmc_try_claim_host(struct mmc_host *host, unsigned int delay_ms) |
| 2342 | { |
| 2343 | int claimed_host = 0; |
| 2344 | unsigned long flags; |
| 2345 | int retry_cnt = delay_ms/10; |
Veerabhadrarao Badiganti | 36371a1 | 2017-04-04 12:38:24 +0530 | [diff] [blame] | 2346 | bool pm = false; |
Veerabhadrarao Badiganti | bf795c5 | 2017-02-09 20:23:17 +0530 | [diff] [blame] | 2347 | |
| 2348 | do { |
| 2349 | spin_lock_irqsave(&host->lock, flags); |
| 2350 | if (!host->claimed || host->claimer == current) { |
| 2351 | host->claimed = 1; |
| 2352 | host->claimer = current; |
| 2353 | host->claim_cnt += 1; |
| 2354 | claimed_host = 1; |
Veerabhadrarao Badiganti | 36371a1 | 2017-04-04 12:38:24 +0530 | [diff] [blame] | 2355 | if (host->claim_cnt == 1) |
| 2356 | pm = true; |
Veerabhadrarao Badiganti | bf795c5 | 2017-02-09 20:23:17 +0530 | [diff] [blame] | 2357 | } |
| 2358 | spin_unlock_irqrestore(&host->lock, flags); |
| 2359 | if (!claimed_host) |
| 2360 | mmc_delay(10); |
| 2361 | } while (!claimed_host && retry_cnt--); |
Veerabhadrarao Badiganti | 36371a1 | 2017-04-04 12:38:24 +0530 | [diff] [blame] | 2362 | |
| 2363 | if (pm) |
| 2364 | pm_runtime_get_sync(mmc_dev(host)); |
| 2365 | |
Veerabhadrarao Badiganti | bf795c5 | 2017-02-09 20:23:17 +0530 | [diff] [blame] | 2366 | if (host->ops->enable && claimed_host && host->claim_cnt == 1) |
| 2367 | host->ops->enable(host); |
| 2368 | return claimed_host; |
| 2369 | } |
| 2370 | EXPORT_SYMBOL(mmc_try_claim_host); |
| 2371 | |
| 2372 | /** |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 2373 | * mmc_release_host - release a host |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 2374 | * @host: mmc host to release |
| 2375 | * |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 2376 | * Release a MMC host, allowing others to claim the host |
| 2377 | * for their operations. |
Ulf Hansson | ab1efd2 | 2011-03-09 09:11:02 +0100 | [diff] [blame] | 2378 | */ |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 2379 | void mmc_release_host(struct mmc_host *host) |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 2380 | { |
| 2381 | unsigned long flags; |
| 2382 | |
Adrian Hunter | 907d2e7 | 2012-02-29 09:17:21 +0200 | [diff] [blame] | 2383 | WARN_ON(!host->claimed); |
| 2384 | |
Subhash Jadavani | 480a79e | 2016-04-27 17:19:29 -0700 | [diff] [blame] | 2385 | if (host->ops->disable && host->claim_cnt == 1) |
| 2386 | host->ops->disable(host); |
| 2387 | |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 2388 | spin_lock_irqsave(&host->lock, flags); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2389 | if (--host->claim_cnt) { |
| 2390 | /* Release for nested claim */ |
| 2391 | spin_unlock_irqrestore(&host->lock, flags); |
| 2392 | } else { |
| 2393 | host->claimed = 0; |
| 2394 | host->claimer = NULL; |
| 2395 | spin_unlock_irqrestore(&host->lock, flags); |
| 2396 | wake_up(&host->wq); |
Ulf Hansson | 9250aea | 2015-03-27 12:15:15 +0100 | [diff] [blame] | 2397 | pm_runtime_mark_last_busy(mmc_dev(host)); |
| 2398 | pm_runtime_put_autosuspend(mmc_dev(host)); |
Adrian Hunter | 319a3f1 | 2009-09-22 16:44:30 -0700 | [diff] [blame] | 2399 | } |
Adrian Hunter | 8ea926b | 2009-09-22 16:44:29 -0700 | [diff] [blame] | 2400 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2401 | EXPORT_SYMBOL(mmc_release_host); |
| 2402 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2403 | /* |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2404 | * This is a helper function, which fetches a runtime pm reference for the |
| 2405 | * card device and also claims the host. |
| 2406 | */ |
| 2407 | void mmc_get_card(struct mmc_card *card) |
| 2408 | { |
| 2409 | pm_runtime_get_sync(&card->dev); |
| 2410 | mmc_claim_host(card->host); |
Sahitya Tummala | fc9ccd6 | 2016-11-30 09:00:53 +0530 | [diff] [blame] | 2411 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 2412 | if (mmc_bus_needs_resume(card->host)) |
| 2413 | mmc_resume_bus(card->host); |
| 2414 | #endif |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2415 | } |
| 2416 | EXPORT_SYMBOL(mmc_get_card); |
| 2417 | |
Dov Levenglick | a029639 | 2015-06-24 19:51:58 +0300 | [diff] [blame] | 2418 | |
| 2419 | /* |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2420 | * This is a helper function, which releases the host and drops the runtime |
| 2421 | * pm reference for the card device. |
| 2422 | */ |
| 2423 | void mmc_put_card(struct mmc_card *card) |
| 2424 | { |
| 2425 | mmc_release_host(card->host); |
| 2426 | pm_runtime_mark_last_busy(&card->dev); |
Konstantin Dorfman | 5dd9f86 | 2015-12-31 13:25:40 +0200 | [diff] [blame] | 2427 | pm_runtime_put_autosuspend(&card->dev); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2428 | } |
| 2429 | EXPORT_SYMBOL(mmc_put_card); |
| 2430 | |
| 2431 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2432 | * Internal function that does the actual ios call to the host driver, |
| 2433 | * optionally printing some debug output. |
| 2434 | */ |
Sujit Reddy Thumma | 8ca66b4 | 2014-12-03 18:10:07 +0200 | [diff] [blame] | 2435 | void mmc_set_ios(struct mmc_host *host) |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 2436 | { |
| 2437 | struct mmc_ios *ios = &host->ios; |
| 2438 | |
Pierre Ossman | cd9277c | 2007-02-18 12:07:47 +0100 | [diff] [blame] | 2439 | pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u " |
| 2440 | "width %u timing %u\n", |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 2441 | mmc_hostname(host), ios->clock, ios->bus_mode, |
| 2442 | ios->power_mode, ios->chip_select, ios->vdd, |
Wolfram Sang | ed9feec | 2016-01-29 09:27:50 +0100 | [diff] [blame] | 2443 | 1 << ios->bus_width, ios->timing); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 2444 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2445 | if (ios->clock > 0) |
| 2446 | mmc_set_ungated(host); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 2447 | host->ops->set_ios(host, ios); |
Sujit Reddy Thumma | 14ec3cc | 2013-02-14 08:13:52 +0530 | [diff] [blame] | 2448 | if (ios->old_rate != ios->clock) { |
| 2449 | if (likely(ios->clk_ts)) { |
| 2450 | char trace_info[80]; |
| 2451 | snprintf(trace_info, 80, |
| 2452 | "%s: freq_KHz %d --> %d | t = %d", |
| 2453 | mmc_hostname(host), ios->old_rate / 1000, |
| 2454 | ios->clock / 1000, jiffies_to_msecs( |
| 2455 | (long)jiffies - (long)ios->clk_ts)); |
| 2456 | trace_mmc_clk(trace_info); |
| 2457 | } |
| 2458 | ios->old_rate = ios->clock; |
| 2459 | ios->clk_ts = jiffies; |
| 2460 | } |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 2461 | } |
Sujit Reddy Thumma | 8ca66b4 | 2014-12-03 18:10:07 +0200 | [diff] [blame] | 2462 | EXPORT_SYMBOL(mmc_set_ios); |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 2463 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2464 | /* |
| 2465 | * Control chip select pin on a host. |
| 2466 | */ |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 2467 | void mmc_set_chip_select(struct mmc_host *host, int mode) |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 2468 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2469 | mmc_host_clk_hold(host); |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 2470 | host->ios.chip_select = mode; |
| 2471 | mmc_set_ios(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2472 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | } |
| 2474 | |
| 2475 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2476 | * Sets the host clock to the highest possible frequency that |
| 2477 | * is below "hz". |
| 2478 | */ |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2479 | static void __mmc_set_clock(struct mmc_host *host, unsigned int hz) |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2480 | { |
Adrian Hunter | 6a98f1e | 2014-09-23 23:00:26 +0300 | [diff] [blame] | 2481 | WARN_ON(hz && hz < host->f_min); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2482 | |
| 2483 | if (hz > host->f_max) |
| 2484 | hz = host->f_max; |
| 2485 | |
| 2486 | host->ios.clock = hz; |
| 2487 | mmc_set_ios(host); |
| 2488 | } |
| 2489 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2490 | void mmc_set_clock(struct mmc_host *host, unsigned int hz) |
| 2491 | { |
| 2492 | mmc_host_clk_hold(host); |
| 2493 | __mmc_set_clock(host, hz); |
| 2494 | mmc_host_clk_release(host); |
| 2495 | } |
| 2496 | |
| 2497 | #ifdef CONFIG_MMC_CLKGATE |
| 2498 | /* |
| 2499 | * This gates the clock by setting it to 0 Hz. |
| 2500 | */ |
| 2501 | void mmc_gate_clock(struct mmc_host *host) |
| 2502 | { |
| 2503 | unsigned long flags; |
| 2504 | |
Sujit Reddy Thumma | 3a75608d | 2014-12-03 18:13:58 +0200 | [diff] [blame] | 2505 | WARN_ON(!host->ios.clock); |
| 2506 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2507 | spin_lock_irqsave(&host->clk_lock, flags); |
| 2508 | host->clk_old = host->ios.clock; |
| 2509 | host->ios.clock = 0; |
| 2510 | host->clk_gated = true; |
| 2511 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 2512 | mmc_set_ios(host); |
| 2513 | } |
| 2514 | |
| 2515 | /* |
| 2516 | * This restores the clock from gating by using the cached |
| 2517 | * clock value. |
| 2518 | */ |
| 2519 | void mmc_ungate_clock(struct mmc_host *host) |
| 2520 | { |
| 2521 | /* |
| 2522 | * We should previously have gated the clock, so the clock shall |
| 2523 | * be 0 here! The clock may however be 0 during initialization, |
| 2524 | * when some request operations are performed before setting |
| 2525 | * the frequency. When ungate is requested in that situation |
| 2526 | * we just ignore the call. |
| 2527 | */ |
| 2528 | if (host->clk_old) { |
Sujit Reddy Thumma | 3a75608d | 2014-12-03 18:13:58 +0200 | [diff] [blame] | 2529 | WARN_ON(host->ios.clock); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2530 | /* This call will also set host->clk_gated to false */ |
| 2531 | __mmc_set_clock(host, host->clk_old); |
Veerabhadrarao Badiganti | 3ae97c7 | 2017-05-18 20:24:31 +0530 | [diff] [blame] | 2532 | /* |
| 2533 | * We have seen that host controller's clock tuning circuit may |
| 2534 | * go out of sync if controller clocks are gated. |
| 2535 | * To workaround this issue, we are triggering retuning of the |
| 2536 | * tuning circuit after ungating the controller clocks. |
| 2537 | */ |
| 2538 | mmc_retune_needed(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2539 | } |
| 2540 | } |
| 2541 | |
| 2542 | void mmc_set_ungated(struct mmc_host *host) |
| 2543 | { |
| 2544 | unsigned long flags; |
| 2545 | |
| 2546 | /* |
| 2547 | * We've been given a new frequency while the clock is gated, |
| 2548 | * so make sure we regard this as ungating it. |
| 2549 | */ |
| 2550 | spin_lock_irqsave(&host->clk_lock, flags); |
| 2551 | host->clk_gated = false; |
| 2552 | spin_unlock_irqrestore(&host->clk_lock, flags); |
| 2553 | } |
| 2554 | |
| 2555 | #else |
| 2556 | void mmc_set_ungated(struct mmc_host *host) |
| 2557 | { |
| 2558 | } |
Ritesh Harjani | ce4b8f8 | 2015-11-25 10:37:21 +0530 | [diff] [blame] | 2559 | |
| 2560 | void mmc_gate_clock(struct mmc_host *host) |
| 2561 | { |
| 2562 | } |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2563 | #endif |
| 2564 | |
Adrian Hunter | 63e415c | 2014-12-05 19:40:59 +0200 | [diff] [blame] | 2565 | int mmc_execute_tuning(struct mmc_card *card) |
| 2566 | { |
| 2567 | struct mmc_host *host = card->host; |
| 2568 | u32 opcode; |
| 2569 | int err; |
| 2570 | |
| 2571 | if (!host->ops->execute_tuning) |
| 2572 | return 0; |
| 2573 | |
| 2574 | if (mmc_card_mmc(card)) |
| 2575 | opcode = MMC_SEND_TUNING_BLOCK_HS200; |
| 2576 | else |
| 2577 | opcode = MMC_SEND_TUNING_BLOCK; |
| 2578 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2579 | mmc_host_clk_hold(host); |
Adrian Hunter | 63e415c | 2014-12-05 19:40:59 +0200 | [diff] [blame] | 2580 | err = host->ops->execute_tuning(host, opcode); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2581 | mmc_host_clk_release(host); |
Adrian Hunter | 63e415c | 2014-12-05 19:40:59 +0200 | [diff] [blame] | 2582 | |
| 2583 | if (err) |
Russell King | 07d97d8 | 2016-01-29 09:44:05 +0000 | [diff] [blame] | 2584 | pr_err("%s: tuning execution failed: %d\n", |
| 2585 | mmc_hostname(host), err); |
Adrian Hunter | 79d5a65 | 2015-05-07 13:10:13 +0300 | [diff] [blame] | 2586 | else |
| 2587 | mmc_retune_enable(host); |
Adrian Hunter | 63e415c | 2014-12-05 19:40:59 +0200 | [diff] [blame] | 2588 | |
| 2589 | return err; |
| 2590 | } |
| 2591 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2592 | /* |
| 2593 | * Change the bus mode (open drain/push-pull) of a host. |
| 2594 | */ |
| 2595 | void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode) |
| 2596 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2597 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2598 | host->ios.bus_mode = mode; |
| 2599 | mmc_set_ios(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2600 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* |
| 2604 | * Change data bus width of a host. |
| 2605 | */ |
| 2606 | void mmc_set_bus_width(struct mmc_host *host, unsigned int width) |
| 2607 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2608 | mmc_host_clk_hold(host); |
Philip Rakity | 4c4cb17 | 2011-05-13 11:17:18 +0530 | [diff] [blame] | 2609 | host->ios.bus_width = width; |
| 2610 | mmc_set_ios(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 2611 | mmc_host_clk_release(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 2614 | /* |
| 2615 | * Set initial state after a power cycle or a hw_reset. |
| 2616 | */ |
| 2617 | void mmc_set_initial_state(struct mmc_host *host) |
| 2618 | { |
Adrian Hunter | 79d5a65 | 2015-05-07 13:10:13 +0300 | [diff] [blame] | 2619 | mmc_retune_disable(host); |
| 2620 | |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 2621 | if (mmc_host_is_spi(host)) |
| 2622 | host->ios.chip_select = MMC_CS_HIGH; |
Author: Stephen Boyd | c0432a1 | 2014-12-03 19:49:13 +0200 | [diff] [blame] | 2623 | else { |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 2624 | host->ios.chip_select = MMC_CS_DONTCARE; |
Author: Stephen Boyd | c0432a1 | 2014-12-03 19:49:13 +0200 | [diff] [blame] | 2625 | host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN; |
| 2626 | } |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 2627 | host->ios.bus_width = MMC_BUS_WIDTH_1; |
| 2628 | host->ios.timing = MMC_TIMING_LEGACY; |
Adrian Hunter | 75e8a22 | 2015-02-06 14:12:51 +0200 | [diff] [blame] | 2629 | host->ios.drv_type = 0; |
Shawn Lin | 81ac2af | 2016-05-26 09:56:22 +0800 | [diff] [blame] | 2630 | host->ios.enhanced_strobe = false; |
| 2631 | |
| 2632 | /* |
| 2633 | * Make sure we are in non-enhanced strobe mode before we |
| 2634 | * actually enable it in ext_csd. |
| 2635 | */ |
| 2636 | if ((host->caps2 & MMC_CAP2_HS400_ES) && |
| 2637 | host->ops->hs400_enhanced_strobe) |
| 2638 | host->ops->hs400_enhanced_strobe(host, &host->ios); |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 2639 | |
| 2640 | mmc_set_ios(host); |
| 2641 | } |
| 2642 | |
Anton Vorontsov | 86e8286 | 2008-11-26 22:54:17 +0300 | [diff] [blame] | 2643 | /** |
| 2644 | * mmc_vdd_to_ocrbitnum - Convert a voltage to the OCR bit number |
| 2645 | * @vdd: voltage (mV) |
| 2646 | * @low_bits: prefer low bits in boundary cases |
| 2647 | * |
| 2648 | * This function returns the OCR bit number according to the provided @vdd |
| 2649 | * value. If conversion is not possible a negative errno value returned. |
| 2650 | * |
| 2651 | * Depending on the @low_bits flag the function prefers low or high OCR bits |
| 2652 | * on boundary voltages. For example, |
| 2653 | * with @low_bits = true, 3300 mV translates to ilog2(MMC_VDD_32_33); |
| 2654 | * with @low_bits = false, 3300 mV translates to ilog2(MMC_VDD_33_34); |
| 2655 | * |
| 2656 | * Any value in the [1951:1999] range translates to the ilog2(MMC_VDD_20_21). |
| 2657 | */ |
| 2658 | static int mmc_vdd_to_ocrbitnum(int vdd, bool low_bits) |
| 2659 | { |
| 2660 | const int max_bit = ilog2(MMC_VDD_35_36); |
| 2661 | int bit; |
| 2662 | |
| 2663 | if (vdd < 1650 || vdd > 3600) |
| 2664 | return -EINVAL; |
| 2665 | |
| 2666 | if (vdd >= 1650 && vdd <= 1950) |
| 2667 | return ilog2(MMC_VDD_165_195); |
| 2668 | |
| 2669 | if (low_bits) |
| 2670 | vdd -= 1; |
| 2671 | |
| 2672 | /* Base 2000 mV, step 100 mV, bit's base 8. */ |
| 2673 | bit = (vdd - 2000) / 100 + 8; |
| 2674 | if (bit > max_bit) |
| 2675 | return max_bit; |
| 2676 | return bit; |
| 2677 | } |
| 2678 | |
| 2679 | /** |
| 2680 | * mmc_vddrange_to_ocrmask - Convert a voltage range to the OCR mask |
| 2681 | * @vdd_min: minimum voltage value (mV) |
| 2682 | * @vdd_max: maximum voltage value (mV) |
| 2683 | * |
| 2684 | * This function returns the OCR mask bits according to the provided @vdd_min |
| 2685 | * and @vdd_max values. If conversion is not possible the function returns 0. |
| 2686 | * |
| 2687 | * Notes wrt boundary cases: |
| 2688 | * This function sets the OCR bits for all boundary voltages, for example |
| 2689 | * [3300:3400] range is translated to MMC_VDD_32_33 | MMC_VDD_33_34 | |
| 2690 | * MMC_VDD_34_35 mask. |
| 2691 | */ |
| 2692 | u32 mmc_vddrange_to_ocrmask(int vdd_min, int vdd_max) |
| 2693 | { |
| 2694 | u32 mask = 0; |
| 2695 | |
| 2696 | if (vdd_max < vdd_min) |
| 2697 | return 0; |
| 2698 | |
| 2699 | /* Prefer high bits for the boundary vdd_max values. */ |
| 2700 | vdd_max = mmc_vdd_to_ocrbitnum(vdd_max, false); |
| 2701 | if (vdd_max < 0) |
| 2702 | return 0; |
| 2703 | |
| 2704 | /* Prefer low bits for the boundary vdd_min values. */ |
| 2705 | vdd_min = mmc_vdd_to_ocrbitnum(vdd_min, true); |
| 2706 | if (vdd_min < 0) |
| 2707 | return 0; |
| 2708 | |
| 2709 | /* Fill the mask, from max bit to min bit. */ |
| 2710 | while (vdd_max >= vdd_min) |
| 2711 | mask |= 1 << vdd_max--; |
| 2712 | |
| 2713 | return mask; |
| 2714 | } |
| 2715 | EXPORT_SYMBOL(mmc_vddrange_to_ocrmask); |
| 2716 | |
Haijun Zhang | 6e9e318 | 2013-08-26 09:19:22 +0800 | [diff] [blame] | 2717 | #ifdef CONFIG_OF |
| 2718 | |
| 2719 | /** |
| 2720 | * mmc_of_parse_voltage - return mask of supported voltages |
| 2721 | * @np: The device node need to be parsed. |
| 2722 | * @mask: mask of voltages available for MMC/SD/SDIO |
| 2723 | * |
Russell King | cf92574 | 2016-01-29 09:43:55 +0000 | [diff] [blame] | 2724 | * Parse the "voltage-ranges" DT property, returning zero if it is not |
| 2725 | * found, negative errno if the voltage-range specification is invalid, |
| 2726 | * or one if the voltage-range is specified and successfully parsed. |
Haijun Zhang | 6e9e318 | 2013-08-26 09:19:22 +0800 | [diff] [blame] | 2727 | */ |
| 2728 | int mmc_of_parse_voltage(struct device_node *np, u32 *mask) |
| 2729 | { |
| 2730 | const u32 *voltage_ranges; |
| 2731 | int num_ranges, i; |
| 2732 | |
| 2733 | voltage_ranges = of_get_property(np, "voltage-ranges", &num_ranges); |
| 2734 | num_ranges = num_ranges / sizeof(*voltage_ranges) / 2; |
Russell King | 10a16a0 | 2016-01-29 09:43:50 +0000 | [diff] [blame] | 2735 | if (!voltage_ranges) { |
| 2736 | pr_debug("%s: voltage-ranges unspecified\n", np->full_name); |
Russell King | cf92574 | 2016-01-29 09:43:55 +0000 | [diff] [blame] | 2737 | return 0; |
Russell King | 10a16a0 | 2016-01-29 09:43:50 +0000 | [diff] [blame] | 2738 | } |
| 2739 | if (!num_ranges) { |
| 2740 | pr_err("%s: voltage-ranges empty\n", np->full_name); |
Haijun Zhang | 6e9e318 | 2013-08-26 09:19:22 +0800 | [diff] [blame] | 2741 | return -EINVAL; |
| 2742 | } |
| 2743 | |
| 2744 | for (i = 0; i < num_ranges; i++) { |
| 2745 | const int j = i * 2; |
| 2746 | u32 ocr_mask; |
| 2747 | |
| 2748 | ocr_mask = mmc_vddrange_to_ocrmask( |
| 2749 | be32_to_cpu(voltage_ranges[j]), |
| 2750 | be32_to_cpu(voltage_ranges[j + 1])); |
| 2751 | if (!ocr_mask) { |
| 2752 | pr_err("%s: voltage-range #%d is invalid\n", |
| 2753 | np->full_name, i); |
| 2754 | return -EINVAL; |
| 2755 | } |
| 2756 | *mask |= ocr_mask; |
| 2757 | } |
| 2758 | |
Russell King | cf92574 | 2016-01-29 09:43:55 +0000 | [diff] [blame] | 2759 | return 1; |
Haijun Zhang | 6e9e318 | 2013-08-26 09:19:22 +0800 | [diff] [blame] | 2760 | } |
| 2761 | EXPORT_SYMBOL(mmc_of_parse_voltage); |
| 2762 | |
| 2763 | #endif /* CONFIG_OF */ |
| 2764 | |
Sascha Hauer | 25185f3 | 2014-06-30 11:07:25 +0200 | [diff] [blame] | 2765 | static int mmc_of_get_func_num(struct device_node *node) |
| 2766 | { |
| 2767 | u32 reg; |
| 2768 | int ret; |
| 2769 | |
| 2770 | ret = of_property_read_u32(node, "reg", ®); |
| 2771 | if (ret < 0) |
| 2772 | return ret; |
| 2773 | |
| 2774 | return reg; |
| 2775 | } |
| 2776 | |
| 2777 | struct device_node *mmc_of_find_child_device(struct mmc_host *host, |
| 2778 | unsigned func_num) |
| 2779 | { |
| 2780 | struct device_node *node; |
| 2781 | |
| 2782 | if (!host->parent || !host->parent->of_node) |
| 2783 | return NULL; |
| 2784 | |
| 2785 | for_each_child_of_node(host->parent->of_node, node) { |
| 2786 | if (mmc_of_get_func_num(node) == func_num) |
| 2787 | return node; |
| 2788 | } |
| 2789 | |
| 2790 | return NULL; |
| 2791 | } |
| 2792 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2793 | #ifdef CONFIG_REGULATOR |
| 2794 | |
| 2795 | /** |
Heiko Stuebner | 310c805 | 2015-10-12 18:00:54 +0200 | [diff] [blame] | 2796 | * mmc_ocrbitnum_to_vdd - Convert a OCR bit number to its voltage |
| 2797 | * @vdd_bit: OCR bit number |
| 2798 | * @min_uV: minimum voltage value (mV) |
| 2799 | * @max_uV: maximum voltage value (mV) |
| 2800 | * |
| 2801 | * This function returns the voltage range according to the provided OCR |
| 2802 | * bit number. If conversion is not possible a negative errno value returned. |
| 2803 | */ |
| 2804 | static int mmc_ocrbitnum_to_vdd(int vdd_bit, int *min_uV, int *max_uV) |
| 2805 | { |
| 2806 | int tmp; |
| 2807 | |
| 2808 | if (!vdd_bit) |
| 2809 | return -EINVAL; |
| 2810 | |
| 2811 | /* |
| 2812 | * REVISIT mmc_vddrange_to_ocrmask() may have set some |
| 2813 | * bits this regulator doesn't quite support ... don't |
| 2814 | * be too picky, most cards and regulators are OK with |
| 2815 | * a 0.1V range goof (it's a small error percentage). |
| 2816 | */ |
| 2817 | tmp = vdd_bit - ilog2(MMC_VDD_165_195); |
| 2818 | if (tmp == 0) { |
| 2819 | *min_uV = 1650 * 1000; |
| 2820 | *max_uV = 1950 * 1000; |
| 2821 | } else { |
| 2822 | *min_uV = 1900 * 1000 + tmp * 100 * 1000; |
| 2823 | *max_uV = *min_uV + 100 * 1000; |
| 2824 | } |
| 2825 | |
| 2826 | return 0; |
| 2827 | } |
| 2828 | |
| 2829 | /** |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2830 | * mmc_regulator_get_ocrmask - return mask of supported voltages |
| 2831 | * @supply: regulator to use |
| 2832 | * |
| 2833 | * This returns either a negative errno, or a mask of voltages that |
| 2834 | * can be provided to MMC/SD/SDIO devices using the specified voltage |
| 2835 | * regulator. This would normally be called before registering the |
| 2836 | * MMC host adapter. |
| 2837 | */ |
| 2838 | int mmc_regulator_get_ocrmask(struct regulator *supply) |
| 2839 | { |
| 2840 | int result = 0; |
| 2841 | int count; |
| 2842 | int i; |
Javier Martinez Canillas | 9ed7ca8 | 2014-08-14 14:39:00 +0200 | [diff] [blame] | 2843 | int vdd_uV; |
| 2844 | int vdd_mV; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2845 | |
| 2846 | count = regulator_count_voltages(supply); |
| 2847 | if (count < 0) |
| 2848 | return count; |
| 2849 | |
| 2850 | for (i = 0; i < count; i++) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2851 | vdd_uV = regulator_list_voltage(supply, i); |
| 2852 | if (vdd_uV <= 0) |
| 2853 | continue; |
| 2854 | |
| 2855 | vdd_mV = vdd_uV / 1000; |
| 2856 | result |= mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 2857 | } |
| 2858 | |
Javier Martinez Canillas | 9ed7ca8 | 2014-08-14 14:39:00 +0200 | [diff] [blame] | 2859 | if (!result) { |
| 2860 | vdd_uV = regulator_get_voltage(supply); |
| 2861 | if (vdd_uV <= 0) |
| 2862 | return vdd_uV; |
| 2863 | |
| 2864 | vdd_mV = vdd_uV / 1000; |
| 2865 | result = mmc_vddrange_to_ocrmask(vdd_mV, vdd_mV); |
| 2866 | } |
| 2867 | |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2868 | return result; |
| 2869 | } |
Chris Ball | 45a6b32 | 2012-06-11 09:39:12 -0400 | [diff] [blame] | 2870 | EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2871 | |
| 2872 | /** |
| 2873 | * mmc_regulator_set_ocr - set regulator to match host->ios voltage |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2874 | * @mmc: the host to regulate |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2875 | * @supply: regulator to use |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2876 | * @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] | 2877 | * |
| 2878 | * Returns zero on success, else negative errno. |
| 2879 | * |
| 2880 | * MMC host drivers may use this to enable or disable a regulator using |
| 2881 | * a particular supply voltage. This would normally be called from the |
| 2882 | * set_ios() method. |
| 2883 | */ |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2884 | int mmc_regulator_set_ocr(struct mmc_host *mmc, |
| 2885 | struct regulator *supply, |
| 2886 | unsigned short vdd_bit) |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2887 | { |
| 2888 | int result = 0; |
| 2889 | int min_uV, max_uV; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2890 | |
| 2891 | if (vdd_bit) { |
Heiko Stuebner | 310c805 | 2015-10-12 18:00:54 +0200 | [diff] [blame] | 2892 | mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2893 | |
Tim Kryger | ca6429d | 2014-08-11 22:05:12 -0700 | [diff] [blame] | 2894 | result = regulator_set_voltage(supply, min_uV, max_uV); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2895 | if (result == 0 && !mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2896 | result = regulator_enable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2897 | if (!result) |
| 2898 | mmc->regulator_enabled = true; |
| 2899 | } |
| 2900 | } else if (mmc->regulator_enabled) { |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2901 | result = regulator_disable(supply); |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2902 | if (result == 0) |
| 2903 | mmc->regulator_enabled = false; |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2904 | } |
| 2905 | |
Linus Walleij | 99fc513 | 2010-09-29 01:08:27 -0400 | [diff] [blame] | 2906 | if (result) |
| 2907 | dev_err(mmc_dev(mmc), |
| 2908 | "could not set regulator OCR (%d)\n", result); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2909 | return result; |
| 2910 | } |
Chris Ball | 45a6b32 | 2012-06-11 09:39:12 -0400 | [diff] [blame] | 2911 | EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr); |
David Brownell | 5c13941 | 2009-03-11 03:30:43 -0800 | [diff] [blame] | 2912 | |
Douglas Anderson | 2086f80 | 2015-10-12 14:48:25 +0200 | [diff] [blame] | 2913 | static int mmc_regulator_set_voltage_if_supported(struct regulator *regulator, |
| 2914 | int min_uV, int target_uV, |
| 2915 | int max_uV) |
| 2916 | { |
| 2917 | /* |
| 2918 | * Check if supported first to avoid errors since we may try several |
| 2919 | * signal levels during power up and don't want to show errors. |
| 2920 | */ |
| 2921 | if (!regulator_is_supported_voltage(regulator, min_uV, max_uV)) |
| 2922 | return -EINVAL; |
| 2923 | |
| 2924 | return regulator_set_voltage_triplet(regulator, min_uV, target_uV, |
| 2925 | max_uV); |
| 2926 | } |
| 2927 | |
| 2928 | /** |
| 2929 | * mmc_regulator_set_vqmmc - Set VQMMC as per the ios |
| 2930 | * |
| 2931 | * For 3.3V signaling, we try to match VQMMC to VMMC as closely as possible. |
| 2932 | * That will match the behavior of old boards where VQMMC and VMMC were supplied |
| 2933 | * by the same supply. The Bus Operating conditions for 3.3V signaling in the |
| 2934 | * SD card spec also define VQMMC in terms of VMMC. |
| 2935 | * If this is not possible we'll try the full 2.7-3.6V of the spec. |
| 2936 | * |
| 2937 | * For 1.2V and 1.8V signaling we'll try to get as close as possible to the |
| 2938 | * requested voltage. This is definitely a good idea for UHS where there's a |
| 2939 | * separate regulator on the card that's trying to make 1.8V and it's best if |
| 2940 | * we match. |
| 2941 | * |
| 2942 | * This function is expected to be used by a controller's |
| 2943 | * start_signal_voltage_switch() function. |
| 2944 | */ |
| 2945 | int mmc_regulator_set_vqmmc(struct mmc_host *mmc, struct mmc_ios *ios) |
| 2946 | { |
| 2947 | struct device *dev = mmc_dev(mmc); |
| 2948 | int ret, volt, min_uV, max_uV; |
| 2949 | |
| 2950 | /* If no vqmmc supply then we can't change the voltage */ |
| 2951 | if (IS_ERR(mmc->supply.vqmmc)) |
| 2952 | return -EINVAL; |
| 2953 | |
| 2954 | switch (ios->signal_voltage) { |
| 2955 | case MMC_SIGNAL_VOLTAGE_120: |
| 2956 | return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
| 2957 | 1100000, 1200000, 1300000); |
| 2958 | case MMC_SIGNAL_VOLTAGE_180: |
| 2959 | return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
| 2960 | 1700000, 1800000, 1950000); |
| 2961 | case MMC_SIGNAL_VOLTAGE_330: |
| 2962 | ret = mmc_ocrbitnum_to_vdd(mmc->ios.vdd, &volt, &max_uV); |
| 2963 | if (ret < 0) |
| 2964 | return ret; |
| 2965 | |
| 2966 | dev_dbg(dev, "%s: found vmmc voltage range of %d-%duV\n", |
| 2967 | __func__, volt, max_uV); |
| 2968 | |
| 2969 | min_uV = max(volt - 300000, 2700000); |
| 2970 | max_uV = min(max_uV + 200000, 3600000); |
| 2971 | |
| 2972 | /* |
| 2973 | * Due to a limitation in the current implementation of |
| 2974 | * regulator_set_voltage_triplet() which is taking the lowest |
| 2975 | * voltage possible if below the target, search for a suitable |
| 2976 | * voltage in two steps and try to stay close to vmmc |
| 2977 | * with a 0.3V tolerance at first. |
| 2978 | */ |
| 2979 | if (!mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
| 2980 | min_uV, volt, max_uV)) |
| 2981 | return 0; |
| 2982 | |
| 2983 | return mmc_regulator_set_voltage_if_supported(mmc->supply.vqmmc, |
| 2984 | 2700000, volt, 3600000); |
| 2985 | default: |
| 2986 | return -EINVAL; |
| 2987 | } |
| 2988 | } |
| 2989 | EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc); |
| 2990 | |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 2991 | #endif /* CONFIG_REGULATOR */ |
| 2992 | |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 2993 | int mmc_regulator_get_supply(struct mmc_host *mmc) |
| 2994 | { |
| 2995 | struct device *dev = mmc_dev(mmc); |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 2996 | int ret; |
| 2997 | |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 2998 | mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc"); |
Mark Brown | bc35d5e | 2013-07-29 21:58:01 +0100 | [diff] [blame] | 2999 | mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc"); |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 3000 | |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 3001 | if (IS_ERR(mmc->supply.vmmc)) { |
| 3002 | if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER) |
| 3003 | return -EPROBE_DEFER; |
ludovic.desroches@atmel.com | 6e1bbc5 | 2015-11-09 15:03:59 +0100 | [diff] [blame] | 3004 | dev_dbg(dev, "No vmmc regulator found\n"); |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 3005 | } else { |
| 3006 | ret = mmc_regulator_get_ocrmask(mmc->supply.vmmc); |
| 3007 | if (ret > 0) |
| 3008 | mmc->ocr_avail = ret; |
| 3009 | else |
| 3010 | dev_warn(dev, "Failed getting OCR mask: %d\n", ret); |
| 3011 | } |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 3012 | |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 3013 | if (IS_ERR(mmc->supply.vqmmc)) { |
| 3014 | if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER) |
| 3015 | return -EPROBE_DEFER; |
ludovic.desroches@atmel.com | 6e1bbc5 | 2015-11-09 15:03:59 +0100 | [diff] [blame] | 3016 | dev_dbg(dev, "No vqmmc regulator found\n"); |
Tim Kryger | 4d1f52f | 2014-05-06 15:57:01 -0700 | [diff] [blame] | 3017 | } |
Guennadi Liakhovetski | e137788 | 2012-06-20 02:28:43 -0400 | [diff] [blame] | 3018 | |
| 3019 | return 0; |
| 3020 | } |
| 3021 | EXPORT_SYMBOL_GPL(mmc_regulator_get_supply); |
| 3022 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3023 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | * Mask off any voltages we don't support and select |
| 3025 | * the lowest voltage |
| 3026 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3027 | u32 mmc_select_voltage(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | { |
| 3029 | int bit; |
| 3030 | |
Ulf Hansson | 726d6f2 | 2013-09-16 12:06:15 +0200 | [diff] [blame] | 3031 | /* |
| 3032 | * Sanity check the voltages that the card claims to |
| 3033 | * support. |
| 3034 | */ |
| 3035 | if (ocr & 0x7F) { |
| 3036 | dev_warn(mmc_dev(host), |
| 3037 | "card claims to support voltages below defined range\n"); |
| 3038 | ocr &= ~0x7F; |
| 3039 | } |
| 3040 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3041 | ocr &= host->ocr_avail; |
Ulf Hansson | ce69d37 | 2013-09-16 11:28:42 +0200 | [diff] [blame] | 3042 | if (!ocr) { |
| 3043 | dev_warn(mmc_dev(host), "no support for card's volts\n"); |
| 3044 | return 0; |
| 3045 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3046 | |
Ulf Hansson | ce69d37 | 2013-09-16 11:28:42 +0200 | [diff] [blame] | 3047 | if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) { |
| 3048 | bit = ffs(ocr) - 1; |
Timo Teras | 63ef731 | 2006-11-02 19:43:27 +0100 | [diff] [blame] | 3049 | ocr &= 3 << bit; |
Ulf Hansson | ce69d37 | 2013-09-16 11:28:42 +0200 | [diff] [blame] | 3050 | mmc_power_cycle(host, ocr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | } else { |
Ulf Hansson | ce69d37 | 2013-09-16 11:28:42 +0200 | [diff] [blame] | 3052 | bit = fls(ocr) - 1; |
| 3053 | ocr &= 3 << bit; |
| 3054 | if (bit != host->ios.vdd) |
| 3055 | dev_warn(mmc_dev(host), "exceeding card's volts\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | return ocr; |
| 3059 | } |
| 3060 | |
Johan Rudholm | 567c890 | 2013-01-28 15:08:27 +0100 | [diff] [blame] | 3061 | int __mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage) |
| 3062 | { |
| 3063 | int err = 0; |
| 3064 | int old_signal_voltage = host->ios.signal_voltage; |
| 3065 | |
| 3066 | host->ios.signal_voltage = signal_voltage; |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3067 | if (host->ops->start_signal_voltage_switch) { |
| 3068 | mmc_host_clk_hold(host); |
Johan Rudholm | 567c890 | 2013-01-28 15:08:27 +0100 | [diff] [blame] | 3069 | err = host->ops->start_signal_voltage_switch(host, &host->ios); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3070 | mmc_host_clk_release(host); |
| 3071 | } |
Johan Rudholm | 567c890 | 2013-01-28 15:08:27 +0100 | [diff] [blame] | 3072 | |
| 3073 | if (err) |
| 3074 | host->ios.signal_voltage = old_signal_voltage; |
| 3075 | |
| 3076 | return err; |
| 3077 | |
| 3078 | } |
| 3079 | |
Ulf Hansson | 0f791fd | 2013-09-12 15:36:34 +0200 | [diff] [blame] | 3080 | int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr) |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3081 | { |
| 3082 | struct mmc_command cmd = {0}; |
| 3083 | int err = 0; |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3084 | u32 clock; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3085 | |
| 3086 | BUG_ON(!host); |
| 3087 | |
| 3088 | /* |
| 3089 | * Send CMD11 only if the request is to switch the card to |
| 3090 | * 1.8V signalling. |
| 3091 | */ |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3092 | if (signal_voltage == MMC_SIGNAL_VOLTAGE_330) |
| 3093 | return __mmc_set_signal_voltage(host, signal_voltage); |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3094 | |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3095 | /* |
| 3096 | * If we cannot switch voltages, return failure so the caller |
| 3097 | * can continue without UHS mode |
| 3098 | */ |
| 3099 | if (!host->ops->start_signal_voltage_switch) |
| 3100 | return -EPERM; |
| 3101 | if (!host->ops->card_busy) |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 3102 | pr_warn("%s: cannot verify signal voltage switch\n", |
| 3103 | mmc_hostname(host)); |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3104 | |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3105 | cmd.opcode = SD_SWITCH_VOLTAGE; |
| 3106 | cmd.arg = 0; |
| 3107 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 3108 | |
Subhash Jadavani | 5fc13cd | 2013-09-06 19:07:07 +0530 | [diff] [blame] | 3109 | /* |
| 3110 | * Hold the clock reference so clock doesn't get auto gated during this |
| 3111 | * voltage switch sequence. |
| 3112 | */ |
| 3113 | mmc_host_clk_hold(host); |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3114 | err = mmc_wait_for_cmd(host, &cmd, 0); |
| 3115 | if (err) |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3116 | goto err_command; |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3117 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3118 | if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) { |
| 3119 | err = -EIO; |
| 3120 | goto err_command; |
| 3121 | } |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3122 | /* |
| 3123 | * The card should drive cmd and dat[0:3] low immediately |
| 3124 | * after the response of cmd11, but wait 1 ms to be sure |
| 3125 | */ |
| 3126 | mmc_delay(1); |
| 3127 | if (host->ops->card_busy && !host->ops->card_busy(host)) { |
| 3128 | err = -EAGAIN; |
| 3129 | goto power_cycle; |
| 3130 | } |
| 3131 | /* |
| 3132 | * During a signal voltage level switch, the clock must be gated |
| 3133 | * for 5 ms according to the SD spec |
| 3134 | */ |
Sahitya Tummala | dc18298 | 2013-08-20 15:32:09 +0530 | [diff] [blame] | 3135 | host->card_clock_off = true; |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3136 | clock = host->ios.clock; |
| 3137 | host->ios.clock = 0; |
| 3138 | mmc_set_ios(host); |
| 3139 | |
| 3140 | if (__mmc_set_signal_voltage(host, signal_voltage)) { |
| 3141 | /* |
| 3142 | * Voltages may not have been switched, but we've already |
| 3143 | * sent CMD11, so a power cycle is required anyway |
| 3144 | */ |
| 3145 | err = -EAGAIN; |
Sahitya Tummala | dc18298 | 2013-08-20 15:32:09 +0530 | [diff] [blame] | 3146 | host->ios.clock = clock; |
| 3147 | mmc_set_ios(host); |
| 3148 | host->card_clock_off = false; |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3149 | goto power_cycle; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3150 | } |
| 3151 | |
Doug Anderson | 7c5209c | 2015-05-12 14:46:11 -0700 | [diff] [blame] | 3152 | /* Keep clock gated for at least 10 ms, though spec only says 5 ms */ |
| 3153 | mmc_delay(10); |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3154 | host->ios.clock = clock; |
| 3155 | mmc_set_ios(host); |
| 3156 | |
Sahitya Tummala | dc18298 | 2013-08-20 15:32:09 +0530 | [diff] [blame] | 3157 | host->card_clock_off = false; |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3158 | /* Wait for at least 1 ms according to spec */ |
| 3159 | mmc_delay(1); |
| 3160 | |
| 3161 | /* |
| 3162 | * Failure to switch is indicated by the card holding |
| 3163 | * dat[0:3] low |
| 3164 | */ |
| 3165 | if (host->ops->card_busy && host->ops->card_busy(host)) |
| 3166 | err = -EAGAIN; |
| 3167 | |
| 3168 | power_cycle: |
| 3169 | if (err) { |
| 3170 | pr_debug("%s: Signal voltage switch failed, " |
| 3171 | "power cycling card\n", mmc_hostname(host)); |
Ulf Hansson | 0f791fd | 2013-09-12 15:36:34 +0200 | [diff] [blame] | 3172 | mmc_power_cycle(host, ocr); |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3173 | } |
| 3174 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3175 | err_command: |
| 3176 | mmc_host_clk_release(host); |
| 3177 | |
Johan Rudholm | 0797e5f | 2013-01-28 15:08:28 +0100 | [diff] [blame] | 3178 | return err; |
Arindam Nath | f2119df | 2011-05-05 12:18:57 +0530 | [diff] [blame] | 3179 | } |
| 3180 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3182 | * Select timing parameters for host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3184 | void mmc_set_timing(struct mmc_host *host, unsigned int timing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3186 | mmc_host_clk_hold(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3187 | host->ios.timing = timing; |
| 3188 | mmc_set_ios(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3189 | mmc_host_clk_release(host); |
Pierre Ossman | b57c43a | 2005-09-06 15:18:53 -0700 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 3193 | * Select appropriate driver type for host. |
| 3194 | */ |
| 3195 | void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type) |
| 3196 | { |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3197 | mmc_host_clk_hold(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 3198 | host->ios.drv_type = drv_type; |
| 3199 | mmc_set_ios(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3200 | mmc_host_clk_release(host); |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 3201 | } |
| 3202 | |
Adrian Hunter | e23350b | 2015-02-06 14:12:55 +0200 | [diff] [blame] | 3203 | int mmc_select_drive_strength(struct mmc_card *card, unsigned int max_dtr, |
| 3204 | int card_drv_type, int *drv_type) |
| 3205 | { |
| 3206 | struct mmc_host *host = card->host; |
| 3207 | int host_drv_type = SD_DRIVER_TYPE_B; |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3208 | int drive_strength; |
Adrian Hunter | e23350b | 2015-02-06 14:12:55 +0200 | [diff] [blame] | 3209 | |
| 3210 | *drv_type = 0; |
| 3211 | |
| 3212 | if (!host->ops->select_drive_strength) |
| 3213 | return 0; |
| 3214 | |
| 3215 | /* Use SD definition of driver strength for hosts */ |
| 3216 | if (host->caps & MMC_CAP_DRIVER_TYPE_A) |
| 3217 | host_drv_type |= SD_DRIVER_TYPE_A; |
| 3218 | |
| 3219 | if (host->caps & MMC_CAP_DRIVER_TYPE_C) |
| 3220 | host_drv_type |= SD_DRIVER_TYPE_C; |
| 3221 | |
| 3222 | if (host->caps & MMC_CAP_DRIVER_TYPE_D) |
| 3223 | host_drv_type |= SD_DRIVER_TYPE_D; |
| 3224 | |
| 3225 | /* |
| 3226 | * The drive strength that the hardware can support |
| 3227 | * depends on the board design. Pass the appropriate |
| 3228 | * information and let the hardware specific code |
| 3229 | * return what is possible given the options |
| 3230 | */ |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3231 | mmc_host_clk_hold(host); |
| 3232 | drive_strength = host->ops->select_drive_strength(card, max_dtr, |
| 3233 | host_drv_type, |
| 3234 | card_drv_type, |
| 3235 | drv_type); |
| 3236 | mmc_host_clk_release(host); |
| 3237 | |
| 3238 | return drive_strength; |
Adrian Hunter | e23350b | 2015-02-06 14:12:55 +0200 | [diff] [blame] | 3239 | } |
| 3240 | |
Arindam Nath | d6d50a1 | 2011-05-05 12:18:59 +0530 | [diff] [blame] | 3241 | /* |
Russell King | 45f8245 | 2005-12-14 14:57:35 +0000 | [diff] [blame] | 3242 | * Apply power to the MMC stack. This is a two-stage process. |
| 3243 | * First, we enable power to the card without the clock running. |
| 3244 | * We then wait a bit for the power to stabilise. Finally, |
| 3245 | * enable the bus drivers and clock to the card. |
| 3246 | * |
| 3247 | * We must _NOT_ enable the clock prior to power stablising. |
| 3248 | * |
| 3249 | * If a host does all the power sequencing itself, ignore the |
| 3250 | * initial MMC_POWER_UP stage. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | */ |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 3252 | void mmc_power_up(struct mmc_host *host, u32 ocr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3253 | { |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 3254 | if (host->ios.power_mode == MMC_POWER_ON) |
| 3255 | return; |
| 3256 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3257 | mmc_host_clk_hold(host); |
| 3258 | |
Ulf Hansson | 3aa8793 | 2014-11-28 14:38:36 +0100 | [diff] [blame] | 3259 | mmc_pwrseq_pre_power_on(host); |
| 3260 | |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 3261 | host->ios.vdd = fls(ocr) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3262 | host->ios.power_mode = MMC_POWER_UP; |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 3263 | /* Set initial state and call mmc_set_ios */ |
| 3264 | mmc_set_initial_state(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | |
Tim Kryger | ceae98f | 2014-04-24 14:44:26 -0700 | [diff] [blame] | 3266 | /* Try to set signal voltage to 3.3V but fall back to 1.8v or 1.2v */ |
| 3267 | if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_330) == 0) |
| 3268 | dev_dbg(mmc_dev(host), "Initial signal voltage of 3.3v\n"); |
| 3269 | else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_180) == 0) |
| 3270 | dev_dbg(mmc_dev(host), "Initial signal voltage of 1.8v\n"); |
| 3271 | else if (__mmc_set_signal_voltage(host, MMC_SIGNAL_VOLTAGE_120) == 0) |
| 3272 | dev_dbg(mmc_dev(host), "Initial signal voltage of 1.2v\n"); |
Aaron Lu | 108ecc4 | 2012-07-10 16:55:37 +0800 | [diff] [blame] | 3273 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 3274 | /* |
| 3275 | * This delay should be sufficient to allow the power supply |
| 3276 | * to reach the minimum voltage. |
| 3277 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 3278 | mmc_delay(10); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3279 | |
Ulf Hansson | 4febb7e | 2015-02-02 16:01:14 +0100 | [diff] [blame] | 3280 | mmc_pwrseq_post_power_on(host); |
| 3281 | |
Hein Tibosch | 88ae8b8 | 2010-09-06 09:37:19 +0800 | [diff] [blame] | 3282 | host->ios.clock = host->f_init; |
Sascha Hauer | 8dfd037 | 2009-04-09 08:32:02 +0200 | [diff] [blame] | 3283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3284 | host->ios.power_mode = MMC_POWER_ON; |
Russell King | 920e70c | 2006-05-04 18:22:51 +0100 | [diff] [blame] | 3285 | mmc_set_ios(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3286 | |
Pierre Ossman | f9996ae | 2007-09-19 18:38:50 +0200 | [diff] [blame] | 3287 | /* |
| 3288 | * This delay must be at least 74 clock sizes, or 1 ms, or the |
| 3289 | * time required to reach a stable voltage. |
| 3290 | */ |
José M. Fernández | 79bccc5 | 2009-03-10 02:21:21 +0100 | [diff] [blame] | 3291 | mmc_delay(10); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3292 | |
| 3293 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3294 | } |
| 3295 | |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 3296 | void mmc_power_off(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | { |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 3298 | if (host->ios.power_mode == MMC_POWER_OFF) |
| 3299 | return; |
| 3300 | |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3301 | mmc_host_clk_hold(host); |
| 3302 | |
Ulf Hansson | 3aa8793 | 2014-11-28 14:38:36 +0100 | [diff] [blame] | 3303 | mmc_pwrseq_power_off(host); |
| 3304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | host->ios.clock = 0; |
| 3306 | host->ios.vdd = 0; |
Ulf Hansson | b33d46c | 2011-03-05 14:36:24 +0100 | [diff] [blame] | 3307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | host->ios.power_mode = MMC_POWER_OFF; |
Johan Rudholm | 2d079c4 | 2014-11-06 14:46:54 +0100 | [diff] [blame] | 3309 | /* Set initial state and call mmc_set_ios */ |
| 3310 | mmc_set_initial_state(host); |
Mika Westerberg | 778e277 | 2011-08-18 15:23:48 +0300 | [diff] [blame] | 3311 | |
Daniel Drake | 041beb1 | 2011-09-07 10:22:09 +0100 | [diff] [blame] | 3312 | /* |
| 3313 | * Some configurations, such as the 802.11 SDIO card in the OLPC |
| 3314 | * XO-1.5, require a short delay after poweroff before the card |
| 3315 | * can be successfully turned on again. |
| 3316 | */ |
| 3317 | mmc_delay(1); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3318 | |
| 3319 | mmc_host_clk_release(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | } |
| 3321 | |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 3322 | void mmc_power_cycle(struct mmc_host *host, u32 ocr) |
Johan Rudholm | 276e090 | 2013-01-28 15:08:25 +0100 | [diff] [blame] | 3323 | { |
| 3324 | mmc_power_off(host); |
| 3325 | /* Wait at least 1 ms according to SD spec */ |
| 3326 | mmc_delay(1); |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 3327 | mmc_power_up(host, ocr); |
Johan Rudholm | 276e090 | 2013-01-28 15:08:25 +0100 | [diff] [blame] | 3328 | } |
| 3329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | /* |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 3331 | * Cleanup when the last reference to the bus operator is dropped. |
| 3332 | */ |
Adrian Bunk | 261172f | 2008-04-13 21:15:47 +0300 | [diff] [blame] | 3333 | static void __mmc_release_bus(struct mmc_host *host) |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 3334 | { |
| 3335 | BUG_ON(!host); |
| 3336 | BUG_ON(host->bus_refs); |
| 3337 | BUG_ON(!host->bus_dead); |
| 3338 | |
| 3339 | host->bus_ops = NULL; |
| 3340 | } |
| 3341 | |
| 3342 | /* |
| 3343 | * Increase reference count of bus operator |
| 3344 | */ |
| 3345 | static inline void mmc_bus_get(struct mmc_host *host) |
| 3346 | { |
| 3347 | unsigned long flags; |
| 3348 | |
| 3349 | spin_lock_irqsave(&host->lock, flags); |
| 3350 | host->bus_refs++; |
| 3351 | spin_unlock_irqrestore(&host->lock, flags); |
| 3352 | } |
| 3353 | |
| 3354 | /* |
| 3355 | * Decrease reference count of bus operator and free it if |
| 3356 | * it is the last reference. |
| 3357 | */ |
| 3358 | static inline void mmc_bus_put(struct mmc_host *host) |
| 3359 | { |
| 3360 | unsigned long flags; |
| 3361 | |
| 3362 | spin_lock_irqsave(&host->lock, flags); |
| 3363 | host->bus_refs--; |
| 3364 | if ((host->bus_refs == 0) && host->bus_ops) |
| 3365 | __mmc_release_bus(host); |
| 3366 | spin_unlock_irqrestore(&host->lock, flags); |
| 3367 | } |
| 3368 | |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3369 | int mmc_resume_bus(struct mmc_host *host) |
| 3370 | { |
Dmitry Shmidt | 3ad1837 | 2010-10-14 15:20:21 -0700 | [diff] [blame] | 3371 | unsigned long flags; |
Asutosh Das | b77f57b | 2016-04-12 11:52:33 +0530 | [diff] [blame] | 3372 | int err = 0; |
Dmitry Shmidt | 3ad1837 | 2010-10-14 15:20:21 -0700 | [diff] [blame] | 3373 | |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3374 | if (!mmc_bus_needs_resume(host)) |
| 3375 | return -EINVAL; |
| 3376 | |
| 3377 | pr_debug("%s: Starting deferred resume\n", mmc_hostname(host)); |
Dmitry Shmidt | 3ad1837 | 2010-10-14 15:20:21 -0700 | [diff] [blame] | 3378 | spin_lock_irqsave(&host->lock, flags); |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3379 | host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME; |
Dmitry Shmidt | 3ad1837 | 2010-10-14 15:20:21 -0700 | [diff] [blame] | 3380 | spin_unlock_irqrestore(&host->lock, flags); |
| 3381 | |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3382 | mmc_bus_get(host); |
| 3383 | if (host->bus_ops && !host->bus_dead && host->card) { |
| 3384 | mmc_power_up(host, host->card->ocr); |
| 3385 | BUG_ON(!host->bus_ops->resume); |
| 3386 | host->bus_ops->resume(host); |
Asutosh Das | b77f57b | 2016-04-12 11:52:33 +0530 | [diff] [blame] | 3387 | if (mmc_card_cmdq(host->card)) { |
| 3388 | err = mmc_cmdq_halt(host, false); |
| 3389 | if (err) |
| 3390 | pr_err("%s: %s: unhalt failed: %d\n", |
| 3391 | mmc_hostname(host), __func__, err); |
| 3392 | else |
| 3393 | mmc_card_clr_suspended(host->card); |
| 3394 | } |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3395 | } |
| 3396 | |
San Mehat | faa9848 | 2009-07-30 07:55:28 -0700 | [diff] [blame] | 3397 | mmc_bus_put(host); |
| 3398 | pr_debug("%s: Deferred resume completed\n", mmc_hostname(host)); |
| 3399 | return 0; |
| 3400 | } |
| 3401 | EXPORT_SYMBOL(mmc_resume_bus); |
| 3402 | |
Adrian Bunk | 3936185 | 2007-07-25 00:40:58 +0200 | [diff] [blame] | 3403 | /* |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3404 | * Assign a mmc bus handler to a host. Only one bus handler may control a |
| 3405 | * host at any given time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3407 | 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] | 3408 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3409 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3411 | BUG_ON(!host); |
| 3412 | BUG_ON(!ops); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 3413 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 3414 | WARN_ON(!host->claimed); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 3415 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3416 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 3417 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3418 | BUG_ON(host->bus_ops); |
| 3419 | BUG_ON(host->bus_refs); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 3420 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3421 | host->bus_ops = ops; |
| 3422 | host->bus_refs = 1; |
| 3423 | host->bus_dead = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3425 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | } |
| 3427 | |
| 3428 | /* |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 3429 | * Remove the current bus handler from a host. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3430 | */ |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3431 | void mmc_detach_bus(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3432 | { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3433 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3434 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3435 | BUG_ON(!host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3436 | |
Pierre Ossman | d84075c8 | 2007-08-09 13:23:56 +0200 | [diff] [blame] | 3437 | WARN_ON(!host->claimed); |
| 3438 | WARN_ON(!host->bus_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3439 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3440 | spin_lock_irqsave(&host->lock, flags); |
| 3441 | |
| 3442 | host->bus_dead = 1; |
| 3443 | |
| 3444 | spin_unlock_irqrestore(&host->lock, flags); |
| 3445 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 3446 | mmc_bus_put(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | } |
| 3448 | |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 3449 | static void _mmc_detect_change(struct mmc_host *host, unsigned long delay, |
| 3450 | bool cd_irq) |
| 3451 | { |
| 3452 | #ifdef CONFIG_MMC_DEBUG |
| 3453 | unsigned long flags; |
| 3454 | spin_lock_irqsave(&host->lock, flags); |
| 3455 | WARN_ON(host->removed); |
| 3456 | spin_unlock_irqrestore(&host->lock, flags); |
| 3457 | #endif |
| 3458 | |
| 3459 | /* |
| 3460 | * If the device is configured as wakeup, we prevent a new sleep for |
| 3461 | * 5 s to give provision for user space to consume the event. |
| 3462 | */ |
| 3463 | if (cd_irq && !(host->caps & MMC_CAP_NEEDS_POLL) && |
| 3464 | device_can_wakeup(mmc_dev(host))) |
| 3465 | pm_wakeup_event(mmc_dev(host), 5000); |
| 3466 | |
| 3467 | host->detect_change = 1; |
Veerabhadrarao Badiganti | cdf0d57 | 2017-08-07 14:58:15 +0530 | [diff] [blame] | 3468 | /* |
| 3469 | * Change in cd_gpio state, so make sure detection part is |
| 3470 | * not overided because of manual resume. |
| 3471 | */ |
| 3472 | if (cd_irq && mmc_bus_manual_resume(host)) |
| 3473 | host->ignore_bus_resume_flags = true; |
| 3474 | |
Can Guo | 5ebdb44 | 2017-11-04 14:08:30 +0800 | [diff] [blame] | 3475 | if (delayed_work_pending(&host->detect)) |
| 3476 | cancel_delayed_work(&host->detect); |
| 3477 | |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 3478 | mmc_schedule_delayed_work(&host->detect, delay); |
| 3479 | } |
| 3480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | /** |
| 3482 | * mmc_detect_change - process change of state on a MMC socket |
| 3483 | * @host: host which changed state. |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 3484 | * @delay: optional delay to wait before detection (jiffies) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3485 | * |
Pierre Ossman | 67a61c4 | 2007-07-11 20:22:11 +0200 | [diff] [blame] | 3486 | * MMC drivers should call this when they detect a card has been |
| 3487 | * inserted or removed. The MMC layer will confirm that any |
| 3488 | * present card is still functional, and initialize any newly |
| 3489 | * inserted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | */ |
Richard Purdie | 8dc0033 | 2005-09-08 17:53:01 +0100 | [diff] [blame] | 3491 | void mmc_detect_change(struct mmc_host *host, unsigned long delay) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3492 | { |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 3493 | _mmc_detect_change(host, delay, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3495 | EXPORT_SYMBOL(mmc_detect_change); |
| 3496 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3497 | void mmc_init_erase(struct mmc_card *card) |
| 3498 | { |
| 3499 | unsigned int sz; |
| 3500 | |
| 3501 | if (is_power_of_2(card->erase_size)) |
| 3502 | card->erase_shift = ffs(card->erase_size) - 1; |
| 3503 | else |
| 3504 | card->erase_shift = 0; |
| 3505 | |
| 3506 | /* |
| 3507 | * It is possible to erase an arbitrarily large area of an SD or MMC |
| 3508 | * card. That is not desirable because it can take a long time |
| 3509 | * (minutes) potentially delaying more important I/O, and also the |
| 3510 | * timeout calculations become increasingly hugely over-estimated. |
| 3511 | * Consequently, 'pref_erase' is defined as a guide to limit erases |
| 3512 | * to that size and alignment. |
| 3513 | * |
| 3514 | * For SD cards that define Allocation Unit size, limit erases to one |
Gwendal Grignou | c6d8fd6 | 2016-06-03 09:08:52 -0700 | [diff] [blame] | 3515 | * Allocation Unit at a time. |
| 3516 | * For MMC, have a stab at ai good value and for modern cards it will |
| 3517 | * end up being 4MiB. Note that if the value is too small, it can end |
| 3518 | * up taking longer to erase. Also note, erase_size is already set to |
| 3519 | * High Capacity Erase Size if available when this function is called. |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3520 | */ |
| 3521 | if (mmc_card_sd(card) && card->ssr.au) { |
| 3522 | card->pref_erase = card->ssr.au; |
| 3523 | card->erase_shift = ffs(card->ssr.au) - 1; |
Chuanxiao Dong | cc8aa7d | 2014-08-14 18:29:24 +0800 | [diff] [blame] | 3524 | } else if (card->erase_size) { |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3525 | sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; |
| 3526 | if (sz < 128) |
| 3527 | card->pref_erase = 512 * 1024 / 512; |
| 3528 | else if (sz < 512) |
| 3529 | card->pref_erase = 1024 * 1024 / 512; |
| 3530 | else if (sz < 1024) |
| 3531 | card->pref_erase = 2 * 1024 * 1024 / 512; |
| 3532 | else |
| 3533 | card->pref_erase = 4 * 1024 * 1024 / 512; |
| 3534 | if (card->pref_erase < card->erase_size) |
| 3535 | card->pref_erase = card->erase_size; |
| 3536 | else { |
| 3537 | sz = card->pref_erase % card->erase_size; |
| 3538 | if (sz) |
| 3539 | card->pref_erase += card->erase_size - sz; |
| 3540 | } |
Chuanxiao Dong | cc8aa7d | 2014-08-14 18:29:24 +0800 | [diff] [blame] | 3541 | } else |
| 3542 | card->pref_erase = 0; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3543 | } |
| 3544 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3545 | static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card, |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 3546 | unsigned int arg, unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3547 | { |
| 3548 | unsigned int erase_timeout; |
| 3549 | |
Adrian Hunter | 7194efb | 2012-04-05 14:45:47 +0300 | [diff] [blame] | 3550 | if (arg == MMC_DISCARD_ARG || |
| 3551 | (arg == MMC_TRIM_ARG && card->ext_csd.rev >= 6)) { |
| 3552 | erase_timeout = card->ext_csd.trim_timeout; |
| 3553 | } else if (card->ext_csd.erase_group_def & 1) { |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3554 | /* High Capacity Erase Group Size uses HC timeouts */ |
| 3555 | if (arg == MMC_TRIM_ARG) |
| 3556 | erase_timeout = card->ext_csd.trim_timeout; |
| 3557 | else |
| 3558 | erase_timeout = card->ext_csd.hc_erase_timeout; |
| 3559 | } else { |
| 3560 | /* CSD Erase Group Size uses write timeout */ |
| 3561 | unsigned int mult = (10 << card->csd.r2w_factor); |
| 3562 | unsigned int timeout_clks = card->csd.tacc_clks * mult; |
| 3563 | unsigned int timeout_us; |
| 3564 | |
| 3565 | /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ |
| 3566 | if (card->csd.tacc_ns < 1000000) |
| 3567 | timeout_us = (card->csd.tacc_ns * mult) / 1000; |
| 3568 | else |
| 3569 | timeout_us = (card->csd.tacc_ns / 1000) * mult; |
| 3570 | |
| 3571 | /* |
| 3572 | * ios.clock is only a target. The real clock rate might be |
| 3573 | * less but not that much less, so fudge it by multiplying by 2. |
| 3574 | */ |
| 3575 | timeout_clks <<= 1; |
| 3576 | timeout_us += (timeout_clks * 1000) / |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 3577 | (mmc_host_clk_rate(card->host) / 1000); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3578 | |
| 3579 | erase_timeout = timeout_us / 1000; |
| 3580 | |
| 3581 | /* |
| 3582 | * Theoretically, the calculation could underflow so round up |
| 3583 | * to 1ms in that case. |
| 3584 | */ |
| 3585 | if (!erase_timeout) |
| 3586 | erase_timeout = 1; |
| 3587 | } |
| 3588 | |
| 3589 | /* Multiplier for secure operations */ |
| 3590 | if (arg & MMC_SECURE_ARGS) { |
| 3591 | if (arg == MMC_SECURE_ERASE_ARG) |
| 3592 | erase_timeout *= card->ext_csd.sec_erase_mult; |
| 3593 | else |
| 3594 | erase_timeout *= card->ext_csd.sec_trim_mult; |
| 3595 | } |
| 3596 | |
| 3597 | erase_timeout *= qty; |
| 3598 | |
| 3599 | /* |
| 3600 | * Ensure at least a 1 second timeout for SPI as per |
| 3601 | * 'mmc_set_data_timeout()' |
| 3602 | */ |
| 3603 | if (mmc_host_is_spi(card->host) && erase_timeout < 1000) |
| 3604 | erase_timeout = 1000; |
| 3605 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3606 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3607 | } |
| 3608 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3609 | static unsigned int mmc_sd_erase_timeout(struct mmc_card *card, |
| 3610 | unsigned int arg, |
| 3611 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3612 | { |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3613 | unsigned int erase_timeout; |
| 3614 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3615 | if (card->ssr.erase_timeout) { |
| 3616 | /* Erase timeout specified in SD Status Register (SSR) */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3617 | erase_timeout = card->ssr.erase_timeout * qty + |
| 3618 | card->ssr.erase_offset; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3619 | } else { |
| 3620 | /* |
| 3621 | * Erase timeout not specified in SD Status Register (SSR) so |
| 3622 | * use 250ms per write block. |
| 3623 | */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3624 | erase_timeout = 250 * qty; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3625 | } |
| 3626 | |
| 3627 | /* Must not be less than 1 second */ |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3628 | if (erase_timeout < 1000) |
| 3629 | erase_timeout = 1000; |
| 3630 | |
| 3631 | return erase_timeout; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3632 | } |
| 3633 | |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3634 | static unsigned int mmc_erase_timeout(struct mmc_card *card, |
| 3635 | unsigned int arg, |
| 3636 | unsigned int qty) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3637 | { |
| 3638 | if (mmc_card_sd(card)) |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3639 | return mmc_sd_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3640 | else |
Andrei Warkentin | eaa02f7 | 2011-04-11 16:13:41 -0500 | [diff] [blame] | 3641 | return mmc_mmc_erase_timeout(card, arg, qty); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3642 | } |
| 3643 | |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3644 | static u32 mmc_get_erase_qty(struct mmc_card *card, u32 from, u32 to) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3645 | { |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3646 | u32 qty = 0; |
Adrian Hunter | 8f11d10 | 2015-05-07 13:10:16 +0300 | [diff] [blame] | 3647 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3648 | /* |
| 3649 | * qty is used to calculate the erase timeout which depends on how many |
| 3650 | * erase groups (or allocation units in SD terminology) are affected. |
| 3651 | * We count erasing part of an erase group as one erase group. |
| 3652 | * For SD, the allocation units are always a power of 2. For MMC, the |
| 3653 | * erase group size is almost certainly also power of 2, but it does not |
| 3654 | * seem to insist on that in the JEDEC standard, so we fall back to |
| 3655 | * division in that case. SD may not specify an allocation unit size, |
| 3656 | * in which case the timeout is based on the number of write blocks. |
| 3657 | * |
| 3658 | * Note that the timeout for secure trim 2 will only be correct if the |
| 3659 | * number of erase groups specified is the same as the total of all |
| 3660 | * preceding secure trim 1 commands. Since the power may have been |
| 3661 | * lost since the secure trim 1 commands occurred, it is generally |
| 3662 | * impossible to calculate the secure trim 2 timeout correctly. |
| 3663 | */ |
| 3664 | if (card->erase_shift) |
| 3665 | qty += ((to >> card->erase_shift) - |
| 3666 | (from >> card->erase_shift)) + 1; |
| 3667 | else if (mmc_card_sd(card)) |
| 3668 | qty += to - from + 1; |
| 3669 | else |
| 3670 | qty += ((to / card->erase_size) - |
| 3671 | (from / card->erase_size)) + 1; |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3672 | return qty; |
| 3673 | } |
| 3674 | |
| 3675 | static int mmc_cmdq_send_erase_cmd(struct mmc_cmdq_req *cmdq_req, |
| 3676 | struct mmc_card *card, u32 opcode, u32 arg, u32 qty) |
| 3677 | { |
| 3678 | struct mmc_command *cmd = cmdq_req->mrq.cmd; |
| 3679 | int err; |
| 3680 | |
| 3681 | memset(cmd, 0, sizeof(struct mmc_command)); |
| 3682 | |
| 3683 | cmd->opcode = opcode; |
| 3684 | cmd->arg = arg; |
| 3685 | if (cmd->opcode == MMC_ERASE) { |
| 3686 | cmd->flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 3687 | cmd->busy_timeout = mmc_erase_timeout(card, arg, qty); |
| 3688 | } else { |
| 3689 | cmd->flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 3690 | } |
| 3691 | |
| 3692 | err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req); |
| 3693 | if (err) { |
| 3694 | pr_err("mmc_erase: group start error %d, status %#x\n", |
| 3695 | err, cmd->resp[0]); |
| 3696 | return -EIO; |
| 3697 | } |
| 3698 | return 0; |
| 3699 | } |
| 3700 | |
| 3701 | static int mmc_cmdq_do_erase(struct mmc_cmdq_req *cmdq_req, |
| 3702 | struct mmc_card *card, unsigned int from, |
| 3703 | unsigned int to, unsigned int arg) |
| 3704 | { |
| 3705 | struct mmc_command *cmd = cmdq_req->mrq.cmd; |
| 3706 | unsigned int qty = 0; |
| 3707 | unsigned long timeout; |
| 3708 | unsigned int fr, nr; |
| 3709 | int err; |
| 3710 | |
| 3711 | fr = from; |
| 3712 | nr = to - from + 1; |
| 3713 | |
| 3714 | qty = mmc_get_erase_qty(card, from, to); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3715 | |
| 3716 | if (!mmc_card_blockaddr(card)) { |
| 3717 | from <<= 9; |
| 3718 | to <<= 9; |
| 3719 | } |
| 3720 | |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3721 | err = mmc_cmdq_send_erase_cmd(cmdq_req, card, MMC_ERASE_GROUP_START, |
| 3722 | from, qty); |
| 3723 | if (err) |
| 3724 | goto out; |
| 3725 | |
| 3726 | err = mmc_cmdq_send_erase_cmd(cmdq_req, card, MMC_ERASE_GROUP_END, |
| 3727 | to, qty); |
| 3728 | if (err) |
| 3729 | goto out; |
| 3730 | |
| 3731 | err = mmc_cmdq_send_erase_cmd(cmdq_req, card, MMC_ERASE, |
| 3732 | arg, qty); |
| 3733 | if (err) |
| 3734 | goto out; |
| 3735 | |
| 3736 | timeout = jiffies + msecs_to_jiffies(MMC_CORE_TIMEOUT_MS); |
| 3737 | do { |
| 3738 | memset(cmd, 0, sizeof(struct mmc_command)); |
| 3739 | cmd->opcode = MMC_SEND_STATUS; |
| 3740 | cmd->arg = card->rca << 16; |
| 3741 | cmd->flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 3742 | /* Do not retry else we can't see errors */ |
| 3743 | err = mmc_cmdq_wait_for_dcmd(card->host, cmdq_req); |
| 3744 | if (err || (cmd->resp[0] & 0xFDF92000)) { |
| 3745 | pr_err("error %d requesting status %#x\n", |
| 3746 | err, cmd->resp[0]); |
| 3747 | err = -EIO; |
| 3748 | goto out; |
| 3749 | } |
| 3750 | /* Timeout if the device never becomes ready for data and |
| 3751 | * never leaves the program state. |
| 3752 | */ |
| 3753 | if (time_after(jiffies, timeout)) { |
| 3754 | pr_err("%s: Card stuck in programming state! %s\n", |
| 3755 | mmc_hostname(card->host), __func__); |
| 3756 | err = -EIO; |
| 3757 | goto out; |
| 3758 | } |
| 3759 | } while (!(cmd->resp[0] & R1_READY_FOR_DATA) || |
| 3760 | (R1_CURRENT_STATE(cmd->resp[0]) == R1_STATE_PRG)); |
| 3761 | out: |
| 3762 | return err; |
| 3763 | } |
| 3764 | |
| 3765 | static int mmc_do_erase(struct mmc_card *card, unsigned int from, |
| 3766 | unsigned int to, unsigned int arg) |
| 3767 | { |
| 3768 | struct mmc_command cmd = {0}; |
| 3769 | unsigned int qty = 0, busy_timeout = 0; |
| 3770 | bool use_r1b_resp = false; |
| 3771 | unsigned long timeout; |
| 3772 | unsigned int fr, nr; |
| 3773 | int err; |
| 3774 | |
| 3775 | fr = from; |
| 3776 | nr = to - from + 1; |
| 3777 | |
| 3778 | qty = mmc_get_erase_qty(card, from, to); |
| 3779 | |
| 3780 | if (!mmc_card_blockaddr(card)) { |
| 3781 | from <<= 9; |
| 3782 | to <<= 9; |
| 3783 | } |
| 3784 | |
| 3785 | mmc_retune_hold(card->host); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3786 | if (mmc_card_sd(card)) |
| 3787 | cmd.opcode = SD_ERASE_WR_BLK_START; |
| 3788 | else |
| 3789 | cmd.opcode = MMC_ERASE_GROUP_START; |
| 3790 | cmd.arg = from; |
| 3791 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 3792 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 3793 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3794 | pr_err("mmc_erase: group start error %d, " |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3795 | "status %#x\n", err, cmd.resp[0]); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 3796 | err = -EIO; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3797 | goto out; |
| 3798 | } |
| 3799 | |
| 3800 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 3801 | if (mmc_card_sd(card)) |
| 3802 | cmd.opcode = SD_ERASE_WR_BLK_END; |
| 3803 | else |
| 3804 | cmd.opcode = MMC_ERASE_GROUP_END; |
| 3805 | cmd.arg = to; |
| 3806 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 3807 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 3808 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3809 | pr_err("mmc_erase: group end error %d, status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3810 | err, cmd.resp[0]); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 3811 | err = -EIO; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3812 | goto out; |
| 3813 | } |
| 3814 | |
| 3815 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 3816 | cmd.opcode = MMC_ERASE; |
| 3817 | cmd.arg = arg; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 3818 | busy_timeout = mmc_erase_timeout(card, arg, qty); |
| 3819 | /* |
| 3820 | * If the host controller supports busy signalling and the timeout for |
| 3821 | * the erase operation does not exceed the max_busy_timeout, we should |
| 3822 | * use R1B response. Or we need to prevent the host from doing hw busy |
| 3823 | * detection, which is done by converting to a R1 response instead. |
| 3824 | */ |
| 3825 | if (card->host->max_busy_timeout && |
| 3826 | busy_timeout > card->host->max_busy_timeout) { |
| 3827 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 3828 | } else { |
| 3829 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 3830 | cmd.busy_timeout = busy_timeout; |
| 3831 | use_r1b_resp = true; |
| 3832 | } |
| 3833 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3834 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 3835 | if (err) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3836 | pr_err("mmc_erase: erase error %d, status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3837 | err, cmd.resp[0]); |
| 3838 | err = -EIO; |
| 3839 | goto out; |
| 3840 | } |
| 3841 | |
| 3842 | if (mmc_host_is_spi(card->host)) |
| 3843 | goto out; |
| 3844 | |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 3845 | /* |
| 3846 | * In case of when R1B + MMC_CAP_WAIT_WHILE_BUSY is used, the polling |
| 3847 | * shall be avoided. |
| 3848 | */ |
| 3849 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && use_r1b_resp) |
| 3850 | goto out; |
| 3851 | |
| 3852 | timeout = jiffies + msecs_to_jiffies(busy_timeout); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3853 | do { |
| 3854 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 3855 | cmd.opcode = MMC_SEND_STATUS; |
| 3856 | cmd.arg = card->rca << 16; |
| 3857 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 3858 | /* Do not retry else we can't see errors */ |
| 3859 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 3860 | if (err || (cmd.resp[0] & 0xFDF92000)) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3861 | pr_err("error %d requesting status %#x\n", |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3862 | err, cmd.resp[0]); |
| 3863 | err = -EIO; |
| 3864 | goto out; |
| 3865 | } |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 3866 | |
| 3867 | /* Timeout if the device never becomes ready for data and |
| 3868 | * never leaves the program state. |
| 3869 | */ |
| 3870 | if (time_after(jiffies, timeout)) { |
| 3871 | pr_err("%s: Card stuck in programming state! %s\n", |
| 3872 | mmc_hostname(card->host), __func__); |
| 3873 | err = -EIO; |
| 3874 | goto out; |
| 3875 | } |
| 3876 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3877 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 3878 | (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG)); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3879 | out: |
Adrian Hunter | 8f11d10 | 2015-05-07 13:10:16 +0300 | [diff] [blame] | 3880 | mmc_retune_release(card->host); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3881 | return err; |
| 3882 | } |
| 3883 | |
Baolin Wang | 7108512 | 2016-09-07 10:38:24 +0800 | [diff] [blame] | 3884 | static unsigned int mmc_align_erase_size(struct mmc_card *card, |
| 3885 | unsigned int *from, |
| 3886 | unsigned int *to, |
| 3887 | unsigned int nr) |
| 3888 | { |
| 3889 | unsigned int from_new = *from, nr_new = nr, rem; |
| 3890 | |
Baolin Wang | 6c68988 | 2016-09-07 10:38:25 +0800 | [diff] [blame] | 3891 | /* |
| 3892 | * When the 'card->erase_size' is power of 2, we can use round_up/down() |
| 3893 | * to align the erase size efficiently. |
| 3894 | */ |
| 3895 | if (is_power_of_2(card->erase_size)) { |
| 3896 | unsigned int temp = from_new; |
| 3897 | |
| 3898 | from_new = round_up(temp, card->erase_size); |
| 3899 | rem = from_new - temp; |
| 3900 | |
Baolin Wang | 7108512 | 2016-09-07 10:38:24 +0800 | [diff] [blame] | 3901 | if (nr_new > rem) |
| 3902 | nr_new -= rem; |
| 3903 | else |
| 3904 | return 0; |
Baolin Wang | 7108512 | 2016-09-07 10:38:24 +0800 | [diff] [blame] | 3905 | |
Baolin Wang | 6c68988 | 2016-09-07 10:38:25 +0800 | [diff] [blame] | 3906 | nr_new = round_down(nr_new, card->erase_size); |
| 3907 | } else { |
| 3908 | rem = from_new % card->erase_size; |
| 3909 | if (rem) { |
| 3910 | rem = card->erase_size - rem; |
| 3911 | from_new += rem; |
| 3912 | if (nr_new > rem) |
| 3913 | nr_new -= rem; |
| 3914 | else |
| 3915 | return 0; |
| 3916 | } |
| 3917 | |
| 3918 | rem = nr_new % card->erase_size; |
| 3919 | if (rem) |
| 3920 | nr_new -= rem; |
| 3921 | } |
Baolin Wang | 7108512 | 2016-09-07 10:38:24 +0800 | [diff] [blame] | 3922 | |
| 3923 | if (nr_new == 0) |
| 3924 | return 0; |
| 3925 | |
| 3926 | *to = from_new + nr_new; |
| 3927 | *from = from_new; |
| 3928 | |
| 3929 | return nr_new; |
| 3930 | } |
| 3931 | |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3932 | int mmc_erase_sanity_check(struct mmc_card *card, unsigned int from, |
| 3933 | unsigned int nr, unsigned int arg) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3934 | { |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 3935 | |
| 3936 | if (!(card->host->caps & MMC_CAP_ERASE) || |
| 3937 | !(card->csd.cmdclass & CCC_ERASE)) |
| 3938 | return -EOPNOTSUPP; |
| 3939 | |
| 3940 | if (!card->erase_size) |
| 3941 | return -EOPNOTSUPP; |
| 3942 | |
| 3943 | if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) |
| 3944 | return -EOPNOTSUPP; |
| 3945 | |
| 3946 | if ((arg & MMC_SECURE_ARGS) && |
| 3947 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) |
| 3948 | return -EOPNOTSUPP; |
| 3949 | |
| 3950 | if ((arg & MMC_TRIM_ARGS) && |
| 3951 | !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) |
| 3952 | return -EOPNOTSUPP; |
| 3953 | |
| 3954 | if (arg == MMC_SECURE_ERASE_ARG) { |
| 3955 | if (from % card->erase_size || nr % card->erase_size) |
| 3956 | return -EINVAL; |
| 3957 | } |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 3958 | return 0; |
| 3959 | } |
| 3960 | |
| 3961 | int mmc_cmdq_erase(struct mmc_cmdq_req *cmdq_req, |
| 3962 | struct mmc_card *card, unsigned int from, unsigned int nr, |
| 3963 | unsigned int arg) |
| 3964 | { |
| 3965 | unsigned int rem, to = from + nr; |
| 3966 | int ret; |
| 3967 | |
| 3968 | ret = mmc_erase_sanity_check(card, from, nr, arg); |
| 3969 | if (ret) |
| 3970 | return ret; |
| 3971 | |
| 3972 | if (arg == MMC_ERASE_ARG) { |
| 3973 | rem = from % card->erase_size; |
| 3974 | if (rem) { |
| 3975 | rem = card->erase_size - rem; |
| 3976 | from += rem; |
| 3977 | if (nr > rem) |
| 3978 | nr -= rem; |
| 3979 | else |
| 3980 | return 0; |
| 3981 | } |
| 3982 | rem = nr % card->erase_size; |
| 3983 | if (rem) |
| 3984 | nr -= rem; |
| 3985 | } |
| 3986 | |
| 3987 | if (nr == 0) |
| 3988 | return 0; |
| 3989 | |
| 3990 | to = from + nr; |
| 3991 | |
| 3992 | if (to <= from) |
| 3993 | return -EINVAL; |
| 3994 | |
| 3995 | /* 'from' and 'to' are inclusive */ |
| 3996 | to -= 1; |
| 3997 | |
| 3998 | return mmc_cmdq_do_erase(cmdq_req, card, from, to, arg); |
| 3999 | } |
| 4000 | EXPORT_SYMBOL(mmc_cmdq_erase); |
| 4001 | |
| 4002 | /** |
| 4003 | * mmc_erase - erase sectors. |
| 4004 | * @card: card to erase |
| 4005 | * @from: first sector to erase |
| 4006 | * @nr: number of sectors to erase |
| 4007 | * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) |
| 4008 | * |
| 4009 | * Caller must claim host before calling this function. |
| 4010 | */ |
| 4011 | int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, |
| 4012 | unsigned int arg) |
| 4013 | { |
| 4014 | unsigned int rem, to = from + nr; |
| 4015 | int ret; |
| 4016 | |
| 4017 | ret = mmc_erase_sanity_check(card, from, nr, arg); |
| 4018 | if (ret) |
| 4019 | return ret; |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4020 | |
Baolin Wang | 7108512 | 2016-09-07 10:38:24 +0800 | [diff] [blame] | 4021 | if (arg == MMC_ERASE_ARG) |
| 4022 | nr = mmc_align_erase_size(card, &from, &to, nr); |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4023 | |
| 4024 | if (nr == 0) |
| 4025 | return 0; |
| 4026 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4027 | if (to <= from) |
| 4028 | return -EINVAL; |
| 4029 | |
| 4030 | /* 'from' and 'to' are inclusive */ |
| 4031 | to -= 1; |
| 4032 | |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4033 | /* |
| 4034 | * Special case where only one erase-group fits in the timeout budget: |
| 4035 | * If the region crosses an erase-group boundary on this particular |
| 4036 | * case, we will be trimming more than one erase-group which, does not |
| 4037 | * fit in the timeout budget of the controller, so we need to split it |
| 4038 | * and call mmc_do_erase() twice if necessary. This special case is |
| 4039 | * identified by the card->eg_boundary flag. |
| 4040 | */ |
Robin van der Gracht | 22d7e85 | 2015-08-04 08:58:33 +0200 | [diff] [blame] | 4041 | rem = card->erase_size - (from % card->erase_size); |
| 4042 | if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) { |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 4043 | ret = mmc_do_erase(card, from, from + rem - 1, arg); |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4044 | from += rem; |
Sahitya Tummala | 9433a13 | 2015-06-09 09:38:36 +0530 | [diff] [blame] | 4045 | if ((ret) || (to <= from)) |
| 4046 | return ret; |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4047 | } |
| 4048 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4049 | return mmc_do_erase(card, from, to, arg); |
| 4050 | } |
| 4051 | EXPORT_SYMBOL(mmc_erase); |
| 4052 | |
| 4053 | int mmc_can_erase(struct mmc_card *card) |
| 4054 | { |
| 4055 | if ((card->host->caps & MMC_CAP_ERASE) && |
| 4056 | (card->csd.cmdclass & CCC_ERASE) && card->erase_size) |
| 4057 | return 1; |
| 4058 | return 0; |
| 4059 | } |
| 4060 | EXPORT_SYMBOL(mmc_can_erase); |
| 4061 | |
| 4062 | int mmc_can_trim(struct mmc_card *card) |
| 4063 | { |
Shawn Lin | b5b4ff0 | 2015-08-12 13:08:32 +0800 | [diff] [blame] | 4064 | if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) && |
| 4065 | (!(card->quirks & MMC_QUIRK_TRIM_BROKEN))) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4066 | return 1; |
| 4067 | return 0; |
| 4068 | } |
| 4069 | EXPORT_SYMBOL(mmc_can_trim); |
| 4070 | |
Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 4071 | int mmc_can_discard(struct mmc_card *card) |
| 4072 | { |
| 4073 | /* |
| 4074 | * As there's no way to detect the discard support bit at v4.5 |
| 4075 | * use the s/w feature support filed. |
| 4076 | */ |
| 4077 | if (card->ext_csd.feature_support & MMC_DISCARD_FEATURE) |
| 4078 | return 1; |
| 4079 | return 0; |
| 4080 | } |
| 4081 | EXPORT_SYMBOL(mmc_can_discard); |
| 4082 | |
Kyungmin Park | d9ddd62 | 2011-10-14 14:15:48 +0900 | [diff] [blame] | 4083 | int mmc_can_sanitize(struct mmc_card *card) |
| 4084 | { |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 4085 | if (!mmc_can_trim(card) && !mmc_can_erase(card)) |
| 4086 | return 0; |
Kyungmin Park | d9ddd62 | 2011-10-14 14:15:48 +0900 | [diff] [blame] | 4087 | if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE) |
| 4088 | return 1; |
| 4089 | return 0; |
| 4090 | } |
| 4091 | EXPORT_SYMBOL(mmc_can_sanitize); |
| 4092 | |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4093 | int mmc_can_secure_erase_trim(struct mmc_card *card) |
| 4094 | { |
Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 4095 | if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) && |
| 4096 | !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN)) |
Adrian Hunter | dfe86cb | 2010-08-11 14:17:46 -0700 | [diff] [blame] | 4097 | return 1; |
| 4098 | return 0; |
| 4099 | } |
| 4100 | EXPORT_SYMBOL(mmc_can_secure_erase_trim); |
| 4101 | |
| 4102 | int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, |
| 4103 | unsigned int nr) |
| 4104 | { |
| 4105 | if (!card->erase_size) |
| 4106 | return 0; |
| 4107 | if (from % card->erase_size || nr % card->erase_size) |
| 4108 | return 0; |
| 4109 | return 1; |
| 4110 | } |
| 4111 | EXPORT_SYMBOL(mmc_erase_group_aligned); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4112 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4113 | static unsigned int mmc_do_calc_max_discard(struct mmc_card *card, |
| 4114 | unsigned int arg) |
| 4115 | { |
| 4116 | struct mmc_host *host = card->host; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4117 | unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4118 | unsigned int last_timeout = 0; |
Ulf Hansson | 12182af | 2016-07-27 00:04:03 +0200 | [diff] [blame] | 4119 | unsigned int max_busy_timeout = host->max_busy_timeout ? |
| 4120 | host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4121 | |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4122 | if (card->erase_shift) { |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4123 | max_qty = UINT_MAX >> card->erase_shift; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4124 | min_qty = card->pref_erase >> card->erase_shift; |
| 4125 | } else if (mmc_card_sd(card)) { |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4126 | max_qty = UINT_MAX; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4127 | min_qty = card->pref_erase; |
| 4128 | } else { |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4129 | max_qty = UINT_MAX / card->erase_size; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4130 | min_qty = card->pref_erase / card->erase_size; |
| 4131 | } |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4132 | |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4133 | /* |
| 4134 | * We should not only use 'host->max_busy_timeout' as the limitation |
| 4135 | * when deciding the max discard sectors. We should set a balance value |
| 4136 | * to improve the erase speed, and it can not get too long timeout at |
| 4137 | * the same time. |
| 4138 | * |
| 4139 | * Here we set 'card->pref_erase' as the minimal discard sectors no |
| 4140 | * matter what size of 'host->max_busy_timeout', but if the |
| 4141 | * 'host->max_busy_timeout' is large enough for more discard sectors, |
| 4142 | * then we can continue to increase the max discard sectors until we |
Ulf Hansson | 12182af | 2016-07-27 00:04:03 +0200 | [diff] [blame] | 4143 | * get a balance value. In cases when the 'host->max_busy_timeout' |
| 4144 | * isn't specified, use the default max erase timeout. |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4145 | */ |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4146 | do { |
| 4147 | y = 0; |
| 4148 | for (x = 1; x && x <= max_qty && max_qty - x >= qty; x <<= 1) { |
| 4149 | timeout = mmc_erase_timeout(card, arg, qty + x); |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4150 | |
Ulf Hansson | 12182af | 2016-07-27 00:04:03 +0200 | [diff] [blame] | 4151 | if (qty + x > min_qty && timeout > max_busy_timeout) |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4152 | break; |
Baolin Wang | bb4eecf | 2016-07-25 16:48:39 +0800 | [diff] [blame] | 4153 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4154 | if (timeout < last_timeout) |
| 4155 | break; |
| 4156 | last_timeout = timeout; |
| 4157 | y = x; |
| 4158 | } |
| 4159 | qty += y; |
| 4160 | } while (y); |
| 4161 | |
| 4162 | if (!qty) |
| 4163 | return 0; |
| 4164 | |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4165 | /* |
| 4166 | * When specifying a sector range to trim, chances are we might cross |
| 4167 | * an erase-group boundary even if the amount of sectors is less than |
| 4168 | * one erase-group. |
| 4169 | * If we can only fit one erase-group in the controller timeout budget, |
| 4170 | * we have to care that erase-group boundaries are not crossed by a |
| 4171 | * single trim operation. We flag that special case with "eg_boundary". |
| 4172 | * In all other cases we can just decrement qty and pretend that we |
| 4173 | * always touch (qty + 1) erase-groups as a simple optimization. |
| 4174 | */ |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4175 | if (qty == 1) |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4176 | card->eg_boundary = 1; |
| 4177 | else |
| 4178 | qty--; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4179 | |
| 4180 | /* Convert qty to sectors */ |
| 4181 | if (card->erase_shift) |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4182 | max_discard = qty << card->erase_shift; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4183 | else if (mmc_card_sd(card)) |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4184 | max_discard = qty + 1; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4185 | else |
David Jander | 642c28a | 2015-06-23 11:43:52 +0200 | [diff] [blame] | 4186 | max_discard = qty * card->erase_size; |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4187 | |
| 4188 | return max_discard; |
| 4189 | } |
| 4190 | |
| 4191 | unsigned int mmc_calc_max_discard(struct mmc_card *card) |
| 4192 | { |
| 4193 | struct mmc_host *host = card->host; |
| 4194 | unsigned int max_discard, max_trim; |
| 4195 | |
Krishna Konda | 871cb9f | 2015-09-26 17:38:26 -0700 | [diff] [blame] | 4196 | if (!host->max_busy_timeout || |
| 4197 | (host->caps2 & MMC_CAP2_MAX_DISCARD_SIZE)) |
Can Guo | 9819915 | 2017-10-17 14:13:17 +0800 | [diff] [blame] | 4198 | return UINT_MAX; |
Krishna Konda | 871cb9f | 2015-09-26 17:38:26 -0700 | [diff] [blame] | 4199 | |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4200 | /* |
| 4201 | * Without erase_group_def set, MMC erase timeout depends on clock |
| 4202 | * frequence which can change. In that case, the best choice is |
| 4203 | * just the preferred erase size. |
| 4204 | */ |
| 4205 | if (mmc_card_mmc(card) && !(card->ext_csd.erase_group_def & 1)) |
| 4206 | return card->pref_erase; |
| 4207 | |
| 4208 | max_discard = mmc_do_calc_max_discard(card, MMC_ERASE_ARG); |
| 4209 | if (mmc_can_trim(card)) { |
| 4210 | max_trim = mmc_do_calc_max_discard(card, MMC_TRIM_ARG); |
| 4211 | if (max_trim < max_discard) |
| 4212 | max_discard = max_trim; |
| 4213 | } else if (max_discard < card->erase_size) { |
| 4214 | max_discard = 0; |
| 4215 | } |
| 4216 | pr_debug("%s: calculated max. discard sectors %u for timeout %u ms\n", |
Ulf Hansson | 12182af | 2016-07-27 00:04:03 +0200 | [diff] [blame] | 4217 | mmc_hostname(host), max_discard, host->max_busy_timeout ? |
| 4218 | host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS); |
Adrian Hunter | e056a1b | 2011-06-28 17:16:02 +0300 | [diff] [blame] | 4219 | return max_discard; |
| 4220 | } |
| 4221 | EXPORT_SYMBOL(mmc_calc_max_discard); |
| 4222 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 4223 | int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen) |
| 4224 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 4225 | struct mmc_command cmd = {0}; |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 4226 | |
Ziyuan Xu | 1712c93 | 2016-09-21 09:43:49 +0800 | [diff] [blame] | 4227 | if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) || |
| 4228 | mmc_card_hs400(card) || mmc_card_hs400es(card)) |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 4229 | return 0; |
| 4230 | |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 4231 | cmd.opcode = MMC_SET_BLOCKLEN; |
| 4232 | cmd.arg = blocklen; |
| 4233 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 4234 | return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 4235 | } |
| 4236 | EXPORT_SYMBOL(mmc_set_blocklen); |
| 4237 | |
Loic Pallardy | 67c79db | 2012-08-06 17:12:30 +0200 | [diff] [blame] | 4238 | int mmc_set_blockcount(struct mmc_card *card, unsigned int blockcount, |
| 4239 | bool is_rel_write) |
| 4240 | { |
| 4241 | struct mmc_command cmd = {0}; |
| 4242 | |
| 4243 | cmd.opcode = MMC_SET_BLOCK_COUNT; |
| 4244 | cmd.arg = blockcount & 0x0000FFFF; |
| 4245 | if (is_rel_write) |
| 4246 | cmd.arg |= 1 << 31; |
| 4247 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 4248 | return mmc_wait_for_cmd(card->host, &cmd, 5); |
| 4249 | } |
| 4250 | EXPORT_SYMBOL(mmc_set_blockcount); |
| 4251 | |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4252 | static void mmc_hw_reset_for_init(struct mmc_host *host) |
| 4253 | { |
| 4254 | if (!(host->caps & MMC_CAP_HW_RESET) || !host->ops->hw_reset) |
| 4255 | return; |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 4256 | mmc_host_clk_hold(host); |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4257 | host->ops->hw_reset(host); |
Subhash Jadavani | dff3126 | 2016-04-19 16:21:16 -0700 | [diff] [blame] | 4258 | mmc_host_clk_release(host); |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4259 | } |
| 4260 | |
Ritesh Harjani | c2f6cf0 | 2015-09-28 15:55:01 +0530 | [diff] [blame] | 4261 | /* |
| 4262 | * mmc_cmdq_hw_reset: Helper API for doing |
| 4263 | * reset_all of host and reinitializing card. |
| 4264 | * This must be called with mmc_claim_host |
| 4265 | * acquired by the caller. |
| 4266 | */ |
| 4267 | int mmc_cmdq_hw_reset(struct mmc_host *host) |
| 4268 | { |
Sayali Lokhande | b263703 | 2017-04-24 13:40:50 +0530 | [diff] [blame] | 4269 | if (!host->bus_ops->reset) |
| 4270 | return -EOPNOTSUPP; |
Ritesh Harjani | c2f6cf0 | 2015-09-28 15:55:01 +0530 | [diff] [blame] | 4271 | |
Sayali Lokhande | b263703 | 2017-04-24 13:40:50 +0530 | [diff] [blame] | 4272 | return host->bus_ops->reset(host); |
Ritesh Harjani | c2f6cf0 | 2015-09-28 15:55:01 +0530 | [diff] [blame] | 4273 | } |
| 4274 | EXPORT_SYMBOL(mmc_cmdq_hw_reset); |
| 4275 | |
Johan Rudholm | 83533ab | 2015-01-12 15:38:04 +0100 | [diff] [blame] | 4276 | int mmc_hw_reset(struct mmc_host *host) |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4277 | { |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 4278 | int ret; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4279 | |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 4280 | if (!host->card) |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4281 | return -EINVAL; |
| 4282 | |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 4283 | mmc_bus_get(host); |
| 4284 | if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) { |
| 4285 | mmc_bus_put(host); |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4286 | return -EOPNOTSUPP; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4287 | } |
| 4288 | |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 4289 | ret = host->bus_ops->reset(host); |
| 4290 | mmc_bus_put(host); |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4291 | |
Gwendal Grignou | 4e6c717 | 2016-04-01 16:04:22 -0700 | [diff] [blame] | 4292 | if (ret) |
| 4293 | pr_warn("%s: tried to reset card, got error %d\n", |
| 4294 | mmc_hostname(host), ret); |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4295 | |
Johan Rudholm | f855a37 | 2015-01-12 15:38:05 +0100 | [diff] [blame] | 4296 | return ret; |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4297 | } |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4298 | EXPORT_SYMBOL(mmc_hw_reset); |
| 4299 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 4300 | static int mmc_rescan_try_freq(struct mmc_host *host, unsigned freq) |
| 4301 | { |
| 4302 | host->f_init = freq; |
| 4303 | |
| 4304 | #ifdef CONFIG_MMC_DEBUG |
| 4305 | pr_info("%s: %s: trying to init card at %u Hz\n", |
| 4306 | mmc_hostname(host), __func__, host->f_init); |
| 4307 | #endif |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 4308 | mmc_power_up(host, host->ocr_avail); |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 4309 | |
| 4310 | /* |
Adrian Hunter | b249951 | 2011-08-29 16:42:11 +0300 | [diff] [blame] | 4311 | * Some eMMCs (with VCCQ always on) may not be reset after power up, so |
| 4312 | * do a hardware reset if possible. |
| 4313 | */ |
| 4314 | mmc_hw_reset_for_init(host); |
| 4315 | |
| 4316 | /* |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 4317 | * sdio_reset sends CMD52 to reset card. Since we do not know |
| 4318 | * if the card is being re-initialized, just send it. CMD52 |
| 4319 | * should be ignored by SD/eMMC cards. |
Carlo Caione | 100a606 | 2015-11-25 15:39:51 +0100 | [diff] [blame] | 4320 | * Skip it if we already know that we do not support SDIO commands |
Philip Rakity | 2f94e55 | 2011-02-13 23:12:28 -0800 | [diff] [blame] | 4321 | */ |
Carlo Caione | 100a606 | 2015-11-25 15:39:51 +0100 | [diff] [blame] | 4322 | if (!(host->caps2 & MMC_CAP2_NO_SDIO)) |
| 4323 | sdio_reset(host); |
| 4324 | |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 4325 | mmc_go_idle(host); |
| 4326 | |
Ulf Hansson | 1b8d79c | 2016-06-21 15:12:46 +0200 | [diff] [blame] | 4327 | if (!(host->caps2 & MMC_CAP2_NO_SD)) |
| 4328 | mmc_send_if_cond(host, host->ocr_avail); |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 4329 | |
| 4330 | /* Order's important: probe SDIO, then SD, then MMC */ |
Carlo Caione | 100a606 | 2015-11-25 15:39:51 +0100 | [diff] [blame] | 4331 | if (!(host->caps2 & MMC_CAP2_NO_SDIO)) |
| 4332 | if (!mmc_attach_sdio(host)) |
| 4333 | return 0; |
| 4334 | |
Ulf Hansson | 1b8d79c | 2016-06-21 15:12:46 +0200 | [diff] [blame] | 4335 | if (!(host->caps2 & MMC_CAP2_NO_SD)) |
| 4336 | if (!mmc_attach_sd(host)) |
| 4337 | return 0; |
| 4338 | |
Shawn Lin | a0c3b68 | 2016-07-01 15:45:28 +0800 | [diff] [blame] | 4339 | if (!(host->caps2 & MMC_CAP2_NO_MMC)) |
| 4340 | if (!mmc_attach_mmc(host)) |
| 4341 | return 0; |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 4342 | |
| 4343 | mmc_power_off(host); |
| 4344 | return -EIO; |
| 4345 | } |
| 4346 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4347 | int _mmc_detect_card_removed(struct mmc_host *host) |
| 4348 | { |
| 4349 | int ret; |
| 4350 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4351 | if (!host->card || mmc_card_removed(host->card)) |
| 4352 | return 1; |
| 4353 | |
| 4354 | ret = host->bus_ops->alive(host); |
Kevin Liu | 1450734 | 2013-02-28 15:29:29 +0800 | [diff] [blame] | 4355 | |
| 4356 | /* |
| 4357 | * Card detect status and alive check may be out of sync if card is |
| 4358 | * removed slowly, when card detect switch changes while card/slot |
| 4359 | * pads are still contacted in hardware (refer to "SD Card Mechanical |
| 4360 | * Addendum, Appendix C: Card Detection Switch"). So reschedule a |
| 4361 | * detect work 200ms later for this case. |
| 4362 | */ |
| 4363 | if (!ret && host->ops->get_cd && !host->ops->get_cd(host)) { |
| 4364 | mmc_detect_change(host, msecs_to_jiffies(200)); |
| 4365 | pr_debug("%s: card removed too slowly\n", mmc_hostname(host)); |
| 4366 | } |
| 4367 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4368 | if (ret) { |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 4369 | if (host->ops->get_cd && host->ops->get_cd(host)) { |
Wanhyeong Ryu | 608a998 | 2017-11-01 15:51:40 +0800 | [diff] [blame] | 4370 | ret = mmc_recovery_fallback_lower_speed(host); |
Can Guo | 6514fbe | 2017-07-04 12:36:09 +0800 | [diff] [blame] | 4371 | } else { |
| 4372 | mmc_card_set_removed(host->card); |
| 4373 | if (host->card->sdr104_blocked) { |
| 4374 | mmc_host_set_sdr104(host); |
| 4375 | host->card->sdr104_blocked = false; |
| 4376 | } |
| 4377 | pr_debug("%s: card remove detected\n", |
| 4378 | mmc_hostname(host)); |
Asutosh Das | af6a03e | 2017-03-30 14:41:16 -0700 | [diff] [blame] | 4379 | } |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | return ret; |
| 4383 | } |
| 4384 | |
| 4385 | int mmc_detect_card_removed(struct mmc_host *host) |
| 4386 | { |
| 4387 | struct mmc_card *card = host->card; |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4388 | int ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4389 | |
| 4390 | WARN_ON(!host->claimed); |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4391 | |
| 4392 | if (!card) |
| 4393 | return 1; |
| 4394 | |
Jaehoon Chung | 6067baf | 2016-01-29 08:52:57 +0900 | [diff] [blame] | 4395 | if (!mmc_card_is_removable(host)) |
Ulf Hansson | 1ff2575 | 2015-11-05 16:21:39 +0100 | [diff] [blame] | 4396 | return 0; |
| 4397 | |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4398 | ret = mmc_card_removed(card); |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4399 | /* |
| 4400 | * The card will be considered unchanged unless we have been asked to |
| 4401 | * detect a change or host requires polling to provide card detection. |
| 4402 | */ |
Ulf Hansson | b689167 | 2013-04-18 11:02:07 +0200 | [diff] [blame] | 4403 | if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL)) |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4404 | return ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4405 | |
| 4406 | host->detect_change = 0; |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4407 | if (!ret) { |
| 4408 | ret = _mmc_detect_card_removed(host); |
Ulf Hansson | b689167 | 2013-04-18 11:02:07 +0200 | [diff] [blame] | 4409 | if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) { |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4410 | /* |
| 4411 | * Schedule a detect work as soon as possible to let a |
| 4412 | * rescan handle the card removal. |
| 4413 | */ |
| 4414 | cancel_delayed_work(&host->detect); |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 4415 | _mmc_detect_change(host, 0, false); |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4416 | } |
| 4417 | } |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4418 | |
Ulf Hansson | f0cc9cf | 2012-02-06 10:42:39 +0100 | [diff] [blame] | 4419 | return ret; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4420 | } |
| 4421 | EXPORT_SYMBOL(mmc_detect_card_removed); |
| 4422 | |
Ankit Jain | 0d55bf0 | 2017-09-19 17:43:02 +0530 | [diff] [blame] | 4423 | /* |
| 4424 | * This should be called to make sure that detect work(mmc_rescan) |
| 4425 | * is completed.Drivers may use this function from async schedule/probe |
| 4426 | * contexts to make sure that the bootdevice detection is completed on |
| 4427 | * completion of async_schedule. |
| 4428 | */ |
| 4429 | void mmc_flush_detect_work(struct mmc_host *host) |
| 4430 | { |
| 4431 | flush_delayed_work(&host->detect); |
| 4432 | } |
| 4433 | EXPORT_SYMBOL(mmc_flush_detect_work); |
| 4434 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 4435 | void mmc_rescan(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4436 | { |
Ritesh Harjani | bc67b40 | 2016-07-11 13:55:50 +0530 | [diff] [blame] | 4437 | unsigned long flags; |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 4438 | struct mmc_host *host = |
| 4439 | container_of(work, struct mmc_host, detect.work); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4440 | |
Ritesh Harjani | bc67b40 | 2016-07-11 13:55:50 +0530 | [diff] [blame] | 4441 | spin_lock_irqsave(&host->lock, flags); |
| 4442 | if (host->rescan_disable) { |
| 4443 | spin_unlock_irqrestore(&host->lock, flags); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4444 | return; |
Ritesh Harjani | bc67b40 | 2016-07-11 13:55:50 +0530 | [diff] [blame] | 4445 | } |
| 4446 | spin_unlock_irqrestore(&host->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4447 | |
Johan Rudholm | 3339d1e | 2012-08-23 13:40:55 +0200 | [diff] [blame] | 4448 | /* If there is a non-removable card registered, only scan once */ |
Jaehoon Chung | 6067baf | 2016-01-29 08:52:57 +0900 | [diff] [blame] | 4449 | if (!mmc_card_is_removable(host) && host->rescan_entered) |
Johan Rudholm | 3339d1e | 2012-08-23 13:40:55 +0200 | [diff] [blame] | 4450 | return; |
| 4451 | host->rescan_entered = 1; |
| 4452 | |
Ulf Hansson | 8623681 | 2015-11-05 16:08:07 +0100 | [diff] [blame] | 4453 | if (host->trigger_card_event && host->ops->card_event) { |
Ulf Hansson | d234d21 | 2015-11-05 16:08:08 +0100 | [diff] [blame] | 4454 | mmc_claim_host(host); |
Ulf Hansson | 8623681 | 2015-11-05 16:08:07 +0100 | [diff] [blame] | 4455 | host->ops->card_event(host); |
Ulf Hansson | d234d21 | 2015-11-05 16:08:08 +0100 | [diff] [blame] | 4456 | mmc_release_host(host); |
Ulf Hansson | 8623681 | 2015-11-05 16:08:07 +0100 | [diff] [blame] | 4457 | host->trigger_card_event = false; |
| 4458 | } |
| 4459 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4460 | mmc_bus_get(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4461 | |
Ohad Ben-Cohen | 30201e7 | 2010-11-28 07:21:28 +0200 | [diff] [blame] | 4462 | /* |
| 4463 | * if there is a _removable_ card registered, check whether it is |
| 4464 | * still present |
| 4465 | */ |
Jaehoon Chung | 6067baf | 2016-01-29 08:52:57 +0900 | [diff] [blame] | 4466 | if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host)) |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4467 | host->bus_ops->detect(host); |
| 4468 | |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4469 | host->detect_change = 0; |
Veerabhadrarao Badiganti | cdf0d57 | 2017-08-07 14:58:15 +0530 | [diff] [blame] | 4470 | if (host->ignore_bus_resume_flags) |
| 4471 | host->ignore_bus_resume_flags = false; |
Adrian Hunter | d304950 | 2011-11-28 16:22:00 +0200 | [diff] [blame] | 4472 | |
Chris Ball | c584179 | 2011-01-04 12:20:22 -0500 | [diff] [blame] | 4473 | /* |
| 4474 | * Let mmc_bus_put() free the bus/bus_ops if we've found that |
| 4475 | * the card is no longer present. |
| 4476 | */ |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4477 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4478 | mmc_bus_get(host); |
| 4479 | |
| 4480 | /* if there still is a card present, stop here */ |
| 4481 | if (host->bus_ops != NULL) { |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4482 | mmc_bus_put(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4483 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | } |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4485 | |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4486 | /* |
| 4487 | * Only we can add a new handler, so it's safe to |
| 4488 | * release the lock here. |
| 4489 | */ |
| 4490 | mmc_bus_put(host); |
| 4491 | |
Ulf Hansson | d234d21 | 2015-11-05 16:08:08 +0100 | [diff] [blame] | 4492 | mmc_claim_host(host); |
Jaehoon Chung | 6067baf | 2016-01-29 08:52:57 +0900 | [diff] [blame] | 4493 | if (mmc_card_is_removable(host) && host->ops->get_cd && |
Sascha Hauer | c1b55bf | 2013-12-05 14:34:46 +0100 | [diff] [blame] | 4494 | host->ops->get_cd(host) == 0) { |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 4495 | mmc_power_off(host); |
| 4496 | mmc_release_host(host); |
Jorg Schummer | 94d89ef | 2009-03-31 17:51:21 +0300 | [diff] [blame] | 4497 | goto out; |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 4498 | } |
Xiaonian Wang | 90d085b | 2017-04-20 01:21:21 -0700 | [diff] [blame] | 4499 | mmc_rescan_try_freq(host, host->f_min); |
Veerabhadrarao Badiganti | 4c3e670 | 2017-10-17 19:07:00 +0530 | [diff] [blame] | 4500 | host->err_stats[MMC_ERR_CMD_TIMEOUT] = 0; |
Andy Ross | 807e8e4 | 2011-01-03 10:36:56 -0800 | [diff] [blame] | 4501 | mmc_release_host(host); |
| 4502 | |
| 4503 | out: |
Anton Vorontsov | 28f5248 | 2008-06-17 18:17:15 +0400 | [diff] [blame] | 4504 | if (host->caps & MMC_CAP_NEEDS_POLL) |
| 4505 | mmc_schedule_delayed_work(&host->detect, HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | } |
| 4507 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 4508 | void mmc_start_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4509 | { |
Pavan Anamula | db78bb9 | 2015-08-24 20:15:16 +0530 | [diff] [blame] | 4510 | mmc_claim_host(host); |
Ulf Hansson | fa55018 | 2012-05-09 16:15:26 +0200 | [diff] [blame] | 4511 | host->f_init = max(freqs[0], host->f_min); |
Guennadi Liakhovetski | d9adcc1 | 2012-06-14 10:17:39 +0200 | [diff] [blame] | 4512 | host->rescan_disable = 0; |
Roger Tseng | 8af465d | 2014-09-24 17:07:13 +0800 | [diff] [blame] | 4513 | host->ios.power_mode = MMC_POWER_UNDEFINED; |
Ulf Hansson | 8d1ffc8c | 2015-09-11 14:41:55 +0200 | [diff] [blame] | 4514 | |
Adrian Hunter | a08b17b | 2013-04-15 11:27:25 -0400 | [diff] [blame] | 4515 | if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP) |
| 4516 | mmc_power_off(host); |
| 4517 | else |
Ulf Hansson | 4a06519 | 2013-09-12 14:36:53 +0200 | [diff] [blame] | 4518 | mmc_power_up(host, host->ocr_avail); |
Ulf Hansson | 8d1ffc8c | 2015-09-11 14:41:55 +0200 | [diff] [blame] | 4519 | |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 4520 | mmc_gpiod_request_cd_irq(host); |
Subhash Jadavani | 083d5e2 | 2017-05-08 18:52:56 -0700 | [diff] [blame] | 4521 | mmc_register_extcon(host); |
Pavan Anamula | db78bb9 | 2015-08-24 20:15:16 +0530 | [diff] [blame] | 4522 | mmc_release_host(host); |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 4523 | _mmc_detect_change(host, 0, false); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
Pierre Ossman | b93931a | 2007-05-19 14:06:24 +0200 | [diff] [blame] | 4526 | void mmc_stop_host(struct mmc_host *host) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | { |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 4528 | #ifdef CONFIG_MMC_DEBUG |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 4529 | unsigned long flags; |
| 4530 | spin_lock_irqsave(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 4531 | host->removed = 1; |
Pierre Ossman | 1efd48b | 2007-05-08 22:35:17 +0200 | [diff] [blame] | 4532 | spin_unlock_irqrestore(&host->lock, flags); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 4533 | #endif |
Adrian Hunter | 740a221 | 2014-03-10 15:02:41 +0200 | [diff] [blame] | 4534 | if (host->slot.cd_irq >= 0) |
| 4535 | disable_irq(host->slot.cd_irq); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 4536 | |
Guennadi Liakhovetski | d9adcc1 | 2012-06-14 10:17:39 +0200 | [diff] [blame] | 4537 | host->rescan_disable = 1; |
Guennadi Liakhovetski | d9bcbf3 | 2010-11-11 17:32:25 +0100 | [diff] [blame] | 4538 | cancel_delayed_work_sync(&host->detect); |
Pierre Ossman | 3b91e55 | 2007-02-11 20:43:19 +0100 | [diff] [blame] | 4539 | |
Nicolas Pitre | da68c4e | 2010-03-05 13:43:31 -0800 | [diff] [blame] | 4540 | /* clear pm flags now and let card drivers set them as needed */ |
| 4541 | host->pm_flags = 0; |
| 4542 | |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4543 | mmc_bus_get(host); |
| 4544 | if (host->bus_ops && !host->bus_dead) { |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 4545 | /* Calling bus_ops->remove() with a claimed host can deadlock */ |
Ulf Hansson | 58a8a4a | 2013-06-10 17:03:36 +0200 | [diff] [blame] | 4546 | host->bus_ops->remove(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4547 | mmc_claim_host(host); |
| 4548 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 4549 | mmc_power_off(host); |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4550 | mmc_release_host(host); |
Denis Karpov | 53509f0 | 2009-09-22 16:44:36 -0700 | [diff] [blame] | 4551 | mmc_bus_put(host); |
| 4552 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | } |
Pierre Ossman | 7ea239d | 2006-12-31 00:11:32 +0100 | [diff] [blame] | 4554 | mmc_bus_put(host); |
| 4555 | |
| 4556 | BUG_ON(host->card); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | |
Subhash Jadavani | ee2aaee | 2017-08-02 16:09:45 -0700 | [diff] [blame] | 4558 | mmc_unregister_extcon(host); |
Subhash Jadavani | 083d5e2 | 2017-05-08 18:52:56 -0700 | [diff] [blame] | 4559 | |
Ulf Hansson | 8d1ffc8c | 2015-09-11 14:41:55 +0200 | [diff] [blame] | 4560 | mmc_claim_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4561 | mmc_power_off(host); |
Ulf Hansson | 8d1ffc8c | 2015-09-11 14:41:55 +0200 | [diff] [blame] | 4562 | mmc_release_host(host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | } |
| 4564 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4565 | int mmc_power_save_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4566 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4567 | int ret = 0; |
| 4568 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 4569 | #ifdef CONFIG_MMC_DEBUG |
| 4570 | pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__); |
| 4571 | #endif |
| 4572 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4573 | mmc_bus_get(host); |
| 4574 | |
Ulf Hansson | 5601aaf | 2013-10-30 23:15:30 +0100 | [diff] [blame] | 4575 | if (!host->bus_ops || host->bus_dead) { |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4576 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4577 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4578 | } |
| 4579 | |
| 4580 | if (host->bus_ops->power_save) |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4581 | ret = host->bus_ops->power_save(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4582 | |
| 4583 | mmc_bus_put(host); |
| 4584 | |
| 4585 | mmc_power_off(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4586 | |
| 4587 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4588 | } |
| 4589 | EXPORT_SYMBOL(mmc_power_save_host); |
| 4590 | |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4591 | int mmc_power_restore_host(struct mmc_host *host) |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4592 | { |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4593 | int ret; |
| 4594 | |
Daniel Drake | bb9cab9 | 2011-07-17 16:38:41 +0100 | [diff] [blame] | 4595 | #ifdef CONFIG_MMC_DEBUG |
| 4596 | pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__); |
| 4597 | #endif |
| 4598 | |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4599 | mmc_bus_get(host); |
| 4600 | |
Ulf Hansson | 5601aaf | 2013-10-30 23:15:30 +0100 | [diff] [blame] | 4601 | if (!host->bus_ops || host->bus_dead) { |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4602 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4603 | return -EINVAL; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Ulf Hansson | 6904115 | 2013-09-13 11:31:33 +0200 | [diff] [blame] | 4606 | mmc_power_up(host, host->card->ocr); |
Ritesh Harjani | c2f6cf0 | 2015-09-28 15:55:01 +0530 | [diff] [blame] | 4607 | mmc_claim_host(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4608 | ret = host->bus_ops->power_restore(host); |
Ritesh Harjani | c2f6cf0 | 2015-09-28 15:55:01 +0530 | [diff] [blame] | 4609 | mmc_release_host(host); |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4610 | |
| 4611 | mmc_bus_put(host); |
Ohad Ben-Cohen | 12ae637 | 2010-10-02 13:54:06 +0200 | [diff] [blame] | 4612 | |
| 4613 | return ret; |
Adrian Hunter | eae1aee | 2009-09-22 16:44:33 -0700 | [diff] [blame] | 4614 | } |
| 4615 | EXPORT_SYMBOL(mmc_power_restore_host); |
| 4616 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4617 | /* |
Sahitya Tummala | 61868a4 | 2015-05-28 16:54:19 +0530 | [diff] [blame] | 4618 | * Add barrier request to the requests in cache |
| 4619 | */ |
| 4620 | int mmc_cache_barrier(struct mmc_card *card) |
| 4621 | { |
| 4622 | struct mmc_host *host = card->host; |
| 4623 | int err = 0; |
| 4624 | |
| 4625 | if (!card->ext_csd.cache_ctrl || |
| 4626 | (card->quirks & MMC_QUIRK_CACHE_DISABLE)) |
| 4627 | goto out; |
| 4628 | |
| 4629 | if (!mmc_card_mmc(card)) |
| 4630 | goto out; |
| 4631 | |
| 4632 | if (!card->ext_csd.barrier_en) |
| 4633 | return -ENOTSUPP; |
| 4634 | |
| 4635 | /* |
| 4636 | * If a device receives maximum supported barrier |
| 4637 | * requests, a barrier command is treated as a |
| 4638 | * flush command. Hence, it is betetr to use |
| 4639 | * flush timeout instead a generic CMD6 timeout |
| 4640 | */ |
| 4641 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 4642 | EXT_CSD_FLUSH_CACHE, 0x2, 0); |
| 4643 | if (err) |
| 4644 | pr_err("%s: cache barrier error %d\n", |
| 4645 | mmc_hostname(host), err); |
| 4646 | out: |
| 4647 | return err; |
| 4648 | } |
| 4649 | EXPORT_SYMBOL(mmc_cache_barrier); |
| 4650 | |
| 4651 | /* |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4652 | * Flush the cache to the non-volatile storage. |
| 4653 | */ |
| 4654 | int mmc_flush_cache(struct mmc_card *card) |
| 4655 | { |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4656 | int err = 0; |
| 4657 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4658 | if (mmc_card_mmc(card) && |
| 4659 | (card->ext_csd.cache_size > 0) && |
Talel Shenhar | c1d30bc | 2015-02-04 17:59:23 +0200 | [diff] [blame] | 4660 | (card->ext_csd.cache_ctrl & 1) && |
| 4661 | (!(card->quirks & MMC_QUIRK_CACHE_DISABLE))) { |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4662 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 4663 | EXT_CSD_FLUSH_CACHE, 1, 0); |
Talel Shenhar | 8a8e3b4 | 2015-02-11 12:58:16 +0200 | [diff] [blame] | 4664 | if (err == -ETIMEDOUT) { |
| 4665 | pr_err("%s: cache flush timeout\n", |
| 4666 | mmc_hostname(card->host)); |
| 4667 | err = mmc_interrupt_hpi(card); |
| 4668 | if (err) { |
| 4669 | pr_err("%s: mmc_interrupt_hpi() failed (%d)\n", |
| 4670 | mmc_hostname(card->host), err); |
| 4671 | err = -ENODEV; |
| 4672 | } |
| 4673 | } else if (err) { |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4674 | pr_err("%s: cache flush error %d\n", |
| 4675 | mmc_hostname(card->host), err); |
Talel Shenhar | 8a8e3b4 | 2015-02-11 12:58:16 +0200 | [diff] [blame] | 4676 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 4677 | } |
| 4678 | |
| 4679 | return err; |
| 4680 | } |
| 4681 | EXPORT_SYMBOL(mmc_flush_cache); |
| 4682 | |
Ulf Hansson | 8dede18 | 2015-11-05 16:11:12 +0100 | [diff] [blame] | 4683 | #ifdef CONFIG_PM_SLEEP |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4684 | /* Do the card removal on suspend if card is assumed removeable |
| 4685 | * Do that in pm notifier while userspace isn't yet frozen, so we will be able |
| 4686 | to sync the card. |
| 4687 | */ |
Ulf Hansson | 8dede18 | 2015-11-05 16:11:12 +0100 | [diff] [blame] | 4688 | static int mmc_pm_notify(struct notifier_block *notify_block, |
| 4689 | unsigned long mode, void *unused) |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4690 | { |
| 4691 | struct mmc_host *host = container_of( |
| 4692 | notify_block, struct mmc_host, pm_notify); |
| 4693 | unsigned long flags; |
Ulf Hansson | 810cadd | 2013-06-10 17:03:37 +0200 | [diff] [blame] | 4694 | int err = 0; |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4695 | |
| 4696 | switch (mode) { |
| 4697 | case PM_HIBERNATION_PREPARE: |
| 4698 | case PM_SUSPEND_PREPARE: |
Grygorii Strashko | 184af16 | 2015-04-23 13:43:43 +0300 | [diff] [blame] | 4699 | case PM_RESTORE_PREPARE: |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4700 | spin_lock_irqsave(&host->lock, flags); |
| 4701 | host->rescan_disable = 1; |
| 4702 | spin_unlock_irqrestore(&host->lock, flags); |
| 4703 | cancel_delayed_work_sync(&host->detect); |
| 4704 | |
Ulf Hansson | 810cadd | 2013-06-10 17:03:37 +0200 | [diff] [blame] | 4705 | if (!host->bus_ops) |
| 4706 | break; |
| 4707 | |
| 4708 | /* Validate prerequisites for suspend */ |
| 4709 | if (host->bus_ops->pre_suspend) |
| 4710 | err = host->bus_ops->pre_suspend(host); |
Ulf Hansson | 5601aaf | 2013-10-30 23:15:30 +0100 | [diff] [blame] | 4711 | if (!err) |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4712 | break; |
| 4713 | |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 4714 | /* Calling bus_ops->remove() with a claimed host can deadlock */ |
Ulf Hansson | 58a8a4a | 2013-06-10 17:03:36 +0200 | [diff] [blame] | 4715 | host->bus_ops->remove(host); |
Guennadi Liakhovetski | 0db13fc | 2012-01-04 15:28:45 +0100 | [diff] [blame] | 4716 | mmc_claim_host(host); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4717 | mmc_detach_bus(host); |
Ulf Hansson | 7f7e412 | 2011-09-21 14:08:13 -0400 | [diff] [blame] | 4718 | mmc_power_off(host); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4719 | mmc_release_host(host); |
| 4720 | host->pm_flags = 0; |
| 4721 | break; |
| 4722 | |
| 4723 | case PM_POST_SUSPEND: |
| 4724 | case PM_POST_HIBERNATION: |
Takashi Iwai | 274476f | 2010-12-10 08:40:31 +0100 | [diff] [blame] | 4725 | case PM_POST_RESTORE: |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4726 | |
| 4727 | spin_lock_irqsave(&host->lock, flags); |
| 4728 | host->rescan_disable = 0; |
Veerabhadrarao Badiganti | cdf0d57 | 2017-08-07 14:58:15 +0530 | [diff] [blame] | 4729 | if (mmc_bus_manual_resume(host) && |
| 4730 | !host->ignore_bus_resume_flags) { |
Dmitry Shmidt | 3ad1837 | 2010-10-14 15:20:21 -0700 | [diff] [blame] | 4731 | spin_unlock_irqrestore(&host->lock, flags); |
| 4732 | break; |
| 4733 | } |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4734 | spin_unlock_irqrestore(&host->lock, flags); |
Ulf Hansson | bbd4368 | 2013-09-20 11:02:35 +0200 | [diff] [blame] | 4735 | _mmc_detect_change(host, 0, false); |
Maxim Levitsky | 4c2ef25 | 2010-08-10 18:01:41 -0700 | [diff] [blame] | 4736 | |
| 4737 | } |
| 4738 | |
| 4739 | return 0; |
| 4740 | } |
Ulf Hansson | 8dede18 | 2015-11-05 16:11:12 +0100 | [diff] [blame] | 4741 | |
| 4742 | void mmc_register_pm_notifier(struct mmc_host *host) |
| 4743 | { |
| 4744 | host->pm_notify.notifier_call = mmc_pm_notify; |
| 4745 | register_pm_notifier(&host->pm_notify); |
| 4746 | } |
| 4747 | |
| 4748 | void mmc_unregister_pm_notifier(struct mmc_host *host) |
| 4749 | { |
| 4750 | unregister_pm_notifier(&host->pm_notify); |
| 4751 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4752 | #endif |
| 4753 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 4754 | /** |
| 4755 | * mmc_init_context_info() - init synchronization context |
| 4756 | * @host: mmc host |
| 4757 | * |
| 4758 | * Init struct context_info needed to implement asynchronous |
| 4759 | * request mechanism, used by mmc core, host driver and mmc requests |
| 4760 | * supplier. |
| 4761 | */ |
| 4762 | void mmc_init_context_info(struct mmc_host *host) |
| 4763 | { |
| 4764 | spin_lock_init(&host->context_info.lock); |
| 4765 | host->context_info.is_new_req = false; |
| 4766 | host->context_info.is_done_rcv = false; |
| 4767 | host->context_info.is_waiting_last_req = false; |
| 4768 | init_waitqueue_head(&host->context_info.wait); |
| 4769 | } |
| 4770 | |
San Mehat | 03a7efe | 2008-04-14 15:22:49 -0700 | [diff] [blame] | 4771 | #ifdef CONFIG_MMC_EMBEDDED_SDIO |
| 4772 | void mmc_set_embedded_sdio_data(struct mmc_host *host, |
| 4773 | struct sdio_cis *cis, |
| 4774 | struct sdio_cccr *cccr, |
| 4775 | struct sdio_embedded_func *funcs, |
| 4776 | int num_funcs) |
| 4777 | { |
| 4778 | host->embedded_sdio_data.cis = cis; |
| 4779 | host->embedded_sdio_data.cccr = cccr; |
| 4780 | host->embedded_sdio_data.funcs = funcs; |
| 4781 | host->embedded_sdio_data.num_funcs = num_funcs; |
| 4782 | } |
| 4783 | |
| 4784 | EXPORT_SYMBOL(mmc_set_embedded_sdio_data); |
| 4785 | #endif |
| 4786 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4787 | static int __init mmc_init(void) |
| 4788 | { |
| 4789 | int ret; |
| 4790 | |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4791 | ret = mmc_register_bus(); |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 4792 | if (ret) |
Ulf Hansson | 520bd7a | 2015-12-01 10:35:29 +0100 | [diff] [blame] | 4793 | return ret; |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 4794 | |
| 4795 | ret = mmc_register_host_class(); |
| 4796 | if (ret) |
| 4797 | goto unregister_bus; |
| 4798 | |
| 4799 | ret = sdio_register_bus(); |
| 4800 | if (ret) |
| 4801 | goto unregister_host_class; |
| 4802 | |
| 4803 | return 0; |
| 4804 | |
| 4805 | unregister_host_class: |
| 4806 | mmc_unregister_host_class(); |
| 4807 | unregister_bus: |
| 4808 | mmc_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4809 | return ret; |
| 4810 | } |
| 4811 | |
| 4812 | static void __exit mmc_exit(void) |
| 4813 | { |
Pierre Ossman | e29a7d7 | 2007-05-26 13:48:18 +0200 | [diff] [blame] | 4814 | sdio_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4815 | mmc_unregister_host_class(); |
| 4816 | mmc_unregister_bus(); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4817 | } |
| 4818 | |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 4819 | #ifdef CONFIG_BLOCK |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 4820 | static ssize_t |
| 4821 | latency_hist_show(struct device *dev, struct device_attribute *attr, char *buf) |
| 4822 | { |
| 4823 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 4824 | |
| 4825 | return blk_latency_hist_show(&host->io_lat_s, buf); |
| 4826 | } |
| 4827 | |
| 4828 | /* |
| 4829 | * Values permitted 0, 1, 2. |
| 4830 | * 0 -> Disable IO latency histograms (default) |
| 4831 | * 1 -> Enable IO latency histograms |
| 4832 | * 2 -> Zero out IO latency histograms |
| 4833 | */ |
| 4834 | static ssize_t |
| 4835 | latency_hist_store(struct device *dev, struct device_attribute *attr, |
| 4836 | const char *buf, size_t count) |
| 4837 | { |
| 4838 | struct mmc_host *host = cls_dev_to_mmc_host(dev); |
| 4839 | long value; |
| 4840 | |
| 4841 | if (kstrtol(buf, 0, &value)) |
| 4842 | return -EINVAL; |
| 4843 | if (value == BLK_IO_LAT_HIST_ZERO) |
| 4844 | blk_zero_latency_hist(&host->io_lat_s); |
| 4845 | else if (value == BLK_IO_LAT_HIST_ENABLE || |
| 4846 | value == BLK_IO_LAT_HIST_DISABLE) |
| 4847 | host->latency_hist_enabled = value; |
| 4848 | return count; |
| 4849 | } |
| 4850 | |
| 4851 | static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR, |
| 4852 | latency_hist_show, latency_hist_store); |
| 4853 | |
| 4854 | void |
| 4855 | mmc_latency_hist_sysfs_init(struct mmc_host *host) |
| 4856 | { |
| 4857 | if (device_create_file(&host->class_dev, &dev_attr_latency_hist)) |
| 4858 | dev_err(&host->class_dev, |
| 4859 | "Failed to create latency_hist sysfs entry\n"); |
| 4860 | } |
| 4861 | |
| 4862 | void |
| 4863 | mmc_latency_hist_sysfs_exit(struct mmc_host *host) |
| 4864 | { |
| 4865 | device_remove_file(&host->class_dev, &dev_attr_latency_hist); |
| 4866 | } |
Mohan Srinivasan | 2320e1e | 2016-10-03 16:17:34 -0700 | [diff] [blame] | 4867 | #endif |
Mohan Srinivasan | 0ef170d | 2016-08-25 18:31:01 -0700 | [diff] [blame] | 4868 | |
Nicolas Pitre | 2607496 | 2007-06-16 02:07:53 -0400 | [diff] [blame] | 4869 | subsys_initcall(mmc_init); |
Pierre Ossman | ffce2e7 | 2007-05-19 14:32:22 +0200 | [diff] [blame] | 4870 | module_exit(mmc_exit); |
| 4871 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4872 | MODULE_LICENSE("GPL"); |