blob: e9290a3439d54c2a82ffd2533fc61e4d54637296 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
Adrian Hunter5a436cc2017-03-20 19:50:31 +020017#include <linux/ktime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080018#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010019#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020023#include <linux/scatterlist.h>
Ulf Hansson250dcd12017-11-27 11:28:50 +010024#include <linux/swiotlb.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070025#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030026#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050027#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080028
Pierre Ossman2f730fe2008-03-17 10:29:38 +010029#include <linux/leds.h>
30
Aries Lee22113ef2010-12-15 08:14:24 +010031#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080032#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080033#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080034#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080035#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080036
Pierre Ossmand129bce2006-03-24 03:18:17 -080037#include "sdhci.h"
38
39#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080040
Pierre Ossmand129bce2006-03-24 03:18:17 -080041#define DBG(f, x...) \
Adrian Hunterf4218652017-03-20 19:50:39 +020042 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080043
Adrian Hunter85ad90e2017-03-20 19:50:42 +020044#define SDHCI_DUMP(f, x...) \
45 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
46
Arindam Nathb513ea22011-05-05 12:19:04 +053047#define MAX_TUNING_LOOP 40
48
Pierre Ossmandf673b22006-06-30 02:22:31 -070049static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030050static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070051
Pierre Ossmand129bce2006-03-24 03:18:17 -080052static void sdhci_finish_data(struct sdhci_host *);
53
Kevin Liu52983382013-01-31 11:31:37 +080054static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080055
Adrian Hunterd2898172017-03-20 19:50:43 +020056void sdhci_dumpregs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -080057{
Adrian Hunter85ad90e2017-03-20 19:50:42 +020058 SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -080059
Adrian Hunter85ad90e2017-03-20 19:50:42 +020060 SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
61 sdhci_readl(host, SDHCI_DMA_ADDRESS),
62 sdhci_readw(host, SDHCI_HOST_VERSION));
63 SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
64 sdhci_readw(host, SDHCI_BLOCK_SIZE),
65 sdhci_readw(host, SDHCI_BLOCK_COUNT));
66 SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
67 sdhci_readl(host, SDHCI_ARGUMENT),
68 sdhci_readw(host, SDHCI_TRANSFER_MODE));
69 SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
70 sdhci_readl(host, SDHCI_PRESENT_STATE),
71 sdhci_readb(host, SDHCI_HOST_CONTROL));
72 SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
73 sdhci_readb(host, SDHCI_POWER_CONTROL),
74 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
75 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
76 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
77 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
78 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
79 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
80 sdhci_readl(host, SDHCI_INT_STATUS));
81 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
82 sdhci_readl(host, SDHCI_INT_ENABLE),
83 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
84 SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
85 sdhci_readw(host, SDHCI_ACMD12_ERR),
86 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
87 SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
88 sdhci_readl(host, SDHCI_CAPABILITIES),
89 sdhci_readl(host, SDHCI_CAPABILITIES_1));
90 SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
91 sdhci_readw(host, SDHCI_COMMAND),
92 sdhci_readl(host, SDHCI_MAX_CURRENT));
93 SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020094 sdhci_readl(host, SDHCI_RESPONSE),
95 sdhci_readl(host, SDHCI_RESPONSE + 4));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020096 SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020097 sdhci_readl(host, SDHCI_RESPONSE + 8),
98 sdhci_readl(host, SDHCI_RESPONSE + 12));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020099 SDHCI_DUMP("Host ctl2: 0x%08x\n",
100 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800101
Adrian Huntere57a5f62014-11-04 12:42:46 +0200102 if (host->flags & SDHCI_USE_ADMA) {
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200103 if (host->flags & SDHCI_USE_64_BIT_DMA) {
104 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
105 sdhci_readl(host, SDHCI_ADMA_ERROR),
106 sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
107 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
108 } else {
109 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
110 sdhci_readl(host, SDHCI_ADMA_ERROR),
111 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
112 }
Adrian Huntere57a5f62014-11-04 12:42:46 +0200113 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100114
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200115 SDHCI_DUMP("============================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800116}
Adrian Hunterd2898172017-03-20 19:50:43 +0200117EXPORT_SYMBOL_GPL(sdhci_dumpregs);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800118
119/*****************************************************************************\
120 * *
121 * Low level functions *
122 * *
123\*****************************************************************************/
124
Adrian Hunter56a590d2016-06-29 16:24:32 +0300125static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
126{
127 return cmd->data || cmd->flags & MMC_RSP_BUSY;
128}
129
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300130static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
131{
Russell King5b4f1f62014-04-25 12:57:02 +0100132 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300133
Adrian Hunterc79396c2011-12-27 15:48:42 +0200134 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900135 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300136 return;
137
Russell King5b4f1f62014-04-25 12:57:02 +0100138 if (enable) {
139 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
140 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800141
Russell King5b4f1f62014-04-25 12:57:02 +0100142 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
143 SDHCI_INT_CARD_INSERT;
144 } else {
145 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
146 }
Russell Kingb537f942014-04-25 12:56:01 +0100147
148 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
149 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300150}
151
152static void sdhci_enable_card_detection(struct sdhci_host *host)
153{
154 sdhci_set_card_detection(host, true);
155}
156
157static void sdhci_disable_card_detection(struct sdhci_host *host)
158{
159 sdhci_set_card_detection(host, false);
160}
161
Ulf Hansson02d0b682016-04-11 15:32:41 +0200162static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
163{
164 if (host->bus_on)
165 return;
166 host->bus_on = true;
167 pm_runtime_get_noresume(host->mmc->parent);
168}
169
170static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
171{
172 if (!host->bus_on)
173 return;
174 host->bus_on = false;
175 pm_runtime_put_noidle(host->mmc->parent);
176}
177
Russell King03231f92014-04-25 12:57:12 +0100178void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200180 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800181
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300182 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183
Adrian Hunterf0710a52013-05-06 12:17:32 +0300184 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800185 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300186 /* Reset-all turns off SD Bus Power */
187 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
188 sdhci_runtime_pm_bus_off(host);
189 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800190
Pierre Ossmane16514d82006-06-30 02:22:24 -0700191 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200192 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700193
194 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300195 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200196 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530197 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700198 mmc_hostname(host->mmc), (int)mask);
199 sdhci_dumpregs(host);
200 return;
201 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200202 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800203 }
Russell King03231f92014-04-25 12:57:12 +0100204}
205EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300206
Russell King03231f92014-04-25 12:57:12 +0100207static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
208{
209 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300210 struct mmc_host *mmc = host->mmc;
211
212 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100213 return;
214 }
215
216 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800217
Russell Kingda91a8f2014-04-25 13:00:12 +0100218 if (mask & SDHCI_RESET_ALL) {
219 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
220 if (host->ops->enable_dma)
221 host->ops->enable_dma(host);
222 }
223
224 /* Resetting the controller clears many */
225 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800226 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800227}
228
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200229static void sdhci_set_default_irqs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800230{
Russell Kingb537f942014-04-25 12:56:01 +0100231 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
232 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
233 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
234 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
235 SDHCI_INT_RESPONSE;
236
Dong Aishengf37b20e2016-07-12 15:46:17 +0800237 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
238 host->tuning_mode == SDHCI_TUNING_MODE_3)
239 host->ier |= SDHCI_INT_RETUNE;
240
Russell Kingb537f942014-04-25 12:56:01 +0100241 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
242 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200243}
244
245static void sdhci_init(struct sdhci_host *host, int soft)
246{
247 struct mmc_host *mmc = host->mmc;
248
249 if (soft)
250 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
251 else
252 sdhci_do_reset(host, SDHCI_RESET_ALL);
253
254 sdhci_set_default_irqs(host);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800255
Adrian Hunterf12e39d2017-03-20 19:50:47 +0200256 host->cqe_on = false;
257
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800258 if (soft) {
259 /* force clock reconfiguration */
260 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300261 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800262 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300263}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800264
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300265static void sdhci_reinit(struct sdhci_host *host)
266{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800267 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300268 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269}
270
Adrian Hunter061d17a2016-04-12 14:25:09 +0300271static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272{
273 u8 ctrl;
274
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300277 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278}
279
Adrian Hunter061d17a2016-04-12 14:25:09 +0300280static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800281{
282 u8 ctrl;
283
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300284 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800285 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300286 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800287}
288
Masahiro Yamada4f782302016-04-14 13:19:39 +0900289#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100290static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300291 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100292{
293 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
294 unsigned long flags;
295
296 spin_lock_irqsave(&host->lock, flags);
297
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300298 if (host->runtime_suspended)
299 goto out;
300
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100301 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300302 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100303 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300304 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300305out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100306 spin_unlock_irqrestore(&host->lock, flags);
307}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300308
309static int sdhci_led_register(struct sdhci_host *host)
310{
311 struct mmc_host *mmc = host->mmc;
312
313 snprintf(host->led_name, sizeof(host->led_name),
314 "%s::", mmc_hostname(mmc));
315
316 host->led.name = host->led_name;
317 host->led.brightness = LED_OFF;
318 host->led.default_trigger = mmc_hostname(mmc);
319 host->led.brightness_set = sdhci_led_control;
320
321 return led_classdev_register(mmc_dev(mmc), &host->led);
322}
323
324static void sdhci_led_unregister(struct sdhci_host *host)
325{
326 led_classdev_unregister(&host->led);
327}
328
329static inline void sdhci_led_activate(struct sdhci_host *host)
330{
331}
332
333static inline void sdhci_led_deactivate(struct sdhci_host *host)
334{
335}
336
337#else
338
339static inline int sdhci_led_register(struct sdhci_host *host)
340{
341 return 0;
342}
343
344static inline void sdhci_led_unregister(struct sdhci_host *host)
345{
346}
347
348static inline void sdhci_led_activate(struct sdhci_host *host)
349{
350 __sdhci_led_activate(host);
351}
352
353static inline void sdhci_led_deactivate(struct sdhci_host *host)
354{
355 __sdhci_led_deactivate(host);
356}
357
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100358#endif
359
Pierre Ossmand129bce2006-03-24 03:18:17 -0800360/*****************************************************************************\
361 * *
362 * Core functions *
363 * *
364\*****************************************************************************/
365
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800367{
Pierre Ossman76591502008-07-21 00:32:11 +0200368 unsigned long flags;
369 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700370 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200371 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800372
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100373 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800374
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100375 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200376 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800377
Pierre Ossman76591502008-07-21 00:32:11 +0200378 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800379
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100380 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300381 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800382
Pierre Ossman76591502008-07-21 00:32:11 +0200383 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800384
Pierre Ossman76591502008-07-21 00:32:11 +0200385 blksize -= len;
386 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200387
Pierre Ossman76591502008-07-21 00:32:11 +0200388 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800389
Pierre Ossman76591502008-07-21 00:32:11 +0200390 while (len) {
391 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300392 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200393 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800394 }
Pierre Ossman76591502008-07-21 00:32:11 +0200395
396 *buf = scratch & 0xFF;
397
398 buf++;
399 scratch >>= 8;
400 chunk--;
401 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800402 }
403 }
Pierre Ossman76591502008-07-21 00:32:11 +0200404
405 sg_miter_stop(&host->sg_miter);
406
407 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100408}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800409
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100410static void sdhci_write_block_pio(struct sdhci_host *host)
411{
Pierre Ossman76591502008-07-21 00:32:11 +0200412 unsigned long flags;
413 size_t blksize, len, chunk;
414 u32 scratch;
415 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416
417 DBG("PIO writing\n");
418
419 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200420 chunk = 0;
421 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100422
Pierre Ossman76591502008-07-21 00:32:11 +0200423 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100424
425 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300426 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100427
Pierre Ossman76591502008-07-21 00:32:11 +0200428 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200429
Pierre Ossman76591502008-07-21 00:32:11 +0200430 blksize -= len;
431 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100432
Pierre Ossman76591502008-07-21 00:32:11 +0200433 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100434
Pierre Ossman76591502008-07-21 00:32:11 +0200435 while (len) {
436 scratch |= (u32)*buf << (chunk * 8);
437
438 buf++;
439 chunk++;
440 len--;
441
442 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300443 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200444 chunk = 0;
445 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100446 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100447 }
448 }
Pierre Ossman76591502008-07-21 00:32:11 +0200449
450 sg_miter_stop(&host->sg_miter);
451
452 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100453}
454
455static void sdhci_transfer_pio(struct sdhci_host *host)
456{
457 u32 mask;
458
Pierre Ossman76591502008-07-21 00:32:11 +0200459 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100460 return;
461
462 if (host->data->flags & MMC_DATA_READ)
463 mask = SDHCI_DATA_AVAILABLE;
464 else
465 mask = SDHCI_SPACE_AVAILABLE;
466
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200467 /*
468 * Some controllers (JMicron JMB38x) mess up the buffer bits
469 * for transfers < 4 bytes. As long as it is just one block,
470 * we can ignore the bits.
471 */
472 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
473 (host->data->blocks == 1))
474 mask = ~0;
475
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300476 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300477 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
478 udelay(100);
479
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100480 if (host->data->flags & MMC_DATA_READ)
481 sdhci_read_block_pio(host);
482 else
483 sdhci_write_block_pio(host);
484
Pierre Ossman76591502008-07-21 00:32:11 +0200485 host->blocks--;
486 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100487 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100488 }
489
490 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800491}
492
Russell King48857d92016-01-26 13:40:16 +0000493static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000494 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000495{
496 int sg_count;
497
Russell King94538e52016-01-26 13:40:37 +0000498 /*
499 * If the data buffers are already mapped, return the previous
500 * dma_map_sg() result.
501 */
502 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000503 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000504
505 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +0200506 mmc_get_dma_dir(data));
Russell King48857d92016-01-26 13:40:16 +0000507
508 if (sg_count == 0)
509 return -ENOSPC;
510
511 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000512 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000513
514 return sg_count;
515}
516
Pierre Ossman2134a922008-06-28 18:28:51 +0200517static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
518{
519 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800520 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200521}
522
523static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
524{
Cong Wang482fce92011-11-27 13:27:00 +0800525 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200526 local_irq_restore(*flags);
527}
528
Adrian Huntere57a5f62014-11-04 12:42:46 +0200529static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
530 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800531{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200532 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800533
Adrian Huntere57a5f62014-11-04 12:42:46 +0200534 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200535 dma_desc->cmd = cpu_to_le16(cmd);
536 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200537 dma_desc->addr_lo = cpu_to_le32((u32)addr);
538
539 if (host->flags & SDHCI_USE_64_BIT_DMA)
540 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800541}
542
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200543static void sdhci_adma_mark_end(void *desc)
544{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200545 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200546
Adrian Huntere57a5f62014-11-04 12:42:46 +0200547 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200548 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200549}
550
Russell King60c64762016-01-26 13:40:22 +0000551static void sdhci_adma_table_pre(struct sdhci_host *host,
552 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200553{
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200555 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000556 dma_addr_t addr, align_addr;
557 void *desc, *align;
558 char *buffer;
559 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200560
561 /*
562 * The spec does not specify endianness of descriptor table.
563 * We currently guess that it is LE.
564 */
565
Russell King60c64762016-01-26 13:40:22 +0000566 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200567
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200568 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200569 align = host->align_buffer;
570
571 align_addr = host->align_addr;
572
573 for_each_sg(data->sg, sg, host->sg_count, i) {
574 addr = sg_dma_address(sg);
575 len = sg_dma_len(sg);
576
577 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000578 * The SDHCI specification states that ADMA addresses must
579 * be 32-bit aligned. If they aren't, then we use a bounce
580 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200581 * alignment.
582 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200583 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
584 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585 if (offset) {
586 if (data->flags & MMC_DATA_WRITE) {
587 buffer = sdhci_kmap_atomic(sg, &flags);
588 memcpy(align, buffer, offset);
589 sdhci_kunmap_atomic(buffer, &flags);
590 }
591
Ben Dooks118cd172010-03-05 13:43:26 -0800592 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200593 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200594 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200595
596 BUG_ON(offset > 65536);
597
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200598 align += SDHCI_ADMA2_ALIGN;
599 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200600
Adrian Hunter76fe3792014-11-04 12:42:42 +0200601 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200602
603 addr += offset;
604 len -= offset;
605 }
606
Pierre Ossman2134a922008-06-28 18:28:51 +0200607 BUG_ON(len > 65536);
608
Adrian Hunter347ea322015-11-26 14:00:48 +0200609 if (len) {
610 /* tran, valid */
611 sdhci_adma_write_desc(host, desc, addr, len,
612 ADMA2_TRAN_VALID);
613 desc += host->desc_sz;
614 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200615
616 /*
617 * If this triggers then we have a calculation bug
618 * somewhere. :/
619 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200620 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 }
622
Thomas Abraham70764a92010-05-26 14:42:04 -0700623 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000624 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200625 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200626 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200627 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700628 }
629 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000630 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200631 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700632 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200633}
634
635static void sdhci_adma_table_post(struct sdhci_host *host,
636 struct mmc_data *data)
637{
Pierre Ossman2134a922008-06-28 18:28:51 +0200638 struct scatterlist *sg;
639 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200640 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200641 char *buffer;
642 unsigned long flags;
643
Russell King47fa9612016-01-26 13:40:06 +0000644 if (data->flags & MMC_DATA_READ) {
645 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100646
Russell King47fa9612016-01-26 13:40:06 +0000647 /* Do a quick scan of the SG list for any unaligned mappings */
648 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200649 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000650 has_unaligned = true;
651 break;
652 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200653
Russell King47fa9612016-01-26 13:40:06 +0000654 if (has_unaligned) {
655 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000656 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200657
Russell King47fa9612016-01-26 13:40:06 +0000658 align = host->align_buffer;
659
660 for_each_sg(data->sg, sg, host->sg_count, i) {
661 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
662 size = SDHCI_ADMA2_ALIGN -
663 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
664
665 buffer = sdhci_kmap_atomic(sg, &flags);
666 memcpy(buffer, align, size);
667 sdhci_kunmap_atomic(buffer, &flags);
668
669 align += SDHCI_ADMA2_ALIGN;
670 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200671 }
672 }
673 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200674}
675
Andrei Warkentina3c77782011-04-11 16:13:42 -0500676static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800677{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700678 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500679 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700680 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800681
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200682 /*
683 * If the host controller provides us with an incorrect timeout
684 * value, just skip the check and use 0xE. The hardware may take
685 * longer to time out, but that's much better than having a too-short
686 * timeout value.
687 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200688 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200689 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200690
Andrei Warkentina3c77782011-04-11 16:13:42 -0500691 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100692 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500693 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800694
Andrei Warkentina3c77782011-04-11 16:13:42 -0500695 /* timeout in us */
696 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100697 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300698 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000699 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000700 if (host->clock && data->timeout_clks) {
701 unsigned long long val;
702
703 /*
704 * data->timeout_clks is in units of clock cycles.
705 * host->clock is in Hz. target_timeout is in us.
706 * Hence, us = 1000000 * cycles / Hz. Round up.
707 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200708 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000709 if (do_div(val, host->clock))
710 target_timeout++;
711 target_timeout += val;
712 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300713 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700714
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700715 /*
716 * Figure out needed cycles.
717 * We do this in steps in order to fit inside a 32 bit int.
718 * The first step is the minimum timeout, which will have a
719 * minimum resolution of 6 bits:
720 * (1) 2^13*1000 > 2^22,
721 * (2) host->timeout_clk < 2^16
722 * =>
723 * (1) / (2) > 2^6
724 */
725 count = 0;
726 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
727 while (current_timeout < target_timeout) {
728 count++;
729 current_timeout <<= 1;
730 if (count >= 0xF)
731 break;
732 }
733
734 if (count >= 0xF) {
Adrian Hunterf4218652017-03-20 19:50:39 +0200735 DBG("Too large timeout 0x%x requested for CMD%d!\n",
736 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700737 count = 0xE;
738 }
739
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200740 return count;
741}
742
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300743static void sdhci_set_transfer_irqs(struct sdhci_host *host)
744{
745 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
746 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
747
748 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100749 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300750 else
Russell Kingb537f942014-04-25 12:56:01 +0100751 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
752
753 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
754 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300755}
756
Aisheng Dongb45e6682014-08-27 15:26:29 +0800757static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200758{
759 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800760
761 if (host->ops->set_timeout) {
762 host->ops->set_timeout(host, cmd);
763 } else {
764 count = sdhci_calc_timeout(host, cmd);
765 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
766 }
767}
768
769static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
770{
Pierre Ossman2134a922008-06-28 18:28:51 +0200771 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500772 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200773
Adrian Hunter56a590d2016-06-29 16:24:32 +0300774 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800775 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500776
777 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200778 return;
779
Adrian Hunter43dea092016-06-29 16:24:26 +0300780 WARN_ON(host->data);
781
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200782 /* Sanity checks */
783 BUG_ON(data->blksz * data->blocks > 524288);
784 BUG_ON(data->blksz > host->mmc->max_blk_size);
785 BUG_ON(data->blocks > 65535);
786
787 host->data = data;
788 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400789 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200790
Russell Kingfce14422016-01-26 13:41:20 +0000791 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200792 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000793 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000794 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200795
Russell Kingfce14422016-01-26 13:41:20 +0000796 host->flags |= SDHCI_REQ_USE_DMA;
797
798 /*
799 * FIXME: This doesn't account for merging when mapping the
800 * scatterlist.
801 *
802 * The assumption here being that alignment and lengths are
803 * the same after DMA mapping to device address space.
804 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000805 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000806 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200807 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000808 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000809 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000810 /*
811 * As we use up to 3 byte chunks to work
812 * around alignment problems, we need to
813 * check the offset as well.
814 */
815 offset_mask = 3;
816 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200817 } else {
818 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000819 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000820 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
821 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200822 }
823
Russell Kingdf953922016-01-26 13:41:14 +0000824 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200825 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000826 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100827 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000828 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200829 host->flags &= ~SDHCI_REQ_USE_DMA;
830 break;
831 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000832 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100833 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200834 host->flags &= ~SDHCI_REQ_USE_DMA;
835 break;
836 }
837 }
838 }
839 }
840
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200841 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000842 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200843
Russell King60c64762016-01-26 13:40:22 +0000844 if (sg_cnt <= 0) {
845 /*
846 * This only happens when someone fed
847 * us an invalid request.
848 */
849 WARN_ON(1);
850 host->flags &= ~SDHCI_REQ_USE_DMA;
851 } else if (host->flags & SDHCI_USE_ADMA) {
852 sdhci_adma_table_pre(host, data, sg_cnt);
853
854 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
855 if (host->flags & SDHCI_USE_64_BIT_DMA)
856 sdhci_writel(host,
857 (u64)host->adma_addr >> 32,
858 SDHCI_ADMA_ADDRESS_HI);
859 } else {
860 WARN_ON(sg_cnt != 1);
861 sdhci_writel(host, sg_dma_address(data->sg),
862 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200863 }
864 }
865
Pierre Ossman2134a922008-06-28 18:28:51 +0200866 /*
867 * Always adjust the DMA selection as some controllers
868 * (e.g. JMicron) can't do PIO properly when the selection
869 * is ADMA.
870 */
871 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300872 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200873 ctrl &= ~SDHCI_CTRL_DMA_MASK;
874 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200875 (host->flags & SDHCI_USE_ADMA)) {
876 if (host->flags & SDHCI_USE_64_BIT_DMA)
877 ctrl |= SDHCI_CTRL_ADMA64;
878 else
879 ctrl |= SDHCI_CTRL_ADMA32;
880 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200881 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200882 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300883 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100884 }
885
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200886 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200887 int flags;
888
889 flags = SG_MITER_ATOMIC;
890 if (host->data->flags & MMC_DATA_READ)
891 flags |= SG_MITER_TO_SG;
892 else
893 flags |= SG_MITER_FROM_SG;
894 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200895 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800896 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700897
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300898 sdhci_set_transfer_irqs(host);
899
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400900 /* Set the DMA boundary value and block size */
Srinivas Kandagatlac846a002017-08-03 14:46:13 +0200901 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
902 SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300903 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700904}
905
Adrian Hunter0293d502016-06-29 16:24:35 +0300906static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
907 struct mmc_request *mrq)
908{
Adrian Hunter20845be2016-08-16 13:44:13 +0300909 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
910 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300911}
912
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700913static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500914 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700915{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800916 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500917 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700918
Dong Aisheng2b558c12013-10-30 22:09:48 +0800919 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800920 if (host->quirks2 &
921 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
922 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
923 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800924 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800925 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
926 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800927 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800928 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700929 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800930 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700931
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200932 WARN_ON(!host->data);
933
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800934 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
935 mode = SDHCI_TRNS_BLK_CNT_EN;
936
Andrei Warkentine89d4562011-05-23 15:06:37 -0500937 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800938 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500939 /*
940 * If we are sending CMD23, CMD12 never gets sent
941 * on successful completion (so no Auto-CMD12).
942 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300943 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800944 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500945 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300946 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500947 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300948 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500949 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700950 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500951
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700952 if (data->flags & MMC_DATA_READ)
953 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100954 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700955 mode |= SDHCI_TRNS_DMA;
956
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300957 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800958}
959
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300960static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
961{
962 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
963 ((mrq->cmd && mrq->cmd->error) ||
964 (mrq->sbc && mrq->sbc->error) ||
965 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
966 (mrq->data->stop && mrq->data->stop->error))) ||
967 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
968}
969
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300970static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
971{
972 int i;
973
974 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
975 if (host->mrqs_done[i] == mrq) {
976 WARN_ON(1);
977 return;
978 }
979 }
980
981 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
982 if (!host->mrqs_done[i]) {
983 host->mrqs_done[i] = mrq;
984 break;
985 }
986 }
987
988 WARN_ON(i >= SDHCI_MAX_MRQS);
989
990 tasklet_schedule(&host->finish_tasklet);
991}
992
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300993static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
994{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300995 if (host->cmd && host->cmd->mrq == mrq)
996 host->cmd = NULL;
997
998 if (host->data_cmd && host->data_cmd->mrq == mrq)
999 host->data_cmd = NULL;
1000
1001 if (host->data && host->data->mrq == mrq)
1002 host->data = NULL;
1003
Adrian Huntered1563d2016-06-29 16:24:29 +03001004 if (sdhci_needs_reset(host, mrq))
1005 host->pending_reset = true;
1006
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001007 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001008}
1009
Pierre Ossmand129bce2006-03-24 03:18:17 -08001010static void sdhci_finish_data(struct sdhci_host *host)
1011{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001012 struct mmc_command *data_cmd = host->data_cmd;
1013 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014
Pierre Ossmand129bce2006-03-24 03:18:17 -08001015 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001016 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001017
Russell Kingadd89132016-01-26 13:40:42 +00001018 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1019 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1020 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001021
1022 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001023 * The specification states that the block count register must
1024 * be updated, but it does not specify at what point in the
1025 * data flow. That makes the register entirely useless to read
1026 * back so we have to assume that nothing made it to the card
1027 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001028 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001029 if (data->error)
1030 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001031 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001032 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001033
Andrei Warkentine89d4562011-05-23 15:06:37 -05001034 /*
1035 * Need to send CMD12 if -
1036 * a) open-ended multiblock transfer (no CMD23)
1037 * b) error in multiblock transfer
1038 */
1039 if (data->stop &&
1040 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001041 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001042
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043 /*
1044 * The controller needs a reset of internal state machines
1045 * upon error conditions.
1046 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001047 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001048 if (!host->cmd || host->cmd == data_cmd)
1049 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001050 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051 }
1052
Adrian Hunter20845be2016-08-16 13:44:13 +03001053 /*
1054 * 'cap_cmd_during_tfr' request must not use the command line
1055 * after mmc_command_done() has been called. It is upper layer's
1056 * responsibility to send the stop command if required.
1057 */
1058 if (data->mrq->cap_cmd_during_tfr) {
1059 sdhci_finish_mrq(host, data->mrq);
1060 } else {
1061 /* Avoid triggering warning in sdhci_send_command() */
1062 host->cmd = NULL;
1063 sdhci_send_command(host, data->stop);
1064 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001065 } else {
1066 sdhci_finish_mrq(host, data->mrq);
1067 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001068}
1069
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001070static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1071 unsigned long timeout)
1072{
1073 if (sdhci_data_line_cmd(mrq->cmd))
1074 mod_timer(&host->data_timer, timeout);
1075 else
1076 mod_timer(&host->timer, timeout);
1077}
1078
1079static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1080{
1081 if (sdhci_data_line_cmd(mrq->cmd))
1082 del_timer(&host->data_timer);
1083 else
1084 del_timer(&host->timer);
1085}
1086
Dong Aishengc0e551292013-09-13 19:11:31 +08001087void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001088{
1089 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001090 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001091 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001092
1093 WARN_ON(host->cmd);
1094
Russell King96776202016-01-26 13:39:34 +00001095 /* Initially, a command has no error */
1096 cmd->error = 0;
1097
Adrian Hunterfc605f12016-10-05 12:11:21 +03001098 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1099 cmd->opcode == MMC_STOP_TRANSMISSION)
1100 cmd->flags |= MMC_RSP_BUSY;
1101
Pierre Ossmand129bce2006-03-24 03:18:17 -08001102 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001103 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001104
1105 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001106 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001107 mask |= SDHCI_DATA_INHIBIT;
1108
1109 /* We shouldn't wait for data inihibit for stop commands, even
1110 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001111 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001112 mask &= ~SDHCI_DATA_INHIBIT;
1113
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001114 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001115 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001116 pr_err("%s: Controller never released inhibit bit(s).\n",
1117 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001119 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001120 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001121 return;
1122 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001123 timeout--;
1124 mdelay(1);
1125 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001127 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001128 if (!cmd->data && cmd->busy_timeout > 9000)
1129 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001130 else
1131 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001132 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
1134 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001135 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001136 WARN_ON(host->data_cmd);
1137 host->data_cmd = cmd;
1138 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001139
Andrei Warkentina3c77782011-04-11 16:13:42 -05001140 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001141
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001142 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143
Andrei Warkentine89d4562011-05-23 15:06:37 -05001144 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001145
Pierre Ossmand129bce2006-03-24 03:18:17 -08001146 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301147 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001148 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001149 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001150 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001151 return;
1152 }
1153
1154 if (!(cmd->flags & MMC_RSP_PRESENT))
1155 flags = SDHCI_CMD_RESP_NONE;
1156 else if (cmd->flags & MMC_RSP_136)
1157 flags = SDHCI_CMD_RESP_LONG;
1158 else if (cmd->flags & MMC_RSP_BUSY)
1159 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1160 else
1161 flags = SDHCI_CMD_RESP_SHORT;
1162
1163 if (cmd->flags & MMC_RSP_CRC)
1164 flags |= SDHCI_CMD_CRC;
1165 if (cmd->flags & MMC_RSP_OPCODE)
1166 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301167
1168 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301169 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1170 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001171 flags |= SDHCI_CMD_DATA;
1172
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001173 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001174}
Dong Aishengc0e551292013-09-13 19:11:31 +08001175EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301177static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
1178{
1179 int i, reg;
1180
1181 for (i = 0; i < 4; i++) {
1182 reg = SDHCI_RESPONSE + (3 - i) * 4;
1183 cmd->resp[i] = sdhci_readl(host, reg);
1184 }
1185
Kishon Vijay Abraham I1284c242017-08-21 13:11:29 +05301186 if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
1187 return;
1188
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301189 /* CRC is stripped so we need to do some shifting */
1190 for (i = 0; i < 4; i++) {
1191 cmd->resp[i] <<= 8;
1192 if (i != 3)
1193 cmd->resp[i] |= cmd->resp[i + 1] >> 24;
1194 }
1195}
1196
Pierre Ossmand129bce2006-03-24 03:18:17 -08001197static void sdhci_finish_command(struct sdhci_host *host)
1198{
Adrian Huntere0a56402016-06-29 16:24:22 +03001199 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200
Adrian Huntere0a56402016-06-29 16:24:22 +03001201 host->cmd = NULL;
1202
1203 if (cmd->flags & MMC_RSP_PRESENT) {
1204 if (cmd->flags & MMC_RSP_136) {
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301205 sdhci_read_rsp_136(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001206 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001207 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001208 }
1209 }
1210
Adrian Hunter20845be2016-08-16 13:44:13 +03001211 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1212 mmc_command_done(host->mmc, cmd->mrq);
1213
Adrian Hunter6bde8682016-06-29 16:24:20 +03001214 /*
1215 * The host can send and interrupt when the busy state has
1216 * ended, allowing us to wait without wasting CPU cycles.
1217 * The busy signal uses DAT0 so this is similar to waiting
1218 * for data to complete.
1219 *
1220 * Note: The 1.0 specification is a bit ambiguous about this
1221 * feature so there might be some problems with older
1222 * controllers.
1223 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001224 if (cmd->flags & MMC_RSP_BUSY) {
1225 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001226 DBG("Cannot wait for busy signal when also doing a data transfer");
1227 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001228 cmd == host->data_cmd) {
1229 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001230 return;
1231 }
1232 }
1233
Andrei Warkentine89d4562011-05-23 15:06:37 -05001234 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001235 if (cmd == cmd->mrq->sbc) {
1236 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001237 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001238
Andrei Warkentine89d4562011-05-23 15:06:37 -05001239 /* Processed actual command. */
1240 if (host->data && host->data_early)
1241 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001242
Adrian Huntere0a56402016-06-29 16:24:22 +03001243 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001244 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001245 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246}
1247
Kevin Liu52983382013-01-31 11:31:37 +08001248static u16 sdhci_get_preset_value(struct sdhci_host *host)
1249{
Russell Kingd975f122014-04-25 12:59:31 +01001250 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001251
Russell Kingd975f122014-04-25 12:59:31 +01001252 switch (host->timing) {
1253 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001254 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1255 break;
Russell Kingd975f122014-04-25 12:59:31 +01001256 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001257 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1258 break;
Russell Kingd975f122014-04-25 12:59:31 +01001259 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001260 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1261 break;
Russell Kingd975f122014-04-25 12:59:31 +01001262 case MMC_TIMING_UHS_SDR104:
1263 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001264 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1265 break;
Russell Kingd975f122014-04-25 12:59:31 +01001266 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001267 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001268 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1269 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001270 case MMC_TIMING_MMC_HS400:
1271 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1272 break;
Kevin Liu52983382013-01-31 11:31:37 +08001273 default:
1274 pr_warn("%s: Invalid UHS-I mode selected\n",
1275 mmc_hostname(host->mmc));
1276 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1277 break;
1278 }
1279 return preset;
1280}
1281
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001282u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1283 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001284{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301285 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001286 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301287 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001288 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001289
Zhangfei Gao85105c52010-08-06 07:10:01 +08001290 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001291 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001292 u16 pre_val;
1293
1294 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1295 pre_val = sdhci_get_preset_value(host);
1296 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1297 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1298 if (host->clk_mul &&
1299 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1300 clk = SDHCI_PROG_CLOCK_MODE;
1301 real_div = div + 1;
1302 clk_mul = host->clk_mul;
1303 } else {
1304 real_div = max_t(int, 1, div << 1);
1305 }
1306 goto clock_set;
1307 }
1308
Arindam Nathc3ed3872011-05-05 12:19:06 +05301309 /*
1310 * Check if the Host Controller supports Programmable Clock
1311 * Mode.
1312 */
1313 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001314 for (div = 1; div <= 1024; div++) {
1315 if ((host->max_clk * host->clk_mul / div)
1316 <= clock)
1317 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001318 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001319 if ((host->max_clk * host->clk_mul / div) <= clock) {
1320 /*
1321 * Set Programmable Clock Mode in the Clock
1322 * Control register.
1323 */
1324 clk = SDHCI_PROG_CLOCK_MODE;
1325 real_div = div;
1326 clk_mul = host->clk_mul;
1327 div--;
1328 } else {
1329 /*
1330 * Divisor can be too small to reach clock
1331 * speed requirement. Then use the base clock.
1332 */
1333 switch_base_clk = true;
1334 }
1335 }
1336
1337 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301338 /* Version 3.00 divisors must be a multiple of 2. */
1339 if (host->max_clk <= clock)
1340 div = 1;
1341 else {
1342 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1343 div += 2) {
1344 if ((host->max_clk / div) <= clock)
1345 break;
1346 }
1347 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001348 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301349 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301350 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1351 && !div && host->max_clk <= 25000000)
1352 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001353 }
1354 } else {
1355 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001356 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001357 if ((host->max_clk / div) <= clock)
1358 break;
1359 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001360 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301361 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001362 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001363
Kevin Liu52983382013-01-31 11:31:37 +08001364clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001365 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001366 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301367 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001368 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1369 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001370
1371 return clk;
1372}
1373EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1374
Ritesh Harjanifec79672016-11-21 12:07:19 +05301375void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001376{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001377 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001378
Pierre Ossmand129bce2006-03-24 03:18:17 -08001379 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001380 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001381
Chris Ball27f6cb12009-09-22 16:45:31 -07001382 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001383 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001384 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001385 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001386 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001387 pr_err("%s: Internal clock never stabilised.\n",
1388 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389 sdhci_dumpregs(host);
1390 return;
1391 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001392 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001393 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001394
1395 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001396 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001397}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301398EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1399
1400void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1401{
1402 u16 clk;
1403
1404 host->mmc->actual_clock = 0;
1405
1406 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1407
1408 if (clock == 0)
1409 return;
1410
1411 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1412 sdhci_enable_clk(host, clk);
1413}
Russell King17710592014-04-25 12:58:55 +01001414EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001415
Adrian Hunter1dceb042016-03-29 12:45:43 +03001416static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1417 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001418{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001419 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001420
Adrian Hunter1dceb042016-03-29 12:45:43 +03001421 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001422
1423 if (mode != MMC_POWER_OFF)
1424 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1425 else
1426 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1427}
1428
Adrian Hunter606d3132016-10-05 12:11:22 +03001429void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1430 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001431{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001432 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001433
Russell King24fbb3c2014-04-25 13:00:06 +01001434 if (mode != MMC_POWER_OFF) {
1435 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001436 case MMC_VDD_165_195:
1437 pwr = SDHCI_POWER_180;
1438 break;
1439 case MMC_VDD_29_30:
1440 case MMC_VDD_30_31:
1441 pwr = SDHCI_POWER_300;
1442 break;
1443 case MMC_VDD_32_33:
1444 case MMC_VDD_33_34:
1445 pwr = SDHCI_POWER_330;
1446 break;
1447 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001448 WARN(1, "%s: Invalid vdd %#x\n",
1449 mmc_hostname(host->mmc), vdd);
1450 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001451 }
1452 }
1453
1454 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001455 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001456
Pierre Ossmanae628902009-05-03 20:45:03 +02001457 host->pwr = pwr;
1458
1459 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001460 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001461 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1462 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001463 } else {
1464 /*
1465 * Spec says that we should clear the power reg before setting
1466 * a new value. Some controllers don't seem to like this though.
1467 */
1468 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1469 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001470
Russell Kinge921a8b2014-04-25 13:00:01 +01001471 /*
1472 * At least the Marvell CaFe chip gets confused if we set the
1473 * voltage and set turn on power at the same time, so set the
1474 * voltage first.
1475 */
1476 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1477 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001478
Russell Kinge921a8b2014-04-25 13:00:01 +01001479 pwr |= SDHCI_POWER_ON;
1480
Pierre Ossmanae628902009-05-03 20:45:03 +02001481 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1482
Russell Kinge921a8b2014-04-25 13:00:01 +01001483 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1484 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001485
Russell Kinge921a8b2014-04-25 13:00:01 +01001486 /*
1487 * Some controllers need an extra 10ms delay of 10ms before
1488 * they can apply clock after applying power
1489 */
1490 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1491 mdelay(10);
1492 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001493}
Adrian Hunter606d3132016-10-05 12:11:22 +03001494EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001495
Adrian Hunter606d3132016-10-05 12:11:22 +03001496void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1497 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001498{
Adrian Hunter606d3132016-10-05 12:11:22 +03001499 if (IS_ERR(host->mmc->supply.vmmc))
1500 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001501 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001502 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001503}
Adrian Hunter606d3132016-10-05 12:11:22 +03001504EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001505
Pierre Ossmand129bce2006-03-24 03:18:17 -08001506/*****************************************************************************\
1507 * *
1508 * MMC callbacks *
1509 * *
1510\*****************************************************************************/
1511
1512static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1513{
1514 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001515 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001516 unsigned long flags;
1517
1518 host = mmc_priv(mmc);
1519
Scott Branden04e079cf2015-03-10 11:35:10 -07001520 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001521 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001522
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523 spin_lock_irqsave(&host->lock, flags);
1524
Adrian Hunter061d17a2016-04-12 14:25:09 +03001525 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001526
1527 /*
1528 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1529 * requests if Auto-CMD12 is enabled.
1530 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001531 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001532 if (mrq->stop) {
1533 mrq->data->stop = NULL;
1534 mrq->stop = NULL;
1535 }
1536 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001537
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001538 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001539 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001540 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301541 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001542 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001543 sdhci_send_command(host, mrq->sbc);
1544 else
1545 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301546 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001547
Pierre Ossman5f25a662006-10-04 02:15:39 -07001548 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001549 spin_unlock_irqrestore(&host->lock, flags);
1550}
1551
Russell King2317f562014-04-25 12:57:07 +01001552void sdhci_set_bus_width(struct sdhci_host *host, int width)
1553{
1554 u8 ctrl;
1555
1556 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1557 if (width == MMC_BUS_WIDTH_8) {
1558 ctrl &= ~SDHCI_CTRL_4BITBUS;
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001559 ctrl |= SDHCI_CTRL_8BITBUS;
Russell King2317f562014-04-25 12:57:07 +01001560 } else {
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001561 if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
Russell King2317f562014-04-25 12:57:07 +01001562 ctrl &= ~SDHCI_CTRL_8BITBUS;
1563 if (width == MMC_BUS_WIDTH_4)
1564 ctrl |= SDHCI_CTRL_4BITBUS;
1565 else
1566 ctrl &= ~SDHCI_CTRL_4BITBUS;
1567 }
1568 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1569}
1570EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1571
Russell King96d7b782014-04-25 12:59:26 +01001572void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1573{
1574 u16 ctrl_2;
1575
1576 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1577 /* Select Bus Speed Mode for host */
1578 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1579 if ((timing == MMC_TIMING_MMC_HS200) ||
1580 (timing == MMC_TIMING_UHS_SDR104))
1581 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1582 else if (timing == MMC_TIMING_UHS_SDR12)
1583 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1584 else if (timing == MMC_TIMING_UHS_SDR25)
1585 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1586 else if (timing == MMC_TIMING_UHS_SDR50)
1587 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1588 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1589 (timing == MMC_TIMING_MMC_DDR52))
1590 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001591 else if (timing == MMC_TIMING_MMC_HS400)
1592 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001593 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1594}
1595EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1596
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001597void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001598{
Dong Aishengded97e02016-04-16 01:29:25 +08001599 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600 u8 ctrl;
1601
Adrian Hunter84ec0482016-12-19 15:33:11 +02001602 if (ios->power_mode == MMC_POWER_UNDEFINED)
1603 return;
1604
Adrian Hunterceb61432011-12-27 15:48:41 +02001605 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001606 if (!IS_ERR(mmc->supply.vmmc) &&
1607 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001608 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001609 return;
1610 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001611
Pierre Ossmand129bce2006-03-24 03:18:17 -08001612 /*
1613 * Reset the chip on each power off.
1614 * Should clear out any weird states.
1615 */
1616 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001617 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001618 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619 }
1620
Kevin Liu52983382013-01-31 11:31:37 +08001621 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001622 (ios->power_mode == MMC_POWER_UP) &&
1623 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001624 sdhci_enable_preset_value(host, false);
1625
Russell King373073e2014-04-25 12:58:45 +01001626 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001627 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001628 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001629
1630 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1631 host->clock) {
1632 host->timeout_clk = host->mmc->actual_clock ?
1633 host->mmc->actual_clock / 1000 :
1634 host->clock / 1000;
1635 host->mmc->max_busy_timeout =
1636 host->ops->get_max_timeout_count ?
1637 host->ops->get_max_timeout_count(host) :
1638 1 << 27;
1639 host->mmc->max_busy_timeout /= host->timeout_clk;
1640 }
Russell King373073e2014-04-25 12:58:45 +01001641 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001642
Adrian Hunter606d3132016-10-05 12:11:22 +03001643 if (host->ops->set_power)
1644 host->ops->set_power(host, ios->power_mode, ios->vdd);
1645 else
1646 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001647
Philip Rakity643a81f2010-09-23 08:24:32 -07001648 if (host->ops->platform_send_init_74_clocks)
1649 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1650
Russell King2317f562014-04-25 12:57:07 +01001651 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001652
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001653 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001654
yangbo lu501639b2017-08-15 10:16:47 +08001655 if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
1656 if (ios->timing == MMC_TIMING_SD_HS ||
1657 ios->timing == MMC_TIMING_MMC_HS ||
1658 ios->timing == MMC_TIMING_MMC_HS400 ||
1659 ios->timing == MMC_TIMING_MMC_HS200 ||
1660 ios->timing == MMC_TIMING_MMC_DDR52 ||
1661 ios->timing == MMC_TIMING_UHS_SDR50 ||
1662 ios->timing == MMC_TIMING_UHS_SDR104 ||
1663 ios->timing == MMC_TIMING_UHS_DDR50 ||
1664 ios->timing == MMC_TIMING_UHS_SDR25)
1665 ctrl |= SDHCI_CTRL_HISPD;
1666 else
1667 ctrl &= ~SDHCI_CTRL_HISPD;
1668 }
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001669
Arindam Nathd6d50a12011-05-05 12:18:59 +05301670 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301671 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301672
Russell Kingda91a8f2014-04-25 13:00:12 +01001673 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301674 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301675 /*
1676 * We only need to set Driver Strength if the
1677 * preset value enable is not set.
1678 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001679 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301680 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1681 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1682 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001683 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1684 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301685 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1686 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001687 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1688 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1689 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001690 pr_warn("%s: invalid driver type, default to driver type B\n",
1691 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001692 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1693 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301694
1695 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301696 } else {
1697 /*
1698 * According to SDHC Spec v3.00, if the Preset Value
1699 * Enable in the Host Control 2 register is set, we
1700 * need to reset SD Clock Enable before changing High
1701 * Speed Enable to avoid generating clock gliches.
1702 */
Arindam Nath758535c2011-05-05 12:19:00 +05301703
1704 /* Reset SD Clock Enable */
1705 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1706 clk &= ~SDHCI_CLOCK_CARD_EN;
1707 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1708
1709 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1710
1711 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001712 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301713 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301714
Arindam Nath49c468f2011-05-05 12:19:01 +05301715 /* Reset SD Clock Enable */
1716 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1717 clk &= ~SDHCI_CLOCK_CARD_EN;
1718 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1719
Russell King96d7b782014-04-25 12:59:26 +01001720 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001721 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301722
Kevin Liu52983382013-01-31 11:31:37 +08001723 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1724 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1725 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1726 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1727 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001728 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1729 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001730 u16 preset;
1731
1732 sdhci_enable_preset_value(host, true);
1733 preset = sdhci_get_preset_value(host);
1734 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1735 >> SDHCI_PRESET_DRV_SHIFT;
1736 }
1737
Arindam Nath49c468f2011-05-05 12:19:01 +05301738 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001739 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301740 } else
1741 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301742
Leandro Dorileob8352262007-07-25 23:47:04 +02001743 /*
1744 * Some (ENE) controllers go apeshit on some ios operation,
1745 * signalling timeout and CRC errors even on CMD0. Resetting
1746 * it on each ios seems to solve the problem.
1747 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301748 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001749 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001750
Pierre Ossman5f25a662006-10-04 02:15:39 -07001751 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001752}
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001753EXPORT_SYMBOL_GPL(sdhci_set_ios);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001754
Dong Aishengded97e02016-04-16 01:29:25 +08001755static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001756{
1757 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001758 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001759
1760 if (host->flags & SDHCI_DEVICE_DEAD)
1761 return 0;
1762
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001763 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001764 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001765 return 1;
1766
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001767 /*
1768 * Try slot gpio detect, if defined it take precedence
1769 * over build in controller functionality
1770 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001771 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001772 return !!gpio_cd;
1773
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001774 /* If polling, assume that the card is always present. */
1775 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1776 return 1;
1777
Kevin Liu94144a42013-02-28 17:35:53 +08001778 /* Host native card detect */
1779 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1780}
1781
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001782static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001783{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001784 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001785 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001786
Pierre Ossmand129bce2006-03-24 03:18:17 -08001787 spin_lock_irqsave(&host->lock, flags);
1788
Pierre Ossman1e728592008-04-16 19:13:13 +02001789 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001790 is_readonly = 0;
1791 else if (host->ops->get_ro)
1792 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001793 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001794 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1795 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001796
1797 spin_unlock_irqrestore(&host->lock, flags);
1798
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001799 /* This quirk needs to be replaced by a callback-function later */
1800 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1801 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001802}
1803
Takashi Iwai82b0e232011-04-21 20:26:38 +02001804#define SAMPLE_COUNT 5
1805
Dong Aishengded97e02016-04-16 01:29:25 +08001806static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001807{
Dong Aishengded97e02016-04-16 01:29:25 +08001808 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001809 int i, ro_count;
1810
Takashi Iwai82b0e232011-04-21 20:26:38 +02001811 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001812 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001813
1814 ro_count = 0;
1815 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001816 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001817 if (++ro_count > SAMPLE_COUNT / 2)
1818 return 1;
1819 }
1820 msleep(30);
1821 }
1822 return 0;
1823}
1824
Adrian Hunter20758b62011-08-29 16:42:12 +03001825static void sdhci_hw_reset(struct mmc_host *mmc)
1826{
1827 struct sdhci_host *host = mmc_priv(mmc);
1828
1829 if (host->ops && host->ops->hw_reset)
1830 host->ops->hw_reset(host);
1831}
1832
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001833static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1834{
Russell Kingbe138552014-04-25 12:55:56 +01001835 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001836 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001837 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001838 else
Russell Kingb537f942014-04-25 12:56:01 +01001839 host->ier &= ~SDHCI_INT_CARD_INT;
1840
1841 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1842 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001843 mmiowb();
1844 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001845}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001846
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001847void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001848{
1849 struct sdhci_host *host = mmc_priv(mmc);
1850 unsigned long flags;
1851
Hans de Goede923713b2017-03-26 13:14:45 +02001852 if (enable)
1853 pm_runtime_get_noresume(host->mmc->parent);
1854
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001855 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001856 if (enable)
1857 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1858 else
1859 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1860
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001861 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001862 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001863
1864 if (!enable)
1865 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001866}
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001867EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001868
Hu Zijic376ea92017-03-30 17:22:56 +02001869int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1870 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001871{
Dong Aishengded97e02016-04-16 01:29:25 +08001872 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001873 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001874 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001875
1876 /*
1877 * Signal Voltage Switching is only applicable for Host Controllers
1878 * v3.00 and above.
1879 */
1880 if (host->version < SDHCI_SPEC_300)
1881 return 0;
1882
Philip Rakity6231f3d2012-07-23 15:56:23 -07001883 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001884
Fabio Estevam21f59982013-02-14 10:35:03 -02001885 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001886 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001887 if (!(host->flags & SDHCI_SIGNALING_330))
1888 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001889 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1890 ctrl &= ~SDHCI_CTRL_VDD_180;
1891 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1892
Tim Kryger3a48edc2014-06-13 10:13:56 -07001893 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001894 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001895 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001896 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1897 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001898 return -EIO;
1899 }
1900 }
1901 /* Wait for 5ms */
1902 usleep_range(5000, 5500);
1903
1904 /* 3.3V regulator output should be stable within 5 ms */
1905 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1906 if (!(ctrl & SDHCI_CTRL_VDD_180))
1907 return 0;
1908
Joe Perches66061102014-09-12 14:56:56 -07001909 pr_warn("%s: 3.3V regulator output did not became stable\n",
1910 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001911
1912 return -EAGAIN;
1913 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001914 if (!(host->flags & SDHCI_SIGNALING_180))
1915 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001916 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001917 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001918 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001919 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1920 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001921 return -EIO;
1922 }
1923 }
1924
1925 /*
1926 * Enable 1.8V Signal Enable in the Host Control2
1927 * register
1928 */
1929 ctrl |= SDHCI_CTRL_VDD_180;
1930 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1931
Vincent Yang9d967a62015-01-20 16:05:15 +08001932 /* Some controller need to do more when switching */
1933 if (host->ops->voltage_switch)
1934 host->ops->voltage_switch(host);
1935
Kevin Liu20b92a32012-12-17 19:29:26 +08001936 /* 1.8V regulator output should be stable within 5 ms */
1937 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1938 if (ctrl & SDHCI_CTRL_VDD_180)
1939 return 0;
1940
Joe Perches66061102014-09-12 14:56:56 -07001941 pr_warn("%s: 1.8V regulator output did not became stable\n",
1942 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001943
1944 return -EAGAIN;
1945 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001946 if (!(host->flags & SDHCI_SIGNALING_120))
1947 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001948 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001949 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001950 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001951 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1952 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001953 return -EIO;
1954 }
1955 }
1956 return 0;
1957 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301958 /* No signal voltage switch required */
1959 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001960 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301961}
Hu Zijic376ea92017-03-30 17:22:56 +02001962EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
Arindam Nathf2119df2011-05-05 12:18:57 +05301963
Kevin Liu20b92a32012-12-17 19:29:26 +08001964static int sdhci_card_busy(struct mmc_host *mmc)
1965{
1966 struct sdhci_host *host = mmc_priv(mmc);
1967 u32 present_state;
1968
Adrian Huntere613cc42016-06-23 14:00:58 +03001969 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001970 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001971
Adrian Huntere613cc42016-06-23 14:00:58 +03001972 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001973}
1974
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001975static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1976{
1977 struct sdhci_host *host = mmc_priv(mmc);
1978 unsigned long flags;
1979
1980 spin_lock_irqsave(&host->lock, flags);
1981 host->flags |= SDHCI_HS400_TUNING;
1982 spin_unlock_irqrestore(&host->lock, flags);
1983
1984 return 0;
1985}
1986
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02001987static void sdhci_start_tuning(struct sdhci_host *host)
1988{
1989 u16 ctrl;
1990
1991 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1992 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1993 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1994 ctrl |= SDHCI_CTRL_TUNED_CLK;
1995 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1996
1997 /*
1998 * As per the Host Controller spec v3.00, tuning command
1999 * generates Buffer Read Ready interrupt, so enable that.
2000 *
2001 * Note: The spec clearly says that when tuning sequence
2002 * is being performed, the controller does not generate
2003 * interrupts other than Buffer Read Ready interrupt. But
2004 * to make sure we don't hit a controller bug, we _only_
2005 * enable Buffer Read Ready interrupt here.
2006 */
2007 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2008 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
2009}
2010
2011static void sdhci_end_tuning(struct sdhci_host *host)
2012{
2013 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2014 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2015}
2016
2017static void sdhci_reset_tuning(struct sdhci_host *host)
2018{
2019 u16 ctrl;
2020
2021 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2022 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2023 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2024 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2025}
2026
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002027static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002028{
2029 sdhci_reset_tuning(host);
2030
2031 sdhci_do_reset(host, SDHCI_RESET_CMD);
2032 sdhci_do_reset(host, SDHCI_RESET_DATA);
2033
2034 sdhci_end_tuning(host);
2035
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002036 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002037}
2038
2039/*
2040 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2041 * tuning command does not have a data payload (or rather the hardware does it
2042 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2043 * interrupt setup is different to other commands and there is no timeout
2044 * interrupt so special handling is needed.
2045 */
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002046static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002047{
2048 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002049 struct mmc_command cmd = {};
2050 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002051 unsigned long flags;
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002052 u32 b = host->sdma_boundary;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002053
2054 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002055
2056 cmd.opcode = opcode;
2057 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2058 cmd.mrq = &mrq;
2059
2060 mrq.cmd = &cmd;
2061 /*
2062 * In response to CMD19, the card sends 64 bytes of tuning
2063 * block to the Host Controller. So we set the block size
2064 * to 64 here.
2065 */
Adrian Hunter85336102016-12-02 15:14:26 +02002066 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2067 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002068 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
Adrian Hunter85336102016-12-02 15:14:26 +02002069 else
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002070 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002071
2072 /*
2073 * The tuning block is sent by the card to the host controller.
2074 * So we set the TRNS_READ bit in the Transfer Mode register.
2075 * This also takes care of setting DMA Enable and Multi Block
2076 * Select in the same register to 0.
2077 */
2078 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2079
2080 sdhci_send_command(host, &cmd);
2081
2082 host->cmd = NULL;
2083
2084 sdhci_del_timer(host, &mrq);
2085
2086 host->tuning_done = 0;
2087
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002088 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002089 spin_unlock_irqrestore(&host->lock, flags);
2090
2091 /* Wait for Buffer Read Ready interrupt */
2092 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2093 msecs_to_jiffies(50));
2094
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002095}
2096
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002097static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002098{
2099 int i;
2100
2101 /*
2102 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2103 * of loops reaches 40 times.
2104 */
2105 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2106 u16 ctrl;
2107
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002108 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002109
2110 if (!host->tuning_done) {
2111 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2112 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002113 sdhci_abort_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002114 return;
2115 }
2116
2117 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2118 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2119 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2120 return; /* Success! */
2121 break;
2122 }
2123
Adrian Hunter83b600b2017-04-20 16:14:43 +08002124 /* Spec does not require a delay between tuning cycles */
2125 if (host->tuning_delay > 0)
2126 mdelay(host->tuning_delay);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002127 }
2128
2129 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2130 mmc_hostname(host->mmc));
2131 sdhci_reset_tuning(host);
2132}
2133
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002134int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302135{
Russell King4b6f37d2014-04-25 12:59:36 +01002136 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302137 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002138 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002139 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302140
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002141 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002142
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002143 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2144 tuning_count = host->tuning_count;
2145
Arindam Nathb513ea22011-05-05 12:19:04 +05302146 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002147 * The Host Controller needs tuning in case of SDR104 and DDR50
2148 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2149 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302150 * If the Host Controller supports the HS200 mode then the
2151 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302152 */
Russell King4b6f37d2014-04-25 12:59:36 +01002153 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002154 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002155 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002156 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002157 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002158
Russell King4b6f37d2014-04-25 12:59:36 +01002159 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002160 /*
2161 * Periodic re-tuning for HS400 is not expected to be needed, so
2162 * disable it here.
2163 */
2164 if (hs400_tuning)
2165 tuning_count = 0;
2166 break;
2167
Russell King4b6f37d2014-04-25 12:59:36 +01002168 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002169 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002170 break;
Girish K S069c9f12012-01-06 09:56:39 +05302171
Russell King4b6f37d2014-04-25 12:59:36 +01002172 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002173 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002174 break;
2175 /* FALLTHROUGH */
2176
2177 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002178 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302179 }
2180
Dong Aisheng45251812013-09-13 19:11:30 +08002181 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302182 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002183 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002184 }
2185
Adrian Hunter6b11e702016-12-02 15:14:27 +02002186 host->mmc->retune_period = tuning_count;
2187
Adrian Hunter83b600b2017-04-20 16:14:43 +08002188 if (host->tuning_delay < 0)
2189 host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
2190
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002191 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302192
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002193 __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302194
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002195 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002196out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302197 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002198
Arindam Nathb513ea22011-05-05 12:19:04 +05302199 return err;
2200}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002201EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302202
Kevin Liu52983382013-01-31 11:31:37 +08002203static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302204{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302205 /* Host Controller v3.00 defines preset value registers */
2206 if (host->version < SDHCI_SPEC_300)
2207 return;
2208
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302209 /*
2210 * We only enable or disable Preset Value if they are not already
2211 * enabled or disabled respectively. Otherwise, we bail out.
2212 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002213 if (host->preset_enabled != enable) {
2214 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2215
2216 if (enable)
2217 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2218 else
2219 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2220
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302221 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002222
2223 if (enable)
2224 host->flags |= SDHCI_PV_ENABLED;
2225 else
2226 host->flags &= ~SDHCI_PV_ENABLED;
2227
2228 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302229 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002230}
2231
Haibo Chen348487c2014-12-09 17:04:05 +08002232static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2233 int err)
2234{
2235 struct sdhci_host *host = mmc_priv(mmc);
2236 struct mmc_data *data = mrq->data;
2237
Russell Kingf48f0392016-01-26 13:40:32 +00002238 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002239 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02002240 mmc_get_dma_dir(data));
Russell King771a3dc2016-01-26 13:40:53 +00002241
2242 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002243}
2244
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002245static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002246{
2247 struct sdhci_host *host = mmc_priv(mmc);
2248
Haibo Chend31911b2015-08-25 10:02:11 +08002249 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002250
2251 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002252 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002253}
2254
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002255static inline bool sdhci_has_requests(struct sdhci_host *host)
2256{
2257 return host->cmd || host->data_cmd;
2258}
2259
2260static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2261{
2262 if (host->data_cmd) {
2263 host->data_cmd->error = err;
2264 sdhci_finish_mrq(host, host->data_cmd->mrq);
2265 }
2266
2267 if (host->cmd) {
2268 host->cmd->error = err;
2269 sdhci_finish_mrq(host, host->cmd->mrq);
2270 }
2271}
2272
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002273static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002275 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002276 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002277 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002278
Christian Daudt722e1282013-06-20 14:26:36 -07002279 /* First check if client has provided their own card event */
2280 if (host->ops->card_event)
2281 host->ops->card_event(host);
2282
Adrian Hunterd3940f22016-06-29 16:24:14 +03002283 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002284
Pierre Ossmand129bce2006-03-24 03:18:17 -08002285 spin_lock_irqsave(&host->lock, flags);
2286
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002287 /* Check sdhci_has_requests() first in case we are runtime suspended */
2288 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302289 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002290 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302291 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002292 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002293
Russell King03231f92014-04-25 12:57:12 +01002294 sdhci_do_reset(host, SDHCI_RESET_CMD);
2295 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002296
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002297 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002298 }
2299
2300 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002301}
2302
2303static const struct mmc_host_ops sdhci_ops = {
2304 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002305 .post_req = sdhci_post_req,
2306 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002307 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002308 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002309 .get_ro = sdhci_get_ro,
2310 .hw_reset = sdhci_hw_reset,
2311 .enable_sdio_irq = sdhci_enable_sdio_irq,
2312 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002313 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002314 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002315 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002316 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002317};
2318
2319/*****************************************************************************\
2320 * *
2321 * Tasklets *
2322 * *
2323\*****************************************************************************/
2324
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002325static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002326{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327 unsigned long flags;
2328 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002329 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002330
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002331 spin_lock_irqsave(&host->lock, flags);
2332
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002333 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2334 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002335 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002336 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002337 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002339 if (!mrq) {
2340 spin_unlock_irqrestore(&host->lock, flags);
2341 return true;
2342 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002344 sdhci_del_timer(host, mrq);
2345
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346 /*
Russell King054cedf2016-01-26 13:40:42 +00002347 * Always unmap the data buffers if they were mapped by
2348 * sdhci_prepare_data() whenever we finish with a request.
2349 * This avoids leaking DMA mappings on error.
2350 */
2351 if (host->flags & SDHCI_REQ_USE_DMA) {
2352 struct mmc_data *data = mrq->data;
2353
2354 if (data && data->host_cookie == COOKIE_MAPPED) {
2355 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02002356 mmc_get_dma_dir(data));
Russell King054cedf2016-01-26 13:40:42 +00002357 data->host_cookie = COOKIE_UNMAPPED;
2358 }
2359 }
2360
2361 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002362 * The controller needs a reset of internal state machines
2363 * upon error conditions.
2364 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002365 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002366 /*
2367 * Do not finish until command and data lines are available for
2368 * reset. Note there can only be one other mrq, so it cannot
2369 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2370 * would both be null.
2371 */
2372 if (host->cmd || host->data_cmd) {
2373 spin_unlock_irqrestore(&host->lock, flags);
2374 return true;
2375 }
2376
Pierre Ossman645289d2006-06-30 02:22:33 -07002377 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002378 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002379 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002380 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002381
2382 /* Spec says we should do both at the same time, but Ricoh
2383 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002384 sdhci_do_reset(host, SDHCI_RESET_CMD);
2385 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002386
2387 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002388 }
2389
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002390 if (!sdhci_has_requests(host))
2391 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002392
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002393 host->mrqs_done[i] = NULL;
2394
Pierre Ossman5f25a662006-10-04 02:15:39 -07002395 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002396 spin_unlock_irqrestore(&host->lock, flags);
2397
2398 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002399
2400 return false;
2401}
2402
2403static void sdhci_tasklet_finish(unsigned long param)
2404{
2405 struct sdhci_host *host = (struct sdhci_host *)param;
2406
2407 while (!sdhci_request_done(host))
2408 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002409}
2410
Kees Cook2ee4f622017-10-24 08:03:45 -07002411static void sdhci_timeout_timer(struct timer_list *t)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002412{
2413 struct sdhci_host *host;
2414 unsigned long flags;
2415
Kees Cook2ee4f622017-10-24 08:03:45 -07002416 host = from_timer(host, t, timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417
2418 spin_lock_irqsave(&host->lock, flags);
2419
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002420 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2421 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2422 mmc_hostname(host->mmc));
2423 sdhci_dumpregs(host);
2424
2425 host->cmd->error = -ETIMEDOUT;
2426 sdhci_finish_mrq(host, host->cmd->mrq);
2427 }
2428
2429 mmiowb();
2430 spin_unlock_irqrestore(&host->lock, flags);
2431}
2432
Kees Cook2ee4f622017-10-24 08:03:45 -07002433static void sdhci_timeout_data_timer(struct timer_list *t)
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002434{
2435 struct sdhci_host *host;
2436 unsigned long flags;
2437
Kees Cook2ee4f622017-10-24 08:03:45 -07002438 host = from_timer(host, t, data_timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002439
2440 spin_lock_irqsave(&host->lock, flags);
2441
2442 if (host->data || host->data_cmd ||
2443 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002444 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2445 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002446 sdhci_dumpregs(host);
2447
2448 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002449 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002451 } else if (host->data_cmd) {
2452 host->data_cmd->error = -ETIMEDOUT;
2453 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002454 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002455 host->cmd->error = -ETIMEDOUT;
2456 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002457 }
2458 }
2459
Pierre Ossman5f25a662006-10-04 02:15:39 -07002460 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002461 spin_unlock_irqrestore(&host->lock, flags);
2462}
2463
2464/*****************************************************************************\
2465 * *
2466 * Interrupt handling *
2467 * *
2468\*****************************************************************************/
2469
Adrian Hunterfc605f12016-10-05 12:11:21 +03002470static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002471{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002472 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002473 /*
2474 * SDHCI recovers from errors by resetting the cmd and data
2475 * circuits. Until that is done, there very well might be more
2476 * interrupts, so ignore them in that case.
2477 */
2478 if (host->pending_reset)
2479 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002480 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2481 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002482 sdhci_dumpregs(host);
2483 return;
2484 }
2485
Russell Kingec014cb2016-01-26 13:39:39 +00002486 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2487 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2488 if (intmask & SDHCI_INT_TIMEOUT)
2489 host->cmd->error = -ETIMEDOUT;
2490 else
2491 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492
Russell King71fcbda2016-01-26 13:39:45 +00002493 /*
2494 * If this command initiates a data phase and a response
2495 * CRC error is signalled, the card can start transferring
2496 * data - the card may have received the command without
2497 * error. We must not terminate the mmc_request early.
2498 *
2499 * If the card did not receive the command or returned an
2500 * error which prevented it sending data, the data phase
2501 * will time out.
2502 */
2503 if (host->cmd->data &&
2504 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2505 SDHCI_INT_CRC) {
2506 host->cmd = NULL;
2507 return;
2508 }
2509
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002510 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002511 return;
2512 }
2513
Pierre Ossmane8095172008-07-25 01:09:08 +02002514 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002515 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002516}
2517
Adrian Hunter08621b12014-11-04 12:42:38 +02002518static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002519{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002520 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002521
2522 sdhci_dumpregs(host);
2523
2524 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002525 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002526
Adrian Huntere57a5f62014-11-04 12:42:46 +02002527 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002528 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2529 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002530 le32_to_cpu(dma_desc->addr_lo),
2531 le16_to_cpu(dma_desc->len),
2532 le16_to_cpu(dma_desc->cmd));
2533 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002534 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2535 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002536 le16_to_cpu(dma_desc->len),
2537 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002538
Adrian Hunter76fe3792014-11-04 12:42:42 +02002539 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002540
Adrian Hunter05452302014-11-04 12:42:45 +02002541 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002542 break;
2543 }
2544}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002545
Pierre Ossmand129bce2006-03-24 03:18:17 -08002546static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2547{
Girish K S069c9f12012-01-06 09:56:39 +05302548 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549
Arindam Nathb513ea22011-05-05 12:19:04 +05302550 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2551 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302552 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2553 if (command == MMC_SEND_TUNING_BLOCK ||
2554 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302555 host->tuning_done = 1;
2556 wake_up(&host->buf_ready_int);
2557 return;
2558 }
2559 }
2560
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002562 struct mmc_command *data_cmd = host->data_cmd;
2563
Pierre Ossmand129bce2006-03-24 03:18:17 -08002564 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002565 * The "data complete" interrupt is also used to
2566 * indicate that a busy state has ended. See comment
2567 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002568 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002569 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002570 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002571 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002572 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002573 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002574 return;
2575 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002576 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002577 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002578 /*
2579 * Some cards handle busy-end interrupt
2580 * before the command completed, so make
2581 * sure we do things in the proper order.
2582 */
Adrian Hunterea968022016-06-29 16:24:24 +03002583 if (host->cmd == data_cmd)
2584 return;
2585
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002586 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002587 return;
2588 }
2589 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002590
Adrian Huntered1563d2016-06-29 16:24:29 +03002591 /*
2592 * SDHCI recovers from errors by resetting the cmd and data
2593 * circuits. Until that is done, there very well might be more
2594 * interrupts, so ignore them in that case.
2595 */
2596 if (host->pending_reset)
2597 return;
2598
Marek Vasut2e4456f2015-11-18 10:47:02 +01002599 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2600 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601 sdhci_dumpregs(host);
2602
2603 return;
2604 }
2605
2606 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002607 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002608 else if (intmask & SDHCI_INT_DATA_END_BIT)
2609 host->data->error = -EILSEQ;
2610 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2611 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2612 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002613 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002614 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302615 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002616 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002617 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002618 if (host->ops->adma_workaround)
2619 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002620 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002621
Pierre Ossman17b04292007-07-22 22:18:46 +02002622 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623 sdhci_finish_data(host);
2624 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002625 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626 sdhci_transfer_pio(host);
2627
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002628 /*
2629 * We currently don't do anything fancy with DMA
2630 * boundaries, but as we can't disable the feature
2631 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002632 *
2633 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2634 * should return a valid address to continue from, but as
2635 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002636 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002637 if (intmask & SDHCI_INT_DMA_END) {
2638 u32 dmastart, dmanow;
2639 dmastart = sg_dma_address(host->data->sg);
2640 dmanow = dmastart + host->data->bytes_xfered;
2641 /*
2642 * Force update to the next DMA block boundary.
2643 */
2644 dmanow = (dmanow &
2645 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2646 SDHCI_DEFAULT_BOUNDARY_SIZE;
2647 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002648 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2649 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002650 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2651 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002652
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002653 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002654 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002655 /*
2656 * Data managed to finish before the
2657 * command completed. Make sure we do
2658 * things in the proper order.
2659 */
2660 host->data_early = 1;
2661 } else {
2662 sdhci_finish_data(host);
2663 }
2664 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002665 }
2666}
2667
David Howells7d12e782006-10-05 14:55:46 +01002668static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002669{
Russell King781e9892014-04-25 12:55:46 +01002670 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002671 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002672 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002673 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002674
2675 spin_lock(&host->lock);
2676
Russell Kingbe138552014-04-25 12:55:56 +01002677 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002678 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002679 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002680 }
2681
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002682 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002683 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002684 result = IRQ_NONE;
2685 goto out;
2686 }
2687
Russell King41005002014-04-25 12:55:36 +01002688 do {
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002689 DBG("IRQ status 0x%08x\n", intmask);
2690
2691 if (host->ops->irq) {
2692 intmask = host->ops->irq(host, intmask);
2693 if (!intmask)
2694 goto cont;
2695 }
2696
Russell King41005002014-04-25 12:55:36 +01002697 /* Clear selected interrupts. */
2698 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2699 SDHCI_INT_BUS_POWER);
2700 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002701
Russell King41005002014-04-25 12:55:36 +01002702 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2703 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2704 SDHCI_CARD_PRESENT;
2705
2706 /*
2707 * There is a observation on i.mx esdhc. INSERT
2708 * bit will be immediately set again when it gets
2709 * cleared, if a card is inserted. We have to mask
2710 * the irq to prevent interrupt storm which will
2711 * freeze the system. And the REMOVE gets the
2712 * same situation.
2713 *
2714 * More testing are needed here to ensure it works
2715 * for other platforms though.
2716 */
Russell Kingb537f942014-04-25 12:56:01 +01002717 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2718 SDHCI_INT_CARD_REMOVE);
2719 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2720 SDHCI_INT_CARD_INSERT;
2721 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2722 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002723
2724 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2725 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002726
2727 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2728 SDHCI_INT_CARD_REMOVE);
2729 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002730 }
2731
2732 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002733 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002734
2735 if (intmask & SDHCI_INT_DATA_MASK)
2736 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2737
2738 if (intmask & SDHCI_INT_BUS_POWER)
2739 pr_err("%s: Card is consuming too much power!\n",
2740 mmc_hostname(host->mmc));
2741
Dong Aishengf37b20e2016-07-12 15:46:17 +08002742 if (intmask & SDHCI_INT_RETUNE)
2743 mmc_retune_needed(host->mmc);
2744
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002745 if ((intmask & SDHCI_INT_CARD_INT) &&
2746 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002747 sdhci_enable_sdio_irq_nolock(host, false);
2748 host->thread_isr |= SDHCI_INT_CARD_INT;
2749 result = IRQ_WAKE_THREAD;
2750 }
Russell King41005002014-04-25 12:55:36 +01002751
2752 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2753 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2754 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002755 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002756
2757 if (intmask) {
2758 unexpected |= intmask;
2759 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2760 }
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002761cont:
Russell King781e9892014-04-25 12:55:46 +01002762 if (result == IRQ_NONE)
2763 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002764
2765 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002766 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767out:
2768 spin_unlock(&host->lock);
2769
Alexander Stein6379b232012-03-14 09:52:10 +01002770 if (unexpected) {
2771 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2772 mmc_hostname(host->mmc), unexpected);
2773 sdhci_dumpregs(host);
2774 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002775
Pierre Ossmand129bce2006-03-24 03:18:17 -08002776 return result;
2777}
2778
Russell King781e9892014-04-25 12:55:46 +01002779static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2780{
2781 struct sdhci_host *host = dev_id;
2782 unsigned long flags;
2783 u32 isr;
2784
2785 spin_lock_irqsave(&host->lock, flags);
2786 isr = host->thread_isr;
2787 host->thread_isr = 0;
2788 spin_unlock_irqrestore(&host->lock, flags);
2789
Russell King3560db82014-04-25 12:55:51 +01002790 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002791 struct mmc_host *mmc = host->mmc;
2792
2793 mmc->ops->card_event(mmc);
2794 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002795 }
2796
Russell King781e9892014-04-25 12:55:46 +01002797 if (isr & SDHCI_INT_CARD_INT) {
2798 sdio_run_irqs(host->mmc);
2799
2800 spin_lock_irqsave(&host->lock, flags);
2801 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2802 sdhci_enable_sdio_irq_nolock(host, true);
2803 spin_unlock_irqrestore(&host->lock, flags);
2804 }
2805
2806 return isr ? IRQ_HANDLED : IRQ_NONE;
2807}
2808
Pierre Ossmand129bce2006-03-24 03:18:17 -08002809/*****************************************************************************\
2810 * *
2811 * Suspend/resume *
2812 * *
2813\*****************************************************************************/
2814
2815#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002816/*
2817 * To enable wakeup events, the corresponding events have to be enabled in
2818 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2819 * Table' in the SD Host Controller Standard Specification.
2820 * It is useless to restore SDHCI_INT_ENABLE state in
2821 * sdhci_disable_irq_wakeups() since it will be set by
2822 * sdhci_enable_card_detection() or sdhci_init().
2823 */
Kevin Liuad080d72013-01-05 17:21:33 +08002824void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2825{
2826 u8 val;
2827 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2828 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002829 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2830 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002831
2832 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2833 val |= mask ;
2834 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002835 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002836 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002837 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2838 }
Kevin Liuad080d72013-01-05 17:21:33 +08002839 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002840 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002841}
2842EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2843
Fabio Estevam0b10f472014-08-30 14:53:13 -03002844static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002845{
2846 u8 val;
2847 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2848 | SDHCI_WAKE_ON_INT;
2849
2850 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2851 val &= ~mask;
2852 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2853}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854
Manuel Lauss29495aa2011-11-03 11:09:45 +01002855int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002857 sdhci_disable_card_detection(host);
2858
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002859 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302860
Kevin Liuad080d72013-01-05 17:21:33 +08002861 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002862 host->ier = 0;
2863 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2864 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002865 free_irq(host->irq, host);
2866 } else {
2867 sdhci_enable_irq_wakeups(host);
2868 enable_irq_wake(host->irq);
2869 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002870 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871}
2872
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002873EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002874
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002875int sdhci_resume_host(struct sdhci_host *host)
2876{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002877 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002878 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002879
Richard Röjforsa13abc72009-09-22 16:45:30 -07002880 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002881 if (host->ops->enable_dma)
2882 host->ops->enable_dma(host);
2883 }
2884
Adrian Hunter6308d292012-02-07 14:48:54 +02002885 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2886 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2887 /* Card keeps power but host controller does not */
2888 sdhci_init(host, 0);
2889 host->pwr = 0;
2890 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002891 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002892 } else {
2893 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2894 mmiowb();
2895 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002896
Haibo Chen14a7b41642015-09-15 18:32:58 +08002897 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2898 ret = request_threaded_irq(host->irq, sdhci_irq,
2899 sdhci_thread_irq, IRQF_SHARED,
2900 mmc_hostname(host->mmc), host);
2901 if (ret)
2902 return ret;
2903 } else {
2904 sdhci_disable_irq_wakeups(host);
2905 disable_irq_wake(host->irq);
2906 }
2907
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002908 sdhci_enable_card_detection(host);
2909
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002910 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002911}
2912
2913EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002914
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002915int sdhci_runtime_suspend_host(struct sdhci_host *host)
2916{
2917 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002918
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002919 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002920
2921 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002922 host->ier &= SDHCI_INT_CARD_INT;
2923 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2924 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002925 spin_unlock_irqrestore(&host->lock, flags);
2926
Russell King781e9892014-04-25 12:55:46 +01002927 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002928
2929 spin_lock_irqsave(&host->lock, flags);
2930 host->runtime_suspended = true;
2931 spin_unlock_irqrestore(&host->lock, flags);
2932
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002933 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002934}
2935EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2936
2937int sdhci_runtime_resume_host(struct sdhci_host *host)
2938{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002939 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002940 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002941 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002942
2943 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2944 if (host->ops->enable_dma)
2945 host->ops->enable_dma(host);
2946 }
2947
2948 sdhci_init(host, 0);
2949
Zhoujie Wu70bc85a2017-08-03 12:28:40 -07002950 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
2951 mmc->ios.power_mode != MMC_POWER_OFF) {
Adrian Hunter84ec0482016-12-19 15:33:11 +02002952 /* Force clock and power re-program */
2953 host->pwr = 0;
2954 host->clock = 0;
2955 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2956 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002957
Adrian Hunter84ec0482016-12-19 15:33:11 +02002958 if ((host_flags & SDHCI_PV_ENABLED) &&
2959 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2960 spin_lock_irqsave(&host->lock, flags);
2961 sdhci_enable_preset_value(host, true);
2962 spin_unlock_irqrestore(&host->lock, flags);
2963 }
2964
2965 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2966 mmc->ops->hs400_enhanced_strobe)
2967 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002968 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002969
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002970 spin_lock_irqsave(&host->lock, flags);
2971
2972 host->runtime_suspended = false;
2973
2974 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002975 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002976 sdhci_enable_sdio_irq_nolock(host, true);
2977
2978 /* Enable Card Detection */
2979 sdhci_enable_card_detection(host);
2980
2981 spin_unlock_irqrestore(&host->lock, flags);
2982
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002983 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002984}
2985EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2986
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002987#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002988
Pierre Ossmand129bce2006-03-24 03:18:17 -08002989/*****************************************************************************\
2990 * *
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002991 * Command Queue Engine (CQE) helpers *
2992 * *
2993\*****************************************************************************/
2994
2995void sdhci_cqe_enable(struct mmc_host *mmc)
2996{
2997 struct sdhci_host *host = mmc_priv(mmc);
2998 unsigned long flags;
2999 u8 ctrl;
3000
3001 spin_lock_irqsave(&host->lock, flags);
3002
3003 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
3004 ctrl &= ~SDHCI_CTRL_DMA_MASK;
3005 if (host->flags & SDHCI_USE_64_BIT_DMA)
3006 ctrl |= SDHCI_CTRL_ADMA64;
3007 else
3008 ctrl |= SDHCI_CTRL_ADMA32;
3009 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
3010
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003011 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003012 SDHCI_BLOCK_SIZE);
3013
3014 /* Set maximum timeout */
3015 sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
3016
3017 host->ier = host->cqe_ier;
3018
3019 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3020 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
3021
3022 host->cqe_on = true;
3023
3024 pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
3025 mmc_hostname(mmc), host->ier,
3026 sdhci_readl(host, SDHCI_INT_STATUS));
3027
3028 mmiowb();
3029 spin_unlock_irqrestore(&host->lock, flags);
3030}
3031EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
3032
3033void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
3034{
3035 struct sdhci_host *host = mmc_priv(mmc);
3036 unsigned long flags;
3037
3038 spin_lock_irqsave(&host->lock, flags);
3039
3040 sdhci_set_default_irqs(host);
3041
3042 host->cqe_on = false;
3043
3044 if (recovery) {
3045 sdhci_do_reset(host, SDHCI_RESET_CMD);
3046 sdhci_do_reset(host, SDHCI_RESET_DATA);
3047 }
3048
3049 pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
3050 mmc_hostname(mmc), host->ier,
3051 sdhci_readl(host, SDHCI_INT_STATUS));
3052
3053 mmiowb();
3054 spin_unlock_irqrestore(&host->lock, flags);
3055}
3056EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
3057
3058bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
3059 int *data_error)
3060{
3061 u32 mask;
3062
3063 if (!host->cqe_on)
3064 return false;
3065
3066 if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
3067 *cmd_error = -EILSEQ;
3068 else if (intmask & SDHCI_INT_TIMEOUT)
3069 *cmd_error = -ETIMEDOUT;
3070 else
3071 *cmd_error = 0;
3072
3073 if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3074 *data_error = -EILSEQ;
3075 else if (intmask & SDHCI_INT_DATA_TIMEOUT)
3076 *data_error = -ETIMEDOUT;
3077 else if (intmask & SDHCI_INT_ADMA_ERROR)
3078 *data_error = -EIO;
3079 else
3080 *data_error = 0;
3081
3082 /* Clear selected interrupts. */
3083 mask = intmask & host->cqe_ier;
3084 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3085
3086 if (intmask & SDHCI_INT_BUS_POWER)
3087 pr_err("%s: Card is consuming too much power!\n",
3088 mmc_hostname(host->mmc));
3089
3090 intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
3091 if (intmask) {
3092 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3093 pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
3094 mmc_hostname(host->mmc), intmask);
3095 sdhci_dumpregs(host);
3096 }
3097
3098 return true;
3099}
3100EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
3101
3102/*****************************************************************************\
3103 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003104 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003105 * *
3106\*****************************************************************************/
3107
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003108struct sdhci_host *sdhci_alloc_host(struct device *dev,
3109 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003110{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003111 struct mmc_host *mmc;
3112 struct sdhci_host *host;
3113
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003114 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003115
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003116 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003117 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003118 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003119
3120 host = mmc_priv(mmc);
3121 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003122 host->mmc_host_ops = sdhci_ops;
3123 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003124
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003125 host->flags = SDHCI_SIGNALING_330;
3126
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003127 host->cqe_ier = SDHCI_CQE_INT_MASK;
3128 host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
3129
Adrian Hunter83b600b2017-04-20 16:14:43 +08003130 host->tuning_delay = -1;
3131
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003132 host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
3133
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003134 return host;
3135}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003136
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003137EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003138
Alexandre Courbot7b913692016-03-07 11:07:55 +09003139static int sdhci_set_dma_mask(struct sdhci_host *host)
3140{
3141 struct mmc_host *mmc = host->mmc;
3142 struct device *dev = mmc_dev(mmc);
3143 int ret = -EINVAL;
3144
3145 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3146 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3147
3148 /* Try 64-bit mask if hardware is capable of it */
3149 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3150 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3151 if (ret) {
3152 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3153 mmc_hostname(mmc));
3154 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3155 }
3156 }
3157
3158 /* 32-bit mask as default & fallback */
3159 if (ret) {
3160 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3161 if (ret)
3162 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3163 mmc_hostname(mmc));
3164 }
3165
3166 return ret;
3167}
3168
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003169void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3170{
3171 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003172 u64 dt_caps_mask = 0;
3173 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003174
3175 if (host->read_caps)
3176 return;
3177
3178 host->read_caps = true;
3179
3180 if (debug_quirks)
3181 host->quirks = debug_quirks;
3182
3183 if (debug_quirks2)
3184 host->quirks2 = debug_quirks2;
3185
3186 sdhci_do_reset(host, SDHCI_RESET_ALL);
3187
Zach Brown92e0c442016-11-02 10:26:16 -05003188 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3189 "sdhci-caps-mask", &dt_caps_mask);
3190 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3191 "sdhci-caps", &dt_caps);
3192
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003193 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3194 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3195
3196 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3197 return;
3198
Zach Brown92e0c442016-11-02 10:26:16 -05003199 if (caps) {
3200 host->caps = *caps;
3201 } else {
3202 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3203 host->caps &= ~lower_32_bits(dt_caps_mask);
3204 host->caps |= lower_32_bits(dt_caps);
3205 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003206
3207 if (host->version < SDHCI_SPEC_300)
3208 return;
3209
Zach Brown92e0c442016-11-02 10:26:16 -05003210 if (caps1) {
3211 host->caps1 = *caps1;
3212 } else {
3213 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3214 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3215 host->caps1 |= upper_32_bits(dt_caps);
3216 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003217}
3218EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3219
Adrian Hunter52f53362016-06-29 16:24:15 +03003220int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003221{
3222 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303223 u32 max_current_caps;
3224 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003225 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003226 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003227 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003228
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003229 WARN_ON(host == NULL);
3230 if (host == NULL)
3231 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003232
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003233 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003234
Jon Hunterefba1422016-07-12 14:53:36 +01003235 /*
3236 * If there are external regulators, get them. Note this must be done
3237 * early before resetting the host and reading the capabilities so that
3238 * the host can take the appropriate action if regulators are not
3239 * available.
3240 */
3241 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang2a633032017-10-14 21:17:18 +02003242 if (ret)
Jon Hunterefba1422016-07-12 14:53:36 +01003243 return ret;
3244
Shawn Lin06ebc602017-07-19 15:55:49 +08003245 DBG("Version: 0x%08x | Present: 0x%08x\n",
3246 sdhci_readw(host, SDHCI_HOST_VERSION),
3247 sdhci_readl(host, SDHCI_PRESENT_STATE));
3248 DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
3249 sdhci_readl(host, SDHCI_CAPABILITIES),
3250 sdhci_readl(host, SDHCI_CAPABILITIES_1));
3251
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003252 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003253
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003254 override_timeout_clk = host->timeout_clk;
3255
Zhangfei Gao85105c52010-08-06 07:10:01 +08003256 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003257 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3258 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003259 }
3260
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003261 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003262 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003263 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003264 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003265 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003266 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003267
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003268 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003269 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003270 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003271 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003272 }
3273
Arindam Nathf2119df2011-05-05 12:18:57 +05303274 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003275 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003276 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003277
3278 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3279 (host->flags & SDHCI_USE_ADMA)) {
3280 DBG("Disabling ADMA as it is marked broken\n");
3281 host->flags &= ~SDHCI_USE_ADMA;
3282 }
3283
Adrian Huntere57a5f62014-11-04 12:42:46 +02003284 /*
3285 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3286 * and *must* do 64-bit DMA. A driver has the opportunity to change
3287 * that during the first call to ->enable_dma(). Similarly
3288 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3289 * implement.
3290 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003291 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003292 host->flags |= SDHCI_USE_64_BIT_DMA;
3293
Richard Röjforsa13abc72009-09-22 16:45:30 -07003294 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003295 ret = sdhci_set_dma_mask(host);
3296
3297 if (!ret && host->ops->enable_dma)
3298 ret = host->ops->enable_dma(host);
3299
3300 if (ret) {
3301 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3302 mmc_hostname(mmc));
3303 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3304
3305 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003306 }
3307 }
3308
Adrian Huntere57a5f62014-11-04 12:42:46 +02003309 /* SDMA does not support 64-bit DMA */
3310 if (host->flags & SDHCI_USE_64_BIT_DMA)
3311 host->flags &= ~SDHCI_USE_SDMA;
3312
Pierre Ossman2134a922008-06-28 18:28:51 +02003313 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003314 dma_addr_t dma;
3315 void *buf;
3316
Pierre Ossman2134a922008-06-28 18:28:51 +02003317 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003318 * The DMA descriptor table size is calculated as the maximum
3319 * number of segments times 2, to allow for an alignment
3320 * descriptor for each segment, plus 1 for a nop end descriptor,
3321 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003322 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003323 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3324 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3325 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003326 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003327 } else {
3328 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3329 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003330 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003331 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003332
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003333 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003334 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3335 host->adma_table_sz, &dma, GFP_KERNEL);
3336 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003337 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003338 mmc_hostname(mmc));
3339 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003340 } else if ((dma + host->align_buffer_sz) &
3341 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003342 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3343 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003344 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003345 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3346 host->adma_table_sz, buf, dma);
3347 } else {
3348 host->align_buffer = buf;
3349 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003350
Russell Kinge66e61c2016-01-26 13:39:55 +00003351 host->adma_table = buf + host->align_buffer_sz;
3352 host->adma_addr = dma + host->align_buffer_sz;
3353 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003354 }
3355
Pierre Ossman76591502008-07-21 00:32:11 +02003356 /*
3357 * If we use DMA, then it's up to the caller to set the DMA
3358 * mask, but PIO does not need the hw shim so we set a new
3359 * mask here in that case.
3360 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003361 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003362 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003363 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003364 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003365
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003366 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003367 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003368 >> SDHCI_CLOCK_BASE_SHIFT;
3369 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003370 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003371 >> SDHCI_CLOCK_BASE_SHIFT;
3372
Pierre Ossmand129bce2006-03-24 03:18:17 -08003373 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003374 if (host->max_clk == 0 || host->quirks &
3375 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003376 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003377 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3378 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003379 ret = -ENODEV;
3380 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003381 }
3382 host->max_clk = host->ops->get_max_clock(host);
3383 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003384
3385 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303386 * In case of Host Controller v3.00, find out whether clock
3387 * multiplier is supported.
3388 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003389 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303390 SDHCI_CLOCK_MUL_SHIFT;
3391
3392 /*
3393 * In case the value in Clock Multiplier is 0, then programmable
3394 * clock mode is not supported, otherwise the actual clock
3395 * multiplier is one more than the value of Clock Multiplier
3396 * in the Capabilities Register.
3397 */
3398 if (host->clk_mul)
3399 host->clk_mul += 1;
3400
3401 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003402 * Set host parameters.
3403 */
Dong Aisheng59241752015-07-22 20:53:07 +08003404 max_clk = host->max_clk;
3405
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003406 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003407 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303408 else if (host->version >= SDHCI_SPEC_300) {
3409 if (host->clk_mul) {
3410 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003411 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303412 } else
3413 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3414 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003415 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003416
Adrian Hunterd310ae42016-04-12 14:25:07 +03003417 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003418 mmc->f_max = max_clk;
3419
Aisheng Dong28aab052014-08-27 15:26:31 +08003420 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003421 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003422 SDHCI_TIMEOUT_CLK_SHIFT;
Shawn Lin8cc35282017-03-24 15:50:12 +08003423
3424 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
3425 host->timeout_clk *= 1000;
3426
Aisheng Dong28aab052014-08-27 15:26:31 +08003427 if (host->timeout_clk == 0) {
Shawn Lin8cc35282017-03-24 15:50:12 +08003428 if (!host->ops->get_timeout_clock) {
Aisheng Dong28aab052014-08-27 15:26:31 +08003429 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3430 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003431 ret = -ENODEV;
3432 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003433 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003434
Shawn Lin8cc35282017-03-24 15:50:12 +08003435 host->timeout_clk =
3436 DIV_ROUND_UP(host->ops->get_timeout_clock(host),
3437 1000);
3438 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003439
Adrian Hunter99513622016-03-07 13:33:55 +02003440 if (override_timeout_clk)
3441 host->timeout_clk = override_timeout_clk;
3442
Aisheng Dong28aab052014-08-27 15:26:31 +08003443 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003444 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003445 mmc->max_busy_timeout /= host->timeout_clk;
3446 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003447
Andrei Warkentine89d4562011-05-23 15:06:37 -05003448 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003449 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003450
3451 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3452 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003453
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003454 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003455 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003456 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003457 !(host->flags & SDHCI_USE_SDMA)) &&
3458 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003459 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003460 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003461 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003462 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003463 }
3464
Philip Rakity15ec4462010-11-19 16:48:39 -05003465 /*
3466 * A controller may support 8-bit width, but the board itself
3467 * might not have the pins brought out. Boards that support
3468 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3469 * their platform code before calling sdhci_add_host(), and we
3470 * won't assume 8-bit width for hosts without that CAP.
3471 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003472 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003473 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003474
Jerry Huang63ef5d82012-10-25 13:47:19 +08003475 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3476 mmc->caps &= ~MMC_CAP_CMD23;
3477
Adrian Hunter28da3582016-06-29 16:24:17 +03003478 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003479 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003480
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003481 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003482 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003483 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003484 mmc->caps |= MMC_CAP_NEEDS_POLL;
3485
Philip Rakity6231f3d2012-07-23 15:56:23 -07003486 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003487 if (!IS_ERR(mmc->supply.vqmmc)) {
3488 ret = regulator_enable(mmc->supply.vqmmc);
3489 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3490 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003491 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3492 SDHCI_SUPPORT_SDR50 |
3493 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003494 if (ret) {
3495 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3496 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003497 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003498 }
Kevin Liu8363c372012-11-17 17:55:51 -05003499 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003500
Adrian Hunter28da3582016-06-29 16:24:17 +03003501 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3502 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3503 SDHCI_SUPPORT_DDR50);
3504 }
Daniel Drake6a661802012-11-25 13:01:19 -05003505
Al Cooper4188bba2012-03-16 15:54:17 -04003506 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003507 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3508 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303509 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3510
3511 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003512 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303513 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003514 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3515 * field can be promoted to support HS200.
3516 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003517 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003518 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003519 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303520 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003521 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303522
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003523 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003524 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003525 mmc->caps2 |= MMC_CAP2_HS400;
3526
Adrian Hunter549c0b12014-11-06 15:19:05 +02003527 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3528 (IS_ERR(mmc->supply.vqmmc) ||
3529 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3530 1300000)))
3531 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3532
Adrian Hunter28da3582016-06-29 16:24:17 +03003533 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3534 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303535 mmc->caps |= MMC_CAP_UHS_DDR50;
3536
Girish K S069c9f12012-01-06 09:56:39 +05303537 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003538 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303539 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3540
Arindam Nathd6d50a12011-05-05 12:18:59 +05303541 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003542 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303543 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003544 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303545 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003546 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303547 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3548
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303549 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003550 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3551 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303552
3553 /*
3554 * In case Re-tuning Timer is not disabled, the actual value of
3555 * re-tuning timer will be 2 ^ (n - 1).
3556 */
3557 if (host->tuning_count)
3558 host->tuning_count = 1 << (host->tuning_count - 1);
3559
3560 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003561 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303562 SDHCI_RETUNING_MODE_SHIFT;
3563
Takashi Iwai8f230f42010-12-08 10:04:30 +01003564 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003565
Arindam Nathf2119df2011-05-05 12:18:57 +05303566 /*
3567 * According to SD Host Controller spec v3.00, if the Host System
3568 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3569 * the value is meaningful only if Voltage Support in the Capabilities
3570 * register is set. The actual current value is 4 times the register
3571 * value.
3572 */
3573 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003574 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003575 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003576 if (curr > 0) {
3577
3578 /* convert to SDHCI_MAX_CURRENT format */
3579 curr = curr/1000; /* convert to mA */
3580 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3581
3582 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3583 max_current_caps =
3584 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3585 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3586 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3587 }
3588 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303589
Adrian Hunter28da3582016-06-29 16:24:17 +03003590 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003591 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303592
Aaron Lu55c46652012-07-04 13:31:48 +08003593 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303594 SDHCI_MAX_CURRENT_330_MASK) >>
3595 SDHCI_MAX_CURRENT_330_SHIFT) *
3596 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303597 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003598 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003599 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303600
Aaron Lu55c46652012-07-04 13:31:48 +08003601 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303602 SDHCI_MAX_CURRENT_300_MASK) >>
3603 SDHCI_MAX_CURRENT_300_SHIFT) *
3604 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303605 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003606 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003607 ocr_avail |= MMC_VDD_165_195;
3608
Aaron Lu55c46652012-07-04 13:31:48 +08003609 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303610 SDHCI_MAX_CURRENT_180_MASK) >>
3611 SDHCI_MAX_CURRENT_180_SHIFT) *
3612 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303613 }
3614
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003615 /* If OCR set by host, use it instead. */
3616 if (host->ocr_mask)
3617 ocr_avail = host->ocr_mask;
3618
3619 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003620 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003621 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003622
Takashi Iwai8f230f42010-12-08 10:04:30 +01003623 mmc->ocr_avail = ocr_avail;
3624 mmc->ocr_avail_sdio = ocr_avail;
3625 if (host->ocr_avail_sdio)
3626 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3627 mmc->ocr_avail_sd = ocr_avail;
3628 if (host->ocr_avail_sd)
3629 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3630 else /* normal SD controllers don't support 1.8V */
3631 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3632 mmc->ocr_avail_mmc = ocr_avail;
3633 if (host->ocr_avail_mmc)
3634 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003635
3636 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003637 pr_err("%s: Hardware doesn't report any support voltages.\n",
3638 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003639 ret = -ENODEV;
3640 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003641 }
3642
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003643 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3644 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3645 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3646 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3647 host->flags |= SDHCI_SIGNALING_180;
3648
3649 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3650 host->flags |= SDHCI_SIGNALING_120;
3651
Pierre Ossmand129bce2006-03-24 03:18:17 -08003652 spin_lock_init(&host->lock);
3653
3654 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003655 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3656 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3657 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003658 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003659 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003660
3661 /*
Ulf Hansson250dcd12017-11-27 11:28:50 +01003662 * Maximum number of segments. Depends on if the hardware
3663 * can do scatter/gather or not.
3664 */
3665 if (host->flags & SDHCI_USE_ADMA) {
3666 mmc->max_segs = SDHCI_MAX_SEGS;
3667 } else if (host->flags & SDHCI_USE_SDMA) {
3668 mmc->max_segs = 1;
3669 if (swiotlb_max_segment()) {
3670 unsigned int max_req_size = (1 << IO_TLB_SHIFT) *
3671 IO_TLB_SEGSIZE;
3672 mmc->max_req_size = min(mmc->max_req_size,
3673 max_req_size);
3674 }
3675 } else { /* PIO */
3676 mmc->max_segs = SDHCI_MAX_SEGS;
3677 }
3678
3679 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003680 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003681 * of bytes. When doing hardware scatter/gather, each entry cannot
3682 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003683 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003684 if (host->flags & SDHCI_USE_ADMA) {
3685 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3686 mmc->max_seg_size = 65535;
3687 else
3688 mmc->max_seg_size = 65536;
3689 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003690 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003691 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003692
3693 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003694 * Maximum block size. This varies from controller to controller and
3695 * is specified in the capabilities register.
3696 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003697 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3698 mmc->max_blk_size = 2;
3699 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003700 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003701 SDHCI_MAX_BLOCK_SHIFT;
3702 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003703 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3704 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003705 mmc->max_blk_size = 0;
3706 }
3707 }
3708
3709 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003710
3711 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003712 * Maximum block count.
3713 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003714 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003715
Adrian Hunter52f53362016-06-29 16:24:15 +03003716 return 0;
3717
3718unreg:
3719 if (!IS_ERR(mmc->supply.vqmmc))
3720 regulator_disable(mmc->supply.vqmmc);
3721undma:
3722 if (host->align_buffer)
3723 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3724 host->adma_table_sz, host->align_buffer,
3725 host->align_addr);
3726 host->adma_table = NULL;
3727 host->align_buffer = NULL;
3728
3729 return ret;
3730}
3731EXPORT_SYMBOL_GPL(sdhci_setup_host);
3732
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003733void sdhci_cleanup_host(struct sdhci_host *host)
3734{
3735 struct mmc_host *mmc = host->mmc;
3736
3737 if (!IS_ERR(mmc->supply.vqmmc))
3738 regulator_disable(mmc->supply.vqmmc);
3739
3740 if (host->align_buffer)
3741 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3742 host->adma_table_sz, host->align_buffer,
3743 host->align_addr);
3744 host->adma_table = NULL;
3745 host->align_buffer = NULL;
3746}
3747EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
3748
Adrian Hunter52f53362016-06-29 16:24:15 +03003749int __sdhci_add_host(struct sdhci_host *host)
3750{
3751 struct mmc_host *mmc = host->mmc;
3752 int ret;
3753
Pierre Ossman55db8902006-11-21 17:55:45 +01003754 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003755 * Init tasklets.
3756 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003757 tasklet_init(&host->finish_tasklet,
3758 sdhci_tasklet_finish, (unsigned long)host);
3759
Kees Cook2ee4f622017-10-24 08:03:45 -07003760 timer_setup(&host->timer, sdhci_timeout_timer, 0);
3761 timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003762
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003763 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303764
Shawn Guo2af502c2013-07-05 14:38:55 +08003765 sdhci_init(host, 0);
3766
Russell King781e9892014-04-25 12:55:46 +01003767 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3768 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003769 if (ret) {
3770 pr_err("%s: Failed to request IRQ %d: %d\n",
3771 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003772 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003773 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003774
Adrian Hunter061d17a2016-04-12 14:25:09 +03003775 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003776 if (ret) {
3777 pr_err("%s: Failed to register LED device: %d\n",
3778 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003779 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003780 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003781
Pierre Ossman5f25a662006-10-04 02:15:39 -07003782 mmiowb();
3783
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003784 ret = mmc_add_host(mmc);
3785 if (ret)
3786 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003787
Girish K Sa3c76eb2011-10-11 11:44:09 +05303788 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003789 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003790 (host->flags & SDHCI_USE_ADMA) ?
3791 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003792 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003793
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003794 sdhci_enable_card_detection(host);
3795
Pierre Ossmand129bce2006-03-24 03:18:17 -08003796 return 0;
3797
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003798unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003799 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003800unirq:
Russell King03231f92014-04-25 12:57:12 +01003801 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003802 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3803 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003804 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003805untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003806 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003807
Pierre Ossmand129bce2006-03-24 03:18:17 -08003808 return ret;
3809}
Adrian Hunter52f53362016-06-29 16:24:15 +03003810EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003811
Adrian Hunter52f53362016-06-29 16:24:15 +03003812int sdhci_add_host(struct sdhci_host *host)
3813{
3814 int ret;
3815
3816 ret = sdhci_setup_host(host);
3817 if (ret)
3818 return ret;
3819
Adrian Hunter4180ffa2017-03-20 19:50:45 +02003820 ret = __sdhci_add_host(host);
3821 if (ret)
3822 goto cleanup;
3823
3824 return 0;
3825
3826cleanup:
3827 sdhci_cleanup_host(host);
3828
3829 return ret;
Adrian Hunter52f53362016-06-29 16:24:15 +03003830}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003831EXPORT_SYMBOL_GPL(sdhci_add_host);
3832
Pierre Ossman1e728592008-04-16 19:13:13 +02003833void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003834{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003835 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003836 unsigned long flags;
3837
3838 if (dead) {
3839 spin_lock_irqsave(&host->lock, flags);
3840
3841 host->flags |= SDHCI_DEVICE_DEAD;
3842
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003843 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303844 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003845 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003846 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003847 }
3848
3849 spin_unlock_irqrestore(&host->lock, flags);
3850 }
3851
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003852 sdhci_disable_card_detection(host);
3853
Markus Mayer4e743f12014-07-03 13:27:42 -07003854 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003855
Adrian Hunter061d17a2016-04-12 14:25:09 +03003856 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003857
Pierre Ossman1e728592008-04-16 19:13:13 +02003858 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003859 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003860
Russell Kingb537f942014-04-25 12:56:01 +01003861 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3862 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003863 free_irq(host->irq, host);
3864
3865 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003866 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003867
Pierre Ossmand129bce2006-03-24 03:18:17 -08003868 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003869
Tim Kryger3a48edc2014-06-13 10:13:56 -07003870 if (!IS_ERR(mmc->supply.vqmmc))
3871 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003872
Russell Kingedd63fc2016-01-26 13:39:50 +00003873 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003874 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3875 host->adma_table_sz, host->align_buffer,
3876 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003877
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003878 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003879 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003880}
3881
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003882EXPORT_SYMBOL_GPL(sdhci_remove_host);
3883
3884void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003885{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003886 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003887}
3888
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003889EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003890
3891/*****************************************************************************\
3892 * *
3893 * Driver init/exit *
3894 * *
3895\*****************************************************************************/
3896
3897static int __init sdhci_drv_init(void)
3898{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303899 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003900 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303901 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003902
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003903 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003904}
3905
3906static void __exit sdhci_drv_exit(void)
3907{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003908}
3909
3910module_init(sdhci_drv_init);
3911module_exit(sdhci_drv_exit);
3912
Pierre Ossmandf673b22006-06-30 02:22:31 -07003913module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003914module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003915
Pierre Ossman32710e82009-04-08 20:14:54 +02003916MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003917MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003918MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003919
Pierre Ossmandf673b22006-06-30 02:22:31 -07003920MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003921MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");