blob: 48631b8126881a8fcc0a94a92ae820e6cf888326 [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
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800228static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300230 struct mmc_host *mmc = host->mmc;
231
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800232 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100233 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800234 else
Russell King03231f92014-04-25 12:57:12 +0100235 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800236
Russell Kingb537f942014-04-25 12:56:01 +0100237 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
238 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
239 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
240 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
241 SDHCI_INT_RESPONSE;
242
Dong Aishengf37b20e2016-07-12 15:46:17 +0800243 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
244 host->tuning_mode == SDHCI_TUNING_MODE_3)
245 host->ier |= SDHCI_INT_RETUNE;
246
Russell Kingb537f942014-04-25 12:56:01 +0100247 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
248 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800249
250 if (soft) {
251 /* force clock reconfiguration */
252 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300253 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300255}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800256
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300257static void sdhci_reinit(struct sdhci_host *host)
258{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800259 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300260 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800261}
262
Adrian Hunter061d17a2016-04-12 14:25:09 +0300263static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264{
265 u8 ctrl;
266
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300267 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300269 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270}
271
Adrian Hunter061d17a2016-04-12 14:25:09 +0300272static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273{
274 u8 ctrl;
275
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300278 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279}
280
Masahiro Yamada4f782302016-04-14 13:19:39 +0900281#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100282static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300283 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100284{
285 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
286 unsigned long flags;
287
288 spin_lock_irqsave(&host->lock, flags);
289
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300290 if (host->runtime_suspended)
291 goto out;
292
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100293 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300294 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100295 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300296 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300297out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100298 spin_unlock_irqrestore(&host->lock, flags);
299}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300300
301static int sdhci_led_register(struct sdhci_host *host)
302{
303 struct mmc_host *mmc = host->mmc;
304
305 snprintf(host->led_name, sizeof(host->led_name),
306 "%s::", mmc_hostname(mmc));
307
308 host->led.name = host->led_name;
309 host->led.brightness = LED_OFF;
310 host->led.default_trigger = mmc_hostname(mmc);
311 host->led.brightness_set = sdhci_led_control;
312
313 return led_classdev_register(mmc_dev(mmc), &host->led);
314}
315
316static void sdhci_led_unregister(struct sdhci_host *host)
317{
318 led_classdev_unregister(&host->led);
319}
320
321static inline void sdhci_led_activate(struct sdhci_host *host)
322{
323}
324
325static inline void sdhci_led_deactivate(struct sdhci_host *host)
326{
327}
328
329#else
330
331static inline int sdhci_led_register(struct sdhci_host *host)
332{
333 return 0;
334}
335
336static inline void sdhci_led_unregister(struct sdhci_host *host)
337{
338}
339
340static inline void sdhci_led_activate(struct sdhci_host *host)
341{
342 __sdhci_led_activate(host);
343}
344
345static inline void sdhci_led_deactivate(struct sdhci_host *host)
346{
347 __sdhci_led_deactivate(host);
348}
349
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100350#endif
351
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352/*****************************************************************************\
353 * *
354 * Core functions *
355 * *
356\*****************************************************************************/
357
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100358static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359{
Pierre Ossman76591502008-07-21 00:32:11 +0200360 unsigned long flags;
361 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700362 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200363 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800364
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800371
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300373 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800374
Pierre Ossman76591502008-07-21 00:32:11 +0200375 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 blksize -= len;
378 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200379
Pierre Ossman76591502008-07-21 00:32:11 +0200380 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 while (len) {
383 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300384 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200385 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800386 }
Pierre Ossman76591502008-07-21 00:32:11 +0200387
388 *buf = scratch & 0xFF;
389
390 buf++;
391 scratch >>= 8;
392 chunk--;
393 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800394 }
395 }
Pierre Ossman76591502008-07-21 00:32:11 +0200396
397 sg_miter_stop(&host->sg_miter);
398
399 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800401
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402static void sdhci_write_block_pio(struct sdhci_host *host)
403{
Pierre Ossman76591502008-07-21 00:32:11 +0200404 unsigned long flags;
405 size_t blksize, len, chunk;
406 u32 scratch;
407 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408
409 DBG("PIO writing\n");
410
411 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200412 chunk = 0;
413 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100414
Pierre Ossman76591502008-07-21 00:32:11 +0200415 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416
417 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300418 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100419
Pierre Ossman76591502008-07-21 00:32:11 +0200420 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200421
Pierre Ossman76591502008-07-21 00:32:11 +0200422 blksize -= len;
423 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424
Pierre Ossman76591502008-07-21 00:32:11 +0200425 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426
Pierre Ossman76591502008-07-21 00:32:11 +0200427 while (len) {
428 scratch |= (u32)*buf << (chunk * 8);
429
430 buf++;
431 chunk++;
432 len--;
433
434 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300435 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200436 chunk = 0;
437 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100439 }
440 }
Pierre Ossman76591502008-07-21 00:32:11 +0200441
442 sg_miter_stop(&host->sg_miter);
443
444 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100445}
446
447static void sdhci_transfer_pio(struct sdhci_host *host)
448{
449 u32 mask;
450
Pierre Ossman76591502008-07-21 00:32:11 +0200451 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100452 return;
453
454 if (host->data->flags & MMC_DATA_READ)
455 mask = SDHCI_DATA_AVAILABLE;
456 else
457 mask = SDHCI_SPACE_AVAILABLE;
458
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200459 /*
460 * Some controllers (JMicron JMB38x) mess up the buffer bits
461 * for transfers < 4 bytes. As long as it is just one block,
462 * we can ignore the bits.
463 */
464 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
465 (host->data->blocks == 1))
466 mask = ~0;
467
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300468 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300469 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
470 udelay(100);
471
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100472 if (host->data->flags & MMC_DATA_READ)
473 sdhci_read_block_pio(host);
474 else
475 sdhci_write_block_pio(host);
476
Pierre Ossman76591502008-07-21 00:32:11 +0200477 host->blocks--;
478 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100479 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100480 }
481
482 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800483}
484
Russell King48857d92016-01-26 13:40:16 +0000485static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000486 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000487{
488 int sg_count;
489
Russell King94538e52016-01-26 13:40:37 +0000490 /*
491 * If the data buffers are already mapped, return the previous
492 * dma_map_sg() result.
493 */
494 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000495 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000496
497 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
498 data->flags & MMC_DATA_WRITE ?
499 DMA_TO_DEVICE : DMA_FROM_DEVICE);
500
501 if (sg_count == 0)
502 return -ENOSPC;
503
504 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000505 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000506
507 return sg_count;
508}
509
Pierre Ossman2134a922008-06-28 18:28:51 +0200510static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
511{
512 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800513 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200514}
515
516static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
517{
Cong Wang482fce92011-11-27 13:27:00 +0800518 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200519 local_irq_restore(*flags);
520}
521
Adrian Huntere57a5f62014-11-04 12:42:46 +0200522static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
523 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800524{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200525 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800526
Adrian Huntere57a5f62014-11-04 12:42:46 +0200527 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200528 dma_desc->cmd = cpu_to_le16(cmd);
529 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200530 dma_desc->addr_lo = cpu_to_le32((u32)addr);
531
532 if (host->flags & SDHCI_USE_64_BIT_DMA)
533 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800534}
535
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200536static void sdhci_adma_mark_end(void *desc)
537{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200538 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200539
Adrian Huntere57a5f62014-11-04 12:42:46 +0200540 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200541 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200542}
543
Russell King60c64762016-01-26 13:40:22 +0000544static void sdhci_adma_table_pre(struct sdhci_host *host,
545 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200546{
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200548 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000549 dma_addr_t addr, align_addr;
550 void *desc, *align;
551 char *buffer;
552 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200553
554 /*
555 * The spec does not specify endianness of descriptor table.
556 * We currently guess that it is LE.
557 */
558
Russell King60c64762016-01-26 13:40:22 +0000559 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200560
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200561 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200562 align = host->align_buffer;
563
564 align_addr = host->align_addr;
565
566 for_each_sg(data->sg, sg, host->sg_count, i) {
567 addr = sg_dma_address(sg);
568 len = sg_dma_len(sg);
569
570 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000571 * The SDHCI specification states that ADMA addresses must
572 * be 32-bit aligned. If they aren't, then we use a bounce
573 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200574 * alignment.
575 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200576 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
577 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578 if (offset) {
579 if (data->flags & MMC_DATA_WRITE) {
580 buffer = sdhci_kmap_atomic(sg, &flags);
581 memcpy(align, buffer, offset);
582 sdhci_kunmap_atomic(buffer, &flags);
583 }
584
Ben Dooks118cd172010-03-05 13:43:26 -0800585 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200586 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200587 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200588
589 BUG_ON(offset > 65536);
590
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200591 align += SDHCI_ADMA2_ALIGN;
592 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200593
Adrian Hunter76fe3792014-11-04 12:42:42 +0200594 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200595
596 addr += offset;
597 len -= offset;
598 }
599
Pierre Ossman2134a922008-06-28 18:28:51 +0200600 BUG_ON(len > 65536);
601
Adrian Hunter347ea322015-11-26 14:00:48 +0200602 if (len) {
603 /* tran, valid */
604 sdhci_adma_write_desc(host, desc, addr, len,
605 ADMA2_TRAN_VALID);
606 desc += host->desc_sz;
607 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200608
609 /*
610 * If this triggers then we have a calculation bug
611 * somewhere. :/
612 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200613 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200614 }
615
Thomas Abraham70764a92010-05-26 14:42:04 -0700616 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000617 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200618 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200619 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200620 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700621 }
622 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000623 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200624 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700625 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200626}
627
628static void sdhci_adma_table_post(struct sdhci_host *host,
629 struct mmc_data *data)
630{
Pierre Ossman2134a922008-06-28 18:28:51 +0200631 struct scatterlist *sg;
632 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200633 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200634 char *buffer;
635 unsigned long flags;
636
Russell King47fa9612016-01-26 13:40:06 +0000637 if (data->flags & MMC_DATA_READ) {
638 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100639
Russell King47fa9612016-01-26 13:40:06 +0000640 /* Do a quick scan of the SG list for any unaligned mappings */
641 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200642 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000643 has_unaligned = true;
644 break;
645 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200646
Russell King47fa9612016-01-26 13:40:06 +0000647 if (has_unaligned) {
648 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000649 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200650
Russell King47fa9612016-01-26 13:40:06 +0000651 align = host->align_buffer;
652
653 for_each_sg(data->sg, sg, host->sg_count, i) {
654 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
655 size = SDHCI_ADMA2_ALIGN -
656 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
657
658 buffer = sdhci_kmap_atomic(sg, &flags);
659 memcpy(buffer, align, size);
660 sdhci_kunmap_atomic(buffer, &flags);
661
662 align += SDHCI_ADMA2_ALIGN;
663 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200664 }
665 }
666 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200667}
668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800670{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700671 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500672 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700673 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800674
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200675 /*
676 * If the host controller provides us with an incorrect timeout
677 * value, just skip the check and use 0xE. The hardware may take
678 * longer to time out, but that's much better than having a too-short
679 * timeout value.
680 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200681 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200682 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200683
Andrei Warkentina3c77782011-04-11 16:13:42 -0500684 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100685 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500686 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800687
Andrei Warkentina3c77782011-04-11 16:13:42 -0500688 /* timeout in us */
689 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100690 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300691 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000692 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000693 if (host->clock && data->timeout_clks) {
694 unsigned long long val;
695
696 /*
697 * data->timeout_clks is in units of clock cycles.
698 * host->clock is in Hz. target_timeout is in us.
699 * Hence, us = 1000000 * cycles / Hz. Round up.
700 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200701 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000702 if (do_div(val, host->clock))
703 target_timeout++;
704 target_timeout += val;
705 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300706 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700707
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700708 /*
709 * Figure out needed cycles.
710 * We do this in steps in order to fit inside a 32 bit int.
711 * The first step is the minimum timeout, which will have a
712 * minimum resolution of 6 bits:
713 * (1) 2^13*1000 > 2^22,
714 * (2) host->timeout_clk < 2^16
715 * =>
716 * (1) / (2) > 2^6
717 */
718 count = 0;
719 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
720 while (current_timeout < target_timeout) {
721 count++;
722 current_timeout <<= 1;
723 if (count >= 0xF)
724 break;
725 }
726
727 if (count >= 0xF) {
Adrian Hunterf4218652017-03-20 19:50:39 +0200728 DBG("Too large timeout 0x%x requested for CMD%d!\n",
729 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700730 count = 0xE;
731 }
732
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200733 return count;
734}
735
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300736static void sdhci_set_transfer_irqs(struct sdhci_host *host)
737{
738 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
739 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
740
741 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100742 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300743 else
Russell Kingb537f942014-04-25 12:56:01 +0100744 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
745
746 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
747 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300748}
749
Aisheng Dongb45e6682014-08-27 15:26:29 +0800750static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200751{
752 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800753
754 if (host->ops->set_timeout) {
755 host->ops->set_timeout(host, cmd);
756 } else {
757 count = sdhci_calc_timeout(host, cmd);
758 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
759 }
760}
761
762static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
763{
Pierre Ossman2134a922008-06-28 18:28:51 +0200764 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500765 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200766
Adrian Hunter56a590d2016-06-29 16:24:32 +0300767 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800768 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500769
770 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200771 return;
772
Adrian Hunter43dea092016-06-29 16:24:26 +0300773 WARN_ON(host->data);
774
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200775 /* Sanity checks */
776 BUG_ON(data->blksz * data->blocks > 524288);
777 BUG_ON(data->blksz > host->mmc->max_blk_size);
778 BUG_ON(data->blocks > 65535);
779
780 host->data = data;
781 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400782 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200783
Russell Kingfce14422016-01-26 13:41:20 +0000784 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200785 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000786 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000787 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200788
Russell Kingfce14422016-01-26 13:41:20 +0000789 host->flags |= SDHCI_REQ_USE_DMA;
790
791 /*
792 * FIXME: This doesn't account for merging when mapping the
793 * scatterlist.
794 *
795 * The assumption here being that alignment and lengths are
796 * the same after DMA mapping to device address space.
797 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000798 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000799 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000801 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000802 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000803 /*
804 * As we use up to 3 byte chunks to work
805 * around alignment problems, we need to
806 * check the offset as well.
807 */
808 offset_mask = 3;
809 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200810 } else {
811 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000812 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000813 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
814 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200815 }
816
Russell Kingdf953922016-01-26 13:41:14 +0000817 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200818 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000819 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100820 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000821 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200822 host->flags &= ~SDHCI_REQ_USE_DMA;
823 break;
824 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000825 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100826 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200827 host->flags &= ~SDHCI_REQ_USE_DMA;
828 break;
829 }
830 }
831 }
832 }
833
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200834 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000835 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200836
Russell King60c64762016-01-26 13:40:22 +0000837 if (sg_cnt <= 0) {
838 /*
839 * This only happens when someone fed
840 * us an invalid request.
841 */
842 WARN_ON(1);
843 host->flags &= ~SDHCI_REQ_USE_DMA;
844 } else if (host->flags & SDHCI_USE_ADMA) {
845 sdhci_adma_table_pre(host, data, sg_cnt);
846
847 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
848 if (host->flags & SDHCI_USE_64_BIT_DMA)
849 sdhci_writel(host,
850 (u64)host->adma_addr >> 32,
851 SDHCI_ADMA_ADDRESS_HI);
852 } else {
853 WARN_ON(sg_cnt != 1);
854 sdhci_writel(host, sg_dma_address(data->sg),
855 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200856 }
857 }
858
Pierre Ossman2134a922008-06-28 18:28:51 +0200859 /*
860 * Always adjust the DMA selection as some controllers
861 * (e.g. JMicron) can't do PIO properly when the selection
862 * is ADMA.
863 */
864 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300865 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200866 ctrl &= ~SDHCI_CTRL_DMA_MASK;
867 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200868 (host->flags & SDHCI_USE_ADMA)) {
869 if (host->flags & SDHCI_USE_64_BIT_DMA)
870 ctrl |= SDHCI_CTRL_ADMA64;
871 else
872 ctrl |= SDHCI_CTRL_ADMA32;
873 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200874 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200875 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300876 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100877 }
878
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200879 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200880 int flags;
881
882 flags = SG_MITER_ATOMIC;
883 if (host->data->flags & MMC_DATA_READ)
884 flags |= SG_MITER_TO_SG;
885 else
886 flags |= SG_MITER_FROM_SG;
887 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200888 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800889 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700890
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300891 sdhci_set_transfer_irqs(host);
892
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400893 /* Set the DMA boundary value and block size */
894 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
895 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300896 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897}
898
Adrian Hunter0293d502016-06-29 16:24:35 +0300899static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
900 struct mmc_request *mrq)
901{
Adrian Hunter20845be2016-08-16 13:44:13 +0300902 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
903 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300904}
905
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700906static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500907 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700908{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800909 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500910 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700911
Dong Aisheng2b558c12013-10-30 22:09:48 +0800912 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800913 if (host->quirks2 &
914 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
915 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
916 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800917 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800918 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
919 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800920 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800921 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700922 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800923 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200925 WARN_ON(!host->data);
926
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800927 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
928 mode = SDHCI_TRNS_BLK_CNT_EN;
929
Andrei Warkentine89d4562011-05-23 15:06:37 -0500930 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800931 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500932 /*
933 * If we are sending CMD23, CMD12 never gets sent
934 * on successful completion (so no Auto-CMD12).
935 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300936 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800937 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500938 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300939 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500940 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300941 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500942 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700943 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500944
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700945 if (data->flags & MMC_DATA_READ)
946 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100947 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700948 mode |= SDHCI_TRNS_DMA;
949
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300950 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951}
952
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300953static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
954{
955 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
956 ((mrq->cmd && mrq->cmd->error) ||
957 (mrq->sbc && mrq->sbc->error) ||
958 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
959 (mrq->data->stop && mrq->data->stop->error))) ||
960 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
961}
962
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300963static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
964{
965 int i;
966
967 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
968 if (host->mrqs_done[i] == mrq) {
969 WARN_ON(1);
970 return;
971 }
972 }
973
974 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
975 if (!host->mrqs_done[i]) {
976 host->mrqs_done[i] = mrq;
977 break;
978 }
979 }
980
981 WARN_ON(i >= SDHCI_MAX_MRQS);
982
983 tasklet_schedule(&host->finish_tasklet);
984}
985
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300986static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
987{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300988 if (host->cmd && host->cmd->mrq == mrq)
989 host->cmd = NULL;
990
991 if (host->data_cmd && host->data_cmd->mrq == mrq)
992 host->data_cmd = NULL;
993
994 if (host->data && host->data->mrq == mrq)
995 host->data = NULL;
996
Adrian Huntered1563d2016-06-29 16:24:29 +0300997 if (sdhci_needs_reset(host, mrq))
998 host->pending_reset = true;
999
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001000 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001001}
1002
Pierre Ossmand129bce2006-03-24 03:18:17 -08001003static void sdhci_finish_data(struct sdhci_host *host)
1004{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001005 struct mmc_command *data_cmd = host->data_cmd;
1006 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001009 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010
Russell Kingadd89132016-01-26 13:40:42 +00001011 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1012 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1013 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
1015 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001016 * The specification states that the block count register must
1017 * be updated, but it does not specify at what point in the
1018 * data flow. That makes the register entirely useless to read
1019 * back so we have to assume that nothing made it to the card
1020 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001022 if (data->error)
1023 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001025 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Andrei Warkentine89d4562011-05-23 15:06:37 -05001027 /*
1028 * Need to send CMD12 if -
1029 * a) open-ended multiblock transfer (no CMD23)
1030 * b) error in multiblock transfer
1031 */
1032 if (data->stop &&
1033 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001034 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001035
Pierre Ossmand129bce2006-03-24 03:18:17 -08001036 /*
1037 * The controller needs a reset of internal state machines
1038 * upon error conditions.
1039 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001040 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001041 if (!host->cmd || host->cmd == data_cmd)
1042 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001043 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001044 }
1045
Adrian Hunter20845be2016-08-16 13:44:13 +03001046 /*
1047 * 'cap_cmd_during_tfr' request must not use the command line
1048 * after mmc_command_done() has been called. It is upper layer's
1049 * responsibility to send the stop command if required.
1050 */
1051 if (data->mrq->cap_cmd_during_tfr) {
1052 sdhci_finish_mrq(host, data->mrq);
1053 } else {
1054 /* Avoid triggering warning in sdhci_send_command() */
1055 host->cmd = NULL;
1056 sdhci_send_command(host, data->stop);
1057 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001058 } else {
1059 sdhci_finish_mrq(host, data->mrq);
1060 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061}
1062
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001063static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1064 unsigned long timeout)
1065{
1066 if (sdhci_data_line_cmd(mrq->cmd))
1067 mod_timer(&host->data_timer, timeout);
1068 else
1069 mod_timer(&host->timer, timeout);
1070}
1071
1072static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1073{
1074 if (sdhci_data_line_cmd(mrq->cmd))
1075 del_timer(&host->data_timer);
1076 else
1077 del_timer(&host->timer);
1078}
1079
Dong Aishengc0e551292013-09-13 19:11:31 +08001080void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081{
1082 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001083 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001084 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085
1086 WARN_ON(host->cmd);
1087
Russell King96776202016-01-26 13:39:34 +00001088 /* Initially, a command has no error */
1089 cmd->error = 0;
1090
Adrian Hunterfc605f12016-10-05 12:11:21 +03001091 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1092 cmd->opcode == MMC_STOP_TRANSMISSION)
1093 cmd->flags |= MMC_RSP_BUSY;
1094
Pierre Ossmand129bce2006-03-24 03:18:17 -08001095 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001096 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001097
1098 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001099 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001100 mask |= SDHCI_DATA_INHIBIT;
1101
1102 /* We shouldn't wait for data inihibit for stop commands, even
1103 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001104 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001105 mask &= ~SDHCI_DATA_INHIBIT;
1106
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001107 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001108 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001109 pr_err("%s: Controller never released inhibit bit(s).\n",
1110 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001111 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001112 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001113 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001114 return;
1115 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001116 timeout--;
1117 mdelay(1);
1118 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001119
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001120 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001121 if (!cmd->data && cmd->busy_timeout > 9000)
1122 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001123 else
1124 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001125 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
1127 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001128 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001129 WARN_ON(host->data_cmd);
1130 host->data_cmd = cmd;
1131 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001132
Andrei Warkentina3c77782011-04-11 16:13:42 -05001133 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001135 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001136
Andrei Warkentine89d4562011-05-23 15:06:37 -05001137 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001138
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301140 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001142 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001143 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001144 return;
1145 }
1146
1147 if (!(cmd->flags & MMC_RSP_PRESENT))
1148 flags = SDHCI_CMD_RESP_NONE;
1149 else if (cmd->flags & MMC_RSP_136)
1150 flags = SDHCI_CMD_RESP_LONG;
1151 else if (cmd->flags & MMC_RSP_BUSY)
1152 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1153 else
1154 flags = SDHCI_CMD_RESP_SHORT;
1155
1156 if (cmd->flags & MMC_RSP_CRC)
1157 flags |= SDHCI_CMD_CRC;
1158 if (cmd->flags & MMC_RSP_OPCODE)
1159 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301160
1161 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301162 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1163 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001164 flags |= SDHCI_CMD_DATA;
1165
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001166 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001167}
Dong Aishengc0e551292013-09-13 19:11:31 +08001168EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169
1170static void sdhci_finish_command(struct sdhci_host *host)
1171{
Adrian Huntere0a56402016-06-29 16:24:22 +03001172 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001173 int i;
1174
Adrian Huntere0a56402016-06-29 16:24:22 +03001175 host->cmd = NULL;
1176
1177 if (cmd->flags & MMC_RSP_PRESENT) {
1178 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001179 /* CRC is stripped so we need to do some shifting. */
1180 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001181 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001182 SDHCI_RESPONSE + (3-i)*4) << 8;
1183 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001184 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001185 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 SDHCI_RESPONSE + (3-i)*4-1);
1187 }
1188 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001189 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001190 }
1191 }
1192
Adrian Hunter20845be2016-08-16 13:44:13 +03001193 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1194 mmc_command_done(host->mmc, cmd->mrq);
1195
Adrian Hunter6bde8682016-06-29 16:24:20 +03001196 /*
1197 * The host can send and interrupt when the busy state has
1198 * ended, allowing us to wait without wasting CPU cycles.
1199 * The busy signal uses DAT0 so this is similar to waiting
1200 * for data to complete.
1201 *
1202 * Note: The 1.0 specification is a bit ambiguous about this
1203 * feature so there might be some problems with older
1204 * controllers.
1205 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001206 if (cmd->flags & MMC_RSP_BUSY) {
1207 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001208 DBG("Cannot wait for busy signal when also doing a data transfer");
1209 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001210 cmd == host->data_cmd) {
1211 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001212 return;
1213 }
1214 }
1215
Andrei Warkentine89d4562011-05-23 15:06:37 -05001216 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001217 if (cmd == cmd->mrq->sbc) {
1218 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001219 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001220
Andrei Warkentine89d4562011-05-23 15:06:37 -05001221 /* Processed actual command. */
1222 if (host->data && host->data_early)
1223 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001224
Adrian Huntere0a56402016-06-29 16:24:22 +03001225 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001226 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001227 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001228}
1229
Kevin Liu52983382013-01-31 11:31:37 +08001230static u16 sdhci_get_preset_value(struct sdhci_host *host)
1231{
Russell Kingd975f122014-04-25 12:59:31 +01001232 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001233
Russell Kingd975f122014-04-25 12:59:31 +01001234 switch (host->timing) {
1235 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001236 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1237 break;
Russell Kingd975f122014-04-25 12:59:31 +01001238 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001239 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1240 break;
Russell Kingd975f122014-04-25 12:59:31 +01001241 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001242 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1243 break;
Russell Kingd975f122014-04-25 12:59:31 +01001244 case MMC_TIMING_UHS_SDR104:
1245 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001246 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1247 break;
Russell Kingd975f122014-04-25 12:59:31 +01001248 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001249 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001250 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1251 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001252 case MMC_TIMING_MMC_HS400:
1253 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1254 break;
Kevin Liu52983382013-01-31 11:31:37 +08001255 default:
1256 pr_warn("%s: Invalid UHS-I mode selected\n",
1257 mmc_hostname(host->mmc));
1258 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1259 break;
1260 }
1261 return preset;
1262}
1263
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001264u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1265 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001266{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301267 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001268 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301269 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001270 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001271
Zhangfei Gao85105c52010-08-06 07:10:01 +08001272 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001273 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001274 u16 pre_val;
1275
1276 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1277 pre_val = sdhci_get_preset_value(host);
1278 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1279 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1280 if (host->clk_mul &&
1281 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1282 clk = SDHCI_PROG_CLOCK_MODE;
1283 real_div = div + 1;
1284 clk_mul = host->clk_mul;
1285 } else {
1286 real_div = max_t(int, 1, div << 1);
1287 }
1288 goto clock_set;
1289 }
1290
Arindam Nathc3ed3872011-05-05 12:19:06 +05301291 /*
1292 * Check if the Host Controller supports Programmable Clock
1293 * Mode.
1294 */
1295 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001296 for (div = 1; div <= 1024; div++) {
1297 if ((host->max_clk * host->clk_mul / div)
1298 <= clock)
1299 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001300 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001301 if ((host->max_clk * host->clk_mul / div) <= clock) {
1302 /*
1303 * Set Programmable Clock Mode in the Clock
1304 * Control register.
1305 */
1306 clk = SDHCI_PROG_CLOCK_MODE;
1307 real_div = div;
1308 clk_mul = host->clk_mul;
1309 div--;
1310 } else {
1311 /*
1312 * Divisor can be too small to reach clock
1313 * speed requirement. Then use the base clock.
1314 */
1315 switch_base_clk = true;
1316 }
1317 }
1318
1319 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301320 /* Version 3.00 divisors must be a multiple of 2. */
1321 if (host->max_clk <= clock)
1322 div = 1;
1323 else {
1324 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1325 div += 2) {
1326 if ((host->max_clk / div) <= clock)
1327 break;
1328 }
1329 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001330 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301331 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301332 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1333 && !div && host->max_clk <= 25000000)
1334 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001335 }
1336 } else {
1337 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001338 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001339 if ((host->max_clk / div) <= clock)
1340 break;
1341 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001342 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301343 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001345
Kevin Liu52983382013-01-31 11:31:37 +08001346clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001347 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001348 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301349 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001350 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1351 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001352
1353 return clk;
1354}
1355EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1356
Ritesh Harjanifec79672016-11-21 12:07:19 +05301357void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001358{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001359 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001360
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001362 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363
Chris Ball27f6cb12009-09-22 16:45:31 -07001364 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001365 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001366 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001367 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001368 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001369 pr_err("%s: Internal clock never stabilised.\n",
1370 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371 sdhci_dumpregs(host);
1372 return;
1373 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001374 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001375 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376
1377 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001378 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301380EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1381
1382void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1383{
1384 u16 clk;
1385
1386 host->mmc->actual_clock = 0;
1387
1388 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1389
1390 if (clock == 0)
1391 return;
1392
1393 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1394 sdhci_enable_clk(host, clk);
1395}
Russell King17710592014-04-25 12:58:55 +01001396EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397
Adrian Hunter1dceb042016-03-29 12:45:43 +03001398static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1399 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001400{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001401 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001402
Adrian Hunter1dceb042016-03-29 12:45:43 +03001403 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001404
1405 if (mode != MMC_POWER_OFF)
1406 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1407 else
1408 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1409}
1410
Adrian Hunter606d3132016-10-05 12:11:22 +03001411void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1412 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001413{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001414 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001415
Russell King24fbb3c2014-04-25 13:00:06 +01001416 if (mode != MMC_POWER_OFF) {
1417 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001418 case MMC_VDD_165_195:
1419 pwr = SDHCI_POWER_180;
1420 break;
1421 case MMC_VDD_29_30:
1422 case MMC_VDD_30_31:
1423 pwr = SDHCI_POWER_300;
1424 break;
1425 case MMC_VDD_32_33:
1426 case MMC_VDD_33_34:
1427 pwr = SDHCI_POWER_330;
1428 break;
1429 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001430 WARN(1, "%s: Invalid vdd %#x\n",
1431 mmc_hostname(host->mmc), vdd);
1432 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001433 }
1434 }
1435
1436 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001437 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001438
Pierre Ossmanae628902009-05-03 20:45:03 +02001439 host->pwr = pwr;
1440
1441 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001442 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001443 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1444 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001445 } else {
1446 /*
1447 * Spec says that we should clear the power reg before setting
1448 * a new value. Some controllers don't seem to like this though.
1449 */
1450 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1451 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001452
Russell Kinge921a8b2014-04-25 13:00:01 +01001453 /*
1454 * At least the Marvell CaFe chip gets confused if we set the
1455 * voltage and set turn on power at the same time, so set the
1456 * voltage first.
1457 */
1458 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1459 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001460
Russell Kinge921a8b2014-04-25 13:00:01 +01001461 pwr |= SDHCI_POWER_ON;
1462
Pierre Ossmanae628902009-05-03 20:45:03 +02001463 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1464
Russell Kinge921a8b2014-04-25 13:00:01 +01001465 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1466 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001467
Russell Kinge921a8b2014-04-25 13:00:01 +01001468 /*
1469 * Some controllers need an extra 10ms delay of 10ms before
1470 * they can apply clock after applying power
1471 */
1472 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1473 mdelay(10);
1474 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001475}
Adrian Hunter606d3132016-10-05 12:11:22 +03001476EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001477
Adrian Hunter606d3132016-10-05 12:11:22 +03001478void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1479 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001480{
Adrian Hunter606d3132016-10-05 12:11:22 +03001481 if (IS_ERR(host->mmc->supply.vmmc))
1482 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001483 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001484 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001485}
Adrian Hunter606d3132016-10-05 12:11:22 +03001486EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001487
Pierre Ossmand129bce2006-03-24 03:18:17 -08001488/*****************************************************************************\
1489 * *
1490 * MMC callbacks *
1491 * *
1492\*****************************************************************************/
1493
1494static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1495{
1496 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001497 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498 unsigned long flags;
1499
1500 host = mmc_priv(mmc);
1501
Scott Branden04e079cf2015-03-10 11:35:10 -07001502 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001503 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001504
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 spin_lock_irqsave(&host->lock, flags);
1506
Adrian Hunter061d17a2016-04-12 14:25:09 +03001507 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001508
1509 /*
1510 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1511 * requests if Auto-CMD12 is enabled.
1512 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001513 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001514 if (mrq->stop) {
1515 mrq->data->stop = NULL;
1516 mrq->stop = NULL;
1517 }
1518 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001519
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001520 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001521 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001522 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301523 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001524 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001525 sdhci_send_command(host, mrq->sbc);
1526 else
1527 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301528 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529
Pierre Ossman5f25a662006-10-04 02:15:39 -07001530 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001531 spin_unlock_irqrestore(&host->lock, flags);
1532}
1533
Russell King2317f562014-04-25 12:57:07 +01001534void sdhci_set_bus_width(struct sdhci_host *host, int width)
1535{
1536 u8 ctrl;
1537
1538 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1539 if (width == MMC_BUS_WIDTH_8) {
1540 ctrl &= ~SDHCI_CTRL_4BITBUS;
1541 if (host->version >= SDHCI_SPEC_300)
1542 ctrl |= SDHCI_CTRL_8BITBUS;
1543 } else {
1544 if (host->version >= SDHCI_SPEC_300)
1545 ctrl &= ~SDHCI_CTRL_8BITBUS;
1546 if (width == MMC_BUS_WIDTH_4)
1547 ctrl |= SDHCI_CTRL_4BITBUS;
1548 else
1549 ctrl &= ~SDHCI_CTRL_4BITBUS;
1550 }
1551 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1552}
1553EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1554
Russell King96d7b782014-04-25 12:59:26 +01001555void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1556{
1557 u16 ctrl_2;
1558
1559 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1560 /* Select Bus Speed Mode for host */
1561 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1562 if ((timing == MMC_TIMING_MMC_HS200) ||
1563 (timing == MMC_TIMING_UHS_SDR104))
1564 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1565 else if (timing == MMC_TIMING_UHS_SDR12)
1566 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1567 else if (timing == MMC_TIMING_UHS_SDR25)
1568 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1569 else if (timing == MMC_TIMING_UHS_SDR50)
1570 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1571 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1572 (timing == MMC_TIMING_MMC_DDR52))
1573 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001574 else if (timing == MMC_TIMING_MMC_HS400)
1575 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001576 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1577}
1578EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1579
Dong Aishengded97e02016-04-16 01:29:25 +08001580static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001581{
Dong Aishengded97e02016-04-16 01:29:25 +08001582 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583 u8 ctrl;
1584
Adrian Hunter84ec0482016-12-19 15:33:11 +02001585 if (ios->power_mode == MMC_POWER_UNDEFINED)
1586 return;
1587
Adrian Hunterceb61432011-12-27 15:48:41 +02001588 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001589 if (!IS_ERR(mmc->supply.vmmc) &&
1590 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001591 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001592 return;
1593 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001594
Pierre Ossmand129bce2006-03-24 03:18:17 -08001595 /*
1596 * Reset the chip on each power off.
1597 * Should clear out any weird states.
1598 */
1599 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001600 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001601 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001602 }
1603
Kevin Liu52983382013-01-31 11:31:37 +08001604 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001605 (ios->power_mode == MMC_POWER_UP) &&
1606 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001607 sdhci_enable_preset_value(host, false);
1608
Russell King373073e2014-04-25 12:58:45 +01001609 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001610 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001611 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001612
1613 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1614 host->clock) {
1615 host->timeout_clk = host->mmc->actual_clock ?
1616 host->mmc->actual_clock / 1000 :
1617 host->clock / 1000;
1618 host->mmc->max_busy_timeout =
1619 host->ops->get_max_timeout_count ?
1620 host->ops->get_max_timeout_count(host) :
1621 1 << 27;
1622 host->mmc->max_busy_timeout /= host->timeout_clk;
1623 }
Russell King373073e2014-04-25 12:58:45 +01001624 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001625
Adrian Hunter606d3132016-10-05 12:11:22 +03001626 if (host->ops->set_power)
1627 host->ops->set_power(host, ios->power_mode, ios->vdd);
1628 else
1629 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001630
Philip Rakity643a81f2010-09-23 08:24:32 -07001631 if (host->ops->platform_send_init_74_clocks)
1632 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1633
Russell King2317f562014-04-25 12:57:07 +01001634 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001635
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001636 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001637
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001638 if ((ios->timing == MMC_TIMING_SD_HS ||
Jaehoon Chung273c5412016-10-07 14:08:43 +09001639 ios->timing == MMC_TIMING_MMC_HS ||
1640 ios->timing == MMC_TIMING_MMC_HS400 ||
1641 ios->timing == MMC_TIMING_MMC_HS200 ||
1642 ios->timing == MMC_TIMING_MMC_DDR52 ||
1643 ios->timing == MMC_TIMING_UHS_SDR50 ||
1644 ios->timing == MMC_TIMING_UHS_SDR104 ||
1645 ios->timing == MMC_TIMING_UHS_DDR50 ||
1646 ios->timing == MMC_TIMING_UHS_SDR25)
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001647 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001648 ctrl |= SDHCI_CTRL_HISPD;
1649 else
1650 ctrl &= ~SDHCI_CTRL_HISPD;
1651
Arindam Nathd6d50a12011-05-05 12:18:59 +05301652 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301653 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301654
Russell Kingda91a8f2014-04-25 13:00:12 +01001655 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301656 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301657 /*
1658 * We only need to set Driver Strength if the
1659 * preset value enable is not set.
1660 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001661 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301662 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1663 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1664 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001665 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1666 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301667 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1668 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001669 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1670 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1671 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001672 pr_warn("%s: invalid driver type, default to driver type B\n",
1673 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001674 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1675 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301676
1677 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301678 } else {
1679 /*
1680 * According to SDHC Spec v3.00, if the Preset Value
1681 * Enable in the Host Control 2 register is set, we
1682 * need to reset SD Clock Enable before changing High
1683 * Speed Enable to avoid generating clock gliches.
1684 */
Arindam Nath758535c2011-05-05 12:19:00 +05301685
1686 /* Reset SD Clock Enable */
1687 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1688 clk &= ~SDHCI_CLOCK_CARD_EN;
1689 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1690
1691 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1692
1693 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001694 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301695 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301696
Arindam Nath49c468f2011-05-05 12:19:01 +05301697 /* Reset SD Clock Enable */
1698 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1699 clk &= ~SDHCI_CLOCK_CARD_EN;
1700 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1701
Russell King96d7b782014-04-25 12:59:26 +01001702 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001703 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301704
Kevin Liu52983382013-01-31 11:31:37 +08001705 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1706 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1707 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1708 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1709 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001710 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1711 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001712 u16 preset;
1713
1714 sdhci_enable_preset_value(host, true);
1715 preset = sdhci_get_preset_value(host);
1716 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1717 >> SDHCI_PRESET_DRV_SHIFT;
1718 }
1719
Arindam Nath49c468f2011-05-05 12:19:01 +05301720 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001721 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301722 } else
1723 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301724
Leandro Dorileob8352262007-07-25 23:47:04 +02001725 /*
1726 * Some (ENE) controllers go apeshit on some ios operation,
1727 * signalling timeout and CRC errors even on CMD0. Resetting
1728 * it on each ios seems to solve the problem.
1729 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301730 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001731 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001732
Pierre Ossman5f25a662006-10-04 02:15:39 -07001733 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001734}
1735
Dong Aishengded97e02016-04-16 01:29:25 +08001736static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001737{
1738 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001739 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001740
1741 if (host->flags & SDHCI_DEVICE_DEAD)
1742 return 0;
1743
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001744 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001745 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001746 return 1;
1747
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001748 /*
1749 * Try slot gpio detect, if defined it take precedence
1750 * over build in controller functionality
1751 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001752 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001753 return !!gpio_cd;
1754
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001755 /* If polling, assume that the card is always present. */
1756 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1757 return 1;
1758
Kevin Liu94144a42013-02-28 17:35:53 +08001759 /* Host native card detect */
1760 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1761}
1762
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001763static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001765 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001766 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001767
Pierre Ossmand129bce2006-03-24 03:18:17 -08001768 spin_lock_irqsave(&host->lock, flags);
1769
Pierre Ossman1e728592008-04-16 19:13:13 +02001770 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001771 is_readonly = 0;
1772 else if (host->ops->get_ro)
1773 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001774 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001775 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1776 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001777
1778 spin_unlock_irqrestore(&host->lock, flags);
1779
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001780 /* This quirk needs to be replaced by a callback-function later */
1781 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1782 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001783}
1784
Takashi Iwai82b0e232011-04-21 20:26:38 +02001785#define SAMPLE_COUNT 5
1786
Dong Aishengded97e02016-04-16 01:29:25 +08001787static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001788{
Dong Aishengded97e02016-04-16 01:29:25 +08001789 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001790 int i, ro_count;
1791
Takashi Iwai82b0e232011-04-21 20:26:38 +02001792 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001793 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001794
1795 ro_count = 0;
1796 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001797 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001798 if (++ro_count > SAMPLE_COUNT / 2)
1799 return 1;
1800 }
1801 msleep(30);
1802 }
1803 return 0;
1804}
1805
Adrian Hunter20758b62011-08-29 16:42:12 +03001806static void sdhci_hw_reset(struct mmc_host *mmc)
1807{
1808 struct sdhci_host *host = mmc_priv(mmc);
1809
1810 if (host->ops && host->ops->hw_reset)
1811 host->ops->hw_reset(host);
1812}
1813
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001814static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1815{
Russell Kingbe138552014-04-25 12:55:56 +01001816 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001817 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001818 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001819 else
Russell Kingb537f942014-04-25 12:56:01 +01001820 host->ier &= ~SDHCI_INT_CARD_INT;
1821
1822 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1823 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001824 mmiowb();
1825 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001826}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001827
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001828static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1829{
1830 struct sdhci_host *host = mmc_priv(mmc);
1831 unsigned long flags;
1832
Hans de Goede923713b2017-03-26 13:14:45 +02001833 if (enable)
1834 pm_runtime_get_noresume(host->mmc->parent);
1835
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001836 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001837 if (enable)
1838 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1839 else
1840 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1841
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001842 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001843 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001844
1845 if (!enable)
1846 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001847}
1848
Dong Aishengded97e02016-04-16 01:29:25 +08001849static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1850 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001851{
Dong Aishengded97e02016-04-16 01:29:25 +08001852 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001853 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001854 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001855
1856 /*
1857 * Signal Voltage Switching is only applicable for Host Controllers
1858 * v3.00 and above.
1859 */
1860 if (host->version < SDHCI_SPEC_300)
1861 return 0;
1862
Philip Rakity6231f3d2012-07-23 15:56:23 -07001863 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001864
Fabio Estevam21f59982013-02-14 10:35:03 -02001865 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001866 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001867 if (!(host->flags & SDHCI_SIGNALING_330))
1868 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001869 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1870 ctrl &= ~SDHCI_CTRL_VDD_180;
1871 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1872
Tim Kryger3a48edc2014-06-13 10:13:56 -07001873 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001874 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001875 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001876 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1877 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001878 return -EIO;
1879 }
1880 }
1881 /* Wait for 5ms */
1882 usleep_range(5000, 5500);
1883
1884 /* 3.3V regulator output should be stable within 5 ms */
1885 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1886 if (!(ctrl & SDHCI_CTRL_VDD_180))
1887 return 0;
1888
Joe Perches66061102014-09-12 14:56:56 -07001889 pr_warn("%s: 3.3V regulator output did not became stable\n",
1890 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001891
1892 return -EAGAIN;
1893 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001894 if (!(host->flags & SDHCI_SIGNALING_180))
1895 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001896 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001897 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001898 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001899 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1900 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001901 return -EIO;
1902 }
1903 }
1904
1905 /*
1906 * Enable 1.8V Signal Enable in the Host Control2
1907 * register
1908 */
1909 ctrl |= SDHCI_CTRL_VDD_180;
1910 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1911
Vincent Yang9d967a62015-01-20 16:05:15 +08001912 /* Some controller need to do more when switching */
1913 if (host->ops->voltage_switch)
1914 host->ops->voltage_switch(host);
1915
Kevin Liu20b92a32012-12-17 19:29:26 +08001916 /* 1.8V regulator output should be stable within 5 ms */
1917 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1918 if (ctrl & SDHCI_CTRL_VDD_180)
1919 return 0;
1920
Joe Perches66061102014-09-12 14:56:56 -07001921 pr_warn("%s: 1.8V regulator output did not became stable\n",
1922 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001923
1924 return -EAGAIN;
1925 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001926 if (!(host->flags & SDHCI_SIGNALING_120))
1927 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001928 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001929 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001930 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001931 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1932 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001933 return -EIO;
1934 }
1935 }
1936 return 0;
1937 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301938 /* No signal voltage switch required */
1939 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001940 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301941}
1942
Kevin Liu20b92a32012-12-17 19:29:26 +08001943static int sdhci_card_busy(struct mmc_host *mmc)
1944{
1945 struct sdhci_host *host = mmc_priv(mmc);
1946 u32 present_state;
1947
Adrian Huntere613cc42016-06-23 14:00:58 +03001948 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001949 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001950
Adrian Huntere613cc42016-06-23 14:00:58 +03001951 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001952}
1953
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001954static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1955{
1956 struct sdhci_host *host = mmc_priv(mmc);
1957 unsigned long flags;
1958
1959 spin_lock_irqsave(&host->lock, flags);
1960 host->flags |= SDHCI_HS400_TUNING;
1961 spin_unlock_irqrestore(&host->lock, flags);
1962
1963 return 0;
1964}
1965
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02001966static void sdhci_start_tuning(struct sdhci_host *host)
1967{
1968 u16 ctrl;
1969
1970 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1971 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1972 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1973 ctrl |= SDHCI_CTRL_TUNED_CLK;
1974 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1975
1976 /*
1977 * As per the Host Controller spec v3.00, tuning command
1978 * generates Buffer Read Ready interrupt, so enable that.
1979 *
1980 * Note: The spec clearly says that when tuning sequence
1981 * is being performed, the controller does not generate
1982 * interrupts other than Buffer Read Ready interrupt. But
1983 * to make sure we don't hit a controller bug, we _only_
1984 * enable Buffer Read Ready interrupt here.
1985 */
1986 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1987 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1988}
1989
1990static void sdhci_end_tuning(struct sdhci_host *host)
1991{
1992 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1993 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1994}
1995
1996static void sdhci_reset_tuning(struct sdhci_host *host)
1997{
1998 u16 ctrl;
1999
2000 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2001 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2002 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2003 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2004}
2005
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002006static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002007{
2008 sdhci_reset_tuning(host);
2009
2010 sdhci_do_reset(host, SDHCI_RESET_CMD);
2011 sdhci_do_reset(host, SDHCI_RESET_DATA);
2012
2013 sdhci_end_tuning(host);
2014
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002015 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002016}
2017
2018/*
2019 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2020 * tuning command does not have a data payload (or rather the hardware does it
2021 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2022 * interrupt setup is different to other commands and there is no timeout
2023 * interrupt so special handling is needed.
2024 */
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002025static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002026{
2027 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002028 struct mmc_command cmd = {};
2029 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002030 unsigned long flags;
2031
2032 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002033
2034 cmd.opcode = opcode;
2035 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2036 cmd.mrq = &mrq;
2037
2038 mrq.cmd = &cmd;
2039 /*
2040 * In response to CMD19, the card sends 64 bytes of tuning
2041 * block to the Host Controller. So we set the block size
2042 * to 64 here.
2043 */
Adrian Hunter85336102016-12-02 15:14:26 +02002044 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2045 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2046 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
2047 else
2048 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002049
2050 /*
2051 * The tuning block is sent by the card to the host controller.
2052 * So we set the TRNS_READ bit in the Transfer Mode register.
2053 * This also takes care of setting DMA Enable and Multi Block
2054 * Select in the same register to 0.
2055 */
2056 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2057
2058 sdhci_send_command(host, &cmd);
2059
2060 host->cmd = NULL;
2061
2062 sdhci_del_timer(host, &mrq);
2063
2064 host->tuning_done = 0;
2065
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002066 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002067 spin_unlock_irqrestore(&host->lock, flags);
2068
2069 /* Wait for Buffer Read Ready interrupt */
2070 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2071 msecs_to_jiffies(50));
2072
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002073}
2074
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002075static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002076{
2077 int i;
2078
2079 /*
2080 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2081 * of loops reaches 40 times.
2082 */
2083 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2084 u16 ctrl;
2085
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002086 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002087
2088 if (!host->tuning_done) {
2089 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2090 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002091 sdhci_abort_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002092 return;
2093 }
2094
2095 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2096 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2097 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2098 return; /* Success! */
2099 break;
2100 }
2101
2102 /* eMMC spec does not require a delay between tuning cycles */
2103 if (opcode == MMC_SEND_TUNING_BLOCK)
2104 mdelay(1);
2105 }
2106
2107 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2108 mmc_hostname(host->mmc));
2109 sdhci_reset_tuning(host);
2110}
2111
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002112int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302113{
Russell King4b6f37d2014-04-25 12:59:36 +01002114 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302115 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002116 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002117 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302118
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002119 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002120
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002121 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2122 tuning_count = host->tuning_count;
2123
Arindam Nathb513ea22011-05-05 12:19:04 +05302124 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002125 * The Host Controller needs tuning in case of SDR104 and DDR50
2126 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2127 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302128 * If the Host Controller supports the HS200 mode then the
2129 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302130 */
Russell King4b6f37d2014-04-25 12:59:36 +01002131 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002132 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002133 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002134 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002135 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002136
Russell King4b6f37d2014-04-25 12:59:36 +01002137 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002138 /*
2139 * Periodic re-tuning for HS400 is not expected to be needed, so
2140 * disable it here.
2141 */
2142 if (hs400_tuning)
2143 tuning_count = 0;
2144 break;
2145
Russell King4b6f37d2014-04-25 12:59:36 +01002146 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002147 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002148 break;
Girish K S069c9f12012-01-06 09:56:39 +05302149
Russell King4b6f37d2014-04-25 12:59:36 +01002150 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002151 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002152 break;
2153 /* FALLTHROUGH */
2154
2155 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002156 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302157 }
2158
Dong Aisheng45251812013-09-13 19:11:30 +08002159 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302160 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002161 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002162 }
2163
Adrian Hunter6b11e702016-12-02 15:14:27 +02002164 host->mmc->retune_period = tuning_count;
2165
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002166 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302167
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002168 __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302169
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002170 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002171out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302172 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002173
Arindam Nathb513ea22011-05-05 12:19:04 +05302174 return err;
2175}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002176EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302177
Kevin Liu52983382013-01-31 11:31:37 +08002178static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302179{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302180 /* Host Controller v3.00 defines preset value registers */
2181 if (host->version < SDHCI_SPEC_300)
2182 return;
2183
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302184 /*
2185 * We only enable or disable Preset Value if they are not already
2186 * enabled or disabled respectively. Otherwise, we bail out.
2187 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002188 if (host->preset_enabled != enable) {
2189 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2190
2191 if (enable)
2192 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2193 else
2194 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2195
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302196 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002197
2198 if (enable)
2199 host->flags |= SDHCI_PV_ENABLED;
2200 else
2201 host->flags &= ~SDHCI_PV_ENABLED;
2202
2203 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302204 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002205}
2206
Haibo Chen348487c2014-12-09 17:04:05 +08002207static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2208 int err)
2209{
2210 struct sdhci_host *host = mmc_priv(mmc);
2211 struct mmc_data *data = mrq->data;
2212
Russell Kingf48f0392016-01-26 13:40:32 +00002213 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002214 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2215 data->flags & MMC_DATA_WRITE ?
2216 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2217
2218 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002219}
2220
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002221static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002222{
2223 struct sdhci_host *host = mmc_priv(mmc);
2224
Haibo Chend31911b2015-08-25 10:02:11 +08002225 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002226
2227 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002228 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002229}
2230
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002231static inline bool sdhci_has_requests(struct sdhci_host *host)
2232{
2233 return host->cmd || host->data_cmd;
2234}
2235
2236static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2237{
2238 if (host->data_cmd) {
2239 host->data_cmd->error = err;
2240 sdhci_finish_mrq(host, host->data_cmd->mrq);
2241 }
2242
2243 if (host->cmd) {
2244 host->cmd->error = err;
2245 sdhci_finish_mrq(host, host->cmd->mrq);
2246 }
2247}
2248
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002249static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002250{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002251 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002253 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002254
Christian Daudt722e1282013-06-20 14:26:36 -07002255 /* First check if client has provided their own card event */
2256 if (host->ops->card_event)
2257 host->ops->card_event(host);
2258
Adrian Hunterd3940f22016-06-29 16:24:14 +03002259 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002260
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261 spin_lock_irqsave(&host->lock, flags);
2262
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002263 /* Check sdhci_has_requests() first in case we are runtime suspended */
2264 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302265 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002266 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302267 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002268 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002269
Russell King03231f92014-04-25 12:57:12 +01002270 sdhci_do_reset(host, SDHCI_RESET_CMD);
2271 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002273 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274 }
2275
2276 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002277}
2278
2279static const struct mmc_host_ops sdhci_ops = {
2280 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002281 .post_req = sdhci_post_req,
2282 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002283 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002284 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002285 .get_ro = sdhci_get_ro,
2286 .hw_reset = sdhci_hw_reset,
2287 .enable_sdio_irq = sdhci_enable_sdio_irq,
2288 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002289 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002290 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002291 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002292 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002293};
2294
2295/*****************************************************************************\
2296 * *
2297 * Tasklets *
2298 * *
2299\*****************************************************************************/
2300
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002301static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 unsigned long flags;
2304 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002305 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002306
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002307 spin_lock_irqsave(&host->lock, flags);
2308
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002309 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2310 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002311 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002312 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002313 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002314
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002315 if (!mrq) {
2316 spin_unlock_irqrestore(&host->lock, flags);
2317 return true;
2318 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002319
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002320 sdhci_del_timer(host, mrq);
2321
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322 /*
Russell King054cedf2016-01-26 13:40:42 +00002323 * Always unmap the data buffers if they were mapped by
2324 * sdhci_prepare_data() whenever we finish with a request.
2325 * This avoids leaking DMA mappings on error.
2326 */
2327 if (host->flags & SDHCI_REQ_USE_DMA) {
2328 struct mmc_data *data = mrq->data;
2329
2330 if (data && data->host_cookie == COOKIE_MAPPED) {
2331 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2332 (data->flags & MMC_DATA_READ) ?
2333 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2334 data->host_cookie = COOKIE_UNMAPPED;
2335 }
2336 }
2337
2338 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002339 * The controller needs a reset of internal state machines
2340 * upon error conditions.
2341 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002342 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002343 /*
2344 * Do not finish until command and data lines are available for
2345 * reset. Note there can only be one other mrq, so it cannot
2346 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2347 * would both be null.
2348 */
2349 if (host->cmd || host->data_cmd) {
2350 spin_unlock_irqrestore(&host->lock, flags);
2351 return true;
2352 }
2353
Pierre Ossman645289d2006-06-30 02:22:33 -07002354 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002355 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002356 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002357 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002358
2359 /* Spec says we should do both at the same time, but Ricoh
2360 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002361 sdhci_do_reset(host, SDHCI_RESET_CMD);
2362 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002363
2364 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002365 }
2366
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002367 if (!sdhci_has_requests(host))
2368 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002369
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002370 host->mrqs_done[i] = NULL;
2371
Pierre Ossman5f25a662006-10-04 02:15:39 -07002372 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002373 spin_unlock_irqrestore(&host->lock, flags);
2374
2375 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002376
2377 return false;
2378}
2379
2380static void sdhci_tasklet_finish(unsigned long param)
2381{
2382 struct sdhci_host *host = (struct sdhci_host *)param;
2383
2384 while (!sdhci_request_done(host))
2385 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386}
2387
2388static void sdhci_timeout_timer(unsigned long data)
2389{
2390 struct sdhci_host *host;
2391 unsigned long flags;
2392
2393 host = (struct sdhci_host*)data;
2394
2395 spin_lock_irqsave(&host->lock, flags);
2396
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002397 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2398 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2399 mmc_hostname(host->mmc));
2400 sdhci_dumpregs(host);
2401
2402 host->cmd->error = -ETIMEDOUT;
2403 sdhci_finish_mrq(host, host->cmd->mrq);
2404 }
2405
2406 mmiowb();
2407 spin_unlock_irqrestore(&host->lock, flags);
2408}
2409
2410static void sdhci_timeout_data_timer(unsigned long data)
2411{
2412 struct sdhci_host *host;
2413 unsigned long flags;
2414
2415 host = (struct sdhci_host *)data;
2416
2417 spin_lock_irqsave(&host->lock, flags);
2418
2419 if (host->data || host->data_cmd ||
2420 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002421 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2422 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423 sdhci_dumpregs(host);
2424
2425 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002426 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002427 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002428 } else if (host->data_cmd) {
2429 host->data_cmd->error = -ETIMEDOUT;
2430 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002431 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002432 host->cmd->error = -ETIMEDOUT;
2433 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 }
2435 }
2436
Pierre Ossman5f25a662006-10-04 02:15:39 -07002437 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002438 spin_unlock_irqrestore(&host->lock, flags);
2439}
2440
2441/*****************************************************************************\
2442 * *
2443 * Interrupt handling *
2444 * *
2445\*****************************************************************************/
2446
Adrian Hunterfc605f12016-10-05 12:11:21 +03002447static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002448{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002449 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002450 /*
2451 * SDHCI recovers from errors by resetting the cmd and data
2452 * circuits. Until that is done, there very well might be more
2453 * interrupts, so ignore them in that case.
2454 */
2455 if (host->pending_reset)
2456 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002457 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2458 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 sdhci_dumpregs(host);
2460 return;
2461 }
2462
Russell Kingec014cb2016-01-26 13:39:39 +00002463 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2464 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2465 if (intmask & SDHCI_INT_TIMEOUT)
2466 host->cmd->error = -ETIMEDOUT;
2467 else
2468 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002469
Russell King71fcbda2016-01-26 13:39:45 +00002470 /*
2471 * If this command initiates a data phase and a response
2472 * CRC error is signalled, the card can start transferring
2473 * data - the card may have received the command without
2474 * error. We must not terminate the mmc_request early.
2475 *
2476 * If the card did not receive the command or returned an
2477 * error which prevented it sending data, the data phase
2478 * will time out.
2479 */
2480 if (host->cmd->data &&
2481 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2482 SDHCI_INT_CRC) {
2483 host->cmd = NULL;
2484 return;
2485 }
2486
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002487 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002488 return;
2489 }
2490
Pierre Ossmane8095172008-07-25 01:09:08 +02002491 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002492 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002493}
2494
George G. Davis0957c332010-02-18 12:32:12 -05002495#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002496static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002497{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002498 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002499
2500 sdhci_dumpregs(host);
2501
2502 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002503 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002504
Adrian Huntere57a5f62014-11-04 12:42:46 +02002505 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002506 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2507 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002508 le32_to_cpu(dma_desc->addr_lo),
2509 le16_to_cpu(dma_desc->len),
2510 le16_to_cpu(dma_desc->cmd));
2511 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002512 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2513 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002514 le16_to_cpu(dma_desc->len),
2515 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002516
Adrian Hunter76fe3792014-11-04 12:42:42 +02002517 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002518
Adrian Hunter05452302014-11-04 12:42:45 +02002519 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002520 break;
2521 }
2522}
2523#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002524static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002525#endif
2526
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2528{
Girish K S069c9f12012-01-06 09:56:39 +05302529 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002530
Arindam Nathb513ea22011-05-05 12:19:04 +05302531 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2532 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302533 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2534 if (command == MMC_SEND_TUNING_BLOCK ||
2535 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302536 host->tuning_done = 1;
2537 wake_up(&host->buf_ready_int);
2538 return;
2539 }
2540 }
2541
Pierre Ossmand129bce2006-03-24 03:18:17 -08002542 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002543 struct mmc_command *data_cmd = host->data_cmd;
2544
Pierre Ossmand129bce2006-03-24 03:18:17 -08002545 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002546 * The "data complete" interrupt is also used to
2547 * indicate that a busy state has ended. See comment
2548 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002550 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002551 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002552 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002553 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002554 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002555 return;
2556 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002557 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002558 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002559 /*
2560 * Some cards handle busy-end interrupt
2561 * before the command completed, so make
2562 * sure we do things in the proper order.
2563 */
Adrian Hunterea968022016-06-29 16:24:24 +03002564 if (host->cmd == data_cmd)
2565 return;
2566
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002567 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002568 return;
2569 }
2570 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571
Adrian Huntered1563d2016-06-29 16:24:29 +03002572 /*
2573 * SDHCI recovers from errors by resetting the cmd and data
2574 * circuits. Until that is done, there very well might be more
2575 * interrupts, so ignore them in that case.
2576 */
2577 if (host->pending_reset)
2578 return;
2579
Marek Vasut2e4456f2015-11-18 10:47:02 +01002580 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2581 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002582 sdhci_dumpregs(host);
2583
2584 return;
2585 }
2586
2587 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002588 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002589 else if (intmask & SDHCI_INT_DATA_END_BIT)
2590 host->data->error = -EILSEQ;
2591 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2592 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2593 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002594 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002595 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302596 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002597 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002598 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002599 if (host->ops->adma_workaround)
2600 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002601 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602
Pierre Ossman17b04292007-07-22 22:18:46 +02002603 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604 sdhci_finish_data(host);
2605 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002606 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002607 sdhci_transfer_pio(host);
2608
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002609 /*
2610 * We currently don't do anything fancy with DMA
2611 * boundaries, but as we can't disable the feature
2612 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002613 *
2614 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2615 * should return a valid address to continue from, but as
2616 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002617 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002618 if (intmask & SDHCI_INT_DMA_END) {
2619 u32 dmastart, dmanow;
2620 dmastart = sg_dma_address(host->data->sg);
2621 dmanow = dmastart + host->data->bytes_xfered;
2622 /*
2623 * Force update to the next DMA block boundary.
2624 */
2625 dmanow = (dmanow &
2626 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2627 SDHCI_DEFAULT_BOUNDARY_SIZE;
2628 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002629 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2630 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002631 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2632 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002633
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002634 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002635 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002636 /*
2637 * Data managed to finish before the
2638 * command completed. Make sure we do
2639 * things in the proper order.
2640 */
2641 host->data_early = 1;
2642 } else {
2643 sdhci_finish_data(host);
2644 }
2645 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002646 }
2647}
2648
David Howells7d12e782006-10-05 14:55:46 +01002649static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002650{
Russell King781e9892014-04-25 12:55:46 +01002651 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002652 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002653 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002654 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002655
2656 spin_lock(&host->lock);
2657
Russell Kingbe138552014-04-25 12:55:56 +01002658 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002659 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002660 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002661 }
2662
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002663 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002664 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665 result = IRQ_NONE;
2666 goto out;
2667 }
2668
Russell King41005002014-04-25 12:55:36 +01002669 do {
2670 /* Clear selected interrupts. */
2671 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2672 SDHCI_INT_BUS_POWER);
2673 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002674
Adrian Hunterf4218652017-03-20 19:50:39 +02002675 DBG("IRQ status 0x%08x\n", intmask);
Russell King41005002014-04-25 12:55:36 +01002676
2677 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2678 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2679 SDHCI_CARD_PRESENT;
2680
2681 /*
2682 * There is a observation on i.mx esdhc. INSERT
2683 * bit will be immediately set again when it gets
2684 * cleared, if a card is inserted. We have to mask
2685 * the irq to prevent interrupt storm which will
2686 * freeze the system. And the REMOVE gets the
2687 * same situation.
2688 *
2689 * More testing are needed here to ensure it works
2690 * for other platforms though.
2691 */
Russell Kingb537f942014-04-25 12:56:01 +01002692 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2693 SDHCI_INT_CARD_REMOVE);
2694 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2695 SDHCI_INT_CARD_INSERT;
2696 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2697 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002698
2699 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2700 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002701
2702 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2703 SDHCI_INT_CARD_REMOVE);
2704 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002705 }
2706
2707 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002708 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002709
2710 if (intmask & SDHCI_INT_DATA_MASK)
2711 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2712
2713 if (intmask & SDHCI_INT_BUS_POWER)
2714 pr_err("%s: Card is consuming too much power!\n",
2715 mmc_hostname(host->mmc));
2716
Dong Aishengf37b20e2016-07-12 15:46:17 +08002717 if (intmask & SDHCI_INT_RETUNE)
2718 mmc_retune_needed(host->mmc);
2719
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002720 if ((intmask & SDHCI_INT_CARD_INT) &&
2721 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002722 sdhci_enable_sdio_irq_nolock(host, false);
2723 host->thread_isr |= SDHCI_INT_CARD_INT;
2724 result = IRQ_WAKE_THREAD;
2725 }
Russell King41005002014-04-25 12:55:36 +01002726
2727 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2728 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2729 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002730 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002731
2732 if (intmask) {
2733 unexpected |= intmask;
2734 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2735 }
2736
Russell King781e9892014-04-25 12:55:46 +01002737 if (result == IRQ_NONE)
2738 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002739
2740 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002741 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002742out:
2743 spin_unlock(&host->lock);
2744
Alexander Stein6379b232012-03-14 09:52:10 +01002745 if (unexpected) {
2746 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2747 mmc_hostname(host->mmc), unexpected);
2748 sdhci_dumpregs(host);
2749 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002750
Pierre Ossmand129bce2006-03-24 03:18:17 -08002751 return result;
2752}
2753
Russell King781e9892014-04-25 12:55:46 +01002754static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2755{
2756 struct sdhci_host *host = dev_id;
2757 unsigned long flags;
2758 u32 isr;
2759
2760 spin_lock_irqsave(&host->lock, flags);
2761 isr = host->thread_isr;
2762 host->thread_isr = 0;
2763 spin_unlock_irqrestore(&host->lock, flags);
2764
Russell King3560db82014-04-25 12:55:51 +01002765 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002766 struct mmc_host *mmc = host->mmc;
2767
2768 mmc->ops->card_event(mmc);
2769 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002770 }
2771
Russell King781e9892014-04-25 12:55:46 +01002772 if (isr & SDHCI_INT_CARD_INT) {
2773 sdio_run_irqs(host->mmc);
2774
2775 spin_lock_irqsave(&host->lock, flags);
2776 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2777 sdhci_enable_sdio_irq_nolock(host, true);
2778 spin_unlock_irqrestore(&host->lock, flags);
2779 }
2780
2781 return isr ? IRQ_HANDLED : IRQ_NONE;
2782}
2783
Pierre Ossmand129bce2006-03-24 03:18:17 -08002784/*****************************************************************************\
2785 * *
2786 * Suspend/resume *
2787 * *
2788\*****************************************************************************/
2789
2790#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002791/*
2792 * To enable wakeup events, the corresponding events have to be enabled in
2793 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2794 * Table' in the SD Host Controller Standard Specification.
2795 * It is useless to restore SDHCI_INT_ENABLE state in
2796 * sdhci_disable_irq_wakeups() since it will be set by
2797 * sdhci_enable_card_detection() or sdhci_init().
2798 */
Kevin Liuad080d72013-01-05 17:21:33 +08002799void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2800{
2801 u8 val;
2802 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2803 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002804 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2805 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002806
2807 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2808 val |= mask ;
2809 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002810 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002811 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002812 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2813 }
Kevin Liuad080d72013-01-05 17:21:33 +08002814 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002815 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002816}
2817EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2818
Fabio Estevam0b10f472014-08-30 14:53:13 -03002819static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002820{
2821 u8 val;
2822 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2823 | SDHCI_WAKE_ON_INT;
2824
2825 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2826 val &= ~mask;
2827 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2828}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002829
Manuel Lauss29495aa2011-11-03 11:09:45 +01002830int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002831{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002832 sdhci_disable_card_detection(host);
2833
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002834 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302835
Kevin Liuad080d72013-01-05 17:21:33 +08002836 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002837 host->ier = 0;
2838 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2839 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002840 free_irq(host->irq, host);
2841 } else {
2842 sdhci_enable_irq_wakeups(host);
2843 enable_irq_wake(host->irq);
2844 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002845 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002846}
2847
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002848EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002849
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002850int sdhci_resume_host(struct sdhci_host *host)
2851{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002852 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002853 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002854
Richard Röjforsa13abc72009-09-22 16:45:30 -07002855 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002856 if (host->ops->enable_dma)
2857 host->ops->enable_dma(host);
2858 }
2859
Adrian Hunter6308d292012-02-07 14:48:54 +02002860 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2861 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2862 /* Card keeps power but host controller does not */
2863 sdhci_init(host, 0);
2864 host->pwr = 0;
2865 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002866 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002867 } else {
2868 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2869 mmiowb();
2870 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002871
Haibo Chen14a7b41642015-09-15 18:32:58 +08002872 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2873 ret = request_threaded_irq(host->irq, sdhci_irq,
2874 sdhci_thread_irq, IRQF_SHARED,
2875 mmc_hostname(host->mmc), host);
2876 if (ret)
2877 return ret;
2878 } else {
2879 sdhci_disable_irq_wakeups(host);
2880 disable_irq_wake(host->irq);
2881 }
2882
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002883 sdhci_enable_card_detection(host);
2884
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002885 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002886}
2887
2888EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002889
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002890int sdhci_runtime_suspend_host(struct sdhci_host *host)
2891{
2892 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002893
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002894 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002895
2896 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002897 host->ier &= SDHCI_INT_CARD_INT;
2898 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2899 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002900 spin_unlock_irqrestore(&host->lock, flags);
2901
Russell King781e9892014-04-25 12:55:46 +01002902 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002903
2904 spin_lock_irqsave(&host->lock, flags);
2905 host->runtime_suspended = true;
2906 spin_unlock_irqrestore(&host->lock, flags);
2907
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002908 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002909}
2910EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2911
2912int sdhci_runtime_resume_host(struct sdhci_host *host)
2913{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002914 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002915 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002916 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002917
2918 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2919 if (host->ops->enable_dma)
2920 host->ops->enable_dma(host);
2921 }
2922
2923 sdhci_init(host, 0);
2924
Adrian Hunter84ec0482016-12-19 15:33:11 +02002925 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED) {
2926 /* Force clock and power re-program */
2927 host->pwr = 0;
2928 host->clock = 0;
2929 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2930 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002931
Adrian Hunter84ec0482016-12-19 15:33:11 +02002932 if ((host_flags & SDHCI_PV_ENABLED) &&
2933 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2934 spin_lock_irqsave(&host->lock, flags);
2935 sdhci_enable_preset_value(host, true);
2936 spin_unlock_irqrestore(&host->lock, flags);
2937 }
2938
2939 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2940 mmc->ops->hs400_enhanced_strobe)
2941 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002942 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002943
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002944 spin_lock_irqsave(&host->lock, flags);
2945
2946 host->runtime_suspended = false;
2947
2948 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002949 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002950 sdhci_enable_sdio_irq_nolock(host, true);
2951
2952 /* Enable Card Detection */
2953 sdhci_enable_card_detection(host);
2954
2955 spin_unlock_irqrestore(&host->lock, flags);
2956
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002957 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002958}
2959EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2960
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002961#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002962
Pierre Ossmand129bce2006-03-24 03:18:17 -08002963/*****************************************************************************\
2964 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002965 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002966 * *
2967\*****************************************************************************/
2968
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002969struct sdhci_host *sdhci_alloc_host(struct device *dev,
2970 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 struct mmc_host *mmc;
2973 struct sdhci_host *host;
2974
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002975 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002976
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002977 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002978 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002979 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002980
2981 host = mmc_priv(mmc);
2982 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002983 host->mmc_host_ops = sdhci_ops;
2984 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002985
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002986 host->flags = SDHCI_SIGNALING_330;
2987
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002988 return host;
2989}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002990
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002991EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002992
Alexandre Courbot7b913692016-03-07 11:07:55 +09002993static int sdhci_set_dma_mask(struct sdhci_host *host)
2994{
2995 struct mmc_host *mmc = host->mmc;
2996 struct device *dev = mmc_dev(mmc);
2997 int ret = -EINVAL;
2998
2999 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3000 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3001
3002 /* Try 64-bit mask if hardware is capable of it */
3003 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3004 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3005 if (ret) {
3006 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3007 mmc_hostname(mmc));
3008 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3009 }
3010 }
3011
3012 /* 32-bit mask as default & fallback */
3013 if (ret) {
3014 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3015 if (ret)
3016 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3017 mmc_hostname(mmc));
3018 }
3019
3020 return ret;
3021}
3022
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003023void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3024{
3025 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003026 u64 dt_caps_mask = 0;
3027 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003028
3029 if (host->read_caps)
3030 return;
3031
3032 host->read_caps = true;
3033
3034 if (debug_quirks)
3035 host->quirks = debug_quirks;
3036
3037 if (debug_quirks2)
3038 host->quirks2 = debug_quirks2;
3039
3040 sdhci_do_reset(host, SDHCI_RESET_ALL);
3041
Zach Brown92e0c442016-11-02 10:26:16 -05003042 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3043 "sdhci-caps-mask", &dt_caps_mask);
3044 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3045 "sdhci-caps", &dt_caps);
3046
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003047 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3048 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3049
3050 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3051 return;
3052
Zach Brown92e0c442016-11-02 10:26:16 -05003053 if (caps) {
3054 host->caps = *caps;
3055 } else {
3056 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3057 host->caps &= ~lower_32_bits(dt_caps_mask);
3058 host->caps |= lower_32_bits(dt_caps);
3059 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003060
3061 if (host->version < SDHCI_SPEC_300)
3062 return;
3063
Zach Brown92e0c442016-11-02 10:26:16 -05003064 if (caps1) {
3065 host->caps1 = *caps1;
3066 } else {
3067 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3068 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3069 host->caps1 |= upper_32_bits(dt_caps);
3070 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003071}
3072EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3073
Adrian Hunter52f53362016-06-29 16:24:15 +03003074int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003075{
3076 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303077 u32 max_current_caps;
3078 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003079 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003080 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003081 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003082
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003083 WARN_ON(host == NULL);
3084 if (host == NULL)
3085 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003086
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003087 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003088
Jon Hunterefba1422016-07-12 14:53:36 +01003089 /*
3090 * If there are external regulators, get them. Note this must be done
3091 * early before resetting the host and reading the capabilities so that
3092 * the host can take the appropriate action if regulators are not
3093 * available.
3094 */
3095 ret = mmc_regulator_get_supply(mmc);
3096 if (ret == -EPROBE_DEFER)
3097 return ret;
3098
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003099 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003100
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003101 override_timeout_clk = host->timeout_clk;
3102
Zhangfei Gao85105c52010-08-06 07:10:01 +08003103 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003104 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3105 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003106 }
3107
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003108 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003109 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003110 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003111 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003112 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003113 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003114
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003115 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003116 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003117 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003118 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003119 }
3120
Arindam Nathf2119df2011-05-05 12:18:57 +05303121 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003122 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003123 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003124
3125 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3126 (host->flags & SDHCI_USE_ADMA)) {
3127 DBG("Disabling ADMA as it is marked broken\n");
3128 host->flags &= ~SDHCI_USE_ADMA;
3129 }
3130
Adrian Huntere57a5f62014-11-04 12:42:46 +02003131 /*
3132 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3133 * and *must* do 64-bit DMA. A driver has the opportunity to change
3134 * that during the first call to ->enable_dma(). Similarly
3135 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3136 * implement.
3137 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003138 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003139 host->flags |= SDHCI_USE_64_BIT_DMA;
3140
Richard Röjforsa13abc72009-09-22 16:45:30 -07003141 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003142 ret = sdhci_set_dma_mask(host);
3143
3144 if (!ret && host->ops->enable_dma)
3145 ret = host->ops->enable_dma(host);
3146
3147 if (ret) {
3148 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3149 mmc_hostname(mmc));
3150 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3151
3152 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003153 }
3154 }
3155
Adrian Huntere57a5f62014-11-04 12:42:46 +02003156 /* SDMA does not support 64-bit DMA */
3157 if (host->flags & SDHCI_USE_64_BIT_DMA)
3158 host->flags &= ~SDHCI_USE_SDMA;
3159
Pierre Ossman2134a922008-06-28 18:28:51 +02003160 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003161 dma_addr_t dma;
3162 void *buf;
3163
Pierre Ossman2134a922008-06-28 18:28:51 +02003164 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003165 * The DMA descriptor table size is calculated as the maximum
3166 * number of segments times 2, to allow for an alignment
3167 * descriptor for each segment, plus 1 for a nop end descriptor,
3168 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003169 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003170 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3171 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3172 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003173 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003174 } else {
3175 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3176 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003177 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003178 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003179
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003180 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003181 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3182 host->adma_table_sz, &dma, GFP_KERNEL);
3183 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003184 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003185 mmc_hostname(mmc));
3186 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003187 } else if ((dma + host->align_buffer_sz) &
3188 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003189 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3190 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003191 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003192 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3193 host->adma_table_sz, buf, dma);
3194 } else {
3195 host->align_buffer = buf;
3196 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003197
Russell Kinge66e61c2016-01-26 13:39:55 +00003198 host->adma_table = buf + host->align_buffer_sz;
3199 host->adma_addr = dma + host->align_buffer_sz;
3200 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003201 }
3202
Pierre Ossman76591502008-07-21 00:32:11 +02003203 /*
3204 * If we use DMA, then it's up to the caller to set the DMA
3205 * mask, but PIO does not need the hw shim so we set a new
3206 * mask here in that case.
3207 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003208 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003209 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003210 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003211 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003212
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003213 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003214 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003215 >> SDHCI_CLOCK_BASE_SHIFT;
3216 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003217 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003218 >> SDHCI_CLOCK_BASE_SHIFT;
3219
Pierre Ossmand129bce2006-03-24 03:18:17 -08003220 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003221 if (host->max_clk == 0 || host->quirks &
3222 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003223 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003224 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3225 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003226 ret = -ENODEV;
3227 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003228 }
3229 host->max_clk = host->ops->get_max_clock(host);
3230 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003231
3232 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303233 * In case of Host Controller v3.00, find out whether clock
3234 * multiplier is supported.
3235 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003236 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303237 SDHCI_CLOCK_MUL_SHIFT;
3238
3239 /*
3240 * In case the value in Clock Multiplier is 0, then programmable
3241 * clock mode is not supported, otherwise the actual clock
3242 * multiplier is one more than the value of Clock Multiplier
3243 * in the Capabilities Register.
3244 */
3245 if (host->clk_mul)
3246 host->clk_mul += 1;
3247
3248 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003249 * Set host parameters.
3250 */
Dong Aisheng59241752015-07-22 20:53:07 +08003251 max_clk = host->max_clk;
3252
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003253 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003254 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303255 else if (host->version >= SDHCI_SPEC_300) {
3256 if (host->clk_mul) {
3257 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003258 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303259 } else
3260 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3261 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003262 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003263
Adrian Hunterd310ae42016-04-12 14:25:07 +03003264 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003265 mmc->f_max = max_clk;
3266
Aisheng Dong28aab052014-08-27 15:26:31 +08003267 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003268 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003269 SDHCI_TIMEOUT_CLK_SHIFT;
3270 if (host->timeout_clk == 0) {
3271 if (host->ops->get_timeout_clock) {
3272 host->timeout_clk =
3273 host->ops->get_timeout_clock(host);
3274 } else {
3275 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3276 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003277 ret = -ENODEV;
3278 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003279 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003280 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003281
Adrian Hunter28da3582016-06-29 16:24:17 +03003282 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003283 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003284
Adrian Hunter99513622016-03-07 13:33:55 +02003285 if (override_timeout_clk)
3286 host->timeout_clk = override_timeout_clk;
3287
Aisheng Dong28aab052014-08-27 15:26:31 +08003288 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003289 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003290 mmc->max_busy_timeout /= host->timeout_clk;
3291 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003292
Andrei Warkentine89d4562011-05-23 15:06:37 -05003293 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003294 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003295
3296 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3297 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003298
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003299 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003300 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003301 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003302 !(host->flags & SDHCI_USE_SDMA)) &&
3303 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003304 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003305 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003306 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003307 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003308 }
3309
Philip Rakity15ec4462010-11-19 16:48:39 -05003310 /*
3311 * A controller may support 8-bit width, but the board itself
3312 * might not have the pins brought out. Boards that support
3313 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3314 * their platform code before calling sdhci_add_host(), and we
3315 * won't assume 8-bit width for hosts without that CAP.
3316 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003317 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003318 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319
Jerry Huang63ef5d82012-10-25 13:47:19 +08003320 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3321 mmc->caps &= ~MMC_CAP_CMD23;
3322
Adrian Hunter28da3582016-06-29 16:24:17 +03003323 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003324 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003325
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003326 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003327 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003328 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003329 mmc->caps |= MMC_CAP_NEEDS_POLL;
3330
Philip Rakity6231f3d2012-07-23 15:56:23 -07003331 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003332 if (!IS_ERR(mmc->supply.vqmmc)) {
3333 ret = regulator_enable(mmc->supply.vqmmc);
3334 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3335 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003336 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3337 SDHCI_SUPPORT_SDR50 |
3338 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003339 if (ret) {
3340 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3341 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003342 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003343 }
Kevin Liu8363c372012-11-17 17:55:51 -05003344 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003345
Adrian Hunter28da3582016-06-29 16:24:17 +03003346 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3347 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3348 SDHCI_SUPPORT_DDR50);
3349 }
Daniel Drake6a661802012-11-25 13:01:19 -05003350
Al Cooper4188bba2012-03-16 15:54:17 -04003351 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003352 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3353 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303354 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3355
3356 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003357 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303358 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003359 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3360 * field can be promoted to support HS200.
3361 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003362 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003363 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003364 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303365 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003366 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303367
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003368 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003369 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003370 mmc->caps2 |= MMC_CAP2_HS400;
3371
Adrian Hunter549c0b12014-11-06 15:19:05 +02003372 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3373 (IS_ERR(mmc->supply.vqmmc) ||
3374 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3375 1300000)))
3376 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3377
Adrian Hunter28da3582016-06-29 16:24:17 +03003378 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3379 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303380 mmc->caps |= MMC_CAP_UHS_DDR50;
3381
Girish K S069c9f12012-01-06 09:56:39 +05303382 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003383 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303384 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3385
Arindam Nathd6d50a12011-05-05 12:18:59 +05303386 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003387 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303388 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003389 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303390 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003391 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303392 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3393
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303394 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003395 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3396 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303397
3398 /*
3399 * In case Re-tuning Timer is not disabled, the actual value of
3400 * re-tuning timer will be 2 ^ (n - 1).
3401 */
3402 if (host->tuning_count)
3403 host->tuning_count = 1 << (host->tuning_count - 1);
3404
3405 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003406 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303407 SDHCI_RETUNING_MODE_SHIFT;
3408
Takashi Iwai8f230f42010-12-08 10:04:30 +01003409 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003410
Arindam Nathf2119df2011-05-05 12:18:57 +05303411 /*
3412 * According to SD Host Controller spec v3.00, if the Host System
3413 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3414 * the value is meaningful only if Voltage Support in the Capabilities
3415 * register is set. The actual current value is 4 times the register
3416 * value.
3417 */
3418 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003419 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003420 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003421 if (curr > 0) {
3422
3423 /* convert to SDHCI_MAX_CURRENT format */
3424 curr = curr/1000; /* convert to mA */
3425 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3426
3427 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3428 max_current_caps =
3429 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3430 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3431 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3432 }
3433 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303434
Adrian Hunter28da3582016-06-29 16:24:17 +03003435 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003436 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303437
Aaron Lu55c46652012-07-04 13:31:48 +08003438 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303439 SDHCI_MAX_CURRENT_330_MASK) >>
3440 SDHCI_MAX_CURRENT_330_SHIFT) *
3441 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303442 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003443 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003444 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303445
Aaron Lu55c46652012-07-04 13:31:48 +08003446 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303447 SDHCI_MAX_CURRENT_300_MASK) >>
3448 SDHCI_MAX_CURRENT_300_SHIFT) *
3449 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303450 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003451 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003452 ocr_avail |= MMC_VDD_165_195;
3453
Aaron Lu55c46652012-07-04 13:31:48 +08003454 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303455 SDHCI_MAX_CURRENT_180_MASK) >>
3456 SDHCI_MAX_CURRENT_180_SHIFT) *
3457 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303458 }
3459
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003460 /* If OCR set by host, use it instead. */
3461 if (host->ocr_mask)
3462 ocr_avail = host->ocr_mask;
3463
3464 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003465 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003466 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003467
Takashi Iwai8f230f42010-12-08 10:04:30 +01003468 mmc->ocr_avail = ocr_avail;
3469 mmc->ocr_avail_sdio = ocr_avail;
3470 if (host->ocr_avail_sdio)
3471 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3472 mmc->ocr_avail_sd = ocr_avail;
3473 if (host->ocr_avail_sd)
3474 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3475 else /* normal SD controllers don't support 1.8V */
3476 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3477 mmc->ocr_avail_mmc = ocr_avail;
3478 if (host->ocr_avail_mmc)
3479 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003480
3481 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003482 pr_err("%s: Hardware doesn't report any support voltages.\n",
3483 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003484 ret = -ENODEV;
3485 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003486 }
3487
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003488 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3489 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3490 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3491 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3492 host->flags |= SDHCI_SIGNALING_180;
3493
3494 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3495 host->flags |= SDHCI_SIGNALING_120;
3496
Pierre Ossmand129bce2006-03-24 03:18:17 -08003497 spin_lock_init(&host->lock);
3498
3499 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003500 * Maximum number of segments. Depends on if the hardware
3501 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003502 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003503 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003504 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003505 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003506 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003507 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003508 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003509
3510 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003511 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3512 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3513 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003514 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003515 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003516
3517 /*
3518 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003519 * of bytes. When doing hardware scatter/gather, each entry cannot
3520 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003521 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003522 if (host->flags & SDHCI_USE_ADMA) {
3523 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3524 mmc->max_seg_size = 65535;
3525 else
3526 mmc->max_seg_size = 65536;
3527 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003528 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003529 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003530
3531 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003532 * Maximum block size. This varies from controller to controller and
3533 * is specified in the capabilities register.
3534 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003535 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3536 mmc->max_blk_size = 2;
3537 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003538 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003539 SDHCI_MAX_BLOCK_SHIFT;
3540 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003541 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3542 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003543 mmc->max_blk_size = 0;
3544 }
3545 }
3546
3547 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003548
3549 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003550 * Maximum block count.
3551 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003552 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003553
Adrian Hunter52f53362016-06-29 16:24:15 +03003554 return 0;
3555
3556unreg:
3557 if (!IS_ERR(mmc->supply.vqmmc))
3558 regulator_disable(mmc->supply.vqmmc);
3559undma:
3560 if (host->align_buffer)
3561 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3562 host->adma_table_sz, host->align_buffer,
3563 host->align_addr);
3564 host->adma_table = NULL;
3565 host->align_buffer = NULL;
3566
3567 return ret;
3568}
3569EXPORT_SYMBOL_GPL(sdhci_setup_host);
3570
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003571void sdhci_cleanup_host(struct sdhci_host *host)
3572{
3573 struct mmc_host *mmc = host->mmc;
3574
3575 if (!IS_ERR(mmc->supply.vqmmc))
3576 regulator_disable(mmc->supply.vqmmc);
3577
3578 if (host->align_buffer)
3579 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3580 host->adma_table_sz, host->align_buffer,
3581 host->align_addr);
3582 host->adma_table = NULL;
3583 host->align_buffer = NULL;
3584}
3585EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
3586
Adrian Hunter52f53362016-06-29 16:24:15 +03003587int __sdhci_add_host(struct sdhci_host *host)
3588{
3589 struct mmc_host *mmc = host->mmc;
3590 int ret;
3591
Pierre Ossman55db8902006-11-21 17:55:45 +01003592 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003593 * Init tasklets.
3594 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003595 tasklet_init(&host->finish_tasklet,
3596 sdhci_tasklet_finish, (unsigned long)host);
3597
Al Viroe4cad1b2006-10-10 22:47:07 +01003598 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003599 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3600 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003601
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003602 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303603
Shawn Guo2af502c2013-07-05 14:38:55 +08003604 sdhci_init(host, 0);
3605
Russell King781e9892014-04-25 12:55:46 +01003606 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3607 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003608 if (ret) {
3609 pr_err("%s: Failed to request IRQ %d: %d\n",
3610 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003611 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003612 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003613
Pierre Ossmand129bce2006-03-24 03:18:17 -08003614#ifdef CONFIG_MMC_DEBUG
3615 sdhci_dumpregs(host);
3616#endif
3617
Adrian Hunter061d17a2016-04-12 14:25:09 +03003618 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003619 if (ret) {
3620 pr_err("%s: Failed to register LED device: %d\n",
3621 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003622 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003623 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003624
Pierre Ossman5f25a662006-10-04 02:15:39 -07003625 mmiowb();
3626
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003627 ret = mmc_add_host(mmc);
3628 if (ret)
3629 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003630
Girish K Sa3c76eb2011-10-11 11:44:09 +05303631 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003632 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003633 (host->flags & SDHCI_USE_ADMA) ?
3634 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003635 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003636
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003637 sdhci_enable_card_detection(host);
3638
Pierre Ossmand129bce2006-03-24 03:18:17 -08003639 return 0;
3640
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003641unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003642 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003643unirq:
Russell King03231f92014-04-25 12:57:12 +01003644 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003645 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3646 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003647 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003648untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003649 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003650
Pierre Ossmand129bce2006-03-24 03:18:17 -08003651 return ret;
3652}
Adrian Hunter52f53362016-06-29 16:24:15 +03003653EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003654
Adrian Hunter52f53362016-06-29 16:24:15 +03003655int sdhci_add_host(struct sdhci_host *host)
3656{
3657 int ret;
3658
3659 ret = sdhci_setup_host(host);
3660 if (ret)
3661 return ret;
3662
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003663 ret = __sdhci_add_host(host);
3664 if (ret)
3665 goto cleanup;
3666
3667 return 0;
3668
3669cleanup:
3670 sdhci_cleanup_host(host);
3671
3672 return ret;
Adrian Hunter52f53362016-06-29 16:24:15 +03003673}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003674EXPORT_SYMBOL_GPL(sdhci_add_host);
3675
Pierre Ossman1e728592008-04-16 19:13:13 +02003676void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003677{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003678 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003679 unsigned long flags;
3680
3681 if (dead) {
3682 spin_lock_irqsave(&host->lock, flags);
3683
3684 host->flags |= SDHCI_DEVICE_DEAD;
3685
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003686 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303687 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003688 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003689 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003690 }
3691
3692 spin_unlock_irqrestore(&host->lock, flags);
3693 }
3694
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003695 sdhci_disable_card_detection(host);
3696
Markus Mayer4e743f12014-07-03 13:27:42 -07003697 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003698
Adrian Hunter061d17a2016-04-12 14:25:09 +03003699 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003700
Pierre Ossman1e728592008-04-16 19:13:13 +02003701 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003702 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003703
Russell Kingb537f942014-04-25 12:56:01 +01003704 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3705 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003706 free_irq(host->irq, host);
3707
3708 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003709 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003710
Pierre Ossmand129bce2006-03-24 03:18:17 -08003711 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003712
Tim Kryger3a48edc2014-06-13 10:13:56 -07003713 if (!IS_ERR(mmc->supply.vqmmc))
3714 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003715
Russell Kingedd63fc2016-01-26 13:39:50 +00003716 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003717 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3718 host->adma_table_sz, host->align_buffer,
3719 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003720
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003721 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003722 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003723}
3724
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003725EXPORT_SYMBOL_GPL(sdhci_remove_host);
3726
3727void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003728{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003729 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003730}
3731
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003732EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003733
3734/*****************************************************************************\
3735 * *
3736 * Driver init/exit *
3737 * *
3738\*****************************************************************************/
3739
3740static int __init sdhci_drv_init(void)
3741{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303742 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003743 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303744 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003745
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003746 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003747}
3748
3749static void __exit sdhci_drv_exit(void)
3750{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003751}
3752
3753module_init(sdhci_drv_init);
3754module_exit(sdhci_drv_exit);
3755
Pierre Ossmandf673b22006-06-30 02:22:31 -07003756module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003757module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003758
Pierre Ossman32710e82009-04-08 20:14:54 +02003759MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003760MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003761MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003762
Pierre Ossmandf673b22006-06-30 02:22:31 -07003763MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003764MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");