blob: 2f14334e42df91135fc88fdc474c0d318c4dfad3 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
Adrian Hunter5a436cc2017-03-20 19:50:31 +020017#include <linux/ktime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080018#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010019#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020023#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070024#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030025#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050026#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossman2f730fe2008-03-17 10:29:38 +010028#include <linux/leds.h>
29
Aries Lee22113ef2010-12-15 08:14:24 +010030#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080032#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080033#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080034#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmand129bce2006-03-24 03:18:17 -080036#include "sdhci.h"
37
38#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040#define DBG(f, x...) \
Adrian Hunterf4218652017-03-20 19:50:39 +020041 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080042
Adrian Hunter85ad90e2017-03-20 19:50:42 +020043#define SDHCI_DUMP(f, x...) \
44 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Kevin Liu52983382013-01-31 11:31:37 +080053static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunterd2898172017-03-20 19:50:43 +020055void sdhci_dumpregs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -080056{
Adrian Hunter85ad90e2017-03-20 19:50:42 +020057 SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Adrian Hunter85ad90e2017-03-20 19:50:42 +020059 SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
60 sdhci_readl(host, SDHCI_DMA_ADDRESS),
61 sdhci_readw(host, SDHCI_HOST_VERSION));
62 SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
63 sdhci_readw(host, SDHCI_BLOCK_SIZE),
64 sdhci_readw(host, SDHCI_BLOCK_COUNT));
65 SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
66 sdhci_readl(host, SDHCI_ARGUMENT),
67 sdhci_readw(host, SDHCI_TRANSFER_MODE));
68 SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
69 sdhci_readl(host, SDHCI_PRESENT_STATE),
70 sdhci_readb(host, SDHCI_HOST_CONTROL));
71 SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
72 sdhci_readb(host, SDHCI_POWER_CONTROL),
73 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
74 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
75 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
76 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
77 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
78 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
79 sdhci_readl(host, SDHCI_INT_STATUS));
80 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
81 sdhci_readl(host, SDHCI_INT_ENABLE),
82 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
83 SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
84 sdhci_readw(host, SDHCI_ACMD12_ERR),
85 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
86 SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
87 sdhci_readl(host, SDHCI_CAPABILITIES),
88 sdhci_readl(host, SDHCI_CAPABILITIES_1));
89 SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
90 sdhci_readw(host, SDHCI_COMMAND),
91 sdhci_readl(host, SDHCI_MAX_CURRENT));
92 SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020093 sdhci_readl(host, SDHCI_RESPONSE),
94 sdhci_readl(host, SDHCI_RESPONSE + 4));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020095 SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020096 sdhci_readl(host, SDHCI_RESPONSE + 8),
97 sdhci_readl(host, SDHCI_RESPONSE + 12));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020098 SDHCI_DUMP("Host ctl2: 0x%08x\n",
99 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800100
Adrian Huntere57a5f62014-11-04 12:42:46 +0200101 if (host->flags & SDHCI_USE_ADMA) {
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200102 if (host->flags & SDHCI_USE_64_BIT_DMA) {
103 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
104 sdhci_readl(host, SDHCI_ADMA_ERROR),
105 sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
106 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
107 } else {
108 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
109 sdhci_readl(host, SDHCI_ADMA_ERROR),
110 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
111 }
Adrian Huntere57a5f62014-11-04 12:42:46 +0200112 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100113
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200114 SDHCI_DUMP("============================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800115}
Adrian Hunterd2898172017-03-20 19:50:43 +0200116EXPORT_SYMBOL_GPL(sdhci_dumpregs);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Adrian Hunter56a590d2016-06-29 16:24:32 +0300124static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
125{
126 return cmd->data || cmd->flags & MMC_RSP_BUSY;
127}
128
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300129static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
130{
Russell King5b4f1f62014-04-25 12:57:02 +0100131 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300132
Adrian Hunterc79396c2011-12-27 15:48:42 +0200133 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900134 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300135 return;
136
Russell King5b4f1f62014-04-25 12:57:02 +0100137 if (enable) {
138 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
139 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800140
Russell King5b4f1f62014-04-25 12:57:02 +0100141 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
142 SDHCI_INT_CARD_INSERT;
143 } else {
144 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
145 }
Russell Kingb537f942014-04-25 12:56:01 +0100146
147 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
148 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300149}
150
151static void sdhci_enable_card_detection(struct sdhci_host *host)
152{
153 sdhci_set_card_detection(host, true);
154}
155
156static void sdhci_disable_card_detection(struct sdhci_host *host)
157{
158 sdhci_set_card_detection(host, false);
159}
160
Ulf Hansson02d0b682016-04-11 15:32:41 +0200161static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
162{
163 if (host->bus_on)
164 return;
165 host->bus_on = true;
166 pm_runtime_get_noresume(host->mmc->parent);
167}
168
169static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
170{
171 if (!host->bus_on)
172 return;
173 host->bus_on = false;
174 pm_runtime_put_noidle(host->mmc->parent);
175}
176
Russell King03231f92014-04-25 12:57:12 +0100177void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200179 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800180
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182
Adrian Hunterf0710a52013-05-06 12:17:32 +0300183 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300185 /* Reset-all turns off SD Bus Power */
186 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
187 sdhci_runtime_pm_bus_off(host);
188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189
Pierre Ossmane16514d82006-06-30 02:22:24 -0700190 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200191 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192
193 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200195 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530196 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 mmc_hostname(host->mmc), (int)mask);
198 sdhci_dumpregs(host);
199 return;
200 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200201 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 }
Russell King03231f92014-04-25 12:57:12 +0100203}
204EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300205
Russell King03231f92014-04-25 12:57:12 +0100206static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207{
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300209 struct mmc_host *mmc = host->mmc;
210
211 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100212 return;
213 }
214
215 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800216
Russell Kingda91a8f2014-04-25 13:00:12 +0100217 if (mask & SDHCI_RESET_ALL) {
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if (host->ops->enable_dma)
220 host->ops->enable_dma(host);
221 }
222
223 /* Resetting the controller clears many */
224 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200228static void sdhci_set_default_irqs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Russell Kingb537f942014-04-25 12:56:01 +0100230 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
231 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
232 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
233 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
234 SDHCI_INT_RESPONSE;
235
Dong Aishengf37b20e2016-07-12 15:46:17 +0800236 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
237 host->tuning_mode == SDHCI_TUNING_MODE_3)
238 host->ier |= SDHCI_INT_RETUNE;
239
Russell Kingb537f942014-04-25 12:56:01 +0100240 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
241 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200242}
243
244static void sdhci_init(struct sdhci_host *host, int soft)
245{
246 struct mmc_host *mmc = host->mmc;
247
248 if (soft)
249 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
250 else
251 sdhci_do_reset(host, SDHCI_RESET_ALL);
252
253 sdhci_set_default_irqs(host);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254
Adrian Hunterf12e39d2017-03-20 19:50:47 +0200255 host->cqe_on = false;
256
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800257 if (soft) {
258 /* force clock reconfiguration */
259 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300260 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800261 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300264static void sdhci_reinit(struct sdhci_host *host)
265{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800266 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300267 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268}
269
Adrian Hunter061d17a2016-04-12 14:25:09 +0300270static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271{
272 u8 ctrl;
273
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277}
278
Adrian Hunter061d17a2016-04-12 14:25:09 +0300279static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280{
281 u8 ctrl;
282
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300285 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286}
287
Masahiro Yamada4f782302016-04-14 13:19:39 +0900288#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300290 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100291{
292 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
293 unsigned long flags;
294
295 spin_lock_irqsave(&host->lock, flags);
296
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300297 if (host->runtime_suspended)
298 goto out;
299
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100300 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300301 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100302 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300303 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300304out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100305 spin_unlock_irqrestore(&host->lock, flags);
306}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300307
308static int sdhci_led_register(struct sdhci_host *host)
309{
310 struct mmc_host *mmc = host->mmc;
311
312 snprintf(host->led_name, sizeof(host->led_name),
313 "%s::", mmc_hostname(mmc));
314
315 host->led.name = host->led_name;
316 host->led.brightness = LED_OFF;
317 host->led.default_trigger = mmc_hostname(mmc);
318 host->led.brightness_set = sdhci_led_control;
319
320 return led_classdev_register(mmc_dev(mmc), &host->led);
321}
322
323static void sdhci_led_unregister(struct sdhci_host *host)
324{
325 led_classdev_unregister(&host->led);
326}
327
328static inline void sdhci_led_activate(struct sdhci_host *host)
329{
330}
331
332static inline void sdhci_led_deactivate(struct sdhci_host *host)
333{
334}
335
336#else
337
338static inline int sdhci_led_register(struct sdhci_host *host)
339{
340 return 0;
341}
342
343static inline void sdhci_led_unregister(struct sdhci_host *host)
344{
345}
346
347static inline void sdhci_led_activate(struct sdhci_host *host)
348{
349 __sdhci_led_activate(host);
350}
351
352static inline void sdhci_led_deactivate(struct sdhci_host *host)
353{
354 __sdhci_led_deactivate(host);
355}
356
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100357#endif
358
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359/*****************************************************************************\
360 * *
361 * Core functions *
362 * *
363\*****************************************************************************/
364
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366{
Pierre Ossman76591502008-07-21 00:32:11 +0200367 unsigned long flags;
368 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700369 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200370 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800371
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800373
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200375 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800378
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300380 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 blksize -= len;
385 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200386
Pierre Ossman76591502008-07-21 00:32:11 +0200387 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800388
Pierre Ossman76591502008-07-21 00:32:11 +0200389 while (len) {
390 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300391 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200392 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800393 }
Pierre Ossman76591502008-07-21 00:32:11 +0200394
395 *buf = scratch & 0xFF;
396
397 buf++;
398 scratch >>= 8;
399 chunk--;
400 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800401 }
402 }
Pierre Ossman76591502008-07-21 00:32:11 +0200403
404 sg_miter_stop(&host->sg_miter);
405
406 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800408
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409static void sdhci_write_block_pio(struct sdhci_host *host)
410{
Pierre Ossman76591502008-07-21 00:32:11 +0200411 unsigned long flags;
412 size_t blksize, len, chunk;
413 u32 scratch;
414 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100415
416 DBG("PIO writing\n");
417
418 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200419 chunk = 0;
420 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421
Pierre Ossman76591502008-07-21 00:32:11 +0200422 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423
424 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300425 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426
Pierre Ossman76591502008-07-21 00:32:11 +0200427 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200428
Pierre Ossman76591502008-07-21 00:32:11 +0200429 blksize -= len;
430 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431
Pierre Ossman76591502008-07-21 00:32:11 +0200432 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 while (len) {
435 scratch |= (u32)*buf << (chunk * 8);
436
437 buf++;
438 chunk++;
439 len--;
440
441 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300442 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200443 chunk = 0;
444 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100445 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100446 }
447 }
Pierre Ossman76591502008-07-21 00:32:11 +0200448
449 sg_miter_stop(&host->sg_miter);
450
451 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100452}
453
454static void sdhci_transfer_pio(struct sdhci_host *host)
455{
456 u32 mask;
457
Pierre Ossman76591502008-07-21 00:32:11 +0200458 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100459 return;
460
461 if (host->data->flags & MMC_DATA_READ)
462 mask = SDHCI_DATA_AVAILABLE;
463 else
464 mask = SDHCI_SPACE_AVAILABLE;
465
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200466 /*
467 * Some controllers (JMicron JMB38x) mess up the buffer bits
468 * for transfers < 4 bytes. As long as it is just one block,
469 * we can ignore the bits.
470 */
471 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
472 (host->data->blocks == 1))
473 mask = ~0;
474
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300475 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300476 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
477 udelay(100);
478
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100479 if (host->data->flags & MMC_DATA_READ)
480 sdhci_read_block_pio(host);
481 else
482 sdhci_write_block_pio(host);
483
Pierre Ossman76591502008-07-21 00:32:11 +0200484 host->blocks--;
485 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100486 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100487 }
488
489 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800490}
491
Russell King48857d92016-01-26 13:40:16 +0000492static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000493 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000494{
495 int sg_count;
496
Russell King94538e52016-01-26 13:40:37 +0000497 /*
498 * If the data buffers are already mapped, return the previous
499 * dma_map_sg() result.
500 */
501 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000502 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000503
504 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200505 mmc_get_dma_dir(data));
Russell King48857d92016-01-26 13:40:16 +0000506
507 if (sg_count == 0)
508 return -ENOSPC;
509
510 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000511 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000512
513 return sg_count;
514}
515
Pierre Ossman2134a922008-06-28 18:28:51 +0200516static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
517{
518 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800519 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200520}
521
522static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
523{
Cong Wang482fce92011-11-27 13:27:00 +0800524 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200525 local_irq_restore(*flags);
526}
527
Adrian Huntere57a5f62014-11-04 12:42:46 +0200528static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
529 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800530{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200531 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800532
Adrian Huntere57a5f62014-11-04 12:42:46 +0200533 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200534 dma_desc->cmd = cpu_to_le16(cmd);
535 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200536 dma_desc->addr_lo = cpu_to_le32((u32)addr);
537
538 if (host->flags & SDHCI_USE_64_BIT_DMA)
539 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800540}
541
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200542static void sdhci_adma_mark_end(void *desc)
543{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200544 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200545
Adrian Huntere57a5f62014-11-04 12:42:46 +0200546 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200547 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200548}
549
Russell King60c64762016-01-26 13:40:22 +0000550static void sdhci_adma_table_pre(struct sdhci_host *host,
551 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200552{
Pierre Ossman2134a922008-06-28 18:28:51 +0200553 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000555 dma_addr_t addr, align_addr;
556 void *desc, *align;
557 char *buffer;
558 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200559
560 /*
561 * The spec does not specify endianness of descriptor table.
562 * We currently guess that it is LE.
563 */
564
Russell King60c64762016-01-26 13:40:22 +0000565 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200566
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200567 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200568 align = host->align_buffer;
569
570 align_addr = host->align_addr;
571
572 for_each_sg(data->sg, sg, host->sg_count, i) {
573 addr = sg_dma_address(sg);
574 len = sg_dma_len(sg);
575
576 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000577 * The SDHCI specification states that ADMA addresses must
578 * be 32-bit aligned. If they aren't, then we use a bounce
579 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200580 * alignment.
581 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200582 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
583 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200584 if (offset) {
585 if (data->flags & MMC_DATA_WRITE) {
586 buffer = sdhci_kmap_atomic(sg, &flags);
587 memcpy(align, buffer, offset);
588 sdhci_kunmap_atomic(buffer, &flags);
589 }
590
Ben Dooks118cd172010-03-05 13:43:26 -0800591 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200592 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200593 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200594
595 BUG_ON(offset > 65536);
596
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200597 align += SDHCI_ADMA2_ALIGN;
598 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200599
Adrian Hunter76fe3792014-11-04 12:42:42 +0200600 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200601
602 addr += offset;
603 len -= offset;
604 }
605
Pierre Ossman2134a922008-06-28 18:28:51 +0200606 BUG_ON(len > 65536);
607
Adrian Hunter347ea322015-11-26 14:00:48 +0200608 if (len) {
609 /* tran, valid */
610 sdhci_adma_write_desc(host, desc, addr, len,
611 ADMA2_TRAN_VALID);
612 desc += host->desc_sz;
613 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200614
615 /*
616 * If this triggers then we have a calculation bug
617 * somewhere. :/
618 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200619 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200620 }
621
Thomas Abraham70764a92010-05-26 14:42:04 -0700622 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000623 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200624 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200625 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200626 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700627 }
628 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000629 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200630 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700631 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200632}
633
634static void sdhci_adma_table_post(struct sdhci_host *host,
635 struct mmc_data *data)
636{
Pierre Ossman2134a922008-06-28 18:28:51 +0200637 struct scatterlist *sg;
638 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200639 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200640 char *buffer;
641 unsigned long flags;
642
Russell King47fa9612016-01-26 13:40:06 +0000643 if (data->flags & MMC_DATA_READ) {
644 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100645
Russell King47fa9612016-01-26 13:40:06 +0000646 /* Do a quick scan of the SG list for any unaligned mappings */
647 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200648 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000649 has_unaligned = true;
650 break;
651 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200652
Russell King47fa9612016-01-26 13:40:06 +0000653 if (has_unaligned) {
654 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000655 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200656
Russell King47fa9612016-01-26 13:40:06 +0000657 align = host->align_buffer;
658
659 for_each_sg(data->sg, sg, host->sg_count, i) {
660 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
661 size = SDHCI_ADMA2_ALIGN -
662 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
663
664 buffer = sdhci_kmap_atomic(sg, &flags);
665 memcpy(buffer, align, size);
666 sdhci_kunmap_atomic(buffer, &flags);
667
668 align += SDHCI_ADMA2_ALIGN;
669 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200670 }
671 }
672 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200673}
674
Andrei Warkentina3c77782011-04-11 16:13:42 -0500675static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800676{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700677 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500678 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700679 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800680
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200681 /*
682 * If the host controller provides us with an incorrect timeout
683 * value, just skip the check and use 0xE. The hardware may take
684 * longer to time out, but that's much better than having a too-short
685 * timeout value.
686 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200687 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200688 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200689
Andrei Warkentina3c77782011-04-11 16:13:42 -0500690 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100691 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500692 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800693
Andrei Warkentina3c77782011-04-11 16:13:42 -0500694 /* timeout in us */
695 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100696 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300697 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000698 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000699 if (host->clock && data->timeout_clks) {
700 unsigned long long val;
701
702 /*
703 * data->timeout_clks is in units of clock cycles.
704 * host->clock is in Hz. target_timeout is in us.
705 * Hence, us = 1000000 * cycles / Hz. Round up.
706 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200707 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000708 if (do_div(val, host->clock))
709 target_timeout++;
710 target_timeout += val;
711 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300712 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700713
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700714 /*
715 * Figure out needed cycles.
716 * We do this in steps in order to fit inside a 32 bit int.
717 * The first step is the minimum timeout, which will have a
718 * minimum resolution of 6 bits:
719 * (1) 2^13*1000 > 2^22,
720 * (2) host->timeout_clk < 2^16
721 * =>
722 * (1) / (2) > 2^6
723 */
724 count = 0;
725 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
726 while (current_timeout < target_timeout) {
727 count++;
728 current_timeout <<= 1;
729 if (count >= 0xF)
730 break;
731 }
732
733 if (count >= 0xF) {
Adrian Hunterf4218652017-03-20 19:50:39 +0200734 DBG("Too large timeout 0x%x requested for CMD%d!\n",
735 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700736 count = 0xE;
737 }
738
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200739 return count;
740}
741
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300742static void sdhci_set_transfer_irqs(struct sdhci_host *host)
743{
744 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
745 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
746
747 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100748 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300749 else
Russell Kingb537f942014-04-25 12:56:01 +0100750 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
751
752 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
753 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300754}
755
Aisheng Dongb45e6682014-08-27 15:26:29 +0800756static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200757{
758 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800759
760 if (host->ops->set_timeout) {
761 host->ops->set_timeout(host, cmd);
762 } else {
763 count = sdhci_calc_timeout(host, cmd);
764 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
765 }
766}
767
768static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
769{
Pierre Ossman2134a922008-06-28 18:28:51 +0200770 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500771 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200772
Adrian Hunter56a590d2016-06-29 16:24:32 +0300773 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800774 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500775
776 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200777 return;
778
Adrian Hunter43dea092016-06-29 16:24:26 +0300779 WARN_ON(host->data);
780
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200781 /* Sanity checks */
782 BUG_ON(data->blksz * data->blocks > 524288);
783 BUG_ON(data->blksz > host->mmc->max_blk_size);
784 BUG_ON(data->blocks > 65535);
785
786 host->data = data;
787 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400788 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200789
Russell Kingfce14422016-01-26 13:41:20 +0000790 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200791 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000792 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000793 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200794
Russell Kingfce14422016-01-26 13:41:20 +0000795 host->flags |= SDHCI_REQ_USE_DMA;
796
797 /*
798 * FIXME: This doesn't account for merging when mapping the
799 * scatterlist.
800 *
801 * The assumption here being that alignment and lengths are
802 * the same after DMA mapping to device address space.
803 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000804 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000805 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200806 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000807 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000808 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000809 /*
810 * As we use up to 3 byte chunks to work
811 * around alignment problems, we need to
812 * check the offset as well.
813 */
814 offset_mask = 3;
815 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200816 } else {
817 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000818 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000819 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
820 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200821 }
822
Russell Kingdf953922016-01-26 13:41:14 +0000823 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200824 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000825 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100826 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000827 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200828 host->flags &= ~SDHCI_REQ_USE_DMA;
829 break;
830 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000831 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100832 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200833 host->flags &= ~SDHCI_REQ_USE_DMA;
834 break;
835 }
836 }
837 }
838 }
839
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200840 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000841 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200842
Russell King60c64762016-01-26 13:40:22 +0000843 if (sg_cnt <= 0) {
844 /*
845 * This only happens when someone fed
846 * us an invalid request.
847 */
848 WARN_ON(1);
849 host->flags &= ~SDHCI_REQ_USE_DMA;
850 } else if (host->flags & SDHCI_USE_ADMA) {
851 sdhci_adma_table_pre(host, data, sg_cnt);
852
853 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
854 if (host->flags & SDHCI_USE_64_BIT_DMA)
855 sdhci_writel(host,
856 (u64)host->adma_addr >> 32,
857 SDHCI_ADMA_ADDRESS_HI);
858 } else {
859 WARN_ON(sg_cnt != 1);
860 sdhci_writel(host, sg_dma_address(data->sg),
861 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200862 }
863 }
864
Pierre Ossman2134a922008-06-28 18:28:51 +0200865 /*
866 * Always adjust the DMA selection as some controllers
867 * (e.g. JMicron) can't do PIO properly when the selection
868 * is ADMA.
869 */
870 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300871 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200872 ctrl &= ~SDHCI_CTRL_DMA_MASK;
873 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200874 (host->flags & SDHCI_USE_ADMA)) {
875 if (host->flags & SDHCI_USE_64_BIT_DMA)
876 ctrl |= SDHCI_CTRL_ADMA64;
877 else
878 ctrl |= SDHCI_CTRL_ADMA32;
879 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200880 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200881 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300882 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100883 }
884
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200885 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200886 int flags;
887
888 flags = SG_MITER_ATOMIC;
889 if (host->data->flags & MMC_DATA_READ)
890 flags |= SG_MITER_TO_SG;
891 else
892 flags |= SG_MITER_FROM_SG;
893 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200894 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800895 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300897 sdhci_set_transfer_irqs(host);
898
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400899 /* Set the DMA boundary value and block size */
Srinivas Kandagatlac846a002017-08-03 14:46:13 +0200900 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
901 SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300902 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700903}
904
Adrian Hunter0293d502016-06-29 16:24:35 +0300905static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
906 struct mmc_request *mrq)
907{
Adrian Hunter20845be2016-08-16 13:44:13 +0300908 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
909 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300910}
911
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500913 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700914{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800915 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500916 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700917
Dong Aisheng2b558c12013-10-30 22:09:48 +0800918 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800919 if (host->quirks2 &
920 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
921 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
922 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800923 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800924 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
925 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800926 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800927 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700928 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800929 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700930
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200931 WARN_ON(!host->data);
932
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800933 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
934 mode = SDHCI_TRNS_BLK_CNT_EN;
935
Andrei Warkentine89d4562011-05-23 15:06:37 -0500936 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800937 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500938 /*
939 * If we are sending CMD23, CMD12 never gets sent
940 * on successful completion (so no Auto-CMD12).
941 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300942 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800943 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500944 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300945 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500946 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300947 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500948 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700949 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500950
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700951 if (data->flags & MMC_DATA_READ)
952 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100953 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700954 mode |= SDHCI_TRNS_DMA;
955
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300956 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800957}
958
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300959static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
960{
961 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
962 ((mrq->cmd && mrq->cmd->error) ||
963 (mrq->sbc && mrq->sbc->error) ||
964 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
965 (mrq->data->stop && mrq->data->stop->error))) ||
966 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
967}
968
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300969static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
970{
971 int i;
972
973 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
974 if (host->mrqs_done[i] == mrq) {
975 WARN_ON(1);
976 return;
977 }
978 }
979
980 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
981 if (!host->mrqs_done[i]) {
982 host->mrqs_done[i] = mrq;
983 break;
984 }
985 }
986
987 WARN_ON(i >= SDHCI_MAX_MRQS);
988
989 tasklet_schedule(&host->finish_tasklet);
990}
991
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300992static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
993{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300994 if (host->cmd && host->cmd->mrq == mrq)
995 host->cmd = NULL;
996
997 if (host->data_cmd && host->data_cmd->mrq == mrq)
998 host->data_cmd = NULL;
999
1000 if (host->data && host->data->mrq == mrq)
1001 host->data = NULL;
1002
Adrian Huntered1563d2016-06-29 16:24:29 +03001003 if (sdhci_needs_reset(host, mrq))
1004 host->pending_reset = true;
1005
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001006 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001007}
1008
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009static void sdhci_finish_data(struct sdhci_host *host)
1010{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001011 struct mmc_command *data_cmd = host->data_cmd;
1012 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001015 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016
Russell Kingadd89132016-01-26 13:40:42 +00001017 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1018 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1019 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020
1021 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001022 * The specification states that the block count register must
1023 * be updated, but it does not specify at what point in the
1024 * data flow. That makes the register entirely useless to read
1025 * back so we have to assume that nothing made it to the card
1026 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001027 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001028 if (data->error)
1029 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001030 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001031 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001032
Andrei Warkentine89d4562011-05-23 15:06:37 -05001033 /*
1034 * Need to send CMD12 if -
1035 * a) open-ended multiblock transfer (no CMD23)
1036 * b) error in multiblock transfer
1037 */
1038 if (data->stop &&
1039 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001040 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001041
Pierre Ossmand129bce2006-03-24 03:18:17 -08001042 /*
1043 * The controller needs a reset of internal state machines
1044 * upon error conditions.
1045 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001046 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001047 if (!host->cmd || host->cmd == data_cmd)
1048 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001049 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001050 }
1051
Adrian Hunter20845be2016-08-16 13:44:13 +03001052 /*
1053 * 'cap_cmd_during_tfr' request must not use the command line
1054 * after mmc_command_done() has been called. It is upper layer's
1055 * responsibility to send the stop command if required.
1056 */
1057 if (data->mrq->cap_cmd_during_tfr) {
1058 sdhci_finish_mrq(host, data->mrq);
1059 } else {
1060 /* Avoid triggering warning in sdhci_send_command() */
1061 host->cmd = NULL;
1062 sdhci_send_command(host, data->stop);
1063 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001064 } else {
1065 sdhci_finish_mrq(host, data->mrq);
1066 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001067}
1068
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001069static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1070 unsigned long timeout)
1071{
1072 if (sdhci_data_line_cmd(mrq->cmd))
1073 mod_timer(&host->data_timer, timeout);
1074 else
1075 mod_timer(&host->timer, timeout);
1076}
1077
1078static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1079{
1080 if (sdhci_data_line_cmd(mrq->cmd))
1081 del_timer(&host->data_timer);
1082 else
1083 del_timer(&host->timer);
1084}
1085
Dong Aishengc0e551292013-09-13 19:11:31 +08001086void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001087{
1088 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001089 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001090 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
1092 WARN_ON(host->cmd);
1093
Russell King96776202016-01-26 13:39:34 +00001094 /* Initially, a command has no error */
1095 cmd->error = 0;
1096
Adrian Hunterfc605f12016-10-05 12:11:21 +03001097 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1098 cmd->opcode == MMC_STOP_TRANSMISSION)
1099 cmd->flags |= MMC_RSP_BUSY;
1100
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001102 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001103
1104 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001105 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001106 mask |= SDHCI_DATA_INHIBIT;
1107
1108 /* We shouldn't wait for data inihibit for stop commands, even
1109 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001110 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001111 mask &= ~SDHCI_DATA_INHIBIT;
1112
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001113 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001114 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001115 pr_err("%s: Controller never released inhibit bit(s).\n",
1116 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001117 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001118 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001119 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001120 return;
1121 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001122 timeout--;
1123 mdelay(1);
1124 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001126 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001127 if (!cmd->data && cmd->busy_timeout > 9000)
1128 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001129 else
1130 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001131 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132
1133 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001134 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001135 WARN_ON(host->data_cmd);
1136 host->data_cmd = cmd;
1137 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138
Andrei Warkentina3c77782011-04-11 16:13:42 -05001139 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001141 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142
Andrei Warkentine89d4562011-05-23 15:06:37 -05001143 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001144
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301146 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001147 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001148 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001149 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001150 return;
1151 }
1152
1153 if (!(cmd->flags & MMC_RSP_PRESENT))
1154 flags = SDHCI_CMD_RESP_NONE;
1155 else if (cmd->flags & MMC_RSP_136)
1156 flags = SDHCI_CMD_RESP_LONG;
1157 else if (cmd->flags & MMC_RSP_BUSY)
1158 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1159 else
1160 flags = SDHCI_CMD_RESP_SHORT;
1161
1162 if (cmd->flags & MMC_RSP_CRC)
1163 flags |= SDHCI_CMD_CRC;
1164 if (cmd->flags & MMC_RSP_OPCODE)
1165 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301166
1167 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301168 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1169 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001170 flags |= SDHCI_CMD_DATA;
1171
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001172 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173}
Dong Aishengc0e551292013-09-13 19:11:31 +08001174EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001175
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301176static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
1177{
1178 int i, reg;
1179
1180 for (i = 0; i < 4; i++) {
1181 reg = SDHCI_RESPONSE + (3 - i) * 4;
1182 cmd->resp[i] = sdhci_readl(host, reg);
1183 }
1184
Kishon Vijay Abraham I1284c242017-08-21 13:11:29 +05301185 if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
1186 return;
1187
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301188 /* CRC is stripped so we need to do some shifting */
1189 for (i = 0; i < 4; i++) {
1190 cmd->resp[i] <<= 8;
1191 if (i != 3)
1192 cmd->resp[i] |= cmd->resp[i + 1] >> 24;
1193 }
1194}
1195
Pierre Ossmand129bce2006-03-24 03:18:17 -08001196static void sdhci_finish_command(struct sdhci_host *host)
1197{
Adrian Huntere0a56402016-06-29 16:24:22 +03001198 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001199
Adrian Huntere0a56402016-06-29 16:24:22 +03001200 host->cmd = NULL;
1201
1202 if (cmd->flags & MMC_RSP_PRESENT) {
1203 if (cmd->flags & MMC_RSP_136) {
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301204 sdhci_read_rsp_136(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001205 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001206 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001207 }
1208 }
1209
Adrian Hunter20845be2016-08-16 13:44:13 +03001210 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1211 mmc_command_done(host->mmc, cmd->mrq);
1212
Adrian Hunter6bde8682016-06-29 16:24:20 +03001213 /*
1214 * The host can send and interrupt when the busy state has
1215 * ended, allowing us to wait without wasting CPU cycles.
1216 * The busy signal uses DAT0 so this is similar to waiting
1217 * for data to complete.
1218 *
1219 * Note: The 1.0 specification is a bit ambiguous about this
1220 * feature so there might be some problems with older
1221 * controllers.
1222 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001223 if (cmd->flags & MMC_RSP_BUSY) {
1224 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001225 DBG("Cannot wait for busy signal when also doing a data transfer");
1226 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001227 cmd == host->data_cmd) {
1228 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001229 return;
1230 }
1231 }
1232
Andrei Warkentine89d4562011-05-23 15:06:37 -05001233 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001234 if (cmd == cmd->mrq->sbc) {
1235 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001236 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001237
Andrei Warkentine89d4562011-05-23 15:06:37 -05001238 /* Processed actual command. */
1239 if (host->data && host->data_early)
1240 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001241
Adrian Huntere0a56402016-06-29 16:24:22 +03001242 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001243 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001244 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001245}
1246
Kevin Liu52983382013-01-31 11:31:37 +08001247static u16 sdhci_get_preset_value(struct sdhci_host *host)
1248{
Russell Kingd975f122014-04-25 12:59:31 +01001249 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001250
Russell Kingd975f122014-04-25 12:59:31 +01001251 switch (host->timing) {
1252 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001253 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1254 break;
Russell Kingd975f122014-04-25 12:59:31 +01001255 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001256 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1257 break;
Russell Kingd975f122014-04-25 12:59:31 +01001258 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001259 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1260 break;
Russell Kingd975f122014-04-25 12:59:31 +01001261 case MMC_TIMING_UHS_SDR104:
1262 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001263 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1264 break;
Russell Kingd975f122014-04-25 12:59:31 +01001265 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001266 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001267 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1268 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001269 case MMC_TIMING_MMC_HS400:
1270 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1271 break;
Kevin Liu52983382013-01-31 11:31:37 +08001272 default:
1273 pr_warn("%s: Invalid UHS-I mode selected\n",
1274 mmc_hostname(host->mmc));
1275 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1276 break;
1277 }
1278 return preset;
1279}
1280
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001281u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1282 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001283{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301284 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001285 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301286 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001287 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001288
Zhangfei Gao85105c52010-08-06 07:10:01 +08001289 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001290 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001291 u16 pre_val;
1292
1293 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1294 pre_val = sdhci_get_preset_value(host);
1295 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1296 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1297 if (host->clk_mul &&
1298 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1299 clk = SDHCI_PROG_CLOCK_MODE;
1300 real_div = div + 1;
1301 clk_mul = host->clk_mul;
1302 } else {
1303 real_div = max_t(int, 1, div << 1);
1304 }
1305 goto clock_set;
1306 }
1307
Arindam Nathc3ed3872011-05-05 12:19:06 +05301308 /*
1309 * Check if the Host Controller supports Programmable Clock
1310 * Mode.
1311 */
1312 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001313 for (div = 1; div <= 1024; div++) {
1314 if ((host->max_clk * host->clk_mul / div)
1315 <= clock)
1316 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001317 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001318 if ((host->max_clk * host->clk_mul / div) <= clock) {
1319 /*
1320 * Set Programmable Clock Mode in the Clock
1321 * Control register.
1322 */
1323 clk = SDHCI_PROG_CLOCK_MODE;
1324 real_div = div;
1325 clk_mul = host->clk_mul;
1326 div--;
1327 } else {
1328 /*
1329 * Divisor can be too small to reach clock
1330 * speed requirement. Then use the base clock.
1331 */
1332 switch_base_clk = true;
1333 }
1334 }
1335
1336 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301337 /* Version 3.00 divisors must be a multiple of 2. */
1338 if (host->max_clk <= clock)
1339 div = 1;
1340 else {
1341 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1342 div += 2) {
1343 if ((host->max_clk / div) <= clock)
1344 break;
1345 }
1346 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001347 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301348 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301349 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1350 && !div && host->max_clk <= 25000000)
1351 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001352 }
1353 } else {
1354 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001355 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001356 if ((host->max_clk / div) <= clock)
1357 break;
1358 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001359 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301360 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001362
Kevin Liu52983382013-01-31 11:31:37 +08001363clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001364 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001365 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301366 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001367 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1368 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001369
1370 return clk;
1371}
1372EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1373
Ritesh Harjanifec79672016-11-21 12:07:19 +05301374void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001375{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001376 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001377
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001379 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001380
Chris Ball27f6cb12009-09-22 16:45:31 -07001381 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001382 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001383 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001384 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001385 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001386 pr_err("%s: Internal clock never stabilised.\n",
1387 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388 sdhci_dumpregs(host);
1389 return;
1390 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001391 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001392 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001393
1394 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001395 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301397EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1398
1399void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1400{
1401 u16 clk;
1402
1403 host->mmc->actual_clock = 0;
1404
1405 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1406
1407 if (clock == 0)
1408 return;
1409
1410 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1411 sdhci_enable_clk(host, clk);
1412}
Russell King17710592014-04-25 12:58:55 +01001413EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001414
Adrian Hunter1dceb042016-03-29 12:45:43 +03001415static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1416 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001417{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001418 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001419
Adrian Hunter1dceb042016-03-29 12:45:43 +03001420 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001421
1422 if (mode != MMC_POWER_OFF)
1423 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1424 else
1425 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1426}
1427
Adrian Hunter606d3132016-10-05 12:11:22 +03001428void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1429 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001430{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001431 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001432
Russell King24fbb3c2014-04-25 13:00:06 +01001433 if (mode != MMC_POWER_OFF) {
1434 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001435 case MMC_VDD_165_195:
1436 pwr = SDHCI_POWER_180;
1437 break;
1438 case MMC_VDD_29_30:
1439 case MMC_VDD_30_31:
1440 pwr = SDHCI_POWER_300;
1441 break;
1442 case MMC_VDD_32_33:
1443 case MMC_VDD_33_34:
1444 pwr = SDHCI_POWER_330;
1445 break;
1446 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001447 WARN(1, "%s: Invalid vdd %#x\n",
1448 mmc_hostname(host->mmc), vdd);
1449 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001450 }
1451 }
1452
1453 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001454 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001455
Pierre Ossmanae628902009-05-03 20:45:03 +02001456 host->pwr = pwr;
1457
1458 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001459 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001460 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1461 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001462 } else {
1463 /*
1464 * Spec says that we should clear the power reg before setting
1465 * a new value. Some controllers don't seem to like this though.
1466 */
1467 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1468 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001469
Russell Kinge921a8b2014-04-25 13:00:01 +01001470 /*
1471 * At least the Marvell CaFe chip gets confused if we set the
1472 * voltage and set turn on power at the same time, so set the
1473 * voltage first.
1474 */
1475 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1476 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001477
Russell Kinge921a8b2014-04-25 13:00:01 +01001478 pwr |= SDHCI_POWER_ON;
1479
Pierre Ossmanae628902009-05-03 20:45:03 +02001480 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1481
Russell Kinge921a8b2014-04-25 13:00:01 +01001482 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1483 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001484
Russell Kinge921a8b2014-04-25 13:00:01 +01001485 /*
1486 * Some controllers need an extra 10ms delay of 10ms before
1487 * they can apply clock after applying power
1488 */
1489 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1490 mdelay(10);
1491 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001492}
Adrian Hunter606d3132016-10-05 12:11:22 +03001493EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001494
Adrian Hunter606d3132016-10-05 12:11:22 +03001495void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1496 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001497{
Adrian Hunter606d3132016-10-05 12:11:22 +03001498 if (IS_ERR(host->mmc->supply.vmmc))
1499 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001500 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001501 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001502}
Adrian Hunter606d3132016-10-05 12:11:22 +03001503EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001504
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505/*****************************************************************************\
1506 * *
1507 * MMC callbacks *
1508 * *
1509\*****************************************************************************/
1510
1511static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1512{
1513 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001514 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001515 unsigned long flags;
1516
1517 host = mmc_priv(mmc);
1518
Scott Branden04e079cf2015-03-10 11:35:10 -07001519 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001520 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001521
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522 spin_lock_irqsave(&host->lock, flags);
1523
Adrian Hunter061d17a2016-04-12 14:25:09 +03001524 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001525
1526 /*
1527 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1528 * requests if Auto-CMD12 is enabled.
1529 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001530 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001531 if (mrq->stop) {
1532 mrq->data->stop = NULL;
1533 mrq->stop = NULL;
1534 }
1535 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001536
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001537 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001538 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001539 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301540 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001541 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001542 sdhci_send_command(host, mrq->sbc);
1543 else
1544 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301545 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001546
Pierre Ossman5f25a662006-10-04 02:15:39 -07001547 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001548 spin_unlock_irqrestore(&host->lock, flags);
1549}
1550
Russell King2317f562014-04-25 12:57:07 +01001551void sdhci_set_bus_width(struct sdhci_host *host, int width)
1552{
1553 u8 ctrl;
1554
1555 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1556 if (width == MMC_BUS_WIDTH_8) {
1557 ctrl &= ~SDHCI_CTRL_4BITBUS;
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001558 ctrl |= SDHCI_CTRL_8BITBUS;
Russell King2317f562014-04-25 12:57:07 +01001559 } else {
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001560 if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
Russell King2317f562014-04-25 12:57:07 +01001561 ctrl &= ~SDHCI_CTRL_8BITBUS;
1562 if (width == MMC_BUS_WIDTH_4)
1563 ctrl |= SDHCI_CTRL_4BITBUS;
1564 else
1565 ctrl &= ~SDHCI_CTRL_4BITBUS;
1566 }
1567 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1568}
1569EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1570
Russell King96d7b782014-04-25 12:59:26 +01001571void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1572{
1573 u16 ctrl_2;
1574
1575 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1576 /* Select Bus Speed Mode for host */
1577 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1578 if ((timing == MMC_TIMING_MMC_HS200) ||
1579 (timing == MMC_TIMING_UHS_SDR104))
1580 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1581 else if (timing == MMC_TIMING_UHS_SDR12)
1582 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1583 else if (timing == MMC_TIMING_UHS_SDR25)
1584 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1585 else if (timing == MMC_TIMING_UHS_SDR50)
1586 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1587 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1588 (timing == MMC_TIMING_MMC_DDR52))
1589 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001590 else if (timing == MMC_TIMING_MMC_HS400)
1591 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001592 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1593}
1594EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1595
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001596void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001597{
Dong Aishengded97e02016-04-16 01:29:25 +08001598 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001599 u8 ctrl;
1600
Adrian Hunter84ec0482016-12-19 15:33:11 +02001601 if (ios->power_mode == MMC_POWER_UNDEFINED)
1602 return;
1603
Adrian Hunterceb61432011-12-27 15:48:41 +02001604 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001605 if (!IS_ERR(mmc->supply.vmmc) &&
1606 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001607 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001608 return;
1609 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001610
Pierre Ossmand129bce2006-03-24 03:18:17 -08001611 /*
1612 * Reset the chip on each power off.
1613 * Should clear out any weird states.
1614 */
1615 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001616 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001617 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001618 }
1619
Kevin Liu52983382013-01-31 11:31:37 +08001620 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001621 (ios->power_mode == MMC_POWER_UP) &&
1622 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001623 sdhci_enable_preset_value(host, false);
1624
Russell King373073e2014-04-25 12:58:45 +01001625 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001626 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001627 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001628
1629 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1630 host->clock) {
1631 host->timeout_clk = host->mmc->actual_clock ?
1632 host->mmc->actual_clock / 1000 :
1633 host->clock / 1000;
1634 host->mmc->max_busy_timeout =
1635 host->ops->get_max_timeout_count ?
1636 host->ops->get_max_timeout_count(host) :
1637 1 << 27;
1638 host->mmc->max_busy_timeout /= host->timeout_clk;
1639 }
Russell King373073e2014-04-25 12:58:45 +01001640 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001641
Adrian Hunter606d3132016-10-05 12:11:22 +03001642 if (host->ops->set_power)
1643 host->ops->set_power(host, ios->power_mode, ios->vdd);
1644 else
1645 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001646
Philip Rakity643a81f2010-09-23 08:24:32 -07001647 if (host->ops->platform_send_init_74_clocks)
1648 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1649
Russell King2317f562014-04-25 12:57:07 +01001650 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001651
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001652 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001653
yangbo lu501639b2017-08-15 10:16:47 +08001654 if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
1655 if (ios->timing == MMC_TIMING_SD_HS ||
1656 ios->timing == MMC_TIMING_MMC_HS ||
1657 ios->timing == MMC_TIMING_MMC_HS400 ||
1658 ios->timing == MMC_TIMING_MMC_HS200 ||
1659 ios->timing == MMC_TIMING_MMC_DDR52 ||
1660 ios->timing == MMC_TIMING_UHS_SDR50 ||
1661 ios->timing == MMC_TIMING_UHS_SDR104 ||
1662 ios->timing == MMC_TIMING_UHS_DDR50 ||
1663 ios->timing == MMC_TIMING_UHS_SDR25)
1664 ctrl |= SDHCI_CTRL_HISPD;
1665 else
1666 ctrl &= ~SDHCI_CTRL_HISPD;
1667 }
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001668
Arindam Nathd6d50a12011-05-05 12:18:59 +05301669 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301670 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301671
Russell Kingda91a8f2014-04-25 13:00:12 +01001672 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301673 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301674 /*
1675 * We only need to set Driver Strength if the
1676 * preset value enable is not set.
1677 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001678 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301679 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1680 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1681 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001682 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1683 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301684 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1685 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001686 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1687 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1688 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001689 pr_warn("%s: invalid driver type, default to driver type B\n",
1690 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001691 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1692 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301693
1694 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301695 } else {
1696 /*
1697 * According to SDHC Spec v3.00, if the Preset Value
1698 * Enable in the Host Control 2 register is set, we
1699 * need to reset SD Clock Enable before changing High
1700 * Speed Enable to avoid generating clock gliches.
1701 */
Arindam Nath758535c2011-05-05 12:19:00 +05301702
1703 /* Reset SD Clock Enable */
1704 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1705 clk &= ~SDHCI_CLOCK_CARD_EN;
1706 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1707
1708 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1709
1710 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001711 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301712 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301713
Arindam Nath49c468f2011-05-05 12:19:01 +05301714 /* Reset SD Clock Enable */
1715 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1716 clk &= ~SDHCI_CLOCK_CARD_EN;
1717 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1718
Russell King96d7b782014-04-25 12:59:26 +01001719 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001720 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301721
Kevin Liu52983382013-01-31 11:31:37 +08001722 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1723 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1724 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1725 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1726 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001727 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1728 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001729 u16 preset;
1730
1731 sdhci_enable_preset_value(host, true);
1732 preset = sdhci_get_preset_value(host);
1733 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1734 >> SDHCI_PRESET_DRV_SHIFT;
1735 }
1736
Arindam Nath49c468f2011-05-05 12:19:01 +05301737 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001738 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301739 } else
1740 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301741
Leandro Dorileob8352262007-07-25 23:47:04 +02001742 /*
1743 * Some (ENE) controllers go apeshit on some ios operation,
1744 * signalling timeout and CRC errors even on CMD0. Resetting
1745 * it on each ios seems to solve the problem.
1746 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301747 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001748 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001749
Pierre Ossman5f25a662006-10-04 02:15:39 -07001750 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001751}
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001752EXPORT_SYMBOL_GPL(sdhci_set_ios);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001753
Dong Aishengded97e02016-04-16 01:29:25 +08001754static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001755{
1756 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001757 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001758
1759 if (host->flags & SDHCI_DEVICE_DEAD)
1760 return 0;
1761
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001762 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001763 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001764 return 1;
1765
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001766 /*
1767 * Try slot gpio detect, if defined it take precedence
1768 * over build in controller functionality
1769 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001770 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001771 return !!gpio_cd;
1772
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001773 /* If polling, assume that the card is always present. */
1774 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1775 return 1;
1776
Kevin Liu94144a42013-02-28 17:35:53 +08001777 /* Host native card detect */
1778 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1779}
1780
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001781static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001782{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001783 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001784 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001785
Pierre Ossmand129bce2006-03-24 03:18:17 -08001786 spin_lock_irqsave(&host->lock, flags);
1787
Pierre Ossman1e728592008-04-16 19:13:13 +02001788 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001789 is_readonly = 0;
1790 else if (host->ops->get_ro)
1791 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001792 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001793 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1794 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001795
1796 spin_unlock_irqrestore(&host->lock, flags);
1797
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001798 /* This quirk needs to be replaced by a callback-function later */
1799 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1800 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001801}
1802
Takashi Iwai82b0e232011-04-21 20:26:38 +02001803#define SAMPLE_COUNT 5
1804
Dong Aishengded97e02016-04-16 01:29:25 +08001805static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001806{
Dong Aishengded97e02016-04-16 01:29:25 +08001807 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001808 int i, ro_count;
1809
Takashi Iwai82b0e232011-04-21 20:26:38 +02001810 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001811 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001812
1813 ro_count = 0;
1814 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001815 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001816 if (++ro_count > SAMPLE_COUNT / 2)
1817 return 1;
1818 }
1819 msleep(30);
1820 }
1821 return 0;
1822}
1823
Adrian Hunter20758b62011-08-29 16:42:12 +03001824static void sdhci_hw_reset(struct mmc_host *mmc)
1825{
1826 struct sdhci_host *host = mmc_priv(mmc);
1827
1828 if (host->ops && host->ops->hw_reset)
1829 host->ops->hw_reset(host);
1830}
1831
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001832static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1833{
Russell Kingbe138552014-04-25 12:55:56 +01001834 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001835 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001836 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001837 else
Russell Kingb537f942014-04-25 12:56:01 +01001838 host->ier &= ~SDHCI_INT_CARD_INT;
1839
1840 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1841 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001842 mmiowb();
1843 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001844}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001845
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001846void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001847{
1848 struct sdhci_host *host = mmc_priv(mmc);
1849 unsigned long flags;
1850
Hans de Goede923713b2017-03-26 13:14:45 +02001851 if (enable)
1852 pm_runtime_get_noresume(host->mmc->parent);
1853
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001854 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001855 if (enable)
1856 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1857 else
1858 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1859
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001860 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001861 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001862
1863 if (!enable)
1864 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001865}
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001866EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001867
Hu Zijic376ea92017-03-30 17:22:56 +02001868int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1869 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001870{
Dong Aishengded97e02016-04-16 01:29:25 +08001871 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001872 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001873 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001874
1875 /*
1876 * Signal Voltage Switching is only applicable for Host Controllers
1877 * v3.00 and above.
1878 */
1879 if (host->version < SDHCI_SPEC_300)
1880 return 0;
1881
Philip Rakity6231f3d2012-07-23 15:56:23 -07001882 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001883
Fabio Estevam21f59982013-02-14 10:35:03 -02001884 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001885 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001886 if (!(host->flags & SDHCI_SIGNALING_330))
1887 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001888 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1889 ctrl &= ~SDHCI_CTRL_VDD_180;
1890 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1891
Tim Kryger3a48edc2014-06-13 10:13:56 -07001892 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001893 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001894 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001895 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1896 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001897 return -EIO;
1898 }
1899 }
1900 /* Wait for 5ms */
1901 usleep_range(5000, 5500);
1902
1903 /* 3.3V regulator output should be stable within 5 ms */
1904 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1905 if (!(ctrl & SDHCI_CTRL_VDD_180))
1906 return 0;
1907
Joe Perches66061102014-09-12 14:56:56 -07001908 pr_warn("%s: 3.3V regulator output did not became stable\n",
1909 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001910
1911 return -EAGAIN;
1912 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001913 if (!(host->flags & SDHCI_SIGNALING_180))
1914 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001915 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001916 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001917 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001918 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1919 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001920 return -EIO;
1921 }
1922 }
1923
1924 /*
1925 * Enable 1.8V Signal Enable in the Host Control2
1926 * register
1927 */
1928 ctrl |= SDHCI_CTRL_VDD_180;
1929 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1930
Vincent Yang9d967a62015-01-20 16:05:15 +08001931 /* Some controller need to do more when switching */
1932 if (host->ops->voltage_switch)
1933 host->ops->voltage_switch(host);
1934
Kevin Liu20b92a32012-12-17 19:29:26 +08001935 /* 1.8V regulator output should be stable within 5 ms */
1936 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1937 if (ctrl & SDHCI_CTRL_VDD_180)
1938 return 0;
1939
Joe Perches66061102014-09-12 14:56:56 -07001940 pr_warn("%s: 1.8V regulator output did not became stable\n",
1941 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001942
1943 return -EAGAIN;
1944 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001945 if (!(host->flags & SDHCI_SIGNALING_120))
1946 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001947 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001948 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001949 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001950 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1951 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001952 return -EIO;
1953 }
1954 }
1955 return 0;
1956 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301957 /* No signal voltage switch required */
1958 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001959 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301960}
Hu Zijic376ea92017-03-30 17:22:56 +02001961EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
Arindam Nathf2119df2011-05-05 12:18:57 +05301962
Kevin Liu20b92a32012-12-17 19:29:26 +08001963static int sdhci_card_busy(struct mmc_host *mmc)
1964{
1965 struct sdhci_host *host = mmc_priv(mmc);
1966 u32 present_state;
1967
Adrian Huntere613cc42016-06-23 14:00:58 +03001968 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001969 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001970
Adrian Huntere613cc42016-06-23 14:00:58 +03001971 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001972}
1973
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001974static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1975{
1976 struct sdhci_host *host = mmc_priv(mmc);
1977 unsigned long flags;
1978
1979 spin_lock_irqsave(&host->lock, flags);
1980 host->flags |= SDHCI_HS400_TUNING;
1981 spin_unlock_irqrestore(&host->lock, flags);
1982
1983 return 0;
1984}
1985
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02001986static void sdhci_start_tuning(struct sdhci_host *host)
1987{
1988 u16 ctrl;
1989
1990 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1991 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1992 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1993 ctrl |= SDHCI_CTRL_TUNED_CLK;
1994 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1995
1996 /*
1997 * As per the Host Controller spec v3.00, tuning command
1998 * generates Buffer Read Ready interrupt, so enable that.
1999 *
2000 * Note: The spec clearly says that when tuning sequence
2001 * is being performed, the controller does not generate
2002 * interrupts other than Buffer Read Ready interrupt. But
2003 * to make sure we don't hit a controller bug, we _only_
2004 * enable Buffer Read Ready interrupt here.
2005 */
2006 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2007 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
2008}
2009
2010static void sdhci_end_tuning(struct sdhci_host *host)
2011{
2012 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2013 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2014}
2015
2016static void sdhci_reset_tuning(struct sdhci_host *host)
2017{
2018 u16 ctrl;
2019
2020 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2021 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2022 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2023 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2024}
2025
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002026static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002027{
2028 sdhci_reset_tuning(host);
2029
2030 sdhci_do_reset(host, SDHCI_RESET_CMD);
2031 sdhci_do_reset(host, SDHCI_RESET_DATA);
2032
2033 sdhci_end_tuning(host);
2034
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002035 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002036}
2037
2038/*
2039 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2040 * tuning command does not have a data payload (or rather the hardware does it
2041 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2042 * interrupt setup is different to other commands and there is no timeout
2043 * interrupt so special handling is needed.
2044 */
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002045static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002046{
2047 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002048 struct mmc_command cmd = {};
2049 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002050 unsigned long flags;
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002051 u32 b = host->sdma_boundary;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002052
2053 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002054
2055 cmd.opcode = opcode;
2056 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2057 cmd.mrq = &mrq;
2058
2059 mrq.cmd = &cmd;
2060 /*
2061 * In response to CMD19, the card sends 64 bytes of tuning
2062 * block to the Host Controller. So we set the block size
2063 * to 64 here.
2064 */
Adrian Hunter85336102016-12-02 15:14:26 +02002065 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2066 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002067 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
Adrian Hunter85336102016-12-02 15:14:26 +02002068 else
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002069 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002070
2071 /*
2072 * The tuning block is sent by the card to the host controller.
2073 * So we set the TRNS_READ bit in the Transfer Mode register.
2074 * This also takes care of setting DMA Enable and Multi Block
2075 * Select in the same register to 0.
2076 */
2077 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2078
2079 sdhci_send_command(host, &cmd);
2080
2081 host->cmd = NULL;
2082
2083 sdhci_del_timer(host, &mrq);
2084
2085 host->tuning_done = 0;
2086
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002087 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002088 spin_unlock_irqrestore(&host->lock, flags);
2089
2090 /* Wait for Buffer Read Ready interrupt */
2091 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2092 msecs_to_jiffies(50));
2093
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002094}
2095
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002096static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002097{
2098 int i;
2099
2100 /*
2101 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2102 * of loops reaches 40 times.
2103 */
2104 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2105 u16 ctrl;
2106
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002107 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002108
2109 if (!host->tuning_done) {
2110 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2111 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002112 sdhci_abort_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002113 return;
2114 }
2115
2116 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2117 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2118 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2119 return; /* Success! */
2120 break;
2121 }
2122
Adrian Hunter83b600b2017-04-20 16:14:43 +08002123 /* Spec does not require a delay between tuning cycles */
2124 if (host->tuning_delay > 0)
2125 mdelay(host->tuning_delay);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002126 }
2127
2128 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2129 mmc_hostname(host->mmc));
2130 sdhci_reset_tuning(host);
2131}
2132
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002133int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302134{
Russell King4b6f37d2014-04-25 12:59:36 +01002135 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302136 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002137 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002138 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302139
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002140 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002141
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002142 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2143 tuning_count = host->tuning_count;
2144
Arindam Nathb513ea22011-05-05 12:19:04 +05302145 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002146 * The Host Controller needs tuning in case of SDR104 and DDR50
2147 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2148 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302149 * If the Host Controller supports the HS200 mode then the
2150 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302151 */
Russell King4b6f37d2014-04-25 12:59:36 +01002152 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002153 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002154 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002155 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002156 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002157
Russell King4b6f37d2014-04-25 12:59:36 +01002158 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002159 /*
2160 * Periodic re-tuning for HS400 is not expected to be needed, so
2161 * disable it here.
2162 */
2163 if (hs400_tuning)
2164 tuning_count = 0;
2165 break;
2166
Russell King4b6f37d2014-04-25 12:59:36 +01002167 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002168 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002169 break;
Girish K S069c9f12012-01-06 09:56:39 +05302170
Russell King4b6f37d2014-04-25 12:59:36 +01002171 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002172 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002173 break;
2174 /* FALLTHROUGH */
2175
2176 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002177 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302178 }
2179
Dong Aisheng45251812013-09-13 19:11:30 +08002180 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302181 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002182 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002183 }
2184
Adrian Hunter6b11e702016-12-02 15:14:27 +02002185 host->mmc->retune_period = tuning_count;
2186
Adrian Hunter83b600b2017-04-20 16:14:43 +08002187 if (host->tuning_delay < 0)
2188 host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
2189
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002190 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302191
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002192 __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302193
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002194 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002195out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302196 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002197
Arindam Nathb513ea22011-05-05 12:19:04 +05302198 return err;
2199}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002200EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302201
Kevin Liu52983382013-01-31 11:31:37 +08002202static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302203{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302204 /* Host Controller v3.00 defines preset value registers */
2205 if (host->version < SDHCI_SPEC_300)
2206 return;
2207
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302208 /*
2209 * We only enable or disable Preset Value if they are not already
2210 * enabled or disabled respectively. Otherwise, we bail out.
2211 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002212 if (host->preset_enabled != enable) {
2213 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2214
2215 if (enable)
2216 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2217 else
2218 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2219
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302220 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002221
2222 if (enable)
2223 host->flags |= SDHCI_PV_ENABLED;
2224 else
2225 host->flags &= ~SDHCI_PV_ENABLED;
2226
2227 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302228 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002229}
2230
Haibo Chen348487c2014-12-09 17:04:05 +08002231static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2232 int err)
2233{
2234 struct sdhci_host *host = mmc_priv(mmc);
2235 struct mmc_data *data = mrq->data;
2236
Russell Kingf48f0392016-01-26 13:40:32 +00002237 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002238 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02002239 mmc_get_dma_dir(data));
Russell King771a3dc2016-01-26 13:40:53 +00002240
2241 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002242}
2243
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002244static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002245{
2246 struct sdhci_host *host = mmc_priv(mmc);
2247
Haibo Chend31911b2015-08-25 10:02:11 +08002248 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002249
2250 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002251 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002252}
2253
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002254static inline bool sdhci_has_requests(struct sdhci_host *host)
2255{
2256 return host->cmd || host->data_cmd;
2257}
2258
2259static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2260{
2261 if (host->data_cmd) {
2262 host->data_cmd->error = err;
2263 sdhci_finish_mrq(host, host->data_cmd->mrq);
2264 }
2265
2266 if (host->cmd) {
2267 host->cmd->error = err;
2268 sdhci_finish_mrq(host, host->cmd->mrq);
2269 }
2270}
2271
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002272static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002274 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002275 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002276 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002277
Christian Daudt722e1282013-06-20 14:26:36 -07002278 /* First check if client has provided their own card event */
2279 if (host->ops->card_event)
2280 host->ops->card_event(host);
2281
Adrian Hunterd3940f22016-06-29 16:24:14 +03002282 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002283
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284 spin_lock_irqsave(&host->lock, flags);
2285
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002286 /* Check sdhci_has_requests() first in case we are runtime suspended */
2287 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302288 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002289 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302290 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002291 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292
Russell King03231f92014-04-25 12:57:12 +01002293 sdhci_do_reset(host, SDHCI_RESET_CMD);
2294 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002296 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002297 }
2298
2299 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002300}
2301
2302static const struct mmc_host_ops sdhci_ops = {
2303 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002304 .post_req = sdhci_post_req,
2305 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002306 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002307 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002308 .get_ro = sdhci_get_ro,
2309 .hw_reset = sdhci_hw_reset,
2310 .enable_sdio_irq = sdhci_enable_sdio_irq,
2311 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002312 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002313 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002314 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002315 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002316};
2317
2318/*****************************************************************************\
2319 * *
2320 * Tasklets *
2321 * *
2322\*****************************************************************************/
2323
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002324static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002325{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326 unsigned long flags;
2327 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002328 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002329
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002330 spin_lock_irqsave(&host->lock, flags);
2331
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002332 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2333 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002334 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002335 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002336 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002337
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002338 if (!mrq) {
2339 spin_unlock_irqrestore(&host->lock, flags);
2340 return true;
2341 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002342
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002343 sdhci_del_timer(host, mrq);
2344
Pierre Ossmand129bce2006-03-24 03:18:17 -08002345 /*
Russell King054cedf2016-01-26 13:40:42 +00002346 * Always unmap the data buffers if they were mapped by
2347 * sdhci_prepare_data() whenever we finish with a request.
2348 * This avoids leaking DMA mappings on error.
2349 */
2350 if (host->flags & SDHCI_REQ_USE_DMA) {
2351 struct mmc_data *data = mrq->data;
2352
2353 if (data && data->host_cookie == COOKIE_MAPPED) {
2354 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02002355 mmc_get_dma_dir(data));
Russell King054cedf2016-01-26 13:40:42 +00002356 data->host_cookie = COOKIE_UNMAPPED;
2357 }
2358 }
2359
2360 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002361 * The controller needs a reset of internal state machines
2362 * upon error conditions.
2363 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002364 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002365 /*
2366 * Do not finish until command and data lines are available for
2367 * reset. Note there can only be one other mrq, so it cannot
2368 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2369 * would both be null.
2370 */
2371 if (host->cmd || host->data_cmd) {
2372 spin_unlock_irqrestore(&host->lock, flags);
2373 return true;
2374 }
2375
Pierre Ossman645289d2006-06-30 02:22:33 -07002376 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002377 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002378 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002379 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002380
2381 /* Spec says we should do both at the same time, but Ricoh
2382 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002383 sdhci_do_reset(host, SDHCI_RESET_CMD);
2384 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002385
2386 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002387 }
2388
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002389 if (!sdhci_has_requests(host))
2390 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002391
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002392 host->mrqs_done[i] = NULL;
2393
Pierre Ossman5f25a662006-10-04 02:15:39 -07002394 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002395 spin_unlock_irqrestore(&host->lock, flags);
2396
2397 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002398
2399 return false;
2400}
2401
2402static void sdhci_tasklet_finish(unsigned long param)
2403{
2404 struct sdhci_host *host = (struct sdhci_host *)param;
2405
2406 while (!sdhci_request_done(host))
2407 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002408}
2409
Kees Cook2ee4f622017-10-24 08:03:45 -07002410static void sdhci_timeout_timer(struct timer_list *t)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002411{
2412 struct sdhci_host *host;
2413 unsigned long flags;
2414
Kees Cook2ee4f622017-10-24 08:03:45 -07002415 host = from_timer(host, t, timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002416
2417 spin_lock_irqsave(&host->lock, flags);
2418
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002419 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2420 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2421 mmc_hostname(host->mmc));
2422 sdhci_dumpregs(host);
2423
2424 host->cmd->error = -ETIMEDOUT;
2425 sdhci_finish_mrq(host, host->cmd->mrq);
2426 }
2427
2428 mmiowb();
2429 spin_unlock_irqrestore(&host->lock, flags);
2430}
2431
Kees Cook2ee4f622017-10-24 08:03:45 -07002432static void sdhci_timeout_data_timer(struct timer_list *t)
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002433{
2434 struct sdhci_host *host;
2435 unsigned long flags;
2436
Kees Cook2ee4f622017-10-24 08:03:45 -07002437 host = from_timer(host, t, data_timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002438
2439 spin_lock_irqsave(&host->lock, flags);
2440
2441 if (host->data || host->data_cmd ||
2442 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002443 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2444 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445 sdhci_dumpregs(host);
2446
2447 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002448 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002450 } else if (host->data_cmd) {
2451 host->data_cmd->error = -ETIMEDOUT;
2452 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002453 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002454 host->cmd->error = -ETIMEDOUT;
2455 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 }
2457 }
2458
Pierre Ossman5f25a662006-10-04 02:15:39 -07002459 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002460 spin_unlock_irqrestore(&host->lock, flags);
2461}
2462
2463/*****************************************************************************\
2464 * *
2465 * Interrupt handling *
2466 * *
2467\*****************************************************************************/
2468
Adrian Hunterfc605f12016-10-05 12:11:21 +03002469static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002471 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002472 /*
2473 * SDHCI recovers from errors by resetting the cmd and data
2474 * circuits. Until that is done, there very well might be more
2475 * interrupts, so ignore them in that case.
2476 */
2477 if (host->pending_reset)
2478 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002479 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2480 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002481 sdhci_dumpregs(host);
2482 return;
2483 }
2484
Russell Kingec014cb2016-01-26 13:39:39 +00002485 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2486 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2487 if (intmask & SDHCI_INT_TIMEOUT)
2488 host->cmd->error = -ETIMEDOUT;
2489 else
2490 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002491
Russell King71fcbda2016-01-26 13:39:45 +00002492 /*
2493 * If this command initiates a data phase and a response
2494 * CRC error is signalled, the card can start transferring
2495 * data - the card may have received the command without
2496 * error. We must not terminate the mmc_request early.
2497 *
2498 * If the card did not receive the command or returned an
2499 * error which prevented it sending data, the data phase
2500 * will time out.
2501 */
2502 if (host->cmd->data &&
2503 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2504 SDHCI_INT_CRC) {
2505 host->cmd = NULL;
2506 return;
2507 }
2508
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002509 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002510 return;
2511 }
2512
Pierre Ossmane8095172008-07-25 01:09:08 +02002513 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002514 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002515}
2516
Adrian Hunter08621b12014-11-04 12:42:38 +02002517static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002518{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002519 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002520
2521 sdhci_dumpregs(host);
2522
2523 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002524 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002525
Adrian Huntere57a5f62014-11-04 12:42:46 +02002526 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002527 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2528 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002529 le32_to_cpu(dma_desc->addr_lo),
2530 le16_to_cpu(dma_desc->len),
2531 le16_to_cpu(dma_desc->cmd));
2532 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002533 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2534 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002535 le16_to_cpu(dma_desc->len),
2536 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002537
Adrian Hunter76fe3792014-11-04 12:42:42 +02002538 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002539
Adrian Hunter05452302014-11-04 12:42:45 +02002540 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002541 break;
2542 }
2543}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002544
Pierre Ossmand129bce2006-03-24 03:18:17 -08002545static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2546{
Girish K S069c9f12012-01-06 09:56:39 +05302547 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002548
Arindam Nathb513ea22011-05-05 12:19:04 +05302549 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2550 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302551 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2552 if (command == MMC_SEND_TUNING_BLOCK ||
2553 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302554 host->tuning_done = 1;
2555 wake_up(&host->buf_ready_int);
2556 return;
2557 }
2558 }
2559
Pierre Ossmand129bce2006-03-24 03:18:17 -08002560 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002561 struct mmc_command *data_cmd = host->data_cmd;
2562
Pierre Ossmand129bce2006-03-24 03:18:17 -08002563 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002564 * The "data complete" interrupt is also used to
2565 * indicate that a busy state has ended. See comment
2566 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002568 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002569 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002570 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002571 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002572 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002573 return;
2574 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002575 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002576 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002577 /*
2578 * Some cards handle busy-end interrupt
2579 * before the command completed, so make
2580 * sure we do things in the proper order.
2581 */
Adrian Hunterea968022016-06-29 16:24:24 +03002582 if (host->cmd == data_cmd)
2583 return;
2584
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002585 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002586 return;
2587 }
2588 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589
Adrian Huntered1563d2016-06-29 16:24:29 +03002590 /*
2591 * SDHCI recovers from errors by resetting the cmd and data
2592 * circuits. Until that is done, there very well might be more
2593 * interrupts, so ignore them in that case.
2594 */
2595 if (host->pending_reset)
2596 return;
2597
Marek Vasut2e4456f2015-11-18 10:47:02 +01002598 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2599 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002600 sdhci_dumpregs(host);
2601
2602 return;
2603 }
2604
2605 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002606 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002607 else if (intmask & SDHCI_INT_DATA_END_BIT)
2608 host->data->error = -EILSEQ;
2609 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2610 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2611 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002612 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002613 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302614 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002615 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002616 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002617 if (host->ops->adma_workaround)
2618 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002619 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002620
Pierre Ossman17b04292007-07-22 22:18:46 +02002621 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002622 sdhci_finish_data(host);
2623 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002624 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002625 sdhci_transfer_pio(host);
2626
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002627 /*
2628 * We currently don't do anything fancy with DMA
2629 * boundaries, but as we can't disable the feature
2630 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002631 *
2632 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2633 * should return a valid address to continue from, but as
2634 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002635 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002636 if (intmask & SDHCI_INT_DMA_END) {
2637 u32 dmastart, dmanow;
2638 dmastart = sg_dma_address(host->data->sg);
2639 dmanow = dmastart + host->data->bytes_xfered;
2640 /*
2641 * Force update to the next DMA block boundary.
2642 */
2643 dmanow = (dmanow &
2644 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2645 SDHCI_DEFAULT_BOUNDARY_SIZE;
2646 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002647 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2648 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002649 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2650 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002651
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002652 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002653 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002654 /*
2655 * Data managed to finish before the
2656 * command completed. Make sure we do
2657 * things in the proper order.
2658 */
2659 host->data_early = 1;
2660 } else {
2661 sdhci_finish_data(host);
2662 }
2663 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002664 }
2665}
2666
David Howells7d12e782006-10-05 14:55:46 +01002667static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002668{
Russell King781e9892014-04-25 12:55:46 +01002669 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002670 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002671 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002672 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673
2674 spin_lock(&host->lock);
2675
Russell Kingbe138552014-04-25 12:55:56 +01002676 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002677 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002678 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002679 }
2680
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002681 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002682 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002683 result = IRQ_NONE;
2684 goto out;
2685 }
2686
Russell King41005002014-04-25 12:55:36 +01002687 do {
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002688 DBG("IRQ status 0x%08x\n", intmask);
2689
2690 if (host->ops->irq) {
2691 intmask = host->ops->irq(host, intmask);
2692 if (!intmask)
2693 goto cont;
2694 }
2695
Russell King41005002014-04-25 12:55:36 +01002696 /* Clear selected interrupts. */
2697 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2698 SDHCI_INT_BUS_POWER);
2699 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002700
Russell King41005002014-04-25 12:55:36 +01002701 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2702 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2703 SDHCI_CARD_PRESENT;
2704
2705 /*
2706 * There is a observation on i.mx esdhc. INSERT
2707 * bit will be immediately set again when it gets
2708 * cleared, if a card is inserted. We have to mask
2709 * the irq to prevent interrupt storm which will
2710 * freeze the system. And the REMOVE gets the
2711 * same situation.
2712 *
2713 * More testing are needed here to ensure it works
2714 * for other platforms though.
2715 */
Russell Kingb537f942014-04-25 12:56:01 +01002716 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2717 SDHCI_INT_CARD_REMOVE);
2718 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2719 SDHCI_INT_CARD_INSERT;
2720 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2721 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002722
2723 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2724 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002725
2726 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2727 SDHCI_INT_CARD_REMOVE);
2728 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002729 }
2730
2731 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002732 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002733
2734 if (intmask & SDHCI_INT_DATA_MASK)
2735 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2736
2737 if (intmask & SDHCI_INT_BUS_POWER)
2738 pr_err("%s: Card is consuming too much power!\n",
2739 mmc_hostname(host->mmc));
2740
Dong Aishengf37b20e2016-07-12 15:46:17 +08002741 if (intmask & SDHCI_INT_RETUNE)
2742 mmc_retune_needed(host->mmc);
2743
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002744 if ((intmask & SDHCI_INT_CARD_INT) &&
2745 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002746 sdhci_enable_sdio_irq_nolock(host, false);
2747 host->thread_isr |= SDHCI_INT_CARD_INT;
2748 result = IRQ_WAKE_THREAD;
2749 }
Russell King41005002014-04-25 12:55:36 +01002750
2751 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2752 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2753 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002754 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002755
2756 if (intmask) {
2757 unexpected |= intmask;
2758 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2759 }
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002760cont:
Russell King781e9892014-04-25 12:55:46 +01002761 if (result == IRQ_NONE)
2762 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002763
2764 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002765 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002766out:
2767 spin_unlock(&host->lock);
2768
Alexander Stein6379b232012-03-14 09:52:10 +01002769 if (unexpected) {
2770 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2771 mmc_hostname(host->mmc), unexpected);
2772 sdhci_dumpregs(host);
2773 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002774
Pierre Ossmand129bce2006-03-24 03:18:17 -08002775 return result;
2776}
2777
Russell King781e9892014-04-25 12:55:46 +01002778static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2779{
2780 struct sdhci_host *host = dev_id;
2781 unsigned long flags;
2782 u32 isr;
2783
2784 spin_lock_irqsave(&host->lock, flags);
2785 isr = host->thread_isr;
2786 host->thread_isr = 0;
2787 spin_unlock_irqrestore(&host->lock, flags);
2788
Russell King3560db82014-04-25 12:55:51 +01002789 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002790 struct mmc_host *mmc = host->mmc;
2791
2792 mmc->ops->card_event(mmc);
2793 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002794 }
2795
Russell King781e9892014-04-25 12:55:46 +01002796 if (isr & SDHCI_INT_CARD_INT) {
2797 sdio_run_irqs(host->mmc);
2798
2799 spin_lock_irqsave(&host->lock, flags);
2800 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2801 sdhci_enable_sdio_irq_nolock(host, true);
2802 spin_unlock_irqrestore(&host->lock, flags);
2803 }
2804
2805 return isr ? IRQ_HANDLED : IRQ_NONE;
2806}
2807
Pierre Ossmand129bce2006-03-24 03:18:17 -08002808/*****************************************************************************\
2809 * *
2810 * Suspend/resume *
2811 * *
2812\*****************************************************************************/
2813
2814#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002815/*
2816 * To enable wakeup events, the corresponding events have to be enabled in
2817 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2818 * Table' in the SD Host Controller Standard Specification.
2819 * It is useless to restore SDHCI_INT_ENABLE state in
2820 * sdhci_disable_irq_wakeups() since it will be set by
2821 * sdhci_enable_card_detection() or sdhci_init().
2822 */
Kevin Liuad080d72013-01-05 17:21:33 +08002823void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2824{
2825 u8 val;
2826 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2827 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002828 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2829 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002830
2831 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2832 val |= mask ;
2833 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002834 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002835 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002836 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2837 }
Kevin Liuad080d72013-01-05 17:21:33 +08002838 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002839 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002840}
2841EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2842
Fabio Estevam0b10f472014-08-30 14:53:13 -03002843static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002844{
2845 u8 val;
2846 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2847 | SDHCI_WAKE_ON_INT;
2848
2849 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2850 val &= ~mask;
2851 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2852}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002853
Manuel Lauss29495aa2011-11-03 11:09:45 +01002854int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002855{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002856 sdhci_disable_card_detection(host);
2857
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002858 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302859
Kevin Liuad080d72013-01-05 17:21:33 +08002860 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002861 host->ier = 0;
2862 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2863 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002864 free_irq(host->irq, host);
2865 } else {
2866 sdhci_enable_irq_wakeups(host);
2867 enable_irq_wake(host->irq);
2868 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002869 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002870}
2871
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002872EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002873
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002874int sdhci_resume_host(struct sdhci_host *host)
2875{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002876 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002877 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002878
Richard Röjforsa13abc72009-09-22 16:45:30 -07002879 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002880 if (host->ops->enable_dma)
2881 host->ops->enable_dma(host);
2882 }
2883
Adrian Hunter6308d292012-02-07 14:48:54 +02002884 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2885 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2886 /* Card keeps power but host controller does not */
2887 sdhci_init(host, 0);
2888 host->pwr = 0;
2889 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002890 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002891 } else {
2892 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2893 mmiowb();
2894 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002895
Haibo Chen14a7b41642015-09-15 18:32:58 +08002896 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2897 ret = request_threaded_irq(host->irq, sdhci_irq,
2898 sdhci_thread_irq, IRQF_SHARED,
2899 mmc_hostname(host->mmc), host);
2900 if (ret)
2901 return ret;
2902 } else {
2903 sdhci_disable_irq_wakeups(host);
2904 disable_irq_wake(host->irq);
2905 }
2906
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002907 sdhci_enable_card_detection(host);
2908
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002909 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002910}
2911
2912EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002913
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002914int sdhci_runtime_suspend_host(struct sdhci_host *host)
2915{
2916 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002917
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002918 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002919
2920 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002921 host->ier &= SDHCI_INT_CARD_INT;
2922 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2923 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002924 spin_unlock_irqrestore(&host->lock, flags);
2925
Russell King781e9892014-04-25 12:55:46 +01002926 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002927
2928 spin_lock_irqsave(&host->lock, flags);
2929 host->runtime_suspended = true;
2930 spin_unlock_irqrestore(&host->lock, flags);
2931
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002932 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002933}
2934EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2935
2936int sdhci_runtime_resume_host(struct sdhci_host *host)
2937{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002938 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002939 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002940 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002941
2942 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2943 if (host->ops->enable_dma)
2944 host->ops->enable_dma(host);
2945 }
2946
2947 sdhci_init(host, 0);
2948
Zhoujie Wu70bc85a2017-08-03 12:28:40 -07002949 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
2950 mmc->ios.power_mode != MMC_POWER_OFF) {
Adrian Hunter84ec0482016-12-19 15:33:11 +02002951 /* Force clock and power re-program */
2952 host->pwr = 0;
2953 host->clock = 0;
2954 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2955 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002956
Adrian Hunter84ec0482016-12-19 15:33:11 +02002957 if ((host_flags & SDHCI_PV_ENABLED) &&
2958 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2959 spin_lock_irqsave(&host->lock, flags);
2960 sdhci_enable_preset_value(host, true);
2961 spin_unlock_irqrestore(&host->lock, flags);
2962 }
2963
2964 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2965 mmc->ops->hs400_enhanced_strobe)
2966 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002967 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002968
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002969 spin_lock_irqsave(&host->lock, flags);
2970
2971 host->runtime_suspended = false;
2972
2973 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002974 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002975 sdhci_enable_sdio_irq_nolock(host, true);
2976
2977 /* Enable Card Detection */
2978 sdhci_enable_card_detection(host);
2979
2980 spin_unlock_irqrestore(&host->lock, flags);
2981
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002982 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002983}
2984EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2985
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002986#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002987
Pierre Ossmand129bce2006-03-24 03:18:17 -08002988/*****************************************************************************\
2989 * *
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002990 * Command Queue Engine (CQE) helpers *
2991 * *
2992\*****************************************************************************/
2993
2994void sdhci_cqe_enable(struct mmc_host *mmc)
2995{
2996 struct sdhci_host *host = mmc_priv(mmc);
2997 unsigned long flags;
2998 u8 ctrl;
2999
3000 spin_lock_irqsave(&host->lock, flags);
3001
3002 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
3003 ctrl &= ~SDHCI_CTRL_DMA_MASK;
3004 if (host->flags & SDHCI_USE_64_BIT_DMA)
3005 ctrl |= SDHCI_CTRL_ADMA64;
3006 else
3007 ctrl |= SDHCI_CTRL_ADMA32;
3008 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
3009
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003010 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003011 SDHCI_BLOCK_SIZE);
3012
3013 /* Set maximum timeout */
3014 sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
3015
3016 host->ier = host->cqe_ier;
3017
3018 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3019 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
3020
3021 host->cqe_on = true;
3022
3023 pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
3024 mmc_hostname(mmc), host->ier,
3025 sdhci_readl(host, SDHCI_INT_STATUS));
3026
3027 mmiowb();
3028 spin_unlock_irqrestore(&host->lock, flags);
3029}
3030EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
3031
3032void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
3033{
3034 struct sdhci_host *host = mmc_priv(mmc);
3035 unsigned long flags;
3036
3037 spin_lock_irqsave(&host->lock, flags);
3038
3039 sdhci_set_default_irqs(host);
3040
3041 host->cqe_on = false;
3042
3043 if (recovery) {
3044 sdhci_do_reset(host, SDHCI_RESET_CMD);
3045 sdhci_do_reset(host, SDHCI_RESET_DATA);
3046 }
3047
3048 pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
3049 mmc_hostname(mmc), host->ier,
3050 sdhci_readl(host, SDHCI_INT_STATUS));
3051
3052 mmiowb();
3053 spin_unlock_irqrestore(&host->lock, flags);
3054}
3055EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
3056
3057bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
3058 int *data_error)
3059{
3060 u32 mask;
3061
3062 if (!host->cqe_on)
3063 return false;
3064
3065 if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
3066 *cmd_error = -EILSEQ;
3067 else if (intmask & SDHCI_INT_TIMEOUT)
3068 *cmd_error = -ETIMEDOUT;
3069 else
3070 *cmd_error = 0;
3071
3072 if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3073 *data_error = -EILSEQ;
3074 else if (intmask & SDHCI_INT_DATA_TIMEOUT)
3075 *data_error = -ETIMEDOUT;
3076 else if (intmask & SDHCI_INT_ADMA_ERROR)
3077 *data_error = -EIO;
3078 else
3079 *data_error = 0;
3080
3081 /* Clear selected interrupts. */
3082 mask = intmask & host->cqe_ier;
3083 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3084
3085 if (intmask & SDHCI_INT_BUS_POWER)
3086 pr_err("%s: Card is consuming too much power!\n",
3087 mmc_hostname(host->mmc));
3088
3089 intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
3090 if (intmask) {
3091 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3092 pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
3093 mmc_hostname(host->mmc), intmask);
3094 sdhci_dumpregs(host);
3095 }
3096
3097 return true;
3098}
3099EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
3100
3101/*****************************************************************************\
3102 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003103 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003104 * *
3105\*****************************************************************************/
3106
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003107struct sdhci_host *sdhci_alloc_host(struct device *dev,
3108 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003109{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003110 struct mmc_host *mmc;
3111 struct sdhci_host *host;
3112
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003113 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003115 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003116 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003117 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003118
3119 host = mmc_priv(mmc);
3120 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003121 host->mmc_host_ops = sdhci_ops;
3122 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003123
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003124 host->flags = SDHCI_SIGNALING_330;
3125
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003126 host->cqe_ier = SDHCI_CQE_INT_MASK;
3127 host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
3128
Adrian Hunter83b600b2017-04-20 16:14:43 +08003129 host->tuning_delay = -1;
3130
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003131 host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
3132
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003133 return host;
3134}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003135
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003136EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003137
Alexandre Courbot7b913692016-03-07 11:07:55 +09003138static int sdhci_set_dma_mask(struct sdhci_host *host)
3139{
3140 struct mmc_host *mmc = host->mmc;
3141 struct device *dev = mmc_dev(mmc);
3142 int ret = -EINVAL;
3143
3144 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3145 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3146
3147 /* Try 64-bit mask if hardware is capable of it */
3148 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3149 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3150 if (ret) {
3151 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3152 mmc_hostname(mmc));
3153 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3154 }
3155 }
3156
3157 /* 32-bit mask as default & fallback */
3158 if (ret) {
3159 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3160 if (ret)
3161 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3162 mmc_hostname(mmc));
3163 }
3164
3165 return ret;
3166}
3167
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003168void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3169{
3170 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003171 u64 dt_caps_mask = 0;
3172 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003173
3174 if (host->read_caps)
3175 return;
3176
3177 host->read_caps = true;
3178
3179 if (debug_quirks)
3180 host->quirks = debug_quirks;
3181
3182 if (debug_quirks2)
3183 host->quirks2 = debug_quirks2;
3184
3185 sdhci_do_reset(host, SDHCI_RESET_ALL);
3186
Zach Brown92e0c442016-11-02 10:26:16 -05003187 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3188 "sdhci-caps-mask", &dt_caps_mask);
3189 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3190 "sdhci-caps", &dt_caps);
3191
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003192 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3193 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3194
3195 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3196 return;
3197
Zach Brown92e0c442016-11-02 10:26:16 -05003198 if (caps) {
3199 host->caps = *caps;
3200 } else {
3201 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3202 host->caps &= ~lower_32_bits(dt_caps_mask);
3203 host->caps |= lower_32_bits(dt_caps);
3204 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003205
3206 if (host->version < SDHCI_SPEC_300)
3207 return;
3208
Zach Brown92e0c442016-11-02 10:26:16 -05003209 if (caps1) {
3210 host->caps1 = *caps1;
3211 } else {
3212 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3213 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3214 host->caps1 |= upper_32_bits(dt_caps);
3215 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003216}
3217EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3218
Adrian Hunter52f53362016-06-29 16:24:15 +03003219int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003220{
3221 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303222 u32 max_current_caps;
3223 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003224 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003225 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003226 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003227
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003228 WARN_ON(host == NULL);
3229 if (host == NULL)
3230 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003231
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003232 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003233
Jon Hunterefba1422016-07-12 14:53:36 +01003234 /*
3235 * If there are external regulators, get them. Note this must be done
3236 * early before resetting the host and reading the capabilities so that
3237 * the host can take the appropriate action if regulators are not
3238 * available.
3239 */
3240 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang2a633032017-10-14 21:17:18 +02003241 if (ret)
Jon Hunterefba1422016-07-12 14:53:36 +01003242 return ret;
3243
Shawn Lin06ebc602017-07-19 15:55:49 +08003244 DBG("Version: 0x%08x | Present: 0x%08x\n",
3245 sdhci_readw(host, SDHCI_HOST_VERSION),
3246 sdhci_readl(host, SDHCI_PRESENT_STATE));
3247 DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
3248 sdhci_readl(host, SDHCI_CAPABILITIES),
3249 sdhci_readl(host, SDHCI_CAPABILITIES_1));
3250
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003251 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003252
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003253 override_timeout_clk = host->timeout_clk;
3254
Zhangfei Gao85105c52010-08-06 07:10:01 +08003255 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003256 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3257 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003258 }
3259
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003260 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003261 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003262 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003263 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003264 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003265 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003266
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003267 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003268 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003269 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003270 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003271 }
3272
Arindam Nathf2119df2011-05-05 12:18:57 +05303273 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003274 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003275 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003276
3277 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3278 (host->flags & SDHCI_USE_ADMA)) {
3279 DBG("Disabling ADMA as it is marked broken\n");
3280 host->flags &= ~SDHCI_USE_ADMA;
3281 }
3282
Adrian Huntere57a5f62014-11-04 12:42:46 +02003283 /*
3284 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3285 * and *must* do 64-bit DMA. A driver has the opportunity to change
3286 * that during the first call to ->enable_dma(). Similarly
3287 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3288 * implement.
3289 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003290 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003291 host->flags |= SDHCI_USE_64_BIT_DMA;
3292
Richard Röjforsa13abc72009-09-22 16:45:30 -07003293 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003294 ret = sdhci_set_dma_mask(host);
3295
3296 if (!ret && host->ops->enable_dma)
3297 ret = host->ops->enable_dma(host);
3298
3299 if (ret) {
3300 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3301 mmc_hostname(mmc));
3302 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3303
3304 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305 }
3306 }
3307
Adrian Huntere57a5f62014-11-04 12:42:46 +02003308 /* SDMA does not support 64-bit DMA */
3309 if (host->flags & SDHCI_USE_64_BIT_DMA)
3310 host->flags &= ~SDHCI_USE_SDMA;
3311
Pierre Ossman2134a922008-06-28 18:28:51 +02003312 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003313 dma_addr_t dma;
3314 void *buf;
3315
Pierre Ossman2134a922008-06-28 18:28:51 +02003316 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003317 * The DMA descriptor table size is calculated as the maximum
3318 * number of segments times 2, to allow for an alignment
3319 * descriptor for each segment, plus 1 for a nop end descriptor,
3320 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003321 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003322 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3323 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3324 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003325 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003326 } else {
3327 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3328 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003329 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003330 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003331
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003332 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003333 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3334 host->adma_table_sz, &dma, GFP_KERNEL);
3335 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003336 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003337 mmc_hostname(mmc));
3338 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003339 } else if ((dma + host->align_buffer_sz) &
3340 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003341 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3342 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003343 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003344 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3345 host->adma_table_sz, buf, dma);
3346 } else {
3347 host->align_buffer = buf;
3348 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003349
Russell Kinge66e61c2016-01-26 13:39:55 +00003350 host->adma_table = buf + host->align_buffer_sz;
3351 host->adma_addr = dma + host->align_buffer_sz;
3352 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003353 }
3354
Pierre Ossman76591502008-07-21 00:32:11 +02003355 /*
3356 * If we use DMA, then it's up to the caller to set the DMA
3357 * mask, but PIO does not need the hw shim so we set a new
3358 * mask here in that case.
3359 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003360 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003361 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003362 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003363 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003364
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003365 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003366 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003367 >> SDHCI_CLOCK_BASE_SHIFT;
3368 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003369 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003370 >> SDHCI_CLOCK_BASE_SHIFT;
3371
Pierre Ossmand129bce2006-03-24 03:18:17 -08003372 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003373 if (host->max_clk == 0 || host->quirks &
3374 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003375 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003376 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3377 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003378 ret = -ENODEV;
3379 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003380 }
3381 host->max_clk = host->ops->get_max_clock(host);
3382 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003383
3384 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303385 * In case of Host Controller v3.00, find out whether clock
3386 * multiplier is supported.
3387 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003388 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303389 SDHCI_CLOCK_MUL_SHIFT;
3390
3391 /*
3392 * In case the value in Clock Multiplier is 0, then programmable
3393 * clock mode is not supported, otherwise the actual clock
3394 * multiplier is one more than the value of Clock Multiplier
3395 * in the Capabilities Register.
3396 */
3397 if (host->clk_mul)
3398 host->clk_mul += 1;
3399
3400 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401 * Set host parameters.
3402 */
Dong Aisheng59241752015-07-22 20:53:07 +08003403 max_clk = host->max_clk;
3404
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003405 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003406 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303407 else if (host->version >= SDHCI_SPEC_300) {
3408 if (host->clk_mul) {
3409 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003410 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303411 } else
3412 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3413 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003414 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003415
Adrian Hunterd310ae42016-04-12 14:25:07 +03003416 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003417 mmc->f_max = max_clk;
3418
Aisheng Dong28aab052014-08-27 15:26:31 +08003419 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003420 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003421 SDHCI_TIMEOUT_CLK_SHIFT;
Shawn Lin8cc35282017-03-24 15:50:12 +08003422
3423 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
3424 host->timeout_clk *= 1000;
3425
Aisheng Dong28aab052014-08-27 15:26:31 +08003426 if (host->timeout_clk == 0) {
Shawn Lin8cc35282017-03-24 15:50:12 +08003427 if (!host->ops->get_timeout_clock) {
Aisheng Dong28aab052014-08-27 15:26:31 +08003428 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3429 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003430 ret = -ENODEV;
3431 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003432 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003433
Shawn Lin8cc35282017-03-24 15:50:12 +08003434 host->timeout_clk =
3435 DIV_ROUND_UP(host->ops->get_timeout_clock(host),
3436 1000);
3437 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003438
Adrian Hunter99513622016-03-07 13:33:55 +02003439 if (override_timeout_clk)
3440 host->timeout_clk = override_timeout_clk;
3441
Aisheng Dong28aab052014-08-27 15:26:31 +08003442 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003443 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003444 mmc->max_busy_timeout /= host->timeout_clk;
3445 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003446
Andrei Warkentine89d4562011-05-23 15:06:37 -05003447 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003448 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003449
3450 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3451 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003452
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003453 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003454 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003455 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003456 !(host->flags & SDHCI_USE_SDMA)) &&
3457 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003458 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003459 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003460 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003461 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003462 }
3463
Philip Rakity15ec4462010-11-19 16:48:39 -05003464 /*
3465 * A controller may support 8-bit width, but the board itself
3466 * might not have the pins brought out. Boards that support
3467 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3468 * their platform code before calling sdhci_add_host(), and we
3469 * won't assume 8-bit width for hosts without that CAP.
3470 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003471 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003472 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003473
Jerry Huang63ef5d82012-10-25 13:47:19 +08003474 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3475 mmc->caps &= ~MMC_CAP_CMD23;
3476
Adrian Hunter28da3582016-06-29 16:24:17 +03003477 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003478 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003479
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003480 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003481 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003482 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003483 mmc->caps |= MMC_CAP_NEEDS_POLL;
3484
Philip Rakity6231f3d2012-07-23 15:56:23 -07003485 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003486 if (!IS_ERR(mmc->supply.vqmmc)) {
3487 ret = regulator_enable(mmc->supply.vqmmc);
3488 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3489 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003490 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3491 SDHCI_SUPPORT_SDR50 |
3492 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003493 if (ret) {
3494 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3495 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003496 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003497 }
Kevin Liu8363c372012-11-17 17:55:51 -05003498 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003499
Adrian Hunter28da3582016-06-29 16:24:17 +03003500 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3501 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3502 SDHCI_SUPPORT_DDR50);
3503 }
Daniel Drake6a661802012-11-25 13:01:19 -05003504
Al Cooper4188bba2012-03-16 15:54:17 -04003505 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003506 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3507 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303508 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3509
3510 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003511 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303512 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003513 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3514 * field can be promoted to support HS200.
3515 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003516 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003517 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003518 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303519 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003520 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303521
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003522 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003523 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003524 mmc->caps2 |= MMC_CAP2_HS400;
3525
Adrian Hunter549c0b12014-11-06 15:19:05 +02003526 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3527 (IS_ERR(mmc->supply.vqmmc) ||
3528 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3529 1300000)))
3530 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3531
Adrian Hunter28da3582016-06-29 16:24:17 +03003532 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3533 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303534 mmc->caps |= MMC_CAP_UHS_DDR50;
3535
Girish K S069c9f12012-01-06 09:56:39 +05303536 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003537 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303538 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3539
Arindam Nathd6d50a12011-05-05 12:18:59 +05303540 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003541 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303542 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003543 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303544 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003545 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303546 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3547
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303548 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003549 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3550 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303551
3552 /*
3553 * In case Re-tuning Timer is not disabled, the actual value of
3554 * re-tuning timer will be 2 ^ (n - 1).
3555 */
3556 if (host->tuning_count)
3557 host->tuning_count = 1 << (host->tuning_count - 1);
3558
3559 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003560 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303561 SDHCI_RETUNING_MODE_SHIFT;
3562
Takashi Iwai8f230f42010-12-08 10:04:30 +01003563 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003564
Arindam Nathf2119df2011-05-05 12:18:57 +05303565 /*
3566 * According to SD Host Controller spec v3.00, if the Host System
3567 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3568 * the value is meaningful only if Voltage Support in the Capabilities
3569 * register is set. The actual current value is 4 times the register
3570 * value.
3571 */
3572 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003573 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003574 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003575 if (curr > 0) {
3576
3577 /* convert to SDHCI_MAX_CURRENT format */
3578 curr = curr/1000; /* convert to mA */
3579 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3580
3581 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3582 max_current_caps =
3583 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3584 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3585 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3586 }
3587 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303588
Adrian Hunter28da3582016-06-29 16:24:17 +03003589 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003590 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303591
Aaron Lu55c46652012-07-04 13:31:48 +08003592 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303593 SDHCI_MAX_CURRENT_330_MASK) >>
3594 SDHCI_MAX_CURRENT_330_SHIFT) *
3595 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303596 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003597 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003598 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303599
Aaron Lu55c46652012-07-04 13:31:48 +08003600 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303601 SDHCI_MAX_CURRENT_300_MASK) >>
3602 SDHCI_MAX_CURRENT_300_SHIFT) *
3603 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303604 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003605 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003606 ocr_avail |= MMC_VDD_165_195;
3607
Aaron Lu55c46652012-07-04 13:31:48 +08003608 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303609 SDHCI_MAX_CURRENT_180_MASK) >>
3610 SDHCI_MAX_CURRENT_180_SHIFT) *
3611 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303612 }
3613
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003614 /* If OCR set by host, use it instead. */
3615 if (host->ocr_mask)
3616 ocr_avail = host->ocr_mask;
3617
3618 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003619 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003620 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003621
Takashi Iwai8f230f42010-12-08 10:04:30 +01003622 mmc->ocr_avail = ocr_avail;
3623 mmc->ocr_avail_sdio = ocr_avail;
3624 if (host->ocr_avail_sdio)
3625 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3626 mmc->ocr_avail_sd = ocr_avail;
3627 if (host->ocr_avail_sd)
3628 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3629 else /* normal SD controllers don't support 1.8V */
3630 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3631 mmc->ocr_avail_mmc = ocr_avail;
3632 if (host->ocr_avail_mmc)
3633 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003634
3635 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003636 pr_err("%s: Hardware doesn't report any support voltages.\n",
3637 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003638 ret = -ENODEV;
3639 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003640 }
3641
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003642 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3643 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3644 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3645 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3646 host->flags |= SDHCI_SIGNALING_180;
3647
3648 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3649 host->flags |= SDHCI_SIGNALING_120;
3650
Pierre Ossmand129bce2006-03-24 03:18:17 -08003651 spin_lock_init(&host->lock);
3652
3653 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003654 * Maximum number of segments. Depends on if the hardware
3655 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003656 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003657 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003658 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003659 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003660 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003661 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003662 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003663
3664 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003665 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3666 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3667 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003668 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003669 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003670
3671 /*
3672 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003673 * of bytes. When doing hardware scatter/gather, each entry cannot
3674 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003675 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003676 if (host->flags & SDHCI_USE_ADMA) {
3677 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3678 mmc->max_seg_size = 65535;
3679 else
3680 mmc->max_seg_size = 65536;
3681 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003682 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003683 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003684
3685 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003686 * Maximum block size. This varies from controller to controller and
3687 * is specified in the capabilities register.
3688 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003689 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3690 mmc->max_blk_size = 2;
3691 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003692 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003693 SDHCI_MAX_BLOCK_SHIFT;
3694 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003695 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3696 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003697 mmc->max_blk_size = 0;
3698 }
3699 }
3700
3701 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003702
3703 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003704 * Maximum block count.
3705 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003706 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003707
Adrian Hunter52f53362016-06-29 16:24:15 +03003708 return 0;
3709
3710unreg:
3711 if (!IS_ERR(mmc->supply.vqmmc))
3712 regulator_disable(mmc->supply.vqmmc);
3713undma:
3714 if (host->align_buffer)
3715 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3716 host->adma_table_sz, host->align_buffer,
3717 host->align_addr);
3718 host->adma_table = NULL;
3719 host->align_buffer = NULL;
3720
3721 return ret;
3722}
3723EXPORT_SYMBOL_GPL(sdhci_setup_host);
3724
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003725void sdhci_cleanup_host(struct sdhci_host *host)
3726{
3727 struct mmc_host *mmc = host->mmc;
3728
3729 if (!IS_ERR(mmc->supply.vqmmc))
3730 regulator_disable(mmc->supply.vqmmc);
3731
3732 if (host->align_buffer)
3733 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3734 host->adma_table_sz, host->align_buffer,
3735 host->align_addr);
3736 host->adma_table = NULL;
3737 host->align_buffer = NULL;
3738}
3739EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
3740
Adrian Hunter52f53362016-06-29 16:24:15 +03003741int __sdhci_add_host(struct sdhci_host *host)
3742{
3743 struct mmc_host *mmc = host->mmc;
3744 int ret;
3745
Pierre Ossman55db8902006-11-21 17:55:45 +01003746 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003747 * Init tasklets.
3748 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003749 tasklet_init(&host->finish_tasklet,
3750 sdhci_tasklet_finish, (unsigned long)host);
3751
Kees Cook2ee4f622017-10-24 08:03:45 -07003752 timer_setup(&host->timer, sdhci_timeout_timer, 0);
3753 timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003754
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003755 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303756
Shawn Guo2af502c2013-07-05 14:38:55 +08003757 sdhci_init(host, 0);
3758
Russell King781e9892014-04-25 12:55:46 +01003759 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3760 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003761 if (ret) {
3762 pr_err("%s: Failed to request IRQ %d: %d\n",
3763 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003764 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003765 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003766
Adrian Hunter061d17a2016-04-12 14:25:09 +03003767 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003768 if (ret) {
3769 pr_err("%s: Failed to register LED device: %d\n",
3770 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003771 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003772 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003773
Pierre Ossman5f25a662006-10-04 02:15:39 -07003774 mmiowb();
3775
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003776 ret = mmc_add_host(mmc);
3777 if (ret)
3778 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003779
Girish K Sa3c76eb2011-10-11 11:44:09 +05303780 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003781 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003782 (host->flags & SDHCI_USE_ADMA) ?
3783 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003784 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003785
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003786 sdhci_enable_card_detection(host);
3787
Pierre Ossmand129bce2006-03-24 03:18:17 -08003788 return 0;
3789
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003790unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003791 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003792unirq:
Russell King03231f92014-04-25 12:57:12 +01003793 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003794 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3795 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003796 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003797untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003798 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003799
Pierre Ossmand129bce2006-03-24 03:18:17 -08003800 return ret;
3801}
Adrian Hunter52f53362016-06-29 16:24:15 +03003802EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003803
Adrian Hunter52f53362016-06-29 16:24:15 +03003804int sdhci_add_host(struct sdhci_host *host)
3805{
3806 int ret;
3807
3808 ret = sdhci_setup_host(host);
3809 if (ret)
3810 return ret;
3811
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003812 ret = __sdhci_add_host(host);
3813 if (ret)
3814 goto cleanup;
3815
3816 return 0;
3817
3818cleanup:
3819 sdhci_cleanup_host(host);
3820
3821 return ret;
Adrian Hunter52f53362016-06-29 16:24:15 +03003822}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003823EXPORT_SYMBOL_GPL(sdhci_add_host);
3824
Pierre Ossman1e728592008-04-16 19:13:13 +02003825void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003826{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003827 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003828 unsigned long flags;
3829
3830 if (dead) {
3831 spin_lock_irqsave(&host->lock, flags);
3832
3833 host->flags |= SDHCI_DEVICE_DEAD;
3834
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003835 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303836 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003837 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003838 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003839 }
3840
3841 spin_unlock_irqrestore(&host->lock, flags);
3842 }
3843
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003844 sdhci_disable_card_detection(host);
3845
Markus Mayer4e743f12014-07-03 13:27:42 -07003846 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003847
Adrian Hunter061d17a2016-04-12 14:25:09 +03003848 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003849
Pierre Ossman1e728592008-04-16 19:13:13 +02003850 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003851 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003852
Russell Kingb537f942014-04-25 12:56:01 +01003853 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3854 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003855 free_irq(host->irq, host);
3856
3857 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003858 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003859
Pierre Ossmand129bce2006-03-24 03:18:17 -08003860 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003861
Tim Kryger3a48edc2014-06-13 10:13:56 -07003862 if (!IS_ERR(mmc->supply.vqmmc))
3863 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003864
Russell Kingedd63fc2016-01-26 13:39:50 +00003865 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003866 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3867 host->adma_table_sz, host->align_buffer,
3868 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003869
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003870 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003871 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003872}
3873
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003874EXPORT_SYMBOL_GPL(sdhci_remove_host);
3875
3876void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003877{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003878 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003879}
3880
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003881EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003882
3883/*****************************************************************************\
3884 * *
3885 * Driver init/exit *
3886 * *
3887\*****************************************************************************/
3888
3889static int __init sdhci_drv_init(void)
3890{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303891 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003892 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303893 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003894
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003895 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003896}
3897
3898static void __exit sdhci_drv_exit(void)
3899{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003900}
3901
3902module_init(sdhci_drv_init);
3903module_exit(sdhci_drv_exit);
3904
Pierre Ossmandf673b22006-06-30 02:22:31 -07003905module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003906module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003907
Pierre Ossman32710e82009-04-08 20:14:54 +02003908MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003909MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003910MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003911
Pierre Ossmandf673b22006-06-30 02:22:31 -07003912MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003913MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");