blob: a26c3996d78d501efc62d04b8a5e044266136f17 [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>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080025
Pierre Ossman2f730fe2008-03-17 10:29:38 +010026#include <linux/leds.h>
27
Aries Lee22113ef2010-12-15 08:14:24 +010028#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080030#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080031#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080032#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033
Pierre Ossmand129bce2006-03-24 03:18:17 -080034#include "sdhci.h"
35
36#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010039 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Arindam Nathb513ea22011-05-05 12:19:04 +053041#define MAX_TUNING_LOOP 40
42
Pierre Ossmandf673b22006-06-30 02:22:31 -070043static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030044static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070045
Pierre Ossmand129bce2006-03-24 03:18:17 -080046static void sdhci_finish_data(struct sdhci_host *);
47
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_command(struct sdhci_host *);
Girish K S069c9f12012-01-06 09:56:39 +053049static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode);
Kevin Liu52983382013-01-31 11:31:37 +080050static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Scott Branden04e079cf2015-03-10 11:35:10 -070051static int sdhci_do_get_cd(struct sdhci_host *host);
Pierre Ossmand129bce2006-03-24 03:18:17 -080052
53static void sdhci_dumpregs(struct sdhci_host *host)
54{
Girish K Sa3c76eb2011-10-11 11:44:09 +053055 pr_debug(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
Philip Rakity412ab652010-09-22 15:25:13 -070056 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080057
Girish K Sa3c76eb2011-10-11 11:44:09 +053058 pr_debug(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030059 sdhci_readl(host, SDHCI_DMA_ADDRESS),
60 sdhci_readw(host, SDHCI_HOST_VERSION));
Girish K Sa3c76eb2011-10-11 11:44:09 +053061 pr_debug(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030062 sdhci_readw(host, SDHCI_BLOCK_SIZE),
63 sdhci_readw(host, SDHCI_BLOCK_COUNT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053064 pr_debug(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030065 sdhci_readl(host, SDHCI_ARGUMENT),
66 sdhci_readw(host, SDHCI_TRANSFER_MODE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053067 pr_debug(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030068 sdhci_readl(host, SDHCI_PRESENT_STATE),
69 sdhci_readb(host, SDHCI_HOST_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053070 pr_debug(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030071 sdhci_readb(host, SDHCI_POWER_CONTROL),
72 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053073 pr_debug(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030074 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
75 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
Girish K Sa3c76eb2011-10-11 11:44:09 +053076 pr_debug(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030077 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
78 sdhci_readl(host, SDHCI_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053079 pr_debug(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030080 sdhci_readl(host, SDHCI_INT_ENABLE),
81 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
Girish K Sa3c76eb2011-10-11 11:44:09 +053082 pr_debug(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030083 sdhci_readw(host, SDHCI_ACMD12_ERR),
84 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
Girish K Sa3c76eb2011-10-11 11:44:09 +053085 pr_debug(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030086 sdhci_readl(host, SDHCI_CAPABILITIES),
Philip Rakitye8120ad2010-11-30 00:55:23 -050087 sdhci_readl(host, SDHCI_CAPABILITIES_1));
Girish K Sa3c76eb2011-10-11 11:44:09 +053088 pr_debug(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
Philip Rakitye8120ad2010-11-30 00:55:23 -050089 sdhci_readw(host, SDHCI_COMMAND),
Anton Vorontsov4e4141a2009-03-17 00:13:46 +030090 sdhci_readl(host, SDHCI_MAX_CURRENT));
Girish K Sa3c76eb2011-10-11 11:44:09 +053091 pr_debug(DRIVER_NAME ": Host ctl2: 0x%08x\n",
Arindam Nathf2119df2011-05-05 12:18:57 +053092 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080093
Adrian Huntere57a5f62014-11-04 12:42:46 +020094 if (host->flags & SDHCI_USE_ADMA) {
95 if (host->flags & SDHCI_USE_64_BIT_DMA)
96 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
97 readl(host->ioaddr + SDHCI_ADMA_ERROR),
98 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
99 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
100 else
101 pr_debug(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
102 readl(host->ioaddr + SDHCI_ADMA_ERROR),
103 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
104 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100105
Girish K Sa3c76eb2011-10-11 11:44:09 +0530106 pr_debug(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800107}
108
109/*****************************************************************************\
110 * *
111 * Low level functions *
112 * *
113\*****************************************************************************/
114
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300115static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
116{
Russell King5b4f1f62014-04-25 12:57:02 +0100117 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300118
Adrian Hunterc79396c2011-12-27 15:48:42 +0200119 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Daniel Drake87b87a32012-04-10 00:14:20 +0100120 (host->mmc->caps & MMC_CAP_NONREMOVABLE))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300121 return;
122
Russell King5b4f1f62014-04-25 12:57:02 +0100123 if (enable) {
124 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
125 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800126
Russell King5b4f1f62014-04-25 12:57:02 +0100127 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
128 SDHCI_INT_CARD_INSERT;
129 } else {
130 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
131 }
Russell Kingb537f942014-04-25 12:56:01 +0100132
133 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
134 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300135}
136
137static void sdhci_enable_card_detection(struct sdhci_host *host)
138{
139 sdhci_set_card_detection(host, true);
140}
141
142static void sdhci_disable_card_detection(struct sdhci_host *host)
143{
144 sdhci_set_card_detection(host, false);
145}
146
Ulf Hansson02d0b682016-04-11 15:32:41 +0200147static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
148{
149 if (host->bus_on)
150 return;
151 host->bus_on = true;
152 pm_runtime_get_noresume(host->mmc->parent);
153}
154
155static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
156{
157 if (!host->bus_on)
158 return;
159 host->bus_on = false;
160 pm_runtime_put_noidle(host->mmc->parent);
161}
162
Russell King03231f92014-04-25 12:57:12 +0100163void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800164{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700165 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800166
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300167 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800168
Adrian Hunterf0710a52013-05-06 12:17:32 +0300169 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800170 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300171 /* Reset-all turns off SD Bus Power */
172 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
173 sdhci_runtime_pm_bus_off(host);
174 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800175
Pierre Ossmane16514d82006-06-30 02:22:24 -0700176 /* Wait max 100 ms */
177 timeout = 100;
178
179 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700181 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530182 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700183 mmc_hostname(host->mmc), (int)mask);
184 sdhci_dumpregs(host);
185 return;
186 }
187 timeout--;
188 mdelay(1);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800189 }
Russell King03231f92014-04-25 12:57:12 +0100190}
191EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300192
Russell King03231f92014-04-25 12:57:12 +0100193static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
194{
195 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Ivan T. Ivanov135b0a22015-07-06 15:16:21 +0300196 if (!sdhci_do_get_cd(host))
Russell King03231f92014-04-25 12:57:12 +0100197 return;
198 }
199
200 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800201
Russell Kingda91a8f2014-04-25 13:00:12 +0100202 if (mask & SDHCI_RESET_ALL) {
203 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
204 if (host->ops->enable_dma)
205 host->ops->enable_dma(host);
206 }
207
208 /* Resetting the controller clears many */
209 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800210 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800211}
212
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800213static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios);
214
215static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800217 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100218 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800219 else
Russell King03231f92014-04-25 12:57:12 +0100220 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800221
Russell Kingb537f942014-04-25 12:56:01 +0100222 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
223 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
224 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
225 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
226 SDHCI_INT_RESPONSE;
227
228 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
229 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800230
231 if (soft) {
232 /* force clock reconfiguration */
233 host->clock = 0;
234 sdhci_set_ios(host->mmc, &host->mmc->ios);
235 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300236}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800237
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300238static void sdhci_reinit(struct sdhci_host *host)
239{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800240 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300241 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800242}
243
Adrian Hunter061d17a2016-04-12 14:25:09 +0300244static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800245{
246 u8 ctrl;
247
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300248 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800249 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300250 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251}
252
Adrian Hunter061d17a2016-04-12 14:25:09 +0300253static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254{
255 u8 ctrl;
256
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300257 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300259 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260}
261
Adrian Hunter061d17a2016-04-12 14:25:09 +0300262#if defined(CONFIG_LEDS_CLASS) || (defined(CONFIG_LEDS_CLASS_MODULE) && \
263 defined(CONFIG_MMC_SDHCI_MODULE))
264
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100265static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300266 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100267{
268 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
269 unsigned long flags;
270
271 spin_lock_irqsave(&host->lock, flags);
272
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300273 if (host->runtime_suspended)
274 goto out;
275
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300277 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100278 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300279 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300280out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100281 spin_unlock_irqrestore(&host->lock, flags);
282}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300283
284static int sdhci_led_register(struct sdhci_host *host)
285{
286 struct mmc_host *mmc = host->mmc;
287
288 snprintf(host->led_name, sizeof(host->led_name),
289 "%s::", mmc_hostname(mmc));
290
291 host->led.name = host->led_name;
292 host->led.brightness = LED_OFF;
293 host->led.default_trigger = mmc_hostname(mmc);
294 host->led.brightness_set = sdhci_led_control;
295
296 return led_classdev_register(mmc_dev(mmc), &host->led);
297}
298
299static void sdhci_led_unregister(struct sdhci_host *host)
300{
301 led_classdev_unregister(&host->led);
302}
303
304static inline void sdhci_led_activate(struct sdhci_host *host)
305{
306}
307
308static inline void sdhci_led_deactivate(struct sdhci_host *host)
309{
310}
311
312#else
313
314static inline int sdhci_led_register(struct sdhci_host *host)
315{
316 return 0;
317}
318
319static inline void sdhci_led_unregister(struct sdhci_host *host)
320{
321}
322
323static inline void sdhci_led_activate(struct sdhci_host *host)
324{
325 __sdhci_led_activate(host);
326}
327
328static inline void sdhci_led_deactivate(struct sdhci_host *host)
329{
330 __sdhci_led_deactivate(host);
331}
332
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100333#endif
334
Pierre Ossmand129bce2006-03-24 03:18:17 -0800335/*****************************************************************************\
336 * *
337 * Core functions *
338 * *
339\*****************************************************************************/
340
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100341static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342{
Pierre Ossman76591502008-07-21 00:32:11 +0200343 unsigned long flags;
344 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700345 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200346 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100350 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200351 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352
Pierre Ossman76591502008-07-21 00:32:11 +0200353 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300356 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 blksize -= len;
361 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 while (len) {
366 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300367 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200368 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800369 }
Pierre Ossman76591502008-07-21 00:32:11 +0200370
371 *buf = scratch & 0xFF;
372
373 buf++;
374 scratch >>= 8;
375 chunk--;
376 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800377 }
378 }
Pierre Ossman76591502008-07-21 00:32:11 +0200379
380 sg_miter_stop(&host->sg_miter);
381
382 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800384
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100385static void sdhci_write_block_pio(struct sdhci_host *host)
386{
Pierre Ossman76591502008-07-21 00:32:11 +0200387 unsigned long flags;
388 size_t blksize, len, chunk;
389 u32 scratch;
390 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100391
392 DBG("PIO writing\n");
393
394 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200395 chunk = 0;
396 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397
Pierre Ossman76591502008-07-21 00:32:11 +0200398 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399
400 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300401 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100402
Pierre Ossman76591502008-07-21 00:32:11 +0200403 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200404
Pierre Ossman76591502008-07-21 00:32:11 +0200405 blksize -= len;
406 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407
Pierre Ossman76591502008-07-21 00:32:11 +0200408 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 while (len) {
411 scratch |= (u32)*buf << (chunk * 8);
412
413 buf++;
414 chunk++;
415 len--;
416
417 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300418 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200419 chunk = 0;
420 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100421 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100422 }
423 }
Pierre Ossman76591502008-07-21 00:32:11 +0200424
425 sg_miter_stop(&host->sg_miter);
426
427 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428}
429
430static void sdhci_transfer_pio(struct sdhci_host *host)
431{
432 u32 mask;
433
434 BUG_ON(!host->data);
435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437 return;
438
439 if (host->data->flags & MMC_DATA_READ)
440 mask = SDHCI_DATA_AVAILABLE;
441 else
442 mask = SDHCI_SPACE_AVAILABLE;
443
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200444 /*
445 * Some controllers (JMicron JMB38x) mess up the buffer bits
446 * for transfers < 4 bytes. As long as it is just one block,
447 * we can ignore the bits.
448 */
449 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
450 (host->data->blocks == 1))
451 mask = ~0;
452
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300453 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300454 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
455 udelay(100);
456
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100457 if (host->data->flags & MMC_DATA_READ)
458 sdhci_read_block_pio(host);
459 else
460 sdhci_write_block_pio(host);
461
Pierre Ossman76591502008-07-21 00:32:11 +0200462 host->blocks--;
463 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100464 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100465 }
466
467 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800468}
469
Russell King48857d92016-01-26 13:40:16 +0000470static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000471 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000472{
473 int sg_count;
474
Russell King94538e52016-01-26 13:40:37 +0000475 /*
476 * If the data buffers are already mapped, return the previous
477 * dma_map_sg() result.
478 */
479 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000480 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000481
482 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
483 data->flags & MMC_DATA_WRITE ?
484 DMA_TO_DEVICE : DMA_FROM_DEVICE);
485
486 if (sg_count == 0)
487 return -ENOSPC;
488
489 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000490 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000491
492 return sg_count;
493}
494
Pierre Ossman2134a922008-06-28 18:28:51 +0200495static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
496{
497 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800498 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200499}
500
501static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
502{
Cong Wang482fce92011-11-27 13:27:00 +0800503 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200504 local_irq_restore(*flags);
505}
506
Adrian Huntere57a5f62014-11-04 12:42:46 +0200507static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
508 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800509{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200510 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800511
Adrian Huntere57a5f62014-11-04 12:42:46 +0200512 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200513 dma_desc->cmd = cpu_to_le16(cmd);
514 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200515 dma_desc->addr_lo = cpu_to_le32((u32)addr);
516
517 if (host->flags & SDHCI_USE_64_BIT_DMA)
518 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800519}
520
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200521static void sdhci_adma_mark_end(void *desc)
522{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200523 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200524
Adrian Huntere57a5f62014-11-04 12:42:46 +0200525 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200526 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200527}
528
Russell King60c64762016-01-26 13:40:22 +0000529static void sdhci_adma_table_pre(struct sdhci_host *host,
530 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200531{
Pierre Ossman2134a922008-06-28 18:28:51 +0200532 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200533 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000534 dma_addr_t addr, align_addr;
535 void *desc, *align;
536 char *buffer;
537 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200538
539 /*
540 * The spec does not specify endianness of descriptor table.
541 * We currently guess that it is LE.
542 */
543
Russell King60c64762016-01-26 13:40:22 +0000544 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200546 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 align = host->align_buffer;
548
549 align_addr = host->align_addr;
550
551 for_each_sg(data->sg, sg, host->sg_count, i) {
552 addr = sg_dma_address(sg);
553 len = sg_dma_len(sg);
554
555 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000556 * The SDHCI specification states that ADMA addresses must
557 * be 32-bit aligned. If they aren't, then we use a bounce
558 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200559 * alignment.
560 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200561 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
562 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200563 if (offset) {
564 if (data->flags & MMC_DATA_WRITE) {
565 buffer = sdhci_kmap_atomic(sg, &flags);
566 memcpy(align, buffer, offset);
567 sdhci_kunmap_atomic(buffer, &flags);
568 }
569
Ben Dooks118cd172010-03-05 13:43:26 -0800570 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200571 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200572 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200573
574 BUG_ON(offset > 65536);
575
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200576 align += SDHCI_ADMA2_ALIGN;
577 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
Adrian Hunter76fe3792014-11-04 12:42:42 +0200579 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200580
581 addr += offset;
582 len -= offset;
583 }
584
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 BUG_ON(len > 65536);
586
Adrian Hunter347ea322015-11-26 14:00:48 +0200587 if (len) {
588 /* tran, valid */
589 sdhci_adma_write_desc(host, desc, addr, len,
590 ADMA2_TRAN_VALID);
591 desc += host->desc_sz;
592 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200593
594 /*
595 * If this triggers then we have a calculation bug
596 * somewhere. :/
597 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200598 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200599 }
600
Thomas Abraham70764a92010-05-26 14:42:04 -0700601 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000602 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200603 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200604 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200605 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700606 }
607 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000608 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200609 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700610 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200611}
612
613static void sdhci_adma_table_post(struct sdhci_host *host,
614 struct mmc_data *data)
615{
Pierre Ossman2134a922008-06-28 18:28:51 +0200616 struct scatterlist *sg;
617 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200618 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200619 char *buffer;
620 unsigned long flags;
621
Russell King47fa9612016-01-26 13:40:06 +0000622 if (data->flags & MMC_DATA_READ) {
623 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100624
Russell King47fa9612016-01-26 13:40:06 +0000625 /* Do a quick scan of the SG list for any unaligned mappings */
626 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200627 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000628 has_unaligned = true;
629 break;
630 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200631
Russell King47fa9612016-01-26 13:40:06 +0000632 if (has_unaligned) {
633 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000634 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200635
Russell King47fa9612016-01-26 13:40:06 +0000636 align = host->align_buffer;
637
638 for_each_sg(data->sg, sg, host->sg_count, i) {
639 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
640 size = SDHCI_ADMA2_ALIGN -
641 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
642
643 buffer = sdhci_kmap_atomic(sg, &flags);
644 memcpy(buffer, align, size);
645 sdhci_kunmap_atomic(buffer, &flags);
646
647 align += SDHCI_ADMA2_ALIGN;
648 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200649 }
650 }
651 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200652}
653
Andrei Warkentina3c77782011-04-11 16:13:42 -0500654static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800655{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700656 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500657 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700658 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800659
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200660 /*
661 * If the host controller provides us with an incorrect timeout
662 * value, just skip the check and use 0xE. The hardware may take
663 * longer to time out, but that's much better than having a too-short
664 * timeout value.
665 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200666 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200667 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200668
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100670 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800672
Andrei Warkentina3c77782011-04-11 16:13:42 -0500673 /* timeout in us */
674 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100675 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300676 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000677 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000678 if (host->clock && data->timeout_clks) {
679 unsigned long long val;
680
681 /*
682 * data->timeout_clks is in units of clock cycles.
683 * host->clock is in Hz. target_timeout is in us.
684 * Hence, us = 1000000 * cycles / Hz. Round up.
685 */
686 val = 1000000 * data->timeout_clks;
687 if (do_div(val, host->clock))
688 target_timeout++;
689 target_timeout += val;
690 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300691 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700692
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700693 /*
694 * Figure out needed cycles.
695 * We do this in steps in order to fit inside a 32 bit int.
696 * The first step is the minimum timeout, which will have a
697 * minimum resolution of 6 bits:
698 * (1) 2^13*1000 > 2^22,
699 * (2) host->timeout_clk < 2^16
700 * =>
701 * (1) / (2) > 2^6
702 */
703 count = 0;
704 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
705 while (current_timeout < target_timeout) {
706 count++;
707 current_timeout <<= 1;
708 if (count >= 0xF)
709 break;
710 }
711
712 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400713 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
714 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700715 count = 0xE;
716 }
717
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200718 return count;
719}
720
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300721static void sdhci_set_transfer_irqs(struct sdhci_host *host)
722{
723 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
724 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
725
726 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100727 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300728 else
Russell Kingb537f942014-04-25 12:56:01 +0100729 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
730
731 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
732 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300733}
734
Aisheng Dongb45e6682014-08-27 15:26:29 +0800735static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200736{
737 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800738
739 if (host->ops->set_timeout) {
740 host->ops->set_timeout(host, cmd);
741 } else {
742 count = sdhci_calc_timeout(host, cmd);
743 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
744 }
745}
746
747static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
748{
Pierre Ossman2134a922008-06-28 18:28:51 +0200749 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500750 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200751
752 WARN_ON(host->data);
753
Aisheng Dongb45e6682014-08-27 15:26:29 +0800754 if (data || (cmd->flags & MMC_RSP_BUSY))
755 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500756
757 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200758 return;
759
760 /* Sanity checks */
761 BUG_ON(data->blksz * data->blocks > 524288);
762 BUG_ON(data->blksz > host->mmc->max_blk_size);
763 BUG_ON(data->blocks > 65535);
764
765 host->data = data;
766 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400767 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200768
Russell Kingfce14422016-01-26 13:41:20 +0000769 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200770 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000771 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000772 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200773
Russell Kingfce14422016-01-26 13:41:20 +0000774 host->flags |= SDHCI_REQ_USE_DMA;
775
776 /*
777 * FIXME: This doesn't account for merging when mapping the
778 * scatterlist.
779 *
780 * The assumption here being that alignment and lengths are
781 * the same after DMA mapping to device address space.
782 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000783 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000784 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200785 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000786 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000787 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000788 /*
789 * As we use up to 3 byte chunks to work
790 * around alignment problems, we need to
791 * check the offset as well.
792 */
793 offset_mask = 3;
794 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200795 } else {
796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000797 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000798 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
799 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 }
801
Russell Kingdf953922016-01-26 13:41:14 +0000802 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200803 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000804 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100805 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000806 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 host->flags &= ~SDHCI_REQ_USE_DMA;
808 break;
809 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000810 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100811 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 host->flags &= ~SDHCI_REQ_USE_DMA;
813 break;
814 }
815 }
816 }
817 }
818
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000820 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200821
Russell King60c64762016-01-26 13:40:22 +0000822 if (sg_cnt <= 0) {
823 /*
824 * This only happens when someone fed
825 * us an invalid request.
826 */
827 WARN_ON(1);
828 host->flags &= ~SDHCI_REQ_USE_DMA;
829 } else if (host->flags & SDHCI_USE_ADMA) {
830 sdhci_adma_table_pre(host, data, sg_cnt);
831
832 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
833 if (host->flags & SDHCI_USE_64_BIT_DMA)
834 sdhci_writel(host,
835 (u64)host->adma_addr >> 32,
836 SDHCI_ADMA_ADDRESS_HI);
837 } else {
838 WARN_ON(sg_cnt != 1);
839 sdhci_writel(host, sg_dma_address(data->sg),
840 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 }
842 }
843
Pierre Ossman2134a922008-06-28 18:28:51 +0200844 /*
845 * Always adjust the DMA selection as some controllers
846 * (e.g. JMicron) can't do PIO properly when the selection
847 * is ADMA.
848 */
849 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300850 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200851 ctrl &= ~SDHCI_CTRL_DMA_MASK;
852 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200853 (host->flags & SDHCI_USE_ADMA)) {
854 if (host->flags & SDHCI_USE_64_BIT_DMA)
855 ctrl |= SDHCI_CTRL_ADMA64;
856 else
857 ctrl |= SDHCI_CTRL_ADMA32;
858 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200859 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200860 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300861 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100862 }
863
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200864 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200865 int flags;
866
867 flags = SG_MITER_ATOMIC;
868 if (host->data->flags & MMC_DATA_READ)
869 flags |= SG_MITER_TO_SG;
870 else
871 flags |= SG_MITER_FROM_SG;
872 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200873 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800874 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700875
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300876 sdhci_set_transfer_irqs(host);
877
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400878 /* Set the DMA boundary value and block size */
879 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
880 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300881 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700882}
883
884static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500885 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700886{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800887 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500888 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889
Dong Aisheng2b558c12013-10-30 22:09:48 +0800890 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800891 if (host->quirks2 &
892 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
893 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
894 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800895 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800896 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
897 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800898 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800899 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800901 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700902
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200903 WARN_ON(!host->data);
904
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800905 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
906 mode = SDHCI_TRNS_BLK_CNT_EN;
907
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800909 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500910 /*
911 * If we are sending CMD23, CMD12 never gets sent
912 * on successful completion (so no Auto-CMD12).
913 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800914 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
915 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500916 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500917 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
918 mode |= SDHCI_TRNS_AUTO_CMD23;
919 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
920 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700921 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500922
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923 if (data->flags & MMC_DATA_READ)
924 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100925 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700926 mode |= SDHCI_TRNS_DMA;
927
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300928 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800929}
930
931static void sdhci_finish_data(struct sdhci_host *host)
932{
933 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800934
935 BUG_ON(!host->data);
936
937 data = host->data;
938 host->data = NULL;
939
Russell Kingadd89132016-01-26 13:40:42 +0000940 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
941 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
942 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800943
944 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200945 * The specification states that the block count register must
946 * be updated, but it does not specify at what point in the
947 * data flow. That makes the register entirely useless to read
948 * back so we have to assume that nothing made it to the card
949 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200951 if (data->error)
952 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200954 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800955
Andrei Warkentine89d4562011-05-23 15:06:37 -0500956 /*
957 * Need to send CMD12 if -
958 * a) open-ended multiblock transfer (no CMD23)
959 * b) error in multiblock transfer
960 */
961 if (data->stop &&
962 (data->error ||
963 !host->mrq->sbc)) {
964
Pierre Ossmand129bce2006-03-24 03:18:17 -0800965 /*
966 * The controller needs a reset of internal state machines
967 * upon error conditions.
968 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200969 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100970 sdhci_do_reset(host, SDHCI_RESET_CMD);
971 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800972 }
973
974 sdhci_send_command(host, data->stop);
975 } else
976 tasklet_schedule(&host->finish_tasklet);
977}
978
Dong Aishengc0e551292013-09-13 19:11:31 +0800979void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800980{
981 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700982 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700983 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800984
985 WARN_ON(host->cmd);
986
Russell King96776202016-01-26 13:39:34 +0000987 /* Initially, a command has no error */
988 cmd->error = 0;
989
Pierre Ossmand129bce2006-03-24 03:18:17 -0800990 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700991 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700992
993 mask = SDHCI_CMD_INHIBIT;
994 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
995 mask |= SDHCI_DATA_INHIBIT;
996
997 /* We shouldn't wait for data inihibit for stop commands, even
998 though they might use busy signaling */
999 if (host->mrq->data && (cmd == host->mrq->data->stop))
1000 mask &= ~SDHCI_DATA_INHIBIT;
1001
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001002 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001003 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001004 pr_err("%s: Controller never released inhibit bit(s).\n",
1005 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001007 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001008 tasklet_schedule(&host->finish_tasklet);
1009 return;
1010 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001011 timeout--;
1012 mdelay(1);
1013 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001015 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001016 if (!cmd->data && cmd->busy_timeout > 9000)
1017 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001018 else
1019 timeout += 10 * HZ;
1020 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021
1022 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001023 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024
Andrei Warkentina3c77782011-04-11 16:13:42 -05001025 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001027 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028
Andrei Warkentine89d4562011-05-23 15:06:37 -05001029 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001030
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301032 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001034 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 tasklet_schedule(&host->finish_tasklet);
1036 return;
1037 }
1038
1039 if (!(cmd->flags & MMC_RSP_PRESENT))
1040 flags = SDHCI_CMD_RESP_NONE;
1041 else if (cmd->flags & MMC_RSP_136)
1042 flags = SDHCI_CMD_RESP_LONG;
1043 else if (cmd->flags & MMC_RSP_BUSY)
1044 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1045 else
1046 flags = SDHCI_CMD_RESP_SHORT;
1047
1048 if (cmd->flags & MMC_RSP_CRC)
1049 flags |= SDHCI_CMD_CRC;
1050 if (cmd->flags & MMC_RSP_OPCODE)
1051 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301052
1053 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301054 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1055 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001056 flags |= SDHCI_CMD_DATA;
1057
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001058 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059}
Dong Aishengc0e551292013-09-13 19:11:31 +08001060EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001061
1062static void sdhci_finish_command(struct sdhci_host *host)
1063{
1064 int i;
1065
1066 BUG_ON(host->cmd == NULL);
1067
1068 if (host->cmd->flags & MMC_RSP_PRESENT) {
1069 if (host->cmd->flags & MMC_RSP_136) {
1070 /* CRC is stripped so we need to do some shifting. */
1071 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001072 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001073 SDHCI_RESPONSE + (3-i)*4) << 8;
1074 if (i != 3)
1075 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001076 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001077 SDHCI_RESPONSE + (3-i)*4-1);
1078 }
1079 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001080 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001081 }
1082 }
1083
Andrei Warkentine89d4562011-05-23 15:06:37 -05001084 /* Finished CMD23, now send actual command. */
1085 if (host->cmd == host->mrq->sbc) {
1086 host->cmd = NULL;
1087 sdhci_send_command(host, host->mrq->cmd);
1088 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001089
Andrei Warkentine89d4562011-05-23 15:06:37 -05001090 /* Processed actual command. */
1091 if (host->data && host->data_early)
1092 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001093
Andrei Warkentine89d4562011-05-23 15:06:37 -05001094 if (!host->cmd->data)
1095 tasklet_schedule(&host->finish_tasklet);
1096
1097 host->cmd = NULL;
1098 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001099}
1100
Kevin Liu52983382013-01-31 11:31:37 +08001101static u16 sdhci_get_preset_value(struct sdhci_host *host)
1102{
Russell Kingd975f122014-04-25 12:59:31 +01001103 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001104
Russell Kingd975f122014-04-25 12:59:31 +01001105 switch (host->timing) {
1106 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001107 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1108 break;
Russell Kingd975f122014-04-25 12:59:31 +01001109 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001110 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1111 break;
Russell Kingd975f122014-04-25 12:59:31 +01001112 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001113 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1114 break;
Russell Kingd975f122014-04-25 12:59:31 +01001115 case MMC_TIMING_UHS_SDR104:
1116 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001117 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1118 break;
Russell Kingd975f122014-04-25 12:59:31 +01001119 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001120 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001121 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1122 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001123 case MMC_TIMING_MMC_HS400:
1124 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1125 break;
Kevin Liu52983382013-01-31 11:31:37 +08001126 default:
1127 pr_warn("%s: Invalid UHS-I mode selected\n",
1128 mmc_hostname(host->mmc));
1129 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1130 break;
1131 }
1132 return preset;
1133}
1134
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001135u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1136 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001137{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001139 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301140 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001141 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001142
Zhangfei Gao85105c52010-08-06 07:10:01 +08001143 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001144 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001145 u16 pre_val;
1146
1147 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1148 pre_val = sdhci_get_preset_value(host);
1149 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1150 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1151 if (host->clk_mul &&
1152 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1153 clk = SDHCI_PROG_CLOCK_MODE;
1154 real_div = div + 1;
1155 clk_mul = host->clk_mul;
1156 } else {
1157 real_div = max_t(int, 1, div << 1);
1158 }
1159 goto clock_set;
1160 }
1161
Arindam Nathc3ed3872011-05-05 12:19:06 +05301162 /*
1163 * Check if the Host Controller supports Programmable Clock
1164 * Mode.
1165 */
1166 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001167 for (div = 1; div <= 1024; div++) {
1168 if ((host->max_clk * host->clk_mul / div)
1169 <= clock)
1170 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001171 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001172 if ((host->max_clk * host->clk_mul / div) <= clock) {
1173 /*
1174 * Set Programmable Clock Mode in the Clock
1175 * Control register.
1176 */
1177 clk = SDHCI_PROG_CLOCK_MODE;
1178 real_div = div;
1179 clk_mul = host->clk_mul;
1180 div--;
1181 } else {
1182 /*
1183 * Divisor can be too small to reach clock
1184 * speed requirement. Then use the base clock.
1185 */
1186 switch_base_clk = true;
1187 }
1188 }
1189
1190 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301191 /* Version 3.00 divisors must be a multiple of 2. */
1192 if (host->max_clk <= clock)
1193 div = 1;
1194 else {
1195 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1196 div += 2) {
1197 if ((host->max_clk / div) <= clock)
1198 break;
1199 }
1200 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001201 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301202 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301203 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1204 && !div && host->max_clk <= 25000000)
1205 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001206 }
1207 } else {
1208 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001209 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001210 if ((host->max_clk / div) <= clock)
1211 break;
1212 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001213 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301214 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001215 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001216
Kevin Liu52983382013-01-31 11:31:37 +08001217clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001218 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001219 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301220 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001221 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1222 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001223
1224 return clk;
1225}
1226EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1227
1228void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1229{
1230 u16 clk;
1231 unsigned long timeout;
1232
1233 host->mmc->actual_clock = 0;
1234
1235 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001236
1237 if (clock == 0)
1238 return;
1239
1240 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1241
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001243 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244
Chris Ball27f6cb12009-09-22 16:45:31 -07001245 /* Wait max 20 ms */
1246 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001247 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001248 & SDHCI_CLOCK_INT_STABLE)) {
1249 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001250 pr_err("%s: Internal clock never stabilised.\n",
1251 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001252 sdhci_dumpregs(host);
1253 return;
1254 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001255 timeout--;
1256 mdelay(1);
1257 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001258
1259 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001260 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261}
Russell King17710592014-04-25 12:58:55 +01001262EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001263
Adrian Hunter1dceb042016-03-29 12:45:43 +03001264static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1265 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001266{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001267 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001268
1269 spin_unlock_irq(&host->lock);
1270 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1271 spin_lock_irq(&host->lock);
1272
1273 if (mode != MMC_POWER_OFF)
1274 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1275 else
1276 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1277}
1278
1279void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1280 unsigned short vdd)
1281{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001282 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001283
Russell King24fbb3c2014-04-25 13:00:06 +01001284 if (mode != MMC_POWER_OFF) {
1285 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001286 case MMC_VDD_165_195:
1287 pwr = SDHCI_POWER_180;
1288 break;
1289 case MMC_VDD_29_30:
1290 case MMC_VDD_30_31:
1291 pwr = SDHCI_POWER_300;
1292 break;
1293 case MMC_VDD_32_33:
1294 case MMC_VDD_33_34:
1295 pwr = SDHCI_POWER_330;
1296 break;
1297 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001298 WARN(1, "%s: Invalid vdd %#x\n",
1299 mmc_hostname(host->mmc), vdd);
1300 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001301 }
1302 }
1303
1304 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001305 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001306
Pierre Ossmanae628902009-05-03 20:45:03 +02001307 host->pwr = pwr;
1308
1309 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001310 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001311 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1312 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001313 } else {
1314 /*
1315 * Spec says that we should clear the power reg before setting
1316 * a new value. Some controllers don't seem to like this though.
1317 */
1318 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1319 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001320
Russell Kinge921a8b2014-04-25 13:00:01 +01001321 /*
1322 * At least the Marvell CaFe chip gets confused if we set the
1323 * voltage and set turn on power at the same time, so set the
1324 * voltage first.
1325 */
1326 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1327 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001328
Russell Kinge921a8b2014-04-25 13:00:01 +01001329 pwr |= SDHCI_POWER_ON;
1330
Pierre Ossmanae628902009-05-03 20:45:03 +02001331 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1332
Russell Kinge921a8b2014-04-25 13:00:01 +01001333 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1334 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001335
Russell Kinge921a8b2014-04-25 13:00:01 +01001336 /*
1337 * Some controllers need an extra 10ms delay of 10ms before
1338 * they can apply clock after applying power
1339 */
1340 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1341 mdelay(10);
1342 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001343}
1344EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001345
Adrian Hunter1dceb042016-03-29 12:45:43 +03001346static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1347 unsigned short vdd)
1348{
1349 struct mmc_host *mmc = host->mmc;
1350
1351 if (host->ops->set_power)
1352 host->ops->set_power(host, mode, vdd);
1353 else if (!IS_ERR(mmc->supply.vmmc))
1354 sdhci_set_power_reg(host, mode, vdd);
1355 else
1356 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001357}
1358
Pierre Ossmand129bce2006-03-24 03:18:17 -08001359/*****************************************************************************\
1360 * *
1361 * MMC callbacks *
1362 * *
1363\*****************************************************************************/
1364
1365static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1366{
1367 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001368 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001369 unsigned long flags;
1370
1371 host = mmc_priv(mmc);
1372
Scott Branden04e079cf2015-03-10 11:35:10 -07001373 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001374 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001375
Pierre Ossmand129bce2006-03-24 03:18:17 -08001376 spin_lock_irqsave(&host->lock, flags);
1377
1378 WARN_ON(host->mrq != NULL);
1379
Adrian Hunter061d17a2016-04-12 14:25:09 +03001380 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001381
1382 /*
1383 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1384 * requests if Auto-CMD12 is enabled.
1385 */
1386 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001387 if (mrq->stop) {
1388 mrq->data->stop = NULL;
1389 mrq->stop = NULL;
1390 }
1391 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001392
1393 host->mrq = mrq;
1394
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001395 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001396 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301398 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001399 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001400 sdhci_send_command(host, mrq->sbc);
1401 else
1402 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301403 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404
Pierre Ossman5f25a662006-10-04 02:15:39 -07001405 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001406 spin_unlock_irqrestore(&host->lock, flags);
1407}
1408
Russell King2317f562014-04-25 12:57:07 +01001409void sdhci_set_bus_width(struct sdhci_host *host, int width)
1410{
1411 u8 ctrl;
1412
1413 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1414 if (width == MMC_BUS_WIDTH_8) {
1415 ctrl &= ~SDHCI_CTRL_4BITBUS;
1416 if (host->version >= SDHCI_SPEC_300)
1417 ctrl |= SDHCI_CTRL_8BITBUS;
1418 } else {
1419 if (host->version >= SDHCI_SPEC_300)
1420 ctrl &= ~SDHCI_CTRL_8BITBUS;
1421 if (width == MMC_BUS_WIDTH_4)
1422 ctrl |= SDHCI_CTRL_4BITBUS;
1423 else
1424 ctrl &= ~SDHCI_CTRL_4BITBUS;
1425 }
1426 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1427}
1428EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1429
Russell King96d7b782014-04-25 12:59:26 +01001430void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1431{
1432 u16 ctrl_2;
1433
1434 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1435 /* Select Bus Speed Mode for host */
1436 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1437 if ((timing == MMC_TIMING_MMC_HS200) ||
1438 (timing == MMC_TIMING_UHS_SDR104))
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1440 else if (timing == MMC_TIMING_UHS_SDR12)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1442 else if (timing == MMC_TIMING_UHS_SDR25)
1443 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1444 else if (timing == MMC_TIMING_UHS_SDR50)
1445 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1446 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1447 (timing == MMC_TIMING_MMC_DDR52))
1448 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001449 else if (timing == MMC_TIMING_MMC_HS400)
1450 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001451 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1452}
1453EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1454
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001455static void sdhci_do_set_ios(struct sdhci_host *host, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001457 unsigned long flags;
1458 u8 ctrl;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001459 struct mmc_host *mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001460
Pierre Ossmand129bce2006-03-24 03:18:17 -08001461 spin_lock_irqsave(&host->lock, flags);
1462
Adrian Hunterceb61432011-12-27 15:48:41 +02001463 if (host->flags & SDHCI_DEVICE_DEAD) {
1464 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001465 if (!IS_ERR(mmc->supply.vmmc) &&
1466 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001467 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001468 return;
1469 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001470
Pierre Ossmand129bce2006-03-24 03:18:17 -08001471 /*
1472 * Reset the chip on each power off.
1473 * Should clear out any weird states.
1474 */
1475 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001476 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001477 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001478 }
1479
Kevin Liu52983382013-01-31 11:31:37 +08001480 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001481 (ios->power_mode == MMC_POWER_UP) &&
1482 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001483 sdhci_enable_preset_value(host, false);
1484
Russell King373073e2014-04-25 12:58:45 +01001485 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001486 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001487 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001488
1489 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1490 host->clock) {
1491 host->timeout_clk = host->mmc->actual_clock ?
1492 host->mmc->actual_clock / 1000 :
1493 host->clock / 1000;
1494 host->mmc->max_busy_timeout =
1495 host->ops->get_max_timeout_count ?
1496 host->ops->get_max_timeout_count(host) :
1497 1 << 27;
1498 host->mmc->max_busy_timeout /= host->timeout_clk;
1499 }
Russell King373073e2014-04-25 12:58:45 +01001500 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Adrian Hunter1dceb042016-03-29 12:45:43 +03001502 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503
Philip Rakity643a81f2010-09-23 08:24:32 -07001504 if (host->ops->platform_send_init_74_clocks)
1505 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1506
Russell King2317f562014-04-25 12:57:07 +01001507 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001508
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001509 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001510
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001511 if ((ios->timing == MMC_TIMING_SD_HS ||
1512 ios->timing == MMC_TIMING_MMC_HS)
1513 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001514 ctrl |= SDHCI_CTRL_HISPD;
1515 else
1516 ctrl &= ~SDHCI_CTRL_HISPD;
1517
Arindam Nathd6d50a12011-05-05 12:18:59 +05301518 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301519 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301520
1521 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001522 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1523 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001524 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301525 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301526 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1527 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001528 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301529 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301530
Russell Kingda91a8f2014-04-25 13:00:12 +01001531 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301532 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301533 /*
1534 * We only need to set Driver Strength if the
1535 * preset value enable is not set.
1536 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001537 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301538 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1539 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1540 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001541 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1542 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301543 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1544 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001545 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1546 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1547 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001548 pr_warn("%s: invalid driver type, default to driver type B\n",
1549 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001550 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1551 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301552
1553 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301554 } else {
1555 /*
1556 * According to SDHC Spec v3.00, if the Preset Value
1557 * Enable in the Host Control 2 register is set, we
1558 * need to reset SD Clock Enable before changing High
1559 * Speed Enable to avoid generating clock gliches.
1560 */
Arindam Nath758535c2011-05-05 12:19:00 +05301561
1562 /* Reset SD Clock Enable */
1563 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1564 clk &= ~SDHCI_CLOCK_CARD_EN;
1565 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1566
1567 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1568
1569 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001570 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301571 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301572
Arindam Nath49c468f2011-05-05 12:19:01 +05301573 /* Reset SD Clock Enable */
1574 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1575 clk &= ~SDHCI_CLOCK_CARD_EN;
1576 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1577
Russell King96d7b782014-04-25 12:59:26 +01001578 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001579 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301580
Kevin Liu52983382013-01-31 11:31:37 +08001581 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1582 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1583 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1584 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1585 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001586 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1587 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001588 u16 preset;
1589
1590 sdhci_enable_preset_value(host, true);
1591 preset = sdhci_get_preset_value(host);
1592 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1593 >> SDHCI_PRESET_DRV_SHIFT;
1594 }
1595
Arindam Nath49c468f2011-05-05 12:19:01 +05301596 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001597 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301598 } else
1599 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301600
Leandro Dorileob8352262007-07-25 23:47:04 +02001601 /*
1602 * Some (ENE) controllers go apeshit on some ios operation,
1603 * signalling timeout and CRC errors even on CMD0. Resetting
1604 * it on each ios seems to solve the problem.
1605 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301606 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001607 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001608
Pierre Ossman5f25a662006-10-04 02:15:39 -07001609 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001610 spin_unlock_irqrestore(&host->lock, flags);
1611}
1612
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001613static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1614{
1615 struct sdhci_host *host = mmc_priv(mmc);
1616
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001617 sdhci_do_set_ios(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001618}
1619
Kevin Liu94144a42013-02-28 17:35:53 +08001620static int sdhci_do_get_cd(struct sdhci_host *host)
1621{
1622 int gpio_cd = mmc_gpio_get_cd(host->mmc);
1623
1624 if (host->flags & SDHCI_DEVICE_DEAD)
1625 return 0;
1626
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001627 /* If nonremovable, assume that the card is always present. */
1628 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001629 return 1;
1630
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001631 /*
1632 * Try slot gpio detect, if defined it take precedence
1633 * over build in controller functionality
1634 */
Kevin Liu94144a42013-02-28 17:35:53 +08001635 if (!IS_ERR_VALUE(gpio_cd))
1636 return !!gpio_cd;
1637
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001638 /* If polling, assume that the card is always present. */
1639 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1640 return 1;
1641
Kevin Liu94144a42013-02-28 17:35:53 +08001642 /* Host native card detect */
1643 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1644}
1645
1646static int sdhci_get_cd(struct mmc_host *mmc)
1647{
1648 struct sdhci_host *host = mmc_priv(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001649
Ulf Hansson15e82072016-04-07 10:56:39 +02001650 return sdhci_do_get_cd(host);
Kevin Liu94144a42013-02-28 17:35:53 +08001651}
1652
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001653static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001654{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001655 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001656 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001657
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658 spin_lock_irqsave(&host->lock, flags);
1659
Pierre Ossman1e728592008-04-16 19:13:13 +02001660 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001661 is_readonly = 0;
1662 else if (host->ops->get_ro)
1663 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001664 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001665 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1666 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001667
1668 spin_unlock_irqrestore(&host->lock, flags);
1669
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001670 /* This quirk needs to be replaced by a callback-function later */
1671 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1672 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673}
1674
Takashi Iwai82b0e232011-04-21 20:26:38 +02001675#define SAMPLE_COUNT 5
1676
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001677static int sdhci_do_get_ro(struct sdhci_host *host)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001678{
Takashi Iwai82b0e232011-04-21 20:26:38 +02001679 int i, ro_count;
1680
Takashi Iwai82b0e232011-04-21 20:26:38 +02001681 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001682 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001683
1684 ro_count = 0;
1685 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001686 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001687 if (++ro_count > SAMPLE_COUNT / 2)
1688 return 1;
1689 }
1690 msleep(30);
1691 }
1692 return 0;
1693}
1694
Adrian Hunter20758b62011-08-29 16:42:12 +03001695static void sdhci_hw_reset(struct mmc_host *mmc)
1696{
1697 struct sdhci_host *host = mmc_priv(mmc);
1698
1699 if (host->ops && host->ops->hw_reset)
1700 host->ops->hw_reset(host);
1701}
1702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703static int sdhci_get_ro(struct mmc_host *mmc)
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001704{
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001705 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001706
Ulf Hansson15e82072016-04-07 10:56:39 +02001707 return sdhci_do_get_ro(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001708}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001709
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001710static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1711{
Russell Kingbe138552014-04-25 12:55:56 +01001712 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001713 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001714 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001715 else
Russell Kingb537f942014-04-25 12:56:01 +01001716 host->ier &= ~SDHCI_INT_CARD_INT;
1717
1718 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1719 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001720 mmiowb();
1721 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001722}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001723
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001724static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1725{
1726 struct sdhci_host *host = mmc_priv(mmc);
1727 unsigned long flags;
1728
1729 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001730 if (enable)
1731 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1732 else
1733 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1734
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001735 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001736 spin_unlock_irqrestore(&host->lock, flags);
1737}
1738
Philip Rakity6231f3d2012-07-23 15:56:23 -07001739static int sdhci_do_start_signal_voltage_switch(struct sdhci_host *host,
Fabio Estevam21f59982013-02-14 10:35:03 -02001740 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001741{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001742 struct mmc_host *mmc = host->mmc;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001743 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001744 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001745
1746 /*
1747 * Signal Voltage Switching is only applicable for Host Controllers
1748 * v3.00 and above.
1749 */
1750 if (host->version < SDHCI_SPEC_300)
1751 return 0;
1752
Philip Rakity6231f3d2012-07-23 15:56:23 -07001753 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001754
Fabio Estevam21f59982013-02-14 10:35:03 -02001755 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001756 case MMC_SIGNAL_VOLTAGE_330:
1757 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1758 ctrl &= ~SDHCI_CTRL_VDD_180;
1759 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1760
Tim Kryger3a48edc2014-06-13 10:13:56 -07001761 if (!IS_ERR(mmc->supply.vqmmc)) {
1762 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1763 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001764 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001765 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1766 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001767 return -EIO;
1768 }
1769 }
1770 /* Wait for 5ms */
1771 usleep_range(5000, 5500);
1772
1773 /* 3.3V regulator output should be stable within 5 ms */
1774 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1775 if (!(ctrl & SDHCI_CTRL_VDD_180))
1776 return 0;
1777
Joe Perches66061102014-09-12 14:56:56 -07001778 pr_warn("%s: 3.3V regulator output did not became stable\n",
1779 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001780
1781 return -EAGAIN;
1782 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001783 if (!IS_ERR(mmc->supply.vqmmc)) {
1784 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001785 1700000, 1950000);
1786 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001787 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1788 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001789 return -EIO;
1790 }
1791 }
1792
1793 /*
1794 * Enable 1.8V Signal Enable in the Host Control2
1795 * register
1796 */
1797 ctrl |= SDHCI_CTRL_VDD_180;
1798 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1799
Vincent Yang9d967a62015-01-20 16:05:15 +08001800 /* Some controller need to do more when switching */
1801 if (host->ops->voltage_switch)
1802 host->ops->voltage_switch(host);
1803
Kevin Liu20b92a32012-12-17 19:29:26 +08001804 /* 1.8V regulator output should be stable within 5 ms */
1805 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1806 if (ctrl & SDHCI_CTRL_VDD_180)
1807 return 0;
1808
Joe Perches66061102014-09-12 14:56:56 -07001809 pr_warn("%s: 1.8V regulator output did not became stable\n",
1810 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001811
1812 return -EAGAIN;
1813 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001814 if (!IS_ERR(mmc->supply.vqmmc)) {
1815 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1816 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001817 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001818 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1819 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001820 return -EIO;
1821 }
1822 }
1823 return 0;
1824 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301825 /* No signal voltage switch required */
1826 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001827 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301828}
1829
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001830static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
Fabio Estevam21f59982013-02-14 10:35:03 -02001831 struct mmc_ios *ios)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001832{
1833 struct sdhci_host *host = mmc_priv(mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001834
1835 if (host->version < SDHCI_SPEC_300)
1836 return 0;
Ulf Hansson15e82072016-04-07 10:56:39 +02001837
1838 return sdhci_do_start_signal_voltage_switch(host, ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001839}
1840
Kevin Liu20b92a32012-12-17 19:29:26 +08001841static int sdhci_card_busy(struct mmc_host *mmc)
1842{
1843 struct sdhci_host *host = mmc_priv(mmc);
1844 u32 present_state;
1845
Kevin Liu20b92a32012-12-17 19:29:26 +08001846 /* Check whether DAT[3:0] is 0000 */
1847 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001848
1849 return !(present_state & SDHCI_DATA_LVL_MASK);
1850}
1851
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001852static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1853{
1854 struct sdhci_host *host = mmc_priv(mmc);
1855 unsigned long flags;
1856
1857 spin_lock_irqsave(&host->lock, flags);
1858 host->flags |= SDHCI_HS400_TUNING;
1859 spin_unlock_irqrestore(&host->lock, flags);
1860
1861 return 0;
1862}
1863
Girish K S069c9f12012-01-06 09:56:39 +05301864static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301865{
Russell King4b6f37d2014-04-25 12:59:36 +01001866 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301867 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301868 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301869 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001870 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001871 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001872 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301873
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001874 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301875
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001876 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1877 host->flags &= ~SDHCI_HS400_TUNING;
1878
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001879 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1880 tuning_count = host->tuning_count;
1881
Arindam Nathb513ea22011-05-05 12:19:04 +05301882 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001883 * The Host Controller needs tuning in case of SDR104 and DDR50
1884 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1885 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301886 * If the Host Controller supports the HS200 mode then the
1887 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301888 */
Russell King4b6f37d2014-04-25 12:59:36 +01001889 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001890 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001891 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001892 err = -EINVAL;
1893 goto out_unlock;
1894
Russell King4b6f37d2014-04-25 12:59:36 +01001895 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001896 /*
1897 * Periodic re-tuning for HS400 is not expected to be needed, so
1898 * disable it here.
1899 */
1900 if (hs400_tuning)
1901 tuning_count = 0;
1902 break;
1903
Russell King4b6f37d2014-04-25 12:59:36 +01001904 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001905 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001906 break;
Girish K S069c9f12012-01-06 09:56:39 +05301907
Russell King4b6f37d2014-04-25 12:59:36 +01001908 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001909 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001910 break;
1911 /* FALLTHROUGH */
1912
1913 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001914 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301915 }
1916
Dong Aisheng45251812013-09-13 19:11:30 +08001917 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001918 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001919 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001920 return err;
1921 }
1922
Russell King4b6f37d2014-04-25 12:59:36 +01001923 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1924 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001925 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1926 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301927 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1928
1929 /*
1930 * As per the Host Controller spec v3.00, tuning command
1931 * generates Buffer Read Ready interrupt, so enable that.
1932 *
1933 * Note: The spec clearly says that when tuning sequence
1934 * is being performed, the controller does not generate
1935 * interrupts other than Buffer Read Ready interrupt. But
1936 * to make sure we don't hit a controller bug, we _only_
1937 * enable Buffer Read Ready interrupt here.
1938 */
Russell Kingb537f942014-04-25 12:56:01 +01001939 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1940 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301941
1942 /*
1943 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1944 * of loops reaches 40 times or a timeout of 150ms occurs.
1945 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301946 do {
1947 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001948 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301949
Girish K S069c9f12012-01-06 09:56:39 +05301950 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301951 cmd.arg = 0;
1952 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1953 cmd.retries = 0;
1954 cmd.data = NULL;
1955 cmd.error = 0;
1956
Al Cooper7ce45e92014-05-09 11:34:07 -04001957 if (tuning_loop_counter-- == 0)
1958 break;
1959
Arindam Nathb513ea22011-05-05 12:19:04 +05301960 mrq.cmd = &cmd;
1961 host->mrq = &mrq;
1962
1963 /*
1964 * In response to CMD19, the card sends 64 bytes of tuning
1965 * block to the Host Controller. So we set the block size
1966 * to 64 here.
1967 */
Girish K S069c9f12012-01-06 09:56:39 +05301968 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1969 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1970 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1971 SDHCI_BLOCK_SIZE);
1972 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1973 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1974 SDHCI_BLOCK_SIZE);
1975 } else {
1976 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1977 SDHCI_BLOCK_SIZE);
1978 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301979
1980 /*
1981 * The tuning block is sent by the card to the host controller.
1982 * So we set the TRNS_READ bit in the Transfer Mode register.
1983 * This also takes care of setting DMA Enable and Multi Block
1984 * Select in the same register to 0.
1985 */
1986 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1987
1988 sdhci_send_command(host, &cmd);
1989
1990 host->cmd = NULL;
1991 host->mrq = NULL;
1992
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001993 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301994 /* Wait for Buffer Read Ready interrupt */
1995 wait_event_interruptible_timeout(host->buf_ready_int,
1996 (host->tuning_done == 1),
1997 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001998 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301999
2000 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002001 pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n");
Arindam Nathb513ea22011-05-05 12:19:04 +05302002 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2003 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2004 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2005 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2006
2007 err = -EIO;
2008 goto out;
2009 }
2010
2011 host->tuning_done = 0;
2012
2013 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002014
2015 /* eMMC spec does not require a delay between tuning cycles */
2016 if (opcode == MMC_SEND_TUNING_BLOCK)
2017 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302018 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2019
2020 /*
2021 * The Host Driver has exhausted the maximum number of loops allowed,
2022 * so use fixed sampling frequency.
2023 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002024 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302025 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2026 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002027 }
2028 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002029 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002030 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302031 }
2032
2033out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002034 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002035 /*
2036 * In case tuning fails, host controllers which support
2037 * re-tuning can try tuning again at a later time, when the
2038 * re-tuning timer expires. So for these controllers, we
2039 * return 0. Since there might be other controllers who do not
2040 * have this capability, we return error for them.
2041 */
2042 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302043 }
2044
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002045 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302046
Russell Kingb537f942014-04-25 12:56:01 +01002047 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2048 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002049out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002050 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302051 return err;
2052}
2053
Adrian Huntercb849642015-02-06 14:12:59 +02002054static int sdhci_select_drive_strength(struct mmc_card *card,
2055 unsigned int max_dtr, int host_drv,
2056 int card_drv, int *drv_type)
2057{
2058 struct sdhci_host *host = mmc_priv(card->host);
2059
2060 if (!host->ops->select_drive_strength)
2061 return 0;
2062
2063 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2064 card_drv, drv_type);
2065}
Kevin Liu52983382013-01-31 11:31:37 +08002066
2067static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302068{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302069 /* Host Controller v3.00 defines preset value registers */
2070 if (host->version < SDHCI_SPEC_300)
2071 return;
2072
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302073 /*
2074 * We only enable or disable Preset Value if they are not already
2075 * enabled or disabled respectively. Otherwise, we bail out.
2076 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002077 if (host->preset_enabled != enable) {
2078 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2079
2080 if (enable)
2081 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2082 else
2083 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2084
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302085 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002086
2087 if (enable)
2088 host->flags |= SDHCI_PV_ENABLED;
2089 else
2090 host->flags &= ~SDHCI_PV_ENABLED;
2091
2092 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302093 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002094}
2095
Haibo Chen348487c2014-12-09 17:04:05 +08002096static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2097 int err)
2098{
2099 struct sdhci_host *host = mmc_priv(mmc);
2100 struct mmc_data *data = mrq->data;
2101
Russell Kingf48f0392016-01-26 13:40:32 +00002102 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002103 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2104 data->flags & MMC_DATA_WRITE ?
2105 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2106
2107 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002108}
2109
Haibo Chen348487c2014-12-09 17:04:05 +08002110static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2111 bool is_first_req)
2112{
2113 struct sdhci_host *host = mmc_priv(mmc);
2114
Haibo Chend31911b2015-08-25 10:02:11 +08002115 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002116
2117 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002118 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002119}
2120
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002121static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002122{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002123 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002124 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002125 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002126
Christian Daudt722e1282013-06-20 14:26:36 -07002127 /* First check if client has provided their own card event */
2128 if (host->ops->card_event)
2129 host->ops->card_event(host);
2130
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002131 present = sdhci_do_get_cd(host);
2132
Pierre Ossmand129bce2006-03-24 03:18:17 -08002133 spin_lock_irqsave(&host->lock, flags);
2134
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002135 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002136 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302137 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002138 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302139 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002140 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002141
Russell King03231f92014-04-25 12:57:12 +01002142 sdhci_do_reset(host, SDHCI_RESET_CMD);
2143 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002144
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002145 host->mrq->cmd->error = -ENOMEDIUM;
2146 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002147 }
2148
2149 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002150}
2151
2152static const struct mmc_host_ops sdhci_ops = {
2153 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002154 .post_req = sdhci_post_req,
2155 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002156 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002157 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002158 .get_ro = sdhci_get_ro,
2159 .hw_reset = sdhci_hw_reset,
2160 .enable_sdio_irq = sdhci_enable_sdio_irq,
2161 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002162 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002163 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002164 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002165 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002166 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002167};
2168
2169/*****************************************************************************\
2170 * *
2171 * Tasklets *
2172 * *
2173\*****************************************************************************/
2174
Pierre Ossmand129bce2006-03-24 03:18:17 -08002175static void sdhci_tasklet_finish(unsigned long param)
2176{
2177 struct sdhci_host *host;
2178 unsigned long flags;
2179 struct mmc_request *mrq;
2180
2181 host = (struct sdhci_host*)param;
2182
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002183 spin_lock_irqsave(&host->lock, flags);
2184
Chris Ball0c9c99a2011-04-27 17:35:31 -04002185 /*
2186 * If this tasklet gets rescheduled while running, it will
2187 * be run again afterwards but without any active request.
2188 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002189 if (!host->mrq) {
2190 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002191 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002192 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002193
2194 del_timer(&host->timer);
2195
2196 mrq = host->mrq;
2197
Pierre Ossmand129bce2006-03-24 03:18:17 -08002198 /*
Russell King054cedf2016-01-26 13:40:42 +00002199 * Always unmap the data buffers if they were mapped by
2200 * sdhci_prepare_data() whenever we finish with a request.
2201 * This avoids leaking DMA mappings on error.
2202 */
2203 if (host->flags & SDHCI_REQ_USE_DMA) {
2204 struct mmc_data *data = mrq->data;
2205
2206 if (data && data->host_cookie == COOKIE_MAPPED) {
2207 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2208 (data->flags & MMC_DATA_READ) ?
2209 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2210 data->host_cookie = COOKIE_UNMAPPED;
2211 }
2212 }
2213
2214 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002215 * The controller needs a reset of internal state machines
2216 * upon error conditions.
2217 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002218 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002219 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002220 (mrq->sbc && mrq->sbc->error) ||
2221 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2222 (mrq->data->stop && mrq->data->stop->error))) ||
2223 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002224
2225 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002226 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002227 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002228 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002229
2230 /* Spec says we should do both at the same time, but Ricoh
2231 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002232 sdhci_do_reset(host, SDHCI_RESET_CMD);
2233 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234 }
2235
2236 host->mrq = NULL;
2237 host->cmd = NULL;
2238 host->data = NULL;
2239
Adrian Hunter061d17a2016-04-12 14:25:09 +03002240 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241
Pierre Ossman5f25a662006-10-04 02:15:39 -07002242 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002243 spin_unlock_irqrestore(&host->lock, flags);
2244
2245 mmc_request_done(host->mmc, mrq);
2246}
2247
2248static void sdhci_timeout_timer(unsigned long data)
2249{
2250 struct sdhci_host *host;
2251 unsigned long flags;
2252
2253 host = (struct sdhci_host*)data;
2254
2255 spin_lock_irqsave(&host->lock, flags);
2256
2257 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002258 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2259 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 sdhci_dumpregs(host);
2261
2262 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002263 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002264 sdhci_finish_data(host);
2265 } else {
2266 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002267 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002269 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002270
2271 tasklet_schedule(&host->finish_tasklet);
2272 }
2273 }
2274
Pierre Ossman5f25a662006-10-04 02:15:39 -07002275 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276 spin_unlock_irqrestore(&host->lock, flags);
2277}
2278
2279/*****************************************************************************\
2280 * *
2281 * Interrupt handling *
2282 * *
2283\*****************************************************************************/
2284
Adrian Hunter61541392014-09-24 10:27:27 +03002285static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002286{
2287 BUG_ON(intmask == 0);
2288
2289 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002290 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2291 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292 sdhci_dumpregs(host);
2293 return;
2294 }
2295
Russell Kingec014cb2016-01-26 13:39:39 +00002296 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2297 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2298 if (intmask & SDHCI_INT_TIMEOUT)
2299 host->cmd->error = -ETIMEDOUT;
2300 else
2301 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302
Russell King71fcbda2016-01-26 13:39:45 +00002303 /*
2304 * If this command initiates a data phase and a response
2305 * CRC error is signalled, the card can start transferring
2306 * data - the card may have received the command without
2307 * error. We must not terminate the mmc_request early.
2308 *
2309 * If the card did not receive the command or returned an
2310 * error which prevented it sending data, the data phase
2311 * will time out.
2312 */
2313 if (host->cmd->data &&
2314 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2315 SDHCI_INT_CRC) {
2316 host->cmd = NULL;
2317 return;
2318 }
2319
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002321 return;
2322 }
2323
2324 /*
2325 * The host can send and interrupt when the busy state has
2326 * ended, allowing us to wait without wasting CPU cycles.
2327 * Unfortunately this is overloaded on the "data complete"
2328 * interrupt, so we need to take some care when handling
2329 * it.
2330 *
2331 * Note: The 1.0 specification is a bit ambiguous about this
2332 * feature so there might be some problems with older
2333 * controllers.
2334 */
2335 if (host->cmd->flags & MMC_RSP_BUSY) {
2336 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002337 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002338 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2339 && !host->busy_handle) {
2340 /* Mark that command complete before busy is ended */
2341 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002342 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002343 }
Ben Dooksf9454052009-02-20 20:33:08 +03002344
2345 /* The controller does not support the end-of-busy IRQ,
2346 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002347 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2348 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2349 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002350 }
2351
2352 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002353 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354}
2355
George G. Davis0957c332010-02-18 12:32:12 -05002356#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002357static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002358{
2359 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002360 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002361
2362 sdhci_dumpregs(host);
2363
2364 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002365 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002366
Adrian Huntere57a5f62014-11-04 12:42:46 +02002367 if (host->flags & SDHCI_USE_64_BIT_DMA)
2368 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2369 name, desc, le32_to_cpu(dma_desc->addr_hi),
2370 le32_to_cpu(dma_desc->addr_lo),
2371 le16_to_cpu(dma_desc->len),
2372 le16_to_cpu(dma_desc->cmd));
2373 else
2374 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2375 name, desc, le32_to_cpu(dma_desc->addr_lo),
2376 le16_to_cpu(dma_desc->len),
2377 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002378
Adrian Hunter76fe3792014-11-04 12:42:42 +02002379 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002380
Adrian Hunter05452302014-11-04 12:42:45 +02002381 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002382 break;
2383 }
2384}
2385#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002386static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002387#endif
2388
Pierre Ossmand129bce2006-03-24 03:18:17 -08002389static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2390{
Girish K S069c9f12012-01-06 09:56:39 +05302391 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002392 BUG_ON(intmask == 0);
2393
Arindam Nathb513ea22011-05-05 12:19:04 +05302394 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2395 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302396 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2397 if (command == MMC_SEND_TUNING_BLOCK ||
2398 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302399 host->tuning_done = 1;
2400 wake_up(&host->buf_ready_int);
2401 return;
2402 }
2403 }
2404
Pierre Ossmand129bce2006-03-24 03:18:17 -08002405 if (!host->data) {
2406 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002407 * The "data complete" interrupt is also used to
2408 * indicate that a busy state has ended. See comment
2409 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002410 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002411 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002412 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2413 host->cmd->error = -ETIMEDOUT;
2414 tasklet_schedule(&host->finish_tasklet);
2415 return;
2416 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002417 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002418 /*
2419 * Some cards handle busy-end interrupt
2420 * before the command completed, so make
2421 * sure we do things in the proper order.
2422 */
2423 if (host->busy_handle)
2424 sdhci_finish_command(host);
2425 else
2426 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002427 return;
2428 }
2429 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430
Marek Vasut2e4456f2015-11-18 10:47:02 +01002431 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2432 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002433 sdhci_dumpregs(host);
2434
2435 return;
2436 }
2437
2438 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002439 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002440 else if (intmask & SDHCI_INT_DATA_END_BIT)
2441 host->data->error = -EILSEQ;
2442 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2443 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2444 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002445 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002446 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302447 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002448 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002449 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002450 if (host->ops->adma_workaround)
2451 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002452 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002453
Pierre Ossman17b04292007-07-22 22:18:46 +02002454 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455 sdhci_finish_data(host);
2456 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002457 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002458 sdhci_transfer_pio(host);
2459
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002460 /*
2461 * We currently don't do anything fancy with DMA
2462 * boundaries, but as we can't disable the feature
2463 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002464 *
2465 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2466 * should return a valid address to continue from, but as
2467 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002468 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002469 if (intmask & SDHCI_INT_DMA_END) {
2470 u32 dmastart, dmanow;
2471 dmastart = sg_dma_address(host->data->sg);
2472 dmanow = dmastart + host->data->bytes_xfered;
2473 /*
2474 * Force update to the next DMA block boundary.
2475 */
2476 dmanow = (dmanow &
2477 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2478 SDHCI_DEFAULT_BOUNDARY_SIZE;
2479 host->data->bytes_xfered = dmanow - dmastart;
2480 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2481 " next 0x%08x\n",
2482 mmc_hostname(host->mmc), dmastart,
2483 host->data->bytes_xfered, dmanow);
2484 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2485 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002486
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002487 if (intmask & SDHCI_INT_DATA_END) {
2488 if (host->cmd) {
2489 /*
2490 * Data managed to finish before the
2491 * command completed. Make sure we do
2492 * things in the proper order.
2493 */
2494 host->data_early = 1;
2495 } else {
2496 sdhci_finish_data(host);
2497 }
2498 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002499 }
2500}
2501
David Howells7d12e782006-10-05 14:55:46 +01002502static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002503{
Russell King781e9892014-04-25 12:55:46 +01002504 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002505 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002506 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002507 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002508
2509 spin_lock(&host->lock);
2510
Russell Kingbe138552014-04-25 12:55:56 +01002511 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002512 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002513 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002514 }
2515
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002516 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002517 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002518 result = IRQ_NONE;
2519 goto out;
2520 }
2521
Russell King41005002014-04-25 12:55:36 +01002522 do {
2523 /* Clear selected interrupts. */
2524 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2525 SDHCI_INT_BUS_POWER);
2526 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527
Russell King41005002014-04-25 12:55:36 +01002528 DBG("*** %s got interrupt: 0x%08x\n",
2529 mmc_hostname(host->mmc), intmask);
2530
2531 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2532 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2533 SDHCI_CARD_PRESENT;
2534
2535 /*
2536 * There is a observation on i.mx esdhc. INSERT
2537 * bit will be immediately set again when it gets
2538 * cleared, if a card is inserted. We have to mask
2539 * the irq to prevent interrupt storm which will
2540 * freeze the system. And the REMOVE gets the
2541 * same situation.
2542 *
2543 * More testing are needed here to ensure it works
2544 * for other platforms though.
2545 */
Russell Kingb537f942014-04-25 12:56:01 +01002546 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2547 SDHCI_INT_CARD_REMOVE);
2548 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2549 SDHCI_INT_CARD_INSERT;
2550 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2551 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002552
2553 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2554 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002555
2556 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2557 SDHCI_INT_CARD_REMOVE);
2558 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002559 }
2560
2561 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002562 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2563 &intmask);
Russell King41005002014-04-25 12:55:36 +01002564
2565 if (intmask & SDHCI_INT_DATA_MASK)
2566 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2567
2568 if (intmask & SDHCI_INT_BUS_POWER)
2569 pr_err("%s: Card is consuming too much power!\n",
2570 mmc_hostname(host->mmc));
2571
Russell King781e9892014-04-25 12:55:46 +01002572 if (intmask & SDHCI_INT_CARD_INT) {
2573 sdhci_enable_sdio_irq_nolock(host, false);
2574 host->thread_isr |= SDHCI_INT_CARD_INT;
2575 result = IRQ_WAKE_THREAD;
2576 }
Russell King41005002014-04-25 12:55:36 +01002577
2578 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2579 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2580 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2581 SDHCI_INT_CARD_INT);
2582
2583 if (intmask) {
2584 unexpected |= intmask;
2585 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2586 }
2587
Russell King781e9892014-04-25 12:55:46 +01002588 if (result == IRQ_NONE)
2589 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002590
2591 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002592 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593out:
2594 spin_unlock(&host->lock);
2595
Alexander Stein6379b232012-03-14 09:52:10 +01002596 if (unexpected) {
2597 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2598 mmc_hostname(host->mmc), unexpected);
2599 sdhci_dumpregs(host);
2600 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002601
Pierre Ossmand129bce2006-03-24 03:18:17 -08002602 return result;
2603}
2604
Russell King781e9892014-04-25 12:55:46 +01002605static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2606{
2607 struct sdhci_host *host = dev_id;
2608 unsigned long flags;
2609 u32 isr;
2610
2611 spin_lock_irqsave(&host->lock, flags);
2612 isr = host->thread_isr;
2613 host->thread_isr = 0;
2614 spin_unlock_irqrestore(&host->lock, flags);
2615
Russell King3560db82014-04-25 12:55:51 +01002616 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2617 sdhci_card_event(host->mmc);
2618 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2619 }
2620
Russell King781e9892014-04-25 12:55:46 +01002621 if (isr & SDHCI_INT_CARD_INT) {
2622 sdio_run_irqs(host->mmc);
2623
2624 spin_lock_irqsave(&host->lock, flags);
2625 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2626 sdhci_enable_sdio_irq_nolock(host, true);
2627 spin_unlock_irqrestore(&host->lock, flags);
2628 }
2629
2630 return isr ? IRQ_HANDLED : IRQ_NONE;
2631}
2632
Pierre Ossmand129bce2006-03-24 03:18:17 -08002633/*****************************************************************************\
2634 * *
2635 * Suspend/resume *
2636 * *
2637\*****************************************************************************/
2638
2639#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002640void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2641{
2642 u8 val;
2643 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2644 | SDHCI_WAKE_ON_INT;
2645
2646 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2647 val |= mask ;
2648 /* Avoid fake wake up */
2649 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2650 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2651 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2652}
2653EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2654
Fabio Estevam0b10f472014-08-30 14:53:13 -03002655static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002656{
2657 u8 val;
2658 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2659 | SDHCI_WAKE_ON_INT;
2660
2661 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2662 val &= ~mask;
2663 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2664}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665
Manuel Lauss29495aa2011-11-03 11:09:45 +01002666int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002667{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002668 sdhci_disable_card_detection(host);
2669
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002670 mmc_retune_timer_stop(host->mmc);
2671 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302672
Kevin Liuad080d72013-01-05 17:21:33 +08002673 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002674 host->ier = 0;
2675 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2676 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002677 free_irq(host->irq, host);
2678 } else {
2679 sdhci_enable_irq_wakeups(host);
2680 enable_irq_wake(host->irq);
2681 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002682 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002683}
2684
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002685EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002686
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002687int sdhci_resume_host(struct sdhci_host *host)
2688{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002689 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002690
Richard Röjforsa13abc72009-09-22 16:45:30 -07002691 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002692 if (host->ops->enable_dma)
2693 host->ops->enable_dma(host);
2694 }
2695
Adrian Hunter6308d292012-02-07 14:48:54 +02002696 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2697 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2698 /* Card keeps power but host controller does not */
2699 sdhci_init(host, 0);
2700 host->pwr = 0;
2701 host->clock = 0;
2702 sdhci_do_set_ios(host, &host->mmc->ios);
2703 } else {
2704 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2705 mmiowb();
2706 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002707
Haibo Chen14a7b41642015-09-15 18:32:58 +08002708 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2709 ret = request_threaded_irq(host->irq, sdhci_irq,
2710 sdhci_thread_irq, IRQF_SHARED,
2711 mmc_hostname(host->mmc), host);
2712 if (ret)
2713 return ret;
2714 } else {
2715 sdhci_disable_irq_wakeups(host);
2716 disable_irq_wake(host->irq);
2717 }
2718
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002719 sdhci_enable_card_detection(host);
2720
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002721 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002722}
2723
2724EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002725
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002726int sdhci_runtime_suspend_host(struct sdhci_host *host)
2727{
2728 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002729
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002730 mmc_retune_timer_stop(host->mmc);
2731 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002732
2733 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002734 host->ier &= SDHCI_INT_CARD_INT;
2735 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2736 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002737 spin_unlock_irqrestore(&host->lock, flags);
2738
Russell King781e9892014-04-25 12:55:46 +01002739 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002740
2741 spin_lock_irqsave(&host->lock, flags);
2742 host->runtime_suspended = true;
2743 spin_unlock_irqrestore(&host->lock, flags);
2744
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002745 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002746}
2747EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2748
2749int sdhci_runtime_resume_host(struct sdhci_host *host)
2750{
2751 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002752 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002753
2754 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2755 if (host->ops->enable_dma)
2756 host->ops->enable_dma(host);
2757 }
2758
2759 sdhci_init(host, 0);
2760
2761 /* Force clock and power re-program */
2762 host->pwr = 0;
2763 host->clock = 0;
Jisheng Zhang3396e732015-01-29 17:42:12 +08002764 sdhci_do_start_signal_voltage_switch(host, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002765 sdhci_do_set_ios(host, &host->mmc->ios);
2766
Kevin Liu52983382013-01-31 11:31:37 +08002767 if ((host_flags & SDHCI_PV_ENABLED) &&
2768 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2769 spin_lock_irqsave(&host->lock, flags);
2770 sdhci_enable_preset_value(host, true);
2771 spin_unlock_irqrestore(&host->lock, flags);
2772 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002773
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002774 spin_lock_irqsave(&host->lock, flags);
2775
2776 host->runtime_suspended = false;
2777
2778 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002779 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002780 sdhci_enable_sdio_irq_nolock(host, true);
2781
2782 /* Enable Card Detection */
2783 sdhci_enable_card_detection(host);
2784
2785 spin_unlock_irqrestore(&host->lock, flags);
2786
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002787 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002788}
2789EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2790
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002791#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002792
Pierre Ossmand129bce2006-03-24 03:18:17 -08002793/*****************************************************************************\
2794 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002795 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002796 * *
2797\*****************************************************************************/
2798
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002799struct sdhci_host *sdhci_alloc_host(struct device *dev,
2800 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002801{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002802 struct mmc_host *mmc;
2803 struct sdhci_host *host;
2804
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002805 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002806
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002807 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002808 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002809 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002810
2811 host = mmc_priv(mmc);
2812 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002813 host->mmc_host_ops = sdhci_ops;
2814 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002816 return host;
2817}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002818
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002819EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002820
Alexandre Courbot7b913692016-03-07 11:07:55 +09002821static int sdhci_set_dma_mask(struct sdhci_host *host)
2822{
2823 struct mmc_host *mmc = host->mmc;
2824 struct device *dev = mmc_dev(mmc);
2825 int ret = -EINVAL;
2826
2827 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2828 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2829
2830 /* Try 64-bit mask if hardware is capable of it */
2831 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2832 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2833 if (ret) {
2834 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2835 mmc_hostname(mmc));
2836 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2837 }
2838 }
2839
2840 /* 32-bit mask as default & fallback */
2841 if (ret) {
2842 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2843 if (ret)
2844 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2845 mmc_hostname(mmc));
2846 }
2847
2848 return ret;
2849}
2850
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002851int sdhci_add_host(struct sdhci_host *host)
2852{
2853 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002854 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302855 u32 max_current_caps;
2856 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002857 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002858 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002859 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002860
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002861 WARN_ON(host == NULL);
2862 if (host == NULL)
2863 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002864
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002865 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002866
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002867 if (debug_quirks)
2868 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002869 if (debug_quirks2)
2870 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002872 override_timeout_clk = host->timeout_clk;
2873
Russell King03231f92014-04-25 12:57:12 +01002874 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002875
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002876 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002877 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2878 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002879 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002880 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2881 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002882 }
2883
Arindam Nathf2119df2011-05-05 12:18:57 +05302884 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002885 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002886
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002887 if (host->version >= SDHCI_SPEC_300)
2888 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2889 host->caps1 :
2890 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302891
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002892 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302894 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002895 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002896 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002897 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002898
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002899 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002900 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002901 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002902 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002903 }
2904
Arindam Nathf2119df2011-05-05 12:18:57 +05302905 if ((host->version >= SDHCI_SPEC_200) &&
2906 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002907 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002908
2909 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2910 (host->flags & SDHCI_USE_ADMA)) {
2911 DBG("Disabling ADMA as it is marked broken\n");
2912 host->flags &= ~SDHCI_USE_ADMA;
2913 }
2914
Adrian Huntere57a5f62014-11-04 12:42:46 +02002915 /*
2916 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2917 * and *must* do 64-bit DMA. A driver has the opportunity to change
2918 * that during the first call to ->enable_dma(). Similarly
2919 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2920 * implement.
2921 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002922 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002923 host->flags |= SDHCI_USE_64_BIT_DMA;
2924
Richard Röjforsa13abc72009-09-22 16:45:30 -07002925 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002926 ret = sdhci_set_dma_mask(host);
2927
2928 if (!ret && host->ops->enable_dma)
2929 ret = host->ops->enable_dma(host);
2930
2931 if (ret) {
2932 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2933 mmc_hostname(mmc));
2934 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2935
2936 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937 }
2938 }
2939
Adrian Huntere57a5f62014-11-04 12:42:46 +02002940 /* SDMA does not support 64-bit DMA */
2941 if (host->flags & SDHCI_USE_64_BIT_DMA)
2942 host->flags &= ~SDHCI_USE_SDMA;
2943
Pierre Ossman2134a922008-06-28 18:28:51 +02002944 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002945 dma_addr_t dma;
2946 void *buf;
2947
Pierre Ossman2134a922008-06-28 18:28:51 +02002948 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002949 * The DMA descriptor table size is calculated as the maximum
2950 * number of segments times 2, to allow for an alignment
2951 * descriptor for each segment, plus 1 for a nop end descriptor,
2952 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002953 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002954 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2955 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2956 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002957 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002958 } else {
2959 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2960 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002961 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002962 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002963
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002964 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002965 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2966 host->adma_table_sz, &dma, GFP_KERNEL);
2967 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002968 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002969 mmc_hostname(mmc));
2970 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002971 } else if ((dma + host->align_buffer_sz) &
2972 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002973 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2974 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002975 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002976 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2977 host->adma_table_sz, buf, dma);
2978 } else {
2979 host->align_buffer = buf;
2980 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002981
Russell Kinge66e61c2016-01-26 13:39:55 +00002982 host->adma_table = buf + host->align_buffer_sz;
2983 host->adma_addr = dma + host->align_buffer_sz;
2984 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002985 }
2986
Pierre Ossman76591502008-07-21 00:32:11 +02002987 /*
2988 * If we use DMA, then it's up to the caller to set the DMA
2989 * mask, but PIO does not need the hw shim so we set a new
2990 * mask here in that case.
2991 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002992 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002993 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002994 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002995 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002996
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002997 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302998 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002999 >> SDHCI_CLOCK_BASE_SHIFT;
3000 else
Arindam Nathf2119df2011-05-05 12:18:57 +05303001 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003002 >> SDHCI_CLOCK_BASE_SHIFT;
3003
Pierre Ossmand129bce2006-03-24 03:18:17 -08003004 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003005 if (host->max_clk == 0 || host->quirks &
3006 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003007 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003008 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3009 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003010 ret = -ENODEV;
3011 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003012 }
3013 host->max_clk = host->ops->get_max_clock(host);
3014 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003015
3016 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303017 * In case of Host Controller v3.00, find out whether clock
3018 * multiplier is supported.
3019 */
3020 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
3021 SDHCI_CLOCK_MUL_SHIFT;
3022
3023 /*
3024 * In case the value in Clock Multiplier is 0, then programmable
3025 * clock mode is not supported, otherwise the actual clock
3026 * multiplier is one more than the value of Clock Multiplier
3027 * in the Capabilities Register.
3028 */
3029 if (host->clk_mul)
3030 host->clk_mul += 1;
3031
3032 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003033 * Set host parameters.
3034 */
Dong Aisheng59241752015-07-22 20:53:07 +08003035 max_clk = host->max_clk;
3036
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003037 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003038 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303039 else if (host->version >= SDHCI_SPEC_300) {
3040 if (host->clk_mul) {
3041 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003042 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303043 } else
3044 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3045 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003046 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003047
Adrian Hunterd310ae42016-04-12 14:25:07 +03003048 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003049 mmc->f_max = max_clk;
3050
Aisheng Dong28aab052014-08-27 15:26:31 +08003051 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3052 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3053 SDHCI_TIMEOUT_CLK_SHIFT;
3054 if (host->timeout_clk == 0) {
3055 if (host->ops->get_timeout_clock) {
3056 host->timeout_clk =
3057 host->ops->get_timeout_clock(host);
3058 } else {
3059 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3060 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003061 ret = -ENODEV;
3062 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003063 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003064 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003065
Aisheng Dong28aab052014-08-27 15:26:31 +08003066 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3067 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003068
Adrian Hunter99513622016-03-07 13:33:55 +02003069 if (override_timeout_clk)
3070 host->timeout_clk = override_timeout_clk;
3071
Aisheng Dong28aab052014-08-27 15:26:31 +08003072 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003073 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003074 mmc->max_busy_timeout /= host->timeout_clk;
3075 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003076
Andrei Warkentine89d4562011-05-23 15:06:37 -05003077 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003078 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003079
3080 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3081 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003082
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003083 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003084 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003085 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003086 !(host->flags & SDHCI_USE_SDMA)) &&
3087 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003088 host->flags |= SDHCI_AUTO_CMD23;
3089 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3090 } else {
3091 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3092 }
3093
Philip Rakity15ec4462010-11-19 16:48:39 -05003094 /*
3095 * A controller may support 8-bit width, but the board itself
3096 * might not have the pins brought out. Boards that support
3097 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3098 * their platform code before calling sdhci_add_host(), and we
3099 * won't assume 8-bit width for hosts without that CAP.
3100 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003101 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003102 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003103
Jerry Huang63ef5d82012-10-25 13:47:19 +08003104 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3105 mmc->caps &= ~MMC_CAP_CMD23;
3106
Arindam Nathf2119df2011-05-05 12:18:57 +05303107 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003108 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003109
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003110 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003111 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
3112 IS_ERR_VALUE(mmc_gpio_get_cd(host->mmc)))
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003113 mmc->caps |= MMC_CAP_NEEDS_POLL;
3114
Tim Kryger3a48edc2014-06-13 10:13:56 -07003115 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003116 ret = mmc_regulator_get_supply(mmc);
3117 if (ret == -EPROBE_DEFER)
3118 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003119
Philip Rakity6231f3d2012-07-23 15:56:23 -07003120 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003121 if (!IS_ERR(mmc->supply.vqmmc)) {
3122 ret = regulator_enable(mmc->supply.vqmmc);
3123 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3124 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003125 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3126 SDHCI_SUPPORT_SDR50 |
3127 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003128 if (ret) {
3129 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3130 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003131 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003132 }
Kevin Liu8363c372012-11-17 17:55:51 -05003133 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003134
Daniel Drake6a661802012-11-25 13:01:19 -05003135 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3136 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3137 SDHCI_SUPPORT_DDR50);
3138
Al Cooper4188bba2012-03-16 15:54:17 -04003139 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3140 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3141 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303142 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3143
3144 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003145 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303146 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003147 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3148 * field can be promoted to support HS200.
3149 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003150 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003151 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003152 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303153 mmc->caps |= MMC_CAP_UHS_SDR50;
3154
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003155 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3156 (caps[1] & SDHCI_SUPPORT_HS400))
3157 mmc->caps2 |= MMC_CAP2_HS400;
3158
Adrian Hunter549c0b12014-11-06 15:19:05 +02003159 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3160 (IS_ERR(mmc->supply.vqmmc) ||
3161 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3162 1300000)))
3163 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3164
Micky Ching9107ebb2014-02-21 18:40:35 +08003165 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3166 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303167 mmc->caps |= MMC_CAP_UHS_DDR50;
3168
Girish K S069c9f12012-01-06 09:56:39 +05303169 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303170 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3171 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3172
Arindam Nathd6d50a12011-05-05 12:18:59 +05303173 /* Driver Type(s) (A, C, D) supported by the host */
3174 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3175 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3176 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3177 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3178 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3179 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3180
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303181 /* Initial value for re-tuning timer count */
3182 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3183 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3184
3185 /*
3186 * In case Re-tuning Timer is not disabled, the actual value of
3187 * re-tuning timer will be 2 ^ (n - 1).
3188 */
3189 if (host->tuning_count)
3190 host->tuning_count = 1 << (host->tuning_count - 1);
3191
3192 /* Re-tuning mode supported by the Host Controller */
3193 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3194 SDHCI_RETUNING_MODE_SHIFT;
3195
Takashi Iwai8f230f42010-12-08 10:04:30 +01003196 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003197
Arindam Nathf2119df2011-05-05 12:18:57 +05303198 /*
3199 * According to SD Host Controller spec v3.00, if the Host System
3200 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3201 * the value is meaningful only if Voltage Support in the Capabilities
3202 * register is set. The actual current value is 4 times the register
3203 * value.
3204 */
3205 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003206 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003207 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003208 if (curr > 0) {
3209
3210 /* convert to SDHCI_MAX_CURRENT format */
3211 curr = curr/1000; /* convert to mA */
3212 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3213
3214 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3215 max_current_caps =
3216 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3217 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3218 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3219 }
3220 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303221
3222 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003223 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303224
Aaron Lu55c46652012-07-04 13:31:48 +08003225 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303226 SDHCI_MAX_CURRENT_330_MASK) >>
3227 SDHCI_MAX_CURRENT_330_SHIFT) *
3228 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303229 }
3230 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003231 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303232
Aaron Lu55c46652012-07-04 13:31:48 +08003233 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303234 SDHCI_MAX_CURRENT_300_MASK) >>
3235 SDHCI_MAX_CURRENT_300_SHIFT) *
3236 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303237 }
3238 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003239 ocr_avail |= MMC_VDD_165_195;
3240
Aaron Lu55c46652012-07-04 13:31:48 +08003241 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303242 SDHCI_MAX_CURRENT_180_MASK) >>
3243 SDHCI_MAX_CURRENT_180_SHIFT) *
3244 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303245 }
3246
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003247 /* If OCR set by host, use it instead. */
3248 if (host->ocr_mask)
3249 ocr_avail = host->ocr_mask;
3250
3251 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003252 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003253 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003254
Takashi Iwai8f230f42010-12-08 10:04:30 +01003255 mmc->ocr_avail = ocr_avail;
3256 mmc->ocr_avail_sdio = ocr_avail;
3257 if (host->ocr_avail_sdio)
3258 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3259 mmc->ocr_avail_sd = ocr_avail;
3260 if (host->ocr_avail_sd)
3261 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3262 else /* normal SD controllers don't support 1.8V */
3263 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3264 mmc->ocr_avail_mmc = ocr_avail;
3265 if (host->ocr_avail_mmc)
3266 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003267
3268 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003269 pr_err("%s: Hardware doesn't report any support voltages.\n",
3270 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003271 ret = -ENODEV;
3272 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003273 }
3274
Pierre Ossmand129bce2006-03-24 03:18:17 -08003275 spin_lock_init(&host->lock);
3276
3277 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003278 * Maximum number of segments. Depends on if the hardware
3279 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003280 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003281 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003282 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003283 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003284 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003285 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003286 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003287
3288 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003289 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3290 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3291 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003292 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003293 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003294
3295 /*
3296 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003297 * of bytes. When doing hardware scatter/gather, each entry cannot
3298 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003299 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003300 if (host->flags & SDHCI_USE_ADMA) {
3301 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3302 mmc->max_seg_size = 65535;
3303 else
3304 mmc->max_seg_size = 65536;
3305 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003306 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003307 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003308
3309 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003310 * Maximum block size. This varies from controller to controller and
3311 * is specified in the capabilities register.
3312 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003313 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3314 mmc->max_blk_size = 2;
3315 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303316 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003317 SDHCI_MAX_BLOCK_SHIFT;
3318 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003319 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3320 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003321 mmc->max_blk_size = 0;
3322 }
3323 }
3324
3325 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003326
3327 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003328 * Maximum block count.
3329 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003330 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003331
3332 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003333 * Init tasklets.
3334 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003335 tasklet_init(&host->finish_tasklet,
3336 sdhci_tasklet_finish, (unsigned long)host);
3337
Al Viroe4cad1b2006-10-10 22:47:07 +01003338 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003339
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003340 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303341
Shawn Guo2af502c2013-07-05 14:38:55 +08003342 sdhci_init(host, 0);
3343
Russell King781e9892014-04-25 12:55:46 +01003344 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3345 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003346 if (ret) {
3347 pr_err("%s: Failed to request IRQ %d: %d\n",
3348 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003349 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003350 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003351
Pierre Ossmand129bce2006-03-24 03:18:17 -08003352#ifdef CONFIG_MMC_DEBUG
3353 sdhci_dumpregs(host);
3354#endif
3355
Adrian Hunter061d17a2016-04-12 14:25:09 +03003356 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003357 if (ret) {
3358 pr_err("%s: Failed to register LED device: %d\n",
3359 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003360 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003361 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003362
Pierre Ossman5f25a662006-10-04 02:15:39 -07003363 mmiowb();
3364
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003365 ret = mmc_add_host(mmc);
3366 if (ret)
3367 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003368
Girish K Sa3c76eb2011-10-11 11:44:09 +05303369 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003370 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003371 (host->flags & SDHCI_USE_ADMA) ?
3372 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003373 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003374
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003375 sdhci_enable_card_detection(host);
3376
Pierre Ossmand129bce2006-03-24 03:18:17 -08003377 return 0;
3378
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003379unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003380 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003381unirq:
Russell King03231f92014-04-25 12:57:12 +01003382 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003383 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3384 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003385 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003386untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003387 tasklet_kill(&host->finish_tasklet);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003388unreg:
3389 if (!IS_ERR(mmc->supply.vqmmc))
3390 regulator_disable(mmc->supply.vqmmc);
3391undma:
3392 if (host->align_buffer)
3393 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3394 host->adma_table_sz, host->align_buffer,
3395 host->align_addr);
3396 host->adma_table = NULL;
3397 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003398
3399 return ret;
3400}
3401
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003402EXPORT_SYMBOL_GPL(sdhci_add_host);
3403
Pierre Ossman1e728592008-04-16 19:13:13 +02003404void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003405{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003406 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003407 unsigned long flags;
3408
3409 if (dead) {
3410 spin_lock_irqsave(&host->lock, flags);
3411
3412 host->flags |= SDHCI_DEVICE_DEAD;
3413
3414 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303415 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003416 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003417
3418 host->mrq->cmd->error = -ENOMEDIUM;
3419 tasklet_schedule(&host->finish_tasklet);
3420 }
3421
3422 spin_unlock_irqrestore(&host->lock, flags);
3423 }
3424
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003425 sdhci_disable_card_detection(host);
3426
Markus Mayer4e743f12014-07-03 13:27:42 -07003427 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003428
Adrian Hunter061d17a2016-04-12 14:25:09 +03003429 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003430
Pierre Ossman1e728592008-04-16 19:13:13 +02003431 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003432 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003433
Russell Kingb537f942014-04-25 12:56:01 +01003434 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3435 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003436 free_irq(host->irq, host);
3437
3438 del_timer_sync(&host->timer);
3439
Pierre Ossmand129bce2006-03-24 03:18:17 -08003440 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003441
Tim Kryger3a48edc2014-06-13 10:13:56 -07003442 if (!IS_ERR(mmc->supply.vqmmc))
3443 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003444
Russell Kingedd63fc2016-01-26 13:39:50 +00003445 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003446 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3447 host->adma_table_sz, host->align_buffer,
3448 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003449
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003450 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003451 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003452}
3453
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003454EXPORT_SYMBOL_GPL(sdhci_remove_host);
3455
3456void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003457{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003458 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003459}
3460
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003461EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003462
3463/*****************************************************************************\
3464 * *
3465 * Driver init/exit *
3466 * *
3467\*****************************************************************************/
3468
3469static int __init sdhci_drv_init(void)
3470{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303471 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003472 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303473 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003474
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003475 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003476}
3477
3478static void __exit sdhci_drv_exit(void)
3479{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003480}
3481
3482module_init(sdhci_drv_init);
3483module_exit(sdhci_drv_exit);
3484
Pierre Ossmandf673b22006-06-30 02:22:31 -07003485module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003486module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003487
Pierre Ossman32710e82009-04-08 20:14:54 +02003488MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003489MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003491
Pierre Ossmandf673b22006-06-30 02:22:31 -07003492MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003493MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");