blob: 147df8bcc231e70f8bf67f1f4f002a18d91e2bc0 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
Adrian Hunter5a436cc2017-03-20 19:50:31 +020017#include <linux/ktime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080018#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010019#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020023#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070024#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030025#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050026#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossman2f730fe2008-03-17 10:29:38 +010028#include <linux/leds.h>
29
Aries Lee22113ef2010-12-15 08:14:24 +010030#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080032#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080033#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080034#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmand129bce2006-03-24 03:18:17 -080036#include "sdhci.h"
37
38#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040#define DBG(f, x...) \
Adrian Hunterf4218652017-03-20 19:50:39 +020041 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080042
Adrian Hunter85ad90e2017-03-20 19:50:42 +020043#define SDHCI_DUMP(f, x...) \
44 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Kevin Liu52983382013-01-31 11:31:37 +080053static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
Adrian Hunterd2898172017-03-20 19:50:43 +020055void sdhci_dumpregs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -080056{
Adrian Hunter85ad90e2017-03-20 19:50:42 +020057 SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Adrian Hunter85ad90e2017-03-20 19:50:42 +020059 SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
60 sdhci_readl(host, SDHCI_DMA_ADDRESS),
61 sdhci_readw(host, SDHCI_HOST_VERSION));
62 SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
63 sdhci_readw(host, SDHCI_BLOCK_SIZE),
64 sdhci_readw(host, SDHCI_BLOCK_COUNT));
65 SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
66 sdhci_readl(host, SDHCI_ARGUMENT),
67 sdhci_readw(host, SDHCI_TRANSFER_MODE));
68 SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
69 sdhci_readl(host, SDHCI_PRESENT_STATE),
70 sdhci_readb(host, SDHCI_HOST_CONTROL));
71 SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
72 sdhci_readb(host, SDHCI_POWER_CONTROL),
73 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
74 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
75 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
76 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
77 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
78 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
79 sdhci_readl(host, SDHCI_INT_STATUS));
80 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
81 sdhci_readl(host, SDHCI_INT_ENABLE),
82 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
83 SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
84 sdhci_readw(host, SDHCI_ACMD12_ERR),
85 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
86 SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
87 sdhci_readl(host, SDHCI_CAPABILITIES),
88 sdhci_readl(host, SDHCI_CAPABILITIES_1));
89 SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
90 sdhci_readw(host, SDHCI_COMMAND),
91 sdhci_readl(host, SDHCI_MAX_CURRENT));
92 SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020093 sdhci_readl(host, SDHCI_RESPONSE),
94 sdhci_readl(host, SDHCI_RESPONSE + 4));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020095 SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020096 sdhci_readl(host, SDHCI_RESPONSE + 8),
97 sdhci_readl(host, SDHCI_RESPONSE + 12));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020098 SDHCI_DUMP("Host ctl2: 0x%08x\n",
99 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800100
Adrian Huntere57a5f62014-11-04 12:42:46 +0200101 if (host->flags & SDHCI_USE_ADMA) {
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200102 if (host->flags & SDHCI_USE_64_BIT_DMA) {
103 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
104 sdhci_readl(host, SDHCI_ADMA_ERROR),
105 sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
106 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
107 } else {
108 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
109 sdhci_readl(host, SDHCI_ADMA_ERROR),
110 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
111 }
Adrian Huntere57a5f62014-11-04 12:42:46 +0200112 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100113
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200114 SDHCI_DUMP("============================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800115}
Adrian Hunterd2898172017-03-20 19:50:43 +0200116EXPORT_SYMBOL_GPL(sdhci_dumpregs);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800117
118/*****************************************************************************\
119 * *
120 * Low level functions *
121 * *
122\*****************************************************************************/
123
Adrian Hunter56a590d2016-06-29 16:24:32 +0300124static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
125{
126 return cmd->data || cmd->flags & MMC_RSP_BUSY;
127}
128
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300129static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
130{
Russell King5b4f1f62014-04-25 12:57:02 +0100131 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300132
Adrian Hunterc79396c2011-12-27 15:48:42 +0200133 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900134 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300135 return;
136
Russell King5b4f1f62014-04-25 12:57:02 +0100137 if (enable) {
138 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
139 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800140
Russell King5b4f1f62014-04-25 12:57:02 +0100141 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
142 SDHCI_INT_CARD_INSERT;
143 } else {
144 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
145 }
Russell Kingb537f942014-04-25 12:56:01 +0100146
147 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
148 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300149}
150
151static void sdhci_enable_card_detection(struct sdhci_host *host)
152{
153 sdhci_set_card_detection(host, true);
154}
155
156static void sdhci_disable_card_detection(struct sdhci_host *host)
157{
158 sdhci_set_card_detection(host, false);
159}
160
Ulf Hansson02d0b682016-04-11 15:32:41 +0200161static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
162{
163 if (host->bus_on)
164 return;
165 host->bus_on = true;
166 pm_runtime_get_noresume(host->mmc->parent);
167}
168
169static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
170{
171 if (!host->bus_on)
172 return;
173 host->bus_on = false;
174 pm_runtime_put_noidle(host->mmc->parent);
175}
176
Russell King03231f92014-04-25 12:57:12 +0100177void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200179 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800180
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300181 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800182
Adrian Hunterf0710a52013-05-06 12:17:32 +0300183 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300185 /* Reset-all turns off SD Bus Power */
186 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
187 sdhci_runtime_pm_bus_off(host);
188 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189
Pierre Ossmane16514d82006-06-30 02:22:24 -0700190 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200191 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192
193 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300194 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200195 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530196 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700197 mmc_hostname(host->mmc), (int)mask);
198 sdhci_dumpregs(host);
199 return;
200 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200201 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800202 }
Russell King03231f92014-04-25 12:57:12 +0100203}
204EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300205
Russell King03231f92014-04-25 12:57:12 +0100206static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
207{
208 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300209 struct mmc_host *mmc = host->mmc;
210
211 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100212 return;
213 }
214
215 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800216
Russell Kingda91a8f2014-04-25 13:00:12 +0100217 if (mask & SDHCI_RESET_ALL) {
218 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
219 if (host->ops->enable_dma)
220 host->ops->enable_dma(host);
221 }
222
223 /* Resetting the controller clears many */
224 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800225 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226}
227
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200228static void sdhci_set_default_irqs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800229{
Russell Kingb537f942014-04-25 12:56:01 +0100230 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
231 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
232 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
233 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
234 SDHCI_INT_RESPONSE;
235
Dong Aishengf37b20e2016-07-12 15:46:17 +0800236 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
237 host->tuning_mode == SDHCI_TUNING_MODE_3)
238 host->ier |= SDHCI_INT_RETUNE;
239
Russell Kingb537f942014-04-25 12:56:01 +0100240 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
241 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200242}
243
244static void sdhci_init(struct sdhci_host *host, int soft)
245{
246 struct mmc_host *mmc = host->mmc;
247
248 if (soft)
249 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
250 else
251 sdhci_do_reset(host, SDHCI_RESET_ALL);
252
253 sdhci_set_default_irqs(host);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800254
Adrian Hunterf12e39d2017-03-20 19:50:47 +0200255 host->cqe_on = false;
256
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800257 if (soft) {
258 /* force clock reconfiguration */
259 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300260 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800261 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300262}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300264static void sdhci_reinit(struct sdhci_host *host)
265{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800266 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300267 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800268}
269
Adrian Hunter061d17a2016-04-12 14:25:09 +0300270static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800271{
272 u8 ctrl;
273
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300274 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800275 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277}
278
Adrian Hunter061d17a2016-04-12 14:25:09 +0300279static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800280{
281 u8 ctrl;
282
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300283 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800284 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300285 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286}
287
Masahiro Yamada4f782302016-04-14 13:19:39 +0900288#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100289static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300290 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100291{
292 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
293 unsigned long flags;
294
295 spin_lock_irqsave(&host->lock, flags);
296
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300297 if (host->runtime_suspended)
298 goto out;
299
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100300 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300301 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100302 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300303 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300304out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100305 spin_unlock_irqrestore(&host->lock, flags);
306}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300307
308static int sdhci_led_register(struct sdhci_host *host)
309{
310 struct mmc_host *mmc = host->mmc;
311
312 snprintf(host->led_name, sizeof(host->led_name),
313 "%s::", mmc_hostname(mmc));
314
315 host->led.name = host->led_name;
316 host->led.brightness = LED_OFF;
317 host->led.default_trigger = mmc_hostname(mmc);
318 host->led.brightness_set = sdhci_led_control;
319
320 return led_classdev_register(mmc_dev(mmc), &host->led);
321}
322
323static void sdhci_led_unregister(struct sdhci_host *host)
324{
325 led_classdev_unregister(&host->led);
326}
327
328static inline void sdhci_led_activate(struct sdhci_host *host)
329{
330}
331
332static inline void sdhci_led_deactivate(struct sdhci_host *host)
333{
334}
335
336#else
337
338static inline int sdhci_led_register(struct sdhci_host *host)
339{
340 return 0;
341}
342
343static inline void sdhci_led_unregister(struct sdhci_host *host)
344{
345}
346
347static inline void sdhci_led_activate(struct sdhci_host *host)
348{
349 __sdhci_led_activate(host);
350}
351
352static inline void sdhci_led_deactivate(struct sdhci_host *host)
353{
354 __sdhci_led_deactivate(host);
355}
356
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100357#endif
358
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359/*****************************************************************************\
360 * *
361 * Core functions *
362 * *
363\*****************************************************************************/
364
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100365static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366{
Pierre Ossman76591502008-07-21 00:32:11 +0200367 unsigned long flags;
368 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700369 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200370 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800371
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100372 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800373
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200375 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800376
Pierre Ossman76591502008-07-21 00:32:11 +0200377 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800378
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100379 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300380 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800381
Pierre Ossman76591502008-07-21 00:32:11 +0200382 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 blksize -= len;
385 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200386
Pierre Ossman76591502008-07-21 00:32:11 +0200387 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800388
Pierre Ossman76591502008-07-21 00:32:11 +0200389 while (len) {
390 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300391 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200392 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800393 }
Pierre Ossman76591502008-07-21 00:32:11 +0200394
395 *buf = scratch & 0xFF;
396
397 buf++;
398 scratch >>= 8;
399 chunk--;
400 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800401 }
402 }
Pierre Ossman76591502008-07-21 00:32:11 +0200403
404 sg_miter_stop(&host->sg_miter);
405
406 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800408
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409static void sdhci_write_block_pio(struct sdhci_host *host)
410{
Pierre Ossman76591502008-07-21 00:32:11 +0200411 unsigned long flags;
412 size_t blksize, len, chunk;
413 u32 scratch;
414 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100415
416 DBG("PIO writing\n");
417
418 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200419 chunk = 0;
420 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421
Pierre Ossman76591502008-07-21 00:32:11 +0200422 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423
424 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300425 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426
Pierre Ossman76591502008-07-21 00:32:11 +0200427 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200428
Pierre Ossman76591502008-07-21 00:32:11 +0200429 blksize -= len;
430 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100431
Pierre Ossman76591502008-07-21 00:32:11 +0200432 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 while (len) {
435 scratch |= (u32)*buf << (chunk * 8);
436
437 buf++;
438 chunk++;
439 len--;
440
441 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300442 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200443 chunk = 0;
444 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100445 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100446 }
447 }
Pierre Ossman76591502008-07-21 00:32:11 +0200448
449 sg_miter_stop(&host->sg_miter);
450
451 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100452}
453
454static void sdhci_transfer_pio(struct sdhci_host *host)
455{
456 u32 mask;
457
Pierre Ossman76591502008-07-21 00:32:11 +0200458 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100459 return;
460
461 if (host->data->flags & MMC_DATA_READ)
462 mask = SDHCI_DATA_AVAILABLE;
463 else
464 mask = SDHCI_SPACE_AVAILABLE;
465
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200466 /*
467 * Some controllers (JMicron JMB38x) mess up the buffer bits
468 * for transfers < 4 bytes. As long as it is just one block,
469 * we can ignore the bits.
470 */
471 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
472 (host->data->blocks == 1))
473 mask = ~0;
474
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300475 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300476 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
477 udelay(100);
478
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100479 if (host->data->flags & MMC_DATA_READ)
480 sdhci_read_block_pio(host);
481 else
482 sdhci_write_block_pio(host);
483
Pierre Ossman76591502008-07-21 00:32:11 +0200484 host->blocks--;
485 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100486 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100487 }
488
489 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800490}
491
Russell King48857d92016-01-26 13:40:16 +0000492static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000493 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000494{
495 int sg_count;
496
Russell King94538e52016-01-26 13:40:37 +0000497 /*
498 * If the data buffers are already mapped, return the previous
499 * dma_map_sg() result.
500 */
501 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000502 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000503
504 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
505 data->flags & MMC_DATA_WRITE ?
506 DMA_TO_DEVICE : DMA_FROM_DEVICE);
507
508 if (sg_count == 0)
509 return -ENOSPC;
510
511 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000512 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000513
514 return sg_count;
515}
516
Pierre Ossman2134a922008-06-28 18:28:51 +0200517static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
518{
519 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800520 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200521}
522
523static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
524{
Cong Wang482fce92011-11-27 13:27:00 +0800525 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200526 local_irq_restore(*flags);
527}
528
Adrian Huntere57a5f62014-11-04 12:42:46 +0200529static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
530 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800531{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200532 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800533
Adrian Huntere57a5f62014-11-04 12:42:46 +0200534 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200535 dma_desc->cmd = cpu_to_le16(cmd);
536 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200537 dma_desc->addr_lo = cpu_to_le32((u32)addr);
538
539 if (host->flags & SDHCI_USE_64_BIT_DMA)
540 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800541}
542
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200543static void sdhci_adma_mark_end(void *desc)
544{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200545 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200546
Adrian Huntere57a5f62014-11-04 12:42:46 +0200547 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200548 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200549}
550
Russell King60c64762016-01-26 13:40:22 +0000551static void sdhci_adma_table_pre(struct sdhci_host *host,
552 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200553{
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000556 dma_addr_t addr, align_addr;
557 void *desc, *align;
558 char *buffer;
559 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200560
561 /*
562 * The spec does not specify endianness of descriptor table.
563 * We currently guess that it is LE.
564 */
565
Russell King60c64762016-01-26 13:40:22 +0000566 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200567
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200568 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200569 align = host->align_buffer;
570
571 align_addr = host->align_addr;
572
573 for_each_sg(data->sg, sg, host->sg_count, i) {
574 addr = sg_dma_address(sg);
575 len = sg_dma_len(sg);
576
577 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000578 * The SDHCI specification states that ADMA addresses must
579 * be 32-bit aligned. If they aren't, then we use a bounce
580 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200581 * alignment.
582 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200583 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
584 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 if (offset) {
586 if (data->flags & MMC_DATA_WRITE) {
587 buffer = sdhci_kmap_atomic(sg, &flags);
588 memcpy(align, buffer, offset);
589 sdhci_kunmap_atomic(buffer, &flags);
590 }
591
Ben Dooks118cd172010-03-05 13:43:26 -0800592 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200593 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200594 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200595
596 BUG_ON(offset > 65536);
597
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200598 align += SDHCI_ADMA2_ALIGN;
599 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200600
Adrian Hunter76fe3792014-11-04 12:42:42 +0200601 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602
603 addr += offset;
604 len -= offset;
605 }
606
Pierre Ossman2134a922008-06-28 18:28:51 +0200607 BUG_ON(len > 65536);
608
Adrian Hunter347ea322015-11-26 14:00:48 +0200609 if (len) {
610 /* tran, valid */
611 sdhci_adma_write_desc(host, desc, addr, len,
612 ADMA2_TRAN_VALID);
613 desc += host->desc_sz;
614 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
616 /*
617 * If this triggers then we have a calculation bug
618 * somewhere. :/
619 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200620 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 }
622
Thomas Abraham70764a92010-05-26 14:42:04 -0700623 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000624 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200625 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200626 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200627 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700628 }
629 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000630 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200631 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700632 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200633}
634
635static void sdhci_adma_table_post(struct sdhci_host *host,
636 struct mmc_data *data)
637{
Pierre Ossman2134a922008-06-28 18:28:51 +0200638 struct scatterlist *sg;
639 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200640 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200641 char *buffer;
642 unsigned long flags;
643
Russell King47fa9612016-01-26 13:40:06 +0000644 if (data->flags & MMC_DATA_READ) {
645 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100646
Russell King47fa9612016-01-26 13:40:06 +0000647 /* Do a quick scan of the SG list for any unaligned mappings */
648 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200649 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000650 has_unaligned = true;
651 break;
652 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200653
Russell King47fa9612016-01-26 13:40:06 +0000654 if (has_unaligned) {
655 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000656 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200657
Russell King47fa9612016-01-26 13:40:06 +0000658 align = host->align_buffer;
659
660 for_each_sg(data->sg, sg, host->sg_count, i) {
661 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
662 size = SDHCI_ADMA2_ALIGN -
663 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
664
665 buffer = sdhci_kmap_atomic(sg, &flags);
666 memcpy(buffer, align, size);
667 sdhci_kunmap_atomic(buffer, &flags);
668
669 align += SDHCI_ADMA2_ALIGN;
670 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200671 }
672 }
673 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200674}
675
Andrei Warkentina3c77782011-04-11 16:13:42 -0500676static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800677{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700678 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500679 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700680 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800681
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200682 /*
683 * If the host controller provides us with an incorrect timeout
684 * value, just skip the check and use 0xE. The hardware may take
685 * longer to time out, but that's much better than having a too-short
686 * timeout value.
687 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200688 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200689 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200690
Andrei Warkentina3c77782011-04-11 16:13:42 -0500691 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100692 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500693 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800694
Andrei Warkentina3c77782011-04-11 16:13:42 -0500695 /* timeout in us */
696 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100697 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300698 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000699 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000700 if (host->clock && data->timeout_clks) {
701 unsigned long long val;
702
703 /*
704 * data->timeout_clks is in units of clock cycles.
705 * host->clock is in Hz. target_timeout is in us.
706 * Hence, us = 1000000 * cycles / Hz. Round up.
707 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200708 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000709 if (do_div(val, host->clock))
710 target_timeout++;
711 target_timeout += val;
712 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300713 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700714
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700715 /*
716 * Figure out needed cycles.
717 * We do this in steps in order to fit inside a 32 bit int.
718 * The first step is the minimum timeout, which will have a
719 * minimum resolution of 6 bits:
720 * (1) 2^13*1000 > 2^22,
721 * (2) host->timeout_clk < 2^16
722 * =>
723 * (1) / (2) > 2^6
724 */
725 count = 0;
726 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
727 while (current_timeout < target_timeout) {
728 count++;
729 current_timeout <<= 1;
730 if (count >= 0xF)
731 break;
732 }
733
734 if (count >= 0xF) {
Adrian Hunterf4218652017-03-20 19:50:39 +0200735 DBG("Too large timeout 0x%x requested for CMD%d!\n",
736 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700737 count = 0xE;
738 }
739
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200740 return count;
741}
742
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300743static void sdhci_set_transfer_irqs(struct sdhci_host *host)
744{
745 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
746 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
747
748 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100749 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300750 else
Russell Kingb537f942014-04-25 12:56:01 +0100751 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
752
753 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
754 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300755}
756
Aisheng Dongb45e6682014-08-27 15:26:29 +0800757static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200758{
759 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800760
761 if (host->ops->set_timeout) {
762 host->ops->set_timeout(host, cmd);
763 } else {
764 count = sdhci_calc_timeout(host, cmd);
765 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
766 }
767}
768
769static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
770{
Pierre Ossman2134a922008-06-28 18:28:51 +0200771 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500772 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200773
Adrian Hunter56a590d2016-06-29 16:24:32 +0300774 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800775 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500776
777 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200778 return;
779
Adrian Hunter43dea092016-06-29 16:24:26 +0300780 WARN_ON(host->data);
781
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200782 /* Sanity checks */
783 BUG_ON(data->blksz * data->blocks > 524288);
784 BUG_ON(data->blksz > host->mmc->max_blk_size);
785 BUG_ON(data->blocks > 65535);
786
787 host->data = data;
788 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400789 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200790
Russell Kingfce14422016-01-26 13:41:20 +0000791 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200792 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000793 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000794 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200795
Russell Kingfce14422016-01-26 13:41:20 +0000796 host->flags |= SDHCI_REQ_USE_DMA;
797
798 /*
799 * FIXME: This doesn't account for merging when mapping the
800 * scatterlist.
801 *
802 * The assumption here being that alignment and lengths are
803 * the same after DMA mapping to device address space.
804 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000805 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000806 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000808 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000809 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000810 /*
811 * As we use up to 3 byte chunks to work
812 * around alignment problems, we need to
813 * check the offset as well.
814 */
815 offset_mask = 3;
816 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200817 } else {
818 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000819 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000820 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
821 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200822 }
823
Russell Kingdf953922016-01-26 13:41:14 +0000824 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200825 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000826 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100827 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000828 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 host->flags &= ~SDHCI_REQ_USE_DMA;
830 break;
831 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000832 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100833 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200834 host->flags &= ~SDHCI_REQ_USE_DMA;
835 break;
836 }
837 }
838 }
839 }
840
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000842 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200843
Russell King60c64762016-01-26 13:40:22 +0000844 if (sg_cnt <= 0) {
845 /*
846 * This only happens when someone fed
847 * us an invalid request.
848 */
849 WARN_ON(1);
850 host->flags &= ~SDHCI_REQ_USE_DMA;
851 } else if (host->flags & SDHCI_USE_ADMA) {
852 sdhci_adma_table_pre(host, data, sg_cnt);
853
854 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
855 if (host->flags & SDHCI_USE_64_BIT_DMA)
856 sdhci_writel(host,
857 (u64)host->adma_addr >> 32,
858 SDHCI_ADMA_ADDRESS_HI);
859 } else {
860 WARN_ON(sg_cnt != 1);
861 sdhci_writel(host, sg_dma_address(data->sg),
862 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200863 }
864 }
865
Pierre Ossman2134a922008-06-28 18:28:51 +0200866 /*
867 * Always adjust the DMA selection as some controllers
868 * (e.g. JMicron) can't do PIO properly when the selection
869 * is ADMA.
870 */
871 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300872 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200873 ctrl &= ~SDHCI_CTRL_DMA_MASK;
874 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200875 (host->flags & SDHCI_USE_ADMA)) {
876 if (host->flags & SDHCI_USE_64_BIT_DMA)
877 ctrl |= SDHCI_CTRL_ADMA64;
878 else
879 ctrl |= SDHCI_CTRL_ADMA32;
880 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200881 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200882 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300883 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100884 }
885
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200886 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200887 int flags;
888
889 flags = SG_MITER_ATOMIC;
890 if (host->data->flags & MMC_DATA_READ)
891 flags |= SG_MITER_TO_SG;
892 else
893 flags |= SG_MITER_FROM_SG;
894 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200895 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300898 sdhci_set_transfer_irqs(host);
899
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400900 /* Set the DMA boundary value and block size */
901 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
902 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300903 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700904}
905
Adrian Hunter0293d502016-06-29 16:24:35 +0300906static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
907 struct mmc_request *mrq)
908{
Adrian Hunter20845be2016-08-16 13:44:13 +0300909 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
910 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300911}
912
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700913static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500914 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700915{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800916 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500917 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700918
Dong Aisheng2b558c12013-10-30 22:09:48 +0800919 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800920 if (host->quirks2 &
921 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
922 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
923 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800924 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800925 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
926 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800927 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800928 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700929 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800930 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700931
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200932 WARN_ON(!host->data);
933
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800934 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
935 mode = SDHCI_TRNS_BLK_CNT_EN;
936
Andrei Warkentine89d4562011-05-23 15:06:37 -0500937 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800938 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500939 /*
940 * If we are sending CMD23, CMD12 never gets sent
941 * on successful completion (so no Auto-CMD12).
942 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300943 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800944 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500945 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300946 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500947 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300948 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500949 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700950 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500951
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700952 if (data->flags & MMC_DATA_READ)
953 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100954 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700955 mode |= SDHCI_TRNS_DMA;
956
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300957 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958}
959
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300960static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
961{
962 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
963 ((mrq->cmd && mrq->cmd->error) ||
964 (mrq->sbc && mrq->sbc->error) ||
965 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
966 (mrq->data->stop && mrq->data->stop->error))) ||
967 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
968}
969
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300970static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
971{
972 int i;
973
974 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
975 if (host->mrqs_done[i] == mrq) {
976 WARN_ON(1);
977 return;
978 }
979 }
980
981 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
982 if (!host->mrqs_done[i]) {
983 host->mrqs_done[i] = mrq;
984 break;
985 }
986 }
987
988 WARN_ON(i >= SDHCI_MAX_MRQS);
989
990 tasklet_schedule(&host->finish_tasklet);
991}
992
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300993static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
994{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300995 if (host->cmd && host->cmd->mrq == mrq)
996 host->cmd = NULL;
997
998 if (host->data_cmd && host->data_cmd->mrq == mrq)
999 host->data_cmd = NULL;
1000
1001 if (host->data && host->data->mrq == mrq)
1002 host->data = NULL;
1003
Adrian Huntered1563d2016-06-29 16:24:29 +03001004 if (sdhci_needs_reset(host, mrq))
1005 host->pending_reset = true;
1006
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001007 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001008}
1009
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010static void sdhci_finish_data(struct sdhci_host *host)
1011{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001012 struct mmc_command *data_cmd = host->data_cmd;
1013 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001016 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017
Russell Kingadd89132016-01-26 13:40:42 +00001018 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1019 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1020 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021
1022 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001023 * The specification states that the block count register must
1024 * be updated, but it does not specify at what point in the
1025 * data flow. That makes the register entirely useless to read
1026 * back so we have to assume that nothing made it to the card
1027 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001029 if (data->error)
1030 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001032 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033
Andrei Warkentine89d4562011-05-23 15:06:37 -05001034 /*
1035 * Need to send CMD12 if -
1036 * a) open-ended multiblock transfer (no CMD23)
1037 * b) error in multiblock transfer
1038 */
1039 if (data->stop &&
1040 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001041 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001042
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043 /*
1044 * The controller needs a reset of internal state machines
1045 * upon error conditions.
1046 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001047 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001048 if (!host->cmd || host->cmd == data_cmd)
1049 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001050 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 }
1052
Adrian Hunter20845be2016-08-16 13:44:13 +03001053 /*
1054 * 'cap_cmd_during_tfr' request must not use the command line
1055 * after mmc_command_done() has been called. It is upper layer's
1056 * responsibility to send the stop command if required.
1057 */
1058 if (data->mrq->cap_cmd_during_tfr) {
1059 sdhci_finish_mrq(host, data->mrq);
1060 } else {
1061 /* Avoid triggering warning in sdhci_send_command() */
1062 host->cmd = NULL;
1063 sdhci_send_command(host, data->stop);
1064 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001065 } else {
1066 sdhci_finish_mrq(host, data->mrq);
1067 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068}
1069
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001070static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1071 unsigned long timeout)
1072{
1073 if (sdhci_data_line_cmd(mrq->cmd))
1074 mod_timer(&host->data_timer, timeout);
1075 else
1076 mod_timer(&host->timer, timeout);
1077}
1078
1079static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1080{
1081 if (sdhci_data_line_cmd(mrq->cmd))
1082 del_timer(&host->data_timer);
1083 else
1084 del_timer(&host->timer);
1085}
1086
Dong Aishengc0e551292013-09-13 19:11:31 +08001087void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001088{
1089 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001090 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001091 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092
1093 WARN_ON(host->cmd);
1094
Russell King96776202016-01-26 13:39:34 +00001095 /* Initially, a command has no error */
1096 cmd->error = 0;
1097
Adrian Hunterfc605f12016-10-05 12:11:21 +03001098 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1099 cmd->opcode == MMC_STOP_TRANSMISSION)
1100 cmd->flags |= MMC_RSP_BUSY;
1101
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001103 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001104
1105 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001106 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001107 mask |= SDHCI_DATA_INHIBIT;
1108
1109 /* We shouldn't wait for data inihibit for stop commands, even
1110 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001111 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001112 mask &= ~SDHCI_DATA_INHIBIT;
1113
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001114 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001115 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001116 pr_err("%s: Controller never released inhibit bit(s).\n",
1117 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001119 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001120 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121 return;
1122 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001123 timeout--;
1124 mdelay(1);
1125 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001127 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001128 if (!cmd->data && cmd->busy_timeout > 9000)
1129 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001130 else
1131 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001132 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
1134 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001135 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001136 WARN_ON(host->data_cmd);
1137 host->data_cmd = cmd;
1138 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139
Andrei Warkentina3c77782011-04-11 16:13:42 -05001140 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001142 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143
Andrei Warkentine89d4562011-05-23 15:06:37 -05001144 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001145
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301147 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001149 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001150 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 return;
1152 }
1153
1154 if (!(cmd->flags & MMC_RSP_PRESENT))
1155 flags = SDHCI_CMD_RESP_NONE;
1156 else if (cmd->flags & MMC_RSP_136)
1157 flags = SDHCI_CMD_RESP_LONG;
1158 else if (cmd->flags & MMC_RSP_BUSY)
1159 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1160 else
1161 flags = SDHCI_CMD_RESP_SHORT;
1162
1163 if (cmd->flags & MMC_RSP_CRC)
1164 flags |= SDHCI_CMD_CRC;
1165 if (cmd->flags & MMC_RSP_OPCODE)
1166 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301167
1168 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301169 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1170 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171 flags |= SDHCI_CMD_DATA;
1172
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001173 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001174}
Dong Aishengc0e551292013-09-13 19:11:31 +08001175EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176
1177static void sdhci_finish_command(struct sdhci_host *host)
1178{
Adrian Huntere0a56402016-06-29 16:24:22 +03001179 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180 int i;
1181
Adrian Huntere0a56402016-06-29 16:24:22 +03001182 host->cmd = NULL;
1183
1184 if (cmd->flags & MMC_RSP_PRESENT) {
1185 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001186 /* CRC is stripped so we need to do some shifting. */
1187 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001188 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189 SDHCI_RESPONSE + (3-i)*4) << 8;
1190 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001191 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001192 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001193 SDHCI_RESPONSE + (3-i)*4-1);
1194 }
1195 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001196 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197 }
1198 }
1199
Adrian Hunter20845be2016-08-16 13:44:13 +03001200 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1201 mmc_command_done(host->mmc, cmd->mrq);
1202
Adrian Hunter6bde8682016-06-29 16:24:20 +03001203 /*
1204 * The host can send and interrupt when the busy state has
1205 * ended, allowing us to wait without wasting CPU cycles.
1206 * The busy signal uses DAT0 so this is similar to waiting
1207 * for data to complete.
1208 *
1209 * Note: The 1.0 specification is a bit ambiguous about this
1210 * feature so there might be some problems with older
1211 * controllers.
1212 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001213 if (cmd->flags & MMC_RSP_BUSY) {
1214 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001215 DBG("Cannot wait for busy signal when also doing a data transfer");
1216 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001217 cmd == host->data_cmd) {
1218 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001219 return;
1220 }
1221 }
1222
Andrei Warkentine89d4562011-05-23 15:06:37 -05001223 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001224 if (cmd == cmd->mrq->sbc) {
1225 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001226 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001227
Andrei Warkentine89d4562011-05-23 15:06:37 -05001228 /* Processed actual command. */
1229 if (host->data && host->data_early)
1230 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001231
Adrian Huntere0a56402016-06-29 16:24:22 +03001232 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001233 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001234 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001235}
1236
Kevin Liu52983382013-01-31 11:31:37 +08001237static u16 sdhci_get_preset_value(struct sdhci_host *host)
1238{
Russell Kingd975f122014-04-25 12:59:31 +01001239 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001240
Russell Kingd975f122014-04-25 12:59:31 +01001241 switch (host->timing) {
1242 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001243 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1244 break;
Russell Kingd975f122014-04-25 12:59:31 +01001245 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001246 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1247 break;
Russell Kingd975f122014-04-25 12:59:31 +01001248 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001249 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1250 break;
Russell Kingd975f122014-04-25 12:59:31 +01001251 case MMC_TIMING_UHS_SDR104:
1252 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001253 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1254 break;
Russell Kingd975f122014-04-25 12:59:31 +01001255 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001256 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001257 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1258 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001259 case MMC_TIMING_MMC_HS400:
1260 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1261 break;
Kevin Liu52983382013-01-31 11:31:37 +08001262 default:
1263 pr_warn("%s: Invalid UHS-I mode selected\n",
1264 mmc_hostname(host->mmc));
1265 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1266 break;
1267 }
1268 return preset;
1269}
1270
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001271u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1272 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001273{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301274 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001275 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301276 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001277 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001278
Zhangfei Gao85105c52010-08-06 07:10:01 +08001279 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001280 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001281 u16 pre_val;
1282
1283 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1284 pre_val = sdhci_get_preset_value(host);
1285 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1286 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1287 if (host->clk_mul &&
1288 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1289 clk = SDHCI_PROG_CLOCK_MODE;
1290 real_div = div + 1;
1291 clk_mul = host->clk_mul;
1292 } else {
1293 real_div = max_t(int, 1, div << 1);
1294 }
1295 goto clock_set;
1296 }
1297
Arindam Nathc3ed3872011-05-05 12:19:06 +05301298 /*
1299 * Check if the Host Controller supports Programmable Clock
1300 * Mode.
1301 */
1302 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001303 for (div = 1; div <= 1024; div++) {
1304 if ((host->max_clk * host->clk_mul / div)
1305 <= clock)
1306 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001307 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001308 if ((host->max_clk * host->clk_mul / div) <= clock) {
1309 /*
1310 * Set Programmable Clock Mode in the Clock
1311 * Control register.
1312 */
1313 clk = SDHCI_PROG_CLOCK_MODE;
1314 real_div = div;
1315 clk_mul = host->clk_mul;
1316 div--;
1317 } else {
1318 /*
1319 * Divisor can be too small to reach clock
1320 * speed requirement. Then use the base clock.
1321 */
1322 switch_base_clk = true;
1323 }
1324 }
1325
1326 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301327 /* Version 3.00 divisors must be a multiple of 2. */
1328 if (host->max_clk <= clock)
1329 div = 1;
1330 else {
1331 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1332 div += 2) {
1333 if ((host->max_clk / div) <= clock)
1334 break;
1335 }
1336 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001337 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301338 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301339 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1340 && !div && host->max_clk <= 25000000)
1341 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001342 }
1343 } else {
1344 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001345 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001346 if ((host->max_clk / div) <= clock)
1347 break;
1348 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001349 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301350 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001352
Kevin Liu52983382013-01-31 11:31:37 +08001353clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001354 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001355 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301356 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001357 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1358 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001359
1360 return clk;
1361}
1362EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1363
Ritesh Harjanifec79672016-11-21 12:07:19 +05301364void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001365{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001366 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001367
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001369 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370
Chris Ball27f6cb12009-09-22 16:45:31 -07001371 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001372 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001373 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001374 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001375 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001376 pr_err("%s: Internal clock never stabilised.\n",
1377 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378 sdhci_dumpregs(host);
1379 return;
1380 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001381 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001382 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001383
1384 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001385 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001386}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301387EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1388
1389void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1390{
1391 u16 clk;
1392
1393 host->mmc->actual_clock = 0;
1394
1395 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1396
1397 if (clock == 0)
1398 return;
1399
1400 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1401 sdhci_enable_clk(host, clk);
1402}
Russell King17710592014-04-25 12:58:55 +01001403EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404
Adrian Hunter1dceb042016-03-29 12:45:43 +03001405static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1406 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001407{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001408 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001409
Adrian Hunter1dceb042016-03-29 12:45:43 +03001410 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001411
1412 if (mode != MMC_POWER_OFF)
1413 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1414 else
1415 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1416}
1417
Adrian Hunter606d3132016-10-05 12:11:22 +03001418void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1419 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001420{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001421 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001422
Russell King24fbb3c2014-04-25 13:00:06 +01001423 if (mode != MMC_POWER_OFF) {
1424 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001425 case MMC_VDD_165_195:
1426 pwr = SDHCI_POWER_180;
1427 break;
1428 case MMC_VDD_29_30:
1429 case MMC_VDD_30_31:
1430 pwr = SDHCI_POWER_300;
1431 break;
1432 case MMC_VDD_32_33:
1433 case MMC_VDD_33_34:
1434 pwr = SDHCI_POWER_330;
1435 break;
1436 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001437 WARN(1, "%s: Invalid vdd %#x\n",
1438 mmc_hostname(host->mmc), vdd);
1439 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001440 }
1441 }
1442
1443 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001444 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001445
Pierre Ossmanae628902009-05-03 20:45:03 +02001446 host->pwr = pwr;
1447
1448 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001449 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001450 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1451 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001452 } else {
1453 /*
1454 * Spec says that we should clear the power reg before setting
1455 * a new value. Some controllers don't seem to like this though.
1456 */
1457 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1458 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001459
Russell Kinge921a8b2014-04-25 13:00:01 +01001460 /*
1461 * At least the Marvell CaFe chip gets confused if we set the
1462 * voltage and set turn on power at the same time, so set the
1463 * voltage first.
1464 */
1465 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1466 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001467
Russell Kinge921a8b2014-04-25 13:00:01 +01001468 pwr |= SDHCI_POWER_ON;
1469
Pierre Ossmanae628902009-05-03 20:45:03 +02001470 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1471
Russell Kinge921a8b2014-04-25 13:00:01 +01001472 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1473 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001474
Russell Kinge921a8b2014-04-25 13:00:01 +01001475 /*
1476 * Some controllers need an extra 10ms delay of 10ms before
1477 * they can apply clock after applying power
1478 */
1479 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1480 mdelay(10);
1481 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001482}
Adrian Hunter606d3132016-10-05 12:11:22 +03001483EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001484
Adrian Hunter606d3132016-10-05 12:11:22 +03001485void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1486 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001487{
Adrian Hunter606d3132016-10-05 12:11:22 +03001488 if (IS_ERR(host->mmc->supply.vmmc))
1489 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001490 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001491 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001492}
Adrian Hunter606d3132016-10-05 12:11:22 +03001493EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001494
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495/*****************************************************************************\
1496 * *
1497 * MMC callbacks *
1498 * *
1499\*****************************************************************************/
1500
1501static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1502{
1503 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001504 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001505 unsigned long flags;
1506
1507 host = mmc_priv(mmc);
1508
Scott Branden04e079cf2015-03-10 11:35:10 -07001509 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001510 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001511
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512 spin_lock_irqsave(&host->lock, flags);
1513
Adrian Hunter061d17a2016-04-12 14:25:09 +03001514 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001515
1516 /*
1517 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1518 * requests if Auto-CMD12 is enabled.
1519 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001520 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001521 if (mrq->stop) {
1522 mrq->data->stop = NULL;
1523 mrq->stop = NULL;
1524 }
1525 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001526
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001527 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001528 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001529 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301530 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001531 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001532 sdhci_send_command(host, mrq->sbc);
1533 else
1534 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301535 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001536
Pierre Ossman5f25a662006-10-04 02:15:39 -07001537 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001538 spin_unlock_irqrestore(&host->lock, flags);
1539}
1540
Russell King2317f562014-04-25 12:57:07 +01001541void sdhci_set_bus_width(struct sdhci_host *host, int width)
1542{
1543 u8 ctrl;
1544
1545 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1546 if (width == MMC_BUS_WIDTH_8) {
1547 ctrl &= ~SDHCI_CTRL_4BITBUS;
1548 if (host->version >= SDHCI_SPEC_300)
1549 ctrl |= SDHCI_CTRL_8BITBUS;
1550 } else {
1551 if (host->version >= SDHCI_SPEC_300)
1552 ctrl &= ~SDHCI_CTRL_8BITBUS;
1553 if (width == MMC_BUS_WIDTH_4)
1554 ctrl |= SDHCI_CTRL_4BITBUS;
1555 else
1556 ctrl &= ~SDHCI_CTRL_4BITBUS;
1557 }
1558 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1559}
1560EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1561
Russell King96d7b782014-04-25 12:59:26 +01001562void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1563{
1564 u16 ctrl_2;
1565
1566 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1567 /* Select Bus Speed Mode for host */
1568 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1569 if ((timing == MMC_TIMING_MMC_HS200) ||
1570 (timing == MMC_TIMING_UHS_SDR104))
1571 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1572 else if (timing == MMC_TIMING_UHS_SDR12)
1573 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1574 else if (timing == MMC_TIMING_UHS_SDR25)
1575 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1576 else if (timing == MMC_TIMING_UHS_SDR50)
1577 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1578 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1579 (timing == MMC_TIMING_MMC_DDR52))
1580 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001581 else if (timing == MMC_TIMING_MMC_HS400)
1582 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001583 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1584}
1585EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1586
Dong Aishengded97e02016-04-16 01:29:25 +08001587static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001588{
Dong Aishengded97e02016-04-16 01:29:25 +08001589 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001590 u8 ctrl;
1591
Adrian Hunter84ec0482016-12-19 15:33:11 +02001592 if (ios->power_mode == MMC_POWER_UNDEFINED)
1593 return;
1594
Adrian Hunterceb61432011-12-27 15:48:41 +02001595 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001596 if (!IS_ERR(mmc->supply.vmmc) &&
1597 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001598 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001599 return;
1600 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001601
Pierre Ossmand129bce2006-03-24 03:18:17 -08001602 /*
1603 * Reset the chip on each power off.
1604 * Should clear out any weird states.
1605 */
1606 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001607 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001608 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001609 }
1610
Kevin Liu52983382013-01-31 11:31:37 +08001611 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001612 (ios->power_mode == MMC_POWER_UP) &&
1613 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001614 sdhci_enable_preset_value(host, false);
1615
Russell King373073e2014-04-25 12:58:45 +01001616 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001617 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001618 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001619
1620 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1621 host->clock) {
1622 host->timeout_clk = host->mmc->actual_clock ?
1623 host->mmc->actual_clock / 1000 :
1624 host->clock / 1000;
1625 host->mmc->max_busy_timeout =
1626 host->ops->get_max_timeout_count ?
1627 host->ops->get_max_timeout_count(host) :
1628 1 << 27;
1629 host->mmc->max_busy_timeout /= host->timeout_clk;
1630 }
Russell King373073e2014-04-25 12:58:45 +01001631 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001632
Adrian Hunter606d3132016-10-05 12:11:22 +03001633 if (host->ops->set_power)
1634 host->ops->set_power(host, ios->power_mode, ios->vdd);
1635 else
1636 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637
Philip Rakity643a81f2010-09-23 08:24:32 -07001638 if (host->ops->platform_send_init_74_clocks)
1639 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1640
Russell King2317f562014-04-25 12:57:07 +01001641 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001642
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001643 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001644
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001645 if ((ios->timing == MMC_TIMING_SD_HS ||
Jaehoon Chung273c5412016-10-07 14:08:43 +09001646 ios->timing == MMC_TIMING_MMC_HS ||
1647 ios->timing == MMC_TIMING_MMC_HS400 ||
1648 ios->timing == MMC_TIMING_MMC_HS200 ||
1649 ios->timing == MMC_TIMING_MMC_DDR52 ||
1650 ios->timing == MMC_TIMING_UHS_SDR50 ||
1651 ios->timing == MMC_TIMING_UHS_SDR104 ||
1652 ios->timing == MMC_TIMING_UHS_DDR50 ||
1653 ios->timing == MMC_TIMING_UHS_SDR25)
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001654 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001655 ctrl |= SDHCI_CTRL_HISPD;
1656 else
1657 ctrl &= ~SDHCI_CTRL_HISPD;
1658
Arindam Nathd6d50a12011-05-05 12:18:59 +05301659 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301660 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301661
Russell Kingda91a8f2014-04-25 13:00:12 +01001662 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301663 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301664 /*
1665 * We only need to set Driver Strength if the
1666 * preset value enable is not set.
1667 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001668 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301669 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1670 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1671 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001672 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1673 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301674 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1675 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001676 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1677 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1678 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001679 pr_warn("%s: invalid driver type, default to driver type B\n",
1680 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001681 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1682 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301683
1684 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301685 } else {
1686 /*
1687 * According to SDHC Spec v3.00, if the Preset Value
1688 * Enable in the Host Control 2 register is set, we
1689 * need to reset SD Clock Enable before changing High
1690 * Speed Enable to avoid generating clock gliches.
1691 */
Arindam Nath758535c2011-05-05 12:19:00 +05301692
1693 /* Reset SD Clock Enable */
1694 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1695 clk &= ~SDHCI_CLOCK_CARD_EN;
1696 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1697
1698 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1699
1700 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001701 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301702 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301703
Arindam Nath49c468f2011-05-05 12:19:01 +05301704 /* Reset SD Clock Enable */
1705 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1706 clk &= ~SDHCI_CLOCK_CARD_EN;
1707 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1708
Russell King96d7b782014-04-25 12:59:26 +01001709 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001710 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301711
Kevin Liu52983382013-01-31 11:31:37 +08001712 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1713 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1714 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1715 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1716 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001717 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1718 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001719 u16 preset;
1720
1721 sdhci_enable_preset_value(host, true);
1722 preset = sdhci_get_preset_value(host);
1723 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1724 >> SDHCI_PRESET_DRV_SHIFT;
1725 }
1726
Arindam Nath49c468f2011-05-05 12:19:01 +05301727 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001728 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301729 } else
1730 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301731
Leandro Dorileob8352262007-07-25 23:47:04 +02001732 /*
1733 * Some (ENE) controllers go apeshit on some ios operation,
1734 * signalling timeout and CRC errors even on CMD0. Resetting
1735 * it on each ios seems to solve the problem.
1736 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301737 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001738 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001739
Pierre Ossman5f25a662006-10-04 02:15:39 -07001740 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001741}
1742
Dong Aishengded97e02016-04-16 01:29:25 +08001743static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001744{
1745 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001746 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001747
1748 if (host->flags & SDHCI_DEVICE_DEAD)
1749 return 0;
1750
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001751 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001752 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001753 return 1;
1754
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001755 /*
1756 * Try slot gpio detect, if defined it take precedence
1757 * over build in controller functionality
1758 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001759 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001760 return !!gpio_cd;
1761
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001762 /* If polling, assume that the card is always present. */
1763 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1764 return 1;
1765
Kevin Liu94144a42013-02-28 17:35:53 +08001766 /* Host native card detect */
1767 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1768}
1769
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001770static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001771{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001772 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001773 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001774
Pierre Ossmand129bce2006-03-24 03:18:17 -08001775 spin_lock_irqsave(&host->lock, flags);
1776
Pierre Ossman1e728592008-04-16 19:13:13 +02001777 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001778 is_readonly = 0;
1779 else if (host->ops->get_ro)
1780 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001781 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001782 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1783 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001784
1785 spin_unlock_irqrestore(&host->lock, flags);
1786
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001787 /* This quirk needs to be replaced by a callback-function later */
1788 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1789 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001790}
1791
Takashi Iwai82b0e232011-04-21 20:26:38 +02001792#define SAMPLE_COUNT 5
1793
Dong Aishengded97e02016-04-16 01:29:25 +08001794static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001795{
Dong Aishengded97e02016-04-16 01:29:25 +08001796 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001797 int i, ro_count;
1798
Takashi Iwai82b0e232011-04-21 20:26:38 +02001799 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001800 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001801
1802 ro_count = 0;
1803 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001804 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001805 if (++ro_count > SAMPLE_COUNT / 2)
1806 return 1;
1807 }
1808 msleep(30);
1809 }
1810 return 0;
1811}
1812
Adrian Hunter20758b62011-08-29 16:42:12 +03001813static void sdhci_hw_reset(struct mmc_host *mmc)
1814{
1815 struct sdhci_host *host = mmc_priv(mmc);
1816
1817 if (host->ops && host->ops->hw_reset)
1818 host->ops->hw_reset(host);
1819}
1820
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001821static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1822{
Russell Kingbe138552014-04-25 12:55:56 +01001823 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001824 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001825 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001826 else
Russell Kingb537f942014-04-25 12:56:01 +01001827 host->ier &= ~SDHCI_INT_CARD_INT;
1828
1829 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1830 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001831 mmiowb();
1832 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001833}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001834
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001835static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1836{
1837 struct sdhci_host *host = mmc_priv(mmc);
1838 unsigned long flags;
1839
Hans de Goede923713b2017-03-26 13:14:45 +02001840 if (enable)
1841 pm_runtime_get_noresume(host->mmc->parent);
1842
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001843 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001844 if (enable)
1845 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1846 else
1847 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1848
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001849 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001850 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001851
1852 if (!enable)
1853 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001854}
1855
Dong Aishengded97e02016-04-16 01:29:25 +08001856static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1857 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001858{
Dong Aishengded97e02016-04-16 01:29:25 +08001859 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001860 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001861 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001862
1863 /*
1864 * Signal Voltage Switching is only applicable for Host Controllers
1865 * v3.00 and above.
1866 */
1867 if (host->version < SDHCI_SPEC_300)
1868 return 0;
1869
Philip Rakity6231f3d2012-07-23 15:56:23 -07001870 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001871
Fabio Estevam21f59982013-02-14 10:35:03 -02001872 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001873 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001874 if (!(host->flags & SDHCI_SIGNALING_330))
1875 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001876 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1877 ctrl &= ~SDHCI_CTRL_VDD_180;
1878 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1879
Tim Kryger3a48edc2014-06-13 10:13:56 -07001880 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001881 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001882 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001883 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1884 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001885 return -EIO;
1886 }
1887 }
1888 /* Wait for 5ms */
1889 usleep_range(5000, 5500);
1890
1891 /* 3.3V regulator output should be stable within 5 ms */
1892 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1893 if (!(ctrl & SDHCI_CTRL_VDD_180))
1894 return 0;
1895
Joe Perches66061102014-09-12 14:56:56 -07001896 pr_warn("%s: 3.3V regulator output did not became stable\n",
1897 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001898
1899 return -EAGAIN;
1900 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001901 if (!(host->flags & SDHCI_SIGNALING_180))
1902 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001903 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001904 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001905 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001906 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1907 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001908 return -EIO;
1909 }
1910 }
1911
1912 /*
1913 * Enable 1.8V Signal Enable in the Host Control2
1914 * register
1915 */
1916 ctrl |= SDHCI_CTRL_VDD_180;
1917 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1918
Vincent Yang9d967a62015-01-20 16:05:15 +08001919 /* Some controller need to do more when switching */
1920 if (host->ops->voltage_switch)
1921 host->ops->voltage_switch(host);
1922
Kevin Liu20b92a32012-12-17 19:29:26 +08001923 /* 1.8V regulator output should be stable within 5 ms */
1924 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1925 if (ctrl & SDHCI_CTRL_VDD_180)
1926 return 0;
1927
Joe Perches66061102014-09-12 14:56:56 -07001928 pr_warn("%s: 1.8V regulator output did not became stable\n",
1929 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001930
1931 return -EAGAIN;
1932 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001933 if (!(host->flags & SDHCI_SIGNALING_120))
1934 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001935 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001936 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001937 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001938 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1939 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001940 return -EIO;
1941 }
1942 }
1943 return 0;
1944 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301945 /* No signal voltage switch required */
1946 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001947 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301948}
1949
Kevin Liu20b92a32012-12-17 19:29:26 +08001950static int sdhci_card_busy(struct mmc_host *mmc)
1951{
1952 struct sdhci_host *host = mmc_priv(mmc);
1953 u32 present_state;
1954
Adrian Huntere613cc42016-06-23 14:00:58 +03001955 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001956 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001957
Adrian Huntere613cc42016-06-23 14:00:58 +03001958 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001959}
1960
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001961static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1962{
1963 struct sdhci_host *host = mmc_priv(mmc);
1964 unsigned long flags;
1965
1966 spin_lock_irqsave(&host->lock, flags);
1967 host->flags |= SDHCI_HS400_TUNING;
1968 spin_unlock_irqrestore(&host->lock, flags);
1969
1970 return 0;
1971}
1972
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02001973static void sdhci_start_tuning(struct sdhci_host *host)
1974{
1975 u16 ctrl;
1976
1977 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1978 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1979 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1980 ctrl |= SDHCI_CTRL_TUNED_CLK;
1981 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1982
1983 /*
1984 * As per the Host Controller spec v3.00, tuning command
1985 * generates Buffer Read Ready interrupt, so enable that.
1986 *
1987 * Note: The spec clearly says that when tuning sequence
1988 * is being performed, the controller does not generate
1989 * interrupts other than Buffer Read Ready interrupt. But
1990 * to make sure we don't hit a controller bug, we _only_
1991 * enable Buffer Read Ready interrupt here.
1992 */
1993 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1994 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1995}
1996
1997static void sdhci_end_tuning(struct sdhci_host *host)
1998{
1999 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2000 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2001}
2002
2003static void sdhci_reset_tuning(struct sdhci_host *host)
2004{
2005 u16 ctrl;
2006
2007 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2008 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2009 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2010 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2011}
2012
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002013static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002014{
2015 sdhci_reset_tuning(host);
2016
2017 sdhci_do_reset(host, SDHCI_RESET_CMD);
2018 sdhci_do_reset(host, SDHCI_RESET_DATA);
2019
2020 sdhci_end_tuning(host);
2021
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002022 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002023}
2024
2025/*
2026 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2027 * tuning command does not have a data payload (or rather the hardware does it
2028 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2029 * interrupt setup is different to other commands and there is no timeout
2030 * interrupt so special handling is needed.
2031 */
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002032static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002033{
2034 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002035 struct mmc_command cmd = {};
2036 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002037 unsigned long flags;
2038
2039 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002040
2041 cmd.opcode = opcode;
2042 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2043 cmd.mrq = &mrq;
2044
2045 mrq.cmd = &cmd;
2046 /*
2047 * In response to CMD19, the card sends 64 bytes of tuning
2048 * block to the Host Controller. So we set the block size
2049 * to 64 here.
2050 */
Adrian Hunter85336102016-12-02 15:14:26 +02002051 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2052 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2053 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
2054 else
2055 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002056
2057 /*
2058 * The tuning block is sent by the card to the host controller.
2059 * So we set the TRNS_READ bit in the Transfer Mode register.
2060 * This also takes care of setting DMA Enable and Multi Block
2061 * Select in the same register to 0.
2062 */
2063 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2064
2065 sdhci_send_command(host, &cmd);
2066
2067 host->cmd = NULL;
2068
2069 sdhci_del_timer(host, &mrq);
2070
2071 host->tuning_done = 0;
2072
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002073 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002074 spin_unlock_irqrestore(&host->lock, flags);
2075
2076 /* Wait for Buffer Read Ready interrupt */
2077 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2078 msecs_to_jiffies(50));
2079
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002080}
2081
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002082static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002083{
2084 int i;
2085
2086 /*
2087 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2088 * of loops reaches 40 times.
2089 */
2090 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2091 u16 ctrl;
2092
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002093 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002094
2095 if (!host->tuning_done) {
2096 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2097 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002098 sdhci_abort_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002099 return;
2100 }
2101
2102 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2103 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2104 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2105 return; /* Success! */
2106 break;
2107 }
2108
2109 /* eMMC spec does not require a delay between tuning cycles */
2110 if (opcode == MMC_SEND_TUNING_BLOCK)
2111 mdelay(1);
2112 }
2113
2114 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2115 mmc_hostname(host->mmc));
2116 sdhci_reset_tuning(host);
2117}
2118
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002119int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302120{
Russell King4b6f37d2014-04-25 12:59:36 +01002121 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302122 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002123 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002124 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302125
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002126 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002127
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002128 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2129 tuning_count = host->tuning_count;
2130
Arindam Nathb513ea22011-05-05 12:19:04 +05302131 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002132 * The Host Controller needs tuning in case of SDR104 and DDR50
2133 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2134 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302135 * If the Host Controller supports the HS200 mode then the
2136 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302137 */
Russell King4b6f37d2014-04-25 12:59:36 +01002138 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002139 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002140 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002141 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002142 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002143
Russell King4b6f37d2014-04-25 12:59:36 +01002144 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002145 /*
2146 * Periodic re-tuning for HS400 is not expected to be needed, so
2147 * disable it here.
2148 */
2149 if (hs400_tuning)
2150 tuning_count = 0;
2151 break;
2152
Russell King4b6f37d2014-04-25 12:59:36 +01002153 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002154 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002155 break;
Girish K S069c9f12012-01-06 09:56:39 +05302156
Russell King4b6f37d2014-04-25 12:59:36 +01002157 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002158 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002159 break;
2160 /* FALLTHROUGH */
2161
2162 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002163 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302164 }
2165
Dong Aisheng45251812013-09-13 19:11:30 +08002166 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302167 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002168 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002169 }
2170
Adrian Hunter6b11e702016-12-02 15:14:27 +02002171 host->mmc->retune_period = tuning_count;
2172
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002173 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302174
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002175 __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302176
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002177 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002178out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302179 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002180
Arindam Nathb513ea22011-05-05 12:19:04 +05302181 return err;
2182}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002183EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302184
Kevin Liu52983382013-01-31 11:31:37 +08002185static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302186{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302187 /* Host Controller v3.00 defines preset value registers */
2188 if (host->version < SDHCI_SPEC_300)
2189 return;
2190
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302191 /*
2192 * We only enable or disable Preset Value if they are not already
2193 * enabled or disabled respectively. Otherwise, we bail out.
2194 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002195 if (host->preset_enabled != enable) {
2196 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2197
2198 if (enable)
2199 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2200 else
2201 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2202
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302203 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002204
2205 if (enable)
2206 host->flags |= SDHCI_PV_ENABLED;
2207 else
2208 host->flags &= ~SDHCI_PV_ENABLED;
2209
2210 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302211 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002212}
2213
Haibo Chen348487c2014-12-09 17:04:05 +08002214static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2215 int err)
2216{
2217 struct sdhci_host *host = mmc_priv(mmc);
2218 struct mmc_data *data = mrq->data;
2219
Russell Kingf48f0392016-01-26 13:40:32 +00002220 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002221 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2222 data->flags & MMC_DATA_WRITE ?
2223 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2224
2225 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002226}
2227
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002228static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002229{
2230 struct sdhci_host *host = mmc_priv(mmc);
2231
Haibo Chend31911b2015-08-25 10:02:11 +08002232 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002233
2234 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002235 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002236}
2237
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002238static inline bool sdhci_has_requests(struct sdhci_host *host)
2239{
2240 return host->cmd || host->data_cmd;
2241}
2242
2243static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2244{
2245 if (host->data_cmd) {
2246 host->data_cmd->error = err;
2247 sdhci_finish_mrq(host, host->data_cmd->mrq);
2248 }
2249
2250 if (host->cmd) {
2251 host->cmd->error = err;
2252 sdhci_finish_mrq(host, host->cmd->mrq);
2253 }
2254}
2255
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002256static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002257{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002258 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002259 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002260 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002261
Christian Daudt722e1282013-06-20 14:26:36 -07002262 /* First check if client has provided their own card event */
2263 if (host->ops->card_event)
2264 host->ops->card_event(host);
2265
Adrian Hunterd3940f22016-06-29 16:24:14 +03002266 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002267
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268 spin_lock_irqsave(&host->lock, flags);
2269
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002270 /* Check sdhci_has_requests() first in case we are runtime suspended */
2271 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302272 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002273 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302274 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002275 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276
Russell King03231f92014-04-25 12:57:12 +01002277 sdhci_do_reset(host, SDHCI_RESET_CMD);
2278 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002279
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002280 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002281 }
2282
2283 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002284}
2285
2286static const struct mmc_host_ops sdhci_ops = {
2287 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002288 .post_req = sdhci_post_req,
2289 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002290 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002291 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002292 .get_ro = sdhci_get_ro,
2293 .hw_reset = sdhci_hw_reset,
2294 .enable_sdio_irq = sdhci_enable_sdio_irq,
2295 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002296 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002297 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002298 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002299 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002300};
2301
2302/*****************************************************************************\
2303 * *
2304 * Tasklets *
2305 * *
2306\*****************************************************************************/
2307
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002308static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002309{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002310 unsigned long flags;
2311 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002312 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002314 spin_lock_irqsave(&host->lock, flags);
2315
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002316 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2317 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002318 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002319 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002320 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002322 if (!mrq) {
2323 spin_unlock_irqrestore(&host->lock, flags);
2324 return true;
2325 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002327 sdhci_del_timer(host, mrq);
2328
Pierre Ossmand129bce2006-03-24 03:18:17 -08002329 /*
Russell King054cedf2016-01-26 13:40:42 +00002330 * Always unmap the data buffers if they were mapped by
2331 * sdhci_prepare_data() whenever we finish with a request.
2332 * This avoids leaking DMA mappings on error.
2333 */
2334 if (host->flags & SDHCI_REQ_USE_DMA) {
2335 struct mmc_data *data = mrq->data;
2336
2337 if (data && data->host_cookie == COOKIE_MAPPED) {
2338 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2339 (data->flags & MMC_DATA_READ) ?
2340 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2341 data->host_cookie = COOKIE_UNMAPPED;
2342 }
2343 }
2344
2345 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346 * The controller needs a reset of internal state machines
2347 * upon error conditions.
2348 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002349 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002350 /*
2351 * Do not finish until command and data lines are available for
2352 * reset. Note there can only be one other mrq, so it cannot
2353 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2354 * would both be null.
2355 */
2356 if (host->cmd || host->data_cmd) {
2357 spin_unlock_irqrestore(&host->lock, flags);
2358 return true;
2359 }
2360
Pierre Ossman645289d2006-06-30 02:22:33 -07002361 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002362 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002363 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002364 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002365
2366 /* Spec says we should do both at the same time, but Ricoh
2367 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002368 sdhci_do_reset(host, SDHCI_RESET_CMD);
2369 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002370
2371 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 }
2373
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002374 if (!sdhci_has_requests(host))
2375 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002376
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002377 host->mrqs_done[i] = NULL;
2378
Pierre Ossman5f25a662006-10-04 02:15:39 -07002379 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002380 spin_unlock_irqrestore(&host->lock, flags);
2381
2382 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002383
2384 return false;
2385}
2386
2387static void sdhci_tasklet_finish(unsigned long param)
2388{
2389 struct sdhci_host *host = (struct sdhci_host *)param;
2390
2391 while (!sdhci_request_done(host))
2392 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002393}
2394
2395static void sdhci_timeout_timer(unsigned long data)
2396{
2397 struct sdhci_host *host;
2398 unsigned long flags;
2399
2400 host = (struct sdhci_host*)data;
2401
2402 spin_lock_irqsave(&host->lock, flags);
2403
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002404 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2405 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2406 mmc_hostname(host->mmc));
2407 sdhci_dumpregs(host);
2408
2409 host->cmd->error = -ETIMEDOUT;
2410 sdhci_finish_mrq(host, host->cmd->mrq);
2411 }
2412
2413 mmiowb();
2414 spin_unlock_irqrestore(&host->lock, flags);
2415}
2416
2417static void sdhci_timeout_data_timer(unsigned long data)
2418{
2419 struct sdhci_host *host;
2420 unsigned long flags;
2421
2422 host = (struct sdhci_host *)data;
2423
2424 spin_lock_irqsave(&host->lock, flags);
2425
2426 if (host->data || host->data_cmd ||
2427 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002428 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2429 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 sdhci_dumpregs(host);
2431
2432 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002433 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002434 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002435 } else if (host->data_cmd) {
2436 host->data_cmd->error = -ETIMEDOUT;
2437 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002438 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002439 host->cmd->error = -ETIMEDOUT;
2440 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002441 }
2442 }
2443
Pierre Ossman5f25a662006-10-04 02:15:39 -07002444 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002445 spin_unlock_irqrestore(&host->lock, flags);
2446}
2447
2448/*****************************************************************************\
2449 * *
2450 * Interrupt handling *
2451 * *
2452\*****************************************************************************/
2453
Adrian Hunterfc605f12016-10-05 12:11:21 +03002454static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002456 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002457 /*
2458 * SDHCI recovers from errors by resetting the cmd and data
2459 * circuits. Until that is done, there very well might be more
2460 * interrupts, so ignore them in that case.
2461 */
2462 if (host->pending_reset)
2463 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002464 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2465 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002466 sdhci_dumpregs(host);
2467 return;
2468 }
2469
Russell Kingec014cb2016-01-26 13:39:39 +00002470 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2471 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2472 if (intmask & SDHCI_INT_TIMEOUT)
2473 host->cmd->error = -ETIMEDOUT;
2474 else
2475 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002476
Russell King71fcbda2016-01-26 13:39:45 +00002477 /*
2478 * If this command initiates a data phase and a response
2479 * CRC error is signalled, the card can start transferring
2480 * data - the card may have received the command without
2481 * error. We must not terminate the mmc_request early.
2482 *
2483 * If the card did not receive the command or returned an
2484 * error which prevented it sending data, the data phase
2485 * will time out.
2486 */
2487 if (host->cmd->data &&
2488 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2489 SDHCI_INT_CRC) {
2490 host->cmd = NULL;
2491 return;
2492 }
2493
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002494 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002495 return;
2496 }
2497
Pierre Ossmane8095172008-07-25 01:09:08 +02002498 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002499 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002500}
2501
George G. Davis0957c332010-02-18 12:32:12 -05002502#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002503static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002504{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002505 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002506
2507 sdhci_dumpregs(host);
2508
2509 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002510 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002511
Adrian Huntere57a5f62014-11-04 12:42:46 +02002512 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002513 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2514 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002515 le32_to_cpu(dma_desc->addr_lo),
2516 le16_to_cpu(dma_desc->len),
2517 le16_to_cpu(dma_desc->cmd));
2518 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002519 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2520 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002521 le16_to_cpu(dma_desc->len),
2522 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002523
Adrian Hunter76fe3792014-11-04 12:42:42 +02002524 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002525
Adrian Hunter05452302014-11-04 12:42:45 +02002526 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002527 break;
2528 }
2529}
2530#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002531static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002532#endif
2533
Pierre Ossmand129bce2006-03-24 03:18:17 -08002534static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2535{
Girish K S069c9f12012-01-06 09:56:39 +05302536 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002537
Arindam Nathb513ea22011-05-05 12:19:04 +05302538 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2539 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302540 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2541 if (command == MMC_SEND_TUNING_BLOCK ||
2542 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302543 host->tuning_done = 1;
2544 wake_up(&host->buf_ready_int);
2545 return;
2546 }
2547 }
2548
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002550 struct mmc_command *data_cmd = host->data_cmd;
2551
Pierre Ossmand129bce2006-03-24 03:18:17 -08002552 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002553 * The "data complete" interrupt is also used to
2554 * indicate that a busy state has ended. See comment
2555 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002556 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002557 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002558 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002559 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002560 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002561 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002562 return;
2563 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002564 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002565 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002566 /*
2567 * Some cards handle busy-end interrupt
2568 * before the command completed, so make
2569 * sure we do things in the proper order.
2570 */
Adrian Hunterea968022016-06-29 16:24:24 +03002571 if (host->cmd == data_cmd)
2572 return;
2573
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002574 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002575 return;
2576 }
2577 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002578
Adrian Huntered1563d2016-06-29 16:24:29 +03002579 /*
2580 * SDHCI recovers from errors by resetting the cmd and data
2581 * circuits. Until that is done, there very well might be more
2582 * interrupts, so ignore them in that case.
2583 */
2584 if (host->pending_reset)
2585 return;
2586
Marek Vasut2e4456f2015-11-18 10:47:02 +01002587 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2588 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589 sdhci_dumpregs(host);
2590
2591 return;
2592 }
2593
2594 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002595 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002596 else if (intmask & SDHCI_INT_DATA_END_BIT)
2597 host->data->error = -EILSEQ;
2598 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2599 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2600 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002601 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002602 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302603 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002604 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002605 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002606 if (host->ops->adma_workaround)
2607 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002608 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002609
Pierre Ossman17b04292007-07-22 22:18:46 +02002610 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002611 sdhci_finish_data(host);
2612 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002613 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002614 sdhci_transfer_pio(host);
2615
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002616 /*
2617 * We currently don't do anything fancy with DMA
2618 * boundaries, but as we can't disable the feature
2619 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002620 *
2621 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2622 * should return a valid address to continue from, but as
2623 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002624 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002625 if (intmask & SDHCI_INT_DMA_END) {
2626 u32 dmastart, dmanow;
2627 dmastart = sg_dma_address(host->data->sg);
2628 dmanow = dmastart + host->data->bytes_xfered;
2629 /*
2630 * Force update to the next DMA block boundary.
2631 */
2632 dmanow = (dmanow &
2633 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2634 SDHCI_DEFAULT_BOUNDARY_SIZE;
2635 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002636 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2637 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002638 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2639 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002640
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002641 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002642 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002643 /*
2644 * Data managed to finish before the
2645 * command completed. Make sure we do
2646 * things in the proper order.
2647 */
2648 host->data_early = 1;
2649 } else {
2650 sdhci_finish_data(host);
2651 }
2652 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002653 }
2654}
2655
David Howells7d12e782006-10-05 14:55:46 +01002656static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002657{
Russell King781e9892014-04-25 12:55:46 +01002658 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002659 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002660 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002661 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002662
2663 spin_lock(&host->lock);
2664
Russell Kingbe138552014-04-25 12:55:56 +01002665 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002666 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002667 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002668 }
2669
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002670 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002671 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002672 result = IRQ_NONE;
2673 goto out;
2674 }
2675
Russell King41005002014-04-25 12:55:36 +01002676 do {
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002677 DBG("IRQ status 0x%08x\n", intmask);
2678
2679 if (host->ops->irq) {
2680 intmask = host->ops->irq(host, intmask);
2681 if (!intmask)
2682 goto cont;
2683 }
2684
Russell King41005002014-04-25 12:55:36 +01002685 /* Clear selected interrupts. */
2686 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2687 SDHCI_INT_BUS_POWER);
2688 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689
Russell King41005002014-04-25 12:55:36 +01002690 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2691 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2692 SDHCI_CARD_PRESENT;
2693
2694 /*
2695 * There is a observation on i.mx esdhc. INSERT
2696 * bit will be immediately set again when it gets
2697 * cleared, if a card is inserted. We have to mask
2698 * the irq to prevent interrupt storm which will
2699 * freeze the system. And the REMOVE gets the
2700 * same situation.
2701 *
2702 * More testing are needed here to ensure it works
2703 * for other platforms though.
2704 */
Russell Kingb537f942014-04-25 12:56:01 +01002705 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2706 SDHCI_INT_CARD_REMOVE);
2707 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2708 SDHCI_INT_CARD_INSERT;
2709 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2710 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002711
2712 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2713 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002714
2715 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2716 SDHCI_INT_CARD_REMOVE);
2717 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002718 }
2719
2720 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002721 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002722
2723 if (intmask & SDHCI_INT_DATA_MASK)
2724 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2725
2726 if (intmask & SDHCI_INT_BUS_POWER)
2727 pr_err("%s: Card is consuming too much power!\n",
2728 mmc_hostname(host->mmc));
2729
Dong Aishengf37b20e2016-07-12 15:46:17 +08002730 if (intmask & SDHCI_INT_RETUNE)
2731 mmc_retune_needed(host->mmc);
2732
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002733 if ((intmask & SDHCI_INT_CARD_INT) &&
2734 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002735 sdhci_enable_sdio_irq_nolock(host, false);
2736 host->thread_isr |= SDHCI_INT_CARD_INT;
2737 result = IRQ_WAKE_THREAD;
2738 }
Russell King41005002014-04-25 12:55:36 +01002739
2740 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2741 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2742 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002743 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002744
2745 if (intmask) {
2746 unexpected |= intmask;
2747 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2748 }
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002749cont:
Russell King781e9892014-04-25 12:55:46 +01002750 if (result == IRQ_NONE)
2751 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002752
2753 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002754 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002755out:
2756 spin_unlock(&host->lock);
2757
Alexander Stein6379b232012-03-14 09:52:10 +01002758 if (unexpected) {
2759 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2760 mmc_hostname(host->mmc), unexpected);
2761 sdhci_dumpregs(host);
2762 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002763
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764 return result;
2765}
2766
Russell King781e9892014-04-25 12:55:46 +01002767static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2768{
2769 struct sdhci_host *host = dev_id;
2770 unsigned long flags;
2771 u32 isr;
2772
2773 spin_lock_irqsave(&host->lock, flags);
2774 isr = host->thread_isr;
2775 host->thread_isr = 0;
2776 spin_unlock_irqrestore(&host->lock, flags);
2777
Russell King3560db82014-04-25 12:55:51 +01002778 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002779 struct mmc_host *mmc = host->mmc;
2780
2781 mmc->ops->card_event(mmc);
2782 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002783 }
2784
Russell King781e9892014-04-25 12:55:46 +01002785 if (isr & SDHCI_INT_CARD_INT) {
2786 sdio_run_irqs(host->mmc);
2787
2788 spin_lock_irqsave(&host->lock, flags);
2789 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2790 sdhci_enable_sdio_irq_nolock(host, true);
2791 spin_unlock_irqrestore(&host->lock, flags);
2792 }
2793
2794 return isr ? IRQ_HANDLED : IRQ_NONE;
2795}
2796
Pierre Ossmand129bce2006-03-24 03:18:17 -08002797/*****************************************************************************\
2798 * *
2799 * Suspend/resume *
2800 * *
2801\*****************************************************************************/
2802
2803#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002804/*
2805 * To enable wakeup events, the corresponding events have to be enabled in
2806 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2807 * Table' in the SD Host Controller Standard Specification.
2808 * It is useless to restore SDHCI_INT_ENABLE state in
2809 * sdhci_disable_irq_wakeups() since it will be set by
2810 * sdhci_enable_card_detection() or sdhci_init().
2811 */
Kevin Liuad080d72013-01-05 17:21:33 +08002812void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2813{
2814 u8 val;
2815 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2816 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002817 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2818 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002819
2820 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2821 val |= mask ;
2822 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002823 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002824 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002825 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2826 }
Kevin Liuad080d72013-01-05 17:21:33 +08002827 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002828 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002829}
2830EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2831
Fabio Estevam0b10f472014-08-30 14:53:13 -03002832static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002833{
2834 u8 val;
2835 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2836 | SDHCI_WAKE_ON_INT;
2837
2838 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2839 val &= ~mask;
2840 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2841}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002842
Manuel Lauss29495aa2011-11-03 11:09:45 +01002843int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002845 sdhci_disable_card_detection(host);
2846
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002847 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302848
Kevin Liuad080d72013-01-05 17:21:33 +08002849 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002850 host->ier = 0;
2851 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2852 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002853 free_irq(host->irq, host);
2854 } else {
2855 sdhci_enable_irq_wakeups(host);
2856 enable_irq_wake(host->irq);
2857 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002858 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002859}
2860
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002861EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002862
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002863int sdhci_resume_host(struct sdhci_host *host)
2864{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002865 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002866 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002867
Richard Röjforsa13abc72009-09-22 16:45:30 -07002868 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002869 if (host->ops->enable_dma)
2870 host->ops->enable_dma(host);
2871 }
2872
Adrian Hunter6308d292012-02-07 14:48:54 +02002873 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2874 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2875 /* Card keeps power but host controller does not */
2876 sdhci_init(host, 0);
2877 host->pwr = 0;
2878 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002879 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002880 } else {
2881 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2882 mmiowb();
2883 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002884
Haibo Chen14a7b41642015-09-15 18:32:58 +08002885 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2886 ret = request_threaded_irq(host->irq, sdhci_irq,
2887 sdhci_thread_irq, IRQF_SHARED,
2888 mmc_hostname(host->mmc), host);
2889 if (ret)
2890 return ret;
2891 } else {
2892 sdhci_disable_irq_wakeups(host);
2893 disable_irq_wake(host->irq);
2894 }
2895
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002896 sdhci_enable_card_detection(host);
2897
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002898 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002899}
2900
2901EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002902
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002903int sdhci_runtime_suspend_host(struct sdhci_host *host)
2904{
2905 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002906
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002907 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002908
2909 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002910 host->ier &= SDHCI_INT_CARD_INT;
2911 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2912 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002913 spin_unlock_irqrestore(&host->lock, flags);
2914
Russell King781e9892014-04-25 12:55:46 +01002915 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002916
2917 spin_lock_irqsave(&host->lock, flags);
2918 host->runtime_suspended = true;
2919 spin_unlock_irqrestore(&host->lock, flags);
2920
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002921 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002922}
2923EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2924
2925int sdhci_runtime_resume_host(struct sdhci_host *host)
2926{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002927 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002928 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002929 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002930
2931 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2932 if (host->ops->enable_dma)
2933 host->ops->enable_dma(host);
2934 }
2935
2936 sdhci_init(host, 0);
2937
Adrian Hunter84ec0482016-12-19 15:33:11 +02002938 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED) {
2939 /* Force clock and power re-program */
2940 host->pwr = 0;
2941 host->clock = 0;
2942 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2943 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002944
Adrian Hunter84ec0482016-12-19 15:33:11 +02002945 if ((host_flags & SDHCI_PV_ENABLED) &&
2946 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2947 spin_lock_irqsave(&host->lock, flags);
2948 sdhci_enable_preset_value(host, true);
2949 spin_unlock_irqrestore(&host->lock, flags);
2950 }
2951
2952 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2953 mmc->ops->hs400_enhanced_strobe)
2954 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002955 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002956
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002957 spin_lock_irqsave(&host->lock, flags);
2958
2959 host->runtime_suspended = false;
2960
2961 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002962 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002963 sdhci_enable_sdio_irq_nolock(host, true);
2964
2965 /* Enable Card Detection */
2966 sdhci_enable_card_detection(host);
2967
2968 spin_unlock_irqrestore(&host->lock, flags);
2969
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002970 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002971}
2972EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2973
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002974#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002975
Pierre Ossmand129bce2006-03-24 03:18:17 -08002976/*****************************************************************************\
2977 * *
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002978 * Command Queue Engine (CQE) helpers *
2979 * *
2980\*****************************************************************************/
2981
2982void sdhci_cqe_enable(struct mmc_host *mmc)
2983{
2984 struct sdhci_host *host = mmc_priv(mmc);
2985 unsigned long flags;
2986 u8 ctrl;
2987
2988 spin_lock_irqsave(&host->lock, flags);
2989
2990 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
2991 ctrl &= ~SDHCI_CTRL_DMA_MASK;
2992 if (host->flags & SDHCI_USE_64_BIT_DMA)
2993 ctrl |= SDHCI_CTRL_ADMA64;
2994 else
2995 ctrl |= SDHCI_CTRL_ADMA32;
2996 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
2997
2998 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 512),
2999 SDHCI_BLOCK_SIZE);
3000
3001 /* Set maximum timeout */
3002 sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
3003
3004 host->ier = host->cqe_ier;
3005
3006 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3007 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
3008
3009 host->cqe_on = true;
3010
3011 pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
3012 mmc_hostname(mmc), host->ier,
3013 sdhci_readl(host, SDHCI_INT_STATUS));
3014
3015 mmiowb();
3016 spin_unlock_irqrestore(&host->lock, flags);
3017}
3018EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
3019
3020void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
3021{
3022 struct sdhci_host *host = mmc_priv(mmc);
3023 unsigned long flags;
3024
3025 spin_lock_irqsave(&host->lock, flags);
3026
3027 sdhci_set_default_irqs(host);
3028
3029 host->cqe_on = false;
3030
3031 if (recovery) {
3032 sdhci_do_reset(host, SDHCI_RESET_CMD);
3033 sdhci_do_reset(host, SDHCI_RESET_DATA);
3034 }
3035
3036 pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
3037 mmc_hostname(mmc), host->ier,
3038 sdhci_readl(host, SDHCI_INT_STATUS));
3039
3040 mmiowb();
3041 spin_unlock_irqrestore(&host->lock, flags);
3042}
3043EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
3044
3045bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
3046 int *data_error)
3047{
3048 u32 mask;
3049
3050 if (!host->cqe_on)
3051 return false;
3052
3053 if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
3054 *cmd_error = -EILSEQ;
3055 else if (intmask & SDHCI_INT_TIMEOUT)
3056 *cmd_error = -ETIMEDOUT;
3057 else
3058 *cmd_error = 0;
3059
3060 if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3061 *data_error = -EILSEQ;
3062 else if (intmask & SDHCI_INT_DATA_TIMEOUT)
3063 *data_error = -ETIMEDOUT;
3064 else if (intmask & SDHCI_INT_ADMA_ERROR)
3065 *data_error = -EIO;
3066 else
3067 *data_error = 0;
3068
3069 /* Clear selected interrupts. */
3070 mask = intmask & host->cqe_ier;
3071 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3072
3073 if (intmask & SDHCI_INT_BUS_POWER)
3074 pr_err("%s: Card is consuming too much power!\n",
3075 mmc_hostname(host->mmc));
3076
3077 intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
3078 if (intmask) {
3079 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3080 pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
3081 mmc_hostname(host->mmc), intmask);
3082 sdhci_dumpregs(host);
3083 }
3084
3085 return true;
3086}
3087EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
3088
3089/*****************************************************************************\
3090 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003091 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003092 * *
3093\*****************************************************************************/
3094
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003095struct sdhci_host *sdhci_alloc_host(struct device *dev,
3096 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003097{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003098 struct mmc_host *mmc;
3099 struct sdhci_host *host;
3100
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003101 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003102
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003103 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003104 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003105 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003106
3107 host = mmc_priv(mmc);
3108 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003109 host->mmc_host_ops = sdhci_ops;
3110 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003111
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003112 host->flags = SDHCI_SIGNALING_330;
3113
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003114 host->cqe_ier = SDHCI_CQE_INT_MASK;
3115 host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
3116
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003117 return host;
3118}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003119
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003120EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003121
Alexandre Courbot7b913692016-03-07 11:07:55 +09003122static int sdhci_set_dma_mask(struct sdhci_host *host)
3123{
3124 struct mmc_host *mmc = host->mmc;
3125 struct device *dev = mmc_dev(mmc);
3126 int ret = -EINVAL;
3127
3128 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3129 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3130
3131 /* Try 64-bit mask if hardware is capable of it */
3132 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3133 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3134 if (ret) {
3135 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3136 mmc_hostname(mmc));
3137 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3138 }
3139 }
3140
3141 /* 32-bit mask as default & fallback */
3142 if (ret) {
3143 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3144 if (ret)
3145 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3146 mmc_hostname(mmc));
3147 }
3148
3149 return ret;
3150}
3151
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003152void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3153{
3154 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003155 u64 dt_caps_mask = 0;
3156 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003157
3158 if (host->read_caps)
3159 return;
3160
3161 host->read_caps = true;
3162
3163 if (debug_quirks)
3164 host->quirks = debug_quirks;
3165
3166 if (debug_quirks2)
3167 host->quirks2 = debug_quirks2;
3168
3169 sdhci_do_reset(host, SDHCI_RESET_ALL);
3170
Zach Brown92e0c442016-11-02 10:26:16 -05003171 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3172 "sdhci-caps-mask", &dt_caps_mask);
3173 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3174 "sdhci-caps", &dt_caps);
3175
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003176 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3177 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3178
3179 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3180 return;
3181
Zach Brown92e0c442016-11-02 10:26:16 -05003182 if (caps) {
3183 host->caps = *caps;
3184 } else {
3185 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3186 host->caps &= ~lower_32_bits(dt_caps_mask);
3187 host->caps |= lower_32_bits(dt_caps);
3188 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003189
3190 if (host->version < SDHCI_SPEC_300)
3191 return;
3192
Zach Brown92e0c442016-11-02 10:26:16 -05003193 if (caps1) {
3194 host->caps1 = *caps1;
3195 } else {
3196 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3197 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3198 host->caps1 |= upper_32_bits(dt_caps);
3199 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003200}
3201EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3202
Adrian Hunter52f53362016-06-29 16:24:15 +03003203int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003204{
3205 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303206 u32 max_current_caps;
3207 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003208 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003209 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003210 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003211
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003212 WARN_ON(host == NULL);
3213 if (host == NULL)
3214 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003215
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003216 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003217
Jon Hunterefba1422016-07-12 14:53:36 +01003218 /*
3219 * If there are external regulators, get them. Note this must be done
3220 * early before resetting the host and reading the capabilities so that
3221 * the host can take the appropriate action if regulators are not
3222 * available.
3223 */
3224 ret = mmc_regulator_get_supply(mmc);
3225 if (ret == -EPROBE_DEFER)
3226 return ret;
3227
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003228 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003229
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003230 override_timeout_clk = host->timeout_clk;
3231
Zhangfei Gao85105c52010-08-06 07:10:01 +08003232 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003233 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3234 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003235 }
3236
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003237 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003238 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003239 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003240 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003241 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003242 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003244 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003245 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003246 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003247 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003248 }
3249
Arindam Nathf2119df2011-05-05 12:18:57 +05303250 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003251 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003252 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003253
3254 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3255 (host->flags & SDHCI_USE_ADMA)) {
3256 DBG("Disabling ADMA as it is marked broken\n");
3257 host->flags &= ~SDHCI_USE_ADMA;
3258 }
3259
Adrian Huntere57a5f62014-11-04 12:42:46 +02003260 /*
3261 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3262 * and *must* do 64-bit DMA. A driver has the opportunity to change
3263 * that during the first call to ->enable_dma(). Similarly
3264 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3265 * implement.
3266 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003267 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003268 host->flags |= SDHCI_USE_64_BIT_DMA;
3269
Richard Röjforsa13abc72009-09-22 16:45:30 -07003270 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003271 ret = sdhci_set_dma_mask(host);
3272
3273 if (!ret && host->ops->enable_dma)
3274 ret = host->ops->enable_dma(host);
3275
3276 if (ret) {
3277 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3278 mmc_hostname(mmc));
3279 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3280
3281 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003282 }
3283 }
3284
Adrian Huntere57a5f62014-11-04 12:42:46 +02003285 /* SDMA does not support 64-bit DMA */
3286 if (host->flags & SDHCI_USE_64_BIT_DMA)
3287 host->flags &= ~SDHCI_USE_SDMA;
3288
Pierre Ossman2134a922008-06-28 18:28:51 +02003289 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003290 dma_addr_t dma;
3291 void *buf;
3292
Pierre Ossman2134a922008-06-28 18:28:51 +02003293 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003294 * The DMA descriptor table size is calculated as the maximum
3295 * number of segments times 2, to allow for an alignment
3296 * descriptor for each segment, plus 1 for a nop end descriptor,
3297 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003298 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003299 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3300 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3301 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003302 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003303 } else {
3304 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3305 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003306 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003307 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003308
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003309 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003310 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3311 host->adma_table_sz, &dma, GFP_KERNEL);
3312 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003313 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003314 mmc_hostname(mmc));
3315 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003316 } else if ((dma + host->align_buffer_sz) &
3317 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003318 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3319 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003320 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003321 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3322 host->adma_table_sz, buf, dma);
3323 } else {
3324 host->align_buffer = buf;
3325 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003326
Russell Kinge66e61c2016-01-26 13:39:55 +00003327 host->adma_table = buf + host->align_buffer_sz;
3328 host->adma_addr = dma + host->align_buffer_sz;
3329 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003330 }
3331
Pierre Ossman76591502008-07-21 00:32:11 +02003332 /*
3333 * If we use DMA, then it's up to the caller to set the DMA
3334 * mask, but PIO does not need the hw shim so we set a new
3335 * mask here in that case.
3336 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003337 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003338 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003339 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003340 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003341
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003342 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003343 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003344 >> SDHCI_CLOCK_BASE_SHIFT;
3345 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003346 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003347 >> SDHCI_CLOCK_BASE_SHIFT;
3348
Pierre Ossmand129bce2006-03-24 03:18:17 -08003349 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003350 if (host->max_clk == 0 || host->quirks &
3351 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003352 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003353 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3354 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003355 ret = -ENODEV;
3356 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003357 }
3358 host->max_clk = host->ops->get_max_clock(host);
3359 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003360
3361 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303362 * In case of Host Controller v3.00, find out whether clock
3363 * multiplier is supported.
3364 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003365 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303366 SDHCI_CLOCK_MUL_SHIFT;
3367
3368 /*
3369 * In case the value in Clock Multiplier is 0, then programmable
3370 * clock mode is not supported, otherwise the actual clock
3371 * multiplier is one more than the value of Clock Multiplier
3372 * in the Capabilities Register.
3373 */
3374 if (host->clk_mul)
3375 host->clk_mul += 1;
3376
3377 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003378 * Set host parameters.
3379 */
Dong Aisheng59241752015-07-22 20:53:07 +08003380 max_clk = host->max_clk;
3381
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003382 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003383 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303384 else if (host->version >= SDHCI_SPEC_300) {
3385 if (host->clk_mul) {
3386 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003387 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303388 } else
3389 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3390 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003391 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003392
Adrian Hunterd310ae42016-04-12 14:25:07 +03003393 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003394 mmc->f_max = max_clk;
3395
Aisheng Dong28aab052014-08-27 15:26:31 +08003396 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003397 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003398 SDHCI_TIMEOUT_CLK_SHIFT;
Shawn Lin8cc35282017-03-24 15:50:12 +08003399
3400 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
3401 host->timeout_clk *= 1000;
3402
Aisheng Dong28aab052014-08-27 15:26:31 +08003403 if (host->timeout_clk == 0) {
Shawn Lin8cc35282017-03-24 15:50:12 +08003404 if (!host->ops->get_timeout_clock) {
Aisheng Dong28aab052014-08-27 15:26:31 +08003405 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3406 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003407 ret = -ENODEV;
3408 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003409 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003410
Shawn Lin8cc35282017-03-24 15:50:12 +08003411 host->timeout_clk =
3412 DIV_ROUND_UP(host->ops->get_timeout_clock(host),
3413 1000);
3414 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003415
Adrian Hunter99513622016-03-07 13:33:55 +02003416 if (override_timeout_clk)
3417 host->timeout_clk = override_timeout_clk;
3418
Aisheng Dong28aab052014-08-27 15:26:31 +08003419 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003420 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003421 mmc->max_busy_timeout /= host->timeout_clk;
3422 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003423
Andrei Warkentine89d4562011-05-23 15:06:37 -05003424 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003425 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003426
3427 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3428 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003429
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003430 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003431 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003432 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003433 !(host->flags & SDHCI_USE_SDMA)) &&
3434 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003435 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003436 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003437 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003438 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003439 }
3440
Philip Rakity15ec4462010-11-19 16:48:39 -05003441 /*
3442 * A controller may support 8-bit width, but the board itself
3443 * might not have the pins brought out. Boards that support
3444 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3445 * their platform code before calling sdhci_add_host(), and we
3446 * won't assume 8-bit width for hosts without that CAP.
3447 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003448 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003449 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003450
Jerry Huang63ef5d82012-10-25 13:47:19 +08003451 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3452 mmc->caps &= ~MMC_CAP_CMD23;
3453
Adrian Hunter28da3582016-06-29 16:24:17 +03003454 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003455 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003456
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003457 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003458 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003459 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003460 mmc->caps |= MMC_CAP_NEEDS_POLL;
3461
Philip Rakity6231f3d2012-07-23 15:56:23 -07003462 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003463 if (!IS_ERR(mmc->supply.vqmmc)) {
3464 ret = regulator_enable(mmc->supply.vqmmc);
3465 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3466 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003467 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3468 SDHCI_SUPPORT_SDR50 |
3469 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003470 if (ret) {
3471 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3472 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003473 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003474 }
Kevin Liu8363c372012-11-17 17:55:51 -05003475 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003476
Adrian Hunter28da3582016-06-29 16:24:17 +03003477 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3478 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3479 SDHCI_SUPPORT_DDR50);
3480 }
Daniel Drake6a661802012-11-25 13:01:19 -05003481
Al Cooper4188bba2012-03-16 15:54:17 -04003482 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003483 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3484 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303485 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3486
3487 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003488 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303489 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003490 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3491 * field can be promoted to support HS200.
3492 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003493 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003494 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003495 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303496 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003497 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303498
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003499 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003500 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003501 mmc->caps2 |= MMC_CAP2_HS400;
3502
Adrian Hunter549c0b12014-11-06 15:19:05 +02003503 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3504 (IS_ERR(mmc->supply.vqmmc) ||
3505 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3506 1300000)))
3507 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3508
Adrian Hunter28da3582016-06-29 16:24:17 +03003509 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3510 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303511 mmc->caps |= MMC_CAP_UHS_DDR50;
3512
Girish K S069c9f12012-01-06 09:56:39 +05303513 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003514 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303515 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3516
Arindam Nathd6d50a12011-05-05 12:18:59 +05303517 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003518 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303519 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003520 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303521 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003522 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303523 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3524
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303525 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003526 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3527 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303528
3529 /*
3530 * In case Re-tuning Timer is not disabled, the actual value of
3531 * re-tuning timer will be 2 ^ (n - 1).
3532 */
3533 if (host->tuning_count)
3534 host->tuning_count = 1 << (host->tuning_count - 1);
3535
3536 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003537 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303538 SDHCI_RETUNING_MODE_SHIFT;
3539
Takashi Iwai8f230f42010-12-08 10:04:30 +01003540 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003541
Arindam Nathf2119df2011-05-05 12:18:57 +05303542 /*
3543 * According to SD Host Controller spec v3.00, if the Host System
3544 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3545 * the value is meaningful only if Voltage Support in the Capabilities
3546 * register is set. The actual current value is 4 times the register
3547 * value.
3548 */
3549 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003550 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003551 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003552 if (curr > 0) {
3553
3554 /* convert to SDHCI_MAX_CURRENT format */
3555 curr = curr/1000; /* convert to mA */
3556 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3557
3558 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3559 max_current_caps =
3560 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3561 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3562 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3563 }
3564 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303565
Adrian Hunter28da3582016-06-29 16:24:17 +03003566 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003567 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303568
Aaron Lu55c46652012-07-04 13:31:48 +08003569 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303570 SDHCI_MAX_CURRENT_330_MASK) >>
3571 SDHCI_MAX_CURRENT_330_SHIFT) *
3572 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303573 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003574 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003575 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303576
Aaron Lu55c46652012-07-04 13:31:48 +08003577 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303578 SDHCI_MAX_CURRENT_300_MASK) >>
3579 SDHCI_MAX_CURRENT_300_SHIFT) *
3580 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303581 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003582 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003583 ocr_avail |= MMC_VDD_165_195;
3584
Aaron Lu55c46652012-07-04 13:31:48 +08003585 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303586 SDHCI_MAX_CURRENT_180_MASK) >>
3587 SDHCI_MAX_CURRENT_180_SHIFT) *
3588 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303589 }
3590
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003591 /* If OCR set by host, use it instead. */
3592 if (host->ocr_mask)
3593 ocr_avail = host->ocr_mask;
3594
3595 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003596 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003597 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003598
Takashi Iwai8f230f42010-12-08 10:04:30 +01003599 mmc->ocr_avail = ocr_avail;
3600 mmc->ocr_avail_sdio = ocr_avail;
3601 if (host->ocr_avail_sdio)
3602 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3603 mmc->ocr_avail_sd = ocr_avail;
3604 if (host->ocr_avail_sd)
3605 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3606 else /* normal SD controllers don't support 1.8V */
3607 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3608 mmc->ocr_avail_mmc = ocr_avail;
3609 if (host->ocr_avail_mmc)
3610 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003611
3612 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003613 pr_err("%s: Hardware doesn't report any support voltages.\n",
3614 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003615 ret = -ENODEV;
3616 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003617 }
3618
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003619 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3620 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3621 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3622 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3623 host->flags |= SDHCI_SIGNALING_180;
3624
3625 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3626 host->flags |= SDHCI_SIGNALING_120;
3627
Pierre Ossmand129bce2006-03-24 03:18:17 -08003628 spin_lock_init(&host->lock);
3629
3630 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003631 * Maximum number of segments. Depends on if the hardware
3632 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003633 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003634 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003635 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003636 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003637 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003638 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003639 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003640
3641 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003642 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3643 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3644 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003645 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003646 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003647
3648 /*
3649 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003650 * of bytes. When doing hardware scatter/gather, each entry cannot
3651 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003652 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003653 if (host->flags & SDHCI_USE_ADMA) {
3654 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3655 mmc->max_seg_size = 65535;
3656 else
3657 mmc->max_seg_size = 65536;
3658 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003659 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003660 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003661
3662 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003663 * Maximum block size. This varies from controller to controller and
3664 * is specified in the capabilities register.
3665 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003666 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3667 mmc->max_blk_size = 2;
3668 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003669 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003670 SDHCI_MAX_BLOCK_SHIFT;
3671 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003672 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3673 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003674 mmc->max_blk_size = 0;
3675 }
3676 }
3677
3678 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003679
3680 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003681 * Maximum block count.
3682 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003683 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003684
Adrian Hunter52f53362016-06-29 16:24:15 +03003685 return 0;
3686
3687unreg:
3688 if (!IS_ERR(mmc->supply.vqmmc))
3689 regulator_disable(mmc->supply.vqmmc);
3690undma:
3691 if (host->align_buffer)
3692 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3693 host->adma_table_sz, host->align_buffer,
3694 host->align_addr);
3695 host->adma_table = NULL;
3696 host->align_buffer = NULL;
3697
3698 return ret;
3699}
3700EXPORT_SYMBOL_GPL(sdhci_setup_host);
3701
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003702void sdhci_cleanup_host(struct sdhci_host *host)
3703{
3704 struct mmc_host *mmc = host->mmc;
3705
3706 if (!IS_ERR(mmc->supply.vqmmc))
3707 regulator_disable(mmc->supply.vqmmc);
3708
3709 if (host->align_buffer)
3710 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3711 host->adma_table_sz, host->align_buffer,
3712 host->align_addr);
3713 host->adma_table = NULL;
3714 host->align_buffer = NULL;
3715}
3716EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
3717
Adrian Hunter52f53362016-06-29 16:24:15 +03003718int __sdhci_add_host(struct sdhci_host *host)
3719{
3720 struct mmc_host *mmc = host->mmc;
3721 int ret;
3722
Pierre Ossman55db8902006-11-21 17:55:45 +01003723 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003724 * Init tasklets.
3725 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003726 tasklet_init(&host->finish_tasklet,
3727 sdhci_tasklet_finish, (unsigned long)host);
3728
Al Viroe4cad1b2006-10-10 22:47:07 +01003729 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003730 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3731 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003732
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003733 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303734
Shawn Guo2af502c2013-07-05 14:38:55 +08003735 sdhci_init(host, 0);
3736
Russell King781e9892014-04-25 12:55:46 +01003737 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3738 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003739 if (ret) {
3740 pr_err("%s: Failed to request IRQ %d: %d\n",
3741 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003742 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003743 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003744
Pierre Ossmand129bce2006-03-24 03:18:17 -08003745#ifdef CONFIG_MMC_DEBUG
3746 sdhci_dumpregs(host);
3747#endif
3748
Adrian Hunter061d17a2016-04-12 14:25:09 +03003749 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003750 if (ret) {
3751 pr_err("%s: Failed to register LED device: %d\n",
3752 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003753 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003754 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003755
Pierre Ossman5f25a662006-10-04 02:15:39 -07003756 mmiowb();
3757
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003758 ret = mmc_add_host(mmc);
3759 if (ret)
3760 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003761
Girish K Sa3c76eb2011-10-11 11:44:09 +05303762 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003763 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003764 (host->flags & SDHCI_USE_ADMA) ?
3765 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003766 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003767
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003768 sdhci_enable_card_detection(host);
3769
Pierre Ossmand129bce2006-03-24 03:18:17 -08003770 return 0;
3771
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003772unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003773 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003774unirq:
Russell King03231f92014-04-25 12:57:12 +01003775 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003776 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3777 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003778 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003779untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003780 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003781
Pierre Ossmand129bce2006-03-24 03:18:17 -08003782 return ret;
3783}
Adrian Hunter52f53362016-06-29 16:24:15 +03003784EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003785
Adrian Hunter52f53362016-06-29 16:24:15 +03003786int sdhci_add_host(struct sdhci_host *host)
3787{
3788 int ret;
3789
3790 ret = sdhci_setup_host(host);
3791 if (ret)
3792 return ret;
3793
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003794 ret = __sdhci_add_host(host);
3795 if (ret)
3796 goto cleanup;
3797
3798 return 0;
3799
3800cleanup:
3801 sdhci_cleanup_host(host);
3802
3803 return ret;
Adrian Hunter52f53362016-06-29 16:24:15 +03003804}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003805EXPORT_SYMBOL_GPL(sdhci_add_host);
3806
Pierre Ossman1e728592008-04-16 19:13:13 +02003807void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003808{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003809 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003810 unsigned long flags;
3811
3812 if (dead) {
3813 spin_lock_irqsave(&host->lock, flags);
3814
3815 host->flags |= SDHCI_DEVICE_DEAD;
3816
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003817 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303818 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003819 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003820 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003821 }
3822
3823 spin_unlock_irqrestore(&host->lock, flags);
3824 }
3825
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003826 sdhci_disable_card_detection(host);
3827
Markus Mayer4e743f12014-07-03 13:27:42 -07003828 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003829
Adrian Hunter061d17a2016-04-12 14:25:09 +03003830 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003831
Pierre Ossman1e728592008-04-16 19:13:13 +02003832 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003833 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003834
Russell Kingb537f942014-04-25 12:56:01 +01003835 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3836 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003837 free_irq(host->irq, host);
3838
3839 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003840 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003841
Pierre Ossmand129bce2006-03-24 03:18:17 -08003842 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003843
Tim Kryger3a48edc2014-06-13 10:13:56 -07003844 if (!IS_ERR(mmc->supply.vqmmc))
3845 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003846
Russell Kingedd63fc2016-01-26 13:39:50 +00003847 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003848 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3849 host->adma_table_sz, host->align_buffer,
3850 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003851
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003852 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003853 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003854}
3855
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003856EXPORT_SYMBOL_GPL(sdhci_remove_host);
3857
3858void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003859{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003860 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003861}
3862
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003863EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003864
3865/*****************************************************************************\
3866 * *
3867 * Driver init/exit *
3868 * *
3869\*****************************************************************************/
3870
3871static int __init sdhci_drv_init(void)
3872{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303873 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003874 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303875 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003876
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003877 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003878}
3879
3880static void __exit sdhci_drv_exit(void)
3881{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003882}
3883
3884module_init(sdhci_drv_init);
3885module_exit(sdhci_drv_exit);
3886
Pierre Ossmandf673b22006-06-30 02:22:31 -07003887module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003888module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003889
Pierre Ossman32710e82009-04-08 20:14:54 +02003890MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003891MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003892MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003893
Pierre Ossmandf673b22006-06-30 02:22:31 -07003894MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003895MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");