blob: 2032bea9bd0d01123d680a054269013f5aec6e53 [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
Adrian Hunter5a436cc2017-03-20 19:50:31 +020017#include <linux/ktime.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080018#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010019#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040020#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080021#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020023#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070024#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030025#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050026#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080027
Pierre Ossman2f730fe2008-03-17 10:29:38 +010028#include <linux/leds.h>
29
Aries Lee22113ef2010-12-15 08:14:24 +010030#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080031#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080032#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080033#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080034#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080035
Pierre Ossmand129bce2006-03-24 03:18:17 -080036#include "sdhci.h"
37
38#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080039
Pierre Ossmand129bce2006-03-24 03:18:17 -080040#define DBG(f, x...) \
Adrian Hunterf4218652017-03-20 19:50:39 +020041 pr_debug("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080042
Adrian Hunter85ad90e2017-03-20 19:50:42 +020043#define SDHCI_DUMP(f, x...) \
44 pr_err("%s: " DRIVER_NAME ": " f, mmc_hostname(host->mmc), ## x)
45
Arindam Nathb513ea22011-05-05 12:19:04 +053046#define MAX_TUNING_LOOP 40
47
Pierre Ossmandf673b22006-06-30 02:22:31 -070048static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030049static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070050
Pierre Ossmand129bce2006-03-24 03:18:17 -080051static void sdhci_finish_data(struct sdhci_host *);
52
Kevin Liu52983382013-01-31 11:31:37 +080053static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080054
55static void sdhci_dumpregs(struct sdhci_host *host)
56{
Adrian Hunter85ad90e2017-03-20 19:50:42 +020057 SDHCI_DUMP("============ SDHCI REGISTER DUMP ===========\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -080058
Adrian Hunter85ad90e2017-03-20 19:50:42 +020059 SDHCI_DUMP("Sys addr: 0x%08x | Version: 0x%08x\n",
60 sdhci_readl(host, SDHCI_DMA_ADDRESS),
61 sdhci_readw(host, SDHCI_HOST_VERSION));
62 SDHCI_DUMP("Blk size: 0x%08x | Blk cnt: 0x%08x\n",
63 sdhci_readw(host, SDHCI_BLOCK_SIZE),
64 sdhci_readw(host, SDHCI_BLOCK_COUNT));
65 SDHCI_DUMP("Argument: 0x%08x | Trn mode: 0x%08x\n",
66 sdhci_readl(host, SDHCI_ARGUMENT),
67 sdhci_readw(host, SDHCI_TRANSFER_MODE));
68 SDHCI_DUMP("Present: 0x%08x | Host ctl: 0x%08x\n",
69 sdhci_readl(host, SDHCI_PRESENT_STATE),
70 sdhci_readb(host, SDHCI_HOST_CONTROL));
71 SDHCI_DUMP("Power: 0x%08x | Blk gap: 0x%08x\n",
72 sdhci_readb(host, SDHCI_POWER_CONTROL),
73 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
74 SDHCI_DUMP("Wake-up: 0x%08x | Clock: 0x%08x\n",
75 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
76 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
77 SDHCI_DUMP("Timeout: 0x%08x | Int stat: 0x%08x\n",
78 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
79 sdhci_readl(host, SDHCI_INT_STATUS));
80 SDHCI_DUMP("Int enab: 0x%08x | Sig enab: 0x%08x\n",
81 sdhci_readl(host, SDHCI_INT_ENABLE),
82 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
83 SDHCI_DUMP("AC12 err: 0x%08x | Slot int: 0x%08x\n",
84 sdhci_readw(host, SDHCI_ACMD12_ERR),
85 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
86 SDHCI_DUMP("Caps: 0x%08x | Caps_1: 0x%08x\n",
87 sdhci_readl(host, SDHCI_CAPABILITIES),
88 sdhci_readl(host, SDHCI_CAPABILITIES_1));
89 SDHCI_DUMP("Cmd: 0x%08x | Max curr: 0x%08x\n",
90 sdhci_readw(host, SDHCI_COMMAND),
91 sdhci_readl(host, SDHCI_MAX_CURRENT));
92 SDHCI_DUMP("Resp[0]: 0x%08x | Resp[1]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020093 sdhci_readl(host, SDHCI_RESPONSE),
94 sdhci_readl(host, SDHCI_RESPONSE + 4));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020095 SDHCI_DUMP("Resp[2]: 0x%08x | Resp[3]: 0x%08x\n",
Adrian Hunter79623022017-03-20 19:50:40 +020096 sdhci_readl(host, SDHCI_RESPONSE + 8),
97 sdhci_readl(host, SDHCI_RESPONSE + 12));
Adrian Hunter85ad90e2017-03-20 19:50:42 +020098 SDHCI_DUMP("Host ctl2: 0x%08x\n",
99 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800100
Adrian Huntere57a5f62014-11-04 12:42:46 +0200101 if (host->flags & SDHCI_USE_ADMA) {
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200102 if (host->flags & SDHCI_USE_64_BIT_DMA) {
103 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
104 sdhci_readl(host, SDHCI_ADMA_ERROR),
105 sdhci_readl(host, SDHCI_ADMA_ADDRESS_HI),
106 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
107 } else {
108 SDHCI_DUMP("ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
109 sdhci_readl(host, SDHCI_ADMA_ERROR),
110 sdhci_readl(host, SDHCI_ADMA_ADDRESS));
111 }
Adrian Huntere57a5f62014-11-04 12:42:46 +0200112 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100113
Adrian Hunter85ad90e2017-03-20 19:50:42 +0200114 SDHCI_DUMP("============================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800115}
116
117/*****************************************************************************\
118 * *
119 * Low level functions *
120 * *
121\*****************************************************************************/
122
Adrian Hunter56a590d2016-06-29 16:24:32 +0300123static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
124{
125 return cmd->data || cmd->flags & MMC_RSP_BUSY;
126}
127
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300128static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
129{
Russell King5b4f1f62014-04-25 12:57:02 +0100130 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300131
Adrian Hunterc79396c2011-12-27 15:48:42 +0200132 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900133 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300134 return;
135
Russell King5b4f1f62014-04-25 12:57:02 +0100136 if (enable) {
137 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
138 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800139
Russell King5b4f1f62014-04-25 12:57:02 +0100140 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
141 SDHCI_INT_CARD_INSERT;
142 } else {
143 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
144 }
Russell Kingb537f942014-04-25 12:56:01 +0100145
146 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
147 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300148}
149
150static void sdhci_enable_card_detection(struct sdhci_host *host)
151{
152 sdhci_set_card_detection(host, true);
153}
154
155static void sdhci_disable_card_detection(struct sdhci_host *host)
156{
157 sdhci_set_card_detection(host, false);
158}
159
Ulf Hansson02d0b682016-04-11 15:32:41 +0200160static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
161{
162 if (host->bus_on)
163 return;
164 host->bus_on = true;
165 pm_runtime_get_noresume(host->mmc->parent);
166}
167
168static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
169{
170 if (!host->bus_on)
171 return;
172 host->bus_on = false;
173 pm_runtime_put_noidle(host->mmc->parent);
174}
175
Russell King03231f92014-04-25 12:57:12 +0100176void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800177{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200178 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800179
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300180 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800181
Adrian Hunterf0710a52013-05-06 12:17:32 +0300182 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800183 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300184 /* Reset-all turns off SD Bus Power */
185 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
186 sdhci_runtime_pm_bus_off(host);
187 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800188
Pierre Ossmane16514d82006-06-30 02:22:24 -0700189 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200190 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700191
192 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300193 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200194 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530195 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700196 mmc_hostname(host->mmc), (int)mask);
197 sdhci_dumpregs(host);
198 return;
199 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200200 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800201 }
Russell King03231f92014-04-25 12:57:12 +0100202}
203EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300204
Russell King03231f92014-04-25 12:57:12 +0100205static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
206{
207 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300208 struct mmc_host *mmc = host->mmc;
209
210 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100211 return;
212 }
213
214 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800215
Russell Kingda91a8f2014-04-25 13:00:12 +0100216 if (mask & SDHCI_RESET_ALL) {
217 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
218 if (host->ops->enable_dma)
219 host->ops->enable_dma(host);
220 }
221
222 /* Resetting the controller clears many */
223 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800224 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800225}
226
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800227static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800228{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300229 struct mmc_host *mmc = host->mmc;
230
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800231 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100232 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800233 else
Russell King03231f92014-04-25 12:57:12 +0100234 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800235
Russell Kingb537f942014-04-25 12:56:01 +0100236 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
237 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
238 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
239 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
240 SDHCI_INT_RESPONSE;
241
Dong Aishengf37b20e2016-07-12 15:46:17 +0800242 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
243 host->tuning_mode == SDHCI_TUNING_MODE_3)
244 host->ier |= SDHCI_INT_RETUNE;
245
Russell Kingb537f942014-04-25 12:56:01 +0100246 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
247 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800248
249 if (soft) {
250 /* force clock reconfiguration */
251 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300252 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800253 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300254}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800255
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300256static void sdhci_reinit(struct sdhci_host *host)
257{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800258 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300259 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260}
261
Adrian Hunter061d17a2016-04-12 14:25:09 +0300262static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800263{
264 u8 ctrl;
265
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300266 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800267 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300268 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800269}
270
Adrian Hunter061d17a2016-04-12 14:25:09 +0300271static void __sdhci_led_deactivate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800272{
273 u8 ctrl;
274
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300275 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800276 ctrl &= ~SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300277 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800278}
279
Masahiro Yamada4f782302016-04-14 13:19:39 +0900280#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100281static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300282 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100283{
284 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
285 unsigned long flags;
286
287 spin_lock_irqsave(&host->lock, flags);
288
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300289 if (host->runtime_suspended)
290 goto out;
291
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100292 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300293 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100294 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300295 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300296out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100297 spin_unlock_irqrestore(&host->lock, flags);
298}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300299
300static int sdhci_led_register(struct sdhci_host *host)
301{
302 struct mmc_host *mmc = host->mmc;
303
304 snprintf(host->led_name, sizeof(host->led_name),
305 "%s::", mmc_hostname(mmc));
306
307 host->led.name = host->led_name;
308 host->led.brightness = LED_OFF;
309 host->led.default_trigger = mmc_hostname(mmc);
310 host->led.brightness_set = sdhci_led_control;
311
312 return led_classdev_register(mmc_dev(mmc), &host->led);
313}
314
315static void sdhci_led_unregister(struct sdhci_host *host)
316{
317 led_classdev_unregister(&host->led);
318}
319
320static inline void sdhci_led_activate(struct sdhci_host *host)
321{
322}
323
324static inline void sdhci_led_deactivate(struct sdhci_host *host)
325{
326}
327
328#else
329
330static inline int sdhci_led_register(struct sdhci_host *host)
331{
332 return 0;
333}
334
335static inline void sdhci_led_unregister(struct sdhci_host *host)
336{
337}
338
339static inline void sdhci_led_activate(struct sdhci_host *host)
340{
341 __sdhci_led_activate(host);
342}
343
344static inline void sdhci_led_deactivate(struct sdhci_host *host)
345{
346 __sdhci_led_deactivate(host);
347}
348
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100349#endif
350
Pierre Ossmand129bce2006-03-24 03:18:17 -0800351/*****************************************************************************\
352 * *
353 * Core functions *
354 * *
355\*****************************************************************************/
356
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800358{
Pierre Ossman76591502008-07-21 00:32:11 +0200359 unsigned long flags;
360 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700361 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200362 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800363
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100364 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800365
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100366 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200367 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800368
Pierre Ossman76591502008-07-21 00:32:11 +0200369 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800370
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100371 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300372 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800373
Pierre Ossman76591502008-07-21 00:32:11 +0200374 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800375
Pierre Ossman76591502008-07-21 00:32:11 +0200376 blksize -= len;
377 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200378
Pierre Ossman76591502008-07-21 00:32:11 +0200379 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800380
Pierre Ossman76591502008-07-21 00:32:11 +0200381 while (len) {
382 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300383 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200384 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800385 }
Pierre Ossman76591502008-07-21 00:32:11 +0200386
387 *buf = scratch & 0xFF;
388
389 buf++;
390 scratch >>= 8;
391 chunk--;
392 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800393 }
394 }
Pierre Ossman76591502008-07-21 00:32:11 +0200395
396 sg_miter_stop(&host->sg_miter);
397
398 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100399}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800400
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100401static void sdhci_write_block_pio(struct sdhci_host *host)
402{
Pierre Ossman76591502008-07-21 00:32:11 +0200403 unsigned long flags;
404 size_t blksize, len, chunk;
405 u32 scratch;
406 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100407
408 DBG("PIO writing\n");
409
410 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200411 chunk = 0;
412 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100413
Pierre Ossman76591502008-07-21 00:32:11 +0200414 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100415
416 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300417 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100418
Pierre Ossman76591502008-07-21 00:32:11 +0200419 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200420
Pierre Ossman76591502008-07-21 00:32:11 +0200421 blksize -= len;
422 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100423
Pierre Ossman76591502008-07-21 00:32:11 +0200424 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100425
Pierre Ossman76591502008-07-21 00:32:11 +0200426 while (len) {
427 scratch |= (u32)*buf << (chunk * 8);
428
429 buf++;
430 chunk++;
431 len--;
432
433 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300434 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200435 chunk = 0;
436 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100437 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100438 }
439 }
Pierre Ossman76591502008-07-21 00:32:11 +0200440
441 sg_miter_stop(&host->sg_miter);
442
443 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100444}
445
446static void sdhci_transfer_pio(struct sdhci_host *host)
447{
448 u32 mask;
449
Pierre Ossman76591502008-07-21 00:32:11 +0200450 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100451 return;
452
453 if (host->data->flags & MMC_DATA_READ)
454 mask = SDHCI_DATA_AVAILABLE;
455 else
456 mask = SDHCI_SPACE_AVAILABLE;
457
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200458 /*
459 * Some controllers (JMicron JMB38x) mess up the buffer bits
460 * for transfers < 4 bytes. As long as it is just one block,
461 * we can ignore the bits.
462 */
463 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
464 (host->data->blocks == 1))
465 mask = ~0;
466
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300467 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300468 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
469 udelay(100);
470
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100471 if (host->data->flags & MMC_DATA_READ)
472 sdhci_read_block_pio(host);
473 else
474 sdhci_write_block_pio(host);
475
Pierre Ossman76591502008-07-21 00:32:11 +0200476 host->blocks--;
477 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100478 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100479 }
480
481 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800482}
483
Russell King48857d92016-01-26 13:40:16 +0000484static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000485 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000486{
487 int sg_count;
488
Russell King94538e52016-01-26 13:40:37 +0000489 /*
490 * If the data buffers are already mapped, return the previous
491 * dma_map_sg() result.
492 */
493 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000494 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000495
496 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
497 data->flags & MMC_DATA_WRITE ?
498 DMA_TO_DEVICE : DMA_FROM_DEVICE);
499
500 if (sg_count == 0)
501 return -ENOSPC;
502
503 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000504 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000505
506 return sg_count;
507}
508
Pierre Ossman2134a922008-06-28 18:28:51 +0200509static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
510{
511 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800512 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200513}
514
515static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
516{
Cong Wang482fce92011-11-27 13:27:00 +0800517 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200518 local_irq_restore(*flags);
519}
520
Adrian Huntere57a5f62014-11-04 12:42:46 +0200521static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
522 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800523{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200524 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800525
Adrian Huntere57a5f62014-11-04 12:42:46 +0200526 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200527 dma_desc->cmd = cpu_to_le16(cmd);
528 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200529 dma_desc->addr_lo = cpu_to_le32((u32)addr);
530
531 if (host->flags & SDHCI_USE_64_BIT_DMA)
532 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800533}
534
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200535static void sdhci_adma_mark_end(void *desc)
536{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200537 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200538
Adrian Huntere57a5f62014-11-04 12:42:46 +0200539 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200540 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200541}
542
Russell King60c64762016-01-26 13:40:22 +0000543static void sdhci_adma_table_pre(struct sdhci_host *host,
544 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200545{
Pierre Ossman2134a922008-06-28 18:28:51 +0200546 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200547 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000548 dma_addr_t addr, align_addr;
549 void *desc, *align;
550 char *buffer;
551 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200552
553 /*
554 * The spec does not specify endianness of descriptor table.
555 * We currently guess that it is LE.
556 */
557
Russell King60c64762016-01-26 13:40:22 +0000558 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200559
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200560 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200561 align = host->align_buffer;
562
563 align_addr = host->align_addr;
564
565 for_each_sg(data->sg, sg, host->sg_count, i) {
566 addr = sg_dma_address(sg);
567 len = sg_dma_len(sg);
568
569 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000570 * The SDHCI specification states that ADMA addresses must
571 * be 32-bit aligned. If they aren't, then we use a bounce
572 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200573 * alignment.
574 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200575 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
576 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200577 if (offset) {
578 if (data->flags & MMC_DATA_WRITE) {
579 buffer = sdhci_kmap_atomic(sg, &flags);
580 memcpy(align, buffer, offset);
581 sdhci_kunmap_atomic(buffer, &flags);
582 }
583
Ben Dooks118cd172010-03-05 13:43:26 -0800584 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200585 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200586 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200587
588 BUG_ON(offset > 65536);
589
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200590 align += SDHCI_ADMA2_ALIGN;
591 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200592
Adrian Hunter76fe3792014-11-04 12:42:42 +0200593 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200594
595 addr += offset;
596 len -= offset;
597 }
598
Pierre Ossman2134a922008-06-28 18:28:51 +0200599 BUG_ON(len > 65536);
600
Adrian Hunter347ea322015-11-26 14:00:48 +0200601 if (len) {
602 /* tran, valid */
603 sdhci_adma_write_desc(host, desc, addr, len,
604 ADMA2_TRAN_VALID);
605 desc += host->desc_sz;
606 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200607
608 /*
609 * If this triggers then we have a calculation bug
610 * somewhere. :/
611 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200612 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200613 }
614
Thomas Abraham70764a92010-05-26 14:42:04 -0700615 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000616 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200617 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200618 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200619 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700620 }
621 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000622 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200623 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700624 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200625}
626
627static void sdhci_adma_table_post(struct sdhci_host *host,
628 struct mmc_data *data)
629{
Pierre Ossman2134a922008-06-28 18:28:51 +0200630 struct scatterlist *sg;
631 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200632 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200633 char *buffer;
634 unsigned long flags;
635
Russell King47fa9612016-01-26 13:40:06 +0000636 if (data->flags & MMC_DATA_READ) {
637 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100638
Russell King47fa9612016-01-26 13:40:06 +0000639 /* Do a quick scan of the SG list for any unaligned mappings */
640 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200641 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000642 has_unaligned = true;
643 break;
644 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200645
Russell King47fa9612016-01-26 13:40:06 +0000646 if (has_unaligned) {
647 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000648 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200649
Russell King47fa9612016-01-26 13:40:06 +0000650 align = host->align_buffer;
651
652 for_each_sg(data->sg, sg, host->sg_count, i) {
653 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
654 size = SDHCI_ADMA2_ALIGN -
655 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
656
657 buffer = sdhci_kmap_atomic(sg, &flags);
658 memcpy(buffer, align, size);
659 sdhci_kunmap_atomic(buffer, &flags);
660
661 align += SDHCI_ADMA2_ALIGN;
662 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200663 }
664 }
665 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200666}
667
Andrei Warkentina3c77782011-04-11 16:13:42 -0500668static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800669{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700670 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500671 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700672 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800673
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200674 /*
675 * If the host controller provides us with an incorrect timeout
676 * value, just skip the check and use 0xE. The hardware may take
677 * longer to time out, but that's much better than having a too-short
678 * timeout value.
679 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200680 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200681 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200682
Andrei Warkentina3c77782011-04-11 16:13:42 -0500683 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100684 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500685 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800686
Andrei Warkentina3c77782011-04-11 16:13:42 -0500687 /* timeout in us */
688 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100689 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300690 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000691 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000692 if (host->clock && data->timeout_clks) {
693 unsigned long long val;
694
695 /*
696 * data->timeout_clks is in units of clock cycles.
697 * host->clock is in Hz. target_timeout is in us.
698 * Hence, us = 1000000 * cycles / Hz. Round up.
699 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200700 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000701 if (do_div(val, host->clock))
702 target_timeout++;
703 target_timeout += val;
704 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300705 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700706
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700707 /*
708 * Figure out needed cycles.
709 * We do this in steps in order to fit inside a 32 bit int.
710 * The first step is the minimum timeout, which will have a
711 * minimum resolution of 6 bits:
712 * (1) 2^13*1000 > 2^22,
713 * (2) host->timeout_clk < 2^16
714 * =>
715 * (1) / (2) > 2^6
716 */
717 count = 0;
718 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
719 while (current_timeout < target_timeout) {
720 count++;
721 current_timeout <<= 1;
722 if (count >= 0xF)
723 break;
724 }
725
726 if (count >= 0xF) {
Adrian Hunterf4218652017-03-20 19:50:39 +0200727 DBG("Too large timeout 0x%x requested for CMD%d!\n",
728 count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700729 count = 0xE;
730 }
731
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200732 return count;
733}
734
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300735static void sdhci_set_transfer_irqs(struct sdhci_host *host)
736{
737 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
738 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
739
740 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100741 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300742 else
Russell Kingb537f942014-04-25 12:56:01 +0100743 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
744
745 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
746 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300747}
748
Aisheng Dongb45e6682014-08-27 15:26:29 +0800749static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200750{
751 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800752
753 if (host->ops->set_timeout) {
754 host->ops->set_timeout(host, cmd);
755 } else {
756 count = sdhci_calc_timeout(host, cmd);
757 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
758 }
759}
760
761static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
762{
Pierre Ossman2134a922008-06-28 18:28:51 +0200763 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500764 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200765
Adrian Hunter56a590d2016-06-29 16:24:32 +0300766 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800767 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500768
769 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200770 return;
771
Adrian Hunter43dea092016-06-29 16:24:26 +0300772 WARN_ON(host->data);
773
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200774 /* Sanity checks */
775 BUG_ON(data->blksz * data->blocks > 524288);
776 BUG_ON(data->blksz > host->mmc->max_blk_size);
777 BUG_ON(data->blocks > 65535);
778
779 host->data = data;
780 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400781 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200782
Russell Kingfce14422016-01-26 13:41:20 +0000783 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200784 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000785 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000786 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200787
Russell Kingfce14422016-01-26 13:41:20 +0000788 host->flags |= SDHCI_REQ_USE_DMA;
789
790 /*
791 * FIXME: This doesn't account for merging when mapping the
792 * scatterlist.
793 *
794 * The assumption here being that alignment and lengths are
795 * the same after DMA mapping to device address space.
796 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000797 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000798 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200799 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000800 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000801 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000802 /*
803 * As we use up to 3 byte chunks to work
804 * around alignment problems, we need to
805 * check the offset as well.
806 */
807 offset_mask = 3;
808 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200809 } else {
810 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000811 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000812 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
813 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200814 }
815
Russell Kingdf953922016-01-26 13:41:14 +0000816 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200817 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000818 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100819 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000820 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200821 host->flags &= ~SDHCI_REQ_USE_DMA;
822 break;
823 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000824 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100825 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200826 host->flags &= ~SDHCI_REQ_USE_DMA;
827 break;
828 }
829 }
830 }
831 }
832
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200833 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000834 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200835
Russell King60c64762016-01-26 13:40:22 +0000836 if (sg_cnt <= 0) {
837 /*
838 * This only happens when someone fed
839 * us an invalid request.
840 */
841 WARN_ON(1);
842 host->flags &= ~SDHCI_REQ_USE_DMA;
843 } else if (host->flags & SDHCI_USE_ADMA) {
844 sdhci_adma_table_pre(host, data, sg_cnt);
845
846 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
847 if (host->flags & SDHCI_USE_64_BIT_DMA)
848 sdhci_writel(host,
849 (u64)host->adma_addr >> 32,
850 SDHCI_ADMA_ADDRESS_HI);
851 } else {
852 WARN_ON(sg_cnt != 1);
853 sdhci_writel(host, sg_dma_address(data->sg),
854 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200855 }
856 }
857
Pierre Ossman2134a922008-06-28 18:28:51 +0200858 /*
859 * Always adjust the DMA selection as some controllers
860 * (e.g. JMicron) can't do PIO properly when the selection
861 * is ADMA.
862 */
863 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300864 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200865 ctrl &= ~SDHCI_CTRL_DMA_MASK;
866 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200867 (host->flags & SDHCI_USE_ADMA)) {
868 if (host->flags & SDHCI_USE_64_BIT_DMA)
869 ctrl |= SDHCI_CTRL_ADMA64;
870 else
871 ctrl |= SDHCI_CTRL_ADMA32;
872 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200873 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200874 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300875 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100876 }
877
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200878 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200879 int flags;
880
881 flags = SG_MITER_ATOMIC;
882 if (host->data->flags & MMC_DATA_READ)
883 flags |= SG_MITER_TO_SG;
884 else
885 flags |= SG_MITER_FROM_SG;
886 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200887 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800888 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700889
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300890 sdhci_set_transfer_irqs(host);
891
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400892 /* Set the DMA boundary value and block size */
893 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
894 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300895 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896}
897
Adrian Hunter0293d502016-06-29 16:24:35 +0300898static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
899 struct mmc_request *mrq)
900{
Adrian Hunter20845be2016-08-16 13:44:13 +0300901 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
902 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300903}
904
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700905static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500906 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700907{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800908 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500909 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700910
Dong Aisheng2b558c12013-10-30 22:09:48 +0800911 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800912 if (host->quirks2 &
913 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
914 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
915 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800916 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800917 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
918 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800919 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800920 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700921 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800922 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700923
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200924 WARN_ON(!host->data);
925
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800926 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
927 mode = SDHCI_TRNS_BLK_CNT_EN;
928
Andrei Warkentine89d4562011-05-23 15:06:37 -0500929 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800930 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500931 /*
932 * If we are sending CMD23, CMD12 never gets sent
933 * on successful completion (so no Auto-CMD12).
934 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300935 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800936 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500937 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300938 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500939 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300940 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500941 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700942 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500943
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700944 if (data->flags & MMC_DATA_READ)
945 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100946 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700947 mode |= SDHCI_TRNS_DMA;
948
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300949 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800950}
951
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300952static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
953{
954 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
955 ((mrq->cmd && mrq->cmd->error) ||
956 (mrq->sbc && mrq->sbc->error) ||
957 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
958 (mrq->data->stop && mrq->data->stop->error))) ||
959 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
960}
961
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300962static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
963{
964 int i;
965
966 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
967 if (host->mrqs_done[i] == mrq) {
968 WARN_ON(1);
969 return;
970 }
971 }
972
973 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
974 if (!host->mrqs_done[i]) {
975 host->mrqs_done[i] = mrq;
976 break;
977 }
978 }
979
980 WARN_ON(i >= SDHCI_MAX_MRQS);
981
982 tasklet_schedule(&host->finish_tasklet);
983}
984
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300985static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
986{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300987 if (host->cmd && host->cmd->mrq == mrq)
988 host->cmd = NULL;
989
990 if (host->data_cmd && host->data_cmd->mrq == mrq)
991 host->data_cmd = NULL;
992
993 if (host->data && host->data->mrq == mrq)
994 host->data = NULL;
995
Adrian Huntered1563d2016-06-29 16:24:29 +0300996 if (sdhci_needs_reset(host, mrq))
997 host->pending_reset = true;
998
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300999 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001000}
1001
Pierre Ossmand129bce2006-03-24 03:18:17 -08001002static void sdhci_finish_data(struct sdhci_host *host)
1003{
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001004 struct mmc_command *data_cmd = host->data_cmd;
1005 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001006
Pierre Ossmand129bce2006-03-24 03:18:17 -08001007 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001008 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001009
Russell Kingadd89132016-01-26 13:40:42 +00001010 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1011 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1012 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001013
1014 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001015 * The specification states that the block count register must
1016 * be updated, but it does not specify at what point in the
1017 * data flow. That makes the register entirely useless to read
1018 * back so we have to assume that nothing made it to the card
1019 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001020 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001021 if (data->error)
1022 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001023 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001024 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001025
Andrei Warkentine89d4562011-05-23 15:06:37 -05001026 /*
1027 * Need to send CMD12 if -
1028 * a) open-ended multiblock transfer (no CMD23)
1029 * b) error in multiblock transfer
1030 */
1031 if (data->stop &&
1032 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001033 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001034
Pierre Ossmand129bce2006-03-24 03:18:17 -08001035 /*
1036 * The controller needs a reset of internal state machines
1037 * upon error conditions.
1038 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001039 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001040 if (!host->cmd || host->cmd == data_cmd)
1041 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001042 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001043 }
1044
Adrian Hunter20845be2016-08-16 13:44:13 +03001045 /*
1046 * 'cap_cmd_during_tfr' request must not use the command line
1047 * after mmc_command_done() has been called. It is upper layer's
1048 * responsibility to send the stop command if required.
1049 */
1050 if (data->mrq->cap_cmd_during_tfr) {
1051 sdhci_finish_mrq(host, data->mrq);
1052 } else {
1053 /* Avoid triggering warning in sdhci_send_command() */
1054 host->cmd = NULL;
1055 sdhci_send_command(host, data->stop);
1056 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001057 } else {
1058 sdhci_finish_mrq(host, data->mrq);
1059 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001060}
1061
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001062static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1063 unsigned long timeout)
1064{
1065 if (sdhci_data_line_cmd(mrq->cmd))
1066 mod_timer(&host->data_timer, timeout);
1067 else
1068 mod_timer(&host->timer, timeout);
1069}
1070
1071static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1072{
1073 if (sdhci_data_line_cmd(mrq->cmd))
1074 del_timer(&host->data_timer);
1075 else
1076 del_timer(&host->timer);
1077}
1078
Dong Aishengc0e551292013-09-13 19:11:31 +08001079void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001080{
1081 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001082 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001083 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001084
1085 WARN_ON(host->cmd);
1086
Russell King96776202016-01-26 13:39:34 +00001087 /* Initially, a command has no error */
1088 cmd->error = 0;
1089
Adrian Hunterfc605f12016-10-05 12:11:21 +03001090 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1091 cmd->opcode == MMC_STOP_TRANSMISSION)
1092 cmd->flags |= MMC_RSP_BUSY;
1093
Pierre Ossmand129bce2006-03-24 03:18:17 -08001094 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001095 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001096
1097 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001098 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001099 mask |= SDHCI_DATA_INHIBIT;
1100
1101 /* We shouldn't wait for data inihibit for stop commands, even
1102 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001103 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001104 mask &= ~SDHCI_DATA_INHIBIT;
1105
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001106 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001107 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001108 pr_err("%s: Controller never released inhibit bit(s).\n",
1109 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001110 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001111 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001112 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001113 return;
1114 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001115 timeout--;
1116 mdelay(1);
1117 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001118
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001119 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001120 if (!cmd->data && cmd->busy_timeout > 9000)
1121 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001122 else
1123 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001124 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001125
1126 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001127 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001128 WARN_ON(host->data_cmd);
1129 host->data_cmd = cmd;
1130 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131
Andrei Warkentina3c77782011-04-11 16:13:42 -05001132 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001133
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001134 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001135
Andrei Warkentine89d4562011-05-23 15:06:37 -05001136 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001137
Pierre Ossmand129bce2006-03-24 03:18:17 -08001138 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301139 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001140 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001141 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001142 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001143 return;
1144 }
1145
1146 if (!(cmd->flags & MMC_RSP_PRESENT))
1147 flags = SDHCI_CMD_RESP_NONE;
1148 else if (cmd->flags & MMC_RSP_136)
1149 flags = SDHCI_CMD_RESP_LONG;
1150 else if (cmd->flags & MMC_RSP_BUSY)
1151 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1152 else
1153 flags = SDHCI_CMD_RESP_SHORT;
1154
1155 if (cmd->flags & MMC_RSP_CRC)
1156 flags |= SDHCI_CMD_CRC;
1157 if (cmd->flags & MMC_RSP_OPCODE)
1158 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301159
1160 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301161 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1162 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 flags |= SDHCI_CMD_DATA;
1164
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001165 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001166}
Dong Aishengc0e551292013-09-13 19:11:31 +08001167EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001168
1169static void sdhci_finish_command(struct sdhci_host *host)
1170{
Adrian Huntere0a56402016-06-29 16:24:22 +03001171 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172 int i;
1173
Adrian Huntere0a56402016-06-29 16:24:22 +03001174 host->cmd = NULL;
1175
1176 if (cmd->flags & MMC_RSP_PRESENT) {
1177 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001178 /* CRC is stripped so we need to do some shifting. */
1179 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001180 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001181 SDHCI_RESPONSE + (3-i)*4) << 8;
1182 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001183 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001184 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001185 SDHCI_RESPONSE + (3-i)*4-1);
1186 }
1187 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001188 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001189 }
1190 }
1191
Adrian Hunter20845be2016-08-16 13:44:13 +03001192 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1193 mmc_command_done(host->mmc, cmd->mrq);
1194
Adrian Hunter6bde8682016-06-29 16:24:20 +03001195 /*
1196 * The host can send and interrupt when the busy state has
1197 * ended, allowing us to wait without wasting CPU cycles.
1198 * The busy signal uses DAT0 so this is similar to waiting
1199 * for data to complete.
1200 *
1201 * Note: The 1.0 specification is a bit ambiguous about this
1202 * feature so there might be some problems with older
1203 * controllers.
1204 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001205 if (cmd->flags & MMC_RSP_BUSY) {
1206 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001207 DBG("Cannot wait for busy signal when also doing a data transfer");
1208 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001209 cmd == host->data_cmd) {
1210 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001211 return;
1212 }
1213 }
1214
Andrei Warkentine89d4562011-05-23 15:06:37 -05001215 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001216 if (cmd == cmd->mrq->sbc) {
1217 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001218 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001219
Andrei Warkentine89d4562011-05-23 15:06:37 -05001220 /* Processed actual command. */
1221 if (host->data && host->data_early)
1222 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001223
Adrian Huntere0a56402016-06-29 16:24:22 +03001224 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001225 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001226 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001227}
1228
Kevin Liu52983382013-01-31 11:31:37 +08001229static u16 sdhci_get_preset_value(struct sdhci_host *host)
1230{
Russell Kingd975f122014-04-25 12:59:31 +01001231 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001232
Russell Kingd975f122014-04-25 12:59:31 +01001233 switch (host->timing) {
1234 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001235 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1236 break;
Russell Kingd975f122014-04-25 12:59:31 +01001237 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001238 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1239 break;
Russell Kingd975f122014-04-25 12:59:31 +01001240 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001241 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1242 break;
Russell Kingd975f122014-04-25 12:59:31 +01001243 case MMC_TIMING_UHS_SDR104:
1244 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001245 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1246 break;
Russell Kingd975f122014-04-25 12:59:31 +01001247 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001248 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001249 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1250 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001251 case MMC_TIMING_MMC_HS400:
1252 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1253 break;
Kevin Liu52983382013-01-31 11:31:37 +08001254 default:
1255 pr_warn("%s: Invalid UHS-I mode selected\n",
1256 mmc_hostname(host->mmc));
1257 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1258 break;
1259 }
1260 return preset;
1261}
1262
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001263u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1264 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001265{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301266 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001267 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301268 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001269 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001270
Zhangfei Gao85105c52010-08-06 07:10:01 +08001271 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001272 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001273 u16 pre_val;
1274
1275 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1276 pre_val = sdhci_get_preset_value(host);
1277 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1278 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1279 if (host->clk_mul &&
1280 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1281 clk = SDHCI_PROG_CLOCK_MODE;
1282 real_div = div + 1;
1283 clk_mul = host->clk_mul;
1284 } else {
1285 real_div = max_t(int, 1, div << 1);
1286 }
1287 goto clock_set;
1288 }
1289
Arindam Nathc3ed3872011-05-05 12:19:06 +05301290 /*
1291 * Check if the Host Controller supports Programmable Clock
1292 * Mode.
1293 */
1294 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001295 for (div = 1; div <= 1024; div++) {
1296 if ((host->max_clk * host->clk_mul / div)
1297 <= clock)
1298 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001299 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001300 if ((host->max_clk * host->clk_mul / div) <= clock) {
1301 /*
1302 * Set Programmable Clock Mode in the Clock
1303 * Control register.
1304 */
1305 clk = SDHCI_PROG_CLOCK_MODE;
1306 real_div = div;
1307 clk_mul = host->clk_mul;
1308 div--;
1309 } else {
1310 /*
1311 * Divisor can be too small to reach clock
1312 * speed requirement. Then use the base clock.
1313 */
1314 switch_base_clk = true;
1315 }
1316 }
1317
1318 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301319 /* Version 3.00 divisors must be a multiple of 2. */
1320 if (host->max_clk <= clock)
1321 div = 1;
1322 else {
1323 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1324 div += 2) {
1325 if ((host->max_clk / div) <= clock)
1326 break;
1327 }
1328 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001329 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301330 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301331 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1332 && !div && host->max_clk <= 25000000)
1333 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001334 }
1335 } else {
1336 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001337 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001338 if ((host->max_clk / div) <= clock)
1339 break;
1340 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001341 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301342 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001343 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001344
Kevin Liu52983382013-01-31 11:31:37 +08001345clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001346 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001347 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301348 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001349 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1350 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001351
1352 return clk;
1353}
1354EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1355
Ritesh Harjanifec79672016-11-21 12:07:19 +05301356void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001357{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001358 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001359
Pierre Ossmand129bce2006-03-24 03:18:17 -08001360 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001361 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001362
Chris Ball27f6cb12009-09-22 16:45:31 -07001363 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001364 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001365 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001366 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001367 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001368 pr_err("%s: Internal clock never stabilised.\n",
1369 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370 sdhci_dumpregs(host);
1371 return;
1372 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001373 udelay(10);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001374 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001375
1376 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001377 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001378}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301379EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1380
1381void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1382{
1383 u16 clk;
1384
1385 host->mmc->actual_clock = 0;
1386
1387 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1388
1389 if (clock == 0)
1390 return;
1391
1392 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1393 sdhci_enable_clk(host, clk);
1394}
Russell King17710592014-04-25 12:58:55 +01001395EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001396
Adrian Hunter1dceb042016-03-29 12:45:43 +03001397static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1398 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001399{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001400 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001401
Adrian Hunter1dceb042016-03-29 12:45:43 +03001402 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001403
1404 if (mode != MMC_POWER_OFF)
1405 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1406 else
1407 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1408}
1409
Adrian Hunter606d3132016-10-05 12:11:22 +03001410void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1411 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001412{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001413 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001414
Russell King24fbb3c2014-04-25 13:00:06 +01001415 if (mode != MMC_POWER_OFF) {
1416 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001417 case MMC_VDD_165_195:
1418 pwr = SDHCI_POWER_180;
1419 break;
1420 case MMC_VDD_29_30:
1421 case MMC_VDD_30_31:
1422 pwr = SDHCI_POWER_300;
1423 break;
1424 case MMC_VDD_32_33:
1425 case MMC_VDD_33_34:
1426 pwr = SDHCI_POWER_330;
1427 break;
1428 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001429 WARN(1, "%s: Invalid vdd %#x\n",
1430 mmc_hostname(host->mmc), vdd);
1431 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001432 }
1433 }
1434
1435 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001436 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001437
Pierre Ossmanae628902009-05-03 20:45:03 +02001438 host->pwr = pwr;
1439
1440 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001441 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001442 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1443 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001444 } else {
1445 /*
1446 * Spec says that we should clear the power reg before setting
1447 * a new value. Some controllers don't seem to like this though.
1448 */
1449 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1450 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001451
Russell Kinge921a8b2014-04-25 13:00:01 +01001452 /*
1453 * At least the Marvell CaFe chip gets confused if we set the
1454 * voltage and set turn on power at the same time, so set the
1455 * voltage first.
1456 */
1457 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1458 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001459
Russell Kinge921a8b2014-04-25 13:00:01 +01001460 pwr |= SDHCI_POWER_ON;
1461
Pierre Ossmanae628902009-05-03 20:45:03 +02001462 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1463
Russell Kinge921a8b2014-04-25 13:00:01 +01001464 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1465 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001466
Russell Kinge921a8b2014-04-25 13:00:01 +01001467 /*
1468 * Some controllers need an extra 10ms delay of 10ms before
1469 * they can apply clock after applying power
1470 */
1471 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1472 mdelay(10);
1473 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001474}
Adrian Hunter606d3132016-10-05 12:11:22 +03001475EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001476
Adrian Hunter606d3132016-10-05 12:11:22 +03001477void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1478 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001479{
Adrian Hunter606d3132016-10-05 12:11:22 +03001480 if (IS_ERR(host->mmc->supply.vmmc))
1481 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001482 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001483 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001484}
Adrian Hunter606d3132016-10-05 12:11:22 +03001485EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001486
Pierre Ossmand129bce2006-03-24 03:18:17 -08001487/*****************************************************************************\
1488 * *
1489 * MMC callbacks *
1490 * *
1491\*****************************************************************************/
1492
1493static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1494{
1495 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001496 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001497 unsigned long flags;
1498
1499 host = mmc_priv(mmc);
1500
Scott Branden04e079cf2015-03-10 11:35:10 -07001501 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001502 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001503
Pierre Ossmand129bce2006-03-24 03:18:17 -08001504 spin_lock_irqsave(&host->lock, flags);
1505
Adrian Hunter061d17a2016-04-12 14:25:09 +03001506 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001507
1508 /*
1509 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1510 * requests if Auto-CMD12 is enabled.
1511 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001512 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001513 if (mrq->stop) {
1514 mrq->data->stop = NULL;
1515 mrq->stop = NULL;
1516 }
1517 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001518
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001519 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001520 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001521 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301522 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001523 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001524 sdhci_send_command(host, mrq->sbc);
1525 else
1526 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301527 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001528
Pierre Ossman5f25a662006-10-04 02:15:39 -07001529 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001530 spin_unlock_irqrestore(&host->lock, flags);
1531}
1532
Russell King2317f562014-04-25 12:57:07 +01001533void sdhci_set_bus_width(struct sdhci_host *host, int width)
1534{
1535 u8 ctrl;
1536
1537 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1538 if (width == MMC_BUS_WIDTH_8) {
1539 ctrl &= ~SDHCI_CTRL_4BITBUS;
1540 if (host->version >= SDHCI_SPEC_300)
1541 ctrl |= SDHCI_CTRL_8BITBUS;
1542 } else {
1543 if (host->version >= SDHCI_SPEC_300)
1544 ctrl &= ~SDHCI_CTRL_8BITBUS;
1545 if (width == MMC_BUS_WIDTH_4)
1546 ctrl |= SDHCI_CTRL_4BITBUS;
1547 else
1548 ctrl &= ~SDHCI_CTRL_4BITBUS;
1549 }
1550 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1551}
1552EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1553
Russell King96d7b782014-04-25 12:59:26 +01001554void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1555{
1556 u16 ctrl_2;
1557
1558 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1559 /* Select Bus Speed Mode for host */
1560 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1561 if ((timing == MMC_TIMING_MMC_HS200) ||
1562 (timing == MMC_TIMING_UHS_SDR104))
1563 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1564 else if (timing == MMC_TIMING_UHS_SDR12)
1565 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1566 else if (timing == MMC_TIMING_UHS_SDR25)
1567 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1568 else if (timing == MMC_TIMING_UHS_SDR50)
1569 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1570 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1571 (timing == MMC_TIMING_MMC_DDR52))
1572 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001573 else if (timing == MMC_TIMING_MMC_HS400)
1574 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001575 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1576}
1577EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1578
Dong Aishengded97e02016-04-16 01:29:25 +08001579static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001580{
Dong Aishengded97e02016-04-16 01:29:25 +08001581 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001582 u8 ctrl;
1583
Adrian Hunter84ec0482016-12-19 15:33:11 +02001584 if (ios->power_mode == MMC_POWER_UNDEFINED)
1585 return;
1586
Adrian Hunterceb61432011-12-27 15:48:41 +02001587 if (host->flags & SDHCI_DEVICE_DEAD) {
Tim Kryger3a48edc2014-06-13 10:13:56 -07001588 if (!IS_ERR(mmc->supply.vmmc) &&
1589 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001590 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001591 return;
1592 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001593
Pierre Ossmand129bce2006-03-24 03:18:17 -08001594 /*
1595 * Reset the chip on each power off.
1596 * Should clear out any weird states.
1597 */
1598 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001599 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001600 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001601 }
1602
Kevin Liu52983382013-01-31 11:31:37 +08001603 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001604 (ios->power_mode == MMC_POWER_UP) &&
1605 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001606 sdhci_enable_preset_value(host, false);
1607
Russell King373073e2014-04-25 12:58:45 +01001608 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001609 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001610 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001611
1612 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1613 host->clock) {
1614 host->timeout_clk = host->mmc->actual_clock ?
1615 host->mmc->actual_clock / 1000 :
1616 host->clock / 1000;
1617 host->mmc->max_busy_timeout =
1618 host->ops->get_max_timeout_count ?
1619 host->ops->get_max_timeout_count(host) :
1620 1 << 27;
1621 host->mmc->max_busy_timeout /= host->timeout_clk;
1622 }
Russell King373073e2014-04-25 12:58:45 +01001623 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
Adrian Hunter606d3132016-10-05 12:11:22 +03001625 if (host->ops->set_power)
1626 host->ops->set_power(host, ios->power_mode, ios->vdd);
1627 else
1628 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001629
Philip Rakity643a81f2010-09-23 08:24:32 -07001630 if (host->ops->platform_send_init_74_clocks)
1631 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1632
Russell King2317f562014-04-25 12:57:07 +01001633 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001634
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001635 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001636
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001637 if ((ios->timing == MMC_TIMING_SD_HS ||
Jaehoon Chung273c5412016-10-07 14:08:43 +09001638 ios->timing == MMC_TIMING_MMC_HS ||
1639 ios->timing == MMC_TIMING_MMC_HS400 ||
1640 ios->timing == MMC_TIMING_MMC_HS200 ||
1641 ios->timing == MMC_TIMING_MMC_DDR52 ||
1642 ios->timing == MMC_TIMING_UHS_SDR50 ||
1643 ios->timing == MMC_TIMING_UHS_SDR104 ||
1644 ios->timing == MMC_TIMING_UHS_DDR50 ||
1645 ios->timing == MMC_TIMING_UHS_SDR25)
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001646 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001647 ctrl |= SDHCI_CTRL_HISPD;
1648 else
1649 ctrl &= ~SDHCI_CTRL_HISPD;
1650
Arindam Nathd6d50a12011-05-05 12:18:59 +05301651 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301652 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301653
Russell Kingda91a8f2014-04-25 13:00:12 +01001654 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301655 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301656 /*
1657 * We only need to set Driver Strength if the
1658 * preset value enable is not set.
1659 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001660 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301661 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1662 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1663 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001664 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1665 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301666 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1667 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001668 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1669 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1670 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001671 pr_warn("%s: invalid driver type, default to driver type B\n",
1672 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001673 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1674 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301675
1676 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301677 } else {
1678 /*
1679 * According to SDHC Spec v3.00, if the Preset Value
1680 * Enable in the Host Control 2 register is set, we
1681 * need to reset SD Clock Enable before changing High
1682 * Speed Enable to avoid generating clock gliches.
1683 */
Arindam Nath758535c2011-05-05 12:19:00 +05301684
1685 /* Reset SD Clock Enable */
1686 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1687 clk &= ~SDHCI_CLOCK_CARD_EN;
1688 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1689
1690 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1691
1692 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001693 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301694 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301695
Arindam Nath49c468f2011-05-05 12:19:01 +05301696 /* Reset SD Clock Enable */
1697 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1698 clk &= ~SDHCI_CLOCK_CARD_EN;
1699 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1700
Russell King96d7b782014-04-25 12:59:26 +01001701 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001702 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301703
Kevin Liu52983382013-01-31 11:31:37 +08001704 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1705 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1706 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1707 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1708 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001709 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1710 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001711 u16 preset;
1712
1713 sdhci_enable_preset_value(host, true);
1714 preset = sdhci_get_preset_value(host);
1715 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1716 >> SDHCI_PRESET_DRV_SHIFT;
1717 }
1718
Arindam Nath49c468f2011-05-05 12:19:01 +05301719 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001720 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301721 } else
1722 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301723
Leandro Dorileob8352262007-07-25 23:47:04 +02001724 /*
1725 * Some (ENE) controllers go apeshit on some ios operation,
1726 * signalling timeout and CRC errors even on CMD0. Resetting
1727 * it on each ios seems to solve the problem.
1728 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301729 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001730 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001731
Pierre Ossman5f25a662006-10-04 02:15:39 -07001732 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001733}
1734
Dong Aishengded97e02016-04-16 01:29:25 +08001735static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001736{
1737 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001738 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001739
1740 if (host->flags & SDHCI_DEVICE_DEAD)
1741 return 0;
1742
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001743 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001744 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001745 return 1;
1746
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001747 /*
1748 * Try slot gpio detect, if defined it take precedence
1749 * over build in controller functionality
1750 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001751 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001752 return !!gpio_cd;
1753
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001754 /* If polling, assume that the card is always present. */
1755 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1756 return 1;
1757
Kevin Liu94144a42013-02-28 17:35:53 +08001758 /* Host native card detect */
1759 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1760}
1761
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001762static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001764 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001765 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001766
Pierre Ossmand129bce2006-03-24 03:18:17 -08001767 spin_lock_irqsave(&host->lock, flags);
1768
Pierre Ossman1e728592008-04-16 19:13:13 +02001769 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001770 is_readonly = 0;
1771 else if (host->ops->get_ro)
1772 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001773 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001774 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1775 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001776
1777 spin_unlock_irqrestore(&host->lock, flags);
1778
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001779 /* This quirk needs to be replaced by a callback-function later */
1780 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1781 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001782}
1783
Takashi Iwai82b0e232011-04-21 20:26:38 +02001784#define SAMPLE_COUNT 5
1785
Dong Aishengded97e02016-04-16 01:29:25 +08001786static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001787{
Dong Aishengded97e02016-04-16 01:29:25 +08001788 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001789 int i, ro_count;
1790
Takashi Iwai82b0e232011-04-21 20:26:38 +02001791 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001792 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001793
1794 ro_count = 0;
1795 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001796 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001797 if (++ro_count > SAMPLE_COUNT / 2)
1798 return 1;
1799 }
1800 msleep(30);
1801 }
1802 return 0;
1803}
1804
Adrian Hunter20758b62011-08-29 16:42:12 +03001805static void sdhci_hw_reset(struct mmc_host *mmc)
1806{
1807 struct sdhci_host *host = mmc_priv(mmc);
1808
1809 if (host->ops && host->ops->hw_reset)
1810 host->ops->hw_reset(host);
1811}
1812
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001813static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1814{
Russell Kingbe138552014-04-25 12:55:56 +01001815 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001816 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001817 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001818 else
Russell Kingb537f942014-04-25 12:56:01 +01001819 host->ier &= ~SDHCI_INT_CARD_INT;
1820
1821 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1822 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001823 mmiowb();
1824 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001825}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001826
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001827static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1828{
1829 struct sdhci_host *host = mmc_priv(mmc);
1830 unsigned long flags;
1831
Hans de Goede923713b2017-03-26 13:14:45 +02001832 if (enable)
1833 pm_runtime_get_noresume(host->mmc->parent);
1834
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001835 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001836 if (enable)
1837 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1838 else
1839 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1840
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001841 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001842 spin_unlock_irqrestore(&host->lock, flags);
Hans de Goede923713b2017-03-26 13:14:45 +02001843
1844 if (!enable)
1845 pm_runtime_put_noidle(host->mmc->parent);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001846}
1847
Dong Aishengded97e02016-04-16 01:29:25 +08001848static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1849 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001850{
Dong Aishengded97e02016-04-16 01:29:25 +08001851 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001852 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001853 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001854
1855 /*
1856 * Signal Voltage Switching is only applicable for Host Controllers
1857 * v3.00 and above.
1858 */
1859 if (host->version < SDHCI_SPEC_300)
1860 return 0;
1861
Philip Rakity6231f3d2012-07-23 15:56:23 -07001862 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001863
Fabio Estevam21f59982013-02-14 10:35:03 -02001864 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001865 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001866 if (!(host->flags & SDHCI_SIGNALING_330))
1867 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001868 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1869 ctrl &= ~SDHCI_CTRL_VDD_180;
1870 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1871
Tim Kryger3a48edc2014-06-13 10:13:56 -07001872 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001873 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001874 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001875 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1876 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001877 return -EIO;
1878 }
1879 }
1880 /* Wait for 5ms */
1881 usleep_range(5000, 5500);
1882
1883 /* 3.3V regulator output should be stable within 5 ms */
1884 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1885 if (!(ctrl & SDHCI_CTRL_VDD_180))
1886 return 0;
1887
Joe Perches66061102014-09-12 14:56:56 -07001888 pr_warn("%s: 3.3V regulator output did not became stable\n",
1889 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001890
1891 return -EAGAIN;
1892 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001893 if (!(host->flags & SDHCI_SIGNALING_180))
1894 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001895 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001896 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001897 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001898 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1899 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001900 return -EIO;
1901 }
1902 }
1903
1904 /*
1905 * Enable 1.8V Signal Enable in the Host Control2
1906 * register
1907 */
1908 ctrl |= SDHCI_CTRL_VDD_180;
1909 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1910
Vincent Yang9d967a62015-01-20 16:05:15 +08001911 /* Some controller need to do more when switching */
1912 if (host->ops->voltage_switch)
1913 host->ops->voltage_switch(host);
1914
Kevin Liu20b92a32012-12-17 19:29:26 +08001915 /* 1.8V regulator output should be stable within 5 ms */
1916 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1917 if (ctrl & SDHCI_CTRL_VDD_180)
1918 return 0;
1919
Joe Perches66061102014-09-12 14:56:56 -07001920 pr_warn("%s: 1.8V regulator output did not became stable\n",
1921 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001922
1923 return -EAGAIN;
1924 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001925 if (!(host->flags & SDHCI_SIGNALING_120))
1926 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001927 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001928 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001929 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001930 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1931 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001932 return -EIO;
1933 }
1934 }
1935 return 0;
1936 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301937 /* No signal voltage switch required */
1938 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001939 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301940}
1941
Kevin Liu20b92a32012-12-17 19:29:26 +08001942static int sdhci_card_busy(struct mmc_host *mmc)
1943{
1944 struct sdhci_host *host = mmc_priv(mmc);
1945 u32 present_state;
1946
Adrian Huntere613cc42016-06-23 14:00:58 +03001947 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001948 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001949
Adrian Huntere613cc42016-06-23 14:00:58 +03001950 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001951}
1952
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001953static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1954{
1955 struct sdhci_host *host = mmc_priv(mmc);
1956 unsigned long flags;
1957
1958 spin_lock_irqsave(&host->lock, flags);
1959 host->flags |= SDHCI_HS400_TUNING;
1960 spin_unlock_irqrestore(&host->lock, flags);
1961
1962 return 0;
1963}
1964
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02001965static void sdhci_start_tuning(struct sdhci_host *host)
1966{
1967 u16 ctrl;
1968
1969 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1970 ctrl |= SDHCI_CTRL_EXEC_TUNING;
1971 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
1972 ctrl |= SDHCI_CTRL_TUNED_CLK;
1973 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1974
1975 /*
1976 * As per the Host Controller spec v3.00, tuning command
1977 * generates Buffer Read Ready interrupt, so enable that.
1978 *
1979 * Note: The spec clearly says that when tuning sequence
1980 * is being performed, the controller does not generate
1981 * interrupts other than Buffer Read Ready interrupt. But
1982 * to make sure we don't hit a controller bug, we _only_
1983 * enable Buffer Read Ready interrupt here.
1984 */
1985 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
1986 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
1987}
1988
1989static void sdhci_end_tuning(struct sdhci_host *host)
1990{
1991 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1992 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
1993}
1994
1995static void sdhci_reset_tuning(struct sdhci_host *host)
1996{
1997 u16 ctrl;
1998
1999 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2000 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2001 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2002 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2003}
2004
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002005static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002006{
2007 sdhci_reset_tuning(host);
2008
2009 sdhci_do_reset(host, SDHCI_RESET_CMD);
2010 sdhci_do_reset(host, SDHCI_RESET_DATA);
2011
2012 sdhci_end_tuning(host);
2013
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002014 mmc_abort_tuning(host->mmc, opcode);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002015}
2016
2017/*
2018 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2019 * tuning command does not have a data payload (or rather the hardware does it
2020 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2021 * interrupt setup is different to other commands and there is no timeout
2022 * interrupt so special handling is needed.
2023 */
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002024static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002025{
2026 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002027 struct mmc_command cmd = {};
2028 struct mmc_request mrq = {};
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002029 unsigned long flags;
2030
2031 spin_lock_irqsave(&host->lock, flags);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002032
2033 cmd.opcode = opcode;
2034 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2035 cmd.mrq = &mrq;
2036
2037 mrq.cmd = &cmd;
2038 /*
2039 * In response to CMD19, the card sends 64 bytes of tuning
2040 * block to the Host Controller. So we set the block size
2041 * to 64 here.
2042 */
Adrian Hunter85336102016-12-02 15:14:26 +02002043 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2044 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2045 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
2046 else
2047 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002048
2049 /*
2050 * The tuning block is sent by the card to the host controller.
2051 * So we set the TRNS_READ bit in the Transfer Mode register.
2052 * This also takes care of setting DMA Enable and Multi Block
2053 * Select in the same register to 0.
2054 */
2055 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2056
2057 sdhci_send_command(host, &cmd);
2058
2059 host->cmd = NULL;
2060
2061 sdhci_del_timer(host, &mrq);
2062
2063 host->tuning_done = 0;
2064
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002065 mmiowb();
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002066 spin_unlock_irqrestore(&host->lock, flags);
2067
2068 /* Wait for Buffer Read Ready interrupt */
2069 wait_event_timeout(host->buf_ready_int, (host->tuning_done == 1),
2070 msecs_to_jiffies(50));
2071
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002072}
2073
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002074static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode)
Adrian Hunter6b11e702016-12-02 15:14:27 +02002075{
2076 int i;
2077
2078 /*
2079 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2080 * of loops reaches 40 times.
2081 */
2082 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2083 u16 ctrl;
2084
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002085 sdhci_send_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002086
2087 if (!host->tuning_done) {
2088 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2089 mmc_hostname(host->mmc));
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002090 sdhci_abort_tuning(host, opcode);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002091 return;
2092 }
2093
2094 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2095 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2096 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2097 return; /* Success! */
2098 break;
2099 }
2100
2101 /* eMMC spec does not require a delay between tuning cycles */
2102 if (opcode == MMC_SEND_TUNING_BLOCK)
2103 mdelay(1);
2104 }
2105
2106 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2107 mmc_hostname(host->mmc));
2108 sdhci_reset_tuning(host);
2109}
2110
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002111int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302112{
Russell King4b6f37d2014-04-25 12:59:36 +01002113 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302114 int err = 0;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002115 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002116 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302117
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002118 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002119
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002120 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2121 tuning_count = host->tuning_count;
2122
Arindam Nathb513ea22011-05-05 12:19:04 +05302123 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002124 * The Host Controller needs tuning in case of SDR104 and DDR50
2125 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2126 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302127 * If the Host Controller supports the HS200 mode then the
2128 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302129 */
Russell King4b6f37d2014-04-25 12:59:36 +01002130 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002131 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002132 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002133 err = -EINVAL;
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002134 goto out;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002135
Russell King4b6f37d2014-04-25 12:59:36 +01002136 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002137 /*
2138 * Periodic re-tuning for HS400 is not expected to be needed, so
2139 * disable it here.
2140 */
2141 if (hs400_tuning)
2142 tuning_count = 0;
2143 break;
2144
Russell King4b6f37d2014-04-25 12:59:36 +01002145 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002146 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002147 break;
Girish K S069c9f12012-01-06 09:56:39 +05302148
Russell King4b6f37d2014-04-25 12:59:36 +01002149 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002150 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002151 break;
2152 /* FALLTHROUGH */
2153
2154 default:
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002155 goto out;
Arindam Nathb513ea22011-05-05 12:19:04 +05302156 }
2157
Dong Aisheng45251812013-09-13 19:11:30 +08002158 if (host->ops->platform_execute_tuning) {
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302159 err = host->ops->platform_execute_tuning(host, opcode);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002160 goto out;
Dong Aisheng45251812013-09-13 19:11:30 +08002161 }
2162
Adrian Hunter6b11e702016-12-02 15:14:27 +02002163 host->mmc->retune_period = tuning_count;
2164
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002165 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302166
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002167 __sdhci_execute_tuning(host, opcode);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302168
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002169 sdhci_end_tuning(host);
Adrian Hunter2a85ef22017-03-20 19:50:38 +02002170out:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302171 host->flags &= ~SDHCI_HS400_TUNING;
Adrian Hunter6b11e702016-12-02 15:14:27 +02002172
Arindam Nathb513ea22011-05-05 12:19:04 +05302173 return err;
2174}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002175EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302176
Kevin Liu52983382013-01-31 11:31:37 +08002177static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302178{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302179 /* Host Controller v3.00 defines preset value registers */
2180 if (host->version < SDHCI_SPEC_300)
2181 return;
2182
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302183 /*
2184 * We only enable or disable Preset Value if they are not already
2185 * enabled or disabled respectively. Otherwise, we bail out.
2186 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002187 if (host->preset_enabled != enable) {
2188 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2189
2190 if (enable)
2191 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2192 else
2193 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2194
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302195 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002196
2197 if (enable)
2198 host->flags |= SDHCI_PV_ENABLED;
2199 else
2200 host->flags &= ~SDHCI_PV_ENABLED;
2201
2202 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302203 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002204}
2205
Haibo Chen348487c2014-12-09 17:04:05 +08002206static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2207 int err)
2208{
2209 struct sdhci_host *host = mmc_priv(mmc);
2210 struct mmc_data *data = mrq->data;
2211
Russell Kingf48f0392016-01-26 13:40:32 +00002212 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002213 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2214 data->flags & MMC_DATA_WRITE ?
2215 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2216
2217 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002218}
2219
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002220static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002221{
2222 struct sdhci_host *host = mmc_priv(mmc);
2223
Haibo Chend31911b2015-08-25 10:02:11 +08002224 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002225
2226 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002227 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002228}
2229
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002230static inline bool sdhci_has_requests(struct sdhci_host *host)
2231{
2232 return host->cmd || host->data_cmd;
2233}
2234
2235static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2236{
2237 if (host->data_cmd) {
2238 host->data_cmd->error = err;
2239 sdhci_finish_mrq(host, host->data_cmd->mrq);
2240 }
2241
2242 if (host->cmd) {
2243 host->cmd->error = err;
2244 sdhci_finish_mrq(host, host->cmd->mrq);
2245 }
2246}
2247
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002248static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002249{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002250 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002251 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002252 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002253
Christian Daudt722e1282013-06-20 14:26:36 -07002254 /* First check if client has provided their own card event */
2255 if (host->ops->card_event)
2256 host->ops->card_event(host);
2257
Adrian Hunterd3940f22016-06-29 16:24:14 +03002258 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002259
Pierre Ossmand129bce2006-03-24 03:18:17 -08002260 spin_lock_irqsave(&host->lock, flags);
2261
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002262 /* Check sdhci_has_requests() first in case we are runtime suspended */
2263 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302264 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002265 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302266 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002267 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002268
Russell King03231f92014-04-25 12:57:12 +01002269 sdhci_do_reset(host, SDHCI_RESET_CMD);
2270 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002271
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002272 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002273 }
2274
2275 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002276}
2277
2278static const struct mmc_host_ops sdhci_ops = {
2279 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002280 .post_req = sdhci_post_req,
2281 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002282 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002283 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002284 .get_ro = sdhci_get_ro,
2285 .hw_reset = sdhci_hw_reset,
2286 .enable_sdio_irq = sdhci_enable_sdio_irq,
2287 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002288 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002289 .execute_tuning = sdhci_execute_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002290 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002291 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002292};
2293
2294/*****************************************************************************\
2295 * *
2296 * Tasklets *
2297 * *
2298\*****************************************************************************/
2299
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002300static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002301{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002302 unsigned long flags;
2303 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002304 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002305
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002306 spin_lock_irqsave(&host->lock, flags);
2307
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002308 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2309 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002310 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002311 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002312 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002313
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002314 if (!mrq) {
2315 spin_unlock_irqrestore(&host->lock, flags);
2316 return true;
2317 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002318
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002319 sdhci_del_timer(host, mrq);
2320
Pierre Ossmand129bce2006-03-24 03:18:17 -08002321 /*
Russell King054cedf2016-01-26 13:40:42 +00002322 * Always unmap the data buffers if they were mapped by
2323 * sdhci_prepare_data() whenever we finish with a request.
2324 * This avoids leaking DMA mappings on error.
2325 */
2326 if (host->flags & SDHCI_REQ_USE_DMA) {
2327 struct mmc_data *data = mrq->data;
2328
2329 if (data && data->host_cookie == COOKIE_MAPPED) {
2330 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2331 (data->flags & MMC_DATA_READ) ?
2332 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2333 data->host_cookie = COOKIE_UNMAPPED;
2334 }
2335 }
2336
2337 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002338 * The controller needs a reset of internal state machines
2339 * upon error conditions.
2340 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002341 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002342 /*
2343 * Do not finish until command and data lines are available for
2344 * reset. Note there can only be one other mrq, so it cannot
2345 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2346 * would both be null.
2347 */
2348 if (host->cmd || host->data_cmd) {
2349 spin_unlock_irqrestore(&host->lock, flags);
2350 return true;
2351 }
2352
Pierre Ossman645289d2006-06-30 02:22:33 -07002353 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002354 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002355 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002356 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002357
2358 /* Spec says we should do both at the same time, but Ricoh
2359 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002360 sdhci_do_reset(host, SDHCI_RESET_CMD);
2361 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002362
2363 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002364 }
2365
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002366 if (!sdhci_has_requests(host))
2367 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002368
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002369 host->mrqs_done[i] = NULL;
2370
Pierre Ossman5f25a662006-10-04 02:15:39 -07002371 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002372 spin_unlock_irqrestore(&host->lock, flags);
2373
2374 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002375
2376 return false;
2377}
2378
2379static void sdhci_tasklet_finish(unsigned long param)
2380{
2381 struct sdhci_host *host = (struct sdhci_host *)param;
2382
2383 while (!sdhci_request_done(host))
2384 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002385}
2386
2387static void sdhci_timeout_timer(unsigned long data)
2388{
2389 struct sdhci_host *host;
2390 unsigned long flags;
2391
2392 host = (struct sdhci_host*)data;
2393
2394 spin_lock_irqsave(&host->lock, flags);
2395
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002396 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2397 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2398 mmc_hostname(host->mmc));
2399 sdhci_dumpregs(host);
2400
2401 host->cmd->error = -ETIMEDOUT;
2402 sdhci_finish_mrq(host, host->cmd->mrq);
2403 }
2404
2405 mmiowb();
2406 spin_unlock_irqrestore(&host->lock, flags);
2407}
2408
2409static void sdhci_timeout_data_timer(unsigned long data)
2410{
2411 struct sdhci_host *host;
2412 unsigned long flags;
2413
2414 host = (struct sdhci_host *)data;
2415
2416 spin_lock_irqsave(&host->lock, flags);
2417
2418 if (host->data || host->data_cmd ||
2419 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002420 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2421 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002422 sdhci_dumpregs(host);
2423
2424 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002425 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002426 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002427 } else if (host->data_cmd) {
2428 host->data_cmd->error = -ETIMEDOUT;
2429 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002431 host->cmd->error = -ETIMEDOUT;
2432 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002433 }
2434 }
2435
Pierre Ossman5f25a662006-10-04 02:15:39 -07002436 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002437 spin_unlock_irqrestore(&host->lock, flags);
2438}
2439
2440/*****************************************************************************\
2441 * *
2442 * Interrupt handling *
2443 * *
2444\*****************************************************************************/
2445
Adrian Hunterfc605f12016-10-05 12:11:21 +03002446static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002447{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002448 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002449 /*
2450 * SDHCI recovers from errors by resetting the cmd and data
2451 * circuits. Until that is done, there very well might be more
2452 * interrupts, so ignore them in that case.
2453 */
2454 if (host->pending_reset)
2455 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002456 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2457 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002458 sdhci_dumpregs(host);
2459 return;
2460 }
2461
Russell Kingec014cb2016-01-26 13:39:39 +00002462 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2463 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2464 if (intmask & SDHCI_INT_TIMEOUT)
2465 host->cmd->error = -ETIMEDOUT;
2466 else
2467 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002468
Russell King71fcbda2016-01-26 13:39:45 +00002469 /*
2470 * If this command initiates a data phase and a response
2471 * CRC error is signalled, the card can start transferring
2472 * data - the card may have received the command without
2473 * error. We must not terminate the mmc_request early.
2474 *
2475 * If the card did not receive the command or returned an
2476 * error which prevented it sending data, the data phase
2477 * will time out.
2478 */
2479 if (host->cmd->data &&
2480 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2481 SDHCI_INT_CRC) {
2482 host->cmd = NULL;
2483 return;
2484 }
2485
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002486 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002487 return;
2488 }
2489
Pierre Ossmane8095172008-07-25 01:09:08 +02002490 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002491 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002492}
2493
George G. Davis0957c332010-02-18 12:32:12 -05002494#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002495static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002496{
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002497 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002498
2499 sdhci_dumpregs(host);
2500
2501 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002502 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002503
Adrian Huntere57a5f62014-11-04 12:42:46 +02002504 if (host->flags & SDHCI_USE_64_BIT_DMA)
Adrian Hunterf4218652017-03-20 19:50:39 +02002505 DBG("%p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2506 desc, le32_to_cpu(dma_desc->addr_hi),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002507 le32_to_cpu(dma_desc->addr_lo),
2508 le16_to_cpu(dma_desc->len),
2509 le16_to_cpu(dma_desc->cmd));
2510 else
Adrian Hunterf4218652017-03-20 19:50:39 +02002511 DBG("%p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2512 desc, le32_to_cpu(dma_desc->addr_lo),
Adrian Huntere57a5f62014-11-04 12:42:46 +02002513 le16_to_cpu(dma_desc->len),
2514 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002515
Adrian Hunter76fe3792014-11-04 12:42:42 +02002516 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002517
Adrian Hunter05452302014-11-04 12:42:45 +02002518 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002519 break;
2520 }
2521}
2522#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002523static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002524#endif
2525
Pierre Ossmand129bce2006-03-24 03:18:17 -08002526static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2527{
Girish K S069c9f12012-01-06 09:56:39 +05302528 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002529
Arindam Nathb513ea22011-05-05 12:19:04 +05302530 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2531 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302532 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2533 if (command == MMC_SEND_TUNING_BLOCK ||
2534 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302535 host->tuning_done = 1;
2536 wake_up(&host->buf_ready_int);
2537 return;
2538 }
2539 }
2540
Pierre Ossmand129bce2006-03-24 03:18:17 -08002541 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002542 struct mmc_command *data_cmd = host->data_cmd;
2543
Pierre Ossmand129bce2006-03-24 03:18:17 -08002544 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002545 * The "data complete" interrupt is also used to
2546 * indicate that a busy state has ended. See comment
2547 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002548 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002549 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002550 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002551 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002552 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002553 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002554 return;
2555 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002556 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002557 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002558 /*
2559 * Some cards handle busy-end interrupt
2560 * before the command completed, so make
2561 * sure we do things in the proper order.
2562 */
Adrian Hunterea968022016-06-29 16:24:24 +03002563 if (host->cmd == data_cmd)
2564 return;
2565
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002566 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002567 return;
2568 }
2569 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002570
Adrian Huntered1563d2016-06-29 16:24:29 +03002571 /*
2572 * SDHCI recovers from errors by resetting the cmd and data
2573 * circuits. Until that is done, there very well might be more
2574 * interrupts, so ignore them in that case.
2575 */
2576 if (host->pending_reset)
2577 return;
2578
Marek Vasut2e4456f2015-11-18 10:47:02 +01002579 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2580 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002581 sdhci_dumpregs(host);
2582
2583 return;
2584 }
2585
2586 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002587 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002588 else if (intmask & SDHCI_INT_DATA_END_BIT)
2589 host->data->error = -EILSEQ;
2590 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2591 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2592 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002593 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002594 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302595 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002596 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002597 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002598 if (host->ops->adma_workaround)
2599 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002600 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002601
Pierre Ossman17b04292007-07-22 22:18:46 +02002602 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002603 sdhci_finish_data(host);
2604 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002605 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002606 sdhci_transfer_pio(host);
2607
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002608 /*
2609 * We currently don't do anything fancy with DMA
2610 * boundaries, but as we can't disable the feature
2611 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002612 *
2613 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2614 * should return a valid address to continue from, but as
2615 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002616 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002617 if (intmask & SDHCI_INT_DMA_END) {
2618 u32 dmastart, dmanow;
2619 dmastart = sg_dma_address(host->data->sg);
2620 dmanow = dmastart + host->data->bytes_xfered;
2621 /*
2622 * Force update to the next DMA block boundary.
2623 */
2624 dmanow = (dmanow &
2625 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2626 SDHCI_DEFAULT_BOUNDARY_SIZE;
2627 host->data->bytes_xfered = dmanow - dmastart;
Adrian Hunterf4218652017-03-20 19:50:39 +02002628 DBG("DMA base 0x%08x, transferred 0x%06x bytes, next 0x%08x\n",
2629 dmastart, host->data->bytes_xfered, dmanow);
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002630 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2631 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002632
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002633 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002634 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002635 /*
2636 * Data managed to finish before the
2637 * command completed. Make sure we do
2638 * things in the proper order.
2639 */
2640 host->data_early = 1;
2641 } else {
2642 sdhci_finish_data(host);
2643 }
2644 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002645 }
2646}
2647
David Howells7d12e782006-10-05 14:55:46 +01002648static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002649{
Russell King781e9892014-04-25 12:55:46 +01002650 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002651 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002652 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002653 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002654
2655 spin_lock(&host->lock);
2656
Russell Kingbe138552014-04-25 12:55:56 +01002657 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002658 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002659 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002660 }
2661
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002662 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002663 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002664 result = IRQ_NONE;
2665 goto out;
2666 }
2667
Russell King41005002014-04-25 12:55:36 +01002668 do {
2669 /* Clear selected interrupts. */
2670 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2671 SDHCI_INT_BUS_POWER);
2672 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002673
Adrian Hunterf4218652017-03-20 19:50:39 +02002674 DBG("IRQ status 0x%08x\n", intmask);
Russell King41005002014-04-25 12:55:36 +01002675
2676 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2677 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2678 SDHCI_CARD_PRESENT;
2679
2680 /*
2681 * There is a observation on i.mx esdhc. INSERT
2682 * bit will be immediately set again when it gets
2683 * cleared, if a card is inserted. We have to mask
2684 * the irq to prevent interrupt storm which will
2685 * freeze the system. And the REMOVE gets the
2686 * same situation.
2687 *
2688 * More testing are needed here to ensure it works
2689 * for other platforms though.
2690 */
Russell Kingb537f942014-04-25 12:56:01 +01002691 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2692 SDHCI_INT_CARD_REMOVE);
2693 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2694 SDHCI_INT_CARD_INSERT;
2695 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2696 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002697
2698 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2699 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002700
2701 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2702 SDHCI_INT_CARD_REMOVE);
2703 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002704 }
2705
2706 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002707 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002708
2709 if (intmask & SDHCI_INT_DATA_MASK)
2710 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2711
2712 if (intmask & SDHCI_INT_BUS_POWER)
2713 pr_err("%s: Card is consuming too much power!\n",
2714 mmc_hostname(host->mmc));
2715
Dong Aishengf37b20e2016-07-12 15:46:17 +08002716 if (intmask & SDHCI_INT_RETUNE)
2717 mmc_retune_needed(host->mmc);
2718
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002719 if ((intmask & SDHCI_INT_CARD_INT) &&
2720 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002721 sdhci_enable_sdio_irq_nolock(host, false);
2722 host->thread_isr |= SDHCI_INT_CARD_INT;
2723 result = IRQ_WAKE_THREAD;
2724 }
Russell King41005002014-04-25 12:55:36 +01002725
2726 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2727 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2728 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002729 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002730
2731 if (intmask) {
2732 unexpected |= intmask;
2733 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2734 }
2735
Russell King781e9892014-04-25 12:55:46 +01002736 if (result == IRQ_NONE)
2737 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002738
2739 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002740 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002741out:
2742 spin_unlock(&host->lock);
2743
Alexander Stein6379b232012-03-14 09:52:10 +01002744 if (unexpected) {
2745 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2746 mmc_hostname(host->mmc), unexpected);
2747 sdhci_dumpregs(host);
2748 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002749
Pierre Ossmand129bce2006-03-24 03:18:17 -08002750 return result;
2751}
2752
Russell King781e9892014-04-25 12:55:46 +01002753static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2754{
2755 struct sdhci_host *host = dev_id;
2756 unsigned long flags;
2757 u32 isr;
2758
2759 spin_lock_irqsave(&host->lock, flags);
2760 isr = host->thread_isr;
2761 host->thread_isr = 0;
2762 spin_unlock_irqrestore(&host->lock, flags);
2763
Russell King3560db82014-04-25 12:55:51 +01002764 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002765 struct mmc_host *mmc = host->mmc;
2766
2767 mmc->ops->card_event(mmc);
2768 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002769 }
2770
Russell King781e9892014-04-25 12:55:46 +01002771 if (isr & SDHCI_INT_CARD_INT) {
2772 sdio_run_irqs(host->mmc);
2773
2774 spin_lock_irqsave(&host->lock, flags);
2775 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2776 sdhci_enable_sdio_irq_nolock(host, true);
2777 spin_unlock_irqrestore(&host->lock, flags);
2778 }
2779
2780 return isr ? IRQ_HANDLED : IRQ_NONE;
2781}
2782
Pierre Ossmand129bce2006-03-24 03:18:17 -08002783/*****************************************************************************\
2784 * *
2785 * Suspend/resume *
2786 * *
2787\*****************************************************************************/
2788
2789#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002790/*
2791 * To enable wakeup events, the corresponding events have to be enabled in
2792 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2793 * Table' in the SD Host Controller Standard Specification.
2794 * It is useless to restore SDHCI_INT_ENABLE state in
2795 * sdhci_disable_irq_wakeups() since it will be set by
2796 * sdhci_enable_card_detection() or sdhci_init().
2797 */
Kevin Liuad080d72013-01-05 17:21:33 +08002798void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2799{
2800 u8 val;
2801 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2802 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002803 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2804 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002805
2806 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2807 val |= mask ;
2808 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002809 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002810 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002811 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2812 }
Kevin Liuad080d72013-01-05 17:21:33 +08002813 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002814 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002815}
2816EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2817
Fabio Estevam0b10f472014-08-30 14:53:13 -03002818static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002819{
2820 u8 val;
2821 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2822 | SDHCI_WAKE_ON_INT;
2823
2824 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2825 val &= ~mask;
2826 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2827}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002828
Manuel Lauss29495aa2011-11-03 11:09:45 +01002829int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002830{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002831 sdhci_disable_card_detection(host);
2832
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002833 mmc_retune_timer_stop(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302834
Kevin Liuad080d72013-01-05 17:21:33 +08002835 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002836 host->ier = 0;
2837 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2838 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002839 free_irq(host->irq, host);
2840 } else {
2841 sdhci_enable_irq_wakeups(host);
2842 enable_irq_wake(host->irq);
2843 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002844 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002845}
2846
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002847EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002848
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002849int sdhci_resume_host(struct sdhci_host *host)
2850{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002851 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002852 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002853
Richard Röjforsa13abc72009-09-22 16:45:30 -07002854 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002855 if (host->ops->enable_dma)
2856 host->ops->enable_dma(host);
2857 }
2858
Adrian Hunter6308d292012-02-07 14:48:54 +02002859 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2860 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2861 /* Card keeps power but host controller does not */
2862 sdhci_init(host, 0);
2863 host->pwr = 0;
2864 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002865 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002866 } else {
2867 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2868 mmiowb();
2869 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002870
Haibo Chen14a7b41642015-09-15 18:32:58 +08002871 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2872 ret = request_threaded_irq(host->irq, sdhci_irq,
2873 sdhci_thread_irq, IRQF_SHARED,
2874 mmc_hostname(host->mmc), host);
2875 if (ret)
2876 return ret;
2877 } else {
2878 sdhci_disable_irq_wakeups(host);
2879 disable_irq_wake(host->irq);
2880 }
2881
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002882 sdhci_enable_card_detection(host);
2883
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002884 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002885}
2886
2887EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002888
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002889int sdhci_runtime_suspend_host(struct sdhci_host *host)
2890{
2891 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002892
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002893 mmc_retune_timer_stop(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002894
2895 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002896 host->ier &= SDHCI_INT_CARD_INT;
2897 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2898 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002899 spin_unlock_irqrestore(&host->lock, flags);
2900
Russell King781e9892014-04-25 12:55:46 +01002901 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002902
2903 spin_lock_irqsave(&host->lock, flags);
2904 host->runtime_suspended = true;
2905 spin_unlock_irqrestore(&host->lock, flags);
2906
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002907 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002908}
2909EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2910
2911int sdhci_runtime_resume_host(struct sdhci_host *host)
2912{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002913 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002914 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002915 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002916
2917 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2918 if (host->ops->enable_dma)
2919 host->ops->enable_dma(host);
2920 }
2921
2922 sdhci_init(host, 0);
2923
Adrian Hunter84ec0482016-12-19 15:33:11 +02002924 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED) {
2925 /* Force clock and power re-program */
2926 host->pwr = 0;
2927 host->clock = 0;
2928 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2929 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002930
Adrian Hunter84ec0482016-12-19 15:33:11 +02002931 if ((host_flags & SDHCI_PV_ENABLED) &&
2932 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2933 spin_lock_irqsave(&host->lock, flags);
2934 sdhci_enable_preset_value(host, true);
2935 spin_unlock_irqrestore(&host->lock, flags);
2936 }
2937
2938 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2939 mmc->ops->hs400_enhanced_strobe)
2940 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002941 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002942
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002943 spin_lock_irqsave(&host->lock, flags);
2944
2945 host->runtime_suspended = false;
2946
2947 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002948 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002949 sdhci_enable_sdio_irq_nolock(host, true);
2950
2951 /* Enable Card Detection */
2952 sdhci_enable_card_detection(host);
2953
2954 spin_unlock_irqrestore(&host->lock, flags);
2955
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002956 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002957}
2958EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2959
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002960#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002961
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962/*****************************************************************************\
2963 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002964 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002965 * *
2966\*****************************************************************************/
2967
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002968struct sdhci_host *sdhci_alloc_host(struct device *dev,
2969 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002970{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971 struct mmc_host *mmc;
2972 struct sdhci_host *host;
2973
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002974 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002975
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002976 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002977 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002978 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002979
2980 host = mmc_priv(mmc);
2981 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002982 host->mmc_host_ops = sdhci_ops;
2983 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002984
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002985 host->flags = SDHCI_SIGNALING_330;
2986
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002987 return host;
2988}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002989
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002990EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002991
Alexandre Courbot7b913692016-03-07 11:07:55 +09002992static int sdhci_set_dma_mask(struct sdhci_host *host)
2993{
2994 struct mmc_host *mmc = host->mmc;
2995 struct device *dev = mmc_dev(mmc);
2996 int ret = -EINVAL;
2997
2998 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2999 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3000
3001 /* Try 64-bit mask if hardware is capable of it */
3002 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3003 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3004 if (ret) {
3005 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3006 mmc_hostname(mmc));
3007 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3008 }
3009 }
3010
3011 /* 32-bit mask as default & fallback */
3012 if (ret) {
3013 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3014 if (ret)
3015 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3016 mmc_hostname(mmc));
3017 }
3018
3019 return ret;
3020}
3021
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003022void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3023{
3024 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003025 u64 dt_caps_mask = 0;
3026 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003027
3028 if (host->read_caps)
3029 return;
3030
3031 host->read_caps = true;
3032
3033 if (debug_quirks)
3034 host->quirks = debug_quirks;
3035
3036 if (debug_quirks2)
3037 host->quirks2 = debug_quirks2;
3038
3039 sdhci_do_reset(host, SDHCI_RESET_ALL);
3040
Zach Brown92e0c442016-11-02 10:26:16 -05003041 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3042 "sdhci-caps-mask", &dt_caps_mask);
3043 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3044 "sdhci-caps", &dt_caps);
3045
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003046 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3047 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3048
3049 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3050 return;
3051
Zach Brown92e0c442016-11-02 10:26:16 -05003052 if (caps) {
3053 host->caps = *caps;
3054 } else {
3055 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3056 host->caps &= ~lower_32_bits(dt_caps_mask);
3057 host->caps |= lower_32_bits(dt_caps);
3058 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003059
3060 if (host->version < SDHCI_SPEC_300)
3061 return;
3062
Zach Brown92e0c442016-11-02 10:26:16 -05003063 if (caps1) {
3064 host->caps1 = *caps1;
3065 } else {
3066 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3067 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3068 host->caps1 |= upper_32_bits(dt_caps);
3069 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003070}
3071EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3072
Adrian Hunter52f53362016-06-29 16:24:15 +03003073int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003074{
3075 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303076 u32 max_current_caps;
3077 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003078 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003079 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003080 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003081
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003082 WARN_ON(host == NULL);
3083 if (host == NULL)
3084 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003085
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003086 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003087
Jon Hunterefba1422016-07-12 14:53:36 +01003088 /*
3089 * If there are external regulators, get them. Note this must be done
3090 * early before resetting the host and reading the capabilities so that
3091 * the host can take the appropriate action if regulators are not
3092 * available.
3093 */
3094 ret = mmc_regulator_get_supply(mmc);
3095 if (ret == -EPROBE_DEFER)
3096 return ret;
3097
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003098 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003099
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003100 override_timeout_clk = host->timeout_clk;
3101
Zhangfei Gao85105c52010-08-06 07:10:01 +08003102 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003103 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3104 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003105 }
3106
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003107 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003108 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003109 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003110 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003111 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003112 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003113
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003114 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003115 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003116 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003117 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003118 }
3119
Arindam Nathf2119df2011-05-05 12:18:57 +05303120 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003121 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003122 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003123
3124 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3125 (host->flags & SDHCI_USE_ADMA)) {
3126 DBG("Disabling ADMA as it is marked broken\n");
3127 host->flags &= ~SDHCI_USE_ADMA;
3128 }
3129
Adrian Huntere57a5f62014-11-04 12:42:46 +02003130 /*
3131 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3132 * and *must* do 64-bit DMA. A driver has the opportunity to change
3133 * that during the first call to ->enable_dma(). Similarly
3134 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3135 * implement.
3136 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003137 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003138 host->flags |= SDHCI_USE_64_BIT_DMA;
3139
Richard Röjforsa13abc72009-09-22 16:45:30 -07003140 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003141 ret = sdhci_set_dma_mask(host);
3142
3143 if (!ret && host->ops->enable_dma)
3144 ret = host->ops->enable_dma(host);
3145
3146 if (ret) {
3147 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3148 mmc_hostname(mmc));
3149 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3150
3151 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003152 }
3153 }
3154
Adrian Huntere57a5f62014-11-04 12:42:46 +02003155 /* SDMA does not support 64-bit DMA */
3156 if (host->flags & SDHCI_USE_64_BIT_DMA)
3157 host->flags &= ~SDHCI_USE_SDMA;
3158
Pierre Ossman2134a922008-06-28 18:28:51 +02003159 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003160 dma_addr_t dma;
3161 void *buf;
3162
Pierre Ossman2134a922008-06-28 18:28:51 +02003163 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003164 * The DMA descriptor table size is calculated as the maximum
3165 * number of segments times 2, to allow for an alignment
3166 * descriptor for each segment, plus 1 for a nop end descriptor,
3167 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003168 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003169 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3170 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3171 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003172 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003173 } else {
3174 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3175 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003176 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003177 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003178
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003179 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003180 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3181 host->adma_table_sz, &dma, GFP_KERNEL);
3182 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003183 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003184 mmc_hostname(mmc));
3185 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003186 } else if ((dma + host->align_buffer_sz) &
3187 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003188 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3189 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003190 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003191 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3192 host->adma_table_sz, buf, dma);
3193 } else {
3194 host->align_buffer = buf;
3195 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003196
Russell Kinge66e61c2016-01-26 13:39:55 +00003197 host->adma_table = buf + host->align_buffer_sz;
3198 host->adma_addr = dma + host->align_buffer_sz;
3199 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003200 }
3201
Pierre Ossman76591502008-07-21 00:32:11 +02003202 /*
3203 * If we use DMA, then it's up to the caller to set the DMA
3204 * mask, but PIO does not need the hw shim so we set a new
3205 * mask here in that case.
3206 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003207 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003208 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003209 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003210 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003211
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003212 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003213 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003214 >> SDHCI_CLOCK_BASE_SHIFT;
3215 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003216 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003217 >> SDHCI_CLOCK_BASE_SHIFT;
3218
Pierre Ossmand129bce2006-03-24 03:18:17 -08003219 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003220 if (host->max_clk == 0 || host->quirks &
3221 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003222 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003223 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3224 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003225 ret = -ENODEV;
3226 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003227 }
3228 host->max_clk = host->ops->get_max_clock(host);
3229 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003230
3231 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303232 * In case of Host Controller v3.00, find out whether clock
3233 * multiplier is supported.
3234 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003235 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303236 SDHCI_CLOCK_MUL_SHIFT;
3237
3238 /*
3239 * In case the value in Clock Multiplier is 0, then programmable
3240 * clock mode is not supported, otherwise the actual clock
3241 * multiplier is one more than the value of Clock Multiplier
3242 * in the Capabilities Register.
3243 */
3244 if (host->clk_mul)
3245 host->clk_mul += 1;
3246
3247 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003248 * Set host parameters.
3249 */
Dong Aisheng59241752015-07-22 20:53:07 +08003250 max_clk = host->max_clk;
3251
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003252 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003253 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303254 else if (host->version >= SDHCI_SPEC_300) {
3255 if (host->clk_mul) {
3256 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003257 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303258 } else
3259 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3260 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003261 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003262
Adrian Hunterd310ae42016-04-12 14:25:07 +03003263 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003264 mmc->f_max = max_clk;
3265
Aisheng Dong28aab052014-08-27 15:26:31 +08003266 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003267 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003268 SDHCI_TIMEOUT_CLK_SHIFT;
3269 if (host->timeout_clk == 0) {
3270 if (host->ops->get_timeout_clock) {
3271 host->timeout_clk =
3272 host->ops->get_timeout_clock(host);
3273 } else {
3274 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3275 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003276 ret = -ENODEV;
3277 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003278 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003279 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003280
Adrian Hunter28da3582016-06-29 16:24:17 +03003281 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003282 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003283
Adrian Hunter99513622016-03-07 13:33:55 +02003284 if (override_timeout_clk)
3285 host->timeout_clk = override_timeout_clk;
3286
Aisheng Dong28aab052014-08-27 15:26:31 +08003287 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003288 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003289 mmc->max_busy_timeout /= host->timeout_clk;
3290 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003291
Andrei Warkentine89d4562011-05-23 15:06:37 -05003292 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003293 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003294
3295 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3296 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003297
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003298 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003299 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003300 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003301 !(host->flags & SDHCI_USE_SDMA)) &&
3302 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003303 host->flags |= SDHCI_AUTO_CMD23;
Adrian Hunterf4218652017-03-20 19:50:39 +02003304 DBG("Auto-CMD23 available\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003305 } else {
Adrian Hunterf4218652017-03-20 19:50:39 +02003306 DBG("Auto-CMD23 unavailable\n");
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003307 }
3308
Philip Rakity15ec4462010-11-19 16:48:39 -05003309 /*
3310 * A controller may support 8-bit width, but the board itself
3311 * might not have the pins brought out. Boards that support
3312 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3313 * their platform code before calling sdhci_add_host(), and we
3314 * won't assume 8-bit width for hosts without that CAP.
3315 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003316 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003317 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003318
Jerry Huang63ef5d82012-10-25 13:47:19 +08003319 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3320 mmc->caps &= ~MMC_CAP_CMD23;
3321
Adrian Hunter28da3582016-06-29 16:24:17 +03003322 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003323 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003324
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003325 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003326 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003327 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003328 mmc->caps |= MMC_CAP_NEEDS_POLL;
3329
Philip Rakity6231f3d2012-07-23 15:56:23 -07003330 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003331 if (!IS_ERR(mmc->supply.vqmmc)) {
3332 ret = regulator_enable(mmc->supply.vqmmc);
3333 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3334 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003335 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3336 SDHCI_SUPPORT_SDR50 |
3337 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003338 if (ret) {
3339 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3340 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003341 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003342 }
Kevin Liu8363c372012-11-17 17:55:51 -05003343 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003344
Adrian Hunter28da3582016-06-29 16:24:17 +03003345 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3346 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3347 SDHCI_SUPPORT_DDR50);
3348 }
Daniel Drake6a661802012-11-25 13:01:19 -05003349
Al Cooper4188bba2012-03-16 15:54:17 -04003350 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003351 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3352 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303353 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3354
3355 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003356 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303357 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003358 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3359 * field can be promoted to support HS200.
3360 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003361 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003362 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003363 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303364 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003365 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303366
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003367 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003368 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003369 mmc->caps2 |= MMC_CAP2_HS400;
3370
Adrian Hunter549c0b12014-11-06 15:19:05 +02003371 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3372 (IS_ERR(mmc->supply.vqmmc) ||
3373 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3374 1300000)))
3375 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3376
Adrian Hunter28da3582016-06-29 16:24:17 +03003377 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3378 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303379 mmc->caps |= MMC_CAP_UHS_DDR50;
3380
Girish K S069c9f12012-01-06 09:56:39 +05303381 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003382 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303383 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3384
Arindam Nathd6d50a12011-05-05 12:18:59 +05303385 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003386 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303387 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003388 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303389 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003390 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303391 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3392
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303393 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003394 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3395 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303396
3397 /*
3398 * In case Re-tuning Timer is not disabled, the actual value of
3399 * re-tuning timer will be 2 ^ (n - 1).
3400 */
3401 if (host->tuning_count)
3402 host->tuning_count = 1 << (host->tuning_count - 1);
3403
3404 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003405 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303406 SDHCI_RETUNING_MODE_SHIFT;
3407
Takashi Iwai8f230f42010-12-08 10:04:30 +01003408 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003409
Arindam Nathf2119df2011-05-05 12:18:57 +05303410 /*
3411 * According to SD Host Controller spec v3.00, if the Host System
3412 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3413 * the value is meaningful only if Voltage Support in the Capabilities
3414 * register is set. The actual current value is 4 times the register
3415 * value.
3416 */
3417 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003418 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003419 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003420 if (curr > 0) {
3421
3422 /* convert to SDHCI_MAX_CURRENT format */
3423 curr = curr/1000; /* convert to mA */
3424 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3425
3426 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3427 max_current_caps =
3428 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3429 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3430 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3431 }
3432 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303433
Adrian Hunter28da3582016-06-29 16:24:17 +03003434 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003435 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303436
Aaron Lu55c46652012-07-04 13:31:48 +08003437 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303438 SDHCI_MAX_CURRENT_330_MASK) >>
3439 SDHCI_MAX_CURRENT_330_SHIFT) *
3440 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303441 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003442 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003443 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303444
Aaron Lu55c46652012-07-04 13:31:48 +08003445 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303446 SDHCI_MAX_CURRENT_300_MASK) >>
3447 SDHCI_MAX_CURRENT_300_SHIFT) *
3448 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303449 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003450 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003451 ocr_avail |= MMC_VDD_165_195;
3452
Aaron Lu55c46652012-07-04 13:31:48 +08003453 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303454 SDHCI_MAX_CURRENT_180_MASK) >>
3455 SDHCI_MAX_CURRENT_180_SHIFT) *
3456 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303457 }
3458
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003459 /* If OCR set by host, use it instead. */
3460 if (host->ocr_mask)
3461 ocr_avail = host->ocr_mask;
3462
3463 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003464 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003465 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003466
Takashi Iwai8f230f42010-12-08 10:04:30 +01003467 mmc->ocr_avail = ocr_avail;
3468 mmc->ocr_avail_sdio = ocr_avail;
3469 if (host->ocr_avail_sdio)
3470 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3471 mmc->ocr_avail_sd = ocr_avail;
3472 if (host->ocr_avail_sd)
3473 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3474 else /* normal SD controllers don't support 1.8V */
3475 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3476 mmc->ocr_avail_mmc = ocr_avail;
3477 if (host->ocr_avail_mmc)
3478 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003479
3480 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003481 pr_err("%s: Hardware doesn't report any support voltages.\n",
3482 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003483 ret = -ENODEV;
3484 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003485 }
3486
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003487 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3488 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3489 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3490 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3491 host->flags |= SDHCI_SIGNALING_180;
3492
3493 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3494 host->flags |= SDHCI_SIGNALING_120;
3495
Pierre Ossmand129bce2006-03-24 03:18:17 -08003496 spin_lock_init(&host->lock);
3497
3498 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003499 * Maximum number of segments. Depends on if the hardware
3500 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003501 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003502 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003503 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003504 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003505 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003506 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003507 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003508
3509 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003510 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3511 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3512 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003513 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003514 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003515
3516 /*
3517 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003518 * of bytes. When doing hardware scatter/gather, each entry cannot
3519 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003520 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003521 if (host->flags & SDHCI_USE_ADMA) {
3522 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3523 mmc->max_seg_size = 65535;
3524 else
3525 mmc->max_seg_size = 65536;
3526 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003527 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003528 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003529
3530 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003531 * Maximum block size. This varies from controller to controller and
3532 * is specified in the capabilities register.
3533 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003534 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3535 mmc->max_blk_size = 2;
3536 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003537 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003538 SDHCI_MAX_BLOCK_SHIFT;
3539 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003540 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3541 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003542 mmc->max_blk_size = 0;
3543 }
3544 }
3545
3546 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003547
3548 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003549 * Maximum block count.
3550 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003551 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003552
Adrian Hunter52f53362016-06-29 16:24:15 +03003553 return 0;
3554
3555unreg:
3556 if (!IS_ERR(mmc->supply.vqmmc))
3557 regulator_disable(mmc->supply.vqmmc);
3558undma:
3559 if (host->align_buffer)
3560 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3561 host->adma_table_sz, host->align_buffer,
3562 host->align_addr);
3563 host->adma_table = NULL;
3564 host->align_buffer = NULL;
3565
3566 return ret;
3567}
3568EXPORT_SYMBOL_GPL(sdhci_setup_host);
3569
3570int __sdhci_add_host(struct sdhci_host *host)
3571{
3572 struct mmc_host *mmc = host->mmc;
3573 int ret;
3574
Pierre Ossman55db8902006-11-21 17:55:45 +01003575 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003576 * Init tasklets.
3577 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003578 tasklet_init(&host->finish_tasklet,
3579 sdhci_tasklet_finish, (unsigned long)host);
3580
Al Viroe4cad1b2006-10-10 22:47:07 +01003581 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003582 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3583 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003584
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003585 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303586
Shawn Guo2af502c2013-07-05 14:38:55 +08003587 sdhci_init(host, 0);
3588
Russell King781e9892014-04-25 12:55:46 +01003589 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3590 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003591 if (ret) {
3592 pr_err("%s: Failed to request IRQ %d: %d\n",
3593 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003594 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003595 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003596
Pierre Ossmand129bce2006-03-24 03:18:17 -08003597#ifdef CONFIG_MMC_DEBUG
3598 sdhci_dumpregs(host);
3599#endif
3600
Adrian Hunter061d17a2016-04-12 14:25:09 +03003601 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003602 if (ret) {
3603 pr_err("%s: Failed to register LED device: %d\n",
3604 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003605 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003606 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003607
Pierre Ossman5f25a662006-10-04 02:15:39 -07003608 mmiowb();
3609
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003610 ret = mmc_add_host(mmc);
3611 if (ret)
3612 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003613
Girish K Sa3c76eb2011-10-11 11:44:09 +05303614 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003615 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003616 (host->flags & SDHCI_USE_ADMA) ?
3617 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003618 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003619
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003620 sdhci_enable_card_detection(host);
3621
Pierre Ossmand129bce2006-03-24 03:18:17 -08003622 return 0;
3623
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003624unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003625 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003626unirq:
Russell King03231f92014-04-25 12:57:12 +01003627 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003628 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3629 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003630 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003631untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003632 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003633
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003634 if (!IS_ERR(mmc->supply.vqmmc))
3635 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003636
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003637 if (host->align_buffer)
3638 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3639 host->adma_table_sz, host->align_buffer,
3640 host->align_addr);
3641 host->adma_table = NULL;
3642 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003643
3644 return ret;
3645}
Adrian Hunter52f53362016-06-29 16:24:15 +03003646EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003647
Adrian Hunter52f53362016-06-29 16:24:15 +03003648int sdhci_add_host(struct sdhci_host *host)
3649{
3650 int ret;
3651
3652 ret = sdhci_setup_host(host);
3653 if (ret)
3654 return ret;
3655
3656 return __sdhci_add_host(host);
3657}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003658EXPORT_SYMBOL_GPL(sdhci_add_host);
3659
Pierre Ossman1e728592008-04-16 19:13:13 +02003660void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003661{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003662 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003663 unsigned long flags;
3664
3665 if (dead) {
3666 spin_lock_irqsave(&host->lock, flags);
3667
3668 host->flags |= SDHCI_DEVICE_DEAD;
3669
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003670 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303671 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003672 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003673 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003674 }
3675
3676 spin_unlock_irqrestore(&host->lock, flags);
3677 }
3678
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003679 sdhci_disable_card_detection(host);
3680
Markus Mayer4e743f12014-07-03 13:27:42 -07003681 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003682
Adrian Hunter061d17a2016-04-12 14:25:09 +03003683 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003684
Pierre Ossman1e728592008-04-16 19:13:13 +02003685 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003686 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003687
Russell Kingb537f942014-04-25 12:56:01 +01003688 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3689 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003690 free_irq(host->irq, host);
3691
3692 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003693 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003694
Pierre Ossmand129bce2006-03-24 03:18:17 -08003695 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003696
Tim Kryger3a48edc2014-06-13 10:13:56 -07003697 if (!IS_ERR(mmc->supply.vqmmc))
3698 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003699
Russell Kingedd63fc2016-01-26 13:39:50 +00003700 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003701 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3702 host->adma_table_sz, host->align_buffer,
3703 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003704
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003705 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003706 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003707}
3708
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003709EXPORT_SYMBOL_GPL(sdhci_remove_host);
3710
3711void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003712{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003713 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003714}
3715
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003716EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003717
3718/*****************************************************************************\
3719 * *
3720 * Driver init/exit *
3721 * *
3722\*****************************************************************************/
3723
3724static int __init sdhci_drv_init(void)
3725{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303726 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003727 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303728 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003729
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003730 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003731}
3732
3733static void __exit sdhci_drv_exit(void)
3734{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003735}
3736
3737module_init(sdhci_drv_init);
3738module_exit(sdhci_drv_exit);
3739
Pierre Ossmandf673b22006-06-30 02:22:31 -07003740module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003741module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003742
Pierre Ossman32710e82009-04-08 20:14:54 +02003743MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003744MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003745MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003746
Pierre Ossmandf673b22006-06-30 02:22:31 -07003747MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003748MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");