blob: 9f6398ae9181e35b3085dfca184c4d887b9b79f7 [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>
Linus Walleijbd9b9022018-01-29 00:44:53 +010024#include <linux/sizes.h>
Ulf Hansson250dcd12017-11-27 11:28:50 +010025#include <linux/swiotlb.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070026#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030027#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050028#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080029
Pierre Ossman2f730fe2008-03-17 10:29:38 +010030#include <linux/leds.h>
31
Aries Lee22113ef2010-12-15 08:14:24 +010032#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080033#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080034#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080035#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080036#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080037
Pierre Ossmand129bce2006-03-24 03:18:17 -080038#include "sdhci.h"
39
40#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080041
Pierre Ossmand129bce2006-03-24 03:18:17 -080042#define DBG(f, x...) \
Adrian Hunterf4218652017-03-20 19:50:39 +020043 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080044
Adrian Hunter85ad90e2017-03-20 19:50:42 +020045#define SDHCI_DUMP(f, x...) \
46 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
47
Arindam Nathb513ea22011-05-05 12:19:04 +053048#define MAX_TUNING_LOOP 40
49
Pierre Ossmandf673b22006-06-30 02:22:31 -070050static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030051static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070052
Pierre Ossmand129bce2006-03-24 03:18:17 -080053static void sdhci_finish_data(struct sdhci_host *);
54
Kevin Liu52983382013-01-31 11:31:37 +080055static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Adrian Hunterd2898172017-03-20 19:50:43 +020057void sdhci_dumpregs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -080058{
Adrian Hunter85ad90e2017-03-20 19:50:42 +020059 SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -080060
Adrian Hunter85ad90e2017-03-20 19:50:42 +020061 SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
62 sdhci_readl(host, SDHCI_DMA_ADDRESS),
63 sdhci_readw(host, SDHCI_HOST_VERSION));
64 SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
65 sdhci_readw(host, SDHCI_BLOCK_SIZE),
66 sdhci_readw(host, SDHCI_BLOCK_COUNT));
67 SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
68 sdhci_readl(host, SDHCI_ARGUMENT),
69 sdhci_readw(host, SDHCI_TRANSFER_MODE));
70 SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
71 sdhci_readl(host, SDHCI_PRESENT_STATE),
72 sdhci_readb(host, SDHCI_HOST_CONTROL));
73 SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
74 sdhci_readb(host, SDHCI_POWER_CONTROL),
75 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
76 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
77 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
78 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
79 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
80 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
81 sdhci_readl(host, SDHCI_INT_STATUS));
82 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
83 sdhci_readl(host, SDHCI_INT_ENABLE),
84 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
85 SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
86 sdhci_readw(host, SDHCI_ACMD12_ERR),
87 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
88 SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
89 sdhci_readl(host, SDHCI_CAPABILITIES),
90 sdhci_readl(host, SDHCI_CAPABILITIES_1));
91 SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
92 sdhci_readw(host, SDHCI_COMMAND),
93 sdhci_readl(host, SDHCI_MAX_CURRENT));
94 SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020095 sdhci_readl(host, SDHCI_RESPONSE),
96 sdhci_readl(host, SDHCI_RESPONSE + 4));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020097 SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020098 sdhci_readl(host, SDHCI_RESPONSE + 8),
99 sdhci_readl(host, SDHCI_RESPONSE + 12));
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200100 SDHCI_DUMP("Host ctl2: 0x%08x\n",
101 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800102
Adrian Huntere57a5f62014-11-04 12:42:46 +0200103 if (host->flags & SDHCI_USE_ADMA) {
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200104 if (host->flags & SDHCI_USE_64_BIT_DMA) {
105 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
106 sdhci_readl(host, SDHCI_ADMA_ERROR),
107 sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
108 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
109 } else {
110 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
111 sdhci_readl(host, SDHCI_ADMA_ERROR),
112 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
113 }
Adrian Huntere57a5f62014-11-04 12:42:46 +0200114 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100115
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200116 SDHCI_DUMP("============================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800117}
Adrian Hunterd2898172017-03-20 19:50:43 +0200118EXPORT_SYMBOL_GPL(sdhci_dumpregs);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800119
120/*****************************************************************************\
121 * *
122 * Low level functions *
123 * *
124\*****************************************************************************/
125
Adrian Hunter56a590d2016-06-29 16:24:32 +0300126static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
127{
128 return cmd->data || cmd->flags & MMC_RSP_BUSY;
129}
130
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300131static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
132{
Russell King5b4f1f62014-04-25 12:57:02 +0100133 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300134
Adrian Hunterc79396c2011-12-27 15:48:42 +0200135 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900136 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300137 return;
138
Russell King5b4f1f62014-04-25 12:57:02 +0100139 if (enable) {
140 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
141 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800142
Russell King5b4f1f62014-04-25 12:57:02 +0100143 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
144 SDHCI_INT_CARD_INSERT;
145 } else {
146 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
147 }
Russell Kingb537f942014-04-25 12:56:01 +0100148
149 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
150 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300151}
152
153static void sdhci_enable_card_detection(struct sdhci_host *host)
154{
155 sdhci_set_card_detection(host, true);
156}
157
158static void sdhci_disable_card_detection(struct sdhci_host *host)
159{
160 sdhci_set_card_detection(host, false);
161}
162
Ulf Hansson02d0b682016-04-11 15:32:41 +0200163static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
164{
165 if (host->bus_on)
166 return;
167 host->bus_on = true;
168 pm_runtime_get_noresume(host->mmc->parent);
169}
170
171static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
172{
173 if (!host->bus_on)
174 return;
175 host->bus_on = false;
176 pm_runtime_put_noidle(host->mmc->parent);
177}
178
Russell King03231f92014-04-25 12:57:12 +0100179void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800180{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200181 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800182
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300183 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800184
Adrian Hunterf0710a52013-05-06 12:17:32 +0300185 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800186 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300187 /* Reset-all turns off SD Bus Power */
188 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
189 sdhci_runtime_pm_bus_off(host);
190 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800191
Pierre Ossmane16514d82006-06-30 02:22:24 -0700192 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200193 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700194
195 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300196 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200197 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530198 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700199 mmc_hostname(host->mmc), (int)mask);
200 sdhci_dumpregs(host);
201 return;
202 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200203 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800204 }
Russell King03231f92014-04-25 12:57:12 +0100205}
206EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300207
Russell King03231f92014-04-25 12:57:12 +0100208static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
209{
210 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300211 struct mmc_host *mmc = host->mmc;
212
213 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100214 return;
215 }
216
217 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800218
Russell Kingda91a8f2014-04-25 13:00:12 +0100219 if (mask & SDHCI_RESET_ALL) {
220 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
221 if (host->ops->enable_dma)
222 host->ops->enable_dma(host);
223 }
224
225 /* Resetting the controller clears many */
226 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800227 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228}
229
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200230static void sdhci_set_default_irqs(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800231{
Russell Kingb537f942014-04-25 12:56:01 +0100232 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
233 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
234 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
235 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
236 SDHCI_INT_RESPONSE;
237
Dong Aishengf37b20e2016-07-12 15:46:17 +0800238 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
239 host->tuning_mode == SDHCI_TUNING_MODE_3)
240 host->ier |= SDHCI_INT_RETUNE;
241
Russell Kingb537f942014-04-25 12:56:01 +0100242 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
243 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterf5c1ab82017-03-20 19:50:46 +0200244}
245
246static void sdhci_init(struct sdhci_host *host, int soft)
247{
248 struct mmc_host *mmc = host->mmc;
249
250 if (soft)
251 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
252 else
253 sdhci_do_reset(host, SDHCI_RESET_ALL);
254
255 sdhci_set_default_irqs(host);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800256
Adrian Hunterf12e39d2017-03-20 19:50:47 +0200257 host->cqe_on = false;
258
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800259 if (soft) {
260 /* force clock reconfiguration */
261 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300262 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800263 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300264}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800265
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300266static void sdhci_reinit(struct sdhci_host *host)
267{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800268 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300269 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800270}
271
Adrian Hunter061d17a2016-04-12 14:25:09 +0300272static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800273{
274 u8 ctrl;
275
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300276 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800277 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300278 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800279}
280
Adrian Hunter061d17a2016-04-12 14:25:09 +0300281static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800282{
283 u8 ctrl;
284
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300285 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800286 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300287 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800288}
289
Masahiro Yamada4f782302016-04-14 13:19:39 +0900290#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100291static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300292 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100293{
294 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
295 unsigned long flags;
296
297 spin_lock_irqsave(&host->lock, flags);
298
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300299 if (host->runtime_suspended)
300 goto out;
301
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100302 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300303 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100304 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300305 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300306out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100307 spin_unlock_irqrestore(&host->lock, flags);
308}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300309
310static int sdhci_led_register(struct sdhci_host *host)
311{
312 struct mmc_host *mmc = host->mmc;
313
314 snprintf(host->led_name, sizeof(host->led_name),
315 "%s::", mmc_hostname(mmc));
316
317 host->led.name = host->led_name;
318 host->led.brightness = LED_OFF;
319 host->led.default_trigger = mmc_hostname(mmc);
320 host->led.brightness_set = sdhci_led_control;
321
322 return led_classdev_register(mmc_dev(mmc), &host->led);
323}
324
325static void sdhci_led_unregister(struct sdhci_host *host)
326{
327 led_classdev_unregister(&host->led);
328}
329
330static inline void sdhci_led_activate(struct sdhci_host *host)
331{
332}
333
334static inline void sdhci_led_deactivate(struct sdhci_host *host)
335{
336}
337
338#else
339
340static inline int sdhci_led_register(struct sdhci_host *host)
341{
342 return 0;
343}
344
345static inline void sdhci_led_unregister(struct sdhci_host *host)
346{
347}
348
349static inline void sdhci_led_activate(struct sdhci_host *host)
350{
351 __sdhci_led_activate(host);
352}
353
354static inline void sdhci_led_deactivate(struct sdhci_host *host)
355{
356 __sdhci_led_deactivate(host);
357}
358
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100359#endif
360
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361/*****************************************************************************\
362 * *
363 * Core functions *
364 * *
365\*****************************************************************************/
366
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100367static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800368{
Pierre Ossman76591502008-07-21 00:32:11 +0200369 unsigned long flags;
370 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700371 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200372 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800373
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100374 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800375
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100376 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200377 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800380
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100381 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300382 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800383
Pierre Ossman76591502008-07-21 00:32:11 +0200384 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800385
Pierre Ossman76591502008-07-21 00:32:11 +0200386 blksize -= len;
387 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200388
Pierre Ossman76591502008-07-21 00:32:11 +0200389 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800390
Pierre Ossman76591502008-07-21 00:32:11 +0200391 while (len) {
392 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300393 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200394 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800395 }
Pierre Ossman76591502008-07-21 00:32:11 +0200396
397 *buf = scratch & 0xFF;
398
399 buf++;
400 scratch >>= 8;
401 chunk--;
402 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800403 }
404 }
Pierre Ossman76591502008-07-21 00:32:11 +0200405
406 sg_miter_stop(&host->sg_miter);
407
408 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800410
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100411static void sdhci_write_block_pio(struct sdhci_host *host)
412{
Pierre Ossman76591502008-07-21 00:32:11 +0200413 unsigned long flags;
414 size_t blksize, len, chunk;
415 u32 scratch;
416 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100417
418 DBG("PIO writing\n");
419
420 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200421 chunk = 0;
422 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423
Pierre Ossman76591502008-07-21 00:32:11 +0200424 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425
426 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300427 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428
Pierre Ossman76591502008-07-21 00:32:11 +0200429 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200430
Pierre Ossman76591502008-07-21 00:32:11 +0200431 blksize -= len;
432 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100433
Pierre Ossman76591502008-07-21 00:32:11 +0200434 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435
Pierre Ossman76591502008-07-21 00:32:11 +0200436 while (len) {
437 scratch |= (u32)*buf << (chunk * 8);
438
439 buf++;
440 chunk++;
441 len--;
442
443 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300444 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200445 chunk = 0;
446 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100447 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100448 }
449 }
Pierre Ossman76591502008-07-21 00:32:11 +0200450
451 sg_miter_stop(&host->sg_miter);
452
453 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100454}
455
456static void sdhci_transfer_pio(struct sdhci_host *host)
457{
458 u32 mask;
459
Pierre Ossman76591502008-07-21 00:32:11 +0200460 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100461 return;
462
463 if (host->data->flags & MMC_DATA_READ)
464 mask = SDHCI_DATA_AVAILABLE;
465 else
466 mask = SDHCI_SPACE_AVAILABLE;
467
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200468 /*
469 * Some controllers (JMicron JMB38x) mess up the buffer bits
470 * for transfers < 4 bytes. As long as it is just one block,
471 * we can ignore the bits.
472 */
473 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
474 (host->data->blocks == 1))
475 mask = ~0;
476
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300477 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300478 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
479 udelay(100);
480
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100481 if (host->data->flags & MMC_DATA_READ)
482 sdhci_read_block_pio(host);
483 else
484 sdhci_write_block_pio(host);
485
Pierre Ossman76591502008-07-21 00:32:11 +0200486 host->blocks--;
487 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100488 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100489 }
490
491 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800492}
493
Russell King48857d92016-01-26 13:40:16 +0000494static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000495 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000496{
497 int sg_count;
498
Russell King94538e52016-01-26 13:40:37 +0000499 /*
500 * If the data buffers are already mapped, return the previous
501 * dma_map_sg() result.
502 */
503 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000504 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000505
Linus Walleijbd9b9022018-01-29 00:44:53 +0100506 /* Bounce write requests to the bounce buffer */
507 if (host->bounce_buffer) {
508 unsigned int length = data->blksz * data->blocks;
509
510 if (length > host->bounce_buffer_size) {
511 pr_err("%s: asked for transfer of %u bytes exceeds bounce buffer %u bytes\n",
512 mmc_hostname(host->mmc), length,
513 host->bounce_buffer_size);
514 return -EIO;
515 }
516 if (mmc_get_dma_dir(data) == DMA_TO_DEVICE) {
517 /* Copy the data to the bounce buffer */
518 sg_copy_to_buffer(data->sg, data->sg_len,
519 host->bounce_buffer,
520 length);
521 }
522 /* Switch ownership to the DMA */
523 dma_sync_single_for_device(host->mmc->parent,
524 host->bounce_addr,
525 host->bounce_buffer_size,
526 mmc_get_dma_dir(data));
527 /* Just a dummy value */
528 sg_count = 1;
529 } else {
530 /* Just access the data directly from memory */
531 sg_count = dma_map_sg(mmc_dev(host->mmc),
532 data->sg, data->sg_len,
533 mmc_get_dma_dir(data));
534 }
Russell King48857d92016-01-26 13:40:16 +0000535
536 if (sg_count == 0)
537 return -ENOSPC;
538
539 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000540 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000541
542 return sg_count;
543}
544
Pierre Ossman2134a922008-06-28 18:28:51 +0200545static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
546{
547 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800548 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200549}
550
551static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
552{
Cong Wang482fce92011-11-27 13:27:00 +0800553 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200554 local_irq_restore(*flags);
555}
556
Adrian Huntere57a5f62014-11-04 12:42:46 +0200557static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
558 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800559{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200560 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800561
Adrian Huntere57a5f62014-11-04 12:42:46 +0200562 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200563 dma_desc->cmd = cpu_to_le16(cmd);
564 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200565 dma_desc->addr_lo = cpu_to_le32((u32)addr);
566
567 if (host->flags & SDHCI_USE_64_BIT_DMA)
568 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800569}
570
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200571static void sdhci_adma_mark_end(void *desc)
572{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200573 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200574
Adrian Huntere57a5f62014-11-04 12:42:46 +0200575 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200576 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200577}
578
Russell King60c64762016-01-26 13:40:22 +0000579static void sdhci_adma_table_pre(struct sdhci_host *host,
580 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200581{
Pierre Ossman2134a922008-06-28 18:28:51 +0200582 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200583 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000584 dma_addr_t addr, align_addr;
585 void *desc, *align;
586 char *buffer;
587 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200588
589 /*
590 * The spec does not specify endianness of descriptor table.
591 * We currently guess that it is LE.
592 */
593
Russell King60c64762016-01-26 13:40:22 +0000594 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200595
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200596 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200597 align = host->align_buffer;
598
599 align_addr = host->align_addr;
600
601 for_each_sg(data->sg, sg, host->sg_count, i) {
602 addr = sg_dma_address(sg);
603 len = sg_dma_len(sg);
604
605 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000606 * The SDHCI specification states that ADMA addresses must
607 * be 32-bit aligned. If they aren't, then we use a bounce
608 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200609 * alignment.
610 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200611 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
612 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200613 if (offset) {
614 if (data->flags & MMC_DATA_WRITE) {
615 buffer = sdhci_kmap_atomic(sg, &flags);
616 memcpy(align, buffer, offset);
617 sdhci_kunmap_atomic(buffer, &flags);
618 }
619
Ben Dooks118cd172010-03-05 13:43:26 -0800620 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200621 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200622 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200623
624 BUG_ON(offset > 65536);
625
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200626 align += SDHCI_ADMA2_ALIGN;
627 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200628
Adrian Hunter76fe3792014-11-04 12:42:42 +0200629 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200630
631 addr += offset;
632 len -= offset;
633 }
634
Pierre Ossman2134a922008-06-28 18:28:51 +0200635 BUG_ON(len > 65536);
636
Adrian Hunter347ea322015-11-26 14:00:48 +0200637 if (len) {
638 /* tran, valid */
639 sdhci_adma_write_desc(host, desc, addr, len,
640 ADMA2_TRAN_VALID);
641 desc += host->desc_sz;
642 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200643
644 /*
645 * If this triggers then we have a calculation bug
646 * somewhere. :/
647 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200648 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200649 }
650
Thomas Abraham70764a92010-05-26 14:42:04 -0700651 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000652 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200653 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200654 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200655 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700656 }
657 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000658 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200659 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700660 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200661}
662
663static void sdhci_adma_table_post(struct sdhci_host *host,
664 struct mmc_data *data)
665{
Pierre Ossman2134a922008-06-28 18:28:51 +0200666 struct scatterlist *sg;
667 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200668 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200669 char *buffer;
670 unsigned long flags;
671
Russell King47fa9612016-01-26 13:40:06 +0000672 if (data->flags & MMC_DATA_READ) {
673 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100674
Russell King47fa9612016-01-26 13:40:06 +0000675 /* Do a quick scan of the SG list for any unaligned mappings */
676 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200677 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000678 has_unaligned = true;
679 break;
680 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200681
Russell King47fa9612016-01-26 13:40:06 +0000682 if (has_unaligned) {
683 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000684 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200685
Russell King47fa9612016-01-26 13:40:06 +0000686 align = host->align_buffer;
687
688 for_each_sg(data->sg, sg, host->sg_count, i) {
689 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
690 size = SDHCI_ADMA2_ALIGN -
691 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
692
693 buffer = sdhci_kmap_atomic(sg, &flags);
694 memcpy(buffer, align, size);
695 sdhci_kunmap_atomic(buffer, &flags);
696
697 align += SDHCI_ADMA2_ALIGN;
698 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200699 }
700 }
701 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200702}
703
Linus Walleijbd9b9022018-01-29 00:44:53 +0100704static u32 sdhci_sdma_address(struct sdhci_host *host)
705{
706 if (host->bounce_buffer)
707 return host->bounce_addr;
708 else
709 return sg_dma_address(host->data->sg);
710}
711
Adrian Hunter0bb28d72018-04-27 17:17:16 +0530712static unsigned int sdhci_target_timeout(struct sdhci_host *host,
713 struct mmc_command *cmd,
714 struct mmc_data *data)
715{
716 unsigned int target_timeout;
717
718 /* timeout in us */
719 if (!data) {
720 target_timeout = cmd->busy_timeout * 1000;
721 } else {
722 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
723 if (host->clock && data->timeout_clks) {
724 unsigned long long val;
725
726 /*
727 * data->timeout_clks is in units of clock cycles.
728 * host->clock is in Hz. target_timeout is in us.
729 * Hence, us = 1000000 * cycles / Hz. Round up.
730 */
731 val = 1000000ULL * data->timeout_clks;
732 if (do_div(val, host->clock))
733 target_timeout++;
734 target_timeout += val;
735 }
736 }
737
738 return target_timeout;
739}
740
Kishon Vijay Abraham Ifc1fa1b2018-04-27 17:17:17 +0530741static void sdhci_calc_sw_timeout(struct sdhci_host *host,
742 struct mmc_command *cmd)
743{
744 struct mmc_data *data = cmd->data;
745 struct mmc_host *mmc = host->mmc;
746 struct mmc_ios *ios = &mmc->ios;
747 unsigned char bus_width = 1 << ios->bus_width;
748 unsigned int blksz;
749 unsigned int freq;
750 u64 target_timeout;
751 u64 transfer_time;
752
753 target_timeout = sdhci_target_timeout(host, cmd, data);
754 target_timeout *= NSEC_PER_USEC;
755
756 if (data) {
757 blksz = data->blksz;
758 freq = host->mmc->actual_clock ? : host->clock;
759 transfer_time = (u64)blksz * NSEC_PER_SEC * (8 / bus_width);
760 do_div(transfer_time, freq);
761 /* multiply by '2' to account for any unknowns */
762 transfer_time = transfer_time * 2;
763 /* calculate timeout for the entire data */
764 host->data_timeout = data->blocks * target_timeout +
765 transfer_time;
766 } else {
767 host->data_timeout = target_timeout;
768 }
769
770 if (host->data_timeout)
771 host->data_timeout += MMC_CMD_TRANSFER_TIME;
772}
773
Adrian Huntera999fd92018-04-27 17:17:15 +0530774static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd,
775 bool *too_big)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800776{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700777 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500778 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700779 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800780
Adrian Huntera999fd92018-04-27 17:17:15 +0530781 *too_big = true;
782
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200783 /*
784 * If the host controller provides us with an incorrect timeout
785 * value, just skip the check and use 0xE. The hardware may take
786 * longer to time out, but that's much better than having a too-short
787 * timeout value.
788 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200789 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200790 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200791
Andrei Warkentina3c77782011-04-11 16:13:42 -0500792 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100793 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500794 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800795
Andrei Warkentina3c77782011-04-11 16:13:42 -0500796 /* timeout in us */
Adrian Hunter0bb28d72018-04-27 17:17:16 +0530797 target_timeout = sdhci_target_timeout(host, cmd, data);
Anton Vorontsov81b39802009-09-22 16:45:13 -0700798
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700799 /*
800 * Figure out needed cycles.
801 * We do this in steps in order to fit inside a 32 bit int.
802 * The first step is the minimum timeout, which will have a
803 * minimum resolution of 6 bits:
804 * (1) 2^13*1000 > 2^22,
805 * (2) host->timeout_clk < 2^16
806 * =>
807 * (1) / (2) > 2^6
808 */
809 count = 0;
810 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
811 while (current_timeout < target_timeout) {
812 count++;
813 current_timeout <<= 1;
814 if (count >= 0xF)
815 break;
816 }
817
818 if (count >= 0xF) {
Adrian Huntera999fd92018-04-27 17:17:15 +0530819 if (!(host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT))
820 DBG("Too large timeout 0x%x requested for CMD%d!\n",
821 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700822 count = 0xE;
Adrian Huntera999fd92018-04-27 17:17:15 +0530823 } else {
824 *too_big = false;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700825 }
826
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200827 return count;
828}
829
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300830static void sdhci_set_transfer_irqs(struct sdhci_host *host)
831{
832 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
833 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
834
835 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100836 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300837 else
Russell Kingb537f942014-04-25 12:56:01 +0100838 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
839
840 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
841 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300842}
843
Adrian Huntera999fd92018-04-27 17:17:15 +0530844static void sdhci_set_data_timeout_irq(struct sdhci_host *host, bool enable)
845{
846 if (enable)
847 host->ier |= SDHCI_INT_DATA_TIMEOUT;
848 else
849 host->ier &= ~SDHCI_INT_DATA_TIMEOUT;
850 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
851 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
852}
853
Aisheng Dongb45e6682014-08-27 15:26:29 +0800854static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200855{
856 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800857
858 if (host->ops->set_timeout) {
859 host->ops->set_timeout(host, cmd);
860 } else {
Adrian Huntera999fd92018-04-27 17:17:15 +0530861 bool too_big = false;
862
863 count = sdhci_calc_timeout(host, cmd, &too_big);
864
865 if (too_big &&
866 host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT) {
Kishon Vijay Abraham Ifc1fa1b2018-04-27 17:17:17 +0530867 sdhci_calc_sw_timeout(host, cmd);
Adrian Huntera999fd92018-04-27 17:17:15 +0530868 sdhci_set_data_timeout_irq(host, false);
869 } else if (!(host->ier & SDHCI_INT_DATA_TIMEOUT)) {
870 sdhci_set_data_timeout_irq(host, true);
871 }
872
Aisheng Dongb45e6682014-08-27 15:26:29 +0800873 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
874 }
875}
876
877static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
878{
Pierre Ossman2134a922008-06-28 18:28:51 +0200879 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500880 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200881
Kishon Vijay Abraham Ifc1fa1b2018-04-27 17:17:17 +0530882 host->data_timeout = 0;
883
Adrian Hunter56a590d2016-06-29 16:24:32 +0300884 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800885 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500886
887 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200888 return;
889
Adrian Hunter43dea092016-06-29 16:24:26 +0300890 WARN_ON(host->data);
891
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200892 /* Sanity checks */
893 BUG_ON(data->blksz * data->blocks > 524288);
894 BUG_ON(data->blksz > host->mmc->max_blk_size);
895 BUG_ON(data->blocks > 65535);
896
897 host->data = data;
898 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400899 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200900
Russell Kingfce14422016-01-26 13:41:20 +0000901 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200902 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000903 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000904 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200905
Russell Kingfce14422016-01-26 13:41:20 +0000906 host->flags |= SDHCI_REQ_USE_DMA;
907
908 /*
909 * FIXME: This doesn't account for merging when mapping the
910 * scatterlist.
911 *
912 * The assumption here being that alignment and lengths are
913 * the same after DMA mapping to device address space.
914 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000915 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000916 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200917 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000918 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000919 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000920 /*
921 * As we use up to 3 byte chunks to work
922 * around alignment problems, we need to
923 * check the offset as well.
924 */
925 offset_mask = 3;
926 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200927 } else {
928 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000929 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000930 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
931 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200932 }
933
Russell Kingdf953922016-01-26 13:41:14 +0000934 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200935 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000936 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100937 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000938 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200939 host->flags &= ~SDHCI_REQ_USE_DMA;
940 break;
941 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000942 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100943 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200944 host->flags &= ~SDHCI_REQ_USE_DMA;
945 break;
946 }
947 }
948 }
949 }
950
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200951 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000952 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200953
Russell King60c64762016-01-26 13:40:22 +0000954 if (sg_cnt <= 0) {
955 /*
956 * This only happens when someone fed
957 * us an invalid request.
958 */
959 WARN_ON(1);
960 host->flags &= ~SDHCI_REQ_USE_DMA;
961 } else if (host->flags & SDHCI_USE_ADMA) {
962 sdhci_adma_table_pre(host, data, sg_cnt);
963
964 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
965 if (host->flags & SDHCI_USE_64_BIT_DMA)
966 sdhci_writel(host,
967 (u64)host->adma_addr >> 32,
968 SDHCI_ADMA_ADDRESS_HI);
969 } else {
970 WARN_ON(sg_cnt != 1);
Linus Walleijbd9b9022018-01-29 00:44:53 +0100971 sdhci_writel(host, sdhci_sdma_address(host),
972 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200973 }
974 }
975
Pierre Ossman2134a922008-06-28 18:28:51 +0200976 /*
977 * Always adjust the DMA selection as some controllers
978 * (e.g. JMicron) can't do PIO properly when the selection
979 * is ADMA.
980 */
981 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300982 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200983 ctrl &= ~SDHCI_CTRL_DMA_MASK;
984 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200985 (host->flags & SDHCI_USE_ADMA)) {
986 if (host->flags & SDHCI_USE_64_BIT_DMA)
987 ctrl |= SDHCI_CTRL_ADMA64;
988 else
989 ctrl |= SDHCI_CTRL_ADMA32;
990 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200991 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200992 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300993 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100994 }
995
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200996 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200997 int flags;
998
999 flags = SG_MITER_ATOMIC;
1000 if (host->data->flags & MMC_DATA_READ)
1001 flags |= SG_MITER_TO_SG;
1002 else
1003 flags |= SG_MITER_FROM_SG;
1004 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +02001005 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001007
Anton Vorontsov6aa943a2009-03-17 00:13:50 +03001008 sdhci_set_transfer_irqs(host);
1009
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04001010 /* Set the DMA boundary value and block size */
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02001011 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, data->blksz),
1012 SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001013 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001014}
1015
Adrian Hunter0293d502016-06-29 16:24:35 +03001016static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
1017 struct mmc_request *mrq)
1018{
Adrian Hunter20845be2016-08-16 13:44:13 +03001019 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
1020 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +03001021}
1022
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001023static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -05001024 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001025{
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001026 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001027 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001028
Dong Aisheng2b558c12013-10-30 22:09:48 +08001029 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001030 if (host->quirks2 &
1031 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
ernest.zhang0086fc22018-07-16 14:26:54 +08001032 /* must not clear SDHCI_TRANSFER_MODE when tuning */
1033 if (cmd->opcode != MMC_SEND_TUNING_BLOCK_HS200)
1034 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001035 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +08001036 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001037 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
1038 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +08001039 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +08001040 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001041 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +08001042 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001043
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001044 WARN_ON(!host->data);
1045
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001046 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
1047 mode = SDHCI_TRNS_BLK_CNT_EN;
1048
Andrei Warkentine89d4562011-05-23 15:06:37 -05001049 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +08001050 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -05001051 /*
1052 * If we are sending CMD23, CMD12 never gets sent
1053 * on successful completion (so no Auto-CMD12).
1054 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001055 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -08001056 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001057 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001058 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001059 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001060 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001061 }
Jerry Huangc4512f72010-08-10 18:01:59 -07001062 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001063
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001064 if (data->flags & MMC_DATA_READ)
1065 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +01001066 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001067 mode |= SDHCI_TRNS_DMA;
1068
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001069 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001070}
1071
Adrian Hunter0cc563c2016-06-29 16:24:28 +03001072static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
1073{
1074 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
1075 ((mrq->cmd && mrq->cmd->error) ||
1076 (mrq->sbc && mrq->sbc->error) ||
1077 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
1078 (mrq->data->stop && mrq->data->stop->error))) ||
1079 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
1080}
1081
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001082static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1083{
1084 int i;
1085
1086 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1087 if (host->mrqs_done[i] == mrq) {
1088 WARN_ON(1);
1089 return;
1090 }
1091 }
1092
1093 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
1094 if (!host->mrqs_done[i]) {
1095 host->mrqs_done[i] = mrq;
1096 break;
1097 }
1098 }
1099
1100 WARN_ON(i >= SDHCI_MAX_MRQS);
1101
1102 tasklet_schedule(&host->finish_tasklet);
1103}
1104
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001105static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
1106{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +03001107 if (host->cmd && host->cmd->mrq == mrq)
1108 host->cmd = NULL;
1109
1110 if (host->data_cmd && host->data_cmd->mrq == mrq)
1111 host->data_cmd = NULL;
1112
1113 if (host->data && host->data->mrq == mrq)
1114 host->data = NULL;
1115
Adrian Huntered1563d2016-06-29 16:24:29 +03001116 if (sdhci_needs_reset(host, mrq))
1117 host->pending_reset = true;
1118
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03001119 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001120}
1121
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122static void sdhci_finish_data(struct sdhci_host *host)
1123{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001124 struct mmc_command *data_cmd = host->data_cmd;
1125 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Pierre Ossmand129bce2006-03-24 03:18:17 -08001127 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001128 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129
Russell Kingadd89132016-01-26 13:40:42 +00001130 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1131 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1132 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
1134 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001135 * The specification states that the block count register must
1136 * be updated, but it does not specify at what point in the
1137 * data flow. That makes the register entirely useless to read
1138 * back so we have to assume that nothing made it to the card
1139 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001141 if (data->error)
1142 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001144 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001145
Andrei Warkentine89d4562011-05-23 15:06:37 -05001146 /*
1147 * Need to send CMD12 if -
1148 * a) open-ended multiblock transfer (no CMD23)
1149 * b) error in multiblock transfer
1150 */
1151 if (data->stop &&
1152 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001153 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001154
Pierre Ossmand129bce2006-03-24 03:18:17 -08001155 /*
1156 * The controller needs a reset of internal state machines
1157 * upon error conditions.
1158 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001159 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001160 if (!host->cmd || host->cmd == data_cmd)
1161 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001162 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 }
1164
Adrian Hunter20845be2016-08-16 13:44:13 +03001165 /*
1166 * 'cap_cmd_during_tfr' request must not use the command line
1167 * after mmc_command_done() has been called. It is upper layer's
1168 * responsibility to send the stop command if required.
1169 */
1170 if (data->mrq->cap_cmd_during_tfr) {
1171 sdhci_finish_mrq(host, data->mrq);
1172 } else {
1173 /* Avoid triggering warning in sdhci_send_command() */
1174 host->cmd = NULL;
1175 sdhci_send_command(host, data->stop);
1176 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001177 } else {
1178 sdhci_finish_mrq(host, data->mrq);
1179 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180}
1181
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001182static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1183 unsigned long timeout)
1184{
1185 if (sdhci_data_line_cmd(mrq->cmd))
1186 mod_timer(&host->data_timer, timeout);
1187 else
1188 mod_timer(&host->timer, timeout);
1189}
1190
1191static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1192{
1193 if (sdhci_data_line_cmd(mrq->cmd))
1194 del_timer(&host->data_timer);
1195 else
1196 del_timer(&host->timer);
1197}
1198
Dong Aishengc0e551292013-09-13 19:11:31 +08001199void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001200{
1201 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001202 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001203 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001204
1205 WARN_ON(host->cmd);
1206
Russell King96776202016-01-26 13:39:34 +00001207 /* Initially, a command has no error */
1208 cmd->error = 0;
1209
Adrian Hunterfc605f12016-10-05 12:11:21 +03001210 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1211 cmd->opcode == MMC_STOP_TRANSMISSION)
1212 cmd->flags |= MMC_RSP_BUSY;
1213
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001215 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001216
1217 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001218 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001219 mask |= SDHCI_DATA_INHIBIT;
1220
1221 /* We shouldn't wait for data inihibit for stop commands, even
1222 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001223 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001224 mask &= ~SDHCI_DATA_INHIBIT;
1225
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001226 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001227 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001228 pr_err("%s: Controller never released inhibit bit(s).\n",
1229 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001230 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001231 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001232 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001233 return;
1234 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001235 timeout--;
1236 mdelay(1);
1237 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001238
Pierre Ossmand129bce2006-03-24 03:18:17 -08001239 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001240 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001241 WARN_ON(host->data_cmd);
1242 host->data_cmd = cmd;
1243 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001244
Andrei Warkentina3c77782011-04-11 16:13:42 -05001245 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001246
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001247 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001248
Andrei Warkentine89d4562011-05-23 15:06:37 -05001249 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001250
Pierre Ossmand129bce2006-03-24 03:18:17 -08001251 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301252 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001253 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001254 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001255 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256 return;
1257 }
1258
1259 if (!(cmd->flags & MMC_RSP_PRESENT))
1260 flags = SDHCI_CMD_RESP_NONE;
1261 else if (cmd->flags & MMC_RSP_136)
1262 flags = SDHCI_CMD_RESP_LONG;
1263 else if (cmd->flags & MMC_RSP_BUSY)
1264 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1265 else
1266 flags = SDHCI_CMD_RESP_SHORT;
1267
1268 if (cmd->flags & MMC_RSP_CRC)
1269 flags |= SDHCI_CMD_CRC;
1270 if (cmd->flags & MMC_RSP_OPCODE)
1271 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301272
1273 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301274 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1275 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001276 flags |= SDHCI_CMD_DATA;
1277
Kishon Vijay Abraham Ifc1fa1b2018-04-27 17:17:17 +05301278 timeout = jiffies;
1279 if (host->data_timeout)
1280 timeout += nsecs_to_jiffies(host->data_timeout);
1281 else if (!cmd->data && cmd->busy_timeout > 9000)
1282 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
1283 else
1284 timeout += 10 * HZ;
1285 sdhci_mod_timer(host, cmd->mrq, timeout);
1286
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001287 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001288}
Dong Aishengc0e551292013-09-13 19:11:31 +08001289EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001290
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301291static void sdhci_read_rsp_136(struct sdhci_host *host, struct mmc_command *cmd)
1292{
1293 int i, reg;
1294
1295 for (i = 0; i < 4; i++) {
1296 reg = SDHCI_RESPONSE + (3 - i) * 4;
1297 cmd->resp[i] = sdhci_readl(host, reg);
1298 }
1299
Kishon Vijay Abraham I1284c242017-08-21 13:11:29 +05301300 if (host->quirks2 & SDHCI_QUIRK2_RSP_136_HAS_CRC)
1301 return;
1302
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301303 /* CRC is stripped so we need to do some shifting */
1304 for (i = 0; i < 4; i++) {
1305 cmd->resp[i] <<= 8;
1306 if (i != 3)
1307 cmd->resp[i] |= cmd->resp[i + 1] >> 24;
1308 }
1309}
1310
Pierre Ossmand129bce2006-03-24 03:18:17 -08001311static void sdhci_finish_command(struct sdhci_host *host)
1312{
Adrian Huntere0a56402016-06-29 16:24:22 +03001313 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001314
Adrian Huntere0a56402016-06-29 16:24:22 +03001315 host->cmd = NULL;
1316
1317 if (cmd->flags & MMC_RSP_PRESENT) {
1318 if (cmd->flags & MMC_RSP_136) {
Adrian Hunter4a5fc112017-08-21 13:11:28 +05301319 sdhci_read_rsp_136(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001320 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001321 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001322 }
1323 }
1324
Adrian Hunter20845be2016-08-16 13:44:13 +03001325 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1326 mmc_command_done(host->mmc, cmd->mrq);
1327
Adrian Hunter6bde8682016-06-29 16:24:20 +03001328 /*
1329 * The host can send and interrupt when the busy state has
1330 * ended, allowing us to wait without wasting CPU cycles.
1331 * The busy signal uses DAT0 so this is similar to waiting
1332 * for data to complete.
1333 *
1334 * Note: The 1.0 specification is a bit ambiguous about this
1335 * feature so there might be some problems with older
1336 * controllers.
1337 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001338 if (cmd->flags & MMC_RSP_BUSY) {
1339 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001340 DBG("Cannot wait for busy signal when also doing a data transfer");
1341 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001342 cmd == host->data_cmd) {
1343 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001344 return;
1345 }
1346 }
1347
Andrei Warkentine89d4562011-05-23 15:06:37 -05001348 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001349 if (cmd == cmd->mrq->sbc) {
1350 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001351 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001352
Andrei Warkentine89d4562011-05-23 15:06:37 -05001353 /* Processed actual command. */
1354 if (host->data && host->data_early)
1355 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001356
Adrian Huntere0a56402016-06-29 16:24:22 +03001357 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001358 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001359 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001360}
1361
Kevin Liu52983382013-01-31 11:31:37 +08001362static u16 sdhci_get_preset_value(struct sdhci_host *host)
1363{
Russell Kingd975f122014-04-25 12:59:31 +01001364 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001365
Russell Kingd975f122014-04-25 12:59:31 +01001366 switch (host->timing) {
1367 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001368 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1369 break;
Russell Kingd975f122014-04-25 12:59:31 +01001370 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001371 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1372 break;
Russell Kingd975f122014-04-25 12:59:31 +01001373 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001374 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1375 break;
Russell Kingd975f122014-04-25 12:59:31 +01001376 case MMC_TIMING_UHS_SDR104:
1377 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001378 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1379 break;
Russell Kingd975f122014-04-25 12:59:31 +01001380 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001381 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001382 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1383 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001384 case MMC_TIMING_MMC_HS400:
1385 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1386 break;
Kevin Liu52983382013-01-31 11:31:37 +08001387 default:
1388 pr_warn("%s: Invalid UHS-I mode selected\n",
1389 mmc_hostname(host->mmc));
1390 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1391 break;
1392 }
1393 return preset;
1394}
1395
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001396u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1397 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001398{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301399 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001400 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301401 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001402 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001403
Zhangfei Gao85105c52010-08-06 07:10:01 +08001404 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001405 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001406 u16 pre_val;
1407
1408 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1409 pre_val = sdhci_get_preset_value(host);
1410 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1411 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1412 if (host->clk_mul &&
1413 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1414 clk = SDHCI_PROG_CLOCK_MODE;
1415 real_div = div + 1;
1416 clk_mul = host->clk_mul;
1417 } else {
1418 real_div = max_t(int, 1, div << 1);
1419 }
1420 goto clock_set;
1421 }
1422
Arindam Nathc3ed3872011-05-05 12:19:06 +05301423 /*
1424 * Check if the Host Controller supports Programmable Clock
1425 * Mode.
1426 */
1427 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001428 for (div = 1; div <= 1024; div++) {
1429 if ((host->max_clk * host->clk_mul / div)
1430 <= clock)
1431 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001432 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001433 if ((host->max_clk * host->clk_mul / div) <= clock) {
1434 /*
1435 * Set Programmable Clock Mode in the Clock
1436 * Control register.
1437 */
1438 clk = SDHCI_PROG_CLOCK_MODE;
1439 real_div = div;
1440 clk_mul = host->clk_mul;
1441 div--;
1442 } else {
1443 /*
1444 * Divisor can be too small to reach clock
1445 * speed requirement. Then use the base clock.
1446 */
1447 switch_base_clk = true;
1448 }
1449 }
1450
1451 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301452 /* Version 3.00 divisors must be a multiple of 2. */
1453 if (host->max_clk <= clock)
1454 div = 1;
1455 else {
1456 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1457 div += 2) {
1458 if ((host->max_clk / div) <= clock)
1459 break;
1460 }
1461 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001462 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301463 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301464 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1465 && !div && host->max_clk <= 25000000)
1466 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001467 }
1468 } else {
1469 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001470 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001471 if ((host->max_clk / div) <= clock)
1472 break;
1473 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001474 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301475 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001476 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001477
Kevin Liu52983382013-01-31 11:31:37 +08001478clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001479 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001480 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301481 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001482 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1483 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001484
1485 return clk;
1486}
1487EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1488
Ritesh Harjanifec79672016-11-21 12:07:19 +05301489void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001490{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001491 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001492
Pierre Ossmand129bce2006-03-24 03:18:17 -08001493 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001494 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001495
Chris Ball27f6cb12009-09-22 16:45:31 -07001496 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001497 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001498 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001499 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001500 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001501 pr_err("%s: Internal clock never stabilised.\n",
1502 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001503 sdhci_dumpregs(host);
1504 return;
1505 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001506 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001507 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001508
1509 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001510 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001511}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301512EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1513
1514void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1515{
1516 u16 clk;
1517
1518 host->mmc->actual_clock = 0;
1519
1520 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1521
1522 if (clock == 0)
1523 return;
1524
1525 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1526 sdhci_enable_clk(host, clk);
1527}
Russell King17710592014-04-25 12:58:55 +01001528EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001529
Adrian Hunter1dceb042016-03-29 12:45:43 +03001530static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1531 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001532{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001533 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001534
Adrian Hunter1dceb042016-03-29 12:45:43 +03001535 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001536
1537 if (mode != MMC_POWER_OFF)
1538 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1539 else
1540 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1541}
1542
Adrian Hunter606d3132016-10-05 12:11:22 +03001543void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1544 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001545{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001546 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001547
Russell King24fbb3c2014-04-25 13:00:06 +01001548 if (mode != MMC_POWER_OFF) {
1549 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001550 case MMC_VDD_165_195:
Andy Shevchenko2a609ab2018-01-11 15:51:58 +02001551 /*
1552 * Without a regulator, SDHCI does not support 2.0v
1553 * so we only get here if the driver deliberately
1554 * added the 2.0v range to ocr_avail. Map it to 1.8v
1555 * for the purpose of turning on the power.
1556 */
1557 case MMC_VDD_20_21:
Pierre Ossmanae628902009-05-03 20:45:03 +02001558 pwr = SDHCI_POWER_180;
1559 break;
1560 case MMC_VDD_29_30:
1561 case MMC_VDD_30_31:
1562 pwr = SDHCI_POWER_300;
1563 break;
1564 case MMC_VDD_32_33:
1565 case MMC_VDD_33_34:
1566 pwr = SDHCI_POWER_330;
1567 break;
1568 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001569 WARN(1, "%s: Invalid vdd %#x\n",
1570 mmc_hostname(host->mmc), vdd);
1571 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001572 }
1573 }
1574
1575 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001576 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001577
Pierre Ossmanae628902009-05-03 20:45:03 +02001578 host->pwr = pwr;
1579
1580 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001581 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001582 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1583 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001584 } else {
1585 /*
1586 * Spec says that we should clear the power reg before setting
1587 * a new value. Some controllers don't seem to like this though.
1588 */
1589 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1590 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001591
Russell Kinge921a8b2014-04-25 13:00:01 +01001592 /*
1593 * At least the Marvell CaFe chip gets confused if we set the
1594 * voltage and set turn on power at the same time, so set the
1595 * voltage first.
1596 */
1597 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1598 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001599
Russell Kinge921a8b2014-04-25 13:00:01 +01001600 pwr |= SDHCI_POWER_ON;
1601
Pierre Ossmanae628902009-05-03 20:45:03 +02001602 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1603
Russell Kinge921a8b2014-04-25 13:00:01 +01001604 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1605 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001606
Russell Kinge921a8b2014-04-25 13:00:01 +01001607 /*
1608 * Some controllers need an extra 10ms delay of 10ms before
1609 * they can apply clock after applying power
1610 */
1611 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1612 mdelay(10);
1613 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001614}
Adrian Hunter606d3132016-10-05 12:11:22 +03001615EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001616
Adrian Hunter606d3132016-10-05 12:11:22 +03001617void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1618 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001619{
Adrian Hunter606d3132016-10-05 12:11:22 +03001620 if (IS_ERR(host->mmc->supply.vmmc))
1621 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001622 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001623 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001624}
Adrian Hunter606d3132016-10-05 12:11:22 +03001625EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001626
Pierre Ossmand129bce2006-03-24 03:18:17 -08001627/*****************************************************************************\
1628 * *
1629 * MMC callbacks *
1630 * *
1631\*****************************************************************************/
1632
Aapo Vienamod462c1b2018-08-20 12:23:32 +03001633void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001634{
1635 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001636 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001637 unsigned long flags;
1638
1639 host = mmc_priv(mmc);
1640
Scott Branden04e079cf2015-03-10 11:35:10 -07001641 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001642 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001643
Pierre Ossmand129bce2006-03-24 03:18:17 -08001644 spin_lock_irqsave(&host->lock, flags);
1645
Adrian Hunter061d17a2016-04-12 14:25:09 +03001646 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001647
1648 /*
1649 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1650 * requests if Auto-CMD12 is enabled.
1651 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001652 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001653 if (mrq->stop) {
1654 mrq->data->stop = NULL;
1655 mrq->stop = NULL;
1656 }
1657 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001658
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001659 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001660 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001661 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301662 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001663 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001664 sdhci_send_command(host, mrq->sbc);
1665 else
1666 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301667 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001668
Pierre Ossman5f25a662006-10-04 02:15:39 -07001669 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001670 spin_unlock_irqrestore(&host->lock, flags);
1671}
Aapo Vienamod462c1b2018-08-20 12:23:32 +03001672EXPORT_SYMBOL_GPL(sdhci_request);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001673
Russell King2317f562014-04-25 12:57:07 +01001674void sdhci_set_bus_width(struct sdhci_host *host, int width)
1675{
1676 u8 ctrl;
1677
1678 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1679 if (width == MMC_BUS_WIDTH_8) {
1680 ctrl &= ~SDHCI_CTRL_4BITBUS;
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001681 ctrl |= SDHCI_CTRL_8BITBUS;
Russell King2317f562014-04-25 12:57:07 +01001682 } else {
Michał Mirosław98f94ea2017-08-14 22:00:24 +02001683 if (host->mmc->caps & MMC_CAP_8_BIT_DATA)
Russell King2317f562014-04-25 12:57:07 +01001684 ctrl &= ~SDHCI_CTRL_8BITBUS;
1685 if (width == MMC_BUS_WIDTH_4)
1686 ctrl |= SDHCI_CTRL_4BITBUS;
1687 else
1688 ctrl &= ~SDHCI_CTRL_4BITBUS;
1689 }
1690 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1691}
1692EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1693
Russell King96d7b782014-04-25 12:59:26 +01001694void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1695{
1696 u16 ctrl_2;
1697
1698 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1699 /* Select Bus Speed Mode for host */
1700 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1701 if ((timing == MMC_TIMING_MMC_HS200) ||
1702 (timing == MMC_TIMING_UHS_SDR104))
1703 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1704 else if (timing == MMC_TIMING_UHS_SDR12)
1705 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1706 else if (timing == MMC_TIMING_UHS_SDR25)
1707 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1708 else if (timing == MMC_TIMING_UHS_SDR50)
1709 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1710 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1711 (timing == MMC_TIMING_MMC_DDR52))
1712 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001713 else if (timing == MMC_TIMING_MMC_HS400)
1714 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001715 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1716}
1717EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1718
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001719void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001720{
Dong Aishengded97e02016-04-16 01:29:25 +08001721 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001722 u8 ctrl;
1723
Adrian Hunter84ec0482016-12-19 15:33:11 +02001724 if (ios->power_mode == MMC_POWER_UNDEFINED)
1725 return;
1726
Adrian Hunterceb61432011-12-27 15:48:41 +02001727 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001728 if (!IS_ERR(mmc->supply.vmmc) &&
1729 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001730 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001731 return;
1732 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001733
Pierre Ossmand129bce2006-03-24 03:18:17 -08001734 /*
1735 * Reset the chip on each power off.
1736 * Should clear out any weird states.
1737 */
1738 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001739 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001740 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001741 }
1742
Kevin Liu52983382013-01-31 11:31:37 +08001743 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001744 (ios->power_mode == MMC_POWER_UP) &&
1745 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001746 sdhci_enable_preset_value(host, false);
1747
Russell King373073e2014-04-25 12:58:45 +01001748 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001749 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001750 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001751
1752 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1753 host->clock) {
1754 host->timeout_clk = host->mmc->actual_clock ?
1755 host->mmc->actual_clock / 1000 :
1756 host->clock / 1000;
1757 host->mmc->max_busy_timeout =
1758 host->ops->get_max_timeout_count ?
1759 host->ops->get_max_timeout_count(host) :
1760 1 << 27;
1761 host->mmc->max_busy_timeout /= host->timeout_clk;
1762 }
Russell King373073e2014-04-25 12:58:45 +01001763 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764
Adrian Hunter606d3132016-10-05 12:11:22 +03001765 if (host->ops->set_power)
1766 host->ops->set_power(host, ios->power_mode, ios->vdd);
1767 else
1768 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001769
Philip Rakity643a81f2010-09-23 08:24:32 -07001770 if (host->ops->platform_send_init_74_clocks)
1771 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1772
Russell King2317f562014-04-25 12:57:07 +01001773 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001774
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001775 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001776
yangbo lu501639b2017-08-15 10:16:47 +08001777 if (!(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT)) {
1778 if (ios->timing == MMC_TIMING_SD_HS ||
1779 ios->timing == MMC_TIMING_MMC_HS ||
1780 ios->timing == MMC_TIMING_MMC_HS400 ||
1781 ios->timing == MMC_TIMING_MMC_HS200 ||
1782 ios->timing == MMC_TIMING_MMC_DDR52 ||
1783 ios->timing == MMC_TIMING_UHS_SDR50 ||
1784 ios->timing == MMC_TIMING_UHS_SDR104 ||
1785 ios->timing == MMC_TIMING_UHS_DDR50 ||
1786 ios->timing == MMC_TIMING_UHS_SDR25)
1787 ctrl |= SDHCI_CTRL_HISPD;
1788 else
1789 ctrl &= ~SDHCI_CTRL_HISPD;
1790 }
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001791
Arindam Nathd6d50a12011-05-05 12:18:59 +05301792 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301793 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301794
Russell Kingda91a8f2014-04-25 13:00:12 +01001795 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301796 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301797 /*
1798 * We only need to set Driver Strength if the
1799 * preset value enable is not set.
1800 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001801 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301802 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1803 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1804 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001805 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1806 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301807 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1808 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001809 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1810 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1811 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001812 pr_warn("%s: invalid driver type, default to driver type B\n",
1813 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001814 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1815 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301816
1817 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301818 } else {
1819 /*
1820 * According to SDHC Spec v3.00, if the Preset Value
1821 * Enable in the Host Control 2 register is set, we
1822 * need to reset SD Clock Enable before changing High
1823 * Speed Enable to avoid generating clock gliches.
1824 */
Arindam Nath758535c2011-05-05 12:19:00 +05301825
1826 /* Reset SD Clock Enable */
1827 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1828 clk &= ~SDHCI_CLOCK_CARD_EN;
1829 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1830
1831 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1832
1833 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001834 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301835 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301836
Arindam Nath49c468f2011-05-05 12:19:01 +05301837 /* Reset SD Clock Enable */
1838 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1839 clk &= ~SDHCI_CLOCK_CARD_EN;
1840 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1841
Russell King96d7b782014-04-25 12:59:26 +01001842 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001843 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301844
Kevin Liu52983382013-01-31 11:31:37 +08001845 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1846 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1847 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1848 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1849 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001850 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1851 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001852 u16 preset;
1853
1854 sdhci_enable_preset_value(host, true);
1855 preset = sdhci_get_preset_value(host);
1856 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1857 >> SDHCI_PRESET_DRV_SHIFT;
1858 }
1859
Arindam Nath49c468f2011-05-05 12:19:01 +05301860 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001861 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301862 } else
1863 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301864
Leandro Dorileob8352262007-07-25 23:47:04 +02001865 /*
1866 * Some (ENE) controllers go apeshit on some ios operation,
1867 * signalling timeout and CRC errors even on CMD0. Resetting
1868 * it on each ios seems to solve the problem.
1869 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301870 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001871 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001872
Pierre Ossman5f25a662006-10-04 02:15:39 -07001873 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001874}
Hu Ziji6a6d4ce2017-03-30 17:22:55 +02001875EXPORT_SYMBOL_GPL(sdhci_set_ios);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001876
Dong Aishengded97e02016-04-16 01:29:25 +08001877static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001878{
1879 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001880 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001881
1882 if (host->flags & SDHCI_DEVICE_DEAD)
1883 return 0;
1884
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001885 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001886 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001887 return 1;
1888
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001889 /*
1890 * Try slot gpio detect, if defined it take precedence
1891 * over build in controller functionality
1892 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001893 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001894 return !!gpio_cd;
1895
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001896 /* If polling, assume that the card is always present. */
1897 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1898 return 1;
1899
Kevin Liu94144a42013-02-28 17:35:53 +08001900 /* Host native card detect */
1901 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1902}
1903
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001904static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001905{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001906 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001907 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001908
Pierre Ossmand129bce2006-03-24 03:18:17 -08001909 spin_lock_irqsave(&host->lock, flags);
1910
Pierre Ossman1e728592008-04-16 19:13:13 +02001911 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001912 is_readonly = 0;
1913 else if (host->ops->get_ro)
1914 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001915 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001916 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1917 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001918
1919 spin_unlock_irqrestore(&host->lock, flags);
1920
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001921 /* This quirk needs to be replaced by a callback-function later */
1922 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1923 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001924}
1925
Takashi Iwai82b0e232011-04-21 20:26:38 +02001926#define SAMPLE_COUNT 5
1927
Dong Aishengded97e02016-04-16 01:29:25 +08001928static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001929{
Dong Aishengded97e02016-04-16 01:29:25 +08001930 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001931 int i, ro_count;
1932
Takashi Iwai82b0e232011-04-21 20:26:38 +02001933 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001934 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001935
1936 ro_count = 0;
1937 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001938 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001939 if (++ro_count > SAMPLE_COUNT / 2)
1940 return 1;
1941 }
1942 msleep(30);
1943 }
1944 return 0;
1945}
1946
Adrian Hunter20758b62011-08-29 16:42:12 +03001947static void sdhci_hw_reset(struct mmc_host *mmc)
1948{
1949 struct sdhci_host *host = mmc_priv(mmc);
1950
1951 if (host->ops && host->ops->hw_reset)
1952 host->ops->hw_reset(host);
1953}
1954
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001955static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1956{
Russell Kingbe138552014-04-25 12:55:56 +01001957 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001958 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001959 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001960 else
Russell Kingb537f942014-04-25 12:56:01 +01001961 host->ier &= ~SDHCI_INT_CARD_INT;
1962
1963 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1964 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001965 mmiowb();
1966 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001967}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001968
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001969void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001970{
1971 struct sdhci_host *host = mmc_priv(mmc);
1972 unsigned long flags;
1973
Hans de Goede923713b2017-03-26 13:14:45 +02001974 if (enable)
1975 pm_runtime_get_noresume(host->mmc->parent);
1976
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001977 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001978 if (enable)
1979 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1980 else
1981 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1982
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001983 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001984 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001985
1986 if (!enable)
1987 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001988}
Hu Ziji2f05b6ab2017-03-30 17:22:57 +02001989EXPORT_SYMBOL_GPL(sdhci_enable_sdio_irq);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001990
Hu Zijic376ea92017-03-30 17:22:56 +02001991int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1992 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001993{
Dong Aishengded97e02016-04-16 01:29:25 +08001994 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001995 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001996 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001997
1998 /*
1999 * Signal Voltage Switching is only applicable for Host Controllers
2000 * v3.00 and above.
2001 */
2002 if (host->version < SDHCI_SPEC_300)
2003 return 0;
2004
Philip Rakity6231f3d2012-07-23 15:56:23 -07002005 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08002006
Fabio Estevam21f59982013-02-14 10:35:03 -02002007 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08002008 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002009 if (!(host->flags & SDHCI_SIGNALING_330))
2010 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08002011 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
2012 ctrl &= ~SDHCI_CTRL_VDD_180;
2013 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2014
Tim Kryger3a48edc2014-06-13 10:13:56 -07002015 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002016 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002017 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002018 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
2019 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002020 return -EIO;
2021 }
2022 }
2023 /* Wait for 5ms */
2024 usleep_range(5000, 5500);
2025
2026 /* 3.3V regulator output should be stable within 5 ms */
2027 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2028 if (!(ctrl & SDHCI_CTRL_VDD_180))
2029 return 0;
2030
Joe Perches66061102014-09-12 14:56:56 -07002031 pr_warn("%s: 3.3V regulator output did not became stable\n",
2032 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002033
2034 return -EAGAIN;
2035 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002036 if (!(host->flags & SDHCI_SIGNALING_180))
2037 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002038 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002039 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002040 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002041 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
2042 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002043 return -EIO;
2044 }
2045 }
2046
2047 /*
2048 * Enable 1.8V Signal Enable in the Host Control2
2049 * register
2050 */
2051 ctrl |= SDHCI_CTRL_VDD_180;
2052 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2053
Vincent Yang9d967a62015-01-20 16:05:15 +08002054 /* Some controller need to do more when switching */
2055 if (host->ops->voltage_switch)
2056 host->ops->voltage_switch(host);
2057
Kevin Liu20b92a32012-12-17 19:29:26 +08002058 /* 1.8V regulator output should be stable within 5 ms */
2059 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2060 if (ctrl & SDHCI_CTRL_VDD_180)
2061 return 0;
2062
Joe Perches66061102014-09-12 14:56:56 -07002063 pr_warn("%s: 1.8V regulator output did not became stable\n",
2064 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002065
2066 return -EAGAIN;
2067 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002068 if (!(host->flags & SDHCI_SIGNALING_120))
2069 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07002070 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08002071 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08002072 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07002073 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
2074 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08002075 return -EIO;
2076 }
2077 }
2078 return 0;
2079 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05302080 /* No signal voltage switch required */
2081 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08002082 }
Arindam Nathf2119df2011-05-05 12:18:57 +05302083}
Hu Zijic376ea92017-03-30 17:22:56 +02002084EXPORT_SYMBOL_GPL(sdhci_start_signal_voltage_switch);
Arindam Nathf2119df2011-05-05 12:18:57 +05302085
Kevin Liu20b92a32012-12-17 19:29:26 +08002086static int sdhci_card_busy(struct mmc_host *mmc)
2087{
2088 struct sdhci_host *host = mmc_priv(mmc);
2089 u32 present_state;
2090
Adrian Huntere613cc42016-06-23 14:00:58 +03002091 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08002092 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08002093
Adrian Huntere613cc42016-06-23 14:00:58 +03002094 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08002095}
2096
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002097static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
2098{
2099 struct sdhci_host *host = mmc_priv(mmc);
2100 unsigned long flags;
2101
2102 spin_lock_irqsave(&host->lock, flags);
2103 host->flags |= SDHCI_HS400_TUNING;
2104 spin_unlock_irqrestore(&host->lock, flags);
2105
2106 return 0;
2107}
2108
ernest.zhang6663c412018-07-16 14:26:53 +08002109void sdhci_start_tuning(struct sdhci_host *host)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002110{
2111 u16 ctrl;
2112
2113 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2114 ctrl |= SDHCI_CTRL_EXEC_TUNING;
2115 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
2116 ctrl |= SDHCI_CTRL_TUNED_CLK;
2117 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2118
2119 /*
2120 * As per the Host Controller spec v3.00, tuning command
2121 * generates Buffer Read Ready interrupt, so enable that.
2122 *
2123 * Note: The spec clearly says that when tuning sequence
2124 * is being performed, the controller does not generate
2125 * interrupts other than Buffer Read Ready interrupt. But
2126 * to make sure we don't hit a controller bug, we _only_
2127 * enable Buffer Read Ready interrupt here.
2128 */
2129 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2130 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
2131}
ernest.zhang6663c412018-07-16 14:26:53 +08002132EXPORT_SYMBOL_GPL(sdhci_start_tuning);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002133
ernest.zhang6663c412018-07-16 14:26:53 +08002134void sdhci_end_tuning(struct sdhci_host *host)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002135{
2136 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2137 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
2138}
ernest.zhang6663c412018-07-16 14:26:53 +08002139EXPORT_SYMBOL_GPL(sdhci_end_tuning);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002140
ernest.zhang6663c412018-07-16 14:26:53 +08002141void sdhci_reset_tuning(struct sdhci_host *host)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002142{
2143 u16 ctrl;
2144
2145 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2146 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2147 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2148 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2149}
ernest.zhang6663c412018-07-16 14:26:53 +08002150EXPORT_SYMBOL_GPL(sdhci_reset_tuning);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002151
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002152static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002153{
2154 sdhci_reset_tuning(host);
2155
2156 sdhci_do_reset(host, SDHCI_RESET_CMD);
2157 sdhci_do_reset(host, SDHCI_RESET_DATA);
2158
2159 sdhci_end_tuning(host);
2160
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002161 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002162}
2163
2164/*
2165 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2166 * tuning command does not have a data payload (or rather the hardware does it
2167 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2168 * interrupt setup is different to other commands and there is no timeout
2169 * interrupt so special handling is needed.
2170 */
ernest.zhang6663c412018-07-16 14:26:53 +08002171void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002172{
2173 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002174 struct mmc_command cmd = {};
2175 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002176 unsigned long flags;
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002177 u32 b = host->sdma_boundary;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002178
2179 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002180
2181 cmd.opcode = opcode;
2182 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2183 cmd.mrq = &mrq;
2184
2185 mrq.cmd = &cmd;
2186 /*
2187 * In response to CMD19, the card sends 64 bytes of tuning
2188 * block to the Host Controller. So we set the block size
2189 * to 64 here.
2190 */
Adrian Hunter85336102016-12-02 15:14:26 +02002191 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2192 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002193 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 128), SDHCI_BLOCK_SIZE);
Adrian Hunter85336102016-12-02 15:14:26 +02002194 else
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02002195 sdhci_writew(host, SDHCI_MAKE_BLKSZ(b, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002196
2197 /*
2198 * The tuning block is sent by the card to the host controller.
2199 * So we set the TRNS_READ bit in the Transfer Mode register.
2200 * This also takes care of setting DMA Enable and Multi Block
2201 * Select in the same register to 0.
2202 */
2203 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2204
2205 sdhci_send_command(host, &cmd);
2206
2207 host->cmd = NULL;
2208
2209 sdhci_del_timer(host, &mrq);
2210
2211 host->tuning_done = 0;
2212
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002213 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002214 spin_unlock_irqrestore(&host->lock, flags);
2215
2216 /* Wait for Buffer Read Ready interrupt */
2217 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2218 msecs_to_jiffies(50));
2219
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002220}
ernest.zhang6663c412018-07-16 14:26:53 +08002221EXPORT_SYMBOL_GPL(sdhci_send_tuning);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002222
Yinbo Zhu7d8bb1f2018-08-23 16:48:31 +08002223static int __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002224{
2225 int i;
2226
2227 /*
2228 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2229 * of loops reaches 40 times.
2230 */
2231 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2232 u16 ctrl;
2233
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002234 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002235
2236 if (!host->tuning_done) {
2237 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2238 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002239 sdhci_abort_tuning(host, opcode);
Yinbo Zhu7d8bb1f2018-08-23 16:48:31 +08002240 return -ETIMEDOUT;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002241 }
2242
2243 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2244 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2245 if (ctrl & SDHCI_CTRL_TUNED_CLK)
Yinbo Zhu7d8bb1f2018-08-23 16:48:31 +08002246 return 0; /* Success! */
Adrian Hunter6b11e702016-12-02 15:14:27 +02002247 break;
2248 }
2249
Adrian Hunter83b600b2017-04-20 16:14:43 +08002250 /* Spec does not require a delay between tuning cycles */
2251 if (host->tuning_delay > 0)
2252 mdelay(host->tuning_delay);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002253 }
2254
2255 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2256 mmc_hostname(host->mmc));
2257 sdhci_reset_tuning(host);
Yinbo Zhu7d8bb1f2018-08-23 16:48:31 +08002258 return -EAGAIN;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002259}
2260
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002261int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302262{
Russell King4b6f37d2014-04-25 12:59:36 +01002263 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302264 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002265 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002266 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302267
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002268 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002269
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002270 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2271 tuning_count = host->tuning_count;
2272
Arindam Nathb513ea22011-05-05 12:19:04 +05302273 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002274 * The Host Controller needs tuning in case of SDR104 and DDR50
2275 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2276 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302277 * If the Host Controller supports the HS200 mode then the
2278 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302279 */
Russell King4b6f37d2014-04-25 12:59:36 +01002280 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002281 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002282 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002283 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002284 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002285
Russell King4b6f37d2014-04-25 12:59:36 +01002286 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002287 /*
2288 * Periodic re-tuning for HS400 is not expected to be needed, so
2289 * disable it here.
2290 */
2291 if (hs400_tuning)
2292 tuning_count = 0;
2293 break;
2294
Russell King4b6f37d2014-04-25 12:59:36 +01002295 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002296 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002297 break;
Girish K S069c9f12012-01-06 09:56:39 +05302298
Russell King4b6f37d2014-04-25 12:59:36 +01002299 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002300 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002301 break;
2302 /* FALLTHROUGH */
2303
2304 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002305 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302306 }
2307
Dong Aisheng45251812013-09-13 19:11:30 +08002308 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302309 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002310 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002311 }
2312
Adrian Hunter6b11e702016-12-02 15:14:27 +02002313 host->mmc->retune_period = tuning_count;
2314
Adrian Hunter83b600b2017-04-20 16:14:43 +08002315 if (host->tuning_delay < 0)
2316 host->tuning_delay = opcode == MMC_SEND_TUNING_BLOCK;
2317
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002318 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302319
Yinbo Zhu7d8bb1f2018-08-23 16:48:31 +08002320 host->tuning_err = __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302321
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002322 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002323out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302324 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002325
Arindam Nathb513ea22011-05-05 12:19:04 +05302326 return err;
2327}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002328EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302329
Kevin Liu52983382013-01-31 11:31:37 +08002330static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302331{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302332 /* Host Controller v3.00 defines preset value registers */
2333 if (host->version < SDHCI_SPEC_300)
2334 return;
2335
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302336 /*
2337 * We only enable or disable Preset Value if they are not already
2338 * enabled or disabled respectively. Otherwise, we bail out.
2339 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002340 if (host->preset_enabled != enable) {
2341 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2342
2343 if (enable)
2344 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2345 else
2346 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2347
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302348 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002349
2350 if (enable)
2351 host->flags |= SDHCI_PV_ENABLED;
2352 else
2353 host->flags &= ~SDHCI_PV_ENABLED;
2354
2355 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302356 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002357}
2358
Haibo Chen348487c2014-12-09 17:04:05 +08002359static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2360 int err)
2361{
2362 struct sdhci_host *host = mmc_priv(mmc);
2363 struct mmc_data *data = mrq->data;
2364
Russell Kingf48f0392016-01-26 13:40:32 +00002365 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002366 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Heiner Kallweitfeeef092017-03-26 20:45:56 +02002367 mmc_get_dma_dir(data));
Russell King771a3dc2016-01-26 13:40:53 +00002368
2369 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002370}
2371
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002372static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002373{
2374 struct sdhci_host *host = mmc_priv(mmc);
2375
Haibo Chend31911b2015-08-25 10:02:11 +08002376 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002377
Linus Walleijbd9b9022018-01-29 00:44:53 +01002378 /*
2379 * No pre-mapping in the pre hook if we're using the bounce buffer,
2380 * for that we would need two bounce buffers since one buffer is
2381 * in flight when this is getting called.
2382 */
2383 if (host->flags & SDHCI_REQ_USE_DMA && !host->bounce_buffer)
Russell King94538e52016-01-26 13:40:37 +00002384 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002385}
2386
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002387static inline bool sdhci_has_requests(struct sdhci_host *host)
2388{
2389 return host->cmd || host->data_cmd;
2390}
2391
2392static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2393{
2394 if (host->data_cmd) {
2395 host->data_cmd->error = err;
2396 sdhci_finish_mrq(host, host->data_cmd->mrq);
2397 }
2398
2399 if (host->cmd) {
2400 host->cmd->error = err;
2401 sdhci_finish_mrq(host, host->cmd->mrq);
2402 }
2403}
2404
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002405static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002406{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002407 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002408 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002409 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002410
Christian Daudt722e1282013-06-20 14:26:36 -07002411 /* First check if client has provided their own card event */
2412 if (host->ops->card_event)
2413 host->ops->card_event(host);
2414
Adrian Hunterd3940f22016-06-29 16:24:14 +03002415 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002416
Pierre Ossmand129bce2006-03-24 03:18:17 -08002417 spin_lock_irqsave(&host->lock, flags);
2418
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002419 /* Check sdhci_has_requests() first in case we are runtime suspended */
2420 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302421 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002422 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302423 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002424 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002425
Russell King03231f92014-04-25 12:57:12 +01002426 sdhci_do_reset(host, SDHCI_RESET_CMD);
2427 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002428
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002429 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 }
2431
2432 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002433}
2434
2435static const struct mmc_host_ops sdhci_ops = {
2436 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002437 .post_req = sdhci_post_req,
2438 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002439 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002440 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002441 .get_ro = sdhci_get_ro,
2442 .hw_reset = sdhci_hw_reset,
2443 .enable_sdio_irq = sdhci_enable_sdio_irq,
2444 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002445 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002446 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002447 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002448 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002449};
2450
2451/*****************************************************************************\
2452 * *
2453 * Tasklets *
2454 * *
2455\*****************************************************************************/
2456
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002457static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002458{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 unsigned long flags;
2460 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002461 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002462
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002463 spin_lock_irqsave(&host->lock, flags);
2464
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002465 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2466 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002467 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002468 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002469 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002471 if (!mrq) {
2472 spin_unlock_irqrestore(&host->lock, flags);
2473 return true;
2474 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002475
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002476 sdhci_del_timer(host, mrq);
2477
Pierre Ossmand129bce2006-03-24 03:18:17 -08002478 /*
Russell King054cedf2016-01-26 13:40:42 +00002479 * Always unmap the data buffers if they were mapped by
2480 * sdhci_prepare_data() whenever we finish with a request.
2481 * This avoids leaking DMA mappings on error.
2482 */
2483 if (host->flags & SDHCI_REQ_USE_DMA) {
2484 struct mmc_data *data = mrq->data;
2485
2486 if (data && data->host_cookie == COOKIE_MAPPED) {
Linus Walleijbd9b9022018-01-29 00:44:53 +01002487 if (host->bounce_buffer) {
2488 /*
2489 * On reads, copy the bounced data into the
2490 * sglist
2491 */
2492 if (mmc_get_dma_dir(data) == DMA_FROM_DEVICE) {
2493 unsigned int length = data->bytes_xfered;
2494
2495 if (length > host->bounce_buffer_size) {
2496 pr_err("%s: bounce buffer is %u bytes but DMA claims to have transferred %u bytes\n",
2497 mmc_hostname(host->mmc),
2498 host->bounce_buffer_size,
2499 data->bytes_xfered);
2500 /* Cap it down and continue */
2501 length = host->bounce_buffer_size;
2502 }
2503 dma_sync_single_for_cpu(
2504 host->mmc->parent,
2505 host->bounce_addr,
2506 host->bounce_buffer_size,
2507 DMA_FROM_DEVICE);
2508 sg_copy_from_buffer(data->sg,
2509 data->sg_len,
2510 host->bounce_buffer,
2511 length);
2512 } else {
2513 /* No copying, just switch ownership */
2514 dma_sync_single_for_cpu(
2515 host->mmc->parent,
2516 host->bounce_addr,
2517 host->bounce_buffer_size,
2518 mmc_get_dma_dir(data));
2519 }
2520 } else {
2521 /* Unmap the raw data */
2522 dma_unmap_sg(mmc_dev(host->mmc), data->sg,
2523 data->sg_len,
2524 mmc_get_dma_dir(data));
2525 }
Russell King054cedf2016-01-26 13:40:42 +00002526 data->host_cookie = COOKIE_UNMAPPED;
2527 }
2528 }
2529
2530 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002531 * The controller needs a reset of internal state machines
2532 * upon error conditions.
2533 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002534 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002535 /*
2536 * Do not finish until command and data lines are available for
2537 * reset. Note there can only be one other mrq, so it cannot
2538 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2539 * would both be null.
2540 */
2541 if (host->cmd || host->data_cmd) {
2542 spin_unlock_irqrestore(&host->lock, flags);
2543 return true;
2544 }
2545
Pierre Ossman645289d2006-06-30 02:22:33 -07002546 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002547 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002548 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002549 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002550
2551 /* Spec says we should do both at the same time, but Ricoh
2552 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002553 sdhci_do_reset(host, SDHCI_RESET_CMD);
2554 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002555
2556 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002557 }
2558
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002559 if (!sdhci_has_requests(host))
2560 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002561
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002562 host->mrqs_done[i] = NULL;
2563
Pierre Ossman5f25a662006-10-04 02:15:39 -07002564 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002565 spin_unlock_irqrestore(&host->lock, flags);
2566
2567 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002568
2569 return false;
2570}
2571
2572static void sdhci_tasklet_finish(unsigned long param)
2573{
2574 struct sdhci_host *host = (struct sdhci_host *)param;
2575
2576 while (!sdhci_request_done(host))
2577 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002578}
2579
Kees Cook2ee4f622017-10-24 08:03:45 -07002580static void sdhci_timeout_timer(struct timer_list *t)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581{
2582 struct sdhci_host *host;
2583 unsigned long flags;
2584
Kees Cook2ee4f622017-10-24 08:03:45 -07002585 host = from_timer(host, t, timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002586
2587 spin_lock_irqsave(&host->lock, flags);
2588
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002589 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2590 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2591 mmc_hostname(host->mmc));
2592 sdhci_dumpregs(host);
2593
2594 host->cmd->error = -ETIMEDOUT;
2595 sdhci_finish_mrq(host, host->cmd->mrq);
2596 }
2597
2598 mmiowb();
2599 spin_unlock_irqrestore(&host->lock, flags);
2600}
2601
Kees Cook2ee4f622017-10-24 08:03:45 -07002602static void sdhci_timeout_data_timer(struct timer_list *t)
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002603{
2604 struct sdhci_host *host;
2605 unsigned long flags;
2606
Kees Cook2ee4f622017-10-24 08:03:45 -07002607 host = from_timer(host, t, data_timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002608
2609 spin_lock_irqsave(&host->lock, flags);
2610
2611 if (host->data || host->data_cmd ||
2612 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002613 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2614 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002615 sdhci_dumpregs(host);
2616
2617 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002618 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002619 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002620 } else if (host->data_cmd) {
2621 host->data_cmd->error = -ETIMEDOUT;
2622 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002623 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002624 host->cmd->error = -ETIMEDOUT;
2625 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626 }
2627 }
2628
Pierre Ossman5f25a662006-10-04 02:15:39 -07002629 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002630 spin_unlock_irqrestore(&host->lock, flags);
2631}
2632
2633/*****************************************************************************\
2634 * *
2635 * Interrupt handling *
2636 * *
2637\*****************************************************************************/
2638
Adrian Hunterfc605f12016-10-05 12:11:21 +03002639static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002640{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002641 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002642 /*
2643 * SDHCI recovers from errors by resetting the cmd and data
2644 * circuits. Until that is done, there very well might be more
2645 * interrupts, so ignore them in that case.
2646 */
2647 if (host->pending_reset)
2648 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002649 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2650 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002651 sdhci_dumpregs(host);
2652 return;
2653 }
2654
Russell Kingec014cb2016-01-26 13:39:39 +00002655 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2656 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2657 if (intmask & SDHCI_INT_TIMEOUT)
2658 host->cmd->error = -ETIMEDOUT;
2659 else
2660 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002661
Russell King71fcbda2016-01-26 13:39:45 +00002662 /*
2663 * If this command initiates a data phase and a response
2664 * CRC error is signalled, the card can start transferring
2665 * data - the card may have received the command without
2666 * error. We must not terminate the mmc_request early.
2667 *
2668 * If the card did not receive the command or returned an
2669 * error which prevented it sending data, the data phase
2670 * will time out.
2671 */
2672 if (host->cmd->data &&
2673 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2674 SDHCI_INT_CRC) {
2675 host->cmd = NULL;
2676 return;
2677 }
2678
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002679 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002680 return;
2681 }
2682
Pierre Ossmane8095172008-07-25 01:09:08 +02002683 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002684 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002685}
2686
Adrian Hunter08621b12014-11-04 12:42:38 +02002687static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002688{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002689 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002690
2691 sdhci_dumpregs(host);
2692
2693 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002694 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002695
Adrian Huntere57a5f62014-11-04 12:42:46 +02002696 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002697 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2698 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002699 le32_to_cpu(dma_desc->addr_lo),
2700 le16_to_cpu(dma_desc->len),
2701 le16_to_cpu(dma_desc->cmd));
2702 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002703 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2704 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002705 le16_to_cpu(dma_desc->len),
2706 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002707
Adrian Hunter76fe3792014-11-04 12:42:42 +02002708 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002709
Adrian Hunter05452302014-11-04 12:42:45 +02002710 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002711 break;
2712 }
2713}
Ben Dooks6882a8c2009-06-14 13:52:38 +01002714
Pierre Ossmand129bce2006-03-24 03:18:17 -08002715static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2716{
Girish K S069c9f12012-01-06 09:56:39 +05302717 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002718
Arindam Nathb513ea22011-05-05 12:19:04 +05302719 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2720 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302721 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2722 if (command == MMC_SEND_TUNING_BLOCK ||
2723 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302724 host->tuning_done = 1;
2725 wake_up(&host->buf_ready_int);
2726 return;
2727 }
2728 }
2729
Pierre Ossmand129bce2006-03-24 03:18:17 -08002730 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002731 struct mmc_command *data_cmd = host->data_cmd;
2732
Pierre Ossmand129bce2006-03-24 03:18:17 -08002733 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002734 * The "data complete" interrupt is also used to
2735 * indicate that a busy state has ended. See comment
2736 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002737 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002738 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002739 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002740 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002741 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002742 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002743 return;
2744 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002745 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002746 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002747 /*
2748 * Some cards handle busy-end interrupt
2749 * before the command completed, so make
2750 * sure we do things in the proper order.
2751 */
Adrian Hunterea968022016-06-29 16:24:24 +03002752 if (host->cmd == data_cmd)
2753 return;
2754
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002755 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002756 return;
2757 }
2758 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002759
Adrian Huntered1563d2016-06-29 16:24:29 +03002760 /*
2761 * SDHCI recovers from errors by resetting the cmd and data
2762 * circuits. Until that is done, there very well might be more
2763 * interrupts, so ignore them in that case.
2764 */
2765 if (host->pending_reset)
2766 return;
2767
Marek Vasut2e4456f2015-11-18 10:47:02 +01002768 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2769 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002770 sdhci_dumpregs(host);
2771
2772 return;
2773 }
2774
2775 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002776 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002777 else if (intmask & SDHCI_INT_DATA_END_BIT)
2778 host->data->error = -EILSEQ;
2779 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2780 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2781 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002782 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002783 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302784 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002785 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002786 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002787 if (host->ops->adma_workaround)
2788 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002789 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002790
Pierre Ossman17b04292007-07-22 22:18:46 +02002791 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002792 sdhci_finish_data(host);
2793 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002794 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002795 sdhci_transfer_pio(host);
2796
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002797 /*
2798 * We currently don't do anything fancy with DMA
2799 * boundaries, but as we can't disable the feature
2800 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002801 *
2802 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2803 * should return a valid address to continue from, but as
2804 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002805 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002806 if (intmask & SDHCI_INT_DMA_END) {
2807 u32 dmastart, dmanow;
Linus Walleijbd9b9022018-01-29 00:44:53 +01002808
2809 dmastart = sdhci_sdma_address(host);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002810 dmanow = dmastart + host->data->bytes_xfered;
2811 /*
2812 * Force update to the next DMA block boundary.
2813 */
2814 dmanow = (dmanow &
2815 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2816 SDHCI_DEFAULT_BOUNDARY_SIZE;
2817 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002818 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2819 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002820 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2821 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002822
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002823 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002824 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002825 /*
2826 * Data managed to finish before the
2827 * command completed. Make sure we do
2828 * things in the proper order.
2829 */
2830 host->data_early = 1;
2831 } else {
2832 sdhci_finish_data(host);
2833 }
2834 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835 }
2836}
2837
David Howells7d12e782006-10-05 14:55:46 +01002838static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002839{
Russell King781e9892014-04-25 12:55:46 +01002840 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002841 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002842 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002843 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002844
2845 spin_lock(&host->lock);
2846
Russell Kingbe138552014-04-25 12:55:56 +01002847 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002848 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002849 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002850 }
2851
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002852 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002853 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854 result = IRQ_NONE;
2855 goto out;
2856 }
2857
Russell King41005002014-04-25 12:55:36 +01002858 do {
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002859 DBG("IRQ status 0x%08x\n", intmask);
2860
2861 if (host->ops->irq) {
2862 intmask = host->ops->irq(host, intmask);
2863 if (!intmask)
2864 goto cont;
2865 }
2866
Russell King41005002014-04-25 12:55:36 +01002867 /* Clear selected interrupts. */
2868 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2869 SDHCI_INT_BUS_POWER);
2870 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002871
Russell King41005002014-04-25 12:55:36 +01002872 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2873 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2874 SDHCI_CARD_PRESENT;
2875
2876 /*
2877 * There is a observation on i.mx esdhc. INSERT
2878 * bit will be immediately set again when it gets
2879 * cleared, if a card is inserted. We have to mask
2880 * the irq to prevent interrupt storm which will
2881 * freeze the system. And the REMOVE gets the
2882 * same situation.
2883 *
2884 * More testing are needed here to ensure it works
2885 * for other platforms though.
2886 */
Russell Kingb537f942014-04-25 12:56:01 +01002887 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2888 SDHCI_INT_CARD_REMOVE);
2889 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2890 SDHCI_INT_CARD_INSERT;
2891 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2892 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002893
2894 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2895 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002896
2897 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2898 SDHCI_INT_CARD_REMOVE);
2899 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002900 }
2901
2902 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002903 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002904
2905 if (intmask & SDHCI_INT_DATA_MASK)
2906 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2907
2908 if (intmask & SDHCI_INT_BUS_POWER)
2909 pr_err("%s: Card is consuming too much power!\n",
2910 mmc_hostname(host->mmc));
2911
Dong Aishengf37b20e2016-07-12 15:46:17 +08002912 if (intmask & SDHCI_INT_RETUNE)
2913 mmc_retune_needed(host->mmc);
2914
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002915 if ((intmask & SDHCI_INT_CARD_INT) &&
2916 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002917 sdhci_enable_sdio_irq_nolock(host, false);
2918 host->thread_isr |= SDHCI_INT_CARD_INT;
2919 result = IRQ_WAKE_THREAD;
2920 }
Russell King41005002014-04-25 12:55:36 +01002921
2922 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2923 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2924 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002925 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002926
2927 if (intmask) {
2928 unexpected |= intmask;
2929 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2930 }
Adrian Hunterf12e39d2017-03-20 19:50:47 +02002931cont:
Russell King781e9892014-04-25 12:55:46 +01002932 if (result == IRQ_NONE)
2933 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002934
2935 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002936 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002937out:
2938 spin_unlock(&host->lock);
2939
Alexander Stein6379b232012-03-14 09:52:10 +01002940 if (unexpected) {
2941 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2942 mmc_hostname(host->mmc), unexpected);
2943 sdhci_dumpregs(host);
2944 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002945
Pierre Ossmand129bce2006-03-24 03:18:17 -08002946 return result;
2947}
2948
Russell King781e9892014-04-25 12:55:46 +01002949static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2950{
2951 struct sdhci_host *host = dev_id;
2952 unsigned long flags;
2953 u32 isr;
2954
2955 spin_lock_irqsave(&host->lock, flags);
2956 isr = host->thread_isr;
2957 host->thread_isr = 0;
2958 spin_unlock_irqrestore(&host->lock, flags);
2959
Russell King3560db82014-04-25 12:55:51 +01002960 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002961 struct mmc_host *mmc = host->mmc;
2962
2963 mmc->ops->card_event(mmc);
2964 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002965 }
2966
Russell King781e9892014-04-25 12:55:46 +01002967 if (isr & SDHCI_INT_CARD_INT) {
2968 sdio_run_irqs(host->mmc);
2969
2970 spin_lock_irqsave(&host->lock, flags);
2971 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2972 sdhci_enable_sdio_irq_nolock(host, true);
2973 spin_unlock_irqrestore(&host->lock, flags);
2974 }
2975
2976 return isr ? IRQ_HANDLED : IRQ_NONE;
2977}
2978
Pierre Ossmand129bce2006-03-24 03:18:17 -08002979/*****************************************************************************\
2980 * *
2981 * Suspend/resume *
2982 * *
2983\*****************************************************************************/
2984
2985#ifdef CONFIG_PM
Adrian Hunter9c316b32018-02-27 14:51:23 +02002986
2987static bool sdhci_cd_irq_can_wakeup(struct sdhci_host *host)
2988{
2989 return mmc_card_is_removable(host->mmc) &&
2990 !(host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
2991 !mmc_can_gpio_cd(host->mmc);
2992}
2993
Ludovic Desroches84d62602016-05-13 15:16:02 +02002994/*
2995 * To enable wakeup events, the corresponding events have to be enabled in
2996 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2997 * Table' in the SD Host Controller Standard Specification.
2998 * It is useless to restore SDHCI_INT_ENABLE state in
2999 * sdhci_disable_irq_wakeups() since it will be set by
3000 * sdhci_enable_card_detection() or sdhci_init().
3001 */
Adrian Hunter58e79b62018-01-09 09:52:21 +02003002static bool sdhci_enable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08003003{
Adrian Hunter81b14542018-01-09 09:52:22 +02003004 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE |
3005 SDHCI_WAKE_ON_INT;
3006 u32 irq_val = 0;
3007 u8 wake_val = 0;
Kevin Liuad080d72013-01-05 17:21:33 +08003008 u8 val;
Adrian Hunter81b14542018-01-09 09:52:22 +02003009
Adrian Hunter9c316b32018-02-27 14:51:23 +02003010 if (sdhci_cd_irq_can_wakeup(host)) {
Adrian Hunter81b14542018-01-09 09:52:22 +02003011 wake_val |= SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE;
3012 irq_val |= SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE;
3013 }
3014
Adrian Hunterd5d568f2018-02-27 14:51:24 +02003015 if (mmc_card_wake_sdio_irq(host->mmc)) {
3016 wake_val |= SDHCI_WAKE_ON_INT;
3017 irq_val |= SDHCI_INT_CARD_INT;
3018 }
3019
3020 if (!irq_val)
3021 return false;
Kevin Liuad080d72013-01-05 17:21:33 +08003022
3023 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
Adrian Hunter81b14542018-01-09 09:52:22 +02003024 val &= ~mask;
3025 val |= wake_val;
Kevin Liuad080d72013-01-05 17:21:33 +08003026 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Adrian Hunter81b14542018-01-09 09:52:22 +02003027
Ludovic Desroches84d62602016-05-13 15:16:02 +02003028 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Adrian Hunter58e79b62018-01-09 09:52:21 +02003029
3030 host->irq_wake_enabled = !enable_irq_wake(host->irq);
3031
3032 return host->irq_wake_enabled;
Kevin Liuad080d72013-01-05 17:21:33 +08003033}
Kevin Liuad080d72013-01-05 17:21:33 +08003034
Fabio Estevam0b10f472014-08-30 14:53:13 -03003035static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08003036{
3037 u8 val;
3038 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
3039 | SDHCI_WAKE_ON_INT;
3040
3041 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
3042 val &= ~mask;
3043 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Adrian Hunter58e79b62018-01-09 09:52:21 +02003044
3045 disable_irq_wake(host->irq);
3046
3047 host->irq_wake_enabled = false;
Kevin Liuad080d72013-01-05 17:21:33 +08003048}
Pierre Ossmand129bce2006-03-24 03:18:17 -08003049
Manuel Lauss29495aa2011-11-03 11:09:45 +01003050int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003051{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003052 sdhci_disable_card_detection(host);
3053
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003054 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303055
Adrian Hunter58e79b62018-01-09 09:52:21 +02003056 if (!device_may_wakeup(mmc_dev(host->mmc)) ||
3057 !sdhci_enable_irq_wakeups(host)) {
Russell Kingb537f942014-04-25 12:56:01 +01003058 host->ier = 0;
3059 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3060 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08003061 free_irq(host->irq, host);
Kevin Liuad080d72013-01-05 17:21:33 +08003062 }
Adrian Hunter58e79b62018-01-09 09:52:21 +02003063
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003064 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003065}
3066
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003067EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003068
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003069int sdhci_resume_host(struct sdhci_host *host)
3070{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003071 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02003072 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003073
Richard Röjforsa13abc72009-09-22 16:45:30 -07003074 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003075 if (host->ops->enable_dma)
3076 host->ops->enable_dma(host);
3077 }
3078
Adrian Hunter6308d292012-02-07 14:48:54 +02003079 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
3080 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
3081 /* Card keeps power but host controller does not */
3082 sdhci_init(host, 0);
3083 host->pwr = 0;
3084 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03003085 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02003086 } else {
3087 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
3088 mmiowb();
3089 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003090
Adrian Hunter58e79b62018-01-09 09:52:21 +02003091 if (host->irq_wake_enabled) {
3092 sdhci_disable_irq_wakeups(host);
3093 } else {
Haibo Chen14a7b41642015-09-15 18:32:58 +08003094 ret = request_threaded_irq(host->irq, sdhci_irq,
3095 sdhci_thread_irq, IRQF_SHARED,
3096 mmc_hostname(host->mmc), host);
3097 if (ret)
3098 return ret;
Haibo Chen14a7b41642015-09-15 18:32:58 +08003099 }
3100
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003101 sdhci_enable_card_detection(host);
3102
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08003103 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003104}
3105
3106EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003107
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003108int sdhci_runtime_suspend_host(struct sdhci_host *host)
3109{
3110 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003111
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03003112 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003113
3114 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01003115 host->ier &= SDHCI_INT_CARD_INT;
3116 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3117 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003118 spin_unlock_irqrestore(&host->lock, flags);
3119
Russell King781e9892014-04-25 12:55:46 +01003120 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003121
3122 spin_lock_irqsave(&host->lock, flags);
3123 host->runtime_suspended = true;
3124 spin_unlock_irqrestore(&host->lock, flags);
3125
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003126 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003127}
3128EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
3129
3130int sdhci_runtime_resume_host(struct sdhci_host *host)
3131{
Adrian Hunterd3940f22016-06-29 16:24:14 +03003132 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003133 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003134 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003135
3136 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
3137 if (host->ops->enable_dma)
3138 host->ops->enable_dma(host);
3139 }
3140
3141 sdhci_init(host, 0);
3142
Zhoujie Wu70bc85a2017-08-03 12:28:40 -07003143 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED &&
3144 mmc->ios.power_mode != MMC_POWER_OFF) {
Adrian Hunter84ec0482016-12-19 15:33:11 +02003145 /* Force clock and power re-program */
3146 host->pwr = 0;
3147 host->clock = 0;
3148 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
3149 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003150
Adrian Hunter84ec0482016-12-19 15:33:11 +02003151 if ((host_flags & SDHCI_PV_ENABLED) &&
3152 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
3153 spin_lock_irqsave(&host->lock, flags);
3154 sdhci_enable_preset_value(host, true);
3155 spin_unlock_irqrestore(&host->lock, flags);
3156 }
3157
3158 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
3159 mmc->ops->hs400_enhanced_strobe)
3160 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08003161 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003162
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003163 spin_lock_irqsave(&host->lock, flags);
3164
3165 host->runtime_suspended = false;
3166
3167 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01003168 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003169 sdhci_enable_sdio_irq_nolock(host, true);
3170
3171 /* Enable Card Detection */
3172 sdhci_enable_card_detection(host);
3173
3174 spin_unlock_irqrestore(&host->lock, flags);
3175
Markus Pargmann8a125ba2014-06-04 15:24:29 +02003176 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003177}
3178EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
3179
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01003180#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003181
Pierre Ossmand129bce2006-03-24 03:18:17 -08003182/*****************************************************************************\
3183 * *
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003184 * Command Queue Engine (CQE) helpers *
3185 * *
3186\*****************************************************************************/
3187
3188void sdhci_cqe_enable(struct mmc_host *mmc)
3189{
3190 struct sdhci_host *host = mmc_priv(mmc);
3191 unsigned long flags;
3192 u8 ctrl;
3193
3194 spin_lock_irqsave(&host->lock, flags);
3195
3196 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
3197 ctrl &= ~SDHCI_CTRL_DMA_MASK;
3198 if (host->flags & SDHCI_USE_64_BIT_DMA)
3199 ctrl |= SDHCI_CTRL_ADMA64;
3200 else
3201 ctrl |= SDHCI_CTRL_ADMA32;
3202 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
3203
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003204 sdhci_writew(host, SDHCI_MAKE_BLKSZ(host->sdma_boundary, 512),
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003205 SDHCI_BLOCK_SIZE);
3206
3207 /* Set maximum timeout */
3208 sdhci_writeb(host, 0xE, SDHCI_TIMEOUT_CONTROL);
3209
3210 host->ier = host->cqe_ier;
3211
3212 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
3213 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
3214
3215 host->cqe_on = true;
3216
3217 pr_debug("%s: sdhci: CQE on, IRQ mask %#x, IRQ status %#x\n",
3218 mmc_hostname(mmc), host->ier,
3219 sdhci_readl(host, SDHCI_INT_STATUS));
3220
3221 mmiowb();
3222 spin_unlock_irqrestore(&host->lock, flags);
3223}
3224EXPORT_SYMBOL_GPL(sdhci_cqe_enable);
3225
3226void sdhci_cqe_disable(struct mmc_host *mmc, bool recovery)
3227{
3228 struct sdhci_host *host = mmc_priv(mmc);
3229 unsigned long flags;
3230
3231 spin_lock_irqsave(&host->lock, flags);
3232
3233 sdhci_set_default_irqs(host);
3234
3235 host->cqe_on = false;
3236
3237 if (recovery) {
3238 sdhci_do_reset(host, SDHCI_RESET_CMD);
3239 sdhci_do_reset(host, SDHCI_RESET_DATA);
3240 }
3241
3242 pr_debug("%s: sdhci: CQE off, IRQ mask %#x, IRQ status %#x\n",
3243 mmc_hostname(mmc), host->ier,
3244 sdhci_readl(host, SDHCI_INT_STATUS));
3245
3246 mmiowb();
3247 spin_unlock_irqrestore(&host->lock, flags);
3248}
3249EXPORT_SYMBOL_GPL(sdhci_cqe_disable);
3250
3251bool sdhci_cqe_irq(struct sdhci_host *host, u32 intmask, int *cmd_error,
3252 int *data_error)
3253{
3254 u32 mask;
3255
3256 if (!host->cqe_on)
3257 return false;
3258
3259 if (intmask & (SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC))
3260 *cmd_error = -EILSEQ;
3261 else if (intmask & SDHCI_INT_TIMEOUT)
3262 *cmd_error = -ETIMEDOUT;
3263 else
3264 *cmd_error = 0;
3265
3266 if (intmask & (SDHCI_INT_DATA_END_BIT | SDHCI_INT_DATA_CRC))
3267 *data_error = -EILSEQ;
3268 else if (intmask & SDHCI_INT_DATA_TIMEOUT)
3269 *data_error = -ETIMEDOUT;
3270 else if (intmask & SDHCI_INT_ADMA_ERROR)
3271 *data_error = -EIO;
3272 else
3273 *data_error = 0;
3274
3275 /* Clear selected interrupts. */
3276 mask = intmask & host->cqe_ier;
3277 sdhci_writel(host, mask, SDHCI_INT_STATUS);
3278
3279 if (intmask & SDHCI_INT_BUS_POWER)
3280 pr_err("%s: Card is consuming too much power!\n",
3281 mmc_hostname(host->mmc));
3282
3283 intmask &= ~(host->cqe_ier | SDHCI_INT_ERROR);
3284 if (intmask) {
3285 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
3286 pr_err("%s: CQE: Unexpected interrupt 0x%08x.\n",
3287 mmc_hostname(host->mmc), intmask);
3288 sdhci_dumpregs(host);
3289 }
3290
3291 return true;
3292}
3293EXPORT_SYMBOL_GPL(sdhci_cqe_irq);
3294
3295/*****************************************************************************\
3296 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003297 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08003298 * *
3299\*****************************************************************************/
3300
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003301struct sdhci_host *sdhci_alloc_host(struct device *dev,
3302 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003303{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003304 struct mmc_host *mmc;
3305 struct sdhci_host *host;
3306
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003307 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003308
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003309 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003310 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003311 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003312
3313 host = mmc_priv(mmc);
3314 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003315 host->mmc_host_ops = sdhci_ops;
3316 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003317
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003318 host->flags = SDHCI_SIGNALING_330;
3319
Adrian Hunterf12e39d2017-03-20 19:50:47 +02003320 host->cqe_ier = SDHCI_CQE_INT_MASK;
3321 host->cqe_err_ier = SDHCI_CQE_INT_ERR_MASK;
3322
Adrian Hunter83b600b2017-04-20 16:14:43 +08003323 host->tuning_delay = -1;
3324
Srinivas Kandagatlac846a002017-08-03 14:46:13 +02003325 host->sdma_boundary = SDHCI_DEFAULT_BOUNDARY_ARG;
3326
Jisheng Zhange93be382018-08-28 17:46:35 +08003327 /*
3328 * The DMA table descriptor count is calculated as the maximum
3329 * number of segments times 2, to allow for an alignment
3330 * descriptor for each segment, plus 1 for a nop end descriptor.
3331 */
3332 host->adma_table_cnt = SDHCI_MAX_SEGS * 2 + 1;
3333
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003334 return host;
3335}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003336
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003337EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003338
Alexandre Courbot7b913692016-03-07 11:07:55 +09003339static int sdhci_set_dma_mask(struct sdhci_host *host)
3340{
3341 struct mmc_host *mmc = host->mmc;
3342 struct device *dev = mmc_dev(mmc);
3343 int ret = -EINVAL;
3344
3345 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3346 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3347
3348 /* Try 64-bit mask if hardware is capable of it */
3349 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3350 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3351 if (ret) {
3352 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3353 mmc_hostname(mmc));
3354 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3355 }
3356 }
3357
3358 /* 32-bit mask as default & fallback */
3359 if (ret) {
3360 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3361 if (ret)
3362 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3363 mmc_hostname(mmc));
3364 }
3365
3366 return ret;
3367}
3368
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003369void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3370{
3371 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003372 u64 dt_caps_mask = 0;
3373 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003374
3375 if (host->read_caps)
3376 return;
3377
3378 host->read_caps = true;
3379
3380 if (debug_quirks)
3381 host->quirks = debug_quirks;
3382
3383 if (debug_quirks2)
3384 host->quirks2 = debug_quirks2;
3385
3386 sdhci_do_reset(host, SDHCI_RESET_ALL);
3387
Zach Brown92e0c442016-11-02 10:26:16 -05003388 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3389 "sdhci-caps-mask", &dt_caps_mask);
3390 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3391 "sdhci-caps", &dt_caps);
3392
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003393 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3394 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3395
3396 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3397 return;
3398
Zach Brown92e0c442016-11-02 10:26:16 -05003399 if (caps) {
3400 host->caps = *caps;
3401 } else {
3402 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3403 host->caps &= ~lower_32_bits(dt_caps_mask);
3404 host->caps |= lower_32_bits(dt_caps);
3405 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003406
3407 if (host->version < SDHCI_SPEC_300)
3408 return;
3409
Zach Brown92e0c442016-11-02 10:26:16 -05003410 if (caps1) {
3411 host->caps1 = *caps1;
3412 } else {
3413 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3414 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3415 host->caps1 |= upper_32_bits(dt_caps);
3416 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003417}
3418EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3419
Linus Walleijbd9b9022018-01-29 00:44:53 +01003420static int sdhci_allocate_bounce_buffer(struct sdhci_host *host)
3421{
3422 struct mmc_host *mmc = host->mmc;
3423 unsigned int max_blocks;
3424 unsigned int bounce_size;
3425 int ret;
3426
3427 /*
3428 * Cap the bounce buffer at 64KB. Using a bigger bounce buffer
3429 * has diminishing returns, this is probably because SD/MMC
3430 * cards are usually optimized to handle this size of requests.
3431 */
3432 bounce_size = SZ_64K;
3433 /*
3434 * Adjust downwards to maximum request size if this is less
3435 * than our segment size, else hammer down the maximum
3436 * request size to the maximum buffer size.
3437 */
3438 if (mmc->max_req_size < bounce_size)
3439 bounce_size = mmc->max_req_size;
3440 max_blocks = bounce_size / 512;
3441
3442 /*
3443 * When we just support one segment, we can get significant
3444 * speedups by the help of a bounce buffer to group scattered
3445 * reads/writes together.
3446 */
3447 host->bounce_buffer = devm_kmalloc(mmc->parent,
3448 bounce_size,
3449 GFP_KERNEL);
3450 if (!host->bounce_buffer) {
3451 pr_err("%s: failed to allocate %u bytes for bounce buffer, falling back to single segments\n",
3452 mmc_hostname(mmc),
3453 bounce_size);
3454 /*
3455 * Exiting with zero here makes sure we proceed with
3456 * mmc->max_segs == 1.
3457 */
3458 return 0;
3459 }
3460
3461 host->bounce_addr = dma_map_single(mmc->parent,
3462 host->bounce_buffer,
3463 bounce_size,
3464 DMA_BIDIRECTIONAL);
3465 ret = dma_mapping_error(mmc->parent, host->bounce_addr);
3466 if (ret)
3467 /* Again fall back to max_segs == 1 */
3468 return 0;
3469 host->bounce_buffer_size = bounce_size;
3470
3471 /* Lie about this since we're bouncing */
3472 mmc->max_segs = max_blocks;
3473 mmc->max_seg_size = bounce_size;
3474 mmc->max_req_size = bounce_size;
3475
3476 pr_info("%s bounce up to %u segments into one, max segment size %u bytes\n",
3477 mmc_hostname(mmc), max_blocks, bounce_size);
3478
3479 return 0;
3480}
3481
Adrian Hunter52f53362016-06-29 16:24:15 +03003482int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003483{
3484 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303485 u32 max_current_caps;
3486 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003487 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003488 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003489 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003490
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003491 WARN_ON(host == NULL);
3492 if (host == NULL)
3493 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003494
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003495 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003496
Jon Hunterefba1422016-07-12 14:53:36 +01003497 /*
3498 * If there are external regulators, get them. Note this must be done
3499 * early before resetting the host and reading the capabilities so that
3500 * the host can take the appropriate action if regulators are not
3501 * available.
3502 */
3503 ret = mmc_regulator_get_supply(mmc);
Wolfram Sang2a633032017-10-14 21:17:18 +02003504 if (ret)
Jon Hunterefba1422016-07-12 14:53:36 +01003505 return ret;
3506
Shawn Lin06ebc602017-07-19 15:55:49 +08003507 DBG("Version: 0x%08x | Present: 0x%08x\n",
3508 sdhci_readw(host, SDHCI_HOST_VERSION),
3509 sdhci_readl(host, SDHCI_PRESENT_STATE));
3510 DBG("Caps: 0x%08x | Caps_1: 0x%08x\n",
3511 sdhci_readl(host, SDHCI_CAPABILITIES),
3512 sdhci_readl(host, SDHCI_CAPABILITIES_1));
3513
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003514 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003515
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003516 override_timeout_clk = host->timeout_clk;
3517
Zhangfei Gao85105c52010-08-06 07:10:01 +08003518 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003519 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3520 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003521 }
3522
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003523 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003524 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003525 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003526 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003527 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003528 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003529
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003530 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003531 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003532 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003533 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003534 }
3535
Arindam Nathf2119df2011-05-05 12:18:57 +05303536 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003537 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003538 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003539
3540 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3541 (host->flags & SDHCI_USE_ADMA)) {
3542 DBG("Disabling ADMA as it is marked broken\n");
3543 host->flags &= ~SDHCI_USE_ADMA;
3544 }
3545
Adrian Huntere57a5f62014-11-04 12:42:46 +02003546 /*
3547 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3548 * and *must* do 64-bit DMA. A driver has the opportunity to change
3549 * that during the first call to ->enable_dma(). Similarly
3550 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3551 * implement.
3552 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003553 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003554 host->flags |= SDHCI_USE_64_BIT_DMA;
3555
Richard Röjforsa13abc72009-09-22 16:45:30 -07003556 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003557 ret = sdhci_set_dma_mask(host);
3558
3559 if (!ret && host->ops->enable_dma)
3560 ret = host->ops->enable_dma(host);
3561
3562 if (ret) {
3563 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3564 mmc_hostname(mmc));
3565 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3566
3567 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003568 }
3569 }
3570
Adrian Huntere57a5f62014-11-04 12:42:46 +02003571 /* SDMA does not support 64-bit DMA */
3572 if (host->flags & SDHCI_USE_64_BIT_DMA)
3573 host->flags &= ~SDHCI_USE_SDMA;
3574
Pierre Ossman2134a922008-06-28 18:28:51 +02003575 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003576 dma_addr_t dma;
3577 void *buf;
3578
Adrian Huntere57a5f62014-11-04 12:42:46 +02003579 if (host->flags & SDHCI_USE_64_BIT_DMA) {
Jisheng Zhange93be382018-08-28 17:46:35 +08003580 host->adma_table_sz = host->adma_table_cnt *
Adrian Huntere57a5f62014-11-04 12:42:46 +02003581 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003582 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003583 } else {
Jisheng Zhange93be382018-08-28 17:46:35 +08003584 host->adma_table_sz = host->adma_table_cnt *
Adrian Huntere57a5f62014-11-04 12:42:46 +02003585 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003586 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003587 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003588
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003589 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003590 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3591 host->adma_table_sz, &dma, GFP_KERNEL);
3592 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003593 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003594 mmc_hostname(mmc));
3595 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003596 } else if ((dma + host->align_buffer_sz) &
3597 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003598 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3599 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003600 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003601 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3602 host->adma_table_sz, buf, dma);
3603 } else {
3604 host->align_buffer = buf;
3605 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003606
Russell Kinge66e61c2016-01-26 13:39:55 +00003607 host->adma_table = buf + host->align_buffer_sz;
3608 host->adma_addr = dma + host->align_buffer_sz;
3609 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003610 }
3611
Pierre Ossman76591502008-07-21 00:32:11 +02003612 /*
3613 * If we use DMA, then it's up to the caller to set the DMA
3614 * mask, but PIO does not need the hw shim so we set a new
3615 * mask here in that case.
3616 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003617 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003618 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003619 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003620 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003621
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003622 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003623 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003624 >> SDHCI_CLOCK_BASE_SHIFT;
3625 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003626 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003627 >> SDHCI_CLOCK_BASE_SHIFT;
3628
Pierre Ossmand129bce2006-03-24 03:18:17 -08003629 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003630 if (host->max_clk == 0 || host->quirks &
3631 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003632 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003633 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3634 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003635 ret = -ENODEV;
3636 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003637 }
3638 host->max_clk = host->ops->get_max_clock(host);
3639 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003640
3641 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303642 * In case of Host Controller v3.00, find out whether clock
3643 * multiplier is supported.
3644 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003645 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303646 SDHCI_CLOCK_MUL_SHIFT;
3647
3648 /*
3649 * In case the value in Clock Multiplier is 0, then programmable
3650 * clock mode is not supported, otherwise the actual clock
3651 * multiplier is one more than the value of Clock Multiplier
3652 * in the Capabilities Register.
3653 */
3654 if (host->clk_mul)
3655 host->clk_mul += 1;
3656
3657 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003658 * Set host parameters.
3659 */
Dong Aisheng59241752015-07-22 20:53:07 +08003660 max_clk = host->max_clk;
3661
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003662 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003663 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303664 else if (host->version >= SDHCI_SPEC_300) {
3665 if (host->clk_mul) {
3666 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003667 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303668 } else
3669 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3670 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003671 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003672
Adrian Hunterd310ae42016-04-12 14:25:07 +03003673 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003674 mmc->f_max = max_clk;
3675
Aisheng Dong28aab052014-08-27 15:26:31 +08003676 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003677 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003678 SDHCI_TIMEOUT_CLK_SHIFT;
Shawn Lin8cc35282017-03-24 15:50:12 +08003679
3680 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
3681 host->timeout_clk *= 1000;
3682
Aisheng Dong28aab052014-08-27 15:26:31 +08003683 if (host->timeout_clk == 0) {
Shawn Lin8cc35282017-03-24 15:50:12 +08003684 if (!host->ops->get_timeout_clock) {
Aisheng Dong28aab052014-08-27 15:26:31 +08003685 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3686 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003687 ret = -ENODEV;
3688 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003689 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003690
Shawn Lin8cc35282017-03-24 15:50:12 +08003691 host->timeout_clk =
3692 DIV_ROUND_UP(host->ops->get_timeout_clock(host),
3693 1000);
3694 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003695
Adrian Hunter99513622016-03-07 13:33:55 +02003696 if (override_timeout_clk)
3697 host->timeout_clk = override_timeout_clk;
3698
Aisheng Dong28aab052014-08-27 15:26:31 +08003699 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003700 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003701 mmc->max_busy_timeout /= host->timeout_clk;
3702 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003703
Adrian Huntera999fd92018-04-27 17:17:15 +05303704 if (host->quirks2 & SDHCI_QUIRK2_DISABLE_HW_TIMEOUT &&
3705 !host->ops->get_max_timeout_count)
3706 mmc->max_busy_timeout = 0;
3707
Andrei Warkentine89d4562011-05-23 15:06:37 -05003708 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003709 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003710
3711 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3712 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003713
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003714 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003715 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003716 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003717 !(host->flags & SDHCI_USE_SDMA)) &&
3718 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003719 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003720 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003721 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003722 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003723 }
3724
Philip Rakity15ec4462010-11-19 16:48:39 -05003725 /*
3726 * A controller may support 8-bit width, but the board itself
3727 * might not have the pins brought out. Boards that support
3728 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3729 * their platform code before calling sdhci_add_host(), and we
3730 * won't assume 8-bit width for hosts without that CAP.
3731 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003732 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003733 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003734
Jerry Huang63ef5d82012-10-25 13:47:19 +08003735 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3736 mmc->caps &= ~MMC_CAP_CMD23;
3737
Adrian Hunter28da3582016-06-29 16:24:17 +03003738 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003739 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003740
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003741 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003742 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003743 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003744 mmc->caps |= MMC_CAP_NEEDS_POLL;
3745
Tim Kryger3a48edc2014-06-13 10:13:56 -07003746 if (!IS_ERR(mmc->supply.vqmmc)) {
3747 ret = regulator_enable(mmc->supply.vqmmc);
Stefan Agner1b5190c2018-07-05 14:18:19 +02003748
3749 /* If vqmmc provides no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003750 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3751 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003752 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3753 SDHCI_SUPPORT_SDR50 |
3754 SDHCI_SUPPORT_DDR50);
Stefan Agner1b5190c2018-07-05 14:18:19 +02003755
3756 /* In eMMC case vqmmc might be a fixed 1.8V regulator */
3757 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 2700000,
3758 3600000))
3759 host->flags &= ~SDHCI_SIGNALING_330;
3760
Chris Balla3361ab2013-03-11 17:51:53 -04003761 if (ret) {
3762 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3763 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003764 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003765 }
Kevin Liu8363c372012-11-17 17:55:51 -05003766 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003767
Adrian Hunter28da3582016-06-29 16:24:17 +03003768 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3769 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3770 SDHCI_SUPPORT_DDR50);
Kishon Vijay Abraham Ic16bc9a2018-04-27 17:17:14 +05303771 /*
3772 * The SDHCI controller in a SoC might support HS200/HS400
3773 * (indicated using mmc-hs200-1_8v/mmc-hs400-1_8v dt property),
3774 * but if the board is modeled such that the IO lines are not
3775 * connected to 1.8v then HS200/HS400 cannot be supported.
3776 * Disable HS200/HS400 if the board does not have 1.8v connected
3777 * to the IO lines. (Applicable for other modes in 1.8v)
3778 */
3779 mmc->caps2 &= ~(MMC_CAP2_HSX00_1_8V | MMC_CAP2_HS400_ES);
3780 mmc->caps &= ~(MMC_CAP_1_8V_DDR | MMC_CAP_UHS);
Adrian Hunter28da3582016-06-29 16:24:17 +03003781 }
Daniel Drake6a661802012-11-25 13:01:19 -05003782
Al Cooper4188bba2012-03-16 15:54:17 -04003783 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003784 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3785 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303786 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3787
3788 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003789 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303790 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003791 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3792 * field can be promoted to support HS200.
3793 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003794 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003795 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003796 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303797 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003798 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303799
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003800 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003801 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003802 mmc->caps2 |= MMC_CAP2_HS400;
3803
Adrian Hunter549c0b12014-11-06 15:19:05 +02003804 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3805 (IS_ERR(mmc->supply.vqmmc) ||
3806 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3807 1300000)))
3808 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3809
Adrian Hunter28da3582016-06-29 16:24:17 +03003810 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3811 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303812 mmc->caps |= MMC_CAP_UHS_DDR50;
3813
Girish K S069c9f12012-01-06 09:56:39 +05303814 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003815 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303816 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3817
Arindam Nathd6d50a12011-05-05 12:18:59 +05303818 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003819 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303820 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003821 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303822 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003823 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303824 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3825
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303826 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003827 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3828 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303829
3830 /*
3831 * In case Re-tuning Timer is not disabled, the actual value of
3832 * re-tuning timer will be 2 ^ (n - 1).
3833 */
3834 if (host->tuning_count)
3835 host->tuning_count = 1 << (host->tuning_count - 1);
3836
3837 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003838 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303839 SDHCI_RETUNING_MODE_SHIFT;
3840
Takashi Iwai8f230f42010-12-08 10:04:30 +01003841 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003842
Arindam Nathf2119df2011-05-05 12:18:57 +05303843 /*
3844 * According to SD Host Controller spec v3.00, if the Host System
3845 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3846 * the value is meaningful only if Voltage Support in the Capabilities
3847 * register is set. The actual current value is 4 times the register
3848 * value.
3849 */
3850 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003851 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003852 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003853 if (curr > 0) {
3854
3855 /* convert to SDHCI_MAX_CURRENT format */
3856 curr = curr/1000; /* convert to mA */
3857 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3858
3859 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3860 max_current_caps =
3861 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3862 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3863 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3864 }
3865 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303866
Adrian Hunter28da3582016-06-29 16:24:17 +03003867 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003868 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303869
Aaron Lu55c46652012-07-04 13:31:48 +08003870 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303871 SDHCI_MAX_CURRENT_330_MASK) >>
3872 SDHCI_MAX_CURRENT_330_SHIFT) *
3873 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303874 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003875 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003876 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303877
Aaron Lu55c46652012-07-04 13:31:48 +08003878 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303879 SDHCI_MAX_CURRENT_300_MASK) >>
3880 SDHCI_MAX_CURRENT_300_SHIFT) *
3881 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303882 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003883 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003884 ocr_avail |= MMC_VDD_165_195;
3885
Aaron Lu55c46652012-07-04 13:31:48 +08003886 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303887 SDHCI_MAX_CURRENT_180_MASK) >>
3888 SDHCI_MAX_CURRENT_180_SHIFT) *
3889 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303890 }
3891
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003892 /* If OCR set by host, use it instead. */
3893 if (host->ocr_mask)
3894 ocr_avail = host->ocr_mask;
3895
3896 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003897 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003898 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003899
Takashi Iwai8f230f42010-12-08 10:04:30 +01003900 mmc->ocr_avail = ocr_avail;
3901 mmc->ocr_avail_sdio = ocr_avail;
3902 if (host->ocr_avail_sdio)
3903 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3904 mmc->ocr_avail_sd = ocr_avail;
3905 if (host->ocr_avail_sd)
3906 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3907 else /* normal SD controllers don't support 1.8V */
3908 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3909 mmc->ocr_avail_mmc = ocr_avail;
3910 if (host->ocr_avail_mmc)
3911 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003912
3913 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003914 pr_err("%s: Hardware doesn't report any support voltages.\n",
3915 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003916 ret = -ENODEV;
3917 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003918 }
3919
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003920 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3921 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3922 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3923 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3924 host->flags |= SDHCI_SIGNALING_180;
3925
3926 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3927 host->flags |= SDHCI_SIGNALING_120;
3928
Pierre Ossmand129bce2006-03-24 03:18:17 -08003929 spin_lock_init(&host->lock);
3930
3931 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003932 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3933 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3934 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003935 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003936 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003937
3938 /*
Ulf Hansson250dcd12017-11-27 11:28:50 +01003939 * Maximum number of segments. Depends on if the hardware
3940 * can do scatter/gather or not.
3941 */
3942 if (host->flags & SDHCI_USE_ADMA) {
3943 mmc->max_segs = SDHCI_MAX_SEGS;
3944 } else if (host->flags & SDHCI_USE_SDMA) {
3945 mmc->max_segs = 1;
3946 if (swiotlb_max_segment()) {
3947 unsigned int max_req_size = (1 << IO_TLB_SHIFT) *
3948 IO_TLB_SEGSIZE;
3949 mmc->max_req_size = min(mmc->max_req_size,
3950 max_req_size);
3951 }
3952 } else { /* PIO */
3953 mmc->max_segs = SDHCI_MAX_SEGS;
3954 }
3955
3956 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003957 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003958 * of bytes. When doing hardware scatter/gather, each entry cannot
3959 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003960 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003961 if (host->flags & SDHCI_USE_ADMA) {
3962 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3963 mmc->max_seg_size = 65535;
3964 else
3965 mmc->max_seg_size = 65536;
3966 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003967 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003968 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003969
3970 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003971 * Maximum block size. This varies from controller to controller and
3972 * is specified in the capabilities register.
3973 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003974 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3975 mmc->max_blk_size = 2;
3976 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003977 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003978 SDHCI_MAX_BLOCK_SHIFT;
3979 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003980 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3981 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003982 mmc->max_blk_size = 0;
3983 }
3984 }
3985
3986 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003987
3988 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003989 * Maximum block count.
3990 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003991 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003992
Linus Walleijbd9b9022018-01-29 00:44:53 +01003993 if (mmc->max_segs == 1) {
3994 /* This may alter mmc->*_blk_* parameters */
3995 ret = sdhci_allocate_bounce_buffer(host);
3996 if (ret)
3997 return ret;
3998 }
3999
Adrian Hunter52f53362016-06-29 16:24:15 +03004000 return 0;
4001
4002unreg:
4003 if (!IS_ERR(mmc->supply.vqmmc))
4004 regulator_disable(mmc->supply.vqmmc);
4005undma:
4006 if (host->align_buffer)
4007 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4008 host->adma_table_sz, host->align_buffer,
4009 host->align_addr);
4010 host->adma_table = NULL;
4011 host->align_buffer = NULL;
4012
4013 return ret;
4014}
4015EXPORT_SYMBOL_GPL(sdhci_setup_host);
4016
Adrian Hunter4180ffa2017-03-20 19:50:45 +02004017void sdhci_cleanup_host(struct sdhci_host *host)
4018{
4019 struct mmc_host *mmc = host->mmc;
4020
4021 if (!IS_ERR(mmc->supply.vqmmc))
4022 regulator_disable(mmc->supply.vqmmc);
4023
4024 if (host->align_buffer)
4025 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4026 host->adma_table_sz, host->align_buffer,
4027 host->align_addr);
4028 host->adma_table = NULL;
4029 host->align_buffer = NULL;
4030}
4031EXPORT_SYMBOL_GPL(sdhci_cleanup_host);
4032
Adrian Hunter52f53362016-06-29 16:24:15 +03004033int __sdhci_add_host(struct sdhci_host *host)
4034{
4035 struct mmc_host *mmc = host->mmc;
4036 int ret;
4037
Pierre Ossman55db8902006-11-21 17:55:45 +01004038 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08004039 * Init tasklets.
4040 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08004041 tasklet_init(&host->finish_tasklet,
4042 sdhci_tasklet_finish, (unsigned long)host);
4043
Kees Cook2ee4f622017-10-24 08:03:45 -07004044 timer_setup(&host->timer, sdhci_timeout_timer, 0);
4045 timer_setup(&host->data_timer, sdhci_timeout_data_timer, 0);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004046
Adrian Hunter250fb7b42014-12-05 19:41:10 +02004047 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05304048
Shawn Guo2af502c2013-07-05 14:38:55 +08004049 sdhci_init(host, 0);
4050
Russell King781e9892014-04-25 12:55:46 +01004051 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
4052 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01004053 if (ret) {
4054 pr_err("%s: Failed to request IRQ %d: %d\n",
4055 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004056 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01004057 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08004058
Adrian Hunter061d17a2016-04-12 14:25:09 +03004059 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01004060 if (ret) {
4061 pr_err("%s: Failed to register LED device: %d\n",
4062 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004063 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01004064 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004065
Pierre Ossman5f25a662006-10-04 02:15:39 -07004066 mmiowb();
4067
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004068 ret = mmc_add_host(mmc);
4069 if (ret)
4070 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004071
Girish K Sa3c76eb2011-10-11 11:44:09 +05304072 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01004073 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02004074 (host->flags & SDHCI_USE_ADMA) ?
4075 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07004076 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004077
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004078 sdhci_enable_card_detection(host);
4079
Pierre Ossmand129bce2006-03-24 03:18:17 -08004080 return 0;
4081
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004082unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03004083 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03004084unirq:
Russell King03231f92014-04-25 12:57:12 +01004085 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01004086 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4087 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004088 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07004089untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08004090 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03004091
Pierre Ossmand129bce2006-03-24 03:18:17 -08004092 return ret;
4093}
Adrian Hunter52f53362016-06-29 16:24:15 +03004094EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004095
Adrian Hunter52f53362016-06-29 16:24:15 +03004096int sdhci_add_host(struct sdhci_host *host)
4097{
4098 int ret;
4099
4100 ret = sdhci_setup_host(host);
4101 if (ret)
4102 return ret;
4103
Adrian Hunter4180ffa2017-03-20 19:50:45 +02004104 ret = __sdhci_add_host(host);
4105 if (ret)
4106 goto cleanup;
4107
4108 return 0;
4109
4110cleanup:
4111 sdhci_cleanup_host(host);
4112
4113 return ret;
Adrian Hunter52f53362016-06-29 16:24:15 +03004114}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004115EXPORT_SYMBOL_GPL(sdhci_add_host);
4116
Pierre Ossman1e728592008-04-16 19:13:13 +02004117void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004118{
Tim Kryger3a48edc2014-06-13 10:13:56 -07004119 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02004120 unsigned long flags;
4121
4122 if (dead) {
4123 spin_lock_irqsave(&host->lock, flags);
4124
4125 host->flags |= SDHCI_DEVICE_DEAD;
4126
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004127 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05304128 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07004129 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03004130 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02004131 }
4132
4133 spin_unlock_irqrestore(&host->lock, flags);
4134 }
4135
Anton Vorontsov7260cf52009-03-17 00:13:48 +03004136 sdhci_disable_card_detection(host);
4137
Markus Mayer4e743f12014-07-03 13:27:42 -07004138 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004139
Adrian Hunter061d17a2016-04-12 14:25:09 +03004140 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01004141
Pierre Ossman1e728592008-04-16 19:13:13 +02004142 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01004143 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004144
Russell Kingb537f942014-04-25 12:56:01 +01004145 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
4146 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004147 free_irq(host->irq, host);
4148
4149 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03004150 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004151
Pierre Ossmand129bce2006-03-24 03:18:17 -08004152 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02004153
Tim Kryger3a48edc2014-06-13 10:13:56 -07004154 if (!IS_ERR(mmc->supply.vqmmc))
4155 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07004156
Russell Kingedd63fc2016-01-26 13:39:50 +00004157 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00004158 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
4159 host->adma_table_sz, host->align_buffer,
4160 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02004161
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02004162 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02004163 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004164}
4165
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004166EXPORT_SYMBOL_GPL(sdhci_remove_host);
4167
4168void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08004169{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004170 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004171}
4172
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004173EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08004174
4175/*****************************************************************************\
4176 * *
4177 * Driver init/exit *
4178 * *
4179\*****************************************************************************/
4180
4181static int __init sdhci_drv_init(void)
4182{
Girish K Sa3c76eb2011-10-11 11:44:09 +05304183 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01004184 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05304185 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004186
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004187 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08004188}
4189
4190static void __exit sdhci_drv_exit(void)
4191{
Pierre Ossmand129bce2006-03-24 03:18:17 -08004192}
4193
4194module_init(sdhci_drv_init);
4195module_exit(sdhci_drv_exit);
4196
Pierre Ossmandf673b22006-06-30 02:22:31 -07004197module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004198module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07004199
Pierre Ossman32710e82009-04-08 20:14:54 +02004200MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01004201MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08004202MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07004203
Pierre Ossmandf673b22006-06-30 02:22:31 -07004204MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03004205MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");