blob: c172be92771f12474224a6aaf32e57edddaeaba6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossmanaaac1b42007-02-28 15:33:10 +01002 * linux/drivers/mmc/core/core.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003-2004 Russell King, All Rights Reserved.
Pierre Ossman5b4fd9a2005-09-06 15:18:56 -07005 * SD support Copyright (C) 2004 Ian Molton, All Rights Reserved.
Pierre Ossmanad3868b2008-06-28 12:52:45 +02006 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Philip Langdalebce40a32006-10-21 12:35:02 +02007 * MMCv4 support Copyright (C) 2006 Philip Langdale, All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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 Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/interrupt.h>
16#include <linux/completion.h>
Talel Shenhar7dc5f792015-05-18 12:12:48 +030017#include <linux/devfreq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/device.h>
19#include <linux/delay.h>
20#include <linux/pagemap.h>
21#include <linux/err.h>
Pierre Ossmanaf8350c2007-09-24 07:15:48 +020022#include <linux/leds.h>
Pierre Ossmanb57c43a2005-09-06 15:18:53 -070023#include <linux/scatterlist.h>
Anton Vorontsov86e82862008-11-26 22:54:17 +030024#include <linux/log2.h>
David Brownell5c139412009-03-11 03:30:43 -080025#include <linux/regulator/consumer.h>
Ohad Ben-Cohene5945732010-11-28 07:21:30 +020026#include <linux/pm_runtime.h>
Ulf Hanssonbbd43682013-09-20 11:02:35 +020027#include <linux/pm_wakeup.h>
Amerigo Wang35eb6db2011-07-25 17:13:11 -070028#include <linux/suspend.h>
Per Forlin1b676f72011-08-19 14:52:37 +020029#include <linux/fault-inject.h>
30#include <linux/random.h>
Jaehoon Chung950d56a2012-09-17 08:42:02 +000031#include <linux/slab.h>
Haijun Zhang6e9e3182013-08-26 09:19:22 +080032#include <linux/of.h>
Sujith Reddy Thumma1547ccb2011-02-17 21:37:48 +053033#include <linux/pm.h>
Sujit Reddy Thummacb18d852014-12-04 09:57:23 +020034#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
36#include <linux/mmc/card.h>
37#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010038#include <linux/mmc/mmc.h>
39#include <linux/mmc/sd.h>
Adrian Hunter740a2212014-03-10 15:02:41 +020040#include <linux/mmc/slot-gpio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Baolin Wang7962fc32016-03-31 11:16:27 +080042#define CREATE_TRACE_POINTS
43#include <trace/events/mmc.h>
44
Pierre Ossmanaaac1b42007-02-28 15:33:10 +010045#include "core.h"
Pierre Ossmanffce2e72007-05-19 14:32:22 +020046#include "bus.h"
47#include "host.h"
Pierre Ossmane29a7d72007-05-26 13:48:18 +020048#include "sdio_bus.h"
Ulf Hansson3aa87932014-11-28 14:38:36 +010049#include "pwrseq.h"
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010050
51#include "mmc_ops.h"
52#include "sd_ops.h"
Pierre Ossman5c4e6f12007-05-21 20:23:20 +020053#include "sdio_ops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Trey Ramsay8fee4762012-11-16 09:31:41 -060055/* If the device is not responding */
56#define MMC_CORE_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
57
Jaehoon Chung950d56a2012-09-17 08:42:02 +000058/*
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 Hansson12182af2016-07-27 00:04:03 +020064/* 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 Hanssonfa550182012-05-09 16:15:26 +020067static const unsigned freqs[] = { 400000, 300000, 200000, 100000 };
Pierre Ossmanffce2e72007-05-19 14:32:22 +020068
69/*
David Brownellaf517152007-08-08 09:11:32 -070070 * 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 Russell90ab5ee2012-01-13 09:32:20 +103074bool use_spi_crc = 1;
David Brownellaf517152007-08-08 09:11:32 -070075module_param(use_spi_crc, bool, 0);
76
Pierre Ossmanffce2e72007-05-19 14:32:22 +020077static int mmc_schedule_delayed_work(struct delayed_work *work,
78 unsigned long delay)
79{
Ulf Hansson520bd7a2015-12-01 10:35:29 +010080 /*
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 Ossmanffce2e72007-05-19 14:32:22 +020087}
88
Per Forlin1b676f72011-08-19 14:52:37 +020089#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 */
95static 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 Mita2e744fc2013-04-29 16:21:31 -0700113 data->error = data_errors[prandom_u32() % ARRAY_SIZE(data_errors)];
114 data->bytes_xfered = (prandom_u32() % (data->bytes_xfered >> 9)) << 9;
Asutosh Dasf0665412012-07-27 18:10:19 +0530115 data->fault_injected = true;
Per Forlin1b676f72011-08-19 14:52:37 +0200116}
117
118#else /* CONFIG_FAIL_MMC_REQUEST */
119
120static 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 Shenhar7dc5f792015-05-18 12:12:48 +0300127static bool mmc_is_data_request(struct mmc_request *mmc_request)
Sujit Reddy Thummaa12e92b2014-12-09 12:55:38 +0200128{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300129 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
140static void mmc_clk_scaling_start_busy(struct mmc_host *host, bool lock_needed)
141{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300142 struct mmc_devfeq_clk_scaling *clk_scaling = &host->clk_scaling;
143
144 if (!clk_scaling->enable)
145 return;
146
147 if (lock_needed)
Talel Shenhar87605ad2015-06-25 09:30:04 +0300148 spin_lock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300149
150 clk_scaling->start_busy = ktime_get();
151 clk_scaling->is_busy_started = true;
152
153 if (lock_needed)
Talel Shenhar87605ad2015-06-25 09:30:04 +0300154 spin_unlock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300155}
156
157static void mmc_clk_scaling_stop_busy(struct mmc_host *host, bool lock_needed)
158{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300159 struct mmc_devfeq_clk_scaling *clk_scaling = &host->clk_scaling;
160
161 if (!clk_scaling->enable)
162 return;
163
164 if (lock_needed)
Talel Shenhar87605ad2015-06-25 09:30:04 +0300165 spin_lock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300166
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
179out:
180 if (lock_needed)
Talel Shenhar87605ad2015-06-25 09:30:04 +0300181 spin_unlock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300182}
183
184/**
Talel Shenhar339a4e12015-06-29 10:50:19 +0300185 * 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 */
191void 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}
210EXPORT_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 */
221void 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 }
252out:
253 if (lock_needed)
254 spin_unlock_bh(&host->clk_scaling.lock);
255
256}
257EXPORT_SYMBOL(mmc_cmdq_clk_scaling_stop_busy);
258
259/**
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300260 * mmc_can_scale_clk() - Check clock scaling capability
261 * @host: pointer to mmc host structure
262 */
263bool 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 Shenhar999102c2015-09-06 16:38:42 +0300271 return host->caps2 & MMC_CAP2_CLK_SCALE;
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300272}
273EXPORT_SYMBOL(mmc_can_scale_clk);
274
275static int mmc_devfreq_get_dev_status(struct device *dev,
276 struct devfreq_dev_status *status)
277{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300278 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 Shenhar87605ad2015-06-25 09:30:04 +0300292 spin_lock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300293
294 /* accumulate the busy time of ongoing work */
295 memset(status, 0, sizeof(*status));
296 if (clk_scaling->is_busy_started) {
Talel Shenhar339a4e12015-06-29 10:50:19 +0300297 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 Shenhar7dc5f792015-05-18 12:12:48 +0300305 }
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 Shenhar87605ad2015-06-25 09:30:04 +0300320 spin_unlock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300321
322 return 0;
323}
324
Talel Shenhar9591b992015-07-20 16:12:31 +0300325static bool mmc_is_valid_state_for_clk_scaling(struct mmc_host *host)
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300326{
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 Erezee975d32015-09-17 16:07:39 +0300336 (card->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB ||
337 mmc_card_doing_bkops(card))))
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300338 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 Harjanicd62f1b2015-07-13 10:52:36 +0530348int mmc_cmdq_halt_on_empty_queue(struct mmc_host *host)
Talel Shenhar339a4e12015-06-29 10:50:19 +0300349{
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
368out:
369 return err;
370}
Ritesh Harjanicd62f1b2015-07-13 10:52:36 +0530371EXPORT_SYMBOL(mmc_cmdq_halt_on_empty_queue);
Talel Shenhar339a4e12015-06-29 10:50:19 +0300372
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300373int mmc_clk_update_freq(struct mmc_host *host,
374 unsigned long freq, enum mmc_load state)
375{
376 int err = 0;
Talel Shenhar339a4e12015-06-29 10:50:19 +0300377 bool cmdq_mode;
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300378
379 if (!host) {
380 pr_err("bad host parameter\n");
381 WARN_ON(1);
382 return -EINVAL;
383 }
384
Talel Shenhar339a4e12015-06-29 10:50:19 +0300385 mmc_host_clk_hold(host);
386 cmdq_mode = mmc_card_cmdq(host->card);
387
Talel Shenhara52f84e2015-05-27 14:20:34 +0300388 /* 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 Shenhar9591b992015-07-20 16:12:31 +0300403 if (freq == host->clk_scaling.curr_freq)
404 goto out;
405
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300406 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 Thummaa12e92b2014-12-09 12:55:38 +0200412 }
Sujit Reddy Thummaa12e92b2014-12-09 12:55:38 +0200413 }
414
Talel Shenhar9591b992015-07-20 16:12:31 +0300415 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 Shenhar339a4e12015-06-29 10:50:19 +0300421 }
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300422 }
Talel Shenhar9591b992015-07-20 16:12:31 +0300423
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
437invalid_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
444halt_failed:
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300445 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 }
453out:
Talel Shenhar339a4e12015-06-29 10:50:19 +0300454 mmc_host_clk_release(host);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300455 return err;
456}
457EXPORT_SYMBOL(mmc_clk_update_freq);
458
Wanhyeong Ryu608a9982017-11-01 15:51:40 +0800459int mmc_recovery_fallback_lower_speed(struct mmc_host *host)
Can Guo6514fbe2017-07-04 12:36:09 +0800460{
Wanhyeong Ryu608a9982017-11-01 15:51:40 +0800461 int err = 0;
Can Guo6514fbe2017-07-04 12:36:09 +0800462 if (!host->card)
Wanhyeong Ryu608a9982017-11-01 15:51:40 +0800463 return -EINVAL;
Can Guo6514fbe2017-07-04 12:36:09 +0800464
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 Ryu608a9982017-11-01 15:51:40 +0800471 err = mmc_hw_reset(host);
Can Guo6514fbe2017-07-04 12:36:09 +0800472 host->card->sdr104_blocked = true;
473 }
Wanhyeong Ryu608a9982017-11-01 15:51:40 +0800474 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 Guo6514fbe2017-07-04 12:36:09 +0800479}
480
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300481static int mmc_devfreq_set_target(struct device *dev,
482 unsigned long *freq, u32 devfreq_flags)
Sujit Reddy Thummaa12e92b2014-12-09 12:55:38 +0200483{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300484 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 Badiganti2f3f4eb2017-06-23 19:18:45 +0530488 unsigned long pflags = current->flags;
489
490 /* Ensure scaling would happen even in memory pressure conditions */
491 current->flags |= PF_MEMALLOC;
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300492
493 if (!(host && freq)) {
494 pr_err("%s: unexpected host/freq parameter\n", __func__);
495 err = -EINVAL;
496 goto out;
Sujit Reddy Thummaa12e92b2014-12-09 12:55:38 +0200497 }
Veerabhadrarao Badiganti2f3f4eb2017-06-23 19:18:45 +0530498
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300499 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 Shenhar87605ad2015-06-25 09:30:04 +0300515 spin_lock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300516 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 Shenhar87605ad2015-06-25 09:30:04 +0300519 spin_unlock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300520 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 Shenhar87605ad2015-06-25 09:30:04 +0300526 spin_unlock_bh(&clk_scaling->lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300527
528 abort = __mmc_claim_host(host, &clk_scaling->devfreq_abort);
529 if (abort)
530 goto out;
531
Can Guo6514fbe2017-07-04 12:36:09 +0800532 if (mmc_card_sd(host->card) && host->card->sdr104_blocked)
533 goto rel_host;
534
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300535 /*
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 Guo6514fbe2017-07-04 12:36:09 +0800543 if (err && err != -EAGAIN) {
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300544 pr_err("%s: clock scale to %lu failed with error %d\n",
545 mmc_hostname(host), *freq, err);
Wanhyeong Ryu608a9982017-11-01 15:51:40 +0800546 err = mmc_recovery_fallback_lower_speed(host);
Can Guo6514fbe2017-07-04 12:36:09 +0800547 } else {
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300548 pr_debug("%s: clock change to %lu finished successfully (%s)\n",
549 mmc_hostname(host), *freq, current->comm);
Can Guo6514fbe2017-07-04 12:36:09 +0800550 }
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300551
Talel Shenhar39040672015-07-14 16:06:06 +0300552
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300553 mmc_host_clk_release(host);
Can Guo6514fbe2017-07-04 12:36:09 +0800554rel_host:
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300555 mmc_release_host(host);
556out:
Veerabhadrarao Badiganti2f3f4eb2017-06-23 19:18:45 +0530557 tsk_restore_flags(current, pflags, PF_MEMALLOC);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300558 return err;
Sujit Reddy Thummaa12e92b2014-12-09 12:55:38 +0200559}
560
Talel Shenhar339a4e12015-06-29 10:50:19 +0300561/**
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 */
568void mmc_deferred_scaling(struct mmc_host *host)
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300569{
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300570 unsigned long target_freq;
571 int err;
572
573 if (!host->clk_scaling.enable)
574 return;
575
Can Guo6514fbe2017-07-04 12:36:09 +0800576 if (mmc_card_sd(host->card) && host->card->sdr104_blocked)
577 return;
578
Talel Shenhar87605ad2015-06-25 09:30:04 +0300579 spin_lock_bh(&host->clk_scaling.lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300580
581 if (host->clk_scaling.clk_scaling_in_progress ||
582 !(host->clk_scaling.need_freq_change)) {
Talel Shenhar87605ad2015-06-25 09:30:04 +0300583 spin_unlock_bh(&host->clk_scaling.lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300584 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 Shenhar87605ad2015-06-25 09:30:04 +0300592 spin_unlock_bh(&host->clk_scaling.lock);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300593 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 Guo6514fbe2017-07-04 12:36:09 +0800599 if (err && err != -EAGAIN) {
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300600 pr_err("%s: failed on deferred scale clocks (%d)\n",
601 mmc_hostname(host), err);
Can Guo6514fbe2017-07-04 12:36:09 +0800602 mmc_recovery_fallback_lower_speed(host);
603 } else {
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300604 pr_debug("%s: clocks were successfully scaled to %lu (%s)\n",
605 mmc_hostname(host),
606 target_freq, current->comm);
Can Guo6514fbe2017-07-04 12:36:09 +0800607 }
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300608 host->clk_scaling.clk_scaling_in_progress = false;
609 atomic_dec(&host->clk_scaling.devfreq_abort);
610}
Talel Shenhar339a4e12015-06-29 10:50:19 +0300611EXPORT_SYMBOL(mmc_deferred_scaling);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300612
613static 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 Badiganti48887f32017-05-30 20:44:06 +0530623 /*
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 Shenhar7dc5f792015-05-18 12:12:48 +0300629 if (!clk_scaling->freq_table) {
Veerabhadrarao Badiganti48887f32017-05-30 20:44:06 +0530630 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 Shenhar7dc5f792015-05-18 12:12:48 +0300636 clk_scaling->freq_table = kzalloc(
Veerabhadrarao Badiganti48887f32017-05-30 20:44:06 +0530637 (clk_scaling->freq_table_sz *
638 sizeof(*(clk_scaling->freq_table))), GFP_KERNEL);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300639 if (!clk_scaling->freq_table)
640 return -ENOMEM;
Veerabhadrarao Badiganti48887f32017-05-30 20:44:06 +0530641
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 Shenhar7dc5f792015-05-18 12:12:48 +0300656 }
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
675out:
Xiaonian Wangf3218eb2017-04-20 00:30:42 -0700676 /**
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 Shenhar7dc5f792015-05-18 12:12:48 +0300687 clk_scaling->devfreq_profile.max_state = clk_scaling->freq_table_sz;
688
Xiaonian Wangf3218eb2017-04-20 00:30:42 -0700689 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 Shenhar7dc5f792015-05-18 12:12:48 +0300692 pr_debug("%s: freq[%d] = %u\n",
693 mmc_hostname(host), i, clk_scaling->freq_table[i]);
Xiaonian Wangf3218eb2017-04-20 00:30:42 -0700694 }
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300695
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 */
708int 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 Jadavani164db7b2015-10-05 11:33:11 -0700753 host->clk_scaling.upthreshold - host->clk_scaling.downthreshold;
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300754
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}
789EXPORT_SYMBOL(mmc_init_clk_scaling);
790
791/**
Talel Shenhar999102c2015-09-06 16:38:42 +0300792 * 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 */
800int 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 Dorfman9379bf92015-12-20 15:16:40 +0200819 wake_up(&host->wq);
Talel Shenhar999102c2015-09-06 16:38:42 +0300820 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}
834EXPORT_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 */
844int mmc_resume_clk_scaling(struct mmc_host *host)
845{
846 int err = 0;
Sahitya Tummala1a9cf8b2016-02-12 16:53:04 +0530847 u32 max_clk_idx = 0;
848 u32 devfreq_max_clk = 0;
849 u32 devfreq_min_clk = 0;
Talel Shenhar999102c2015-09-06 16:38:42 +0300850
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 Viswanath450552a2017-05-19 16:26:33 +0530859 /*
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 Shenhar999102c2015-09-06 16:38:42 +0300864 if (!host->clk_scaling.devfreq) {
Vijay Viswanath450552a2017-05-19 16:26:33 +0530865 pr_warn("%s: %s: no devfreq is assosiated with this device\n",
Talel Shenhar999102c2015-09-06 16:38:42 +0300866 mmc_hostname(host), __func__);
Vijay Viswanath450552a2017-05-19 16:26:33 +0530867 return 0;
Talel Shenhar999102c2015-09-06 16:38:42 +0300868 }
869
870 atomic_set(&host->clk_scaling.devfreq_abort, 0);
Sahitya Tummala1a9cf8b2016-02-12 16:53:04 +0530871
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 Badiganti4ef47bc2017-06-01 21:16:14 +0530877 if (host->ios.clock < host->clk_scaling.freq_table[max_clk_idx])
Sahitya Tummala1a9cf8b2016-02-12 16:53:04 +0530878 host->clk_scaling.curr_freq = devfreq_min_clk;
879
Talel Shenhar999102c2015-09-06 16:38:42 +0300880 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}
894EXPORT_SYMBOL(mmc_resume_clk_scaling);
895
896/**
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300897 * mmc_exit_devfreq_clk_scaling() - Disable clock scaling
898 * @host: pointer to mmc host structure
899 *
900 * Disable clock scaling permanently.
901 */
902int mmc_exit_clk_scaling(struct mmc_host *host)
903{
904 int err;
905
906 if (!host) {
Talel Shenhar999102c2015-09-06 16:38:42 +0300907 pr_err("%s: bad host parameter\n", __func__);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300908 WARN_ON(1);
909 return -EINVAL;
910 }
911
Talel Shenhar999102c2015-09-06 16:38:42 +0300912 if (!mmc_can_scale_clk(host))
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300913 return 0;
914
915 if (!host->clk_scaling.devfreq) {
Talel Shenhar999102c2015-09-06 16:38:42 +0300916 pr_err("%s: %s: no devfreq is assosiated with this device\n",
917 mmc_hostname(host), __func__);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300918 return -EPERM;
919 }
920
Talel Shenhar999102c2015-09-06 16:38:42 +0300921 err = mmc_suspend_clk_scaling(host);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300922 if (err) {
Talel Shenhar999102c2015-09-06 16:38:42 +0300923 pr_err("%s: %s: fail to suspend clock scaling (%d)\n",
924 mmc_hostname(host), __func__, err);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300925 return err;
926 }
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300927
928 err = devfreq_remove_device(host->clk_scaling.devfreq);
929 if (err) {
Talel Shenhar999102c2015-09-06 16:38:42 +0300930 pr_err("%s: remove devfreq failed (%d)\n",
931 mmc_hostname(host), err);
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300932 return err;
933 }
934
Xiaonian Wangf3218eb2017-04-20 00:30:42 -0700935 kfree(host->clk_scaling.devfreq_profile.freq_table);
936
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300937 host->clk_scaling.devfreq = NULL;
938 atomic_set(&host->clk_scaling.devfreq_abort, 1);
Veerabhadrarao Badiganti48887f32017-05-30 20:44:06 +0530939
940 kfree(host->clk_scaling.freq_table);
941 host->clk_scaling.freq_table = NULL;
942
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300943 pr_debug("%s: devfreq was removed\n", mmc_hostname(host));
944
945 return 0;
946}
947EXPORT_SYMBOL(mmc_exit_clk_scaling);
948
Adrian Hunter5163af52016-08-16 13:44:11 +0300949static 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
955void 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}
965EXPORT_SYMBOL(mmc_command_done);
966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967/**
Russell Kingfe10c6a2006-05-04 13:51:45 +0100968 * mmc_request_done - finish processing an MMC request
969 * @host: MMC host which completed request
970 * @mrq: MMC request which request
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 *
972 * MMC drivers should call this function when they have completed
Russell Kingfe10c6a2006-05-04 13:51:45 +0100973 * their processing of a request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
975void mmc_request_done(struct mmc_host *host, struct mmc_request *mrq)
976{
977 struct mmc_command *cmd = mrq->cmd;
Russell King920e70c2006-05-04 18:22:51 +0100978 int err = cmd->error;
Aparna Mallavarapueb2bbc02010-06-11 18:13:05 +0530979#ifdef CONFIG_MMC_PERF_PROFILING
980 ktime_t diff;
981#endif
Talel Shenhar7dc5f792015-05-18 12:12:48 +0300982
983 if (host->clk_scaling.is_busy_started)
984 mmc_clk_scaling_stop_busy(host, true);
Russell King920e70c2006-05-04 18:22:51 +0100985
Adrian Hunterbd11e8b2015-05-07 13:10:22 +0300986 /* Flag re-tuning needed on CRC errors */
Chaotian Jing031277d2015-09-30 17:37:18 +0800987 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 Hunterbd11e8b2015-05-07 13:10:22 +0300990 (mrq->data && mrq->data->error == -EILSEQ) ||
Chaotian Jing031277d2015-09-30 17:37:18 +0800991 (mrq->stop && mrq->stop->error == -EILSEQ)))
Adrian Hunterbd11e8b2015-05-07 13:10:22 +0300992 mmc_retune_needed(host);
993
David Brownellaf517152007-08-08 09:11:32 -0700994 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 Hunter5163af52016-08-16 13:44:11 +0300999 if (host->ongoing_mrq == mrq)
1000 host->ongoing_mrq = NULL;
1001
1002 mmc_complete_cmd(mrq);
1003
Baolin Wang7962fc32016-03-31 11:16:27 +08001004 trace_mmc_request_done(host, mrq);
1005
Adrian Hunterd3049502011-11-28 16:22:00 +02001006 if (err && cmd->retries && !mmc_card_removed(host->card)) {
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001007 /*
1008 * Request starter must handle retries - see
1009 * mmc_wait_for_req_done().
1010 */
1011 if (mrq->done)
1012 mrq->done(mrq);
Pierre Ossmane4d21702007-07-24 21:46:49 +02001013 } else {
Per Forlin1b676f72011-08-19 14:52:37 +02001014 mmc_should_fail_request(host, mrq);
1015
Adrian Hunter5163af52016-08-16 13:44:11 +03001016 if (!host->ongoing_mrq)
1017 led_trigger_event(host->led, LED_OFF);
Pierre Ossmanaf8350c2007-09-24 07:15:48 +02001018
Andrew Gabbasovfc75b702014-10-01 07:14:10 -05001019 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 Ossmane4d21702007-07-24 21:46:49 +02001027 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 Mallavarapueb2bbc02010-06-11 18:13:05 +05301033#ifdef CONFIG_MMC_PERF_PROFILING
Subhash Jadavaniffb55e82012-03-25 11:15:41 +05301034 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 Mallavarapueb2bbc02010-06-11 18:13:05 +05301044 mrq->data->bytes_xfered;
Subhash Jadavaniffb55e82012-03-25 11:15:41 +05301045 host->perf.wtime_drv =
1046 ktime_add(host->perf.wtime_drv,
1047 diff);
1048 }
Aparna Mallavarapueb2bbc02010-06-11 18:13:05 +05301049 }
1050#endif
Pierre Ossmane4d21702007-07-24 21:46:49 +02001051 pr_debug("%s: %d bytes transferred: %d\n",
1052 mmc_hostname(host),
1053 mrq->data->bytes_xfered, mrq->data->error);
Mohan Srinivasan2320e1e2016-10-03 16:17:34 -07001054#ifdef CONFIG_BLOCK
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07001055 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 Srinivasan2320e1e2016-10-03 16:17:34 -07001066#endif
Pierre Ossmane4d21702007-07-24 21:46:49 +02001067 }
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 Jadavanidff31262016-04-19 16:21:16 -07001079
1080 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 }
1082}
1083
1084EXPORT_SYMBOL(mmc_request_done);
1085
Adrian Hunter90a81482015-05-07 13:10:14 +03001086static 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 Goede5d3f6ef2015-09-22 17:30:25 +02001098 /*
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 Hunter5163af52016-08-16 13:44:11 +03001115 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 Wang7962fc32016-03-31 11:16:27 +08001124 trace_mmc_request_start(host, mrq);
1125
Adrian Hunter90a81482015-05-07 13:10:14 +03001126 host->ops->request(host, mrq);
1127}
1128
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001129static int mmc_start_request(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
Pierre Ossman976d9272007-04-13 22:47:01 +02001131#ifdef CONFIG_MMC_DEBUG
1132 unsigned int i, sz;
Pierre Ossmana84756c2008-07-29 01:09:37 +02001133 struct scatterlist *sg;
Pierre Ossman976d9272007-04-13 22:47:01 +02001134#endif
Adrian Hunter90a81482015-05-07 13:10:14 +03001135 mmc_retune_hold(host);
1136
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001137 if (mmc_card_removed(host->card))
1138 return -ENOMEDIUM;
Pierre Ossman976d9272007-04-13 22:47:01 +02001139
Jaehoon Chung7b2fd4f2012-02-07 14:13:10 +09001140 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 King920e70c2006-05-04 18:22:51 +01001146 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 Torvalds1da177e2005-04-16 15:20:36 -07001149
Pierre Ossmane4d21702007-07-24 21:46:49 +02001150 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 Ossmance252ed2007-08-07 14:06:18 +02001155 mrq->data->timeout_ns / 1000000,
Pierre Ossmane4d21702007-07-24 21:46:49 +02001156 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 Ossmanf22ee4e2006-12-26 15:11:23 +01001165 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
1167 mrq->cmd->error = 0;
1168 mrq->cmd->mrq = mrq;
Andrew Gabbasovcce411e2014-10-01 07:14:09 -05001169 if (mrq->sbc) {
1170 mrq->sbc->error = 0;
1171 mrq->sbc->mrq = mrq;
1172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 if (mrq->data) {
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001174 BUG_ON(mrq->data->blksz > host->max_blk_size);
Pierre Ossman55db8902006-11-21 17:55:45 +01001175 BUG_ON(mrq->data->blocks > host->max_blk_count);
1176 BUG_ON(mrq->data->blocks * mrq->data->blksz >
1177 host->max_req_size);
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01001178
Pierre Ossman976d9272007-04-13 22:47:01 +02001179#ifdef CONFIG_MMC_DEBUG
1180 sz = 0;
Pierre Ossmana84756c2008-07-29 01:09:37 +02001181 for_each_sg(mrq->data->sg, sg, mrq->data->sg_len, i)
1182 sz += sg->length;
Pierre Ossman976d9272007-04-13 22:47:01 +02001183 BUG_ON(sz != mrq->data->blocks * mrq->data->blksz);
1184#endif
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 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 Mallavarapueb2bbc02010-06-11 18:13:05 +05301194#ifdef CONFIG_MMC_PERF_PROFILING
Subhash Jadavaniffb55e82012-03-25 11:15:41 +05301195 if (host->perf_enable)
1196 host->perf.start = ktime_get();
Aparna Mallavarapueb2bbc02010-06-11 18:13:05 +05301197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
Subhash Jadavanidff31262016-04-19 16:21:16 -07001199 mmc_host_clk_hold(host);
Pierre Tardy66c036e2011-02-06 19:02:48 +01001200 led_trigger_event(host->led, LED_FULL);
Sujit Reddy Thummacb18d852014-12-04 09:57:23 +02001201
Talel Shenhar8f004202015-08-18 02:28:56 +03001202 if (mmc_is_data_request(mrq)) {
1203 mmc_deferred_scaling(host);
Talel Shenhar7dc5f792015-05-18 12:12:48 +03001204 mmc_clk_scaling_start_busy(host, true);
Talel Shenhar8f004202015-08-18 02:28:56 +03001205 }
Sujit Reddy Thummacb18d852014-12-04 09:57:23 +02001206
Adrian Hunter90a81482015-05-07 13:10:14 +03001207 __mmc_start_request(host, mrq);
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001208
1209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Vijay Viswanath778491f2017-11-08 14:09:01 +05301212static 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
1245halt_failed:
1246 pr_debug("%s: %s: Retuning done err: %d\n",
1247 mmc_hostname(host), __func__, err);
1248
1249 return err;
1250}
1251
Veerabhadrarao Badiganti4efc0a62017-10-09 20:13:45 +05301252static int mmc_start_cmdq_request(struct mmc_host *host,
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001253 struct mmc_request *mrq)
1254{
Veerabhadrarao Badiganti4efc0a62017-10-09 20:13:45 +05301255 int ret = 0;
1256
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001257 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 Viswanath778491f2017-11-08 14:09:01 +05301278 mmc_cmdq_check_retune(host);
Veerabhadrarao Badiganti4efc0a62017-10-09 20:13:45 +05301279 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 Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001294}
1295
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001296/**
Dov Levenglickea221d42015-06-28 16:45:41 +03001297 * mmc_blk_init_bkops_statistics - initialize bkops statistics
1298 * @card: MMC card to start BKOPS
1299 *
1300 * Initialize and enable the bkops statistics
1301 */
1302void 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}
1323EXPORT_SYMBOL(mmc_blk_init_bkops_statistics);
1324
1325static 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
1333static 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
1341static 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
1349static 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
1357static 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 Levenglicka0296392015-06-24 19:51:58 +03001368 * mmc_set_auto_bkops - set auto BKOPS for supported cards
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001369 * @card: MMC card to start BKOPS
Dov Levenglicka0296392015-06-24 19:51:58 +03001370 * @enable: enable/disable flag
1371 * Configure the card to run automatic BKOPS.
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001372 *
Dov Levenglicka0296392015-06-24 19:51:58 +03001373 * Should be called when host is claimed.
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001374*/
Dov Levenglicka0296392015-06-24 19:51:58 +03001375int 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 Jadavani0a3b83c2016-05-12 11:58:15 -07001392 bkops_en = card->ext_csd.bkops_en | EXT_CSD_BKOPS_AUTO_EN;
Dov Levenglicka0296392015-06-24 19:51:58 +03001393 } else {
1394 if (!mmc_card_doing_auto_bkops(card))
1395 goto out;
Subhash Jadavani0a3b83c2016-05-12 11:58:15 -07001396 bkops_en = card->ext_csd.bkops_en & ~EXT_CSD_BKOPS_AUTO_EN;
Dov Levenglicka0296392015-06-24 19:51:58 +03001397 }
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 Levenglickea221d42015-06-28 16:45:41 +03001405 if (enable) {
Dov Levenglicka0296392015-06-24 19:51:58 +03001406 mmc_card_set_auto_bkops(card);
Dov Levenglickea221d42015-06-28 16:45:41 +03001407 mmc_update_bkops_auto_on(&card->bkops.stats);
1408 } else {
Dov Levenglicka0296392015-06-24 19:51:58 +03001409 mmc_card_clr_auto_bkops(card);
Dov Levenglickea221d42015-06-28 16:45:41 +03001410 mmc_update_bkops_auto_off(&card->bkops.stats);
1411 }
Subhash Jadavani0a3b83c2016-05-12 11:58:15 -07001412 card->ext_csd.bkops_en = bkops_en;
Konstantin Dorfman56380b12015-12-31 14:45:48 +02001413 pr_debug("%s: %s: bkops state %x\n",
1414 mmc_hostname(card->host), __func__, bkops_en);
Dov Levenglicka0296392015-06-24 19:51:58 +03001415 }
1416out:
1417 return ret;
1418}
1419EXPORT_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 */
1428void mmc_check_bkops(struct mmc_card *card)
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001429{
1430 int err;
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001431
1432 BUG_ON(!card);
1433
Konstantin Dorfman56380b12015-12-31 14:45:48 +02001434 if (mmc_card_doing_bkops(card))
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001435 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 Dorfman56380b12015-12-31 14:45:48 +02001444 card->bkops.needs_check = false;
1445
Dov Levenglickea221d42015-06-28 16:45:41 +03001446 mmc_update_bkops_level(&card->bkops.stats,
1447 card->ext_csd.raw_bkops_status);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001448
Konstantin Dorfman56380b12015-12-31 14:45:48 +02001449 card->bkops.needs_bkops = card->ext_csd.raw_bkops_status > 0;
Dov Levenglicka0296392015-06-24 19:51:58 +03001450}
1451EXPORT_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 Erez7ffa1632015-09-24 14:17:08 +03001457 * Send START_BKOPS to the card.
1458 * The function should be called with claimed host.
Dov Levenglicka0296392015-06-24 19:51:58 +03001459 */
1460void 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 Chung950d56a2012-09-17 08:42:02 +00001470 return;
1471
Adrian Hunter66073d82015-05-07 13:10:17 +03001472 mmc_retune_hold(card->host);
1473
Dov Levenglicka0296392015-06-24 19:51:58 +03001474 err = __mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BKOPS_START,
1475 1, 0, false, true, false);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001476 if (err) {
Dov Levenglicka0296392015-06-24 19:51:58 +03001477 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 Levenglickea221d42015-06-28 16:45:41 +03001481 mmc_update_bkops_start(&card->bkops.stats);
Konstantin Dorfman56380b12015-12-31 14:45:48 +02001482 card->bkops.needs_bkops = false;
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001483 }
1484
Dov Levenglicka0296392015-06-24 19:51:58 +03001485 mmc_retune_release(card->host);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001486}
Dov Levenglicka0296392015-06-24 19:51:58 +03001487EXPORT_SYMBOL(mmc_start_manual_bkops);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00001488
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001489/*
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 */
1495static void mmc_wait_data_done(struct mmc_request *mrq)
1496{
Sujit Reddy Thummad67ca6b2014-12-09 20:44:41 +02001497 unsigned long flags;
Jialing Fu71f8a4b2015-08-28 11:13:09 +08001498 struct mmc_context_info *context_info = &mrq->host->context_info;
1499
Sujit Reddy Thummad67ca6b2014-12-09 20:44:41 +02001500 spin_lock_irqsave(&context_info->lock, flags);
Jialing Fu71f8a4b2015-08-28 11:13:09 +08001501 context_info->is_done_rcv = true;
1502 wake_up_interruptible(&context_info->wait);
Sujit Reddy Thummad67ca6b2014-12-09 20:44:41 +02001503 spin_unlock_irqrestore(&context_info->lock, flags);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001504}
1505
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506static void mmc_wait_done(struct mmc_request *mrq)
1507{
Per Forlinaa8b6832011-07-01 18:55:22 +02001508 complete(&mrq->completion);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
Adrian Hunter5163af52016-08-16 13:44:11 +03001511static 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 Dorfman2220eed2013-01-14 14:28:17 -05001523/*
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 Hunter5163af52016-08-16 13:44:11 +03001530 * If an ongoing transfer is already in progress, wait for the command line
1531 * to become available before sending another command.
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001532 */
1533static int __mmc_start_data_req(struct mmc_host *host, struct mmc_request *mrq)
1534{
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001535 int err;
1536
Adrian Hunter5163af52016-08-16 13:44:11 +03001537 mmc_wait_ongoing_tfr_cmd(host);
1538
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001539 mrq->done = mmc_wait_data_done;
1540 mrq->host = host;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001541
Adrian Hunter5163af52016-08-16 13:44:11 +03001542 init_completion(&mrq->cmd_completion);
1543
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001544 err = mmc_start_request(host, mrq);
1545 if (err) {
1546 mrq->cmd->error = err;
Adrian Hunter5163af52016-08-16 13:44:11 +03001547 mmc_complete_cmd(mrq);
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001548 mmc_wait_data_done(mrq);
1549 }
1550
1551 return err;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001552}
1553
Ulf Hansson956d9fd2012-03-05 15:52:43 +01001554static int __mmc_start_req(struct mmc_host *host, struct mmc_request *mrq)
Per Forlinaa8b6832011-07-01 18:55:22 +02001555{
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001556 int err;
1557
Adrian Hunter5163af52016-08-16 13:44:11 +03001558 mmc_wait_ongoing_tfr_cmd(host);
1559
Per Forlinaa8b6832011-07-01 18:55:22 +02001560 init_completion(&mrq->completion);
1561 mrq->done = mmc_wait_done;
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001562
Adrian Hunter5163af52016-08-16 13:44:11 +03001563 init_completion(&mrq->cmd_completion);
1564
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001565 err = mmc_start_request(host, mrq);
1566 if (err) {
1567 mrq->cmd->error = err;
Adrian Hunter5163af52016-08-16 13:44:11 +03001568 mmc_complete_cmd(mrq);
Adrian Hunterd3049502011-11-28 16:22:00 +02001569 complete(&mrq->completion);
Adrian Hunterd3049502011-11-28 16:22:00 +02001570 }
Adrian Hunterf100c1c2014-12-05 19:41:02 +02001571
1572 return err;
Per Forlinaa8b6832011-07-01 18:55:22 +02001573}
1574
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001575/*
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 */
1586static 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 Thummad67ca6b2014-12-09 20:44:41 +02001593 bool is_done_rcv = false;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001594 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 Thummad67ca6b2014-12-09 20:44:41 +02001601 is_done_rcv = context_info->is_done_rcv;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001602 context_info->is_waiting_last_req = false;
1603 spin_unlock_irqrestore(&context_info->lock, flags);
Sujit Reddy Thummad67ca6b2014-12-09 20:44:41 +02001604 if (is_done_rcv) {
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001605 context_info->is_done_rcv = false;
1606 context_info->is_new_req = false;
1607 cmd = mrq->cmd;
Maya Erez775a9362013-04-18 15:41:55 +03001608
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001609 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 Hunter90a81482015-05-07 13:10:14 +03001615 mmc_retune_recheck(host);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001616 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 Hunter90a81482015-05-07 13:10:14 +03001621 __mmc_start_request(host, mrq);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001622 continue; /* wait for done/new event again */
1623 }
1624 } else if (context_info->is_new_req) {
1625 context_info->is_new_req = false;
Adrian Hunter90a81482015-05-07 13:10:14 +03001626 if (!next_req)
1627 return MMC_BLK_NEW_REQUEST;
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001628 }
1629 }
Adrian Hunter90a81482015-05-07 13:10:14 +03001630 mmc_retune_release(host);
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001631 return err;
1632}
1633
Adrian Hunter5163af52016-08-16 13:44:11 +03001634void mmc_wait_for_req_done(struct mmc_host *host, struct mmc_request *mrq)
Per Forlinaa8b6832011-07-01 18:55:22 +02001635{
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001636 struct mmc_command *cmd;
1637
1638 while (1) {
Subhash Jadavani9151c532012-07-13 10:51:37 +05301639 wait_for_completion_io(&mrq->completion);
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001640
1641 cmd = mrq->cmd;
Maya Erez775a9362013-04-18 15:41:55 +03001642
1643 /*
Subhash Jadavani7ad7ea82014-12-04 15:07:32 +02001644 * If host has timed out waiting for the sanitize/bkops
Maya Erez775a9362013-04-18 15:41:55 +03001645 * 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 Jadavani7ad7ea82014-12-04 15:07:32 +02001649 if ((cmd->bkops_busy || cmd->sanitize_busy) && cmd->error == -ETIMEDOUT) {
Maya Erez775a9362013-04-18 15:41:55 +03001650 if (!mmc_interrupt_hpi(host->card)) {
Subhash Jadavani7ad7ea82014-12-04 15:07:32 +02001651 pr_warn("%s: %s: Interrupted sanitize/bkops\n",
1652 mmc_hostname(host), __func__);
Maya Erez775a9362013-04-18 15:41:55 +03001653 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 Hunterd3049502011-11-28 16:22:00 +02001660 if (!cmd->error || !cmd->retries ||
Can Guo6514fbe2017-07-04 12:36:09 +08001661 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 Hunter08a7e1d2011-10-03 15:33:33 +03001666 break;
Can Guo6514fbe2017-07-04 12:36:09 +08001667 }
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001668
Adrian Hunter90a81482015-05-07 13:10:14 +03001669 mmc_retune_recheck(host);
1670
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001671 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 Hunter90a81482015-05-07 13:10:14 +03001675 __mmc_start_request(host, mrq);
Adrian Hunter08a7e1d2011-10-03 15:33:33 +03001676 }
Adrian Hunter90a81482015-05-07 13:10:14 +03001677
1678 mmc_retune_release(host);
Per Forlinaa8b6832011-07-01 18:55:22 +02001679}
Adrian Hunter5163af52016-08-16 13:44:11 +03001680EXPORT_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 */
1694bool 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}
1701EXPORT_SYMBOL(mmc_is_req_done);
Per Forlinaa8b6832011-07-01 18:55:22 +02001702
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 */
1714static void mmc_pre_req(struct mmc_host *host, struct mmc_request *mrq,
1715 bool is_first_req)
1716{
Subhash Jadavanidff31262016-04-19 16:21:16 -07001717 if (host->ops->pre_req) {
1718 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +02001719 host->ops->pre_req(host, mrq, is_first_req);
Subhash Jadavanidff31262016-04-19 16:21:16 -07001720 mmc_host_clk_release(host);
1721 }
Per Forlinaa8b6832011-07-01 18:55:22 +02001722}
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 */
1733static void mmc_post_req(struct mmc_host *host, struct mmc_request *mrq,
1734 int err)
1735{
Subhash Jadavanidff31262016-04-19 16:21:16 -07001736 if (host->ops->post_req) {
1737 mmc_host_clk_hold(host);
Per Forlinaa8b6832011-07-01 18:55:22 +02001738 host->ops->post_req(host, mrq, err);
Subhash Jadavanidff31262016-04-19 16:21:16 -07001739 mmc_host_clk_release(host);
1740 }
Per Forlinaa8b6832011-07-01 18:55:22 +02001741}
1742
1743/**
Asutosh Das02e30862015-05-20 16:52:04 +05301744 * 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 */
1749int mmc_cmdq_discard_queue(struct mmc_host *host, u32 tasks)
1750{
1751 return mmc_discard_queue(host, tasks);
1752}
1753EXPORT_SYMBOL(mmc_cmdq_discard_queue);
1754
1755
1756/**
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001757 * mmc_cmdq_post_req - post process of a completed request
1758 * @host: host instance
Ritesh Harjani121cdec2015-09-27 21:51:01 +05301759 * @tag: the request tag.
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001760 * @err: non-zero is error, success otherwise
1761 */
Ritesh Harjani121cdec2015-09-27 21:51:01 +05301762void mmc_cmdq_post_req(struct mmc_host *host, int tag, int err)
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001763{
1764 if (likely(host->cmdq_ops->post_req))
Ritesh Harjani121cdec2015-09-27 21:51:01 +05301765 host->cmdq_ops->post_req(host, tag, err);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001766}
1767EXPORT_SYMBOL(mmc_cmdq_post_req);
1768
Asutosh Das31150ae2015-05-21 17:21:07 +05301769/**
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 */
1780int mmc_cmdq_halt(struct mmc_host *host, bool halt)
1781{
1782 int err = 0;
1783
Sahitya Tummalae79f4f42016-04-26 15:00:04 +05301784 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 Das31150ae2015-05-21 17:21:07 +05301790 if ((halt && mmc_host_halt(host)) ||
Konstantin Dorfman05a9a492015-06-28 10:25:55 +03001791 (!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 Harjanib431b3f2015-05-19 14:27:34 +05301797 mmc_host_clk_hold(host);
Asutosh Das31150ae2015-05-21 17:21:07 +05301798 if (host->cmdq_ops->halt) {
1799 err = host->cmdq_ops->halt(host, halt);
Talel Shenhare68741c2015-06-25 09:33:24 +03001800 if (!err && host->ops->notify_halt)
1801 host->ops->notify_halt(host, halt);
Asutosh Das31150ae2015-05-21 17:21:07 +05301802 if (!err && halt)
1803 mmc_host_set_halt(host);
Gilad Broner4df18962015-08-13 17:58:30 +03001804 else if (!err && !halt) {
Asutosh Das31150ae2015-05-21 17:21:07 +05301805 mmc_host_clr_halt(host);
Subhash Jadavani987144c2015-08-12 17:53:21 -07001806 wake_up(&host->cmdq_ctx.wait);
Gilad Broner4df18962015-08-13 17:58:30 +03001807 }
Asutosh Das31150ae2015-05-21 17:21:07 +05301808 } else {
1809 err = -ENOSYS;
1810 }
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05301811 mmc_host_clk_release(host);
Asutosh Das31150ae2015-05-21 17:21:07 +05301812 return err;
1813}
1814EXPORT_SYMBOL(mmc_cmdq_halt);
1815
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001816int 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 Badiganti4efc0a62017-10-09 20:13:45 +05301825 return mmc_start_cmdq_request(host, mrq);
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001826}
1827EXPORT_SYMBOL(mmc_cmdq_start_req);
1828
Sahitya Tummala9433a132015-06-09 09:38:36 +05301829static void mmc_cmdq_dcmd_req_done(struct mmc_request *mrq)
1830{
Ritesh Harjanib431b3f2015-05-19 14:27:34 +05301831 mmc_host_clk_release(mrq->host);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301832 complete(&mrq->completion);
1833}
1834
1835int 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 Dorfman9a9c9fd2015-06-30 17:39:43 +03001854 mmc_host_clk_hold(host);
1855 host->cmdq_ops->dumpstate(host);
1856 mmc_host_clk_release(host);
Sahitya Tummala9433a132015-06-09 09:38:36 +05301857 }
1858 return err;
1859}
1860EXPORT_SYMBOL(mmc_cmdq_wait_for_dcmd);
1861
Asutosh Das5238e022015-04-23 16:00:45 +05301862int 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}
1868EXPORT_SYMBOL(mmc_cmdq_prepare_flush);
1869
Venkat Gopalakrishnane95d7bf2015-05-29 16:51:43 -07001870/**
Per Forlinaa8b6832011-07-01 18:55:22 +02001871 * 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 */
1886struct mmc_async_req *mmc_start_req(struct mmc_host *host,
1887 struct mmc_async_req *areq, int *error)
1888{
1889 int err = 0;
Ulf Hansson956d9fd2012-03-05 15:52:43 +01001890 int start_err = 0;
Per Forlinaa8b6832011-07-01 18:55:22 +02001891 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 Chungf5c27582013-02-01 14:32:22 +09001898 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 Chung950d56a2012-09-17 08:42:02 +00001908 /*
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 Kandagatla64b12a62014-10-08 12:24:24 +01001914 (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 Levenglicka0296392015-06-24 19:51:58 +03001920 mmc_check_bkops(host->card);
Srinivas Kandagatla64b12a62014-10-08 12:24:24 +01001921
1922 /* prepare the request again */
1923 if (areq)
1924 mmc_pre_req(host, areq->mrq, !host->areq);
1925 }
Per Forlinaa8b6832011-07-01 18:55:22 +02001926 }
1927
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07001928 if (!err && areq) {
Mohan Srinivasan2320e1e2016-10-03 16:17:34 -07001929#ifdef CONFIG_BLOCK
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07001930 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 Srinivasan2320e1e2016-10-03 16:17:34 -07001935#endif
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05001936 start_err = __mmc_start_data_req(host, areq->mrq);
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07001937 }
Per Forlinaa8b6832011-07-01 18:55:22 +02001938
1939 if (host->areq)
1940 mmc_post_req(host, host->areq->mrq, 0);
1941
Subhash Jadavania7b703a2015-10-14 19:04:26 -07001942 if (err && areq)
Jaehoon Chungf5c27582013-02-01 14:32:22 +09001943 mmc_post_req(host, areq->mrq, -EINVAL);
Ulf Hansson956d9fd2012-03-05 15:52:43 +01001944
1945 if (err)
1946 host->areq = NULL;
1947 else
1948 host->areq = areq;
1949
Per Forlinaa8b6832011-07-01 18:55:22 +02001950 if (error)
1951 *error = err;
1952 return data;
1953}
1954EXPORT_SYMBOL(mmc_start_req);
1955
Pierre Ossman67a61c42007-07-11 20:22:11 +02001956/**
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 Hunter5163af52016-08-16 13:44:11 +03001962 * 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 Ossman67a61c42007-07-11 20:22:11 +02001967 */
1968void mmc_wait_for_req(struct mmc_host *host, struct mmc_request *mrq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969{
Asutosh Dase1f8f972013-10-14 11:35:19 +05301970#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1971 if (mmc_bus_needs_resume(host))
1972 mmc_resume_bus(host);
1973#endif
Per Forlinaa8b6832011-07-01 18:55:22 +02001974 __mmc_start_req(host, mrq);
Adrian Hunter5163af52016-08-16 13:44:11 +03001975
1976 if (!mrq->cap_cmd_during_tfr)
1977 mmc_wait_for_req_done(host, mrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979EXPORT_SYMBOL(mmc_wait_for_req);
1980
1981/**
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -04001982 * 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 Chung950d56a2012-09-17 08:42:02 +00001986 * until out-of prg-state.
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -04001987 */
1988int mmc_interrupt_hpi(struct mmc_card *card)
1989{
1990 int err;
1991 u32 status;
Venkatraman S6af9e962012-06-22 11:42:36 +05301992 unsigned long prg_wait;
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -04001993
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 S6af9e962012-06-22 11:42:36 +05302008 switch (R1_CURRENT_STATE(status)) {
2009 case R1_STATE_IDLE:
2010 case R1_STATE_READY:
2011 case R1_STATE_STBY:
Venkatraman S211d4fe2012-08-07 19:24:45 +05302012 case R1_STATE_TRAN:
Venkatraman S6af9e962012-06-22 11:42:36 +05302013 /*
Venkatraman S211d4fe2012-08-07 19:24:45 +05302014 * In idle and transfer states, HPI is not needed and the caller
Venkatraman S6af9e962012-06-22 11:42:36 +05302015 * 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 Chungeb0d8f12011-10-18 01:26:42 -04002027
Venkatraman S6af9e962012-06-22 11:42:36 +05302028 err = mmc_send_hpi_cmd(card, &status);
Venkatraman S6af9e962012-06-22 11:42:36 +05302029
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 Dorfmancfad32e2013-05-13 15:00:21 +03002036 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 S6af9e962012-06-22 11:42:36 +05302043 } while (!err);
Jaehoon Chungeb0d8f12011-10-18 01:26:42 -04002044
2045out:
2046 mmc_release_host(card->host);
2047 return err;
2048}
2049EXPORT_SYMBOL(mmc_interrupt_hpi);
2050
2051/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 * 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 */
2061int mmc_wait_for_cmd(struct mmc_host *host, struct mmc_command *cmd, int retries)
2062{
Venkatraman Sad5fd972011-08-25 00:30:50 +05302063 struct mmc_request mrq = {NULL};
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
Pierre Ossmand84075c82007-08-09 13:23:56 +02002065 WARN_ON(!host->claimed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 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
2078EXPORT_SYMBOL(mmc_wait_for_cmd);
2079
Pierre Ossman335eadf2005-09-06 15:18:50 -07002080/**
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002081 * 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 */
2089int mmc_stop_bkops(struct mmc_card *card)
2090{
2091 int err = 0;
2092
2093 BUG_ON(!card);
Dov Levenglicka0296392015-06-24 19:51:58 +03002094 if (unlikely(!mmc_card_configured_manual_bkops(card)))
2095 goto out;
2096 if (!mmc_card_doing_bkops(card))
2097 goto out;
2098
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002099 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 Levenglickea221d42015-06-28 16:45:41 +03002107 mmc_update_bkops_hpi(&card->bkops.stats);
Adrian Hunter66073d82015-05-07 13:10:17 +03002108 mmc_retune_release(card->host);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002109 err = 0;
2110 }
Dov Levenglicka0296392015-06-24 19:51:58 +03002111out:
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002112 return err;
2113}
2114EXPORT_SYMBOL(mmc_stop_bkops);
2115
2116int mmc_read_bkops_status(struct mmc_card *card)
2117{
2118 int err;
2119 u8 *ext_csd;
2120
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002121 mmc_claim_host(card->host);
Ulf Hanssonb2cada72014-10-17 11:48:23 +02002122 err = mmc_get_ext_csd(card, &ext_csd);
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002123 mmc_release_host(card->host);
2124 if (err)
Ulf Hanssonb2cada72014-10-17 11:48:23 +02002125 return err;
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002126
Asutosh Das79ccfa52016-04-21 14:15:15 +05302127 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 Chung950d56a2012-09-17 08:42:02 +00002135 kfree(ext_csd);
Ulf Hanssonb2cada72014-10-17 11:48:23 +02002136 return 0;
Jaehoon Chung950d56a2012-09-17 08:42:02 +00002137}
2138EXPORT_SYMBOL(mmc_read_bkops_status);
2139
2140/**
Russell Kingd773d722006-09-07 15:57:12 +01002141 * 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 Ossman67a61c42007-07-11 20:22:11 +02002144 *
2145 * Computes the data timeout parameters according to the
2146 * correct algorithm given the card type.
Russell Kingd773d722006-09-07 15:57:12 +01002147 */
Pierre Ossmanb146d262007-07-24 19:16:54 +02002148void mmc_set_data_timeout(struct mmc_data *data, const struct mmc_card *card)
Russell Kingd773d722006-09-07 15:57:12 +01002149{
2150 unsigned int mult;
2151
Asutosh Dasbbefab32013-10-07 14:53:32 +05302152 if (!card) {
2153 WARN_ON(1);
2154 return;
2155 }
Russell Kingd773d722006-09-07 15:57:12 +01002156 /*
Pierre Ossmane6f918b2007-08-07 14:11:55 +02002157 * 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 Kingd773d722006-09-07 15:57:12 +01002166 * 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 Ossmanb146d262007-07-24 19:16:54 +02002174 if (data->flags & MMC_DATA_WRITE)
Russell Kingd773d722006-09-07 15:57:12 +01002175 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 Jadavanidff31262016-04-19 16:21:16 -07002187 if (mmc_host_clk_rate(card->host))
Linus Walleije9b86842011-01-05 00:44:32 +01002188 timeout_us += data->timeout_clks * 1000 /
Subhash Jadavanidff31262016-04-19 16:21:16 -07002189 (mmc_host_clk_rate(card->host) / 1000);
Russell Kingd773d722006-09-07 15:57:12 +01002190
Pierre Ossmanb146d262007-07-24 19:16:54 +02002191 if (data->flags & MMC_DATA_WRITE)
Pierre Ossman493890e2008-10-26 12:37:25 +01002192 /*
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -06002193 * 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 Ossman493890e2008-10-26 12:37:25 +01002199 */
Paul Walmsley3bdc9ba2012-03-12 04:58:00 -06002200 limit_us = 3000000;
Russell Kingd773d722006-09-07 15:57:12 +01002201 else
2202 limit_us = 100000;
2203
Philip Langdalefba68bd2007-01-04 06:57:32 -08002204 /*
2205 * SDHC cards always use these fixed values.
2206 */
2207 if (timeout_us > limit_us || mmc_card_blockaddr(card)) {
Russell Kingd773d722006-09-07 15:57:12 +01002208 data->timeout_ns = limit_us * 1000;
2209 data->timeout_clks = 0;
2210 }
Stefan Wahrenf7bf11a2014-04-03 17:32:05 +02002211
2212 /* assign limit value if invalid */
2213 if (timeout_us == 0)
2214 data->timeout_ns = limit_us * 1000;
Russell Kingd773d722006-09-07 15:57:12 +01002215 }
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002216
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 Gumbel32ecd322016-05-20 10:33:46 +03002220 * value. For the cards tested, 600ms has proven enough. If necessary,
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002221 * this value can be increased if other problematic cards require this.
Ritesh Harjanie11a1b72016-02-25 17:54:58 +05302222 * Certain Hynix 5.x cards giving read timeout even with 300ms.
2223 * Increasing further to max value (4s).
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002224 */
2225 if (mmc_card_long_read_time(card) && data->flags & MMC_DATA_READ) {
Ritesh Harjanie11a1b72016-02-25 17:54:58 +05302226 data->timeout_ns = 4000000000u;
Stefan Nilsson XK6de5fc92011-11-03 09:44:12 +01002227 data->timeout_clks = 0;
2228 }
2229
Wolfgang Mueesc0c88872009-03-11 14:28:39 +01002230 /*
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 V8d664e32014-12-03 18:26:42 +02002245 /* 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 Kingd773d722006-09-07 15:57:12 +01002250}
2251EXPORT_SYMBOL(mmc_set_data_timeout);
2252
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253/**
Pierre Ossmanad3868b2008-06-28 12:52:45 +02002254 * 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 */
2267unsigned 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}
2278EXPORT_SYMBOL(mmc_align_data_size);
2279
2280/**
Nicolas Pitre2342f332007-06-30 16:21:52 +02002281 * __mmc_claim_host - exclusively claim a host
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * @host: mmc host to claim
Nicolas Pitre2342f332007-06-30 16:21:52 +02002283 * @abort: whether or not the operation should be aborted
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 *
Nicolas Pitre2342f332007-06-30 16:21:52 +02002285 * 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 Torvalds1da177e2005-04-16 15:20:36 -07002289 */
Nicolas Pitre2342f332007-06-30 16:21:52 +02002290int __mmc_claim_host(struct mmc_host *host, atomic_t *abort)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291{
2292 DECLARE_WAITQUEUE(wait, current);
2293 unsigned long flags;
Nicolas Pitre2342f332007-06-30 16:21:52 +02002294 int stop;
Ulf Hansson9250aea2015-03-27 12:15:15 +01002295 bool pm = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Pierre Ossmancf795bf2007-07-11 20:28:02 +02002297 might_sleep();
2298
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 add_wait_queue(&host->wq, &wait);
Sujith Reddy Thumma1547ccb2011-02-17 21:37:48 +05302300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 spin_lock_irqsave(&host->lock, flags);
2302 while (1) {
2303 set_current_state(TASK_UNINTERRUPTIBLE);
Nicolas Pitre2342f332007-06-30 16:21:52 +02002304 stop = abort ? atomic_read(abort) : 0;
Adrian Hunter319a3f12009-09-22 16:44:30 -07002305 if (stop || !host->claimed || host->claimer == current)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 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 Hunter319a3f12009-09-22 16:44:30 -07002312 if (!stop) {
Nicolas Pitre2342f332007-06-30 16:21:52 +02002313 host->claimed = 1;
Adrian Hunter319a3f12009-09-22 16:44:30 -07002314 host->claimer = current;
2315 host->claim_cnt += 1;
Ulf Hansson9250aea2015-03-27 12:15:15 +01002316 if (host->claim_cnt == 1)
2317 pm = true;
Adrian Hunter319a3f12009-09-22 16:44:30 -07002318 } else
Nicolas Pitre2342f332007-06-30 16:21:52 +02002319 wake_up(&host->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 spin_unlock_irqrestore(&host->lock, flags);
2321 remove_wait_queue(&host->wq, &wait);
Ulf Hansson9250aea2015-03-27 12:15:15 +01002322
2323 if (pm)
2324 pm_runtime_get_sync(mmc_dev(host));
2325
Subhash Jadavani480a79e2016-04-27 17:19:29 -07002326 if (host->ops->enable && !stop && host->claim_cnt == 1)
2327 host->ops->enable(host);
2328
Nicolas Pitre2342f332007-06-30 16:21:52 +02002329 return stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330}
Nicolas Pitre2342f332007-06-30 16:21:52 +02002331EXPORT_SYMBOL(__mmc_claim_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Adrian Hunter319a3f12009-09-22 16:44:30 -07002333/**
Veerabhadrarao Badigantibf795c52017-02-09 20:23:17 +05302334 * 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 */
2341int 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 Badiganti36371a12017-04-04 12:38:24 +05302346 bool pm = false;
Veerabhadrarao Badigantibf795c52017-02-09 20:23:17 +05302347
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 Badiganti36371a12017-04-04 12:38:24 +05302355 if (host->claim_cnt == 1)
2356 pm = true;
Veerabhadrarao Badigantibf795c52017-02-09 20:23:17 +05302357 }
2358 spin_unlock_irqrestore(&host->lock, flags);
2359 if (!claimed_host)
2360 mmc_delay(10);
2361 } while (!claimed_host && retry_cnt--);
Veerabhadrarao Badiganti36371a12017-04-04 12:38:24 +05302362
2363 if (pm)
2364 pm_runtime_get_sync(mmc_dev(host));
2365
Veerabhadrarao Badigantibf795c52017-02-09 20:23:17 +05302366 if (host->ops->enable && claimed_host && host->claim_cnt == 1)
2367 host->ops->enable(host);
2368 return claimed_host;
2369}
2370EXPORT_SYMBOL(mmc_try_claim_host);
2371
2372/**
Adrian Hunter907d2e72012-02-29 09:17:21 +02002373 * mmc_release_host - release a host
Ulf Hanssonab1efd22011-03-09 09:11:02 +01002374 * @host: mmc host to release
2375 *
Adrian Hunter907d2e72012-02-29 09:17:21 +02002376 * Release a MMC host, allowing others to claim the host
2377 * for their operations.
Ulf Hanssonab1efd22011-03-09 09:11:02 +01002378 */
Adrian Hunter907d2e72012-02-29 09:17:21 +02002379void mmc_release_host(struct mmc_host *host)
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002380{
2381 unsigned long flags;
2382
Adrian Hunter907d2e72012-02-29 09:17:21 +02002383 WARN_ON(!host->claimed);
2384
Subhash Jadavani480a79e2016-04-27 17:19:29 -07002385 if (host->ops->disable && host->claim_cnt == 1)
2386 host->ops->disable(host);
2387
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002388 spin_lock_irqsave(&host->lock, flags);
Adrian Hunter319a3f12009-09-22 16:44:30 -07002389 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 Hansson9250aea2015-03-27 12:15:15 +01002397 pm_runtime_mark_last_busy(mmc_dev(host));
2398 pm_runtime_put_autosuspend(mmc_dev(host));
Adrian Hunter319a3f12009-09-22 16:44:30 -07002399 }
Adrian Hunter8ea926b2009-09-22 16:44:29 -07002400}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401EXPORT_SYMBOL(mmc_release_host);
2402
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002403/*
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002404 * This is a helper function, which fetches a runtime pm reference for the
2405 * card device and also claims the host.
2406 */
2407void mmc_get_card(struct mmc_card *card)
2408{
2409 pm_runtime_get_sync(&card->dev);
2410 mmc_claim_host(card->host);
Sahitya Tummalafc9ccd62016-11-30 09:00:53 +05302411#ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2412 if (mmc_bus_needs_resume(card->host))
2413 mmc_resume_bus(card->host);
2414#endif
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002415}
2416EXPORT_SYMBOL(mmc_get_card);
2417
Dov Levenglicka0296392015-06-24 19:51:58 +03002418
2419/*
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002420 * This is a helper function, which releases the host and drops the runtime
2421 * pm reference for the card device.
2422 */
2423void mmc_put_card(struct mmc_card *card)
2424{
2425 mmc_release_host(card->host);
2426 pm_runtime_mark_last_busy(&card->dev);
Konstantin Dorfman5dd9f862015-12-31 13:25:40 +02002427 pm_runtime_put_autosuspend(&card->dev);
Ulf Hanssone94cfef2013-05-02 14:02:38 +02002428}
2429EXPORT_SYMBOL(mmc_put_card);
2430
2431/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002432 * Internal function that does the actual ios call to the host driver,
2433 * optionally printing some debug output.
2434 */
Sujit Reddy Thumma8ca66b42014-12-03 18:10:07 +02002435void mmc_set_ios(struct mmc_host *host)
Russell King920e70c2006-05-04 18:22:51 +01002436{
2437 struct mmc_ios *ios = &host->ios;
2438
Pierre Ossmancd9277c2007-02-18 12:07:47 +01002439 pr_debug("%s: clock %uHz busmode %u powermode %u cs %u Vdd %u "
2440 "width %u timing %u\n",
Russell King920e70c2006-05-04 18:22:51 +01002441 mmc_hostname(host), ios->clock, ios->bus_mode,
2442 ios->power_mode, ios->chip_select, ios->vdd,
Wolfram Sanged9feec2016-01-29 09:27:50 +01002443 1 << ios->bus_width, ios->timing);
Philip Langdalefba68bd2007-01-04 06:57:32 -08002444
Subhash Jadavanidff31262016-04-19 16:21:16 -07002445 if (ios->clock > 0)
2446 mmc_set_ungated(host);
Russell King920e70c2006-05-04 18:22:51 +01002447 host->ops->set_ios(host, ios);
Sujit Reddy Thumma14ec3cc2013-02-14 08:13:52 +05302448 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 King920e70c2006-05-04 18:22:51 +01002461}
Sujit Reddy Thumma8ca66b42014-12-03 18:10:07 +02002462EXPORT_SYMBOL(mmc_set_ios);
Russell King920e70c2006-05-04 18:22:51 +01002463
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002464/*
2465 * Control chip select pin on a host.
2466 */
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01002467void mmc_set_chip_select(struct mmc_host *host, int mode)
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07002468{
Subhash Jadavanidff31262016-04-19 16:21:16 -07002469 mmc_host_clk_hold(host);
Pierre Ossmanda7fbe52006-12-24 22:46:55 +01002470 host->ios.chip_select = mode;
2471 mmc_set_ios(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07002472 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
2475/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002476 * Sets the host clock to the highest possible frequency that
2477 * is below "hz".
2478 */
Subhash Jadavanidff31262016-04-19 16:21:16 -07002479static void __mmc_set_clock(struct mmc_host *host, unsigned int hz)
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002480{
Adrian Hunter6a98f1e2014-09-23 23:00:26 +03002481 WARN_ON(hz && hz < host->f_min);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002482
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 Jadavanidff31262016-04-19 16:21:16 -07002490void 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 */
2501void mmc_gate_clock(struct mmc_host *host)
2502{
2503 unsigned long flags;
2504
Sujit Reddy Thumma3a75608d2014-12-03 18:13:58 +02002505 WARN_ON(!host->ios.clock);
2506
Subhash Jadavanidff31262016-04-19 16:21:16 -07002507 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 */
2519void 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 Thumma3a75608d2014-12-03 18:13:58 +02002529 WARN_ON(host->ios.clock);
Subhash Jadavanidff31262016-04-19 16:21:16 -07002530 /* This call will also set host->clk_gated to false */
2531 __mmc_set_clock(host, host->clk_old);
Veerabhadrarao Badiganti3ae97c72017-05-18 20:24:31 +05302532 /*
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 Jadavanidff31262016-04-19 16:21:16 -07002539 }
2540}
2541
2542void 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
2556void mmc_set_ungated(struct mmc_host *host)
2557{
2558}
Ritesh Harjanice4b8f82015-11-25 10:37:21 +05302559
2560void mmc_gate_clock(struct mmc_host *host)
2561{
2562}
Subhash Jadavanidff31262016-04-19 16:21:16 -07002563#endif
2564
Adrian Hunter63e415c2014-12-05 19:40:59 +02002565int 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 Jadavanidff31262016-04-19 16:21:16 -07002579 mmc_host_clk_hold(host);
Adrian Hunter63e415c2014-12-05 19:40:59 +02002580 err = host->ops->execute_tuning(host, opcode);
Subhash Jadavanidff31262016-04-19 16:21:16 -07002581 mmc_host_clk_release(host);
Adrian Hunter63e415c2014-12-05 19:40:59 +02002582
2583 if (err)
Russell King07d97d82016-01-29 09:44:05 +00002584 pr_err("%s: tuning execution failed: %d\n",
2585 mmc_hostname(host), err);
Adrian Hunter79d5a652015-05-07 13:10:13 +03002586 else
2587 mmc_retune_enable(host);
Adrian Hunter63e415c2014-12-05 19:40:59 +02002588
2589 return err;
2590}
2591
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002592/*
2593 * Change the bus mode (open drain/push-pull) of a host.
2594 */
2595void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode)
2596{
Subhash Jadavanidff31262016-04-19 16:21:16 -07002597 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002598 host->ios.bus_mode = mode;
2599 mmc_set_ios(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07002600 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002601}
2602
2603/*
2604 * Change data bus width of a host.
2605 */
2606void mmc_set_bus_width(struct mmc_host *host, unsigned int width)
2607{
Subhash Jadavanidff31262016-04-19 16:21:16 -07002608 mmc_host_clk_hold(host);
Philip Rakity4c4cb172011-05-13 11:17:18 +05302609 host->ios.bus_width = width;
2610 mmc_set_ios(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07002611 mmc_host_clk_release(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01002612}
2613
Johan Rudholm2d079c42014-11-06 14:46:54 +01002614/*
2615 * Set initial state after a power cycle or a hw_reset.
2616 */
2617void mmc_set_initial_state(struct mmc_host *host)
2618{
Adrian Hunter79d5a652015-05-07 13:10:13 +03002619 mmc_retune_disable(host);
2620
Johan Rudholm2d079c42014-11-06 14:46:54 +01002621 if (mmc_host_is_spi(host))
2622 host->ios.chip_select = MMC_CS_HIGH;
Author: Stephen Boydc0432a12014-12-03 19:49:13 +02002623 else {
Johan Rudholm2d079c42014-11-06 14:46:54 +01002624 host->ios.chip_select = MMC_CS_DONTCARE;
Author: Stephen Boydc0432a12014-12-03 19:49:13 +02002625 host->ios.bus_mode = MMC_BUSMODE_OPENDRAIN;
2626 }
Johan Rudholm2d079c42014-11-06 14:46:54 +01002627 host->ios.bus_width = MMC_BUS_WIDTH_1;
2628 host->ios.timing = MMC_TIMING_LEGACY;
Adrian Hunter75e8a222015-02-06 14:12:51 +02002629 host->ios.drv_type = 0;
Shawn Lin81ac2af2016-05-26 09:56:22 +08002630 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 Rudholm2d079c42014-11-06 14:46:54 +01002639
2640 mmc_set_ios(host);
2641}
2642
Anton Vorontsov86e82862008-11-26 22:54:17 +03002643/**
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 */
2658static 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 */
2692u32 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}
2715EXPORT_SYMBOL(mmc_vddrange_to_ocrmask);
2716
Haijun Zhang6e9e3182013-08-26 09:19:22 +08002717#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 Kingcf925742016-01-29 09:43:55 +00002724 * 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 Zhang6e9e3182013-08-26 09:19:22 +08002727 */
2728int 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 King10a16a02016-01-29 09:43:50 +00002735 if (!voltage_ranges) {
2736 pr_debug("%s: voltage-ranges unspecified\n", np->full_name);
Russell Kingcf925742016-01-29 09:43:55 +00002737 return 0;
Russell King10a16a02016-01-29 09:43:50 +00002738 }
2739 if (!num_ranges) {
2740 pr_err("%s: voltage-ranges empty\n", np->full_name);
Haijun Zhang6e9e3182013-08-26 09:19:22 +08002741 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 Kingcf925742016-01-29 09:43:55 +00002759 return 1;
Haijun Zhang6e9e3182013-08-26 09:19:22 +08002760}
2761EXPORT_SYMBOL(mmc_of_parse_voltage);
2762
2763#endif /* CONFIG_OF */
2764
Sascha Hauer25185f32014-06-30 11:07:25 +02002765static 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", &reg);
2771 if (ret < 0)
2772 return ret;
2773
2774 return reg;
2775}
2776
2777struct 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 Brownell5c139412009-03-11 03:30:43 -08002793#ifdef CONFIG_REGULATOR
2794
2795/**
Heiko Stuebner310c8052015-10-12 18:00:54 +02002796 * 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 */
2804static 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 Brownell5c139412009-03-11 03:30:43 -08002830 * 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 */
2838int mmc_regulator_get_ocrmask(struct regulator *supply)
2839{
2840 int result = 0;
2841 int count;
2842 int i;
Javier Martinez Canillas9ed7ca82014-08-14 14:39:00 +02002843 int vdd_uV;
2844 int vdd_mV;
David Brownell5c139412009-03-11 03:30:43 -08002845
2846 count = regulator_count_voltages(supply);
2847 if (count < 0)
2848 return count;
2849
2850 for (i = 0; i < count; i++) {
David Brownell5c139412009-03-11 03:30:43 -08002851 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 Canillas9ed7ca82014-08-14 14:39:00 +02002859 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 Brownell5c139412009-03-11 03:30:43 -08002868 return result;
2869}
Chris Ball45a6b322012-06-11 09:39:12 -04002870EXPORT_SYMBOL_GPL(mmc_regulator_get_ocrmask);
David Brownell5c139412009-03-11 03:30:43 -08002871
2872/**
2873 * mmc_regulator_set_ocr - set regulator to match host->ios voltage
Linus Walleij99fc5132010-09-29 01:08:27 -04002874 * @mmc: the host to regulate
David Brownell5c139412009-03-11 03:30:43 -08002875 * @supply: regulator to use
Linus Walleij99fc5132010-09-29 01:08:27 -04002876 * @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
David Brownell5c139412009-03-11 03:30:43 -08002877 *
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 Walleij99fc5132010-09-29 01:08:27 -04002884int mmc_regulator_set_ocr(struct mmc_host *mmc,
2885 struct regulator *supply,
2886 unsigned short vdd_bit)
David Brownell5c139412009-03-11 03:30:43 -08002887{
2888 int result = 0;
2889 int min_uV, max_uV;
David Brownell5c139412009-03-11 03:30:43 -08002890
2891 if (vdd_bit) {
Heiko Stuebner310c8052015-10-12 18:00:54 +02002892 mmc_ocrbitnum_to_vdd(vdd_bit, &min_uV, &max_uV);
David Brownell5c139412009-03-11 03:30:43 -08002893
Tim Krygerca6429d2014-08-11 22:05:12 -07002894 result = regulator_set_voltage(supply, min_uV, max_uV);
Linus Walleij99fc5132010-09-29 01:08:27 -04002895 if (result == 0 && !mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08002896 result = regulator_enable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04002897 if (!result)
2898 mmc->regulator_enabled = true;
2899 }
2900 } else if (mmc->regulator_enabled) {
David Brownell5c139412009-03-11 03:30:43 -08002901 result = regulator_disable(supply);
Linus Walleij99fc5132010-09-29 01:08:27 -04002902 if (result == 0)
2903 mmc->regulator_enabled = false;
David Brownell5c139412009-03-11 03:30:43 -08002904 }
2905
Linus Walleij99fc5132010-09-29 01:08:27 -04002906 if (result)
2907 dev_err(mmc_dev(mmc),
2908 "could not set regulator OCR (%d)\n", result);
David Brownell5c139412009-03-11 03:30:43 -08002909 return result;
2910}
Chris Ball45a6b322012-06-11 09:39:12 -04002911EXPORT_SYMBOL_GPL(mmc_regulator_set_ocr);
David Brownell5c139412009-03-11 03:30:43 -08002912
Douglas Anderson2086f802015-10-12 14:48:25 +02002913static 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 */
2945int 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}
2989EXPORT_SYMBOL_GPL(mmc_regulator_set_vqmmc);
2990
Tim Kryger4d1f52f2014-05-06 15:57:01 -07002991#endif /* CONFIG_REGULATOR */
2992
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04002993int mmc_regulator_get_supply(struct mmc_host *mmc)
2994{
2995 struct device *dev = mmc_dev(mmc);
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04002996 int ret;
2997
Tim Kryger4d1f52f2014-05-06 15:57:01 -07002998 mmc->supply.vmmc = devm_regulator_get_optional(dev, "vmmc");
Mark Brownbc35d5e2013-07-29 21:58:01 +01002999 mmc->supply.vqmmc = devm_regulator_get_optional(dev, "vqmmc");
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04003000
Tim Kryger4d1f52f2014-05-06 15:57:01 -07003001 if (IS_ERR(mmc->supply.vmmc)) {
3002 if (PTR_ERR(mmc->supply.vmmc) == -EPROBE_DEFER)
3003 return -EPROBE_DEFER;
ludovic.desroches@atmel.com6e1bbc52015-11-09 15:03:59 +01003004 dev_dbg(dev, "No vmmc regulator found\n");
Tim Kryger4d1f52f2014-05-06 15:57:01 -07003005 } 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 Liakhovetskie1377882012-06-20 02:28:43 -04003012
Tim Kryger4d1f52f2014-05-06 15:57:01 -07003013 if (IS_ERR(mmc->supply.vqmmc)) {
3014 if (PTR_ERR(mmc->supply.vqmmc) == -EPROBE_DEFER)
3015 return -EPROBE_DEFER;
ludovic.desroches@atmel.com6e1bbc52015-11-09 15:03:59 +01003016 dev_dbg(dev, "No vqmmc regulator found\n");
Tim Kryger4d1f52f2014-05-06 15:57:01 -07003017 }
Guennadi Liakhovetskie1377882012-06-20 02:28:43 -04003018
3019 return 0;
3020}
3021EXPORT_SYMBOL_GPL(mmc_regulator_get_supply);
3022
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 * Mask off any voltages we don't support and select
3025 * the lowest voltage
3026 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003027u32 mmc_select_voltage(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028{
3029 int bit;
3030
Ulf Hansson726d6f22013-09-16 12:06:15 +02003031 /*
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 Torvalds1da177e2005-04-16 15:20:36 -07003041 ocr &= host->ocr_avail;
Ulf Hanssonce69d372013-09-16 11:28:42 +02003042 if (!ocr) {
3043 dev_warn(mmc_dev(host), "no support for card's volts\n");
3044 return 0;
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046
Ulf Hanssonce69d372013-09-16 11:28:42 +02003047 if (host->caps2 & MMC_CAP2_FULL_PWR_CYCLE) {
3048 bit = ffs(ocr) - 1;
Timo Teras63ef7312006-11-02 19:43:27 +01003049 ocr &= 3 << bit;
Ulf Hanssonce69d372013-09-16 11:28:42 +02003050 mmc_power_cycle(host, ocr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 } else {
Ulf Hanssonce69d372013-09-16 11:28:42 +02003052 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 Torvalds1da177e2005-04-16 15:20:36 -07003056 }
3057
3058 return ocr;
3059}
3060
Johan Rudholm567c8902013-01-28 15:08:27 +01003061int __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 Jadavanidff31262016-04-19 16:21:16 -07003067 if (host->ops->start_signal_voltage_switch) {
3068 mmc_host_clk_hold(host);
Johan Rudholm567c8902013-01-28 15:08:27 +01003069 err = host->ops->start_signal_voltage_switch(host, &host->ios);
Subhash Jadavanidff31262016-04-19 16:21:16 -07003070 mmc_host_clk_release(host);
3071 }
Johan Rudholm567c8902013-01-28 15:08:27 +01003072
3073 if (err)
3074 host->ios.signal_voltage = old_signal_voltage;
3075
3076 return err;
3077
3078}
3079
Ulf Hansson0f791fd2013-09-12 15:36:34 +02003080int mmc_set_signal_voltage(struct mmc_host *host, int signal_voltage, u32 ocr)
Arindam Nathf2119df2011-05-05 12:18:57 +05303081{
3082 struct mmc_command cmd = {0};
3083 int err = 0;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003084 u32 clock;
Arindam Nathf2119df2011-05-05 12:18:57 +05303085
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 Rudholm0797e5f2013-01-28 15:08:28 +01003092 if (signal_voltage == MMC_SIGNAL_VOLTAGE_330)
3093 return __mmc_set_signal_voltage(host, signal_voltage);
Arindam Nathf2119df2011-05-05 12:18:57 +05303094
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003095 /*
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 Perches66061102014-09-12 14:56:56 -07003102 pr_warn("%s: cannot verify signal voltage switch\n",
3103 mmc_hostname(host));
Arindam Nathf2119df2011-05-05 12:18:57 +05303104
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003105 cmd.opcode = SD_SWITCH_VOLTAGE;
3106 cmd.arg = 0;
3107 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
3108
Subhash Jadavani5fc13cd2013-09-06 19:07:07 +05303109 /*
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 Rudholm0797e5f2013-01-28 15:08:28 +01003114 err = mmc_wait_for_cmd(host, &cmd, 0);
3115 if (err)
Subhash Jadavanidff31262016-04-19 16:21:16 -07003116 goto err_command;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003117
Subhash Jadavanidff31262016-04-19 16:21:16 -07003118 if (!mmc_host_is_spi(host) && (cmd.resp[0] & R1_ERROR)) {
3119 err = -EIO;
3120 goto err_command;
3121 }
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003122 /*
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 Tummaladc182982013-08-20 15:32:09 +05303135 host->card_clock_off = true;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003136 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 Tummaladc182982013-08-20 15:32:09 +05303146 host->ios.clock = clock;
3147 mmc_set_ios(host);
3148 host->card_clock_off = false;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003149 goto power_cycle;
Arindam Nathf2119df2011-05-05 12:18:57 +05303150 }
3151
Doug Anderson7c5209c2015-05-12 14:46:11 -07003152 /* Keep clock gated for at least 10 ms, though spec only says 5 ms */
3153 mmc_delay(10);
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003154 host->ios.clock = clock;
3155 mmc_set_ios(host);
3156
Sahitya Tummaladc182982013-08-20 15:32:09 +05303157 host->card_clock_off = false;
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003158 /* 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
3168power_cycle:
3169 if (err) {
3170 pr_debug("%s: Signal voltage switch failed, "
3171 "power cycling card\n", mmc_hostname(host));
Ulf Hansson0f791fd2013-09-12 15:36:34 +02003172 mmc_power_cycle(host, ocr);
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003173 }
3174
Subhash Jadavanidff31262016-04-19 16:21:16 -07003175err_command:
3176 mmc_host_clk_release(host);
3177
Johan Rudholm0797e5f2013-01-28 15:08:28 +01003178 return err;
Arindam Nathf2119df2011-05-05 12:18:57 +05303179}
3180
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003182 * Select timing parameters for host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003184void mmc_set_timing(struct mmc_host *host, unsigned int timing)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185{
Subhash Jadavanidff31262016-04-19 16:21:16 -07003186 mmc_host_clk_hold(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003187 host->ios.timing = timing;
3188 mmc_set_ios(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07003189 mmc_host_clk_release(host);
Pierre Ossmanb57c43a2005-09-06 15:18:53 -07003190}
3191
3192/*
Arindam Nathd6d50a12011-05-05 12:18:59 +05303193 * Select appropriate driver type for host.
3194 */
3195void mmc_set_driver_type(struct mmc_host *host, unsigned int drv_type)
3196{
Subhash Jadavanidff31262016-04-19 16:21:16 -07003197 mmc_host_clk_hold(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05303198 host->ios.drv_type = drv_type;
3199 mmc_set_ios(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07003200 mmc_host_clk_release(host);
Arindam Nathd6d50a12011-05-05 12:18:59 +05303201}
3202
Adrian Huntere23350b2015-02-06 14:12:55 +02003203int 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 Jadavanidff31262016-04-19 16:21:16 -07003208 int drive_strength;
Adrian Huntere23350b2015-02-06 14:12:55 +02003209
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 Jadavanidff31262016-04-19 16:21:16 -07003231 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 Huntere23350b2015-02-06 14:12:55 +02003239}
3240
Arindam Nathd6d50a12011-05-05 12:18:59 +05303241/*
Russell King45f82452005-12-14 14:57:35 +00003242 * 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 Torvalds1da177e2005-04-16 15:20:36 -07003251 */
Ulf Hansson4a065192013-09-12 14:36:53 +02003252void mmc_power_up(struct mmc_host *host, u32 ocr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253{
Ulf Hanssonfa550182012-05-09 16:15:26 +02003254 if (host->ios.power_mode == MMC_POWER_ON)
3255 return;
3256
Subhash Jadavanidff31262016-04-19 16:21:16 -07003257 mmc_host_clk_hold(host);
3258
Ulf Hansson3aa87932014-11-28 14:38:36 +01003259 mmc_pwrseq_pre_power_on(host);
3260
Ulf Hansson4a065192013-09-12 14:36:53 +02003261 host->ios.vdd = fls(ocr) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 host->ios.power_mode = MMC_POWER_UP;
Johan Rudholm2d079c42014-11-06 14:46:54 +01003263 /* Set initial state and call mmc_set_ios */
3264 mmc_set_initial_state(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Tim Krygerceae98f2014-04-24 14:44:26 -07003266 /* 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 Lu108ecc42012-07-10 16:55:37 +08003273
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02003274 /*
3275 * This delay should be sufficient to allow the power supply
3276 * to reach the minimum voltage.
3277 */
José M. Fernández79bccc52009-03-10 02:21:21 +01003278 mmc_delay(10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Ulf Hansson4febb7e2015-02-02 16:01:14 +01003280 mmc_pwrseq_post_power_on(host);
3281
Hein Tibosch88ae8b82010-09-06 09:37:19 +08003282 host->ios.clock = host->f_init;
Sascha Hauer8dfd0372009-04-09 08:32:02 +02003283
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 host->ios.power_mode = MMC_POWER_ON;
Russell King920e70c2006-05-04 18:22:51 +01003285 mmc_set_ios(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286
Pierre Ossmanf9996ae2007-09-19 18:38:50 +02003287 /*
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ández79bccc52009-03-10 02:21:21 +01003291 mmc_delay(10);
Subhash Jadavanidff31262016-04-19 16:21:16 -07003292
3293 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Ulf Hansson7f7e4122011-09-21 14:08:13 -04003296void mmc_power_off(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297{
Ulf Hanssonfa550182012-05-09 16:15:26 +02003298 if (host->ios.power_mode == MMC_POWER_OFF)
3299 return;
3300
Subhash Jadavanidff31262016-04-19 16:21:16 -07003301 mmc_host_clk_hold(host);
3302
Ulf Hansson3aa87932014-11-28 14:38:36 +01003303 mmc_pwrseq_power_off(host);
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 host->ios.clock = 0;
3306 host->ios.vdd = 0;
Ulf Hanssonb33d46c2011-03-05 14:36:24 +01003307
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 host->ios.power_mode = MMC_POWER_OFF;
Johan Rudholm2d079c42014-11-06 14:46:54 +01003309 /* Set initial state and call mmc_set_ios */
3310 mmc_set_initial_state(host);
Mika Westerberg778e2772011-08-18 15:23:48 +03003311
Daniel Drake041beb12011-09-07 10:22:09 +01003312 /*
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 Jadavanidff31262016-04-19 16:21:16 -07003318
3319 mmc_host_clk_release(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320}
3321
Ulf Hansson4a065192013-09-12 14:36:53 +02003322void mmc_power_cycle(struct mmc_host *host, u32 ocr)
Johan Rudholm276e0902013-01-28 15:08:25 +01003323{
3324 mmc_power_off(host);
3325 /* Wait at least 1 ms according to SD spec */
3326 mmc_delay(1);
Ulf Hansson4a065192013-09-12 14:36:53 +02003327 mmc_power_up(host, ocr);
Johan Rudholm276e0902013-01-28 15:08:25 +01003328}
3329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330/*
Adrian Bunk39361852007-07-25 00:40:58 +02003331 * Cleanup when the last reference to the bus operator is dropped.
3332 */
Adrian Bunk261172f2008-04-13 21:15:47 +03003333static void __mmc_release_bus(struct mmc_host *host)
Adrian Bunk39361852007-07-25 00:40:58 +02003334{
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 */
3345static 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 */
3358static 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 Mehatfaa98482009-07-30 07:55:28 -07003369int mmc_resume_bus(struct mmc_host *host)
3370{
Dmitry Shmidt3ad18372010-10-14 15:20:21 -07003371 unsigned long flags;
Asutosh Dasb77f57b2016-04-12 11:52:33 +05303372 int err = 0;
Dmitry Shmidt3ad18372010-10-14 15:20:21 -07003373
San Mehatfaa98482009-07-30 07:55:28 -07003374 if (!mmc_bus_needs_resume(host))
3375 return -EINVAL;
3376
3377 pr_debug("%s: Starting deferred resume\n", mmc_hostname(host));
Dmitry Shmidt3ad18372010-10-14 15:20:21 -07003378 spin_lock_irqsave(&host->lock, flags);
San Mehatfaa98482009-07-30 07:55:28 -07003379 host->bus_resume_flags &= ~MMC_BUSRESUME_NEEDS_RESUME;
Dmitry Shmidt3ad18372010-10-14 15:20:21 -07003380 spin_unlock_irqrestore(&host->lock, flags);
3381
San Mehatfaa98482009-07-30 07:55:28 -07003382 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 Dasb77f57b2016-04-12 11:52:33 +05303387 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 Mehatfaa98482009-07-30 07:55:28 -07003395 }
3396
San Mehatfaa98482009-07-30 07:55:28 -07003397 mmc_bus_put(host);
3398 pr_debug("%s: Deferred resume completed\n", mmc_hostname(host));
3399 return 0;
3400}
3401EXPORT_SYMBOL(mmc_resume_bus);
3402
Adrian Bunk39361852007-07-25 00:40:58 +02003403/*
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003404 * Assign a mmc bus handler to a host. Only one bus handler may control a
3405 * host at any given time.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003407void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003409 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003411 BUG_ON(!host);
3412 BUG_ON(!ops);
Pierre Ossmanb8558852007-01-03 19:47:29 +01003413
Pierre Ossmand84075c82007-08-09 13:23:56 +02003414 WARN_ON(!host->claimed);
Pierre Ossmanb8558852007-01-03 19:47:29 +01003415
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003416 spin_lock_irqsave(&host->lock, flags);
Pierre Ossmanb8558852007-01-03 19:47:29 +01003417
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003418 BUG_ON(host->bus_ops);
3419 BUG_ON(host->bus_refs);
Pierre Ossmanb8558852007-01-03 19:47:29 +01003420
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003421 host->bus_ops = ops;
3422 host->bus_refs = 1;
3423 host->bus_dead = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003425 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428/*
Ulf Hansson7f7e4122011-09-21 14:08:13 -04003429 * Remove the current bus handler from a host.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 */
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003431void mmc_detach_bus(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432{
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003433 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003435 BUG_ON(!host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Pierre Ossmand84075c82007-08-09 13:23:56 +02003437 WARN_ON(!host->claimed);
3438 WARN_ON(!host->bus_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003440 spin_lock_irqsave(&host->lock, flags);
3441
3442 host->bus_dead = 1;
3443
3444 spin_unlock_irqrestore(&host->lock, flags);
3445
Pierre Ossman7ea239d2006-12-31 00:11:32 +01003446 mmc_bus_put(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447}
3448
Ulf Hanssonbbd43682013-09-20 11:02:35 +02003449static 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 Badiganticdf0d572017-08-07 14:58:15 +05303468 /*
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 Guo5ebdb442017-11-04 14:08:30 +08003475 if (delayed_work_pending(&host->detect))
3476 cancel_delayed_work(&host->detect);
3477
Ulf Hanssonbbd43682013-09-20 11:02:35 +02003478 mmc_schedule_delayed_work(&host->detect, delay);
3479}
3480
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481/**
3482 * mmc_detect_change - process change of state on a MMC socket
3483 * @host: host which changed state.
Richard Purdie8dc00332005-09-08 17:53:01 +01003484 * @delay: optional delay to wait before detection (jiffies)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 *
Pierre Ossman67a61c42007-07-11 20:22:11 +02003486 * 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 Torvalds1da177e2005-04-16 15:20:36 -07003490 */
Richard Purdie8dc00332005-09-08 17:53:01 +01003491void mmc_detect_change(struct mmc_host *host, unsigned long delay)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492{
Ulf Hanssonbbd43682013-09-20 11:02:35 +02003493 _mmc_detect_change(host, delay, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495EXPORT_SYMBOL(mmc_detect_change);
3496
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003497void 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 Grignouc6d8fd62016-06-03 09:08:52 -07003515 * 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 Hunterdfe86cb2010-08-11 14:17:46 -07003520 */
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 Dongcc8aa7d2014-08-14 18:29:24 +08003524 } else if (card->erase_size) {
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003525 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 Dongcc8aa7d2014-08-14 18:29:24 +08003541 } else
3542 card->pref_erase = 0;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003543}
3544
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003545static unsigned int mmc_mmc_erase_timeout(struct mmc_card *card,
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07003546 unsigned int arg, unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003547{
3548 unsigned int erase_timeout;
3549
Adrian Hunter7194efb2012-04-05 14:45:47 +03003550 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 Hunterdfe86cb2010-08-11 14:17:46 -07003554 /* 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 Jadavanidff31262016-04-19 16:21:16 -07003577 (mmc_host_clk_rate(card->host) / 1000);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003578
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 Warkentineaa02f72011-04-11 16:13:41 -05003606 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003607}
3608
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003609static unsigned int mmc_sd_erase_timeout(struct mmc_card *card,
3610 unsigned int arg,
3611 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003612{
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003613 unsigned int erase_timeout;
3614
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003615 if (card->ssr.erase_timeout) {
3616 /* Erase timeout specified in SD Status Register (SSR) */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003617 erase_timeout = card->ssr.erase_timeout * qty +
3618 card->ssr.erase_offset;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003619 } else {
3620 /*
3621 * Erase timeout not specified in SD Status Register (SSR) so
3622 * use 250ms per write block.
3623 */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003624 erase_timeout = 250 * qty;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003625 }
3626
3627 /* Must not be less than 1 second */
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003628 if (erase_timeout < 1000)
3629 erase_timeout = 1000;
3630
3631 return erase_timeout;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003632}
3633
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003634static unsigned int mmc_erase_timeout(struct mmc_card *card,
3635 unsigned int arg,
3636 unsigned int qty)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003637{
3638 if (mmc_card_sd(card))
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003639 return mmc_sd_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003640 else
Andrei Warkentineaa02f72011-04-11 16:13:41 -05003641 return mmc_mmc_erase_timeout(card, arg, qty);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003642}
3643
Sahitya Tummala9433a132015-06-09 09:38:36 +05303644static u32 mmc_get_erase_qty(struct mmc_card *card, u32 from, u32 to)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003645{
Sahitya Tummala9433a132015-06-09 09:38:36 +05303646 u32 qty = 0;
Adrian Hunter8f11d102015-05-07 13:10:16 +03003647
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003648 /*
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 Tummala9433a132015-06-09 09:38:36 +05303672 return qty;
3673}
3674
3675static 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
3701static 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 Hunterdfe86cb2010-08-11 14:17:46 -07003715
3716 if (!mmc_card_blockaddr(card)) {
3717 from <<= 9;
3718 to <<= 9;
3719 }
3720
Sahitya Tummala9433a132015-06-09 09:38:36 +05303721 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));
3761out:
3762 return err;
3763}
3764
3765static 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 Hunterdfe86cb2010-08-11 14:17:46 -07003786 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 Sa3c76eb2011-10-11 11:44:09 +05303794 pr_err("mmc_erase: group start error %d, "
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003795 "status %#x\n", err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03003796 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003797 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 Sa3c76eb2011-10-11 11:44:09 +05303809 pr_err("mmc_erase: group end error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003810 err, cmd.resp[0]);
Adrian Hunter67716322011-08-29 16:42:15 +03003811 err = -EIO;
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003812 goto out;
3813 }
3814
3815 memset(&cmd, 0, sizeof(struct mmc_command));
3816 cmd.opcode = MMC_ERASE;
3817 cmd.arg = arg;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08003818 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 Hunterdfe86cb2010-08-11 14:17:46 -07003834 err = mmc_wait_for_cmd(card->host, &cmd, 0);
3835 if (err) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303836 pr_err("mmc_erase: erase error %d, status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003837 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 Wangbb4eecf2016-07-25 16:48:39 +08003845 /*
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 Hunterdfe86cb2010-08-11 14:17:46 -07003853 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 Sa3c76eb2011-10-11 11:44:09 +05303861 pr_err("error %d requesting status %#x\n",
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003862 err, cmd.resp[0]);
3863 err = -EIO;
3864 goto out;
3865 }
Trey Ramsay8fee4762012-11-16 09:31:41 -06003866
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 Hunterdfe86cb2010-08-11 14:17:46 -07003877 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
Trey Ramsay8fee4762012-11-16 09:31:41 -06003878 (R1_CURRENT_STATE(cmd.resp[0]) == R1_STATE_PRG));
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003879out:
Adrian Hunter8f11d102015-05-07 13:10:16 +03003880 mmc_retune_release(card->host);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003881 return err;
3882}
3883
Baolin Wang71085122016-09-07 10:38:24 +08003884static 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 Wang6c689882016-09-07 10:38:25 +08003891 /*
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 Wang71085122016-09-07 10:38:24 +08003901 if (nr_new > rem)
3902 nr_new -= rem;
3903 else
3904 return 0;
Baolin Wang71085122016-09-07 10:38:24 +08003905
Baolin Wang6c689882016-09-07 10:38:25 +08003906 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 Wang71085122016-09-07 10:38:24 +08003922
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 Tummala9433a132015-06-09 09:38:36 +05303932int mmc_erase_sanity_check(struct mmc_card *card, unsigned int from,
3933 unsigned int nr, unsigned int arg)
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003934{
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07003935
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 Tummala9433a132015-06-09 09:38:36 +05303958 return 0;
3959}
3960
3961int 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}
4000EXPORT_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 */
4011int 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 Hunterdfe86cb2010-08-11 14:17:46 -07004020
Baolin Wang71085122016-09-07 10:38:24 +08004021 if (arg == MMC_ERASE_ARG)
4022 nr = mmc_align_erase_size(card, &from, &to, nr);
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004023
4024 if (nr == 0)
4025 return 0;
4026
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004027 if (to <= from)
4028 return -EINVAL;
4029
4030 /* 'from' and 'to' are inclusive */
4031 to -= 1;
4032
David Jander642c28a2015-06-23 11:43:52 +02004033 /*
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 Gracht22d7e852015-08-04 08:58:33 +02004041 rem = card->erase_size - (from % card->erase_size);
4042 if ((arg & MMC_TRIM_ARGS) && (card->eg_boundary) && (nr > rem)) {
Sahitya Tummala9433a132015-06-09 09:38:36 +05304043 ret = mmc_do_erase(card, from, from + rem - 1, arg);
David Jander642c28a2015-06-23 11:43:52 +02004044 from += rem;
Sahitya Tummala9433a132015-06-09 09:38:36 +05304045 if ((ret) || (to <= from))
4046 return ret;
David Jander642c28a2015-06-23 11:43:52 +02004047 }
4048
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004049 return mmc_do_erase(card, from, to, arg);
4050}
4051EXPORT_SYMBOL(mmc_erase);
4052
4053int 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}
4060EXPORT_SYMBOL(mmc_can_erase);
4061
4062int mmc_can_trim(struct mmc_card *card)
4063{
Shawn Linb5b4ff02015-08-12 13:08:32 +08004064 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) &&
4065 (!(card->quirks & MMC_QUIRK_TRIM_BROKEN)))
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004066 return 1;
4067 return 0;
4068}
4069EXPORT_SYMBOL(mmc_can_trim);
4070
Kyungmin Parkb3bf9152011-10-18 09:34:04 +09004071int 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}
4081EXPORT_SYMBOL(mmc_can_discard);
4082
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09004083int mmc_can_sanitize(struct mmc_card *card)
4084{
Adrian Hunter28302812012-04-05 14:45:48 +03004085 if (!mmc_can_trim(card) && !mmc_can_erase(card))
4086 return 0;
Kyungmin Parkd9ddd622011-10-14 14:15:48 +09004087 if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_SANITIZE)
4088 return 1;
4089 return 0;
4090}
4091EXPORT_SYMBOL(mmc_can_sanitize);
4092
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004093int mmc_can_secure_erase_trim(struct mmc_card *card)
4094{
Lukas Czerner5204d002014-06-18 13:18:07 +02004095 if ((card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) &&
4096 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
Adrian Hunterdfe86cb2010-08-11 14:17:46 -07004097 return 1;
4098 return 0;
4099}
4100EXPORT_SYMBOL(mmc_can_secure_erase_trim);
4101
4102int 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}
4111EXPORT_SYMBOL(mmc_erase_group_aligned);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Adrian Huntere056a1b2011-06-28 17:16:02 +03004113static unsigned int mmc_do_calc_max_discard(struct mmc_card *card,
4114 unsigned int arg)
4115{
4116 struct mmc_host *host = card->host;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004117 unsigned int max_discard, x, y, qty = 0, max_qty, min_qty, timeout;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004118 unsigned int last_timeout = 0;
Ulf Hansson12182af2016-07-27 00:04:03 +02004119 unsigned int max_busy_timeout = host->max_busy_timeout ?
4120 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004121
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004122 if (card->erase_shift) {
Adrian Huntere056a1b2011-06-28 17:16:02 +03004123 max_qty = UINT_MAX >> card->erase_shift;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004124 min_qty = card->pref_erase >> card->erase_shift;
4125 } else if (mmc_card_sd(card)) {
Adrian Huntere056a1b2011-06-28 17:16:02 +03004126 max_qty = UINT_MAX;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004127 min_qty = card->pref_erase;
4128 } else {
Adrian Huntere056a1b2011-06-28 17:16:02 +03004129 max_qty = UINT_MAX / card->erase_size;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004130 min_qty = card->pref_erase / card->erase_size;
4131 }
Adrian Huntere056a1b2011-06-28 17:16:02 +03004132
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004133 /*
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 Hansson12182af2016-07-27 00:04:03 +02004143 * get a balance value. In cases when the 'host->max_busy_timeout'
4144 * isn't specified, use the default max erase timeout.
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004145 */
Adrian Huntere056a1b2011-06-28 17:16:02 +03004146 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 Wangbb4eecf2016-07-25 16:48:39 +08004150
Ulf Hansson12182af2016-07-27 00:04:03 +02004151 if (qty + x > min_qty && timeout > max_busy_timeout)
Adrian Huntere056a1b2011-06-28 17:16:02 +03004152 break;
Baolin Wangbb4eecf2016-07-25 16:48:39 +08004153
Adrian Huntere056a1b2011-06-28 17:16:02 +03004154 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 Jander642c28a2015-06-23 11:43:52 +02004165 /*
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 Huntere056a1b2011-06-28 17:16:02 +03004175 if (qty == 1)
David Jander642c28a2015-06-23 11:43:52 +02004176 card->eg_boundary = 1;
4177 else
4178 qty--;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004179
4180 /* Convert qty to sectors */
4181 if (card->erase_shift)
David Jander642c28a2015-06-23 11:43:52 +02004182 max_discard = qty << card->erase_shift;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004183 else if (mmc_card_sd(card))
David Jander642c28a2015-06-23 11:43:52 +02004184 max_discard = qty + 1;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004185 else
David Jander642c28a2015-06-23 11:43:52 +02004186 max_discard = qty * card->erase_size;
Adrian Huntere056a1b2011-06-28 17:16:02 +03004187
4188 return max_discard;
4189}
4190
4191unsigned 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 Konda871cb9f2015-09-26 17:38:26 -07004196 if (!host->max_busy_timeout ||
4197 (host->caps2 & MMC_CAP2_MAX_DISCARD_SIZE))
Can Guo98199152017-10-17 14:13:17 +08004198 return UINT_MAX;
Krishna Konda871cb9f2015-09-26 17:38:26 -07004199
Adrian Huntere056a1b2011-06-28 17:16:02 +03004200 /*
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 Hansson12182af2016-07-27 00:04:03 +02004217 mmc_hostname(host), max_discard, host->max_busy_timeout ?
4218 host->max_busy_timeout : MMC_ERASE_TIMEOUT_MS);
Adrian Huntere056a1b2011-06-28 17:16:02 +03004219 return max_discard;
4220}
4221EXPORT_SYMBOL(mmc_calc_max_discard);
4222
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03004223int mmc_set_blocklen(struct mmc_card *card, unsigned int blocklen)
4224{
Chris Ball1278dba2011-04-13 23:40:30 -04004225 struct mmc_command cmd = {0};
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03004226
Ziyuan Xu1712c932016-09-21 09:43:49 +08004227 if (mmc_card_blockaddr(card) || mmc_card_ddr52(card) ||
4228 mmc_card_hs400(card) || mmc_card_hs400es(card))
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03004229 return 0;
4230
Adrian Hunter0f8d8ea2010-08-24 13:20:26 +03004231 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}
4236EXPORT_SYMBOL(mmc_set_blocklen);
4237
Loic Pallardy67c79db2012-08-06 17:12:30 +02004238int 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}
4250EXPORT_SYMBOL(mmc_set_blockcount);
4251
Adrian Hunterb2499512011-08-29 16:42:11 +03004252static 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 Jadavanidff31262016-04-19 16:21:16 -07004256 mmc_host_clk_hold(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03004257 host->ops->hw_reset(host);
Subhash Jadavanidff31262016-04-19 16:21:16 -07004258 mmc_host_clk_release(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03004259}
4260
Ritesh Harjanic2f6cf02015-09-28 15:55:01 +05304261/*
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 */
4267int mmc_cmdq_hw_reset(struct mmc_host *host)
4268{
Sayali Lokhandeb2637032017-04-24 13:40:50 +05304269 if (!host->bus_ops->reset)
4270 return -EOPNOTSUPP;
Ritesh Harjanic2f6cf02015-09-28 15:55:01 +05304271
Sayali Lokhandeb2637032017-04-24 13:40:50 +05304272 return host->bus_ops->reset(host);
Ritesh Harjanic2f6cf02015-09-28 15:55:01 +05304273}
4274EXPORT_SYMBOL(mmc_cmdq_hw_reset);
4275
Johan Rudholm83533ab2015-01-12 15:38:04 +01004276int mmc_hw_reset(struct mmc_host *host)
Adrian Hunterb2499512011-08-29 16:42:11 +03004277{
Johan Rudholmf855a372015-01-12 15:38:05 +01004278 int ret;
Adrian Hunterb2499512011-08-29 16:42:11 +03004279
Johan Rudholmf855a372015-01-12 15:38:05 +01004280 if (!host->card)
Adrian Hunterb2499512011-08-29 16:42:11 +03004281 return -EINVAL;
4282
Johan Rudholmf855a372015-01-12 15:38:05 +01004283 mmc_bus_get(host);
4284 if (!host->bus_ops || host->bus_dead || !host->bus_ops->reset) {
4285 mmc_bus_put(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03004286 return -EOPNOTSUPP;
Adrian Hunterb2499512011-08-29 16:42:11 +03004287 }
4288
Johan Rudholmf855a372015-01-12 15:38:05 +01004289 ret = host->bus_ops->reset(host);
4290 mmc_bus_put(host);
Adrian Hunterb2499512011-08-29 16:42:11 +03004291
Gwendal Grignou4e6c7172016-04-01 16:04:22 -07004292 if (ret)
4293 pr_warn("%s: tried to reset card, got error %d\n",
4294 mmc_hostname(host), ret);
Adrian Hunterb2499512011-08-29 16:42:11 +03004295
Johan Rudholmf855a372015-01-12 15:38:05 +01004296 return ret;
Adrian Hunterb2499512011-08-29 16:42:11 +03004297}
Adrian Hunterb2499512011-08-29 16:42:11 +03004298EXPORT_SYMBOL(mmc_hw_reset);
4299
Andy Ross807e8e42011-01-03 10:36:56 -08004300static 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 Hansson4a065192013-09-12 14:36:53 +02004308 mmc_power_up(host, host->ocr_avail);
Philip Rakity2f94e552011-02-13 23:12:28 -08004309
4310 /*
Adrian Hunterb2499512011-08-29 16:42:11 +03004311 * 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 Rakity2f94e552011-02-13 23:12:28 -08004317 * 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 Caione100a6062015-11-25 15:39:51 +01004320 * Skip it if we already know that we do not support SDIO commands
Philip Rakity2f94e552011-02-13 23:12:28 -08004321 */
Carlo Caione100a6062015-11-25 15:39:51 +01004322 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
4323 sdio_reset(host);
4324
Andy Ross807e8e42011-01-03 10:36:56 -08004325 mmc_go_idle(host);
4326
Ulf Hansson1b8d79c2016-06-21 15:12:46 +02004327 if (!(host->caps2 & MMC_CAP2_NO_SD))
4328 mmc_send_if_cond(host, host->ocr_avail);
Andy Ross807e8e42011-01-03 10:36:56 -08004329
4330 /* Order's important: probe SDIO, then SD, then MMC */
Carlo Caione100a6062015-11-25 15:39:51 +01004331 if (!(host->caps2 & MMC_CAP2_NO_SDIO))
4332 if (!mmc_attach_sdio(host))
4333 return 0;
4334
Ulf Hansson1b8d79c2016-06-21 15:12:46 +02004335 if (!(host->caps2 & MMC_CAP2_NO_SD))
4336 if (!mmc_attach_sd(host))
4337 return 0;
4338
Shawn Lina0c3b682016-07-01 15:45:28 +08004339 if (!(host->caps2 & MMC_CAP2_NO_MMC))
4340 if (!mmc_attach_mmc(host))
4341 return 0;
Andy Ross807e8e42011-01-03 10:36:56 -08004342
4343 mmc_power_off(host);
4344 return -EIO;
4345}
4346
Adrian Hunterd3049502011-11-28 16:22:00 +02004347int _mmc_detect_card_removed(struct mmc_host *host)
4348{
4349 int ret;
4350
Adrian Hunterd3049502011-11-28 16:22:00 +02004351 if (!host->card || mmc_card_removed(host->card))
4352 return 1;
4353
4354 ret = host->bus_ops->alive(host);
Kevin Liu14507342013-02-28 15:29:29 +08004355
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 Hunterd3049502011-11-28 16:22:00 +02004368 if (ret) {
Can Guo6514fbe2017-07-04 12:36:09 +08004369 if (host->ops->get_cd && host->ops->get_cd(host)) {
Wanhyeong Ryu608a9982017-11-01 15:51:40 +08004370 ret = mmc_recovery_fallback_lower_speed(host);
Can Guo6514fbe2017-07-04 12:36:09 +08004371 } 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 Dasaf6a03e2017-03-30 14:41:16 -07004379 }
Adrian Hunterd3049502011-11-28 16:22:00 +02004380 }
4381
4382 return ret;
4383}
4384
4385int mmc_detect_card_removed(struct mmc_host *host)
4386{
4387 struct mmc_card *card = host->card;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004388 int ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02004389
4390 WARN_ON(!host->claimed);
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004391
4392 if (!card)
4393 return 1;
4394
Jaehoon Chung6067baf2016-01-29 08:52:57 +09004395 if (!mmc_card_is_removable(host))
Ulf Hansson1ff25752015-11-05 16:21:39 +01004396 return 0;
4397
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004398 ret = mmc_card_removed(card);
Adrian Hunterd3049502011-11-28 16:22:00 +02004399 /*
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 Hanssonb6891672013-04-18 11:02:07 +02004403 if (!host->detect_change && !(host->caps & MMC_CAP_NEEDS_POLL))
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004404 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02004405
4406 host->detect_change = 0;
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004407 if (!ret) {
4408 ret = _mmc_detect_card_removed(host);
Ulf Hanssonb6891672013-04-18 11:02:07 +02004409 if (ret && (host->caps & MMC_CAP_NEEDS_POLL)) {
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004410 /*
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 Hanssonbbd43682013-09-20 11:02:35 +02004415 _mmc_detect_change(host, 0, false);
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004416 }
4417 }
Adrian Hunterd3049502011-11-28 16:22:00 +02004418
Ulf Hanssonf0cc9cf2012-02-06 10:42:39 +01004419 return ret;
Adrian Hunterd3049502011-11-28 16:22:00 +02004420}
4421EXPORT_SYMBOL(mmc_detect_card_removed);
4422
Ankit Jain0d55bf02017-09-19 17:43:02 +05304423/*
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 */
4429void mmc_flush_detect_work(struct mmc_host *host)
4430{
4431 flush_delayed_work(&host->detect);
4432}
4433EXPORT_SYMBOL(mmc_flush_detect_work);
4434
Pierre Ossmanb93931a2007-05-19 14:06:24 +02004435void mmc_rescan(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436{
Ritesh Harjanibc67b402016-07-11 13:55:50 +05304437 unsigned long flags;
David Howellsc4028952006-11-22 14:57:56 +00004438 struct mmc_host *host =
4439 container_of(work, struct mmc_host, detect.work);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004440
Ritesh Harjanibc67b402016-07-11 13:55:50 +05304441 spin_lock_irqsave(&host->lock, flags);
4442 if (host->rescan_disable) {
4443 spin_unlock_irqrestore(&host->lock, flags);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004444 return;
Ritesh Harjanibc67b402016-07-11 13:55:50 +05304445 }
4446 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Johan Rudholm3339d1e2012-08-23 13:40:55 +02004448 /* If there is a non-removable card registered, only scan once */
Jaehoon Chung6067baf2016-01-29 08:52:57 +09004449 if (!mmc_card_is_removable(host) && host->rescan_entered)
Johan Rudholm3339d1e2012-08-23 13:40:55 +02004450 return;
4451 host->rescan_entered = 1;
4452
Ulf Hansson86236812015-11-05 16:08:07 +01004453 if (host->trigger_card_event && host->ops->card_event) {
Ulf Hanssond234d212015-11-05 16:08:08 +01004454 mmc_claim_host(host);
Ulf Hansson86236812015-11-05 16:08:07 +01004455 host->ops->card_event(host);
Ulf Hanssond234d212015-11-05 16:08:08 +01004456 mmc_release_host(host);
Ulf Hansson86236812015-11-05 16:08:07 +01004457 host->trigger_card_event = false;
4458 }
4459
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004460 mmc_bus_get(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Ohad Ben-Cohen30201e72010-11-28 07:21:28 +02004462 /*
4463 * if there is a _removable_ card registered, check whether it is
4464 * still present
4465 */
Jaehoon Chung6067baf2016-01-29 08:52:57 +09004466 if (host->bus_ops && !host->bus_dead && mmc_card_is_removable(host))
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004467 host->bus_ops->detect(host);
4468
Adrian Hunterd3049502011-11-28 16:22:00 +02004469 host->detect_change = 0;
Veerabhadrarao Badiganticdf0d572017-08-07 14:58:15 +05304470 if (host->ignore_bus_resume_flags)
4471 host->ignore_bus_resume_flags = false;
Adrian Hunterd3049502011-11-28 16:22:00 +02004472
Chris Ballc5841792011-01-04 12:20:22 -05004473 /*
4474 * Let mmc_bus_put() free the bus/bus_ops if we've found that
4475 * the card is no longer present.
4476 */
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004477 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004478 mmc_bus_get(host);
4479
4480 /* if there still is a card present, stop here */
4481 if (host->bus_ops != NULL) {
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004482 mmc_bus_put(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004483 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 }
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004485
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004486 /*
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 Hanssond234d212015-11-05 16:08:08 +01004492 mmc_claim_host(host);
Jaehoon Chung6067baf2016-01-29 08:52:57 +09004493 if (mmc_card_is_removable(host) && host->ops->get_cd &&
Sascha Hauerc1b55bf2013-12-05 14:34:46 +01004494 host->ops->get_cd(host) == 0) {
Ulf Hanssonfa550182012-05-09 16:15:26 +02004495 mmc_power_off(host);
4496 mmc_release_host(host);
Jorg Schummer94d89ef2009-03-31 17:51:21 +03004497 goto out;
Ulf Hanssonfa550182012-05-09 16:15:26 +02004498 }
Xiaonian Wang90d085b2017-04-20 01:21:21 -07004499 mmc_rescan_try_freq(host, host->f_min);
Veerabhadrarao Badiganti4c3e6702017-10-17 19:07:00 +05304500 host->err_stats[MMC_ERR_CMD_TIMEOUT] = 0;
Andy Ross807e8e42011-01-03 10:36:56 -08004501 mmc_release_host(host);
4502
4503 out:
Anton Vorontsov28f52482008-06-17 18:17:15 +04004504 if (host->caps & MMC_CAP_NEEDS_POLL)
4505 mmc_schedule_delayed_work(&host->detect, HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506}
4507
Pierre Ossmanb93931a2007-05-19 14:06:24 +02004508void mmc_start_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
Pavan Anamuladb78bb92015-08-24 20:15:16 +05304510 mmc_claim_host(host);
Ulf Hanssonfa550182012-05-09 16:15:26 +02004511 host->f_init = max(freqs[0], host->f_min);
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02004512 host->rescan_disable = 0;
Roger Tseng8af465d2014-09-24 17:07:13 +08004513 host->ios.power_mode = MMC_POWER_UNDEFINED;
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02004514
Adrian Huntera08b17b2013-04-15 11:27:25 -04004515 if (host->caps2 & MMC_CAP2_NO_PRESCAN_POWERUP)
4516 mmc_power_off(host);
4517 else
Ulf Hansson4a065192013-09-12 14:36:53 +02004518 mmc_power_up(host, host->ocr_avail);
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02004519
Adrian Hunter740a2212014-03-10 15:02:41 +02004520 mmc_gpiod_request_cd_irq(host);
Subhash Jadavani083d5e22017-05-08 18:52:56 -07004521 mmc_register_extcon(host);
Pavan Anamuladb78bb92015-08-24 20:15:16 +05304522 mmc_release_host(host);
Ulf Hanssonbbd43682013-09-20 11:02:35 +02004523 _mmc_detect_change(host, 0, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524}
4525
Pierre Ossmanb93931a2007-05-19 14:06:24 +02004526void mmc_stop_host(struct mmc_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527{
Pierre Ossman3b91e552007-02-11 20:43:19 +01004528#ifdef CONFIG_MMC_DEBUG
Pierre Ossman1efd48b2007-05-08 22:35:17 +02004529 unsigned long flags;
4530 spin_lock_irqsave(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01004531 host->removed = 1;
Pierre Ossman1efd48b2007-05-08 22:35:17 +02004532 spin_unlock_irqrestore(&host->lock, flags);
Pierre Ossman3b91e552007-02-11 20:43:19 +01004533#endif
Adrian Hunter740a2212014-03-10 15:02:41 +02004534 if (host->slot.cd_irq >= 0)
4535 disable_irq(host->slot.cd_irq);
Pierre Ossman3b91e552007-02-11 20:43:19 +01004536
Guennadi Liakhovetskid9adcc12012-06-14 10:17:39 +02004537 host->rescan_disable = 1;
Guennadi Liakhovetskid9bcbf32010-11-11 17:32:25 +01004538 cancel_delayed_work_sync(&host->detect);
Pierre Ossman3b91e552007-02-11 20:43:19 +01004539
Nicolas Pitreda68c4e2010-03-05 13:43:31 -08004540 /* clear pm flags now and let card drivers set them as needed */
4541 host->pm_flags = 0;
4542
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004543 mmc_bus_get(host);
4544 if (host->bus_ops && !host->bus_dead) {
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01004545 /* Calling bus_ops->remove() with a claimed host can deadlock */
Ulf Hansson58a8a4a2013-06-10 17:03:36 +02004546 host->bus_ops->remove(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004547 mmc_claim_host(host);
4548 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04004549 mmc_power_off(host);
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004550 mmc_release_host(host);
Denis Karpov53509f02009-09-22 16:44:36 -07004551 mmc_bus_put(host);
4552 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 }
Pierre Ossman7ea239d2006-12-31 00:11:32 +01004554 mmc_bus_put(host);
4555
4556 BUG_ON(host->card);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557
Subhash Jadavaniee2aaee2017-08-02 16:09:45 -07004558 mmc_unregister_extcon(host);
Subhash Jadavani083d5e22017-05-08 18:52:56 -07004559
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02004560 mmc_claim_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561 mmc_power_off(host);
Ulf Hansson8d1ffc8c2015-09-11 14:41:55 +02004562 mmc_release_host(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563}
4564
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004565int mmc_power_save_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07004566{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004567 int ret = 0;
4568
Daniel Drakebb9cab92011-07-17 16:38:41 +01004569#ifdef CONFIG_MMC_DEBUG
4570 pr_info("%s: %s: powering down\n", mmc_hostname(host), __func__);
4571#endif
4572
Adrian Huntereae1aee2009-09-22 16:44:33 -07004573 mmc_bus_get(host);
4574
Ulf Hansson5601aaf2013-10-30 23:15:30 +01004575 if (!host->bus_ops || host->bus_dead) {
Adrian Huntereae1aee2009-09-22 16:44:33 -07004576 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004577 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07004578 }
4579
4580 if (host->bus_ops->power_save)
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004581 ret = host->bus_ops->power_save(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07004582
4583 mmc_bus_put(host);
4584
4585 mmc_power_off(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004586
4587 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07004588}
4589EXPORT_SYMBOL(mmc_power_save_host);
4590
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004591int mmc_power_restore_host(struct mmc_host *host)
Adrian Huntereae1aee2009-09-22 16:44:33 -07004592{
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004593 int ret;
4594
Daniel Drakebb9cab92011-07-17 16:38:41 +01004595#ifdef CONFIG_MMC_DEBUG
4596 pr_info("%s: %s: powering up\n", mmc_hostname(host), __func__);
4597#endif
4598
Adrian Huntereae1aee2009-09-22 16:44:33 -07004599 mmc_bus_get(host);
4600
Ulf Hansson5601aaf2013-10-30 23:15:30 +01004601 if (!host->bus_ops || host->bus_dead) {
Adrian Huntereae1aee2009-09-22 16:44:33 -07004602 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004603 return -EINVAL;
Adrian Huntereae1aee2009-09-22 16:44:33 -07004604 }
4605
Ulf Hansson69041152013-09-13 11:31:33 +02004606 mmc_power_up(host, host->card->ocr);
Ritesh Harjanic2f6cf02015-09-28 15:55:01 +05304607 mmc_claim_host(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004608 ret = host->bus_ops->power_restore(host);
Ritesh Harjanic2f6cf02015-09-28 15:55:01 +05304609 mmc_release_host(host);
Adrian Huntereae1aee2009-09-22 16:44:33 -07004610
4611 mmc_bus_put(host);
Ohad Ben-Cohen12ae6372010-10-02 13:54:06 +02004612
4613 return ret;
Adrian Huntereae1aee2009-09-22 16:44:33 -07004614}
4615EXPORT_SYMBOL(mmc_power_restore_host);
4616
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004617/*
Sahitya Tummala61868a42015-05-28 16:54:19 +05304618 * Add barrier request to the requests in cache
4619 */
4620int 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);
4646out:
4647 return err;
4648}
4649EXPORT_SYMBOL(mmc_cache_barrier);
4650
4651/*
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004652 * Flush the cache to the non-volatile storage.
4653 */
4654int mmc_flush_cache(struct mmc_card *card)
4655{
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004656 int err = 0;
4657
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004658 if (mmc_card_mmc(card) &&
4659 (card->ext_csd.cache_size > 0) &&
Talel Shenharc1d30bc2015-02-04 17:59:23 +02004660 (card->ext_csd.cache_ctrl & 1) &&
4661 (!(card->quirks & MMC_QUIRK_CACHE_DISABLE))) {
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004662 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
4663 EXT_CSD_FLUSH_CACHE, 1, 0);
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02004664 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 Jeon881d1c22011-10-14 14:03:21 +09004674 pr_err("%s: cache flush error %d\n",
4675 mmc_hostname(card->host), err);
Talel Shenhar8a8e3b42015-02-11 12:58:16 +02004676 }
Seungwon Jeon881d1c22011-10-14 14:03:21 +09004677 }
4678
4679 return err;
4680}
4681EXPORT_SYMBOL(mmc_flush_cache);
4682
Ulf Hansson8dede182015-11-05 16:11:12 +01004683#ifdef CONFIG_PM_SLEEP
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004684/* 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 Hansson8dede182015-11-05 16:11:12 +01004688static int mmc_pm_notify(struct notifier_block *notify_block,
4689 unsigned long mode, void *unused)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004690{
4691 struct mmc_host *host = container_of(
4692 notify_block, struct mmc_host, pm_notify);
4693 unsigned long flags;
Ulf Hansson810cadd2013-06-10 17:03:37 +02004694 int err = 0;
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004695
4696 switch (mode) {
4697 case PM_HIBERNATION_PREPARE:
4698 case PM_SUSPEND_PREPARE:
Grygorii Strashko184af162015-04-23 13:43:43 +03004699 case PM_RESTORE_PREPARE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004700 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 Hansson810cadd2013-06-10 17:03:37 +02004705 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 Hansson5601aaf2013-10-30 23:15:30 +01004711 if (!err)
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004712 break;
4713
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01004714 /* Calling bus_ops->remove() with a claimed host can deadlock */
Ulf Hansson58a8a4a2013-06-10 17:03:36 +02004715 host->bus_ops->remove(host);
Guennadi Liakhovetski0db13fc2012-01-04 15:28:45 +01004716 mmc_claim_host(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004717 mmc_detach_bus(host);
Ulf Hansson7f7e4122011-09-21 14:08:13 -04004718 mmc_power_off(host);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004719 mmc_release_host(host);
4720 host->pm_flags = 0;
4721 break;
4722
4723 case PM_POST_SUSPEND:
4724 case PM_POST_HIBERNATION:
Takashi Iwai274476f2010-12-10 08:40:31 +01004725 case PM_POST_RESTORE:
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004726
4727 spin_lock_irqsave(&host->lock, flags);
4728 host->rescan_disable = 0;
Veerabhadrarao Badiganticdf0d572017-08-07 14:58:15 +05304729 if (mmc_bus_manual_resume(host) &&
4730 !host->ignore_bus_resume_flags) {
Dmitry Shmidt3ad18372010-10-14 15:20:21 -07004731 spin_unlock_irqrestore(&host->lock, flags);
4732 break;
4733 }
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004734 spin_unlock_irqrestore(&host->lock, flags);
Ulf Hanssonbbd43682013-09-20 11:02:35 +02004735 _mmc_detect_change(host, 0, false);
Maxim Levitsky4c2ef252010-08-10 18:01:41 -07004736
4737 }
4738
4739 return 0;
4740}
Ulf Hansson8dede182015-11-05 16:11:12 +01004741
4742void 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
4748void mmc_unregister_pm_notifier(struct mmc_host *host)
4749{
4750 unregister_pm_notifier(&host->pm_notify);
4751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752#endif
4753
Konstantin Dorfman2220eed2013-01-14 14:28:17 -05004754/**
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 */
4762void 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 Mehat03a7efe2008-04-14 15:22:49 -07004771#ifdef CONFIG_MMC_EMBEDDED_SDIO
4772void 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
4784EXPORT_SYMBOL(mmc_set_embedded_sdio_data);
4785#endif
4786
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004787static int __init mmc_init(void)
4788{
4789 int ret;
4790
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004791 ret = mmc_register_bus();
Pierre Ossmane29a7d72007-05-26 13:48:18 +02004792 if (ret)
Ulf Hansson520bd7a2015-12-01 10:35:29 +01004793 return ret;
Pierre Ossmane29a7d72007-05-26 13:48:18 +02004794
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
4805unregister_host_class:
4806 mmc_unregister_host_class();
4807unregister_bus:
4808 mmc_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004809 return ret;
4810}
4811
4812static void __exit mmc_exit(void)
4813{
Pierre Ossmane29a7d72007-05-26 13:48:18 +02004814 sdio_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004815 mmc_unregister_host_class();
4816 mmc_unregister_bus();
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004817}
4818
Mohan Srinivasan2320e1e2016-10-03 16:17:34 -07004819#ifdef CONFIG_BLOCK
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07004820static ssize_t
4821latency_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 */
4834static ssize_t
4835latency_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
4851static DEVICE_ATTR(latency_hist, S_IRUGO | S_IWUSR,
4852 latency_hist_show, latency_hist_store);
4853
4854void
4855mmc_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
4862void
4863mmc_latency_hist_sysfs_exit(struct mmc_host *host)
4864{
4865 device_remove_file(&host->class_dev, &dev_attr_latency_hist);
4866}
Mohan Srinivasan2320e1e2016-10-03 16:17:34 -07004867#endif
Mohan Srinivasan0ef170d2016-08-25 18:31:01 -07004868
Nicolas Pitre26074962007-06-16 02:07:53 -04004869subsys_initcall(mmc_init);
Pierre Ossmanffce2e72007-05-19 14:32:22 +02004870module_exit(mmc_exit);
4871
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872MODULE_LICENSE("GPL");