blob: 0e3d7c056cb135dfc1408dd30950f4327ccd84ba [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 Lu473b095a2012-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);
Dong Aishengded97e02016-04-16 01:29:25 +080051static int sdhci_get_cd(struct mmc_host *mmc);
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) {
Dong Aishengded97e02016-04-16 01:29:25 +0800196 if (!sdhci_get_cd(host->mmc))
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
Masahiro Yamada4f782302016-04-14 13:19:39 +0900262#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100263static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300264 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100265{
266 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
267 unsigned long flags;
268
269 spin_lock_irqsave(&host->lock, flags);
270
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300271 if (host->runtime_suspended)
272 goto out;
273
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100274 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300275 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100276 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300277 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300278out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100279 spin_unlock_irqrestore(&host->lock, flags);
280}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300281
282static int sdhci_led_register(struct sdhci_host *host)
283{
284 struct mmc_host *mmc = host->mmc;
285
286 snprintf(host->led_name, sizeof(host->led_name),
287 "%s::", mmc_hostname(mmc));
288
289 host->led.name = host->led_name;
290 host->led.brightness = LED_OFF;
291 host->led.default_trigger = mmc_hostname(mmc);
292 host->led.brightness_set = sdhci_led_control;
293
294 return led_classdev_register(mmc_dev(mmc), &host->led);
295}
296
297static void sdhci_led_unregister(struct sdhci_host *host)
298{
299 led_classdev_unregister(&host->led);
300}
301
302static inline void sdhci_led_activate(struct sdhci_host *host)
303{
304}
305
306static inline void sdhci_led_deactivate(struct sdhci_host *host)
307{
308}
309
310#else
311
312static inline int sdhci_led_register(struct sdhci_host *host)
313{
314 return 0;
315}
316
317static inline void sdhci_led_unregister(struct sdhci_host *host)
318{
319}
320
321static inline void sdhci_led_activate(struct sdhci_host *host)
322{
323 __sdhci_led_activate(host);
324}
325
326static inline void sdhci_led_deactivate(struct sdhci_host *host)
327{
328 __sdhci_led_deactivate(host);
329}
330
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100331#endif
332
Pierre Ossmand129bce2006-03-24 03:18:17 -0800333/*****************************************************************************\
334 * *
335 * Core functions *
336 * *
337\*****************************************************************************/
338
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100339static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800340{
Pierre Ossman76591502008-07-21 00:32:11 +0200341 unsigned long flags;
342 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700343 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200344 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800345
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100346 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200349 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800350
Pierre Ossman76591502008-07-21 00:32:11 +0200351 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800352
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100353 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300354 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800355
Pierre Ossman76591502008-07-21 00:32:11 +0200356 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800357
Pierre Ossman76591502008-07-21 00:32:11 +0200358 blksize -= len;
359 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200360
Pierre Ossman76591502008-07-21 00:32:11 +0200361 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800362
Pierre Ossman76591502008-07-21 00:32:11 +0200363 while (len) {
364 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300365 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200366 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800367 }
Pierre Ossman76591502008-07-21 00:32:11 +0200368
369 *buf = scratch & 0xFF;
370
371 buf++;
372 scratch >>= 8;
373 chunk--;
374 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800375 }
376 }
Pierre Ossman76591502008-07-21 00:32:11 +0200377
378 sg_miter_stop(&host->sg_miter);
379
380 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800382
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100383static void sdhci_write_block_pio(struct sdhci_host *host)
384{
Pierre Ossman76591502008-07-21 00:32:11 +0200385 unsigned long flags;
386 size_t blksize, len, chunk;
387 u32 scratch;
388 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100389
390 DBG("PIO writing\n");
391
392 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 0;
394 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100395
Pierre Ossman76591502008-07-21 00:32:11 +0200396 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100397
398 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300399 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100400
Pierre Ossman76591502008-07-21 00:32:11 +0200401 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200402
Pierre Ossman76591502008-07-21 00:32:11 +0200403 blksize -= len;
404 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100405
Pierre Ossman76591502008-07-21 00:32:11 +0200406 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407
Pierre Ossman76591502008-07-21 00:32:11 +0200408 while (len) {
409 scratch |= (u32)*buf << (chunk * 8);
410
411 buf++;
412 chunk++;
413 len--;
414
415 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300416 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200417 chunk = 0;
418 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100419 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100420 }
421 }
Pierre Ossman76591502008-07-21 00:32:11 +0200422
423 sg_miter_stop(&host->sg_miter);
424
425 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100426}
427
428static void sdhci_transfer_pio(struct sdhci_host *host)
429{
430 u32 mask;
431
432 BUG_ON(!host->data);
433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435 return;
436
437 if (host->data->flags & MMC_DATA_READ)
438 mask = SDHCI_DATA_AVAILABLE;
439 else
440 mask = SDHCI_SPACE_AVAILABLE;
441
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200442 /*
443 * Some controllers (JMicron JMB38x) mess up the buffer bits
444 * for transfers < 4 bytes. As long as it is just one block,
445 * we can ignore the bits.
446 */
447 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
448 (host->data->blocks == 1))
449 mask = ~0;
450
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300451 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300452 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
453 udelay(100);
454
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100455 if (host->data->flags & MMC_DATA_READ)
456 sdhci_read_block_pio(host);
457 else
458 sdhci_write_block_pio(host);
459
Pierre Ossman76591502008-07-21 00:32:11 +0200460 host->blocks--;
461 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100462 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100463 }
464
465 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800466}
467
Russell King48857d92016-01-26 13:40:16 +0000468static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000469 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000470{
471 int sg_count;
472
Russell King94538e52016-01-26 13:40:37 +0000473 /*
474 * If the data buffers are already mapped, return the previous
475 * dma_map_sg() result.
476 */
477 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000478 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000479
480 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
481 data->flags & MMC_DATA_WRITE ?
482 DMA_TO_DEVICE : DMA_FROM_DEVICE);
483
484 if (sg_count == 0)
485 return -ENOSPC;
486
487 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000488 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000489
490 return sg_count;
491}
492
Pierre Ossman2134a922008-06-28 18:28:51 +0200493static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
494{
495 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800496 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200497}
498
499static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
500{
Cong Wang482fce92011-11-27 13:27:00 +0800501 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200502 local_irq_restore(*flags);
503}
504
Adrian Huntere57a5f62014-11-04 12:42:46 +0200505static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
506 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800507{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200508 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800509
Adrian Huntere57a5f62014-11-04 12:42:46 +0200510 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200511 dma_desc->cmd = cpu_to_le16(cmd);
512 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200513 dma_desc->addr_lo = cpu_to_le32((u32)addr);
514
515 if (host->flags & SDHCI_USE_64_BIT_DMA)
516 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800517}
518
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200519static void sdhci_adma_mark_end(void *desc)
520{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200521 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200522
Adrian Huntere57a5f62014-11-04 12:42:46 +0200523 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200524 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200525}
526
Russell King60c64762016-01-26 13:40:22 +0000527static void sdhci_adma_table_pre(struct sdhci_host *host,
528 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200529{
Pierre Ossman2134a922008-06-28 18:28:51 +0200530 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200531 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000532 dma_addr_t addr, align_addr;
533 void *desc, *align;
534 char *buffer;
535 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200536
537 /*
538 * The spec does not specify endianness of descriptor table.
539 * We currently guess that it is LE.
540 */
541
Russell King60c64762016-01-26 13:40:22 +0000542 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200543
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200544 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200545 align = host->align_buffer;
546
547 align_addr = host->align_addr;
548
549 for_each_sg(data->sg, sg, host->sg_count, i) {
550 addr = sg_dma_address(sg);
551 len = sg_dma_len(sg);
552
553 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000554 * The SDHCI specification states that ADMA addresses must
555 * be 32-bit aligned. If they aren't, then we use a bounce
556 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200557 * alignment.
558 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200559 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
560 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561 if (offset) {
562 if (data->flags & MMC_DATA_WRITE) {
563 buffer = sdhci_kmap_atomic(sg, &flags);
564 memcpy(align, buffer, offset);
565 sdhci_kunmap_atomic(buffer, &flags);
566 }
567
Ben Dooks118cd172010-03-05 13:43:26 -0800568 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200569 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200570 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200571
572 BUG_ON(offset > 65536);
573
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200574 align += SDHCI_ADMA2_ALIGN;
575 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200576
Adrian Hunter76fe3792014-11-04 12:42:42 +0200577 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 addr += offset;
580 len -= offset;
581 }
582
Pierre Ossman2134a922008-06-28 18:28:51 +0200583 BUG_ON(len > 65536);
584
Adrian Hunter347ea322015-11-26 14:00:48 +0200585 if (len) {
586 /* tran, valid */
587 sdhci_adma_write_desc(host, desc, addr, len,
588 ADMA2_TRAN_VALID);
589 desc += host->desc_sz;
590 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200591
592 /*
593 * If this triggers then we have a calculation bug
594 * somewhere. :/
595 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200596 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200597 }
598
Thomas Abraham70764a92010-05-26 14:42:04 -0700599 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000600 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200601 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200602 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200603 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700604 }
605 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000606 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200607 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700608 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200609}
610
611static void sdhci_adma_table_post(struct sdhci_host *host,
612 struct mmc_data *data)
613{
Pierre Ossman2134a922008-06-28 18:28:51 +0200614 struct scatterlist *sg;
615 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200616 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200617 char *buffer;
618 unsigned long flags;
619
Russell King47fa9612016-01-26 13:40:06 +0000620 if (data->flags & MMC_DATA_READ) {
621 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100622
Russell King47fa9612016-01-26 13:40:06 +0000623 /* Do a quick scan of the SG list for any unaligned mappings */
624 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200625 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000626 has_unaligned = true;
627 break;
628 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200629
Russell King47fa9612016-01-26 13:40:06 +0000630 if (has_unaligned) {
631 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000632 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200633
Russell King47fa9612016-01-26 13:40:06 +0000634 align = host->align_buffer;
635
636 for_each_sg(data->sg, sg, host->sg_count, i) {
637 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
638 size = SDHCI_ADMA2_ALIGN -
639 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
640
641 buffer = sdhci_kmap_atomic(sg, &flags);
642 memcpy(buffer, align, size);
643 sdhci_kunmap_atomic(buffer, &flags);
644
645 align += SDHCI_ADMA2_ALIGN;
646 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200647 }
648 }
649 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200650}
651
Andrei Warkentina3c77782011-04-11 16:13:42 -0500652static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800653{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700654 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500655 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700656 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800657
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200658 /*
659 * If the host controller provides us with an incorrect timeout
660 * value, just skip the check and use 0xE. The hardware may take
661 * longer to time out, but that's much better than having a too-short
662 * timeout value.
663 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200664 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200665 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200666
Andrei Warkentina3c77782011-04-11 16:13:42 -0500667 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100668 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500669 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800670
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 /* timeout in us */
672 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100673 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300674 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000675 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000676 if (host->clock && data->timeout_clks) {
677 unsigned long long val;
678
679 /*
680 * data->timeout_clks is in units of clock cycles.
681 * host->clock is in Hz. target_timeout is in us.
682 * Hence, us = 1000000 * cycles / Hz. Round up.
683 */
684 val = 1000000 * data->timeout_clks;
685 if (do_div(val, host->clock))
686 target_timeout++;
687 target_timeout += val;
688 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300689 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700690
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700691 /*
692 * Figure out needed cycles.
693 * We do this in steps in order to fit inside a 32 bit int.
694 * The first step is the minimum timeout, which will have a
695 * minimum resolution of 6 bits:
696 * (1) 2^13*1000 > 2^22,
697 * (2) host->timeout_clk < 2^16
698 * =>
699 * (1) / (2) > 2^6
700 */
701 count = 0;
702 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
703 while (current_timeout < target_timeout) {
704 count++;
705 current_timeout <<= 1;
706 if (count >= 0xF)
707 break;
708 }
709
710 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400711 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
712 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700713 count = 0xE;
714 }
715
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200716 return count;
717}
718
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300719static void sdhci_set_transfer_irqs(struct sdhci_host *host)
720{
721 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
722 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
723
724 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100725 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300726 else
Russell Kingb537f942014-04-25 12:56:01 +0100727 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
728
729 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
730 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300731}
732
Aisheng Dongb45e6682014-08-27 15:26:29 +0800733static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200734{
735 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800736
737 if (host->ops->set_timeout) {
738 host->ops->set_timeout(host, cmd);
739 } else {
740 count = sdhci_calc_timeout(host, cmd);
741 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
742 }
743}
744
745static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
746{
Pierre Ossman2134a922008-06-28 18:28:51 +0200747 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500748 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200749
750 WARN_ON(host->data);
751
Aisheng Dongb45e6682014-08-27 15:26:29 +0800752 if (data || (cmd->flags & MMC_RSP_BUSY))
753 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500754
755 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200756 return;
757
758 /* Sanity checks */
759 BUG_ON(data->blksz * data->blocks > 524288);
760 BUG_ON(data->blksz > host->mmc->max_blk_size);
761 BUG_ON(data->blocks > 65535);
762
763 host->data = data;
764 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400765 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200766
Russell Kingfce14422016-01-26 13:41:20 +0000767 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200768 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000769 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000770 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200771
Russell Kingfce14422016-01-26 13:41:20 +0000772 host->flags |= SDHCI_REQ_USE_DMA;
773
774 /*
775 * FIXME: This doesn't account for merging when mapping the
776 * scatterlist.
777 *
778 * The assumption here being that alignment and lengths are
779 * the same after DMA mapping to device address space.
780 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000781 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000782 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200783 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000784 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000785 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000786 /*
787 * As we use up to 3 byte chunks to work
788 * around alignment problems, we need to
789 * check the offset as well.
790 */
791 offset_mask = 3;
792 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200793 } else {
794 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000795 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000796 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
797 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200798 }
799
Russell Kingdf953922016-01-26 13:41:14 +0000800 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200801 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000802 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100803 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000804 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200805 host->flags &= ~SDHCI_REQ_USE_DMA;
806 break;
807 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000808 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100809 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200810 host->flags &= ~SDHCI_REQ_USE_DMA;
811 break;
812 }
813 }
814 }
815 }
816
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200817 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000818 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200819
Russell King60c64762016-01-26 13:40:22 +0000820 if (sg_cnt <= 0) {
821 /*
822 * This only happens when someone fed
823 * us an invalid request.
824 */
825 WARN_ON(1);
826 host->flags &= ~SDHCI_REQ_USE_DMA;
827 } else if (host->flags & SDHCI_USE_ADMA) {
828 sdhci_adma_table_pre(host, data, sg_cnt);
829
830 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
831 if (host->flags & SDHCI_USE_64_BIT_DMA)
832 sdhci_writel(host,
833 (u64)host->adma_addr >> 32,
834 SDHCI_ADMA_ADDRESS_HI);
835 } else {
836 WARN_ON(sg_cnt != 1);
837 sdhci_writel(host, sg_dma_address(data->sg),
838 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200839 }
840 }
841
Pierre Ossman2134a922008-06-28 18:28:51 +0200842 /*
843 * Always adjust the DMA selection as some controllers
844 * (e.g. JMicron) can't do PIO properly when the selection
845 * is ADMA.
846 */
847 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300848 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200849 ctrl &= ~SDHCI_CTRL_DMA_MASK;
850 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200851 (host->flags & SDHCI_USE_ADMA)) {
852 if (host->flags & SDHCI_USE_64_BIT_DMA)
853 ctrl |= SDHCI_CTRL_ADMA64;
854 else
855 ctrl |= SDHCI_CTRL_ADMA32;
856 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200857 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200858 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300859 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100860 }
861
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200862 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200863 int flags;
864
865 flags = SG_MITER_ATOMIC;
866 if (host->data->flags & MMC_DATA_READ)
867 flags |= SG_MITER_TO_SG;
868 else
869 flags |= SG_MITER_FROM_SG;
870 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200871 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800872 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700873
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300874 sdhci_set_transfer_irqs(host);
875
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400876 /* Set the DMA boundary value and block size */
877 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
878 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300879 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880}
881
882static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500883 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700884{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800885 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500886 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887
Dong Aisheng2b558c12013-10-30 22:09:48 +0800888 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800889 if (host->quirks2 &
890 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
891 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
892 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800893 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800894 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
895 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800896 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800897 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800899 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700900
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200901 WARN_ON(!host->data);
902
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800903 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
904 mode = SDHCI_TRNS_BLK_CNT_EN;
905
Andrei Warkentine89d4562011-05-23 15:06:37 -0500906 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800907 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500908 /*
909 * If we are sending CMD23, CMD12 never gets sent
910 * on successful completion (so no Auto-CMD12).
911 */
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800912 if (!host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
913 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500914 mode |= SDHCI_TRNS_AUTO_CMD12;
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500915 else if (host->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
916 mode |= SDHCI_TRNS_AUTO_CMD23;
917 sdhci_writel(host, host->mrq->sbc->arg, SDHCI_ARGUMENT2);
918 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700919 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500920
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 if (data->flags & MMC_DATA_READ)
922 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100923 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700924 mode |= SDHCI_TRNS_DMA;
925
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300926 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800927}
928
929static void sdhci_finish_data(struct sdhci_host *host)
930{
931 struct mmc_data *data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800932
933 BUG_ON(!host->data);
934
935 data = host->data;
936 host->data = NULL;
937
Russell Kingadd89132016-01-26 13:40:42 +0000938 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
939 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
940 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941
942 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200943 * The specification states that the block count register must
944 * be updated, but it does not specify at what point in the
945 * data flow. That makes the register entirely useless to read
946 * back so we have to assume that nothing made it to the card
947 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -0800948 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200949 if (data->error)
950 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800951 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +0200952 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800953
Andrei Warkentine89d4562011-05-23 15:06:37 -0500954 /*
955 * Need to send CMD12 if -
956 * a) open-ended multiblock transfer (no CMD23)
957 * b) error in multiblock transfer
958 */
959 if (data->stop &&
960 (data->error ||
961 !host->mrq->sbc)) {
962
Pierre Ossmand129bce2006-03-24 03:18:17 -0800963 /*
964 * The controller needs a reset of internal state machines
965 * upon error conditions.
966 */
Pierre Ossman17b04292007-07-22 22:18:46 +0200967 if (data->error) {
Russell King03231f92014-04-25 12:57:12 +0100968 sdhci_do_reset(host, SDHCI_RESET_CMD);
969 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800970 }
971
972 sdhci_send_command(host, data->stop);
973 } else
974 tasklet_schedule(&host->finish_tasklet);
975}
976
Dong Aishengc0e551292013-09-13 19:11:31 +0800977void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800978{
979 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700980 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700981 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800982
983 WARN_ON(host->cmd);
984
Russell King96776202016-01-26 13:39:34 +0000985 /* Initially, a command has no error */
986 cmd->error = 0;
987
Pierre Ossmand129bce2006-03-24 03:18:17 -0800988 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -0700989 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -0700990
991 mask = SDHCI_CMD_INHIBIT;
992 if ((cmd->data != NULL) || (cmd->flags & MMC_RSP_BUSY))
993 mask |= SDHCI_DATA_INHIBIT;
994
995 /* We shouldn't wait for data inihibit for stop commands, even
996 though they might use busy signaling */
997 if (host->mrq->data && (cmd == host->mrq->data->stop))
998 mask &= ~SDHCI_DATA_INHIBIT;
999
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001000 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001001 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001002 pr_err("%s: Controller never released inhibit bit(s).\n",
1003 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001005 cmd->error = -EIO;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 tasklet_schedule(&host->finish_tasklet);
1007 return;
1008 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001009 timeout--;
1010 mdelay(1);
1011 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001012
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001013 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001014 if (!cmd->data && cmd->busy_timeout > 9000)
1015 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001016 else
1017 timeout += 10 * HZ;
1018 mod_timer(&host->timer, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001019
1020 host->cmd = cmd;
Chanho Mine99783a2014-08-30 12:40:40 +09001021 host->busy_handle = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001022
Andrei Warkentina3c77782011-04-11 16:13:42 -05001023 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001024
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001025 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026
Andrei Warkentine89d4562011-05-23 15:06:37 -05001027 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001028
Pierre Ossmand129bce2006-03-24 03:18:17 -08001029 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301030 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001032 cmd->error = -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033 tasklet_schedule(&host->finish_tasklet);
1034 return;
1035 }
1036
1037 if (!(cmd->flags & MMC_RSP_PRESENT))
1038 flags = SDHCI_CMD_RESP_NONE;
1039 else if (cmd->flags & MMC_RSP_136)
1040 flags = SDHCI_CMD_RESP_LONG;
1041 else if (cmd->flags & MMC_RSP_BUSY)
1042 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1043 else
1044 flags = SDHCI_CMD_RESP_SHORT;
1045
1046 if (cmd->flags & MMC_RSP_CRC)
1047 flags |= SDHCI_CMD_CRC;
1048 if (cmd->flags & MMC_RSP_OPCODE)
1049 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301050
1051 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301052 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1053 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001054 flags |= SDHCI_CMD_DATA;
1055
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001056 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001057}
Dong Aishengc0e551292013-09-13 19:11:31 +08001058EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001059
1060static void sdhci_finish_command(struct sdhci_host *host)
1061{
1062 int i;
1063
1064 BUG_ON(host->cmd == NULL);
1065
1066 if (host->cmd->flags & MMC_RSP_PRESENT) {
1067 if (host->cmd->flags & MMC_RSP_136) {
1068 /* CRC is stripped so we need to do some shifting. */
1069 for (i = 0;i < 4;i++) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001070 host->cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071 SDHCI_RESPONSE + (3-i)*4) << 8;
1072 if (i != 3)
1073 host->cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001074 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075 SDHCI_RESPONSE + (3-i)*4-1);
1076 }
1077 } else {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001078 host->cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001079 }
1080 }
1081
Andrei Warkentine89d4562011-05-23 15:06:37 -05001082 /* Finished CMD23, now send actual command. */
1083 if (host->cmd == host->mrq->sbc) {
1084 host->cmd = NULL;
1085 sdhci_send_command(host, host->mrq->cmd);
1086 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001087
Andrei Warkentine89d4562011-05-23 15:06:37 -05001088 /* Processed actual command. */
1089 if (host->data && host->data_early)
1090 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001091
Andrei Warkentine89d4562011-05-23 15:06:37 -05001092 if (!host->cmd->data)
1093 tasklet_schedule(&host->finish_tasklet);
1094
1095 host->cmd = NULL;
1096 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001097}
1098
Kevin Liu52983382013-01-31 11:31:37 +08001099static u16 sdhci_get_preset_value(struct sdhci_host *host)
1100{
Russell Kingd975f122014-04-25 12:59:31 +01001101 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001102
Russell Kingd975f122014-04-25 12:59:31 +01001103 switch (host->timing) {
1104 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001105 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1106 break;
Russell Kingd975f122014-04-25 12:59:31 +01001107 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001108 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1109 break;
Russell Kingd975f122014-04-25 12:59:31 +01001110 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001111 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1112 break;
Russell Kingd975f122014-04-25 12:59:31 +01001113 case MMC_TIMING_UHS_SDR104:
1114 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001115 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1116 break;
Russell Kingd975f122014-04-25 12:59:31 +01001117 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001118 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001119 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1120 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001121 case MMC_TIMING_MMC_HS400:
1122 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1123 break;
Kevin Liu52983382013-01-31 11:31:37 +08001124 default:
1125 pr_warn("%s: Invalid UHS-I mode selected\n",
1126 mmc_hostname(host->mmc));
1127 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1128 break;
1129 }
1130 return preset;
1131}
1132
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001133u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1134 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301136 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001137 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301138 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001139 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140
Zhangfei Gao85105c52010-08-06 07:10:01 +08001141 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001142 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001143 u16 pre_val;
1144
1145 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1146 pre_val = sdhci_get_preset_value(host);
1147 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1148 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1149 if (host->clk_mul &&
1150 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1151 clk = SDHCI_PROG_CLOCK_MODE;
1152 real_div = div + 1;
1153 clk_mul = host->clk_mul;
1154 } else {
1155 real_div = max_t(int, 1, div << 1);
1156 }
1157 goto clock_set;
1158 }
1159
Arindam Nathc3ed3872011-05-05 12:19:06 +05301160 /*
1161 * Check if the Host Controller supports Programmable Clock
1162 * Mode.
1163 */
1164 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001165 for (div = 1; div <= 1024; div++) {
1166 if ((host->max_clk * host->clk_mul / div)
1167 <= clock)
1168 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001169 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001170 if ((host->max_clk * host->clk_mul / div) <= clock) {
1171 /*
1172 * Set Programmable Clock Mode in the Clock
1173 * Control register.
1174 */
1175 clk = SDHCI_PROG_CLOCK_MODE;
1176 real_div = div;
1177 clk_mul = host->clk_mul;
1178 div--;
1179 } else {
1180 /*
1181 * Divisor can be too small to reach clock
1182 * speed requirement. Then use the base clock.
1183 */
1184 switch_base_clk = true;
1185 }
1186 }
1187
1188 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301189 /* Version 3.00 divisors must be a multiple of 2. */
1190 if (host->max_clk <= clock)
1191 div = 1;
1192 else {
1193 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1194 div += 2) {
1195 if ((host->max_clk / div) <= clock)
1196 break;
1197 }
1198 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001199 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301200 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301201 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1202 && !div && host->max_clk <= 25000000)
1203 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001204 }
1205 } else {
1206 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001207 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001208 if ((host->max_clk / div) <= clock)
1209 break;
1210 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001211 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301212 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001213 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214
Kevin Liu52983382013-01-31 11:31:37 +08001215clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001216 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001217 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301218 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001219 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1220 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001221
1222 return clk;
1223}
1224EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1225
1226void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1227{
1228 u16 clk;
1229 unsigned long timeout;
1230
1231 host->mmc->actual_clock = 0;
1232
1233 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001234
1235 if (clock == 0)
1236 return;
1237
1238 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1239
Pierre Ossmand129bce2006-03-24 03:18:17 -08001240 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001241 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242
Chris Ball27f6cb12009-09-22 16:45:31 -07001243 /* Wait max 20 ms */
1244 timeout = 20;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001245 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001246 & SDHCI_CLOCK_INT_STABLE)) {
1247 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001248 pr_err("%s: Internal clock never stabilised.\n",
1249 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001250 sdhci_dumpregs(host);
1251 return;
1252 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001253 timeout--;
1254 mdelay(1);
1255 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256
1257 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001258 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001259}
Russell King17710592014-04-25 12:58:55 +01001260EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261
Adrian Hunter1dceb042016-03-29 12:45:43 +03001262static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1263 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001264{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001265 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001266
1267 spin_unlock_irq(&host->lock);
1268 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1269 spin_lock_irq(&host->lock);
1270
1271 if (mode != MMC_POWER_OFF)
1272 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1273 else
1274 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1275}
1276
1277void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1278 unsigned short vdd)
1279{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001280 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001281
Russell King24fbb3c2014-04-25 13:00:06 +01001282 if (mode != MMC_POWER_OFF) {
1283 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001284 case MMC_VDD_165_195:
1285 pwr = SDHCI_POWER_180;
1286 break;
1287 case MMC_VDD_29_30:
1288 case MMC_VDD_30_31:
1289 pwr = SDHCI_POWER_300;
1290 break;
1291 case MMC_VDD_32_33:
1292 case MMC_VDD_33_34:
1293 pwr = SDHCI_POWER_330;
1294 break;
1295 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001296 WARN(1, "%s: Invalid vdd %#x\n",
1297 mmc_hostname(host->mmc), vdd);
1298 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001299 }
1300 }
1301
1302 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001303 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001304
Pierre Ossmanae628902009-05-03 20:45:03 +02001305 host->pwr = pwr;
1306
1307 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001308 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001309 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1310 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001311 } else {
1312 /*
1313 * Spec says that we should clear the power reg before setting
1314 * a new value. Some controllers don't seem to like this though.
1315 */
1316 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1317 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001318
Russell Kinge921a8b2014-04-25 13:00:01 +01001319 /*
1320 * At least the Marvell CaFe chip gets confused if we set the
1321 * voltage and set turn on power at the same time, so set the
1322 * voltage first.
1323 */
1324 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1325 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001326
Russell Kinge921a8b2014-04-25 13:00:01 +01001327 pwr |= SDHCI_POWER_ON;
1328
Pierre Ossmanae628902009-05-03 20:45:03 +02001329 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1330
Russell Kinge921a8b2014-04-25 13:00:01 +01001331 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1332 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001333
Russell Kinge921a8b2014-04-25 13:00:01 +01001334 /*
1335 * Some controllers need an extra 10ms delay of 10ms before
1336 * they can apply clock after applying power
1337 */
1338 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1339 mdelay(10);
1340 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001341}
1342EXPORT_SYMBOL_GPL(sdhci_set_power);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001343
Adrian Hunter1dceb042016-03-29 12:45:43 +03001344static void __sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1345 unsigned short vdd)
1346{
1347 struct mmc_host *mmc = host->mmc;
1348
1349 if (host->ops->set_power)
1350 host->ops->set_power(host, mode, vdd);
1351 else if (!IS_ERR(mmc->supply.vmmc))
1352 sdhci_set_power_reg(host, mode, vdd);
1353 else
1354 sdhci_set_power(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001355}
1356
Pierre Ossmand129bce2006-03-24 03:18:17 -08001357/*****************************************************************************\
1358 * *
1359 * MMC callbacks *
1360 * *
1361\*****************************************************************************/
1362
1363static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1364{
1365 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001366 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367 unsigned long flags;
1368
1369 host = mmc_priv(mmc);
1370
Scott Branden04e079c2015-03-10 11:35:10 -07001371 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001372 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001373
Pierre Ossmand129bce2006-03-24 03:18:17 -08001374 spin_lock_irqsave(&host->lock, flags);
1375
1376 WARN_ON(host->mrq != NULL);
1377
Adrian Hunter061d17a2016-04-12 14:25:09 +03001378 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001379
1380 /*
1381 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1382 * requests if Auto-CMD12 is enabled.
1383 */
1384 if (!mrq->sbc && (host->flags & SDHCI_AUTO_CMD12)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001385 if (mrq->stop) {
1386 mrq->data->stop = NULL;
1387 mrq->stop = NULL;
1388 }
1389 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001390
1391 host->mrq = mrq;
1392
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001393 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Pierre Ossman17b04292007-07-22 22:18:46 +02001394 host->mrq->cmd->error = -ENOMEDIUM;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001395 tasklet_schedule(&host->finish_tasklet);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301396 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001397 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001398 sdhci_send_command(host, mrq->sbc);
1399 else
1400 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301401 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001402
Pierre Ossman5f25a662006-10-04 02:15:39 -07001403 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001404 spin_unlock_irqrestore(&host->lock, flags);
1405}
1406
Russell King2317f562014-04-25 12:57:07 +01001407void sdhci_set_bus_width(struct sdhci_host *host, int width)
1408{
1409 u8 ctrl;
1410
1411 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1412 if (width == MMC_BUS_WIDTH_8) {
1413 ctrl &= ~SDHCI_CTRL_4BITBUS;
1414 if (host->version >= SDHCI_SPEC_300)
1415 ctrl |= SDHCI_CTRL_8BITBUS;
1416 } else {
1417 if (host->version >= SDHCI_SPEC_300)
1418 ctrl &= ~SDHCI_CTRL_8BITBUS;
1419 if (width == MMC_BUS_WIDTH_4)
1420 ctrl |= SDHCI_CTRL_4BITBUS;
1421 else
1422 ctrl &= ~SDHCI_CTRL_4BITBUS;
1423 }
1424 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1425}
1426EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1427
Russell King96d7b782014-04-25 12:59:26 +01001428void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1429{
1430 u16 ctrl_2;
1431
1432 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1433 /* Select Bus Speed Mode for host */
1434 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1435 if ((timing == MMC_TIMING_MMC_HS200) ||
1436 (timing == MMC_TIMING_UHS_SDR104))
1437 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1438 else if (timing == MMC_TIMING_UHS_SDR12)
1439 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1440 else if (timing == MMC_TIMING_UHS_SDR25)
1441 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1442 else if (timing == MMC_TIMING_UHS_SDR50)
1443 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1444 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1445 (timing == MMC_TIMING_MMC_DDR52))
1446 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001447 else if (timing == MMC_TIMING_MMC_HS400)
1448 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001449 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1450}
1451EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1452
Dong Aishengded97e02016-04-16 01:29:25 +08001453static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001454{
Dong Aishengded97e02016-04-16 01:29:25 +08001455 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001456 unsigned long flags;
1457 u8 ctrl;
1458
Pierre Ossmand129bce2006-03-24 03:18:17 -08001459 spin_lock_irqsave(&host->lock, flags);
1460
Adrian Hunterceb61432011-12-27 15:48:41 +02001461 if (host->flags & SDHCI_DEVICE_DEAD) {
1462 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001463 if (!IS_ERR(mmc->supply.vmmc) &&
1464 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001465 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001466 return;
1467 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001468
Pierre Ossmand129bce2006-03-24 03:18:17 -08001469 /*
1470 * Reset the chip on each power off.
1471 * Should clear out any weird states.
1472 */
1473 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001474 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001475 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 }
1477
Kevin Liu52983382013-01-31 11:31:37 +08001478 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001479 (ios->power_mode == MMC_POWER_UP) &&
1480 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001481 sdhci_enable_preset_value(host, false);
1482
Russell King373073e2014-04-25 12:58:45 +01001483 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001484 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001485 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001486
1487 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1488 host->clock) {
1489 host->timeout_clk = host->mmc->actual_clock ?
1490 host->mmc->actual_clock / 1000 :
1491 host->clock / 1000;
1492 host->mmc->max_busy_timeout =
1493 host->ops->get_max_timeout_count ?
1494 host->ops->get_max_timeout_count(host) :
1495 1 << 27;
1496 host->mmc->max_busy_timeout /= host->timeout_clk;
1497 }
Russell King373073e2014-04-25 12:58:45 +01001498 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499
Adrian Hunter1dceb042016-03-29 12:45:43 +03001500 __sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001501
Philip Rakity643a81f2010-09-23 08:24:32 -07001502 if (host->ops->platform_send_init_74_clocks)
1503 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1504
Russell King2317f562014-04-25 12:57:07 +01001505 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001506
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001507 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001508
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001509 if ((ios->timing == MMC_TIMING_SD_HS ||
1510 ios->timing == MMC_TIMING_MMC_HS)
1511 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001512 ctrl |= SDHCI_CTRL_HISPD;
1513 else
1514 ctrl &= ~SDHCI_CTRL_HISPD;
1515
Arindam Nathd6d50a12011-05-05 12:18:59 +05301516 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301517 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301518
1519 /* In case of UHS-I modes, set High Speed Enable */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001520 if ((ios->timing == MMC_TIMING_MMC_HS400) ||
1521 (ios->timing == MMC_TIMING_MMC_HS200) ||
Seungwon Jeonbb8175a2014-03-14 21:12:48 +09001522 (ios->timing == MMC_TIMING_MMC_DDR52) ||
Girish K S069c9f12012-01-06 09:56:39 +05301523 (ios->timing == MMC_TIMING_UHS_SDR50) ||
Arindam Nath49c468f2011-05-05 12:19:01 +05301524 (ios->timing == MMC_TIMING_UHS_SDR104) ||
1525 (ios->timing == MMC_TIMING_UHS_DDR50) ||
Alexander Elbsdd8df172012-01-03 23:26:53 -05001526 (ios->timing == MMC_TIMING_UHS_SDR25))
Arindam Nath49c468f2011-05-05 12:19:01 +05301527 ctrl |= SDHCI_CTRL_HISPD;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301528
Russell Kingda91a8f2014-04-25 13:00:12 +01001529 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301530 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301531 /*
1532 * We only need to set Driver Strength if the
1533 * preset value enable is not set.
1534 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001535 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301536 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1537 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1538 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001539 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1540 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301541 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1542 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001543 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1544 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1545 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001546 pr_warn("%s: invalid driver type, default to driver type B\n",
1547 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001548 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1549 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301550
1551 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301552 } else {
1553 /*
1554 * According to SDHC Spec v3.00, if the Preset Value
1555 * Enable in the Host Control 2 register is set, we
1556 * need to reset SD Clock Enable before changing High
1557 * Speed Enable to avoid generating clock gliches.
1558 */
Arindam Nath758535c2011-05-05 12:19:00 +05301559
1560 /* Reset SD Clock Enable */
1561 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1562 clk &= ~SDHCI_CLOCK_CARD_EN;
1563 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1564
1565 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1566
1567 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001568 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301569 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301570
Arindam Nath49c468f2011-05-05 12:19:01 +05301571 /* Reset SD Clock Enable */
1572 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1573 clk &= ~SDHCI_CLOCK_CARD_EN;
1574 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1575
Russell King96d7b782014-04-25 12:59:26 +01001576 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001577 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301578
Kevin Liu52983382013-01-31 11:31:37 +08001579 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1580 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1581 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1582 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1583 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001584 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1585 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001586 u16 preset;
1587
1588 sdhci_enable_preset_value(host, true);
1589 preset = sdhci_get_preset_value(host);
1590 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1591 >> SDHCI_PRESET_DRV_SHIFT;
1592 }
1593
Arindam Nath49c468f2011-05-05 12:19:01 +05301594 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001595 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301596 } else
1597 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301598
Leandro Dorileob8352262007-07-25 23:47:04 +02001599 /*
1600 * Some (ENE) controllers go apeshit on some ios operation,
1601 * signalling timeout and CRC errors even on CMD0. Resetting
1602 * it on each ios seems to solve the problem.
1603 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301604 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001605 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001606
Pierre Ossman5f25a662006-10-04 02:15:39 -07001607 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001608 spin_unlock_irqrestore(&host->lock, flags);
1609}
1610
Dong Aishengded97e02016-04-16 01:29:25 +08001611static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001612{
1613 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001614 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001615
1616 if (host->flags & SDHCI_DEVICE_DEAD)
1617 return 0;
1618
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001619 /* If nonremovable, assume that the card is always present. */
1620 if (host->mmc->caps & MMC_CAP_NONREMOVABLE)
Kevin Liu94144a42013-02-28 17:35:53 +08001621 return 1;
1622
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001623 /*
1624 * Try slot gpio detect, if defined it take precedence
1625 * over build in controller functionality
1626 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001627 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001628 return !!gpio_cd;
1629
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001630 /* If polling, assume that the card is always present. */
1631 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1632 return 1;
1633
Kevin Liu94144a42013-02-28 17:35:53 +08001634 /* Host native card detect */
1635 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1636}
1637
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001638static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001639{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001640 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001641 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642
Pierre Ossmand129bce2006-03-24 03:18:17 -08001643 spin_lock_irqsave(&host->lock, flags);
1644
Pierre Ossman1e728592008-04-16 19:13:13 +02001645 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001646 is_readonly = 0;
1647 else if (host->ops->get_ro)
1648 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001649 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001650 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1651 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001652
1653 spin_unlock_irqrestore(&host->lock, flags);
1654
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001655 /* This quirk needs to be replaced by a callback-function later */
1656 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1657 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658}
1659
Takashi Iwai82b0e232011-04-21 20:26:38 +02001660#define SAMPLE_COUNT 5
1661
Dong Aishengded97e02016-04-16 01:29:25 +08001662static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001663{
Dong Aishengded97e02016-04-16 01:29:25 +08001664 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001665 int i, ro_count;
1666
Takashi Iwai82b0e232011-04-21 20:26:38 +02001667 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001668 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001669
1670 ro_count = 0;
1671 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001672 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001673 if (++ro_count > SAMPLE_COUNT / 2)
1674 return 1;
1675 }
1676 msleep(30);
1677 }
1678 return 0;
1679}
1680
Adrian Hunter20758b62011-08-29 16:42:12 +03001681static void sdhci_hw_reset(struct mmc_host *mmc)
1682{
1683 struct sdhci_host *host = mmc_priv(mmc);
1684
1685 if (host->ops && host->ops->hw_reset)
1686 host->ops->hw_reset(host);
1687}
1688
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001689static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1690{
Russell Kingbe138552014-04-25 12:55:56 +01001691 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001692 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001693 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001694 else
Russell Kingb537f942014-04-25 12:56:01 +01001695 host->ier &= ~SDHCI_INT_CARD_INT;
1696
1697 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1698 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001699 mmiowb();
1700 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001701}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001702
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001703static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1704{
1705 struct sdhci_host *host = mmc_priv(mmc);
1706 unsigned long flags;
1707
1708 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001709 if (enable)
1710 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1711 else
1712 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1713
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001714 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001715 spin_unlock_irqrestore(&host->lock, flags);
1716}
1717
Dong Aishengded97e02016-04-16 01:29:25 +08001718static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1719 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001720{
Dong Aishengded97e02016-04-16 01:29:25 +08001721 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001722 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001723 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001724
1725 /*
1726 * Signal Voltage Switching is only applicable for Host Controllers
1727 * v3.00 and above.
1728 */
1729 if (host->version < SDHCI_SPEC_300)
1730 return 0;
1731
Philip Rakity6231f3d2012-07-23 15:56:23 -07001732 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001733
Fabio Estevam21f59982013-02-14 10:35:03 -02001734 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001735 case MMC_SIGNAL_VOLTAGE_330:
1736 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1737 ctrl &= ~SDHCI_CTRL_VDD_180;
1738 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1739
Tim Kryger3a48edc2014-06-13 10:13:56 -07001740 if (!IS_ERR(mmc->supply.vqmmc)) {
1741 ret = regulator_set_voltage(mmc->supply.vqmmc, 2700000,
1742 3600000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001743 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001744 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1745 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001746 return -EIO;
1747 }
1748 }
1749 /* Wait for 5ms */
1750 usleep_range(5000, 5500);
1751
1752 /* 3.3V regulator output should be stable within 5 ms */
1753 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1754 if (!(ctrl & SDHCI_CTRL_VDD_180))
1755 return 0;
1756
Joe Perches66061102014-09-12 14:56:56 -07001757 pr_warn("%s: 3.3V regulator output did not became stable\n",
1758 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001759
1760 return -EAGAIN;
1761 case MMC_SIGNAL_VOLTAGE_180:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001762 if (!IS_ERR(mmc->supply.vqmmc)) {
1763 ret = regulator_set_voltage(mmc->supply.vqmmc,
Kevin Liu20b92a32012-12-17 19:29:26 +08001764 1700000, 1950000);
1765 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001766 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1767 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001768 return -EIO;
1769 }
1770 }
1771
1772 /*
1773 * Enable 1.8V Signal Enable in the Host Control2
1774 * register
1775 */
1776 ctrl |= SDHCI_CTRL_VDD_180;
1777 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1778
Vincent Yang9d967a62015-01-20 16:05:15 +08001779 /* Some controller need to do more when switching */
1780 if (host->ops->voltage_switch)
1781 host->ops->voltage_switch(host);
1782
Kevin Liu20b92a32012-12-17 19:29:26 +08001783 /* 1.8V regulator output should be stable within 5 ms */
1784 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1785 if (ctrl & SDHCI_CTRL_VDD_180)
1786 return 0;
1787
Joe Perches66061102014-09-12 14:56:56 -07001788 pr_warn("%s: 1.8V regulator output did not became stable\n",
1789 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001790
1791 return -EAGAIN;
1792 case MMC_SIGNAL_VOLTAGE_120:
Tim Kryger3a48edc2014-06-13 10:13:56 -07001793 if (!IS_ERR(mmc->supply.vqmmc)) {
1794 ret = regulator_set_voltage(mmc->supply.vqmmc, 1100000,
1795 1300000);
Kevin Liu20b92a32012-12-17 19:29:26 +08001796 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001797 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1798 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001799 return -EIO;
1800 }
1801 }
1802 return 0;
1803 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301804 /* No signal voltage switch required */
1805 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001806 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301807}
1808
Kevin Liu20b92a32012-12-17 19:29:26 +08001809static int sdhci_card_busy(struct mmc_host *mmc)
1810{
1811 struct sdhci_host *host = mmc_priv(mmc);
1812 u32 present_state;
1813
Kevin Liu20b92a32012-12-17 19:29:26 +08001814 /* Check whether DAT[3:0] is 0000 */
1815 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001816
1817 return !(present_state & SDHCI_DATA_LVL_MASK);
1818}
1819
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001820static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1821{
1822 struct sdhci_host *host = mmc_priv(mmc);
1823 unsigned long flags;
1824
1825 spin_lock_irqsave(&host->lock, flags);
1826 host->flags |= SDHCI_HS400_TUNING;
1827 spin_unlock_irqrestore(&host->lock, flags);
1828
1829 return 0;
1830}
1831
Girish K S069c9f12012-01-06 09:56:39 +05301832static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301833{
Russell King4b6f37d2014-04-25 12:59:36 +01001834 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301835 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301836 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301837 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001838 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001839 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001840 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301841
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001842 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301843
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001844 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1845 host->flags &= ~SDHCI_HS400_TUNING;
1846
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001847 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1848 tuning_count = host->tuning_count;
1849
Arindam Nathb513ea22011-05-05 12:19:04 +05301850 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001851 * The Host Controller needs tuning in case of SDR104 and DDR50
1852 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1853 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301854 * If the Host Controller supports the HS200 mode then the
1855 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301856 */
Russell King4b6f37d2014-04-25 12:59:36 +01001857 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001858 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001859 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001860 err = -EINVAL;
1861 goto out_unlock;
1862
Russell King4b6f37d2014-04-25 12:59:36 +01001863 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001864 /*
1865 * Periodic re-tuning for HS400 is not expected to be needed, so
1866 * disable it here.
1867 */
1868 if (hs400_tuning)
1869 tuning_count = 0;
1870 break;
1871
Russell King4b6f37d2014-04-25 12:59:36 +01001872 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001873 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001874 break;
Girish K S069c9f12012-01-06 09:56:39 +05301875
Russell King4b6f37d2014-04-25 12:59:36 +01001876 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03001877 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01001878 break;
1879 /* FALLTHROUGH */
1880
1881 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02001882 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05301883 }
1884
Dong Aisheng45251812013-09-13 19:11:30 +08001885 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001886 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08001887 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08001888 return err;
1889 }
1890
Russell King4b6f37d2014-04-25 12:59:36 +01001891 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1892 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08001893 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1894 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05301895 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1896
1897 /*
1898 * As per the Host Controller spec v3.00, tuning command
1899 * generates Buffer Read Ready interrupt, so enable that.
1900 *
1901 * Note: The spec clearly says that when tuning sequence
1902 * is being performed, the controller does not generate
1903 * interrupts other than Buffer Read Ready interrupt. But
1904 * to make sure we don't hit a controller bug, we _only_
1905 * enable Buffer Read Ready interrupt here.
1906 */
Russell Kingb537f942014-04-25 12:56:01 +01001907 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1908 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05301909
1910 /*
1911 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
1912 * of loops reaches 40 times or a timeout of 150ms occurs.
1913 */
Arindam Nathb513ea22011-05-05 12:19:04 +05301914 do {
1915 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001916 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05301917
Girish K S069c9f12012-01-06 09:56:39 +05301918 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05301919 cmd.arg = 0;
1920 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
1921 cmd.retries = 0;
1922 cmd.data = NULL;
1923 cmd.error = 0;
1924
Al Cooper7ce45e92014-05-09 11:34:07 -04001925 if (tuning_loop_counter-- == 0)
1926 break;
1927
Arindam Nathb513ea22011-05-05 12:19:04 +05301928 mrq.cmd = &cmd;
1929 host->mrq = &mrq;
1930
1931 /*
1932 * In response to CMD19, the card sends 64 bytes of tuning
1933 * block to the Host Controller. So we set the block size
1934 * to 64 here.
1935 */
Girish K S069c9f12012-01-06 09:56:39 +05301936 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
1937 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
1938 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
1939 SDHCI_BLOCK_SIZE);
1940 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
1941 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1942 SDHCI_BLOCK_SIZE);
1943 } else {
1944 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
1945 SDHCI_BLOCK_SIZE);
1946 }
Arindam Nathb513ea22011-05-05 12:19:04 +05301947
1948 /*
1949 * The tuning block is sent by the card to the host controller.
1950 * So we set the TRNS_READ bit in the Transfer Mode register.
1951 * This also takes care of setting DMA Enable and Multi Block
1952 * Select in the same register to 0.
1953 */
1954 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
1955
1956 sdhci_send_command(host, &cmd);
1957
1958 host->cmd = NULL;
1959 host->mrq = NULL;
1960
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001961 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301962 /* Wait for Buffer Read Ready interrupt */
1963 wait_event_interruptible_timeout(host->buf_ready_int,
1964 (host->tuning_done == 1),
1965 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001966 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301967
1968 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001969 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 +05301970 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1971 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1972 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
1973 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1974
1975 err = -EIO;
1976 goto out;
1977 }
1978
1979 host->tuning_done = 0;
1980
1981 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07001982
1983 /* eMMC spec does not require a delay between tuning cycles */
1984 if (opcode == MMC_SEND_TUNING_BLOCK)
1985 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05301986 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
1987
1988 /*
1989 * The Host Driver has exhausted the maximum number of loops allowed,
1990 * so use fixed sampling frequency.
1991 */
Al Cooper7ce45e92014-05-09 11:34:07 -04001992 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05301993 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
1994 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04001995 }
1996 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001997 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08001998 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05301999 }
2000
2001out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002002 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002003 /*
2004 * In case tuning fails, host controllers which support
2005 * re-tuning can try tuning again at a later time, when the
2006 * re-tuning timer expires. So for these controllers, we
2007 * return 0. Since there might be other controllers who do not
2008 * have this capability, we return error for them.
2009 */
2010 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302011 }
2012
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002013 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302014
Russell Kingb537f942014-04-25 12:56:01 +01002015 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2016 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002017out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002018 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302019 return err;
2020}
2021
Adrian Huntercb849642015-02-06 14:12:59 +02002022static int sdhci_select_drive_strength(struct mmc_card *card,
2023 unsigned int max_dtr, int host_drv,
2024 int card_drv, int *drv_type)
2025{
2026 struct sdhci_host *host = mmc_priv(card->host);
2027
2028 if (!host->ops->select_drive_strength)
2029 return 0;
2030
2031 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2032 card_drv, drv_type);
2033}
Kevin Liu52983382013-01-31 11:31:37 +08002034
2035static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302036{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302037 /* Host Controller v3.00 defines preset value registers */
2038 if (host->version < SDHCI_SPEC_300)
2039 return;
2040
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302041 /*
2042 * We only enable or disable Preset Value if they are not already
2043 * enabled or disabled respectively. Otherwise, we bail out.
2044 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002045 if (host->preset_enabled != enable) {
2046 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2047
2048 if (enable)
2049 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2050 else
2051 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2052
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302053 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002054
2055 if (enable)
2056 host->flags |= SDHCI_PV_ENABLED;
2057 else
2058 host->flags &= ~SDHCI_PV_ENABLED;
2059
2060 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302061 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002062}
2063
Haibo Chen348487c2014-12-09 17:04:05 +08002064static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2065 int err)
2066{
2067 struct sdhci_host *host = mmc_priv(mmc);
2068 struct mmc_data *data = mrq->data;
2069
Russell Kingf48f0392016-01-26 13:40:32 +00002070 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002071 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2072 data->flags & MMC_DATA_WRITE ?
2073 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2074
2075 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002076}
2077
Haibo Chen348487c2014-12-09 17:04:05 +08002078static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq,
2079 bool is_first_req)
2080{
2081 struct sdhci_host *host = mmc_priv(mmc);
2082
Haibo Chend31911b2015-08-25 10:02:11 +08002083 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002084
2085 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002086 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002087}
2088
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002089static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002090{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002091 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002092 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002093 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002094
Christian Daudt722e1282013-06-20 14:26:36 -07002095 /* First check if client has provided their own card event */
2096 if (host->ops->card_event)
2097 host->ops->card_event(host);
2098
Dong Aishengded97e02016-04-16 01:29:25 +08002099 present = sdhci_get_cd(host->mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002100
Pierre Ossmand129bce2006-03-24 03:18:17 -08002101 spin_lock_irqsave(&host->lock, flags);
2102
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002103 /* Check host->mrq first in case we are runtime suspended */
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002104 if (host->mrq && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302105 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002106 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302107 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002108 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002109
Russell King03231f92014-04-25 12:57:12 +01002110 sdhci_do_reset(host, SDHCI_RESET_CMD);
2111 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002112
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002113 host->mrq->cmd->error = -ENOMEDIUM;
2114 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002115 }
2116
2117 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002118}
2119
2120static const struct mmc_host_ops sdhci_ops = {
2121 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002122 .post_req = sdhci_post_req,
2123 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002124 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002125 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002126 .get_ro = sdhci_get_ro,
2127 .hw_reset = sdhci_hw_reset,
2128 .enable_sdio_irq = sdhci_enable_sdio_irq,
2129 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002130 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002131 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002132 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002133 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002134 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002135};
2136
2137/*****************************************************************************\
2138 * *
2139 * Tasklets *
2140 * *
2141\*****************************************************************************/
2142
Pierre Ossmand129bce2006-03-24 03:18:17 -08002143static void sdhci_tasklet_finish(unsigned long param)
2144{
2145 struct sdhci_host *host;
2146 unsigned long flags;
2147 struct mmc_request *mrq;
2148
2149 host = (struct sdhci_host*)param;
2150
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002151 spin_lock_irqsave(&host->lock, flags);
2152
Chris Ball0c9c99a2011-04-27 17:35:31 -04002153 /*
2154 * If this tasklet gets rescheduled while running, it will
2155 * be run again afterwards but without any active request.
2156 */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002157 if (!host->mrq) {
2158 spin_unlock_irqrestore(&host->lock, flags);
Chris Ball0c9c99a2011-04-27 17:35:31 -04002159 return;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002160 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002161
2162 del_timer(&host->timer);
2163
2164 mrq = host->mrq;
2165
Pierre Ossmand129bce2006-03-24 03:18:17 -08002166 /*
Russell King054cedf2016-01-26 13:40:42 +00002167 * Always unmap the data buffers if they were mapped by
2168 * sdhci_prepare_data() whenever we finish with a request.
2169 * This avoids leaking DMA mappings on error.
2170 */
2171 if (host->flags & SDHCI_REQ_USE_DMA) {
2172 struct mmc_data *data = mrq->data;
2173
2174 if (data && data->host_cookie == COOKIE_MAPPED) {
2175 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2176 (data->flags & MMC_DATA_READ) ?
2177 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2178 data->host_cookie = COOKIE_UNMAPPED;
2179 }
2180 }
2181
2182 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002183 * The controller needs a reset of internal state machines
2184 * upon error conditions.
2185 */
Pierre Ossman1e728592008-04-16 19:13:13 +02002186 if (!(host->flags & SDHCI_DEVICE_DEAD) &&
Ben Dooksb7b4d342011-04-27 14:24:19 +01002187 ((mrq->cmd && mrq->cmd->error) ||
Andrew Gabbasovfce9d332014-10-01 07:14:08 -05002188 (mrq->sbc && mrq->sbc->error) ||
2189 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
2190 (mrq->data->stop && mrq->data->stop->error))) ||
2191 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST))) {
Pierre Ossman645289d2006-06-30 02:22:33 -07002192
2193 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002194 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002195 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002196 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002197
2198 /* Spec says we should do both at the same time, but Ricoh
2199 controllers do not like that. */
Russell King03231f92014-04-25 12:57:12 +01002200 sdhci_do_reset(host, SDHCI_RESET_CMD);
2201 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002202 }
2203
2204 host->mrq = NULL;
2205 host->cmd = NULL;
2206 host->data = NULL;
2207
Adrian Hunter061d17a2016-04-12 14:25:09 +03002208 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002209
Pierre Ossman5f25a662006-10-04 02:15:39 -07002210 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002211 spin_unlock_irqrestore(&host->lock, flags);
2212
2213 mmc_request_done(host->mmc, mrq);
2214}
2215
2216static void sdhci_timeout_timer(unsigned long data)
2217{
2218 struct sdhci_host *host;
2219 unsigned long flags;
2220
2221 host = (struct sdhci_host*)data;
2222
2223 spin_lock_irqsave(&host->lock, flags);
2224
2225 if (host->mrq) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002226 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2227 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002228 sdhci_dumpregs(host);
2229
2230 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002231 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232 sdhci_finish_data(host);
2233 } else {
2234 if (host->cmd)
Pierre Ossman17b04292007-07-22 22:18:46 +02002235 host->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002236 else
Pierre Ossman17b04292007-07-22 22:18:46 +02002237 host->mrq->cmd->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002238
2239 tasklet_schedule(&host->finish_tasklet);
2240 }
2241 }
2242
Pierre Ossman5f25a662006-10-04 02:15:39 -07002243 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002244 spin_unlock_irqrestore(&host->lock, flags);
2245}
2246
2247/*****************************************************************************\
2248 * *
2249 * Interrupt handling *
2250 * *
2251\*****************************************************************************/
2252
Adrian Hunter61541392014-09-24 10:27:27 +03002253static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask, u32 *mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002254{
2255 BUG_ON(intmask == 0);
2256
2257 if (!host->cmd) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002258 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2259 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 sdhci_dumpregs(host);
2261 return;
2262 }
2263
Russell Kingec014cb2016-01-26 13:39:39 +00002264 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2265 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2266 if (intmask & SDHCI_INT_TIMEOUT)
2267 host->cmd->error = -ETIMEDOUT;
2268 else
2269 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002270
Russell King71fcbda2016-01-26 13:39:45 +00002271 /*
2272 * If this command initiates a data phase and a response
2273 * CRC error is signalled, the card can start transferring
2274 * data - the card may have received the command without
2275 * error. We must not terminate the mmc_request early.
2276 *
2277 * If the card did not receive the command or returned an
2278 * error which prevented it sending data, the data phase
2279 * will time out.
2280 */
2281 if (host->cmd->data &&
2282 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2283 SDHCI_INT_CRC) {
2284 host->cmd = NULL;
2285 return;
2286 }
2287
Pierre Ossmand129bce2006-03-24 03:18:17 -08002288 tasklet_schedule(&host->finish_tasklet);
Pierre Ossmane8095172008-07-25 01:09:08 +02002289 return;
2290 }
2291
2292 /*
2293 * The host can send and interrupt when the busy state has
2294 * ended, allowing us to wait without wasting CPU cycles.
2295 * Unfortunately this is overloaded on the "data complete"
2296 * interrupt, so we need to take some care when handling
2297 * it.
2298 *
2299 * Note: The 1.0 specification is a bit ambiguous about this
2300 * feature so there might be some problems with older
2301 * controllers.
2302 */
2303 if (host->cmd->flags & MMC_RSP_BUSY) {
2304 if (host->cmd->data)
Marek Vasut2e4456f2015-11-18 10:47:02 +01002305 DBG("Cannot wait for busy signal when also doing a data transfer");
Chanho Mine99783a2014-08-30 12:40:40 +09002306 else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ)
2307 && !host->busy_handle) {
2308 /* Mark that command complete before busy is ended */
2309 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002310 return;
Chanho Mine99783a2014-08-30 12:40:40 +09002311 }
Ben Dooksf9454052009-02-20 20:33:08 +03002312
2313 /* The controller does not support the end-of-busy IRQ,
2314 * fall through and take the SDHCI_INT_RESPONSE */
Adrian Hunter61541392014-09-24 10:27:27 +03002315 } else if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
2316 host->cmd->opcode == MMC_STOP_TRANSMISSION && !host->data) {
2317 *mask &= ~SDHCI_INT_DATA_END;
Pierre Ossmane8095172008-07-25 01:09:08 +02002318 }
2319
2320 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002321 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002322}
2323
George G. Davis0957c332010-02-18 12:32:12 -05002324#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002325static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002326{
2327 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002328 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002329
2330 sdhci_dumpregs(host);
2331
2332 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002333 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002334
Adrian Huntere57a5f62014-11-04 12:42:46 +02002335 if (host->flags & SDHCI_USE_64_BIT_DMA)
2336 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2337 name, desc, le32_to_cpu(dma_desc->addr_hi),
2338 le32_to_cpu(dma_desc->addr_lo),
2339 le16_to_cpu(dma_desc->len),
2340 le16_to_cpu(dma_desc->cmd));
2341 else
2342 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2343 name, desc, le32_to_cpu(dma_desc->addr_lo),
2344 le16_to_cpu(dma_desc->len),
2345 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002346
Adrian Hunter76fe3792014-11-04 12:42:42 +02002347 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002348
Adrian Hunter05452302014-11-04 12:42:45 +02002349 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002350 break;
2351 }
2352}
2353#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002354static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002355#endif
2356
Pierre Ossmand129bce2006-03-24 03:18:17 -08002357static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2358{
Girish K S069c9f12012-01-06 09:56:39 +05302359 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002360 BUG_ON(intmask == 0);
2361
Arindam Nathb513ea22011-05-05 12:19:04 +05302362 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2363 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302364 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2365 if (command == MMC_SEND_TUNING_BLOCK ||
2366 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302367 host->tuning_done = 1;
2368 wake_up(&host->buf_ready_int);
2369 return;
2370 }
2371 }
2372
Pierre Ossmand129bce2006-03-24 03:18:17 -08002373 if (!host->data) {
2374 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002375 * The "data complete" interrupt is also used to
2376 * indicate that a busy state has ended. See comment
2377 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002378 */
Pierre Ossmane8095172008-07-25 01:09:08 +02002379 if (host->cmd && (host->cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002380 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
2381 host->cmd->error = -ETIMEDOUT;
2382 tasklet_schedule(&host->finish_tasklet);
2383 return;
2384 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002385 if (intmask & SDHCI_INT_DATA_END) {
Chanho Mine99783a2014-08-30 12:40:40 +09002386 /*
2387 * Some cards handle busy-end interrupt
2388 * before the command completed, so make
2389 * sure we do things in the proper order.
2390 */
2391 if (host->busy_handle)
2392 sdhci_finish_command(host);
2393 else
2394 host->busy_handle = 1;
Pierre Ossmane8095172008-07-25 01:09:08 +02002395 return;
2396 }
2397 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002398
Marek Vasut2e4456f2015-11-18 10:47:02 +01002399 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2400 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002401 sdhci_dumpregs(host);
2402
2403 return;
2404 }
2405
2406 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002407 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002408 else if (intmask & SDHCI_INT_DATA_END_BIT)
2409 host->data->error = -EILSEQ;
2410 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2411 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2412 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002413 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002414 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302415 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002416 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002417 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002418 if (host->ops->adma_workaround)
2419 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002420 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002421
Pierre Ossman17b04292007-07-22 22:18:46 +02002422 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002423 sdhci_finish_data(host);
2424 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002425 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426 sdhci_transfer_pio(host);
2427
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002428 /*
2429 * We currently don't do anything fancy with DMA
2430 * boundaries, but as we can't disable the feature
2431 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002432 *
2433 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2434 * should return a valid address to continue from, but as
2435 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002436 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002437 if (intmask & SDHCI_INT_DMA_END) {
2438 u32 dmastart, dmanow;
2439 dmastart = sg_dma_address(host->data->sg);
2440 dmanow = dmastart + host->data->bytes_xfered;
2441 /*
2442 * Force update to the next DMA block boundary.
2443 */
2444 dmanow = (dmanow &
2445 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2446 SDHCI_DEFAULT_BOUNDARY_SIZE;
2447 host->data->bytes_xfered = dmanow - dmastart;
2448 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2449 " next 0x%08x\n",
2450 mmc_hostname(host->mmc), dmastart,
2451 host->data->bytes_xfered, dmanow);
2452 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2453 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002454
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002455 if (intmask & SDHCI_INT_DATA_END) {
2456 if (host->cmd) {
2457 /*
2458 * Data managed to finish before the
2459 * command completed. Make sure we do
2460 * things in the proper order.
2461 */
2462 host->data_early = 1;
2463 } else {
2464 sdhci_finish_data(host);
2465 }
2466 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002467 }
2468}
2469
David Howells7d12e782006-10-05 14:55:46 +01002470static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002471{
Russell King781e9892014-04-25 12:55:46 +01002472 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002473 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002474 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002475 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002476
2477 spin_lock(&host->lock);
2478
Russell Kingbe138552014-04-25 12:55:56 +01002479 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002480 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002481 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002482 }
2483
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002484 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002485 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002486 result = IRQ_NONE;
2487 goto out;
2488 }
2489
Russell King41005002014-04-25 12:55:36 +01002490 do {
2491 /* Clear selected interrupts. */
2492 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2493 SDHCI_INT_BUS_POWER);
2494 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002495
Russell King41005002014-04-25 12:55:36 +01002496 DBG("*** %s got interrupt: 0x%08x\n",
2497 mmc_hostname(host->mmc), intmask);
2498
2499 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2500 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2501 SDHCI_CARD_PRESENT;
2502
2503 /*
2504 * There is a observation on i.mx esdhc. INSERT
2505 * bit will be immediately set again when it gets
2506 * cleared, if a card is inserted. We have to mask
2507 * the irq to prevent interrupt storm which will
2508 * freeze the system. And the REMOVE gets the
2509 * same situation.
2510 *
2511 * More testing are needed here to ensure it works
2512 * for other platforms though.
2513 */
Russell Kingb537f942014-04-25 12:56:01 +01002514 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2515 SDHCI_INT_CARD_REMOVE);
2516 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2517 SDHCI_INT_CARD_INSERT;
2518 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2519 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002520
2521 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2522 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002523
2524 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2525 SDHCI_INT_CARD_REMOVE);
2526 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002527 }
2528
2529 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunter61541392014-09-24 10:27:27 +03002530 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK,
2531 &intmask);
Russell King41005002014-04-25 12:55:36 +01002532
2533 if (intmask & SDHCI_INT_DATA_MASK)
2534 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2535
2536 if (intmask & SDHCI_INT_BUS_POWER)
2537 pr_err("%s: Card is consuming too much power!\n",
2538 mmc_hostname(host->mmc));
2539
Russell King781e9892014-04-25 12:55:46 +01002540 if (intmask & SDHCI_INT_CARD_INT) {
2541 sdhci_enable_sdio_irq_nolock(host, false);
2542 host->thread_isr |= SDHCI_INT_CARD_INT;
2543 result = IRQ_WAKE_THREAD;
2544 }
Russell King41005002014-04-25 12:55:36 +01002545
2546 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2547 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2548 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
2549 SDHCI_INT_CARD_INT);
2550
2551 if (intmask) {
2552 unexpected |= intmask;
2553 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2554 }
2555
Russell King781e9892014-04-25 12:55:46 +01002556 if (result == IRQ_NONE)
2557 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002558
2559 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002560 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561out:
2562 spin_unlock(&host->lock);
2563
Alexander Stein6379b232012-03-14 09:52:10 +01002564 if (unexpected) {
2565 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2566 mmc_hostname(host->mmc), unexpected);
2567 sdhci_dumpregs(host);
2568 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002569
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570 return result;
2571}
2572
Russell King781e9892014-04-25 12:55:46 +01002573static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2574{
2575 struct sdhci_host *host = dev_id;
2576 unsigned long flags;
2577 u32 isr;
2578
2579 spin_lock_irqsave(&host->lock, flags);
2580 isr = host->thread_isr;
2581 host->thread_isr = 0;
2582 spin_unlock_irqrestore(&host->lock, flags);
2583
Russell King3560db82014-04-25 12:55:51 +01002584 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2585 sdhci_card_event(host->mmc);
2586 mmc_detect_change(host->mmc, msecs_to_jiffies(200));
2587 }
2588
Russell King781e9892014-04-25 12:55:46 +01002589 if (isr & SDHCI_INT_CARD_INT) {
2590 sdio_run_irqs(host->mmc);
2591
2592 spin_lock_irqsave(&host->lock, flags);
2593 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2594 sdhci_enable_sdio_irq_nolock(host, true);
2595 spin_unlock_irqrestore(&host->lock, flags);
2596 }
2597
2598 return isr ? IRQ_HANDLED : IRQ_NONE;
2599}
2600
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601/*****************************************************************************\
2602 * *
2603 * Suspend/resume *
2604 * *
2605\*****************************************************************************/
2606
2607#ifdef CONFIG_PM
Kevin Liuad080d72013-01-05 17:21:33 +08002608void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2609{
2610 u8 val;
2611 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2612 | SDHCI_WAKE_ON_INT;
2613
2614 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2615 val |= mask ;
2616 /* Avoid fake wake up */
2617 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
2618 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
2619 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2620}
2621EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2622
Fabio Estevam0b10f472014-08-30 14:53:13 -03002623static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002624{
2625 u8 val;
2626 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2627 | SDHCI_WAKE_ON_INT;
2628
2629 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2630 val &= ~mask;
2631 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2632}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002633
Manuel Lauss29495aa2011-11-03 11:09:45 +01002634int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002635{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002636 sdhci_disable_card_detection(host);
2637
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002638 mmc_retune_timer_stop(host->mmc);
2639 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302640
Kevin Liuad080d72013-01-05 17:21:33 +08002641 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002642 host->ier = 0;
2643 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2644 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002645 free_irq(host->irq, host);
2646 } else {
2647 sdhci_enable_irq_wakeups(host);
2648 enable_irq_wake(host->irq);
2649 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002650 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002651}
2652
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002653EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002655int sdhci_resume_host(struct sdhci_host *host)
2656{
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002657 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002658
Richard Röjforsa13abc72009-09-22 16:45:30 -07002659 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002660 if (host->ops->enable_dma)
2661 host->ops->enable_dma(host);
2662 }
2663
Adrian Hunter6308d292012-02-07 14:48:54 +02002664 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2665 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2666 /* Card keeps power but host controller does not */
2667 sdhci_init(host, 0);
2668 host->pwr = 0;
2669 host->clock = 0;
Dong Aishengded97e02016-04-16 01:29:25 +08002670 sdhci_set_ios(host->mmc, &host->mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002671 } else {
2672 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2673 mmiowb();
2674 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002675
Haibo Chen14a7b41642015-09-15 18:32:58 +08002676 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2677 ret = request_threaded_irq(host->irq, sdhci_irq,
2678 sdhci_thread_irq, IRQF_SHARED,
2679 mmc_hostname(host->mmc), host);
2680 if (ret)
2681 return ret;
2682 } else {
2683 sdhci_disable_irq_wakeups(host);
2684 disable_irq_wake(host->irq);
2685 }
2686
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002687 sdhci_enable_card_detection(host);
2688
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002689 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002690}
2691
2692EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002693
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002694int sdhci_runtime_suspend_host(struct sdhci_host *host)
2695{
2696 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002697
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002698 mmc_retune_timer_stop(host->mmc);
2699 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002700
2701 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002702 host->ier &= SDHCI_INT_CARD_INT;
2703 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2704 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002705 spin_unlock_irqrestore(&host->lock, flags);
2706
Russell King781e9892014-04-25 12:55:46 +01002707 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002708
2709 spin_lock_irqsave(&host->lock, flags);
2710 host->runtime_suspended = true;
2711 spin_unlock_irqrestore(&host->lock, flags);
2712
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002713 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002714}
2715EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2716
2717int sdhci_runtime_resume_host(struct sdhci_host *host)
2718{
2719 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002720 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002721
2722 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2723 if (host->ops->enable_dma)
2724 host->ops->enable_dma(host);
2725 }
2726
2727 sdhci_init(host, 0);
2728
2729 /* Force clock and power re-program */
2730 host->pwr = 0;
2731 host->clock = 0;
Dong Aishengded97e02016-04-16 01:29:25 +08002732 sdhci_start_signal_voltage_switch(host->mmc, &host->mmc->ios);
2733 sdhci_set_ios(host->mmc, &host->mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002734
Kevin Liu52983382013-01-31 11:31:37 +08002735 if ((host_flags & SDHCI_PV_ENABLED) &&
2736 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2737 spin_lock_irqsave(&host->lock, flags);
2738 sdhci_enable_preset_value(host, true);
2739 spin_unlock_irqrestore(&host->lock, flags);
2740 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002741
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002742 spin_lock_irqsave(&host->lock, flags);
2743
2744 host->runtime_suspended = false;
2745
2746 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002747 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002748 sdhci_enable_sdio_irq_nolock(host, true);
2749
2750 /* Enable Card Detection */
2751 sdhci_enable_card_detection(host);
2752
2753 spin_unlock_irqrestore(&host->lock, flags);
2754
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002755 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002756}
2757EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2758
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002759#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002760
Pierre Ossmand129bce2006-03-24 03:18:17 -08002761/*****************************************************************************\
2762 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002763 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002764 * *
2765\*****************************************************************************/
2766
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002767struct sdhci_host *sdhci_alloc_host(struct device *dev,
2768 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002769{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002770 struct mmc_host *mmc;
2771 struct sdhci_host *host;
2772
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002773 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002774
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002775 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002776 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002777 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002778
2779 host = mmc_priv(mmc);
2780 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002781 host->mmc_host_ops = sdhci_ops;
2782 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002784 return host;
2785}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002786
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002787EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002788
Alexandre Courbot7b913692016-03-07 11:07:55 +09002789static int sdhci_set_dma_mask(struct sdhci_host *host)
2790{
2791 struct mmc_host *mmc = host->mmc;
2792 struct device *dev = mmc_dev(mmc);
2793 int ret = -EINVAL;
2794
2795 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2796 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2797
2798 /* Try 64-bit mask if hardware is capable of it */
2799 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2800 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2801 if (ret) {
2802 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2803 mmc_hostname(mmc));
2804 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2805 }
2806 }
2807
2808 /* 32-bit mask as default & fallback */
2809 if (ret) {
2810 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
2811 if (ret)
2812 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
2813 mmc_hostname(mmc));
2814 }
2815
2816 return ret;
2817}
2818
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002819int sdhci_add_host(struct sdhci_host *host)
2820{
2821 struct mmc_host *mmc;
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002822 u32 caps[2] = {0, 0};
Arindam Nathf2119df2011-05-05 12:18:57 +05302823 u32 max_current_caps;
2824 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002825 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08002826 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002827 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002829 WARN_ON(host == NULL);
2830 if (host == NULL)
2831 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002832
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002833 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002834
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002835 if (debug_quirks)
2836 host->quirks = debug_quirks;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002837 if (debug_quirks2)
2838 host->quirks2 = debug_quirks2;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002839
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03002840 override_timeout_clk = host->timeout_clk;
2841
Russell King03231f92014-04-25 12:57:12 +01002842 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand96649e2006-06-30 02:22:30 -07002843
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002844 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
Pierre Ossman2134a922008-06-28 18:28:51 +02002845 host->version = (host->version & SDHCI_SPEC_VER_MASK)
2846 >> SDHCI_SPEC_VER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08002847 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002848 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
2849 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07002850 }
2851
Arindam Nathf2119df2011-05-05 12:18:57 +05302852 caps[0] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ? host->caps :
Maxim Levitskyccc92c22010-08-10 18:01:42 -07002853 sdhci_readl(host, SDHCI_CAPABILITIES);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854
Philip Rakitybd6a8c32012-06-27 21:49:27 -07002855 if (host->version >= SDHCI_SPEC_300)
2856 caps[1] = (host->quirks & SDHCI_QUIRK_MISSING_CAPS) ?
2857 host->caps1 :
2858 sdhci_readl(host, SDHCI_CAPABILITIES_1);
Arindam Nathf2119df2011-05-05 12:18:57 +05302859
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002860 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07002861 host->flags |= SDHCI_USE_SDMA;
Arindam Nathf2119df2011-05-05 12:18:57 +05302862 else if (!(caps[0] & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002863 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07002864 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07002865 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002866
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002867 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07002868 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01002869 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07002870 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02002871 }
2872
Arindam Nathf2119df2011-05-05 12:18:57 +05302873 if ((host->version >= SDHCI_SPEC_200) &&
2874 (caps[0] & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07002875 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02002876
2877 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
2878 (host->flags & SDHCI_USE_ADMA)) {
2879 DBG("Disabling ADMA as it is marked broken\n");
2880 host->flags &= ~SDHCI_USE_ADMA;
2881 }
2882
Adrian Huntere57a5f62014-11-04 12:42:46 +02002883 /*
2884 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
2885 * and *must* do 64-bit DMA. A driver has the opportunity to change
2886 * that during the first call to ->enable_dma(). Similarly
2887 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
2888 * implement.
2889 */
Al Cooper5eaa7472016-02-10 15:25:39 -05002890 if (caps[0] & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02002891 host->flags |= SDHCI_USE_64_BIT_DMA;
2892
Richard Röjforsa13abc72009-09-22 16:45:30 -07002893 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09002894 ret = sdhci_set_dma_mask(host);
2895
2896 if (!ret && host->ops->enable_dma)
2897 ret = host->ops->enable_dma(host);
2898
2899 if (ret) {
2900 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
2901 mmc_hostname(mmc));
2902 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
2903
2904 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002905 }
2906 }
2907
Adrian Huntere57a5f62014-11-04 12:42:46 +02002908 /* SDMA does not support 64-bit DMA */
2909 if (host->flags & SDHCI_USE_64_BIT_DMA)
2910 host->flags &= ~SDHCI_USE_SDMA;
2911
Pierre Ossman2134a922008-06-28 18:28:51 +02002912 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00002913 dma_addr_t dma;
2914 void *buf;
2915
Pierre Ossman2134a922008-06-28 18:28:51 +02002916 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02002917 * The DMA descriptor table size is calculated as the maximum
2918 * number of segments times 2, to allow for an alignment
2919 * descriptor for each segment, plus 1 for a nop end descriptor,
2920 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02002921 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02002922 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2923 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2924 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002925 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002926 } else {
2927 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
2928 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002929 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02002930 }
Russell Kinge66e61c2016-01-26 13:39:55 +00002931
Adrian Hunter04a5ae62015-11-26 14:00:49 +02002932 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00002933 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
2934 host->adma_table_sz, &dma, GFP_KERNEL);
2935 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07002936 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02002937 mmc_hostname(mmc));
2938 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002939 } else if ((dma + host->align_buffer_sz) &
2940 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07002941 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
2942 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01002943 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00002944 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
2945 host->adma_table_sz, buf, dma);
2946 } else {
2947 host->align_buffer = buf;
2948 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00002949
Russell Kinge66e61c2016-01-26 13:39:55 +00002950 host->adma_table = buf + host->align_buffer_sz;
2951 host->adma_addr = dma + host->align_buffer_sz;
2952 }
Pierre Ossman2134a922008-06-28 18:28:51 +02002953 }
2954
Pierre Ossman76591502008-07-21 00:32:11 +02002955 /*
2956 * If we use DMA, then it's up to the caller to set the DMA
2957 * mask, but PIO does not need the hw shim so we set a new
2958 * mask here in that case.
2959 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07002960 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02002961 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07002962 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02002963 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002965 if (host->version >= SDHCI_SPEC_300)
Arindam Nathf2119df2011-05-05 12:18:57 +05302966 host->max_clk = (caps[0] & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002967 >> SDHCI_CLOCK_BASE_SHIFT;
2968 else
Arindam Nathf2119df2011-05-05 12:18:57 +05302969 host->max_clk = (caps[0] & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04002970 >> SDHCI_CLOCK_BASE_SHIFT;
2971
Pierre Ossmand129bce2006-03-24 03:18:17 -08002972 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07002973 if (host->max_clk == 0 || host->quirks &
2974 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03002975 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002976 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
2977 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03002978 ret = -ENODEV;
2979 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03002980 }
2981 host->max_clk = host->ops->get_max_clock(host);
2982 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002983
2984 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05302985 * In case of Host Controller v3.00, find out whether clock
2986 * multiplier is supported.
2987 */
2988 host->clk_mul = (caps[1] & SDHCI_CLOCK_MUL_MASK) >>
2989 SDHCI_CLOCK_MUL_SHIFT;
2990
2991 /*
2992 * In case the value in Clock Multiplier is 0, then programmable
2993 * clock mode is not supported, otherwise the actual clock
2994 * multiplier is one more than the value of Clock Multiplier
2995 * in the Capabilities Register.
2996 */
2997 if (host->clk_mul)
2998 host->clk_mul += 1;
2999
3000 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003001 * Set host parameters.
3002 */
Dong Aisheng59241752015-07-22 20:53:07 +08003003 max_clk = host->max_clk;
3004
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003005 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003006 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303007 else if (host->version >= SDHCI_SPEC_300) {
3008 if (host->clk_mul) {
3009 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003010 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303011 } else
3012 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3013 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003014 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003015
Adrian Hunterd310ae42016-04-12 14:25:07 +03003016 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003017 mmc->f_max = max_clk;
3018
Aisheng Dong28aab052014-08-27 15:26:31 +08003019 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
3020 host->timeout_clk = (caps[0] & SDHCI_TIMEOUT_CLK_MASK) >>
3021 SDHCI_TIMEOUT_CLK_SHIFT;
3022 if (host->timeout_clk == 0) {
3023 if (host->ops->get_timeout_clock) {
3024 host->timeout_clk =
3025 host->ops->get_timeout_clock(host);
3026 } else {
3027 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3028 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003029 ret = -ENODEV;
3030 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003031 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003032 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003033
Aisheng Dong28aab052014-08-27 15:26:31 +08003034 if (caps[0] & SDHCI_TIMEOUT_CLK_UNIT)
3035 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003036
Adrian Hunter99513622016-03-07 13:33:55 +02003037 if (override_timeout_clk)
3038 host->timeout_clk = override_timeout_clk;
3039
Aisheng Dong28aab052014-08-27 15:26:31 +08003040 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003041 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003042 mmc->max_busy_timeout /= host->timeout_clk;
3043 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003044
Andrei Warkentine89d4562011-05-23 15:06:37 -05003045 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003046 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003047
3048 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3049 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003050
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003051 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003052 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003053 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003054 !(host->flags & SDHCI_USE_SDMA)) &&
3055 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003056 host->flags |= SDHCI_AUTO_CMD23;
3057 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3058 } else {
3059 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3060 }
3061
Philip Rakity15ec4462010-11-19 16:48:39 -05003062 /*
3063 * A controller may support 8-bit width, but the board itself
3064 * might not have the pins brought out. Boards that support
3065 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3066 * their platform code before calling sdhci_add_host(), and we
3067 * won't assume 8-bit width for hosts without that CAP.
3068 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003069 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003070 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003071
Jerry Huang63ef5d82012-10-25 13:47:19 +08003072 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3073 mmc->caps &= ~MMC_CAP_CMD23;
3074
Arindam Nathf2119df2011-05-05 12:18:57 +05303075 if (caps[0] & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003076 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003077
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003078 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Ivan T. Ivanovc31d22e2015-07-06 15:16:20 +03003079 !(mmc->caps & MMC_CAP_NONREMOVABLE) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003080 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003081 mmc->caps |= MMC_CAP_NEEDS_POLL;
3082
Tim Kryger3a48edc2014-06-13 10:13:56 -07003083 /* If there are external regulators, get them */
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003084 ret = mmc_regulator_get_supply(mmc);
3085 if (ret == -EPROBE_DEFER)
3086 goto undma;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003087
Philip Rakity6231f3d2012-07-23 15:56:23 -07003088 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003089 if (!IS_ERR(mmc->supply.vqmmc)) {
3090 ret = regulator_enable(mmc->supply.vqmmc);
3091 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3092 1950000))
Kevin Liu8363c372012-11-17 17:55:51 -05003093 caps[1] &= ~(SDHCI_SUPPORT_SDR104 |
3094 SDHCI_SUPPORT_SDR50 |
3095 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003096 if (ret) {
3097 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3098 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003099 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003100 }
Kevin Liu8363c372012-11-17 17:55:51 -05003101 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003102
Daniel Drake6a661802012-11-25 13:01:19 -05003103 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V)
3104 caps[1] &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3105 SDHCI_SUPPORT_DDR50);
3106
Al Cooper4188bba2012-03-16 15:54:17 -04003107 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
3108 if (caps[1] & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3109 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303110 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3111
3112 /* SDR104 supports also implies SDR50 support */
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003113 if (caps[1] & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303114 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003115 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3116 * field can be promoted to support HS200.
3117 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003118 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003119 mmc->caps2 |= MMC_CAP2_HS200;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003120 } else if (caps[1] & SDHCI_SUPPORT_SDR50)
Arindam Nathf2119df2011-05-05 12:18:57 +05303121 mmc->caps |= MMC_CAP_UHS_SDR50;
3122
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003123 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
3124 (caps[1] & SDHCI_SUPPORT_HS400))
3125 mmc->caps2 |= MMC_CAP2_HS400;
3126
Adrian Hunter549c0b12014-11-06 15:19:05 +02003127 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3128 (IS_ERR(mmc->supply.vqmmc) ||
3129 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3130 1300000)))
3131 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3132
Micky Ching9107ebb2014-02-21 18:40:35 +08003133 if ((caps[1] & SDHCI_SUPPORT_DDR50) &&
3134 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303135 mmc->caps |= MMC_CAP_UHS_DDR50;
3136
Girish K S069c9f12012-01-06 09:56:39 +05303137 /* Does the host need tuning for SDR50? */
Arindam Nathb513ea22011-05-05 12:19:04 +05303138 if (caps[1] & SDHCI_USE_SDR50_TUNING)
3139 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3140
Arindam Nathd6d50a12011-05-05 12:18:59 +05303141 /* Driver Type(s) (A, C, D) supported by the host */
3142 if (caps[1] & SDHCI_DRIVER_TYPE_A)
3143 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
3144 if (caps[1] & SDHCI_DRIVER_TYPE_C)
3145 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
3146 if (caps[1] & SDHCI_DRIVER_TYPE_D)
3147 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3148
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303149 /* Initial value for re-tuning timer count */
3150 host->tuning_count = (caps[1] & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3151 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
3152
3153 /*
3154 * In case Re-tuning Timer is not disabled, the actual value of
3155 * re-tuning timer will be 2 ^ (n - 1).
3156 */
3157 if (host->tuning_count)
3158 host->tuning_count = 1 << (host->tuning_count - 1);
3159
3160 /* Re-tuning mode supported by the Host Controller */
3161 host->tuning_mode = (caps[1] & SDHCI_RETUNING_MODE_MASK) >>
3162 SDHCI_RETUNING_MODE_SHIFT;
3163
Takashi Iwai8f230f42010-12-08 10:04:30 +01003164 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003165
Arindam Nathf2119df2011-05-05 12:18:57 +05303166 /*
3167 * According to SD Host Controller spec v3.00, if the Host System
3168 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3169 * the value is meaningful only if Voltage Support in the Capabilities
3170 * register is set. The actual current value is 4 times the register
3171 * value.
3172 */
3173 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003174 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003175 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003176 if (curr > 0) {
3177
3178 /* convert to SDHCI_MAX_CURRENT format */
3179 curr = curr/1000; /* convert to mA */
3180 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3181
3182 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3183 max_current_caps =
3184 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3185 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3186 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3187 }
3188 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303189
3190 if (caps[0] & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003191 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303192
Aaron Lu55c46652012-07-04 13:31:48 +08003193 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303194 SDHCI_MAX_CURRENT_330_MASK) >>
3195 SDHCI_MAX_CURRENT_330_SHIFT) *
3196 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303197 }
3198 if (caps[0] & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003199 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303200
Aaron Lu55c46652012-07-04 13:31:48 +08003201 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303202 SDHCI_MAX_CURRENT_300_MASK) >>
3203 SDHCI_MAX_CURRENT_300_SHIFT) *
3204 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303205 }
3206 if (caps[0] & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003207 ocr_avail |= MMC_VDD_165_195;
3208
Aaron Lu55c46652012-07-04 13:31:48 +08003209 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303210 SDHCI_MAX_CURRENT_180_MASK) >>
3211 SDHCI_MAX_CURRENT_180_SHIFT) *
3212 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303213 }
3214
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003215 /* If OCR set by host, use it instead. */
3216 if (host->ocr_mask)
3217 ocr_avail = host->ocr_mask;
3218
3219 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003220 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003221 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003222
Takashi Iwai8f230f42010-12-08 10:04:30 +01003223 mmc->ocr_avail = ocr_avail;
3224 mmc->ocr_avail_sdio = ocr_avail;
3225 if (host->ocr_avail_sdio)
3226 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3227 mmc->ocr_avail_sd = ocr_avail;
3228 if (host->ocr_avail_sd)
3229 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3230 else /* normal SD controllers don't support 1.8V */
3231 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3232 mmc->ocr_avail_mmc = ocr_avail;
3233 if (host->ocr_avail_mmc)
3234 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003235
3236 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003237 pr_err("%s: Hardware doesn't report any support voltages.\n",
3238 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003239 ret = -ENODEV;
3240 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003241 }
3242
Pierre Ossmand129bce2006-03-24 03:18:17 -08003243 spin_lock_init(&host->lock);
3244
3245 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003246 * Maximum number of segments. Depends on if the hardware
3247 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003248 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003249 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003250 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003251 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003252 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003253 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003254 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003255
3256 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003257 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3258 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3259 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003260 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003261 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003262
3263 /*
3264 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003265 * of bytes. When doing hardware scatter/gather, each entry cannot
3266 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003267 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003268 if (host->flags & SDHCI_USE_ADMA) {
3269 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3270 mmc->max_seg_size = 65535;
3271 else
3272 mmc->max_seg_size = 65536;
3273 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003274 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003275 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003276
3277 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003278 * Maximum block size. This varies from controller to controller and
3279 * is specified in the capabilities register.
3280 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003281 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3282 mmc->max_blk_size = 2;
3283 } else {
Arindam Nathf2119df2011-05-05 12:18:57 +05303284 mmc->max_blk_size = (caps[0] & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003285 SDHCI_MAX_BLOCK_SHIFT;
3286 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003287 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3288 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003289 mmc->max_blk_size = 0;
3290 }
3291 }
3292
3293 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003294
3295 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003296 * Maximum block count.
3297 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003298 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003299
3300 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003301 * Init tasklets.
3302 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003303 tasklet_init(&host->finish_tasklet,
3304 sdhci_tasklet_finish, (unsigned long)host);
3305
Al Viroe4cad1b2006-10-10 22:47:07 +01003306 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003307
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003308 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303309
Shawn Guo2af502c2013-07-05 14:38:55 +08003310 sdhci_init(host, 0);
3311
Russell King781e9892014-04-25 12:55:46 +01003312 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3313 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003314 if (ret) {
3315 pr_err("%s: Failed to request IRQ %d: %d\n",
3316 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003317 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003318 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003319
Pierre Ossmand129bce2006-03-24 03:18:17 -08003320#ifdef CONFIG_MMC_DEBUG
3321 sdhci_dumpregs(host);
3322#endif
3323
Adrian Hunter061d17a2016-04-12 14:25:09 +03003324 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003325 if (ret) {
3326 pr_err("%s: Failed to register LED device: %d\n",
3327 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003328 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003329 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003330
Pierre Ossman5f25a662006-10-04 02:15:39 -07003331 mmiowb();
3332
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003333 ret = mmc_add_host(mmc);
3334 if (ret)
3335 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003336
Girish K Sa3c76eb2011-10-11 11:44:09 +05303337 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003338 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003339 (host->flags & SDHCI_USE_ADMA) ?
3340 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003341 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003342
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003343 sdhci_enable_card_detection(host);
3344
Pierre Ossmand129bce2006-03-24 03:18:17 -08003345 return 0;
3346
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003347unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003348 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003349unirq:
Russell King03231f92014-04-25 12:57:12 +01003350 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003351 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3352 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003353 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003354untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003355 tasklet_kill(&host->finish_tasklet);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003356unreg:
3357 if (!IS_ERR(mmc->supply.vqmmc))
3358 regulator_disable(mmc->supply.vqmmc);
3359undma:
3360 if (host->align_buffer)
3361 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3362 host->adma_table_sz, host->align_buffer,
3363 host->align_addr);
3364 host->adma_table = NULL;
3365 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003366
3367 return ret;
3368}
3369
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003370EXPORT_SYMBOL_GPL(sdhci_add_host);
3371
Pierre Ossman1e728592008-04-16 19:13:13 +02003372void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003373{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003374 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003375 unsigned long flags;
3376
3377 if (dead) {
3378 spin_lock_irqsave(&host->lock, flags);
3379
3380 host->flags |= SDHCI_DEVICE_DEAD;
3381
3382 if (host->mrq) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303383 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003384 " transfer!\n", mmc_hostname(mmc));
Pierre Ossman1e728592008-04-16 19:13:13 +02003385
3386 host->mrq->cmd->error = -ENOMEDIUM;
3387 tasklet_schedule(&host->finish_tasklet);
3388 }
3389
3390 spin_unlock_irqrestore(&host->lock, flags);
3391 }
3392
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003393 sdhci_disable_card_detection(host);
3394
Markus Mayer4e743f12014-07-03 13:27:42 -07003395 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003396
Adrian Hunter061d17a2016-04-12 14:25:09 +03003397 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003398
Pierre Ossman1e728592008-04-16 19:13:13 +02003399 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003400 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003401
Russell Kingb537f942014-04-25 12:56:01 +01003402 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3403 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003404 free_irq(host->irq, host);
3405
3406 del_timer_sync(&host->timer);
3407
Pierre Ossmand129bce2006-03-24 03:18:17 -08003408 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003409
Tim Kryger3a48edc2014-06-13 10:13:56 -07003410 if (!IS_ERR(mmc->supply.vqmmc))
3411 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003412
Russell Kingedd63fc2016-01-26 13:39:50 +00003413 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003414 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3415 host->adma_table_sz, host->align_buffer,
3416 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003417
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003418 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003419 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003420}
3421
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003422EXPORT_SYMBOL_GPL(sdhci_remove_host);
3423
3424void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003425{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003426 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003427}
3428
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003429EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003430
3431/*****************************************************************************\
3432 * *
3433 * Driver init/exit *
3434 * *
3435\*****************************************************************************/
3436
3437static int __init sdhci_drv_init(void)
3438{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303439 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003440 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303441 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003442
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003443 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003444}
3445
3446static void __exit sdhci_drv_exit(void)
3447{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003448}
3449
3450module_init(sdhci_drv_init);
3451module_exit(sdhci_drv_exit);
3452
Pierre Ossmandf673b22006-06-30 02:22:31 -07003453module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003454module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003455
Pierre Ossman32710e82009-04-08 20:14:54 +02003456MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003457MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003458MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003459
Pierre Ossmandf673b22006-06-30 02:22:31 -07003460MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003461MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");