blob: 777c82efce1d5286ecc98ac3c943199f59f878f1 [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...) \
Russell Kingc6563172006-03-29 09:30:20 +010041 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080042
Arindam Nathb513ea22011-05-05 12:19:04 +053043#define MAX_TUNING_LOOP 40
44
Pierre Ossmandf673b22006-06-30 02:22:31 -070045static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030046static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070047
Pierre Ossmand129bce2006-03-24 03:18:17 -080048static void sdhci_finish_data(struct sdhci_host *);
49
Kevin Liu52983382013-01-31 11:31:37 +080050static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080051
52static void sdhci_dumpregs(struct sdhci_host *host)
53{
Chuanxiao Donga7c53672016-06-22 14:40:01 +030054 pr_err(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
55 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080056
Chuanxiao Donga7c53672016-06-22 14:40:01 +030057 pr_err(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
58 sdhci_readl(host, SDHCI_DMA_ADDRESS),
59 sdhci_readw(host, SDHCI_HOST_VERSION));
60 pr_err(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
61 sdhci_readw(host, SDHCI_BLOCK_SIZE),
62 sdhci_readw(host, SDHCI_BLOCK_COUNT));
63 pr_err(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
64 sdhci_readl(host, SDHCI_ARGUMENT),
65 sdhci_readw(host, SDHCI_TRANSFER_MODE));
66 pr_err(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
67 sdhci_readl(host, SDHCI_PRESENT_STATE),
68 sdhci_readb(host, SDHCI_HOST_CONTROL));
69 pr_err(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
70 sdhci_readb(host, SDHCI_POWER_CONTROL),
71 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
72 pr_err(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
73 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
74 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
75 pr_err(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
76 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
77 sdhci_readl(host, SDHCI_INT_STATUS));
78 pr_err(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
79 sdhci_readl(host, SDHCI_INT_ENABLE),
80 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
81 pr_err(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
82 sdhci_readw(host, SDHCI_ACMD12_ERR),
83 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
84 pr_err(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
85 sdhci_readl(host, SDHCI_CAPABILITIES),
86 sdhci_readl(host, SDHCI_CAPABILITIES_1));
87 pr_err(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
88 sdhci_readw(host, SDHCI_COMMAND),
89 sdhci_readl(host, SDHCI_MAX_CURRENT));
90 pr_err(DRIVER_NAME ": Host ctl2: 0x%08x\n",
91 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080092
Adrian Huntere57a5f62014-11-04 12:42:46 +020093 if (host->flags & SDHCI_USE_ADMA) {
94 if (host->flags & SDHCI_USE_64_BIT_DMA)
Chuanxiao Donga7c53672016-06-22 14:40:01 +030095 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
96 readl(host->ioaddr + SDHCI_ADMA_ERROR),
97 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
98 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +020099 else
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300100 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
101 readl(host->ioaddr + SDHCI_ADMA_ERROR),
102 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +0200103 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100104
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300105 pr_err(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800106}
107
108/*****************************************************************************\
109 * *
110 * Low level functions *
111 * *
112\*****************************************************************************/
113
Adrian Hunter56a590d2016-06-29 16:24:32 +0300114static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
115{
116 return cmd->data || cmd->flags & MMC_RSP_BUSY;
117}
118
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300119static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
120{
Russell King5b4f1f62014-04-25 12:57:02 +0100121 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300122
Adrian Hunterc79396c2011-12-27 15:48:42 +0200123 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900124 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300125 return;
126
Russell King5b4f1f62014-04-25 12:57:02 +0100127 if (enable) {
128 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
129 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800130
Russell King5b4f1f62014-04-25 12:57:02 +0100131 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
132 SDHCI_INT_CARD_INSERT;
133 } else {
134 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
135 }
Russell Kingb537f942014-04-25 12:56:01 +0100136
137 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
138 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300139}
140
141static void sdhci_enable_card_detection(struct sdhci_host *host)
142{
143 sdhci_set_card_detection(host, true);
144}
145
146static void sdhci_disable_card_detection(struct sdhci_host *host)
147{
148 sdhci_set_card_detection(host, false);
149}
150
Ulf Hansson02d0b682016-04-11 15:32:41 +0200151static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
152{
153 if (host->bus_on)
154 return;
155 host->bus_on = true;
156 pm_runtime_get_noresume(host->mmc->parent);
157}
158
159static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
160{
161 if (!host->bus_on)
162 return;
163 host->bus_on = false;
164 pm_runtime_put_noidle(host->mmc->parent);
165}
166
Russell King03231f92014-04-25 12:57:12 +0100167void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800168{
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200169 ktime_t timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800170
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300171 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800172
Adrian Hunterf0710a52013-05-06 12:17:32 +0300173 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800174 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300175 /* Reset-all turns off SD Bus Power */
176 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
177 sdhci_runtime_pm_bus_off(host);
178 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800179
Pierre Ossmane16514d82006-06-30 02:22:24 -0700180 /* Wait max 100 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200181 timeout = ktime_add_ms(ktime_get(), 100);
Pierre Ossmane16514d82006-06-30 02:22:24 -0700182
183 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300184 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200185 if (ktime_after(ktime_get(), timeout)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530186 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700187 mmc_hostname(host->mmc), (int)mask);
188 sdhci_dumpregs(host);
189 return;
190 }
Adrian Hunter5a436cc2017-03-20 19:50:31 +0200191 udelay(10);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800192 }
Russell King03231f92014-04-25 12:57:12 +0100193}
194EXPORT_SYMBOL_GPL(sdhci_reset);
Anton Vorontsov063a9db2009-03-17 00:14:02 +0300195
Russell King03231f92014-04-25 12:57:12 +0100196static void sdhci_do_reset(struct sdhci_host *host, u8 mask)
197{
198 if (host->quirks & SDHCI_QUIRK_NO_CARD_NO_RESET) {
Adrian Hunterd3940f22016-06-29 16:24:14 +0300199 struct mmc_host *mmc = host->mmc;
200
201 if (!mmc->ops->get_cd(mmc))
Russell King03231f92014-04-25 12:57:12 +0100202 return;
203 }
204
205 host->ops->reset(host, mask);
Philip Rakity393c1a32011-01-21 11:26:40 -0800206
Russell Kingda91a8f2014-04-25 13:00:12 +0100207 if (mask & SDHCI_RESET_ALL) {
208 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
209 if (host->ops->enable_dma)
210 host->ops->enable_dma(host);
211 }
212
213 /* Resetting the controller clears many */
214 host->preset_enabled = false;
Shaohui Xie3abc1e802011-12-29 16:33:00 +0800215 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800216}
217
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800218static void sdhci_init(struct sdhci_host *host, int soft)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800219{
Adrian Hunterd3940f22016-06-29 16:24:14 +0300220 struct mmc_host *mmc = host->mmc;
221
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800222 if (soft)
Russell King03231f92014-04-25 12:57:12 +0100223 sdhci_do_reset(host, SDHCI_RESET_CMD|SDHCI_RESET_DATA);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800224 else
Russell King03231f92014-04-25 12:57:12 +0100225 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800226
Russell Kingb537f942014-04-25 12:56:01 +0100227 host->ier = SDHCI_INT_BUS_POWER | SDHCI_INT_DATA_END_BIT |
228 SDHCI_INT_DATA_CRC | SDHCI_INT_DATA_TIMEOUT |
229 SDHCI_INT_INDEX | SDHCI_INT_END_BIT | SDHCI_INT_CRC |
230 SDHCI_INT_TIMEOUT | SDHCI_INT_DATA_END |
231 SDHCI_INT_RESPONSE;
232
Dong Aishengf37b20e2016-07-12 15:46:17 +0800233 if (host->tuning_mode == SDHCI_TUNING_MODE_2 ||
234 host->tuning_mode == SDHCI_TUNING_MODE_3)
235 host->ier |= SDHCI_INT_RETUNE;
236
Russell Kingb537f942014-04-25 12:56:01 +0100237 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
238 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800239
240 if (soft) {
241 /* force clock reconfiguration */
242 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +0300243 mmc->ops->set_ios(mmc, &mmc->ios);
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800244 }
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300245}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800246
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300247static void sdhci_reinit(struct sdhci_host *host)
248{
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -0800249 sdhci_init(host, 0);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300250 sdhci_enable_card_detection(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800251}
252
Adrian Hunter061d17a2016-04-12 14:25:09 +0300253static void __sdhci_led_activate(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800254{
255 u8 ctrl;
256
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300257 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800258 ctrl |= SDHCI_CTRL_LED;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300259 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800260}
261
Adrian Hunter061d17a2016-04-12 14:25:09 +0300262static void __sdhci_led_deactivate(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
Masahiro Yamada4f782302016-04-14 13:19:39 +0900271#if IS_REACHABLE(CONFIG_LEDS_CLASS)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100272static void sdhci_led_control(struct led_classdev *led,
Adrian Hunter061d17a2016-04-12 14:25:09 +0300273 enum led_brightness brightness)
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100274{
275 struct sdhci_host *host = container_of(led, struct sdhci_host, led);
276 unsigned long flags;
277
278 spin_lock_irqsave(&host->lock, flags);
279
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300280 if (host->runtime_suspended)
281 goto out;
282
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100283 if (brightness == LED_OFF)
Adrian Hunter061d17a2016-04-12 14:25:09 +0300284 __sdhci_led_deactivate(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100285 else
Adrian Hunter061d17a2016-04-12 14:25:09 +0300286 __sdhci_led_activate(host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300287out:
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100288 spin_unlock_irqrestore(&host->lock, flags);
289}
Adrian Hunter061d17a2016-04-12 14:25:09 +0300290
291static int sdhci_led_register(struct sdhci_host *host)
292{
293 struct mmc_host *mmc = host->mmc;
294
295 snprintf(host->led_name, sizeof(host->led_name),
296 "%s::", mmc_hostname(mmc));
297
298 host->led.name = host->led_name;
299 host->led.brightness = LED_OFF;
300 host->led.default_trigger = mmc_hostname(mmc);
301 host->led.brightness_set = sdhci_led_control;
302
303 return led_classdev_register(mmc_dev(mmc), &host->led);
304}
305
306static void sdhci_led_unregister(struct sdhci_host *host)
307{
308 led_classdev_unregister(&host->led);
309}
310
311static inline void sdhci_led_activate(struct sdhci_host *host)
312{
313}
314
315static inline void sdhci_led_deactivate(struct sdhci_host *host)
316{
317}
318
319#else
320
321static inline int sdhci_led_register(struct sdhci_host *host)
322{
323 return 0;
324}
325
326static inline void sdhci_led_unregister(struct sdhci_host *host)
327{
328}
329
330static inline void sdhci_led_activate(struct sdhci_host *host)
331{
332 __sdhci_led_activate(host);
333}
334
335static inline void sdhci_led_deactivate(struct sdhci_host *host)
336{
337 __sdhci_led_deactivate(host);
338}
339
Pierre Ossman2f730fe2008-03-17 10:29:38 +0100340#endif
341
Pierre Ossmand129bce2006-03-24 03:18:17 -0800342/*****************************************************************************\
343 * *
344 * Core functions *
345 * *
346\*****************************************************************************/
347
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100348static void sdhci_read_block_pio(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800349{
Pierre Ossman76591502008-07-21 00:32:11 +0200350 unsigned long flags;
351 size_t blksize, len, chunk;
Steven Noonan7244b852008-10-01 01:50:25 -0700352 u32 uninitialized_var(scratch);
Pierre Ossman76591502008-07-21 00:32:11 +0200353 u8 *buf;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800354
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100355 DBG("PIO reading\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800356
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100357 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200358 chunk = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800359
Pierre Ossman76591502008-07-21 00:32:11 +0200360 local_irq_save(flags);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800361
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100362 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300363 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmand129bce2006-03-24 03:18:17 -0800364
Pierre Ossman76591502008-07-21 00:32:11 +0200365 len = min(host->sg_miter.length, blksize);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800366
Pierre Ossman76591502008-07-21 00:32:11 +0200367 blksize -= len;
368 host->sg_miter.consumed = len;
Alex Dubov14d836e2007-04-13 19:04:38 +0200369
Pierre Ossman76591502008-07-21 00:32:11 +0200370 buf = host->sg_miter.addr;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800371
Pierre Ossman76591502008-07-21 00:32:11 +0200372 while (len) {
373 if (chunk == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300374 scratch = sdhci_readl(host, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200375 chunk = 4;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800376 }
Pierre Ossman76591502008-07-21 00:32:11 +0200377
378 *buf = scratch & 0xFF;
379
380 buf++;
381 scratch >>= 8;
382 chunk--;
383 len--;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800384 }
385 }
Pierre Ossman76591502008-07-21 00:32:11 +0200386
387 sg_miter_stop(&host->sg_miter);
388
389 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100390}
Pierre Ossmand129bce2006-03-24 03:18:17 -0800391
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100392static void sdhci_write_block_pio(struct sdhci_host *host)
393{
Pierre Ossman76591502008-07-21 00:32:11 +0200394 unsigned long flags;
395 size_t blksize, len, chunk;
396 u32 scratch;
397 u8 *buf;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100398
399 DBG("PIO writing\n");
400
401 blksize = host->data->blksz;
Pierre Ossman76591502008-07-21 00:32:11 +0200402 chunk = 0;
403 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100404
Pierre Ossman76591502008-07-21 00:32:11 +0200405 local_irq_save(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100406
407 while (blksize) {
Fabio Estevambf3a35a2015-05-09 18:44:51 -0300408 BUG_ON(!sg_miter_next(&host->sg_miter));
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100409
Pierre Ossman76591502008-07-21 00:32:11 +0200410 len = min(host->sg_miter.length, blksize);
Alex Dubov14d836e2007-04-13 19:04:38 +0200411
Pierre Ossman76591502008-07-21 00:32:11 +0200412 blksize -= len;
413 host->sg_miter.consumed = len;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100414
Pierre Ossman76591502008-07-21 00:32:11 +0200415 buf = host->sg_miter.addr;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100416
Pierre Ossman76591502008-07-21 00:32:11 +0200417 while (len) {
418 scratch |= (u32)*buf << (chunk * 8);
419
420 buf++;
421 chunk++;
422 len--;
423
424 if ((chunk == 4) || ((len == 0) && (blksize == 0))) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300425 sdhci_writel(host, scratch, SDHCI_BUFFER);
Pierre Ossman76591502008-07-21 00:32:11 +0200426 chunk = 0;
427 scratch = 0;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100428 }
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100429 }
430 }
Pierre Ossman76591502008-07-21 00:32:11 +0200431
432 sg_miter_stop(&host->sg_miter);
433
434 local_irq_restore(flags);
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100435}
436
437static void sdhci_transfer_pio(struct sdhci_host *host)
438{
439 u32 mask;
440
Pierre Ossman76591502008-07-21 00:32:11 +0200441 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100442 return;
443
444 if (host->data->flags & MMC_DATA_READ)
445 mask = SDHCI_DATA_AVAILABLE;
446 else
447 mask = SDHCI_SPACE_AVAILABLE;
448
Pierre Ossman4a3cba32008-07-29 00:11:16 +0200449 /*
450 * Some controllers (JMicron JMB38x) mess up the buffer bits
451 * for transfers < 4 bytes. As long as it is just one block,
452 * we can ignore the bits.
453 */
454 if ((host->quirks & SDHCI_QUIRK_BROKEN_SMALL_PIO) &&
455 (host->data->blocks == 1))
456 mask = ~0;
457
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300458 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Anton Vorontsov3e3bf202009-03-17 00:14:00 +0300459 if (host->quirks & SDHCI_QUIRK_PIO_NEEDS_DELAY)
460 udelay(100);
461
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100462 if (host->data->flags & MMC_DATA_READ)
463 sdhci_read_block_pio(host);
464 else
465 sdhci_write_block_pio(host);
466
Pierre Ossman76591502008-07-21 00:32:11 +0200467 host->blocks--;
468 if (host->blocks == 0)
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100469 break;
Pierre Ossmana406f5a2006-07-02 16:50:59 +0100470 }
471
472 DBG("PIO transfer complete.\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800473}
474
Russell King48857d92016-01-26 13:40:16 +0000475static int sdhci_pre_dma_transfer(struct sdhci_host *host,
Russell Kingc0999b72016-01-26 13:40:27 +0000476 struct mmc_data *data, int cookie)
Russell King48857d92016-01-26 13:40:16 +0000477{
478 int sg_count;
479
Russell King94538e52016-01-26 13:40:37 +0000480 /*
481 * If the data buffers are already mapped, return the previous
482 * dma_map_sg() result.
483 */
484 if (data->host_cookie == COOKIE_PRE_MAPPED)
Russell King48857d92016-01-26 13:40:16 +0000485 return data->sg_count;
Russell King48857d92016-01-26 13:40:16 +0000486
487 sg_count = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
488 data->flags & MMC_DATA_WRITE ?
489 DMA_TO_DEVICE : DMA_FROM_DEVICE);
490
491 if (sg_count == 0)
492 return -ENOSPC;
493
494 data->sg_count = sg_count;
Russell Kingc0999b72016-01-26 13:40:27 +0000495 data->host_cookie = cookie;
Russell King48857d92016-01-26 13:40:16 +0000496
497 return sg_count;
498}
499
Pierre Ossman2134a922008-06-28 18:28:51 +0200500static char *sdhci_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
501{
502 local_irq_save(*flags);
Cong Wang482fce92011-11-27 13:27:00 +0800503 return kmap_atomic(sg_page(sg)) + sg->offset;
Pierre Ossman2134a922008-06-28 18:28:51 +0200504}
505
506static void sdhci_kunmap_atomic(void *buffer, unsigned long *flags)
507{
Cong Wang482fce92011-11-27 13:27:00 +0800508 kunmap_atomic(buffer);
Pierre Ossman2134a922008-06-28 18:28:51 +0200509 local_irq_restore(*flags);
510}
511
Adrian Huntere57a5f62014-11-04 12:42:46 +0200512static void sdhci_adma_write_desc(struct sdhci_host *host, void *desc,
513 dma_addr_t addr, int len, unsigned cmd)
Ben Dooks118cd172010-03-05 13:43:26 -0800514{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200515 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks118cd172010-03-05 13:43:26 -0800516
Adrian Huntere57a5f62014-11-04 12:42:46 +0200517 /* 32-bit and 64-bit descriptors have these members in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200518 dma_desc->cmd = cpu_to_le16(cmd);
519 dma_desc->len = cpu_to_le16(len);
Adrian Huntere57a5f62014-11-04 12:42:46 +0200520 dma_desc->addr_lo = cpu_to_le32((u32)addr);
521
522 if (host->flags & SDHCI_USE_64_BIT_DMA)
523 dma_desc->addr_hi = cpu_to_le32((u64)addr >> 32);
Ben Dooks118cd172010-03-05 13:43:26 -0800524}
525
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200526static void sdhci_adma_mark_end(void *desc)
527{
Adrian Huntere57a5f62014-11-04 12:42:46 +0200528 struct sdhci_adma2_64_desc *dma_desc = desc;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200529
Adrian Huntere57a5f62014-11-04 12:42:46 +0200530 /* 32-bit and 64-bit descriptors have 'cmd' in same position */
Adrian Hunter05452302014-11-04 12:42:45 +0200531 dma_desc->cmd |= cpu_to_le16(ADMA2_END);
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200532}
533
Russell King60c64762016-01-26 13:40:22 +0000534static void sdhci_adma_table_pre(struct sdhci_host *host,
535 struct mmc_data *data, int sg_count)
Pierre Ossman2134a922008-06-28 18:28:51 +0200536{
Pierre Ossman2134a922008-06-28 18:28:51 +0200537 struct scatterlist *sg;
Pierre Ossman2134a922008-06-28 18:28:51 +0200538 unsigned long flags;
Russell Kingacc3ad12016-01-26 13:40:00 +0000539 dma_addr_t addr, align_addr;
540 void *desc, *align;
541 char *buffer;
542 int len, offset, i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200543
544 /*
545 * The spec does not specify endianness of descriptor table.
546 * We currently guess that it is LE.
547 */
548
Russell King60c64762016-01-26 13:40:22 +0000549 host->sg_count = sg_count;
Pierre Ossman2134a922008-06-28 18:28:51 +0200550
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200551 desc = host->adma_table;
Pierre Ossman2134a922008-06-28 18:28:51 +0200552 align = host->align_buffer;
553
554 align_addr = host->align_addr;
555
556 for_each_sg(data->sg, sg, host->sg_count, i) {
557 addr = sg_dma_address(sg);
558 len = sg_dma_len(sg);
559
560 /*
Russell Kingacc3ad12016-01-26 13:40:00 +0000561 * The SDHCI specification states that ADMA addresses must
562 * be 32-bit aligned. If they aren't, then we use a bounce
563 * buffer for the (up to three) bytes that screw up the
Pierre Ossman2134a922008-06-28 18:28:51 +0200564 * alignment.
565 */
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200566 offset = (SDHCI_ADMA2_ALIGN - (addr & SDHCI_ADMA2_MASK)) &
567 SDHCI_ADMA2_MASK;
Pierre Ossman2134a922008-06-28 18:28:51 +0200568 if (offset) {
569 if (data->flags & MMC_DATA_WRITE) {
570 buffer = sdhci_kmap_atomic(sg, &flags);
571 memcpy(align, buffer, offset);
572 sdhci_kunmap_atomic(buffer, &flags);
573 }
574
Ben Dooks118cd172010-03-05 13:43:26 -0800575 /* tran, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200576 sdhci_adma_write_desc(host, desc, align_addr, offset,
Adrian Hunter739d46d2014-11-04 12:42:44 +0200577 ADMA2_TRAN_VALID);
Pierre Ossman2134a922008-06-28 18:28:51 +0200578
579 BUG_ON(offset > 65536);
580
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200581 align += SDHCI_ADMA2_ALIGN;
582 align_addr += SDHCI_ADMA2_ALIGN;
Pierre Ossman2134a922008-06-28 18:28:51 +0200583
Adrian Hunter76fe3792014-11-04 12:42:42 +0200584 desc += host->desc_sz;
Pierre Ossman2134a922008-06-28 18:28:51 +0200585
586 addr += offset;
587 len -= offset;
588 }
589
Pierre Ossman2134a922008-06-28 18:28:51 +0200590 BUG_ON(len > 65536);
591
Adrian Hunter347ea322015-11-26 14:00:48 +0200592 if (len) {
593 /* tran, valid */
594 sdhci_adma_write_desc(host, desc, addr, len,
595 ADMA2_TRAN_VALID);
596 desc += host->desc_sz;
597 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200598
599 /*
600 * If this triggers then we have a calculation bug
601 * somewhere. :/
602 */
Adrian Hunter76fe3792014-11-04 12:42:42 +0200603 WARN_ON((desc - host->adma_table) >= host->adma_table_sz);
Pierre Ossman2134a922008-06-28 18:28:51 +0200604 }
605
Thomas Abraham70764a92010-05-26 14:42:04 -0700606 if (host->quirks & SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC) {
Russell Kingacc3ad12016-01-26 13:40:00 +0000607 /* Mark the last descriptor as the terminating descriptor */
Adrian Hunter4efaa6f2014-11-04 12:42:39 +0200608 if (desc != host->adma_table) {
Adrian Hunter76fe3792014-11-04 12:42:42 +0200609 desc -= host->desc_sz;
Adrian Hunterb5ffa672014-11-04 12:42:40 +0200610 sdhci_adma_mark_end(desc);
Thomas Abraham70764a92010-05-26 14:42:04 -0700611 }
612 } else {
Russell Kingacc3ad12016-01-26 13:40:00 +0000613 /* Add a terminating entry - nop, end, valid */
Adrian Huntere57a5f62014-11-04 12:42:46 +0200614 sdhci_adma_write_desc(host, desc, 0, 0, ADMA2_NOP_END_VALID);
Thomas Abraham70764a92010-05-26 14:42:04 -0700615 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200616}
617
618static void sdhci_adma_table_post(struct sdhci_host *host,
619 struct mmc_data *data)
620{
Pierre Ossman2134a922008-06-28 18:28:51 +0200621 struct scatterlist *sg;
622 int i, size;
Adrian Hunter1c3d5f62014-11-04 12:42:41 +0200623 void *align;
Pierre Ossman2134a922008-06-28 18:28:51 +0200624 char *buffer;
625 unsigned long flags;
626
Russell King47fa9612016-01-26 13:40:06 +0000627 if (data->flags & MMC_DATA_READ) {
628 bool has_unaligned = false;
Russell Kingde0b65a2014-04-25 12:58:29 +0100629
Russell King47fa9612016-01-26 13:40:06 +0000630 /* Do a quick scan of the SG list for any unaligned mappings */
631 for_each_sg(data->sg, sg, host->sg_count, i)
Adrian Hunter04a5ae62015-11-26 14:00:49 +0200632 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
Russell King47fa9612016-01-26 13:40:06 +0000633 has_unaligned = true;
634 break;
635 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200636
Russell King47fa9612016-01-26 13:40:06 +0000637 if (has_unaligned) {
638 dma_sync_sg_for_cpu(mmc_dev(host->mmc), data->sg,
Russell Kingf55c98f2016-01-26 13:40:11 +0000639 data->sg_len, DMA_FROM_DEVICE);
Pierre Ossman2134a922008-06-28 18:28:51 +0200640
Russell King47fa9612016-01-26 13:40:06 +0000641 align = host->align_buffer;
642
643 for_each_sg(data->sg, sg, host->sg_count, i) {
644 if (sg_dma_address(sg) & SDHCI_ADMA2_MASK) {
645 size = SDHCI_ADMA2_ALIGN -
646 (sg_dma_address(sg) & SDHCI_ADMA2_MASK);
647
648 buffer = sdhci_kmap_atomic(sg, &flags);
649 memcpy(buffer, align, size);
650 sdhci_kunmap_atomic(buffer, &flags);
651
652 align += SDHCI_ADMA2_ALIGN;
653 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200654 }
655 }
656 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200657}
658
Andrei Warkentina3c77782011-04-11 16:13:42 -0500659static u8 sdhci_calc_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800660{
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700661 u8 count;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500662 struct mmc_data *data = cmd->data;
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700663 unsigned target_timeout, current_timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800664
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200665 /*
666 * If the host controller provides us with an incorrect timeout
667 * value, just skip the check and use 0xE. The hardware may take
668 * longer to time out, but that's much better than having a too-short
669 * timeout value.
670 */
Pierre Ossman11a2f1b2009-06-21 20:59:33 +0200671 if (host->quirks & SDHCI_QUIRK_BROKEN_TIMEOUT_VAL)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200672 return 0xE;
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200673
Andrei Warkentina3c77782011-04-11 16:13:42 -0500674 /* Unspecified timeout, assume max */
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100675 if (!data && !cmd->busy_timeout)
Andrei Warkentina3c77782011-04-11 16:13:42 -0500676 return 0xE;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800677
Andrei Warkentina3c77782011-04-11 16:13:42 -0500678 /* timeout in us */
679 if (!data)
Ulf Hansson1d4d7742014-01-08 15:06:08 +0100680 target_timeout = cmd->busy_timeout * 1000;
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300681 else {
Russell Kingfafcfda2016-01-26 13:40:58 +0000682 target_timeout = DIV_ROUND_UP(data->timeout_ns, 1000);
Russell King7f055382016-01-26 13:41:04 +0000683 if (host->clock && data->timeout_clks) {
684 unsigned long long val;
685
686 /*
687 * data->timeout_clks is in units of clock cycles.
688 * host->clock is in Hz. target_timeout is in us.
689 * Hence, us = 1000000 * cycles / Hz. Round up.
690 */
Haibo Chen02265cd62016-10-17 10:18:37 +0200691 val = 1000000ULL * data->timeout_clks;
Russell King7f055382016-01-26 13:41:04 +0000692 if (do_div(val, host->clock))
693 target_timeout++;
694 target_timeout += val;
695 }
Andy Shevchenko78a2ca22011-08-03 18:35:59 +0300696 }
Anton Vorontsov81b39802009-09-22 16:45:13 -0700697
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700698 /*
699 * Figure out needed cycles.
700 * We do this in steps in order to fit inside a 32 bit int.
701 * The first step is the minimum timeout, which will have a
702 * minimum resolution of 6 bits:
703 * (1) 2^13*1000 > 2^22,
704 * (2) host->timeout_clk < 2^16
705 * =>
706 * (1) / (2) > 2^6
707 */
708 count = 0;
709 current_timeout = (1 << 13) * 1000 / host->timeout_clk;
710 while (current_timeout < target_timeout) {
711 count++;
712 current_timeout <<= 1;
713 if (count >= 0xF)
714 break;
715 }
716
717 if (count >= 0xF) {
Chris Ball09eeff52012-06-01 10:39:45 -0400718 DBG("%s: Too large timeout 0x%x requested for CMD%d!\n",
719 mmc_hostname(host->mmc), count, cmd->opcode);
Pierre Ossman1c8cde92006-06-30 02:22:25 -0700720 count = 0xE;
721 }
722
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200723 return count;
724}
725
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300726static void sdhci_set_transfer_irqs(struct sdhci_host *host)
727{
728 u32 pio_irqs = SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL;
729 u32 dma_irqs = SDHCI_INT_DMA_END | SDHCI_INT_ADMA_ERROR;
730
731 if (host->flags & SDHCI_REQ_USE_DMA)
Russell Kingb537f942014-04-25 12:56:01 +0100732 host->ier = (host->ier & ~pio_irqs) | dma_irqs;
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300733 else
Russell Kingb537f942014-04-25 12:56:01 +0100734 host->ier = (host->ier & ~dma_irqs) | pio_irqs;
735
736 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
737 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300738}
739
Aisheng Dongb45e6682014-08-27 15:26:29 +0800740static void sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200741{
742 u8 count;
Aisheng Dongb45e6682014-08-27 15:26:29 +0800743
744 if (host->ops->set_timeout) {
745 host->ops->set_timeout(host, cmd);
746 } else {
747 count = sdhci_calc_timeout(host, cmd);
748 sdhci_writeb(host, count, SDHCI_TIMEOUT_CONTROL);
749 }
750}
751
752static void sdhci_prepare_data(struct sdhci_host *host, struct mmc_command *cmd)
753{
Pierre Ossman2134a922008-06-28 18:28:51 +0200754 u8 ctrl;
Andrei Warkentina3c77782011-04-11 16:13:42 -0500755 struct mmc_data *data = cmd->data;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200756
Adrian Hunter56a590d2016-06-29 16:24:32 +0300757 if (sdhci_data_line_cmd(cmd))
Aisheng Dongb45e6682014-08-27 15:26:29 +0800758 sdhci_set_timeout(host, cmd);
Andrei Warkentina3c77782011-04-11 16:13:42 -0500759
760 if (!data)
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200761 return;
762
Adrian Hunter43dea092016-06-29 16:24:26 +0300763 WARN_ON(host->data);
764
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200765 /* Sanity checks */
766 BUG_ON(data->blksz * data->blocks > 524288);
767 BUG_ON(data->blksz > host->mmc->max_blk_size);
768 BUG_ON(data->blocks > 65535);
769
770 host->data = data;
771 host->data_early = 0;
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400772 host->data->bytes_xfered = 0;
Pierre Ossmanee53ab52008-07-05 00:25:15 +0200773
Russell Kingfce14422016-01-26 13:41:20 +0000774 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200775 struct scatterlist *sg;
Russell Kingdf953922016-01-26 13:41:14 +0000776 unsigned int length_mask, offset_mask;
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000777 int i;
Pierre Ossman2134a922008-06-28 18:28:51 +0200778
Russell Kingfce14422016-01-26 13:41:20 +0000779 host->flags |= SDHCI_REQ_USE_DMA;
780
781 /*
782 * FIXME: This doesn't account for merging when mapping the
783 * scatterlist.
784 *
785 * The assumption here being that alignment and lengths are
786 * the same after DMA mapping to device address space.
787 */
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000788 length_mask = 0;
Russell Kingdf953922016-01-26 13:41:14 +0000789 offset_mask = 0;
Pierre Ossman2134a922008-06-28 18:28:51 +0200790 if (host->flags & SDHCI_USE_ADMA) {
Russell Kingdf953922016-01-26 13:41:14 +0000791 if (host->quirks & SDHCI_QUIRK_32BIT_ADMA_SIZE) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000792 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000793 /*
794 * As we use up to 3 byte chunks to work
795 * around alignment problems, we need to
796 * check the offset as well.
797 */
798 offset_mask = 3;
799 }
Pierre Ossman2134a922008-06-28 18:28:51 +0200800 } else {
801 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_SIZE)
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000802 length_mask = 3;
Russell Kingdf953922016-01-26 13:41:14 +0000803 if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR)
804 offset_mask = 3;
Pierre Ossman2134a922008-06-28 18:28:51 +0200805 }
806
Russell Kingdf953922016-01-26 13:41:14 +0000807 if (unlikely(length_mask | offset_mask)) {
Pierre Ossman2134a922008-06-28 18:28:51 +0200808 for_each_sg(data->sg, sg, data->sg_len, i) {
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000809 if (sg->length & length_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100810 DBG("Reverting to PIO because of transfer size (%d)\n",
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000811 sg->length);
Pierre Ossman2134a922008-06-28 18:28:51 +0200812 host->flags &= ~SDHCI_REQ_USE_DMA;
813 break;
814 }
Russell Kinga0eaf0f2016-01-26 13:41:09 +0000815 if (sg->offset & offset_mask) {
Marek Vasut2e4456f2015-11-18 10:47:02 +0100816 DBG("Reverting to PIO because of bad alignment\n");
Pierre Ossman2134a922008-06-28 18:28:51 +0200817 host->flags &= ~SDHCI_REQ_USE_DMA;
818 break;
819 }
820 }
821 }
822 }
823
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200824 if (host->flags & SDHCI_REQ_USE_DMA) {
Russell Kingc0999b72016-01-26 13:40:27 +0000825 int sg_cnt = sdhci_pre_dma_transfer(host, data, COOKIE_MAPPED);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200826
Russell King60c64762016-01-26 13:40:22 +0000827 if (sg_cnt <= 0) {
828 /*
829 * This only happens when someone fed
830 * us an invalid request.
831 */
832 WARN_ON(1);
833 host->flags &= ~SDHCI_REQ_USE_DMA;
834 } else if (host->flags & SDHCI_USE_ADMA) {
835 sdhci_adma_table_pre(host, data, sg_cnt);
836
837 sdhci_writel(host, host->adma_addr, SDHCI_ADMA_ADDRESS);
838 if (host->flags & SDHCI_USE_64_BIT_DMA)
839 sdhci_writel(host,
840 (u64)host->adma_addr >> 32,
841 SDHCI_ADMA_ADDRESS_HI);
842 } else {
843 WARN_ON(sg_cnt != 1);
844 sdhci_writel(host, sg_dma_address(data->sg),
845 SDHCI_DMA_ADDRESS);
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200846 }
847 }
848
Pierre Ossman2134a922008-06-28 18:28:51 +0200849 /*
850 * Always adjust the DMA selection as some controllers
851 * (e.g. JMicron) can't do PIO properly when the selection
852 * is ADMA.
853 */
854 if (host->version >= SDHCI_SPEC_200) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300855 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossman2134a922008-06-28 18:28:51 +0200856 ctrl &= ~SDHCI_CTRL_DMA_MASK;
857 if ((host->flags & SDHCI_REQ_USE_DMA) &&
Adrian Huntere57a5f62014-11-04 12:42:46 +0200858 (host->flags & SDHCI_USE_ADMA)) {
859 if (host->flags & SDHCI_USE_64_BIT_DMA)
860 ctrl |= SDHCI_CTRL_ADMA64;
861 else
862 ctrl |= SDHCI_CTRL_ADMA32;
863 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +0200864 ctrl |= SDHCI_CTRL_SDMA;
Adrian Huntere57a5f62014-11-04 12:42:46 +0200865 }
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300866 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100867 }
868
Pierre Ossman8f1934c2008-06-30 21:15:49 +0200869 if (!(host->flags & SDHCI_REQ_USE_DMA)) {
Sebastian Andrzej Siewiorda60a912009-06-18 09:33:32 +0200870 int flags;
871
872 flags = SG_MITER_ATOMIC;
873 if (host->data->flags & MMC_DATA_READ)
874 flags |= SG_MITER_TO_SG;
875 else
876 flags |= SG_MITER_FROM_SG;
877 sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
Pierre Ossman76591502008-07-21 00:32:11 +0200878 host->blocks = data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800879 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700880
Anton Vorontsov6aa943a2009-03-17 00:13:50 +0300881 sdhci_set_transfer_irqs(host);
882
Mikko Vinnif6a03cb2011-04-12 09:36:18 -0400883 /* Set the DMA boundary value and block size */
884 sdhci_writew(host, SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG,
885 data->blksz), SDHCI_BLOCK_SIZE);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300886 sdhci_writew(host, data->blocks, SDHCI_BLOCK_COUNT);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700887}
888
Adrian Hunter0293d502016-06-29 16:24:35 +0300889static inline bool sdhci_auto_cmd12(struct sdhci_host *host,
890 struct mmc_request *mrq)
891{
Adrian Hunter20845be2016-08-16 13:44:13 +0300892 return !mrq->sbc && (host->flags & SDHCI_AUTO_CMD12) &&
893 !mrq->cap_cmd_during_tfr;
Adrian Hunter0293d502016-06-29 16:24:35 +0300894}
895
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700896static void sdhci_set_transfer_mode(struct sdhci_host *host,
Andrei Warkentine89d4562011-05-23 15:06:37 -0500897 struct mmc_command *cmd)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700898{
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800899 u16 mode = 0;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500900 struct mmc_data *data = cmd->data;
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700901
Dong Aisheng2b558c12013-10-30 22:09:48 +0800902 if (data == NULL) {
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800903 if (host->quirks2 &
904 SDHCI_QUIRK2_CLEAR_TRANSFERMODE_REG_BEFORE_CMD) {
905 sdhci_writew(host, 0x0, SDHCI_TRANSFER_MODE);
906 } else {
Dong Aisheng2b558c12013-10-30 22:09:48 +0800907 /* clear Auto CMD settings for no data CMDs */
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800908 mode = sdhci_readw(host, SDHCI_TRANSFER_MODE);
909 sdhci_writew(host, mode & ~(SDHCI_TRNS_AUTO_CMD12 |
Dong Aisheng2b558c12013-10-30 22:09:48 +0800910 SDHCI_TRNS_AUTO_CMD23), SDHCI_TRANSFER_MODE);
Vincent Wan9b8ffea2014-11-05 14:09:00 +0800911 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700912 return;
Dong Aisheng2b558c12013-10-30 22:09:48 +0800913 }
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700914
Pierre Ossmane538fbe2007-08-12 16:46:32 +0200915 WARN_ON(!host->data);
916
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800917 if (!(host->quirks2 & SDHCI_QUIRK2_SUPPORT_SINGLE))
918 mode = SDHCI_TRNS_BLK_CNT_EN;
919
Andrei Warkentine89d4562011-05-23 15:06:37 -0500920 if (mmc_op_multi(cmd->opcode) || data->blocks > 1) {
Vincent Yangd3fc5d72015-01-20 16:05:17 +0800921 mode = SDHCI_TRNS_BLK_CNT_EN | SDHCI_TRNS_MULTI;
Andrei Warkentine89d4562011-05-23 15:06:37 -0500922 /*
923 * If we are sending CMD23, CMD12 never gets sent
924 * on successful completion (so no Auto-CMD12).
925 */
Adrian Hunter0293d502016-06-29 16:24:35 +0300926 if (sdhci_auto_cmd12(host, cmd->mrq) &&
Corneliu Doban85cc1c32015-02-09 16:06:29 -0800927 (cmd->opcode != SD_IO_RW_EXTENDED))
Andrei Warkentine89d4562011-05-23 15:06:37 -0500928 mode |= SDHCI_TRNS_AUTO_CMD12;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300929 else if (cmd->mrq->sbc && (host->flags & SDHCI_AUTO_CMD23)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500930 mode |= SDHCI_TRNS_AUTO_CMD23;
Adrian Huntera4c73ab2016-06-29 16:24:25 +0300931 sdhci_writel(host, cmd->mrq->sbc->arg, SDHCI_ARGUMENT2);
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500932 }
Jerry Huangc4512f72010-08-10 18:01:59 -0700933 }
Andrei Warkentin8edf63712011-05-23 15:06:39 -0500934
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700935 if (data->flags & MMC_DATA_READ)
936 mode |= SDHCI_TRNS_READ;
Pierre Ossmanc9fddbc2007-12-02 19:52:11 +0100937 if (host->flags & SDHCI_REQ_USE_DMA)
Pierre Ossmanc7fa9962006-06-30 02:22:25 -0700938 mode |= SDHCI_TRNS_DMA;
939
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300940 sdhci_writew(host, mode, SDHCI_TRANSFER_MODE);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800941}
942
Adrian Hunter0cc563c2016-06-29 16:24:28 +0300943static bool sdhci_needs_reset(struct sdhci_host *host, struct mmc_request *mrq)
944{
945 return (!(host->flags & SDHCI_DEVICE_DEAD) &&
946 ((mrq->cmd && mrq->cmd->error) ||
947 (mrq->sbc && mrq->sbc->error) ||
948 (mrq->data && ((mrq->data->error && !mrq->data->stop) ||
949 (mrq->data->stop && mrq->data->stop->error))) ||
950 (host->quirks & SDHCI_QUIRK_RESET_AFTER_REQUEST)));
951}
952
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300953static void __sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
954{
955 int i;
956
957 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
958 if (host->mrqs_done[i] == mrq) {
959 WARN_ON(1);
960 return;
961 }
962 }
963
964 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
965 if (!host->mrqs_done[i]) {
966 host->mrqs_done[i] = mrq;
967 break;
968 }
969 }
970
971 WARN_ON(i >= SDHCI_MAX_MRQS);
972
973 tasklet_schedule(&host->finish_tasklet);
974}
975
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300976static void sdhci_finish_mrq(struct sdhci_host *host, struct mmc_request *mrq)
977{
Adrian Hunter5a8a3fe2016-06-29 16:24:30 +0300978 if (host->cmd && host->cmd->mrq == mrq)
979 host->cmd = NULL;
980
981 if (host->data_cmd && host->data_cmd->mrq == mrq)
982 host->data_cmd = NULL;
983
984 if (host->data && host->data->mrq == mrq)
985 host->data = NULL;
986
Adrian Huntered1563d2016-06-29 16:24:29 +0300987 if (sdhci_needs_reset(host, mrq))
988 host->pending_reset = true;
989
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +0300990 __sdhci_finish_mrq(host, mrq);
Adrian Huntera6d3bdd2016-06-29 16:24:27 +0300991}
992
Pierre Ossmand129bce2006-03-24 03:18:17 -0800993static void sdhci_finish_data(struct sdhci_host *host)
994{
Adrian Hunter33a57ad2016-06-29 16:24:36 +0300995 struct mmc_command *data_cmd = host->data_cmd;
996 struct mmc_data *data = host->data;
Pierre Ossmand129bce2006-03-24 03:18:17 -0800997
Pierre Ossmand129bce2006-03-24 03:18:17 -0800998 host->data = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +0300999 host->data_cmd = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001000
Russell Kingadd89132016-01-26 13:40:42 +00001001 if ((host->flags & (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA)) ==
1002 (SDHCI_REQ_USE_DMA | SDHCI_USE_ADMA))
1003 sdhci_adma_table_post(host, data);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001004
1005 /*
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001006 * The specification states that the block count register must
1007 * be updated, but it does not specify at what point in the
1008 * data flow. That makes the register entirely useless to read
1009 * back so we have to assume that nothing made it to the card
1010 * in the event of an error.
Pierre Ossmand129bce2006-03-24 03:18:17 -08001011 */
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001012 if (data->error)
1013 data->bytes_xfered = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001014 else
Pierre Ossmanc9b74c52008-04-18 20:41:49 +02001015 data->bytes_xfered = data->blksz * data->blocks;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001016
Andrei Warkentine89d4562011-05-23 15:06:37 -05001017 /*
1018 * Need to send CMD12 if -
1019 * a) open-ended multiblock transfer (no CMD23)
1020 * b) error in multiblock transfer
1021 */
1022 if (data->stop &&
1023 (data->error ||
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001024 !data->mrq->sbc)) {
Andrei Warkentine89d4562011-05-23 15:06:37 -05001025
Pierre Ossmand129bce2006-03-24 03:18:17 -08001026 /*
1027 * The controller needs a reset of internal state machines
1028 * upon error conditions.
1029 */
Pierre Ossman17b04292007-07-22 22:18:46 +02001030 if (data->error) {
Adrian Hunter33a57ad2016-06-29 16:24:36 +03001031 if (!host->cmd || host->cmd == data_cmd)
1032 sdhci_do_reset(host, SDHCI_RESET_CMD);
Russell King03231f92014-04-25 12:57:12 +01001033 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001034 }
1035
Adrian Hunter20845be2016-08-16 13:44:13 +03001036 /*
1037 * 'cap_cmd_during_tfr' request must not use the command line
1038 * after mmc_command_done() has been called. It is upper layer's
1039 * responsibility to send the stop command if required.
1040 */
1041 if (data->mrq->cap_cmd_during_tfr) {
1042 sdhci_finish_mrq(host, data->mrq);
1043 } else {
1044 /* Avoid triggering warning in sdhci_send_command() */
1045 host->cmd = NULL;
1046 sdhci_send_command(host, data->stop);
1047 }
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001048 } else {
1049 sdhci_finish_mrq(host, data->mrq);
1050 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001051}
1052
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001053static void sdhci_mod_timer(struct sdhci_host *host, struct mmc_request *mrq,
1054 unsigned long timeout)
1055{
1056 if (sdhci_data_line_cmd(mrq->cmd))
1057 mod_timer(&host->data_timer, timeout);
1058 else
1059 mod_timer(&host->timer, timeout);
1060}
1061
1062static void sdhci_del_timer(struct sdhci_host *host, struct mmc_request *mrq)
1063{
1064 if (sdhci_data_line_cmd(mrq->cmd))
1065 del_timer(&host->data_timer);
1066 else
1067 del_timer(&host->timer);
1068}
1069
Dong Aishengc0e551292013-09-13 19:11:31 +08001070void sdhci_send_command(struct sdhci_host *host, struct mmc_command *cmd)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001071{
1072 int flags;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001073 u32 mask;
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001074 unsigned long timeout;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001075
1076 WARN_ON(host->cmd);
1077
Russell King96776202016-01-26 13:39:34 +00001078 /* Initially, a command has no error */
1079 cmd->error = 0;
1080
Adrian Hunterfc605f12016-10-05 12:11:21 +03001081 if ((host->quirks2 & SDHCI_QUIRK2_STOP_WITH_TC) &&
1082 cmd->opcode == MMC_STOP_TRANSMISSION)
1083 cmd->flags |= MMC_RSP_BUSY;
1084
Pierre Ossmand129bce2006-03-24 03:18:17 -08001085 /* Wait max 10 ms */
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001086 timeout = 10;
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001087
1088 mask = SDHCI_CMD_INHIBIT;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001089 if (sdhci_data_line_cmd(cmd))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001090 mask |= SDHCI_DATA_INHIBIT;
1091
1092 /* We shouldn't wait for data inihibit for stop commands, even
1093 though they might use busy signaling */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001094 if (cmd->mrq->data && (cmd == cmd->mrq->data->stop))
Pierre Ossmanfd2208d2006-06-30 02:22:28 -07001095 mask &= ~SDHCI_DATA_INHIBIT;
1096
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001097 while (sdhci_readl(host, SDHCI_PRESENT_STATE) & mask) {
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001098 if (timeout == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001099 pr_err("%s: Controller never released inhibit bit(s).\n",
1100 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001101 sdhci_dumpregs(host);
Pierre Ossman17b04292007-07-22 22:18:46 +02001102 cmd->error = -EIO;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001103 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001104 return;
1105 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001106 timeout--;
1107 mdelay(1);
1108 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001109
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001110 timeout = jiffies;
Ulf Hansson1d4d7742014-01-08 15:06:08 +01001111 if (!cmd->data && cmd->busy_timeout > 9000)
1112 timeout += DIV_ROUND_UP(cmd->busy_timeout, 1000) * HZ + HZ;
Adrian Hunter3e1a6892013-11-14 10:16:20 +02001113 else
1114 timeout += 10 * HZ;
Adrian Hunterd7422fb2016-06-29 16:24:33 +03001115 sdhci_mod_timer(host, cmd->mrq, timeout);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001116
1117 host->cmd = cmd;
Adrian Hunter56a590d2016-06-29 16:24:32 +03001118 if (sdhci_data_line_cmd(cmd)) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03001119 WARN_ON(host->data_cmd);
1120 host->data_cmd = cmd;
1121 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001122
Andrei Warkentina3c77782011-04-11 16:13:42 -05001123 sdhci_prepare_data(host, cmd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001124
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001125 sdhci_writel(host, cmd->arg, SDHCI_ARGUMENT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001126
Andrei Warkentine89d4562011-05-23 15:06:37 -05001127 sdhci_set_transfer_mode(host, cmd);
Pierre Ossmanc7fa9962006-06-30 02:22:25 -07001128
Pierre Ossmand129bce2006-03-24 03:18:17 -08001129 if ((cmd->flags & MMC_RSP_136) && (cmd->flags & MMC_RSP_BUSY)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05301130 pr_err("%s: Unsupported response type!\n",
Pierre Ossmand129bce2006-03-24 03:18:17 -08001131 mmc_hostname(host->mmc));
Pierre Ossman17b04292007-07-22 22:18:46 +02001132 cmd->error = -EINVAL;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001133 sdhci_finish_mrq(host, cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001134 return;
1135 }
1136
1137 if (!(cmd->flags & MMC_RSP_PRESENT))
1138 flags = SDHCI_CMD_RESP_NONE;
1139 else if (cmd->flags & MMC_RSP_136)
1140 flags = SDHCI_CMD_RESP_LONG;
1141 else if (cmd->flags & MMC_RSP_BUSY)
1142 flags = SDHCI_CMD_RESP_SHORT_BUSY;
1143 else
1144 flags = SDHCI_CMD_RESP_SHORT;
1145
1146 if (cmd->flags & MMC_RSP_CRC)
1147 flags |= SDHCI_CMD_CRC;
1148 if (cmd->flags & MMC_RSP_OPCODE)
1149 flags |= SDHCI_CMD_INDEX;
Arindam Nathb513ea22011-05-05 12:19:04 +05301150
1151 /* CMD19 is special in that the Data Present Select should be set */
Girish K S069c9f12012-01-06 09:56:39 +05301152 if (cmd->data || cmd->opcode == MMC_SEND_TUNING_BLOCK ||
1153 cmd->opcode == MMC_SEND_TUNING_BLOCK_HS200)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001154 flags |= SDHCI_CMD_DATA;
1155
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001156 sdhci_writew(host, SDHCI_MAKE_CMD(cmd->opcode, flags), SDHCI_COMMAND);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001157}
Dong Aishengc0e551292013-09-13 19:11:31 +08001158EXPORT_SYMBOL_GPL(sdhci_send_command);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001159
1160static void sdhci_finish_command(struct sdhci_host *host)
1161{
Adrian Huntere0a56402016-06-29 16:24:22 +03001162 struct mmc_command *cmd = host->cmd;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001163 int i;
1164
Adrian Huntere0a56402016-06-29 16:24:22 +03001165 host->cmd = NULL;
1166
1167 if (cmd->flags & MMC_RSP_PRESENT) {
1168 if (cmd->flags & MMC_RSP_136) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08001169 /* CRC is stripped so we need to do some shifting. */
1170 for (i = 0;i < 4;i++) {
Adrian Huntere0a56402016-06-29 16:24:22 +03001171 cmd->resp[i] = sdhci_readl(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001172 SDHCI_RESPONSE + (3-i)*4) << 8;
1173 if (i != 3)
Adrian Huntere0a56402016-06-29 16:24:22 +03001174 cmd->resp[i] |=
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001175 sdhci_readb(host,
Pierre Ossmand129bce2006-03-24 03:18:17 -08001176 SDHCI_RESPONSE + (3-i)*4-1);
1177 }
1178 } else {
Adrian Huntere0a56402016-06-29 16:24:22 +03001179 cmd->resp[0] = sdhci_readl(host, SDHCI_RESPONSE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001180 }
1181 }
1182
Adrian Hunter20845be2016-08-16 13:44:13 +03001183 if (cmd->mrq->cap_cmd_during_tfr && cmd == cmd->mrq->cmd)
1184 mmc_command_done(host->mmc, cmd->mrq);
1185
Adrian Hunter6bde8682016-06-29 16:24:20 +03001186 /*
1187 * The host can send and interrupt when the busy state has
1188 * ended, allowing us to wait without wasting CPU cycles.
1189 * The busy signal uses DAT0 so this is similar to waiting
1190 * for data to complete.
1191 *
1192 * Note: The 1.0 specification is a bit ambiguous about this
1193 * feature so there might be some problems with older
1194 * controllers.
1195 */
Adrian Huntere0a56402016-06-29 16:24:22 +03001196 if (cmd->flags & MMC_RSP_BUSY) {
1197 if (cmd->data) {
Adrian Hunter6bde8682016-06-29 16:24:20 +03001198 DBG("Cannot wait for busy signal when also doing a data transfer");
1199 } else if (!(host->quirks & SDHCI_QUIRK_NO_BUSY_IRQ) &&
Adrian Hunterea968022016-06-29 16:24:24 +03001200 cmd == host->data_cmd) {
1201 /* Command complete before busy is ended */
Adrian Hunter6bde8682016-06-29 16:24:20 +03001202 return;
1203 }
1204 }
1205
Andrei Warkentine89d4562011-05-23 15:06:37 -05001206 /* Finished CMD23, now send actual command. */
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001207 if (cmd == cmd->mrq->sbc) {
1208 sdhci_send_command(host, cmd->mrq->cmd);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001209 } else {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02001210
Andrei Warkentine89d4562011-05-23 15:06:37 -05001211 /* Processed actual command. */
1212 if (host->data && host->data_early)
1213 sdhci_finish_data(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001214
Adrian Huntere0a56402016-06-29 16:24:22 +03001215 if (!cmd->data)
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001216 sdhci_finish_mrq(host, cmd->mrq);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001217 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001218}
1219
Kevin Liu52983382013-01-31 11:31:37 +08001220static u16 sdhci_get_preset_value(struct sdhci_host *host)
1221{
Russell Kingd975f122014-04-25 12:59:31 +01001222 u16 preset = 0;
Kevin Liu52983382013-01-31 11:31:37 +08001223
Russell Kingd975f122014-04-25 12:59:31 +01001224 switch (host->timing) {
1225 case MMC_TIMING_UHS_SDR12:
Kevin Liu52983382013-01-31 11:31:37 +08001226 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1227 break;
Russell Kingd975f122014-04-25 12:59:31 +01001228 case MMC_TIMING_UHS_SDR25:
Kevin Liu52983382013-01-31 11:31:37 +08001229 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR25);
1230 break;
Russell Kingd975f122014-04-25 12:59:31 +01001231 case MMC_TIMING_UHS_SDR50:
Kevin Liu52983382013-01-31 11:31:37 +08001232 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR50);
1233 break;
Russell Kingd975f122014-04-25 12:59:31 +01001234 case MMC_TIMING_UHS_SDR104:
1235 case MMC_TIMING_MMC_HS200:
Kevin Liu52983382013-01-31 11:31:37 +08001236 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR104);
1237 break;
Russell Kingd975f122014-04-25 12:59:31 +01001238 case MMC_TIMING_UHS_DDR50:
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001239 case MMC_TIMING_MMC_DDR52:
Kevin Liu52983382013-01-31 11:31:37 +08001240 preset = sdhci_readw(host, SDHCI_PRESET_FOR_DDR50);
1241 break;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001242 case MMC_TIMING_MMC_HS400:
1243 preset = sdhci_readw(host, SDHCI_PRESET_FOR_HS400);
1244 break;
Kevin Liu52983382013-01-31 11:31:37 +08001245 default:
1246 pr_warn("%s: Invalid UHS-I mode selected\n",
1247 mmc_hostname(host->mmc));
1248 preset = sdhci_readw(host, SDHCI_PRESET_FOR_SDR12);
1249 break;
1250 }
1251 return preset;
1252}
1253
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001254u16 sdhci_calc_clk(struct sdhci_host *host, unsigned int clock,
1255 unsigned int *actual_clock)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001256{
Arindam Nathc3ed3872011-05-05 12:19:06 +05301257 int div = 0; /* Initialized for compiler warning */
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001258 int real_div = div, clk_mul = 1;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301259 u16 clk = 0;
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001260 bool switch_base_clk = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001261
Zhangfei Gao85105c52010-08-06 07:10:01 +08001262 if (host->version >= SDHCI_SPEC_300) {
Russell Kingda91a8f2014-04-25 13:00:12 +01001263 if (host->preset_enabled) {
Kevin Liu52983382013-01-31 11:31:37 +08001264 u16 pre_val;
1265
1266 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1267 pre_val = sdhci_get_preset_value(host);
1268 div = (pre_val & SDHCI_PRESET_SDCLK_FREQ_MASK)
1269 >> SDHCI_PRESET_SDCLK_FREQ_SHIFT;
1270 if (host->clk_mul &&
1271 (pre_val & SDHCI_PRESET_CLKGEN_SEL_MASK)) {
1272 clk = SDHCI_PROG_CLOCK_MODE;
1273 real_div = div + 1;
1274 clk_mul = host->clk_mul;
1275 } else {
1276 real_div = max_t(int, 1, div << 1);
1277 }
1278 goto clock_set;
1279 }
1280
Arindam Nathc3ed3872011-05-05 12:19:06 +05301281 /*
1282 * Check if the Host Controller supports Programmable Clock
1283 * Mode.
1284 */
1285 if (host->clk_mul) {
Kevin Liu52983382013-01-31 11:31:37 +08001286 for (div = 1; div <= 1024; div++) {
1287 if ((host->max_clk * host->clk_mul / div)
1288 <= clock)
1289 break;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001290 }
ludovic.desroches@atmel.com54971592015-07-29 16:22:46 +02001291 if ((host->max_clk * host->clk_mul / div) <= clock) {
1292 /*
1293 * Set Programmable Clock Mode in the Clock
1294 * Control register.
1295 */
1296 clk = SDHCI_PROG_CLOCK_MODE;
1297 real_div = div;
1298 clk_mul = host->clk_mul;
1299 div--;
1300 } else {
1301 /*
1302 * Divisor can be too small to reach clock
1303 * speed requirement. Then use the base clock.
1304 */
1305 switch_base_clk = true;
1306 }
1307 }
1308
1309 if (!host->clk_mul || switch_base_clk) {
Arindam Nathc3ed3872011-05-05 12:19:06 +05301310 /* Version 3.00 divisors must be a multiple of 2. */
1311 if (host->max_clk <= clock)
1312 div = 1;
1313 else {
1314 for (div = 2; div < SDHCI_MAX_DIV_SPEC_300;
1315 div += 2) {
1316 if ((host->max_clk / div) <= clock)
1317 break;
1318 }
1319 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001320 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301321 div >>= 1;
Suneel Garapatid1955c32015-06-09 13:01:50 +05301322 if ((host->quirks2 & SDHCI_QUIRK2_CLOCK_DIV_ZERO_BROKEN)
1323 && !div && host->max_clk <= 25000000)
1324 div = 1;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001325 }
1326 } else {
1327 /* Version 2.00 divisors must be a power of 2. */
Zhangfei Gao03975262010-09-20 15:15:18 -04001328 for (div = 1; div < SDHCI_MAX_DIV_SPEC_200; div *= 2) {
Zhangfei Gao85105c52010-08-06 07:10:01 +08001329 if ((host->max_clk / div) <= clock)
1330 break;
1331 }
Giuseppe CAVALLAROdf162192011-11-04 13:53:19 +01001332 real_div = div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301333 div >>= 1;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001334 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001335
Kevin Liu52983382013-01-31 11:31:37 +08001336clock_set:
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001337 if (real_div)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001338 *actual_clock = (host->max_clk * clk_mul) / real_div;
Arindam Nathc3ed3872011-05-05 12:19:06 +05301339 clk |= (div & SDHCI_DIV_MASK) << SDHCI_DIVIDER_SHIFT;
Zhangfei Gao85105c52010-08-06 07:10:01 +08001340 clk |= ((div & SDHCI_DIV_HI_MASK) >> SDHCI_DIV_MASK_LEN)
1341 << SDHCI_DIVIDER_HI_SHIFT;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001342
1343 return clk;
1344}
1345EXPORT_SYMBOL_GPL(sdhci_calc_clk);
1346
Ritesh Harjanifec79672016-11-21 12:07:19 +05301347void sdhci_enable_clk(struct sdhci_host *host, u16 clk)
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001348{
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001349 ktime_t timeout;
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001350
Pierre Ossmand129bce2006-03-24 03:18:17 -08001351 clk |= SDHCI_CLOCK_INT_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001352 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001353
Chris Ball27f6cb12009-09-22 16:45:31 -07001354 /* Wait max 20 ms */
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001355 timeout = ktime_add_ms(ktime_get(), 20);
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001356 while (!((clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL))
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001357 & SDHCI_CLOCK_INT_STABLE)) {
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001358 if (ktime_after(ktime_get(), timeout)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001359 pr_err("%s: Internal clock never stabilised.\n",
1360 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08001361 sdhci_dumpregs(host);
1362 return;
1363 }
Adrian Huntere2ebfb22017-03-20 19:50:29 +02001364 spin_unlock_irq(&host->lock);
Adrian Hunter5a436cc2017-03-20 19:50:31 +02001365 udelay(10);
Adrian Huntere2ebfb22017-03-20 19:50:29 +02001366 spin_lock_irq(&host->lock);
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001367 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001368
1369 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001370 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001371}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301372EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1373
1374void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1375{
1376 u16 clk;
1377
1378 host->mmc->actual_clock = 0;
1379
1380 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1381
1382 if (clock == 0)
1383 return;
1384
1385 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1386 sdhci_enable_clk(host, clk);
1387}
Russell King17710592014-04-25 12:58:55 +01001388EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001389
Adrian Hunter1dceb042016-03-29 12:45:43 +03001390static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1391 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001392{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001393 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001394
1395 spin_unlock_irq(&host->lock);
1396 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1397 spin_lock_irq(&host->lock);
1398
1399 if (mode != MMC_POWER_OFF)
1400 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1401 else
1402 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1403}
1404
Adrian Hunter606d3132016-10-05 12:11:22 +03001405void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1406 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001407{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001408 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001409
Russell King24fbb3c2014-04-25 13:00:06 +01001410 if (mode != MMC_POWER_OFF) {
1411 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001412 case MMC_VDD_165_195:
1413 pwr = SDHCI_POWER_180;
1414 break;
1415 case MMC_VDD_29_30:
1416 case MMC_VDD_30_31:
1417 pwr = SDHCI_POWER_300;
1418 break;
1419 case MMC_VDD_32_33:
1420 case MMC_VDD_33_34:
1421 pwr = SDHCI_POWER_330;
1422 break;
1423 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001424 WARN(1, "%s: Invalid vdd %#x\n",
1425 mmc_hostname(host->mmc), vdd);
1426 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001427 }
1428 }
1429
1430 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001431 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001432
Pierre Ossmanae628902009-05-03 20:45:03 +02001433 host->pwr = pwr;
1434
1435 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001436 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001437 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1438 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001439 } else {
1440 /*
1441 * Spec says that we should clear the power reg before setting
1442 * a new value. Some controllers don't seem to like this though.
1443 */
1444 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1445 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001446
Russell Kinge921a8b2014-04-25 13:00:01 +01001447 /*
1448 * At least the Marvell CaFe chip gets confused if we set the
1449 * voltage and set turn on power at the same time, so set the
1450 * voltage first.
1451 */
1452 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1453 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001454
Russell Kinge921a8b2014-04-25 13:00:01 +01001455 pwr |= SDHCI_POWER_ON;
1456
Pierre Ossmanae628902009-05-03 20:45:03 +02001457 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1458
Russell Kinge921a8b2014-04-25 13:00:01 +01001459 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1460 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001461
Russell Kinge921a8b2014-04-25 13:00:01 +01001462 /*
1463 * Some controllers need an extra 10ms delay of 10ms before
1464 * they can apply clock after applying power
1465 */
1466 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1467 mdelay(10);
1468 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001469}
Adrian Hunter606d3132016-10-05 12:11:22 +03001470EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001471
Adrian Hunter606d3132016-10-05 12:11:22 +03001472void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1473 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001474{
Adrian Hunter606d3132016-10-05 12:11:22 +03001475 if (IS_ERR(host->mmc->supply.vmmc))
1476 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001477 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001478 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001479}
Adrian Hunter606d3132016-10-05 12:11:22 +03001480EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001481
Pierre Ossmand129bce2006-03-24 03:18:17 -08001482/*****************************************************************************\
1483 * *
1484 * MMC callbacks *
1485 * *
1486\*****************************************************************************/
1487
1488static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1489{
1490 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001491 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001492 unsigned long flags;
1493
1494 host = mmc_priv(mmc);
1495
Scott Branden04e079cf2015-03-10 11:35:10 -07001496 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001497 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001498
Pierre Ossmand129bce2006-03-24 03:18:17 -08001499 spin_lock_irqsave(&host->lock, flags);
1500
Adrian Hunter061d17a2016-04-12 14:25:09 +03001501 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001502
1503 /*
1504 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1505 * requests if Auto-CMD12 is enabled.
1506 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001507 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001508 if (mrq->stop) {
1509 mrq->data->stop = NULL;
1510 mrq->stop = NULL;
1511 }
1512 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001513
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001514 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001515 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001516 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301517 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001518 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001519 sdhci_send_command(host, mrq->sbc);
1520 else
1521 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301522 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001523
Pierre Ossman5f25a662006-10-04 02:15:39 -07001524 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001525 spin_unlock_irqrestore(&host->lock, flags);
1526}
1527
Russell King2317f562014-04-25 12:57:07 +01001528void sdhci_set_bus_width(struct sdhci_host *host, int width)
1529{
1530 u8 ctrl;
1531
1532 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1533 if (width == MMC_BUS_WIDTH_8) {
1534 ctrl &= ~SDHCI_CTRL_4BITBUS;
1535 if (host->version >= SDHCI_SPEC_300)
1536 ctrl |= SDHCI_CTRL_8BITBUS;
1537 } else {
1538 if (host->version >= SDHCI_SPEC_300)
1539 ctrl &= ~SDHCI_CTRL_8BITBUS;
1540 if (width == MMC_BUS_WIDTH_4)
1541 ctrl |= SDHCI_CTRL_4BITBUS;
1542 else
1543 ctrl &= ~SDHCI_CTRL_4BITBUS;
1544 }
1545 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1546}
1547EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1548
Russell King96d7b782014-04-25 12:59:26 +01001549void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1550{
1551 u16 ctrl_2;
1552
1553 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1554 /* Select Bus Speed Mode for host */
1555 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1556 if ((timing == MMC_TIMING_MMC_HS200) ||
1557 (timing == MMC_TIMING_UHS_SDR104))
1558 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1559 else if (timing == MMC_TIMING_UHS_SDR12)
1560 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1561 else if (timing == MMC_TIMING_UHS_SDR25)
1562 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1563 else if (timing == MMC_TIMING_UHS_SDR50)
1564 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1565 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1566 (timing == MMC_TIMING_MMC_DDR52))
1567 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001568 else if (timing == MMC_TIMING_MMC_HS400)
1569 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001570 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1571}
1572EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1573
Dong Aishengded97e02016-04-16 01:29:25 +08001574static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001575{
Dong Aishengded97e02016-04-16 01:29:25 +08001576 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001577 unsigned long flags;
1578 u8 ctrl;
1579
Adrian Hunter84ec0482016-12-19 15:33:11 +02001580 if (ios->power_mode == MMC_POWER_UNDEFINED)
1581 return;
1582
Pierre Ossmand129bce2006-03-24 03:18:17 -08001583 spin_lock_irqsave(&host->lock, flags);
1584
Adrian Hunterceb61432011-12-27 15:48:41 +02001585 if (host->flags & SDHCI_DEVICE_DEAD) {
1586 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001587 if (!IS_ERR(mmc->supply.vmmc) &&
1588 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001589 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001590 return;
1591 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001592
Pierre Ossmand129bce2006-03-24 03:18:17 -08001593 /*
1594 * Reset the chip on each power off.
1595 * Should clear out any weird states.
1596 */
1597 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001598 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001599 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001600 }
1601
Kevin Liu52983382013-01-31 11:31:37 +08001602 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001603 (ios->power_mode == MMC_POWER_UP) &&
1604 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001605 sdhci_enable_preset_value(host, false);
1606
Russell King373073e2014-04-25 12:58:45 +01001607 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001608 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001609 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001610
1611 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1612 host->clock) {
1613 host->timeout_clk = host->mmc->actual_clock ?
1614 host->mmc->actual_clock / 1000 :
1615 host->clock / 1000;
1616 host->mmc->max_busy_timeout =
1617 host->ops->get_max_timeout_count ?
1618 host->ops->get_max_timeout_count(host) :
1619 1 << 27;
1620 host->mmc->max_busy_timeout /= host->timeout_clk;
1621 }
Russell King373073e2014-04-25 12:58:45 +01001622 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001623
Adrian Hunter606d3132016-10-05 12:11:22 +03001624 if (host->ops->set_power)
1625 host->ops->set_power(host, ios->power_mode, ios->vdd);
1626 else
1627 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001628
Philip Rakity643a81f2010-09-23 08:24:32 -07001629 if (host->ops->platform_send_init_74_clocks)
1630 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1631
Russell King2317f562014-04-25 12:57:07 +01001632 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001633
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001634 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001635
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001636 if ((ios->timing == MMC_TIMING_SD_HS ||
Jaehoon Chung273c5412016-10-07 14:08:43 +09001637 ios->timing == MMC_TIMING_MMC_HS ||
1638 ios->timing == MMC_TIMING_MMC_HS400 ||
1639 ios->timing == MMC_TIMING_MMC_HS200 ||
1640 ios->timing == MMC_TIMING_MMC_DDR52 ||
1641 ios->timing == MMC_TIMING_UHS_SDR50 ||
1642 ios->timing == MMC_TIMING_UHS_SDR104 ||
1643 ios->timing == MMC_TIMING_UHS_DDR50 ||
1644 ios->timing == MMC_TIMING_UHS_SDR25)
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001645 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001646 ctrl |= SDHCI_CTRL_HISPD;
1647 else
1648 ctrl &= ~SDHCI_CTRL_HISPD;
1649
Arindam Nathd6d50a12011-05-05 12:18:59 +05301650 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301651 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301652
Russell Kingda91a8f2014-04-25 13:00:12 +01001653 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301654 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301655 /*
1656 * We only need to set Driver Strength if the
1657 * preset value enable is not set.
1658 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001659 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301660 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1661 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1662 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001663 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1664 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301665 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1666 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001667 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1668 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1669 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001670 pr_warn("%s: invalid driver type, default to driver type B\n",
1671 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001672 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1673 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301674
1675 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301676 } else {
1677 /*
1678 * According to SDHC Spec v3.00, if the Preset Value
1679 * Enable in the Host Control 2 register is set, we
1680 * need to reset SD Clock Enable before changing High
1681 * Speed Enable to avoid generating clock gliches.
1682 */
Arindam Nath758535c2011-05-05 12:19:00 +05301683
1684 /* Reset SD Clock Enable */
1685 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1686 clk &= ~SDHCI_CLOCK_CARD_EN;
1687 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1688
1689 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1690
1691 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001692 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301693 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301694
Arindam Nath49c468f2011-05-05 12:19:01 +05301695 /* Reset SD Clock Enable */
1696 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1697 clk &= ~SDHCI_CLOCK_CARD_EN;
1698 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1699
Russell King96d7b782014-04-25 12:59:26 +01001700 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001701 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301702
Kevin Liu52983382013-01-31 11:31:37 +08001703 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1704 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1705 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1706 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1707 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001708 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1709 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001710 u16 preset;
1711
1712 sdhci_enable_preset_value(host, true);
1713 preset = sdhci_get_preset_value(host);
1714 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1715 >> SDHCI_PRESET_DRV_SHIFT;
1716 }
1717
Arindam Nath49c468f2011-05-05 12:19:01 +05301718 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001719 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301720 } else
1721 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301722
Leandro Dorileob8352262007-07-25 23:47:04 +02001723 /*
1724 * Some (ENE) controllers go apeshit on some ios operation,
1725 * signalling timeout and CRC errors even on CMD0. Resetting
1726 * it on each ios seems to solve the problem.
1727 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301728 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001729 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001730
Pierre Ossman5f25a662006-10-04 02:15:39 -07001731 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001732 spin_unlock_irqrestore(&host->lock, flags);
1733}
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
2005static void sdhci_abort_tuning(struct sdhci_host *host, u32 opcode,
2006 unsigned long flags)
2007{
2008 sdhci_reset_tuning(host);
2009
2010 sdhci_do_reset(host, SDHCI_RESET_CMD);
2011 sdhci_do_reset(host, SDHCI_RESET_DATA);
2012
2013 sdhci_end_tuning(host);
2014
2015 spin_unlock_irqrestore(&host->lock, flags);
2016 mmc_abort_tuning(host->mmc, opcode);
2017 spin_lock_irqsave(&host->lock, flags);
2018}
2019
2020/*
2021 * We use sdhci_send_tuning() because mmc_send_tuning() is not a good fit. SDHCI
2022 * tuning command does not have a data payload (or rather the hardware does it
2023 * automatically) so mmc_send_tuning() will return -EIO. Also the tuning command
2024 * interrupt setup is different to other commands and there is no timeout
2025 * interrupt so special handling is needed.
2026 */
2027static void sdhci_send_tuning(struct sdhci_host *host, u32 opcode,
2028 unsigned long flags)
2029{
2030 struct mmc_host *mmc = host->mmc;
Masahiro Yamadac7836d12016-12-19 20:51:18 +09002031 struct mmc_command cmd = {};
2032 struct mmc_request mrq = {};
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002033
2034 cmd.opcode = opcode;
2035 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2036 cmd.mrq = &mrq;
2037
2038 mrq.cmd = &cmd;
2039 /*
2040 * In response to CMD19, the card sends 64 bytes of tuning
2041 * block to the Host Controller. So we set the block size
2042 * to 64 here.
2043 */
Adrian Hunter85336102016-12-02 15:14:26 +02002044 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200 &&
2045 mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2046 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128), SDHCI_BLOCK_SIZE);
2047 else
2048 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64), SDHCI_BLOCK_SIZE);
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002049
2050 /*
2051 * The tuning block is sent by the card to the host controller.
2052 * So we set the TRNS_READ bit in the Transfer Mode register.
2053 * This also takes care of setting DMA Enable and Multi Block
2054 * Select in the same register to 0.
2055 */
2056 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2057
2058 sdhci_send_command(host, &cmd);
2059
2060 host->cmd = NULL;
2061
2062 sdhci_del_timer(host, &mrq);
2063
2064 host->tuning_done = 0;
2065
2066 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
2072 spin_lock_irqsave(&host->lock, flags);
2073}
2074
Adrian Hunter6b11e702016-12-02 15:14:27 +02002075static void __sdhci_execute_tuning(struct sdhci_host *host, u32 opcode,
2076 unsigned long flags)
2077{
2078 int i;
2079
2080 /*
2081 * Issue opcode repeatedly till Execute Tuning is set to 0 or the number
2082 * of loops reaches 40 times.
2083 */
2084 for (i = 0; i < MAX_TUNING_LOOP; i++) {
2085 u16 ctrl;
2086
2087 sdhci_send_tuning(host, opcode, flags);
2088
2089 if (!host->tuning_done) {
2090 pr_info("%s: Tuning timeout, falling back to fixed sampling clock\n",
2091 mmc_hostname(host->mmc));
2092 sdhci_abort_tuning(host, opcode, flags);
2093 return;
2094 }
2095
2096 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2097 if (!(ctrl & SDHCI_CTRL_EXEC_TUNING)) {
2098 if (ctrl & SDHCI_CTRL_TUNED_CLK)
2099 return; /* Success! */
2100 break;
2101 }
2102
2103 /* eMMC spec does not require a delay between tuning cycles */
2104 if (opcode == MMC_SEND_TUNING_BLOCK)
2105 mdelay(1);
2106 }
2107
2108 pr_info("%s: Tuning failed, falling back to fixed sampling clock\n",
2109 mmc_hostname(host->mmc));
2110 sdhci_reset_tuning(host);
2111}
2112
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002113int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05302114{
Russell King4b6f37d2014-04-25 12:59:36 +01002115 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05302116 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002117 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002118 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002119 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05302120
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002121 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302122
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002123 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002124
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002125 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
2126 tuning_count = host->tuning_count;
2127
Arindam Nathb513ea22011-05-05 12:19:04 +05302128 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00002129 * The Host Controller needs tuning in case of SDR104 and DDR50
2130 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
2131 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05302132 * If the Host Controller supports the HS200 mode then the
2133 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05302134 */
Russell King4b6f37d2014-04-25 12:59:36 +01002135 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002136 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02002137 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002138 err = -EINVAL;
2139 goto out_unlock;
2140
Russell King4b6f37d2014-04-25 12:59:36 +01002141 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002142 /*
2143 * Periodic re-tuning for HS400 is not expected to be needed, so
2144 * disable it here.
2145 */
2146 if (hs400_tuning)
2147 tuning_count = 0;
2148 break;
2149
Russell King4b6f37d2014-04-25 12:59:36 +01002150 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00002151 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01002152 break;
Girish K S069c9f12012-01-06 09:56:39 +05302153
Russell King4b6f37d2014-04-25 12:59:36 +01002154 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002155 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002156 break;
2157 /* FALLTHROUGH */
2158
2159 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02002160 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05302161 }
2162
Dong Aisheng45251812013-09-13 19:11:30 +08002163 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002164 spin_unlock_irqrestore(&host->lock, flags);
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302165 err = host->ops->platform_execute_tuning(host, opcode);
2166 spin_lock_irqsave(&host->lock, flags);
2167 goto out_unlock;
Dong Aisheng45251812013-09-13 19:11:30 +08002168 }
2169
Adrian Hunter6b11e702016-12-02 15:14:27 +02002170 host->mmc->retune_period = tuning_count;
2171
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002172 sdhci_start_tuning(host);
Arindam Nathb513ea22011-05-05 12:19:04 +05302173
Adrian Hunter6b11e702016-12-02 15:14:27 +02002174 __sdhci_execute_tuning(host, opcode, flags);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302175
Adrian Hunterda4bc4f2016-12-02 15:59:23 +02002176 sdhci_end_tuning(host);
Adrian Hunterd519c862014-12-05 19:25:29 +02002177out_unlock:
Ritesh Harjani8a8fa872017-01-10 12:30:50 +05302178 host->flags &= ~SDHCI_HS400_TUNING;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002179 spin_unlock_irqrestore(&host->lock, flags);
Adrian Hunter6b11e702016-12-02 15:14:27 +02002180
Arindam Nathb513ea22011-05-05 12:19:04 +05302181 return err;
2182}
Masahiro Yamada85a882c2016-12-08 21:50:54 +09002183EXPORT_SYMBOL_GPL(sdhci_execute_tuning);
Arindam Nathb513ea22011-05-05 12:19:04 +05302184
Adrian Huntercb849642015-02-06 14:12:59 +02002185static int sdhci_select_drive_strength(struct mmc_card *card,
2186 unsigned int max_dtr, int host_drv,
2187 int card_drv, int *drv_type)
2188{
2189 struct sdhci_host *host = mmc_priv(card->host);
2190
2191 if (!host->ops->select_drive_strength)
2192 return 0;
2193
2194 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2195 card_drv, drv_type);
2196}
Kevin Liu52983382013-01-31 11:31:37 +08002197
2198static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302199{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302200 /* Host Controller v3.00 defines preset value registers */
2201 if (host->version < SDHCI_SPEC_300)
2202 return;
2203
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302204 /*
2205 * We only enable or disable Preset Value if they are not already
2206 * enabled or disabled respectively. Otherwise, we bail out.
2207 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002208 if (host->preset_enabled != enable) {
2209 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2210
2211 if (enable)
2212 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2213 else
2214 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2215
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302216 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002217
2218 if (enable)
2219 host->flags |= SDHCI_PV_ENABLED;
2220 else
2221 host->flags &= ~SDHCI_PV_ENABLED;
2222
2223 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302224 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002225}
2226
Haibo Chen348487c2014-12-09 17:04:05 +08002227static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2228 int err)
2229{
2230 struct sdhci_host *host = mmc_priv(mmc);
2231 struct mmc_data *data = mrq->data;
2232
Russell Kingf48f0392016-01-26 13:40:32 +00002233 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002234 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2235 data->flags & MMC_DATA_WRITE ?
2236 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2237
2238 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002239}
2240
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002241static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002242{
2243 struct sdhci_host *host = mmc_priv(mmc);
2244
Haibo Chend31911b2015-08-25 10:02:11 +08002245 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002246
2247 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002248 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002249}
2250
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002251static inline bool sdhci_has_requests(struct sdhci_host *host)
2252{
2253 return host->cmd || host->data_cmd;
2254}
2255
2256static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2257{
2258 if (host->data_cmd) {
2259 host->data_cmd->error = err;
2260 sdhci_finish_mrq(host, host->data_cmd->mrq);
2261 }
2262
2263 if (host->cmd) {
2264 host->cmd->error = err;
2265 sdhci_finish_mrq(host, host->cmd->mrq);
2266 }
2267}
2268
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002269static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002270{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002271 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002272 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002273 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002274
Christian Daudt722e1282013-06-20 14:26:36 -07002275 /* First check if client has provided their own card event */
2276 if (host->ops->card_event)
2277 host->ops->card_event(host);
2278
Adrian Hunterd3940f22016-06-29 16:24:14 +03002279 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002280
Pierre Ossmand129bce2006-03-24 03:18:17 -08002281 spin_lock_irqsave(&host->lock, flags);
2282
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002283 /* Check sdhci_has_requests() first in case we are runtime suspended */
2284 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302285 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002286 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302287 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002288 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002289
Russell King03231f92014-04-25 12:57:12 +01002290 sdhci_do_reset(host, SDHCI_RESET_CMD);
2291 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002292
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002293 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002294 }
2295
2296 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002297}
2298
2299static const struct mmc_host_ops sdhci_ops = {
2300 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002301 .post_req = sdhci_post_req,
2302 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002303 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002304 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002305 .get_ro = sdhci_get_ro,
2306 .hw_reset = sdhci_hw_reset,
2307 .enable_sdio_irq = sdhci_enable_sdio_irq,
2308 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002309 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002310 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002311 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002312 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002313 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002314};
2315
2316/*****************************************************************************\
2317 * *
2318 * Tasklets *
2319 * *
2320\*****************************************************************************/
2321
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002322static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002323{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002324 unsigned long flags;
2325 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002326 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002327
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002328 spin_lock_irqsave(&host->lock, flags);
2329
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002330 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2331 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002332 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002333 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002334 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002335
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002336 if (!mrq) {
2337 spin_unlock_irqrestore(&host->lock, flags);
2338 return true;
2339 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002340
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002341 sdhci_del_timer(host, mrq);
2342
Pierre Ossmand129bce2006-03-24 03:18:17 -08002343 /*
Russell King054cedf2016-01-26 13:40:42 +00002344 * Always unmap the data buffers if they were mapped by
2345 * sdhci_prepare_data() whenever we finish with a request.
2346 * This avoids leaking DMA mappings on error.
2347 */
2348 if (host->flags & SDHCI_REQ_USE_DMA) {
2349 struct mmc_data *data = mrq->data;
2350
2351 if (data && data->host_cookie == COOKIE_MAPPED) {
2352 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2353 (data->flags & MMC_DATA_READ) ?
2354 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2355 data->host_cookie = COOKIE_UNMAPPED;
2356 }
2357 }
2358
2359 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002360 * The controller needs a reset of internal state machines
2361 * upon error conditions.
2362 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002363 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002364 /*
2365 * Do not finish until command and data lines are available for
2366 * reset. Note there can only be one other mrq, so it cannot
2367 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2368 * would both be null.
2369 */
2370 if (host->cmd || host->data_cmd) {
2371 spin_unlock_irqrestore(&host->lock, flags);
2372 return true;
2373 }
2374
Pierre Ossman645289d2006-06-30 02:22:33 -07002375 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002376 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002377 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002378 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002379
2380 /* Spec says we should do both at the same time, but Ricoh
2381 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002382 sdhci_do_reset(host, SDHCI_RESET_CMD);
2383 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002384
2385 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002386 }
2387
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002388 if (!sdhci_has_requests(host))
2389 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002390
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002391 host->mrqs_done[i] = NULL;
2392
Pierre Ossman5f25a662006-10-04 02:15:39 -07002393 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002394 spin_unlock_irqrestore(&host->lock, flags);
2395
2396 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002397
2398 return false;
2399}
2400
2401static void sdhci_tasklet_finish(unsigned long param)
2402{
2403 struct sdhci_host *host = (struct sdhci_host *)param;
2404
2405 while (!sdhci_request_done(host))
2406 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002407}
2408
2409static void sdhci_timeout_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
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002418 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2419 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2420 mmc_hostname(host->mmc));
2421 sdhci_dumpregs(host);
2422
2423 host->cmd->error = -ETIMEDOUT;
2424 sdhci_finish_mrq(host, host->cmd->mrq);
2425 }
2426
2427 mmiowb();
2428 spin_unlock_irqrestore(&host->lock, flags);
2429}
2430
2431static void sdhci_timeout_data_timer(unsigned long data)
2432{
2433 struct sdhci_host *host;
2434 unsigned long flags;
2435
2436 host = (struct sdhci_host *)data;
2437
2438 spin_lock_irqsave(&host->lock, flags);
2439
2440 if (host->data || host->data_cmd ||
2441 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002442 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2443 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002444 sdhci_dumpregs(host);
2445
2446 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002447 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002448 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002449 } else if (host->data_cmd) {
2450 host->data_cmd->error = -ETIMEDOUT;
2451 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002452 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002453 host->cmd->error = -ETIMEDOUT;
2454 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002455 }
2456 }
2457
Pierre Ossman5f25a662006-10-04 02:15:39 -07002458 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002459 spin_unlock_irqrestore(&host->lock, flags);
2460}
2461
2462/*****************************************************************************\
2463 * *
2464 * Interrupt handling *
2465 * *
2466\*****************************************************************************/
2467
Adrian Hunterfc605f12016-10-05 12:11:21 +03002468static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002469{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002470 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002471 /*
2472 * SDHCI recovers from errors by resetting the cmd and data
2473 * circuits. Until that is done, there very well might be more
2474 * interrupts, so ignore them in that case.
2475 */
2476 if (host->pending_reset)
2477 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002478 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2479 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002480 sdhci_dumpregs(host);
2481 return;
2482 }
2483
Russell Kingec014cb2016-01-26 13:39:39 +00002484 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2485 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2486 if (intmask & SDHCI_INT_TIMEOUT)
2487 host->cmd->error = -ETIMEDOUT;
2488 else
2489 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002490
Russell King71fcbda2016-01-26 13:39:45 +00002491 /*
2492 * If this command initiates a data phase and a response
2493 * CRC error is signalled, the card can start transferring
2494 * data - the card may have received the command without
2495 * error. We must not terminate the mmc_request early.
2496 *
2497 * If the card did not receive the command or returned an
2498 * error which prevented it sending data, the data phase
2499 * will time out.
2500 */
2501 if (host->cmd->data &&
2502 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2503 SDHCI_INT_CRC) {
2504 host->cmd = NULL;
2505 return;
2506 }
2507
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002508 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002509 return;
2510 }
2511
Pierre Ossmane8095172008-07-25 01:09:08 +02002512 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002513 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002514}
2515
George G. Davis0957c332010-02-18 12:32:12 -05002516#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002517static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002518{
2519 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002520 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002521
2522 sdhci_dumpregs(host);
2523
2524 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002525 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002526
Adrian Huntere57a5f62014-11-04 12:42:46 +02002527 if (host->flags & SDHCI_USE_64_BIT_DMA)
2528 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2529 name, desc, le32_to_cpu(dma_desc->addr_hi),
2530 le32_to_cpu(dma_desc->addr_lo),
2531 le16_to_cpu(dma_desc->len),
2532 le16_to_cpu(dma_desc->cmd));
2533 else
2534 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2535 name, desc, le32_to_cpu(dma_desc->addr_lo),
2536 le16_to_cpu(dma_desc->len),
2537 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002538
Adrian Hunter76fe3792014-11-04 12:42:42 +02002539 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002540
Adrian Hunter05452302014-11-04 12:42:45 +02002541 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002542 break;
2543 }
2544}
2545#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002546static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002547#endif
2548
Pierre Ossmand129bce2006-03-24 03:18:17 -08002549static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2550{
Girish K S069c9f12012-01-06 09:56:39 +05302551 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002552
Arindam Nathb513ea22011-05-05 12:19:04 +05302553 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2554 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302555 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2556 if (command == MMC_SEND_TUNING_BLOCK ||
2557 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302558 host->tuning_done = 1;
2559 wake_up(&host->buf_ready_int);
2560 return;
2561 }
2562 }
2563
Pierre Ossmand129bce2006-03-24 03:18:17 -08002564 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002565 struct mmc_command *data_cmd = host->data_cmd;
2566
Pierre Ossmand129bce2006-03-24 03:18:17 -08002567 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002568 * The "data complete" interrupt is also used to
2569 * indicate that a busy state has ended. See comment
2570 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002571 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002572 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002573 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002574 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002575 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002576 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002577 return;
2578 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002579 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002580 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002581 /*
2582 * Some cards handle busy-end interrupt
2583 * before the command completed, so make
2584 * sure we do things in the proper order.
2585 */
Adrian Hunterea968022016-06-29 16:24:24 +03002586 if (host->cmd == data_cmd)
2587 return;
2588
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002589 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002590 return;
2591 }
2592 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002593
Adrian Huntered1563d2016-06-29 16:24:29 +03002594 /*
2595 * SDHCI recovers from errors by resetting the cmd and data
2596 * circuits. Until that is done, there very well might be more
2597 * interrupts, so ignore them in that case.
2598 */
2599 if (host->pending_reset)
2600 return;
2601
Marek Vasut2e4456f2015-11-18 10:47:02 +01002602 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2603 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002604 sdhci_dumpregs(host);
2605
2606 return;
2607 }
2608
2609 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002610 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002611 else if (intmask & SDHCI_INT_DATA_END_BIT)
2612 host->data->error = -EILSEQ;
2613 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2614 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2615 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002616 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002617 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302618 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002619 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002620 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002621 if (host->ops->adma_workaround)
2622 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002623 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002624
Pierre Ossman17b04292007-07-22 22:18:46 +02002625 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002626 sdhci_finish_data(host);
2627 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002628 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002629 sdhci_transfer_pio(host);
2630
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002631 /*
2632 * We currently don't do anything fancy with DMA
2633 * boundaries, but as we can't disable the feature
2634 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002635 *
2636 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2637 * should return a valid address to continue from, but as
2638 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002639 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002640 if (intmask & SDHCI_INT_DMA_END) {
2641 u32 dmastart, dmanow;
2642 dmastart = sg_dma_address(host->data->sg);
2643 dmanow = dmastart + host->data->bytes_xfered;
2644 /*
2645 * Force update to the next DMA block boundary.
2646 */
2647 dmanow = (dmanow &
2648 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2649 SDHCI_DEFAULT_BOUNDARY_SIZE;
2650 host->data->bytes_xfered = dmanow - dmastart;
2651 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2652 " next 0x%08x\n",
2653 mmc_hostname(host->mmc), dmastart,
2654 host->data->bytes_xfered, dmanow);
2655 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2656 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002657
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002658 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002659 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002660 /*
2661 * Data managed to finish before the
2662 * command completed. Make sure we do
2663 * things in the proper order.
2664 */
2665 host->data_early = 1;
2666 } else {
2667 sdhci_finish_data(host);
2668 }
2669 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002670 }
2671}
2672
David Howells7d12e782006-10-05 14:55:46 +01002673static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002674{
Russell King781e9892014-04-25 12:55:46 +01002675 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002676 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002677 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002678 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002679
2680 spin_lock(&host->lock);
2681
Russell Kingbe138552014-04-25 12:55:56 +01002682 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002683 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002684 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002685 }
2686
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002687 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002688 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002689 result = IRQ_NONE;
2690 goto out;
2691 }
2692
Russell King41005002014-04-25 12:55:36 +01002693 do {
2694 /* Clear selected interrupts. */
2695 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2696 SDHCI_INT_BUS_POWER);
2697 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002698
Russell King41005002014-04-25 12:55:36 +01002699 DBG("*** %s got interrupt: 0x%08x\n",
2700 mmc_hostname(host->mmc), intmask);
2701
2702 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2703 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2704 SDHCI_CARD_PRESENT;
2705
2706 /*
2707 * There is a observation on i.mx esdhc. INSERT
2708 * bit will be immediately set again when it gets
2709 * cleared, if a card is inserted. We have to mask
2710 * the irq to prevent interrupt storm which will
2711 * freeze the system. And the REMOVE gets the
2712 * same situation.
2713 *
2714 * More testing are needed here to ensure it works
2715 * for other platforms though.
2716 */
Russell Kingb537f942014-04-25 12:56:01 +01002717 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2718 SDHCI_INT_CARD_REMOVE);
2719 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2720 SDHCI_INT_CARD_INSERT;
2721 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2722 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002723
2724 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2725 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002726
2727 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2728 SDHCI_INT_CARD_REMOVE);
2729 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002730 }
2731
2732 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002733 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002734
2735 if (intmask & SDHCI_INT_DATA_MASK)
2736 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2737
2738 if (intmask & SDHCI_INT_BUS_POWER)
2739 pr_err("%s: Card is consuming too much power!\n",
2740 mmc_hostname(host->mmc));
2741
Dong Aishengf37b20e2016-07-12 15:46:17 +08002742 if (intmask & SDHCI_INT_RETUNE)
2743 mmc_retune_needed(host->mmc);
2744
Gabriel Krisman Bertazi161e6d42017-01-16 12:23:42 -02002745 if ((intmask & SDHCI_INT_CARD_INT) &&
2746 (host->ier & SDHCI_INT_CARD_INT)) {
Russell King781e9892014-04-25 12:55:46 +01002747 sdhci_enable_sdio_irq_nolock(host, false);
2748 host->thread_isr |= SDHCI_INT_CARD_INT;
2749 result = IRQ_WAKE_THREAD;
2750 }
Russell King41005002014-04-25 12:55:36 +01002751
2752 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2753 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2754 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002755 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002756
2757 if (intmask) {
2758 unexpected |= intmask;
2759 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2760 }
2761
Russell King781e9892014-04-25 12:55:46 +01002762 if (result == IRQ_NONE)
2763 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002764
2765 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002766 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002767out:
2768 spin_unlock(&host->lock);
2769
Alexander Stein6379b232012-03-14 09:52:10 +01002770 if (unexpected) {
2771 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2772 mmc_hostname(host->mmc), unexpected);
2773 sdhci_dumpregs(host);
2774 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002775
Pierre Ossmand129bce2006-03-24 03:18:17 -08002776 return result;
2777}
2778
Russell King781e9892014-04-25 12:55:46 +01002779static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2780{
2781 struct sdhci_host *host = dev_id;
2782 unsigned long flags;
2783 u32 isr;
2784
2785 spin_lock_irqsave(&host->lock, flags);
2786 isr = host->thread_isr;
2787 host->thread_isr = 0;
2788 spin_unlock_irqrestore(&host->lock, flags);
2789
Russell King3560db82014-04-25 12:55:51 +01002790 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002791 struct mmc_host *mmc = host->mmc;
2792
2793 mmc->ops->card_event(mmc);
2794 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002795 }
2796
Russell King781e9892014-04-25 12:55:46 +01002797 if (isr & SDHCI_INT_CARD_INT) {
2798 sdio_run_irqs(host->mmc);
2799
2800 spin_lock_irqsave(&host->lock, flags);
2801 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2802 sdhci_enable_sdio_irq_nolock(host, true);
2803 spin_unlock_irqrestore(&host->lock, flags);
2804 }
2805
2806 return isr ? IRQ_HANDLED : IRQ_NONE;
2807}
2808
Pierre Ossmand129bce2006-03-24 03:18:17 -08002809/*****************************************************************************\
2810 * *
2811 * Suspend/resume *
2812 * *
2813\*****************************************************************************/
2814
2815#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002816/*
2817 * To enable wakeup events, the corresponding events have to be enabled in
2818 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2819 * Table' in the SD Host Controller Standard Specification.
2820 * It is useless to restore SDHCI_INT_ENABLE state in
2821 * sdhci_disable_irq_wakeups() since it will be set by
2822 * sdhci_enable_card_detection() or sdhci_init().
2823 */
Kevin Liuad080d72013-01-05 17:21:33 +08002824void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2825{
2826 u8 val;
2827 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2828 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002829 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2830 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002831
2832 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2833 val |= mask ;
2834 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002835 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002836 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002837 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2838 }
Kevin Liuad080d72013-01-05 17:21:33 +08002839 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002840 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002841}
2842EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2843
Fabio Estevam0b10f472014-08-30 14:53:13 -03002844static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002845{
2846 u8 val;
2847 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2848 | SDHCI_WAKE_ON_INT;
2849
2850 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2851 val &= ~mask;
2852 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2853}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002854
Manuel Lauss29495aa2011-11-03 11:09:45 +01002855int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002856{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002857 sdhci_disable_card_detection(host);
2858
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002859 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08002860 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
2861 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302862
Kevin Liuad080d72013-01-05 17:21:33 +08002863 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002864 host->ier = 0;
2865 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2866 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002867 free_irq(host->irq, host);
2868 } else {
2869 sdhci_enable_irq_wakeups(host);
2870 enable_irq_wake(host->irq);
2871 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002872 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002873}
2874
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002875EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002876
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002877int sdhci_resume_host(struct sdhci_host *host)
2878{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002879 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002880 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002881
Richard Röjforsa13abc72009-09-22 16:45:30 -07002882 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002883 if (host->ops->enable_dma)
2884 host->ops->enable_dma(host);
2885 }
2886
Adrian Hunter6308d292012-02-07 14:48:54 +02002887 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2888 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2889 /* Card keeps power but host controller does not */
2890 sdhci_init(host, 0);
2891 host->pwr = 0;
2892 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002893 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002894 } else {
2895 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2896 mmiowb();
2897 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002898
Haibo Chen14a7b41642015-09-15 18:32:58 +08002899 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2900 ret = request_threaded_irq(host->irq, sdhci_irq,
2901 sdhci_thread_irq, IRQF_SHARED,
2902 mmc_hostname(host->mmc), host);
2903 if (ret)
2904 return ret;
2905 } else {
2906 sdhci_disable_irq_wakeups(host);
2907 disable_irq_wake(host->irq);
2908 }
2909
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002910 sdhci_enable_card_detection(host);
2911
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002912 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002913}
2914
2915EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002916
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002917int sdhci_runtime_suspend_host(struct sdhci_host *host)
2918{
2919 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002920
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002921 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08002922 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
2923 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002924
2925 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002926 host->ier &= SDHCI_INT_CARD_INT;
2927 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2928 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002929 spin_unlock_irqrestore(&host->lock, flags);
2930
Russell King781e9892014-04-25 12:55:46 +01002931 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002932
2933 spin_lock_irqsave(&host->lock, flags);
2934 host->runtime_suspended = true;
2935 spin_unlock_irqrestore(&host->lock, flags);
2936
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002937 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002938}
2939EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2940
2941int sdhci_runtime_resume_host(struct sdhci_host *host)
2942{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002943 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002944 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002945 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002946
2947 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2948 if (host->ops->enable_dma)
2949 host->ops->enable_dma(host);
2950 }
2951
2952 sdhci_init(host, 0);
2953
Adrian Hunter84ec0482016-12-19 15:33:11 +02002954 if (mmc->ios.power_mode != MMC_POWER_UNDEFINED) {
2955 /* Force clock and power re-program */
2956 host->pwr = 0;
2957 host->clock = 0;
2958 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2959 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002960
Adrian Hunter84ec0482016-12-19 15:33:11 +02002961 if ((host_flags & SDHCI_PV_ENABLED) &&
2962 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2963 spin_lock_irqsave(&host->lock, flags);
2964 sdhci_enable_preset_value(host, true);
2965 spin_unlock_irqrestore(&host->lock, flags);
2966 }
2967
2968 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2969 mmc->ops->hs400_enhanced_strobe)
2970 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
Kevin Liu52983382013-01-31 11:31:37 +08002971 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002972
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002973 spin_lock_irqsave(&host->lock, flags);
2974
2975 host->runtime_suspended = false;
2976
2977 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002978 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002979 sdhci_enable_sdio_irq_nolock(host, true);
2980
2981 /* Enable Card Detection */
2982 sdhci_enable_card_detection(host);
2983
2984 spin_unlock_irqrestore(&host->lock, flags);
2985
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002986 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002987}
2988EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2989
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002990#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002991
Pierre Ossmand129bce2006-03-24 03:18:17 -08002992/*****************************************************************************\
2993 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002994 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002995 * *
2996\*****************************************************************************/
2997
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002998struct sdhci_host *sdhci_alloc_host(struct device *dev,
2999 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003000{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003001 struct mmc_host *mmc;
3002 struct sdhci_host *host;
3003
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003004 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003005
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003006 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003007 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003008 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003009
3010 host = mmc_priv(mmc);
3011 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02003012 host->mmc_host_ops = sdhci_ops;
3013 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003014
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003015 host->flags = SDHCI_SIGNALING_330;
3016
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003017 return host;
3018}
Pierre Ossman8a4da142006-10-04 02:15:40 -07003019
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003020EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003021
Alexandre Courbot7b913692016-03-07 11:07:55 +09003022static int sdhci_set_dma_mask(struct sdhci_host *host)
3023{
3024 struct mmc_host *mmc = host->mmc;
3025 struct device *dev = mmc_dev(mmc);
3026 int ret = -EINVAL;
3027
3028 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
3029 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3030
3031 /* Try 64-bit mask if hardware is capable of it */
3032 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3033 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
3034 if (ret) {
3035 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
3036 mmc_hostname(mmc));
3037 host->flags &= ~SDHCI_USE_64_BIT_DMA;
3038 }
3039 }
3040
3041 /* 32-bit mask as default & fallback */
3042 if (ret) {
3043 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3044 if (ret)
3045 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3046 mmc_hostname(mmc));
3047 }
3048
3049 return ret;
3050}
3051
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003052void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3053{
3054 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003055 u64 dt_caps_mask = 0;
3056 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003057
3058 if (host->read_caps)
3059 return;
3060
3061 host->read_caps = true;
3062
3063 if (debug_quirks)
3064 host->quirks = debug_quirks;
3065
3066 if (debug_quirks2)
3067 host->quirks2 = debug_quirks2;
3068
3069 sdhci_do_reset(host, SDHCI_RESET_ALL);
3070
Zach Brown92e0c442016-11-02 10:26:16 -05003071 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3072 "sdhci-caps-mask", &dt_caps_mask);
3073 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3074 "sdhci-caps", &dt_caps);
3075
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003076 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3077 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3078
3079 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3080 return;
3081
Zach Brown92e0c442016-11-02 10:26:16 -05003082 if (caps) {
3083 host->caps = *caps;
3084 } else {
3085 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3086 host->caps &= ~lower_32_bits(dt_caps_mask);
3087 host->caps |= lower_32_bits(dt_caps);
3088 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003089
3090 if (host->version < SDHCI_SPEC_300)
3091 return;
3092
Zach Brown92e0c442016-11-02 10:26:16 -05003093 if (caps1) {
3094 host->caps1 = *caps1;
3095 } else {
3096 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3097 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3098 host->caps1 |= upper_32_bits(dt_caps);
3099 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003100}
3101EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3102
Adrian Hunter52f53362016-06-29 16:24:15 +03003103int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003104{
3105 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303106 u32 max_current_caps;
3107 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003108 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003109 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003110 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003111
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003112 WARN_ON(host == NULL);
3113 if (host == NULL)
3114 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003115
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003116 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003117
Jon Hunterefba1422016-07-12 14:53:36 +01003118 /*
3119 * If there are external regulators, get them. Note this must be done
3120 * early before resetting the host and reading the capabilities so that
3121 * the host can take the appropriate action if regulators are not
3122 * available.
3123 */
3124 ret = mmc_regulator_get_supply(mmc);
3125 if (ret == -EPROBE_DEFER)
3126 return ret;
3127
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003128 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003129
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003130 override_timeout_clk = host->timeout_clk;
3131
Zhangfei Gao85105c52010-08-06 07:10:01 +08003132 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003133 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3134 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003135 }
3136
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003137 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003138 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003139 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003140 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003141 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003142 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003143
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003144 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003145 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003146 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003147 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003148 }
3149
Arindam Nathf2119df2011-05-05 12:18:57 +05303150 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003151 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003152 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003153
3154 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3155 (host->flags & SDHCI_USE_ADMA)) {
3156 DBG("Disabling ADMA as it is marked broken\n");
3157 host->flags &= ~SDHCI_USE_ADMA;
3158 }
3159
Adrian Huntere57a5f62014-11-04 12:42:46 +02003160 /*
3161 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3162 * and *must* do 64-bit DMA. A driver has the opportunity to change
3163 * that during the first call to ->enable_dma(). Similarly
3164 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3165 * implement.
3166 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003167 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003168 host->flags |= SDHCI_USE_64_BIT_DMA;
3169
Richard Röjforsa13abc72009-09-22 16:45:30 -07003170 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003171 ret = sdhci_set_dma_mask(host);
3172
3173 if (!ret && host->ops->enable_dma)
3174 ret = host->ops->enable_dma(host);
3175
3176 if (ret) {
3177 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3178 mmc_hostname(mmc));
3179 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3180
3181 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003182 }
3183 }
3184
Adrian Huntere57a5f62014-11-04 12:42:46 +02003185 /* SDMA does not support 64-bit DMA */
3186 if (host->flags & SDHCI_USE_64_BIT_DMA)
3187 host->flags &= ~SDHCI_USE_SDMA;
3188
Pierre Ossman2134a922008-06-28 18:28:51 +02003189 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003190 dma_addr_t dma;
3191 void *buf;
3192
Pierre Ossman2134a922008-06-28 18:28:51 +02003193 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003194 * The DMA descriptor table size is calculated as the maximum
3195 * number of segments times 2, to allow for an alignment
3196 * descriptor for each segment, plus 1 for a nop end descriptor,
3197 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003198 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003199 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3200 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3201 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003202 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003203 } else {
3204 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3205 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003206 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003207 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003208
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003209 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003210 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3211 host->adma_table_sz, &dma, GFP_KERNEL);
3212 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003213 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003214 mmc_hostname(mmc));
3215 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003216 } else if ((dma + host->align_buffer_sz) &
3217 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003218 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3219 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003220 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003221 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3222 host->adma_table_sz, buf, dma);
3223 } else {
3224 host->align_buffer = buf;
3225 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003226
Russell Kinge66e61c2016-01-26 13:39:55 +00003227 host->adma_table = buf + host->align_buffer_sz;
3228 host->adma_addr = dma + host->align_buffer_sz;
3229 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003230 }
3231
Pierre Ossman76591502008-07-21 00:32:11 +02003232 /*
3233 * If we use DMA, then it's up to the caller to set the DMA
3234 * mask, but PIO does not need the hw shim so we set a new
3235 * mask here in that case.
3236 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003237 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003238 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003239 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003240 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003241
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003242 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003243 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003244 >> SDHCI_CLOCK_BASE_SHIFT;
3245 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003246 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003247 >> SDHCI_CLOCK_BASE_SHIFT;
3248
Pierre Ossmand129bce2006-03-24 03:18:17 -08003249 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003250 if (host->max_clk == 0 || host->quirks &
3251 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003252 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003253 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3254 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003255 ret = -ENODEV;
3256 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003257 }
3258 host->max_clk = host->ops->get_max_clock(host);
3259 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003260
3261 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303262 * In case of Host Controller v3.00, find out whether clock
3263 * multiplier is supported.
3264 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003265 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303266 SDHCI_CLOCK_MUL_SHIFT;
3267
3268 /*
3269 * In case the value in Clock Multiplier is 0, then programmable
3270 * clock mode is not supported, otherwise the actual clock
3271 * multiplier is one more than the value of Clock Multiplier
3272 * in the Capabilities Register.
3273 */
3274 if (host->clk_mul)
3275 host->clk_mul += 1;
3276
3277 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003278 * Set host parameters.
3279 */
Dong Aisheng59241752015-07-22 20:53:07 +08003280 max_clk = host->max_clk;
3281
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003282 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003283 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303284 else if (host->version >= SDHCI_SPEC_300) {
3285 if (host->clk_mul) {
3286 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003287 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303288 } else
3289 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3290 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003291 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003292
Adrian Hunterd310ae42016-04-12 14:25:07 +03003293 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003294 mmc->f_max = max_clk;
3295
Aisheng Dong28aab052014-08-27 15:26:31 +08003296 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003297 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003298 SDHCI_TIMEOUT_CLK_SHIFT;
3299 if (host->timeout_clk == 0) {
3300 if (host->ops->get_timeout_clock) {
3301 host->timeout_clk =
3302 host->ops->get_timeout_clock(host);
3303 } else {
3304 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3305 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003306 ret = -ENODEV;
3307 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003308 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003309 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003310
Adrian Hunter28da3582016-06-29 16:24:17 +03003311 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003312 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003313
Adrian Hunter99513622016-03-07 13:33:55 +02003314 if (override_timeout_clk)
3315 host->timeout_clk = override_timeout_clk;
3316
Aisheng Dong28aab052014-08-27 15:26:31 +08003317 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003318 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003319 mmc->max_busy_timeout /= host->timeout_clk;
3320 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003321
Andrei Warkentine89d4562011-05-23 15:06:37 -05003322 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003323 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003324
3325 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3326 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003327
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003328 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003329 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003330 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003331 !(host->flags & SDHCI_USE_SDMA)) &&
3332 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003333 host->flags |= SDHCI_AUTO_CMD23;
3334 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3335 } else {
3336 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3337 }
3338
Philip Rakity15ec4462010-11-19 16:48:39 -05003339 /*
3340 * A controller may support 8-bit width, but the board itself
3341 * might not have the pins brought out. Boards that support
3342 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3343 * their platform code before calling sdhci_add_host(), and we
3344 * won't assume 8-bit width for hosts without that CAP.
3345 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003346 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003347 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003348
Jerry Huang63ef5d82012-10-25 13:47:19 +08003349 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3350 mmc->caps &= ~MMC_CAP_CMD23;
3351
Adrian Hunter28da3582016-06-29 16:24:17 +03003352 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003353 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003354
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003355 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003356 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003357 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003358 mmc->caps |= MMC_CAP_NEEDS_POLL;
3359
Philip Rakity6231f3d2012-07-23 15:56:23 -07003360 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003361 if (!IS_ERR(mmc->supply.vqmmc)) {
3362 ret = regulator_enable(mmc->supply.vqmmc);
3363 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3364 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003365 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3366 SDHCI_SUPPORT_SDR50 |
3367 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003368 if (ret) {
3369 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3370 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003371 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003372 }
Kevin Liu8363c372012-11-17 17:55:51 -05003373 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003374
Adrian Hunter28da3582016-06-29 16:24:17 +03003375 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3376 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3377 SDHCI_SUPPORT_DDR50);
3378 }
Daniel Drake6a661802012-11-25 13:01:19 -05003379
Al Cooper4188bba2012-03-16 15:54:17 -04003380 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003381 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3382 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303383 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3384
3385 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003386 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303387 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003388 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3389 * field can be promoted to support HS200.
3390 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003391 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003392 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003393 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303394 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003395 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303396
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003397 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003398 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003399 mmc->caps2 |= MMC_CAP2_HS400;
3400
Adrian Hunter549c0b12014-11-06 15:19:05 +02003401 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3402 (IS_ERR(mmc->supply.vqmmc) ||
3403 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3404 1300000)))
3405 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3406
Adrian Hunter28da3582016-06-29 16:24:17 +03003407 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3408 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303409 mmc->caps |= MMC_CAP_UHS_DDR50;
3410
Girish K S069c9f12012-01-06 09:56:39 +05303411 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003412 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303413 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3414
Arindam Nathd6d50a12011-05-05 12:18:59 +05303415 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003416 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303417 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003418 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303419 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003420 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303421 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3422
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303423 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003424 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3425 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303426
3427 /*
3428 * In case Re-tuning Timer is not disabled, the actual value of
3429 * re-tuning timer will be 2 ^ (n - 1).
3430 */
3431 if (host->tuning_count)
3432 host->tuning_count = 1 << (host->tuning_count - 1);
3433
3434 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003435 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303436 SDHCI_RETUNING_MODE_SHIFT;
3437
Takashi Iwai8f230f42010-12-08 10:04:30 +01003438 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003439
Arindam Nathf2119df2011-05-05 12:18:57 +05303440 /*
3441 * According to SD Host Controller spec v3.00, if the Host System
3442 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3443 * the value is meaningful only if Voltage Support in the Capabilities
3444 * register is set. The actual current value is 4 times the register
3445 * value.
3446 */
3447 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003448 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003449 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003450 if (curr > 0) {
3451
3452 /* convert to SDHCI_MAX_CURRENT format */
3453 curr = curr/1000; /* convert to mA */
3454 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3455
3456 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3457 max_current_caps =
3458 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3459 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3460 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3461 }
3462 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303463
Adrian Hunter28da3582016-06-29 16:24:17 +03003464 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003465 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303466
Aaron Lu55c46652012-07-04 13:31:48 +08003467 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303468 SDHCI_MAX_CURRENT_330_MASK) >>
3469 SDHCI_MAX_CURRENT_330_SHIFT) *
3470 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303471 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003472 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003473 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303474
Aaron Lu55c46652012-07-04 13:31:48 +08003475 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303476 SDHCI_MAX_CURRENT_300_MASK) >>
3477 SDHCI_MAX_CURRENT_300_SHIFT) *
3478 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303479 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003480 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003481 ocr_avail |= MMC_VDD_165_195;
3482
Aaron Lu55c46652012-07-04 13:31:48 +08003483 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303484 SDHCI_MAX_CURRENT_180_MASK) >>
3485 SDHCI_MAX_CURRENT_180_SHIFT) *
3486 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303487 }
3488
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003489 /* If OCR set by host, use it instead. */
3490 if (host->ocr_mask)
3491 ocr_avail = host->ocr_mask;
3492
3493 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003494 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003495 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003496
Takashi Iwai8f230f42010-12-08 10:04:30 +01003497 mmc->ocr_avail = ocr_avail;
3498 mmc->ocr_avail_sdio = ocr_avail;
3499 if (host->ocr_avail_sdio)
3500 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3501 mmc->ocr_avail_sd = ocr_avail;
3502 if (host->ocr_avail_sd)
3503 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3504 else /* normal SD controllers don't support 1.8V */
3505 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3506 mmc->ocr_avail_mmc = ocr_avail;
3507 if (host->ocr_avail_mmc)
3508 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003509
3510 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003511 pr_err("%s: Hardware doesn't report any support voltages.\n",
3512 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003513 ret = -ENODEV;
3514 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003515 }
3516
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003517 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3518 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3519 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3520 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3521 host->flags |= SDHCI_SIGNALING_180;
3522
3523 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3524 host->flags |= SDHCI_SIGNALING_120;
3525
Pierre Ossmand129bce2006-03-24 03:18:17 -08003526 spin_lock_init(&host->lock);
3527
3528 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003529 * Maximum number of segments. Depends on if the hardware
3530 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003531 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003532 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003533 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003534 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003535 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003536 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003537 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003538
3539 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003540 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3541 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3542 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003543 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003544 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003545
3546 /*
3547 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003548 * of bytes. When doing hardware scatter/gather, each entry cannot
3549 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003550 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003551 if (host->flags & SDHCI_USE_ADMA) {
3552 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3553 mmc->max_seg_size = 65535;
3554 else
3555 mmc->max_seg_size = 65536;
3556 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003557 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003558 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003559
3560 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003561 * Maximum block size. This varies from controller to controller and
3562 * is specified in the capabilities register.
3563 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003564 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3565 mmc->max_blk_size = 2;
3566 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003567 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003568 SDHCI_MAX_BLOCK_SHIFT;
3569 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003570 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3571 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003572 mmc->max_blk_size = 0;
3573 }
3574 }
3575
3576 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003577
3578 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003579 * Maximum block count.
3580 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003581 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003582
Adrian Hunter52f53362016-06-29 16:24:15 +03003583 return 0;
3584
3585unreg:
3586 if (!IS_ERR(mmc->supply.vqmmc))
3587 regulator_disable(mmc->supply.vqmmc);
3588undma:
3589 if (host->align_buffer)
3590 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3591 host->adma_table_sz, host->align_buffer,
3592 host->align_addr);
3593 host->adma_table = NULL;
3594 host->align_buffer = NULL;
3595
3596 return ret;
3597}
3598EXPORT_SYMBOL_GPL(sdhci_setup_host);
3599
3600int __sdhci_add_host(struct sdhci_host *host)
3601{
3602 struct mmc_host *mmc = host->mmc;
3603 int ret;
3604
Pierre Ossman55db8902006-11-21 17:55:45 +01003605 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003606 * Init tasklets.
3607 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003608 tasklet_init(&host->finish_tasklet,
3609 sdhci_tasklet_finish, (unsigned long)host);
3610
Al Viroe4cad1b2006-10-10 22:47:07 +01003611 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003612 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3613 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003614
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003615 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303616
Shawn Guo2af502c2013-07-05 14:38:55 +08003617 sdhci_init(host, 0);
3618
Russell King781e9892014-04-25 12:55:46 +01003619 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3620 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003621 if (ret) {
3622 pr_err("%s: Failed to request IRQ %d: %d\n",
3623 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003624 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003625 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003626
Pierre Ossmand129bce2006-03-24 03:18:17 -08003627#ifdef CONFIG_MMC_DEBUG
3628 sdhci_dumpregs(host);
3629#endif
3630
Adrian Hunter061d17a2016-04-12 14:25:09 +03003631 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003632 if (ret) {
3633 pr_err("%s: Failed to register LED device: %d\n",
3634 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003635 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003636 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003637
Pierre Ossman5f25a662006-10-04 02:15:39 -07003638 mmiowb();
3639
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003640 ret = mmc_add_host(mmc);
3641 if (ret)
3642 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003643
Girish K Sa3c76eb2011-10-11 11:44:09 +05303644 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003645 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003646 (host->flags & SDHCI_USE_ADMA) ?
3647 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003648 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003649
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003650 sdhci_enable_card_detection(host);
3651
Pierre Ossmand129bce2006-03-24 03:18:17 -08003652 return 0;
3653
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003654unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003655 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003656unirq:
Russell King03231f92014-04-25 12:57:12 +01003657 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003658 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3659 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003660 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003661untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003662 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003663
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003664 if (!IS_ERR(mmc->supply.vqmmc))
3665 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003666
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003667 if (host->align_buffer)
3668 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3669 host->adma_table_sz, host->align_buffer,
3670 host->align_addr);
3671 host->adma_table = NULL;
3672 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003673
3674 return ret;
3675}
Adrian Hunter52f53362016-06-29 16:24:15 +03003676EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003677
Adrian Hunter52f53362016-06-29 16:24:15 +03003678int sdhci_add_host(struct sdhci_host *host)
3679{
3680 int ret;
3681
3682 ret = sdhci_setup_host(host);
3683 if (ret)
3684 return ret;
3685
3686 return __sdhci_add_host(host);
3687}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003688EXPORT_SYMBOL_GPL(sdhci_add_host);
3689
Pierre Ossman1e728592008-04-16 19:13:13 +02003690void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003691{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003692 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003693 unsigned long flags;
3694
3695 if (dead) {
3696 spin_lock_irqsave(&host->lock, flags);
3697
3698 host->flags |= SDHCI_DEVICE_DEAD;
3699
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003700 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303701 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003702 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003703 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003704 }
3705
3706 spin_unlock_irqrestore(&host->lock, flags);
3707 }
3708
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003709 sdhci_disable_card_detection(host);
3710
Markus Mayer4e743f12014-07-03 13:27:42 -07003711 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003712
Adrian Hunter061d17a2016-04-12 14:25:09 +03003713 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003714
Pierre Ossman1e728592008-04-16 19:13:13 +02003715 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003716 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003717
Russell Kingb537f942014-04-25 12:56:01 +01003718 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3719 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003720 free_irq(host->irq, host);
3721
3722 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003723 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003724
Pierre Ossmand129bce2006-03-24 03:18:17 -08003725 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003726
Tim Kryger3a48edc2014-06-13 10:13:56 -07003727 if (!IS_ERR(mmc->supply.vqmmc))
3728 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003729
Russell Kingedd63fc2016-01-26 13:39:50 +00003730 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003731 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3732 host->adma_table_sz, host->align_buffer,
3733 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003734
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003735 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003736 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003737}
3738
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003739EXPORT_SYMBOL_GPL(sdhci_remove_host);
3740
3741void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003742{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003743 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003744}
3745
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003746EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003747
3748/*****************************************************************************\
3749 * *
3750 * Driver init/exit *
3751 * *
3752\*****************************************************************************/
3753
3754static int __init sdhci_drv_init(void)
3755{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303756 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003757 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303758 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003759
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003760 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003761}
3762
3763static void __exit sdhci_drv_exit(void)
3764{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003765}
3766
3767module_init(sdhci_drv_init);
3768module_exit(sdhci_drv_exit);
3769
Pierre Ossmandf673b22006-06-30 02:22:31 -07003770module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003771module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003772
Pierre Ossman32710e82009-04-08 20:14:54 +02003773MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003774MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003775MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003776
Pierre Ossmandf673b22006-06-30 02:22:31 -07003777MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003778MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");