blob: e761fe2aa99eebaaa0d6cb104b0bf6aa075a8fac [file] [log] [blame]
Pierre Ossmand129bce2006-03-24 03:18:17 -08001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/sdhci.c - Secure Digital Host Controller Interface driver
Pierre Ossmand129bce2006-03-24 03:18:17 -08003 *
Pierre Ossmanb69c9052008-03-08 23:44:25 +01004 * Copyright (C) 2005-2008 Pierre Ossman, All Rights Reserved.
Pierre Ossmand129bce2006-03-24 03:18:17 -08005 *
6 * This program is free software; you can redistribute it and/or modify
Pierre Ossman643f7202006-09-30 23:27:52 -07007 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
Pierre Ossman84c46a52007-12-02 19:58:16 +010010 *
11 * Thanks to the following companies for their support:
12 *
13 * - JMicron (hardware and technical support)
Pierre Ossmand129bce2006-03-24 03:18:17 -080014 */
15
Pierre Ossmand129bce2006-03-24 03:18:17 -080016#include <linux/delay.h>
17#include <linux/highmem.h>
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +010018#include <linux/io.h>
Paul Gortmaker88b47672011-07-03 15:15:51 -040019#include <linux/module.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080020#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Ralf Baechle11763602007-10-23 20:42:11 +020022#include <linux/scatterlist.h>
Marek Szyprowski9bea3c82010-08-10 18:01:59 -070023#include <linux/regulator/consumer.h>
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030024#include <linux/pm_runtime.h>
Zach Brown92e0c442016-11-02 10:26:16 -050025#include <linux/of.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080026
Pierre Ossman2f730fe2008-03-17 10:29:38 +010027#include <linux/leds.h>
28
Aries Lee22113ef2010-12-15 08:14:24 +010029#include <linux/mmc/mmc.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080030#include <linux/mmc/host.h>
Aaron Lu473b0952012-07-03 17:27:49 +080031#include <linux/mmc/card.h>
Corneliu Doban85cc1c32015-02-09 16:06:29 -080032#include <linux/mmc/sdio.h>
Guennadi Liakhovetskibec9d4e2012-09-17 16:45:10 +080033#include <linux/mmc/slot-gpio.h>
Pierre Ossmand129bce2006-03-24 03:18:17 -080034
Pierre Ossmand129bce2006-03-24 03:18:17 -080035#include "sdhci.h"
36
37#define DRIVER_NAME "sdhci"
Pierre Ossmand129bce2006-03-24 03:18:17 -080038
Pierre Ossmand129bce2006-03-24 03:18:17 -080039#define DBG(f, x...) \
Russell Kingc6563172006-03-29 09:30:20 +010040 pr_debug(DRIVER_NAME " [%s()]: " f, __func__,## x)
Pierre Ossmand129bce2006-03-24 03:18:17 -080041
Arindam Nathb513ea22011-05-05 12:19:04 +053042#define MAX_TUNING_LOOP 40
43
Pierre Ossmandf673b22006-06-30 02:22:31 -070044static unsigned int debug_quirks = 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +030045static unsigned int debug_quirks2;
Pierre Ossman67435272006-06-30 02:22:31 -070046
Pierre Ossmand129bce2006-03-24 03:18:17 -080047static void sdhci_finish_data(struct sdhci_host *);
48
Kevin Liu52983382013-01-31 11:31:37 +080049static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable);
Pierre Ossmand129bce2006-03-24 03:18:17 -080050
51static void sdhci_dumpregs(struct sdhci_host *host)
52{
Chuanxiao Donga7c53672016-06-22 14:40:01 +030053 pr_err(DRIVER_NAME ": =========== REGISTER DUMP (%s)===========\n",
54 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -080055
Chuanxiao Donga7c53672016-06-22 14:40:01 +030056 pr_err(DRIVER_NAME ": Sys addr: 0x%08x | Version: 0x%08x\n",
57 sdhci_readl(host, SDHCI_DMA_ADDRESS),
58 sdhci_readw(host, SDHCI_HOST_VERSION));
59 pr_err(DRIVER_NAME ": Blk size: 0x%08x | Blk cnt: 0x%08x\n",
60 sdhci_readw(host, SDHCI_BLOCK_SIZE),
61 sdhci_readw(host, SDHCI_BLOCK_COUNT));
62 pr_err(DRIVER_NAME ": Argument: 0x%08x | Trn mode: 0x%08x\n",
63 sdhci_readl(host, SDHCI_ARGUMENT),
64 sdhci_readw(host, SDHCI_TRANSFER_MODE));
65 pr_err(DRIVER_NAME ": Present: 0x%08x | Host ctl: 0x%08x\n",
66 sdhci_readl(host, SDHCI_PRESENT_STATE),
67 sdhci_readb(host, SDHCI_HOST_CONTROL));
68 pr_err(DRIVER_NAME ": Power: 0x%08x | Blk gap: 0x%08x\n",
69 sdhci_readb(host, SDHCI_POWER_CONTROL),
70 sdhci_readb(host, SDHCI_BLOCK_GAP_CONTROL));
71 pr_err(DRIVER_NAME ": Wake-up: 0x%08x | Clock: 0x%08x\n",
72 sdhci_readb(host, SDHCI_WAKE_UP_CONTROL),
73 sdhci_readw(host, SDHCI_CLOCK_CONTROL));
74 pr_err(DRIVER_NAME ": Timeout: 0x%08x | Int stat: 0x%08x\n",
75 sdhci_readb(host, SDHCI_TIMEOUT_CONTROL),
76 sdhci_readl(host, SDHCI_INT_STATUS));
77 pr_err(DRIVER_NAME ": Int enab: 0x%08x | Sig enab: 0x%08x\n",
78 sdhci_readl(host, SDHCI_INT_ENABLE),
79 sdhci_readl(host, SDHCI_SIGNAL_ENABLE));
80 pr_err(DRIVER_NAME ": AC12 err: 0x%08x | Slot int: 0x%08x\n",
81 sdhci_readw(host, SDHCI_ACMD12_ERR),
82 sdhci_readw(host, SDHCI_SLOT_INT_STATUS));
83 pr_err(DRIVER_NAME ": Caps: 0x%08x | Caps_1: 0x%08x\n",
84 sdhci_readl(host, SDHCI_CAPABILITIES),
85 sdhci_readl(host, SDHCI_CAPABILITIES_1));
86 pr_err(DRIVER_NAME ": Cmd: 0x%08x | Max curr: 0x%08x\n",
87 sdhci_readw(host, SDHCI_COMMAND),
88 sdhci_readl(host, SDHCI_MAX_CURRENT));
89 pr_err(DRIVER_NAME ": Host ctl2: 0x%08x\n",
90 sdhci_readw(host, SDHCI_HOST_CONTROL2));
Pierre Ossmand129bce2006-03-24 03:18:17 -080091
Adrian Huntere57a5f62014-11-04 12:42:46 +020092 if (host->flags & SDHCI_USE_ADMA) {
93 if (host->flags & SDHCI_USE_64_BIT_DMA)
Chuanxiao Donga7c53672016-06-22 14:40:01 +030094 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x%08x\n",
95 readl(host->ioaddr + SDHCI_ADMA_ERROR),
96 readl(host->ioaddr + SDHCI_ADMA_ADDRESS_HI),
97 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +020098 else
Chuanxiao Donga7c53672016-06-22 14:40:01 +030099 pr_err(DRIVER_NAME ": ADMA Err: 0x%08x | ADMA Ptr: 0x%08x\n",
100 readl(host->ioaddr + SDHCI_ADMA_ERROR),
101 readl(host->ioaddr + SDHCI_ADMA_ADDRESS));
Adrian Huntere57a5f62014-11-04 12:42:46 +0200102 }
Ben Dooksbe3f4ae2009-06-08 23:33:52 +0100103
Chuanxiao Donga7c53672016-06-22 14:40:01 +0300104 pr_err(DRIVER_NAME ": ===========================================\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -0800105}
106
107/*****************************************************************************\
108 * *
109 * Low level functions *
110 * *
111\*****************************************************************************/
112
Adrian Hunter56a590d2016-06-29 16:24:32 +0300113static inline bool sdhci_data_line_cmd(struct mmc_command *cmd)
114{
115 return cmd->data || cmd->flags & MMC_RSP_BUSY;
116}
117
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300118static void sdhci_set_card_detection(struct sdhci_host *host, bool enable)
119{
Russell King5b4f1f62014-04-25 12:57:02 +0100120 u32 present;
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300121
Adrian Hunterc79396c2011-12-27 15:48:42 +0200122 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) ||
Jaehoon Chung860951c2016-06-21 10:13:26 +0900123 !mmc_card_is_removable(host->mmc))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +0300124 return;
125
Russell King5b4f1f62014-04-25 12:57:02 +0100126 if (enable) {
127 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
128 SDHCI_CARD_PRESENT;
Shawn Guod25928d2011-06-21 22:41:48 +0800129
Russell King5b4f1f62014-04-25 12:57:02 +0100130 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
131 SDHCI_INT_CARD_INSERT;
132 } else {
133 host->ier &= ~(SDHCI_INT_CARD_REMOVE | SDHCI_INT_CARD_INSERT);
134 }
Russell Kingb537f942014-04-25 12:56:01 +0100135
136 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
137 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +0300138}
139
140static void sdhci_enable_card_detection(struct sdhci_host *host)
141{
142 sdhci_set_card_detection(host, true);
143}
144
145static void sdhci_disable_card_detection(struct sdhci_host *host)
146{
147 sdhci_set_card_detection(host, false);
148}
149
Ulf Hansson02d0b682016-04-11 15:32:41 +0200150static void sdhci_runtime_pm_bus_on(struct sdhci_host *host)
151{
152 if (host->bus_on)
153 return;
154 host->bus_on = true;
155 pm_runtime_get_noresume(host->mmc->parent);
156}
157
158static void sdhci_runtime_pm_bus_off(struct sdhci_host *host)
159{
160 if (!host->bus_on)
161 return;
162 host->bus_on = false;
163 pm_runtime_put_noidle(host->mmc->parent);
164}
165
Russell King03231f92014-04-25 12:57:12 +0100166void sdhci_reset(struct sdhci_host *host, u8 mask)
Pierre Ossmand129bce2006-03-24 03:18:17 -0800167{
Pierre Ossmane16514d82006-06-30 02:22:24 -0700168 unsigned long timeout;
Philip Rakity393c1a32011-01-21 11:26:40 -0800169
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300170 sdhci_writeb(host, mask, SDHCI_SOFTWARE_RESET);
Pierre Ossmand129bce2006-03-24 03:18:17 -0800171
Adrian Hunterf0710a52013-05-06 12:17:32 +0300172 if (mask & SDHCI_RESET_ALL) {
Pierre Ossmand129bce2006-03-24 03:18:17 -0800173 host->clock = 0;
Adrian Hunterf0710a52013-05-06 12:17:32 +0300174 /* Reset-all turns off SD Bus Power */
175 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
176 sdhci_runtime_pm_bus_off(host);
177 }
Pierre Ossmand129bce2006-03-24 03:18:17 -0800178
Pierre Ossmane16514d82006-06-30 02:22:24 -0700179 /* Wait max 100 ms */
180 timeout = 100;
181
182 /* hw clears the bit when it's done */
Anton Vorontsov4e4141a2009-03-17 00:13:46 +0300183 while (sdhci_readb(host, SDHCI_SOFTWARE_RESET) & mask) {
Pierre Ossmane16514d82006-06-30 02:22:24 -0700184 if (timeout == 0) {
Girish K Sa3c76eb2011-10-11 11:44:09 +0530185 pr_err("%s: Reset 0x%x never completed.\n",
Pierre Ossmane16514d82006-06-30 02:22:24 -0700186 mmc_hostname(host->mmc), (int)mask);
187 sdhci_dumpregs(host);
188 return;
189 }
190 timeout--;
191 mdelay(1);
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{
Ludovic Desrochesfb9ee042016-04-07 11:13:08 +02001349 unsigned long timeout;
1350
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 */
1355 timeout = 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)) {
1358 if (timeout == 0) {
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 }
Pierre Ossman7cb2c762006-06-30 02:22:23 -07001364 timeout--;
1365 mdelay(1);
1366 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001367
1368 clk |= SDHCI_CLOCK_CARD_EN;
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001369 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001370}
Ritesh Harjanifec79672016-11-21 12:07:19 +05301371EXPORT_SYMBOL_GPL(sdhci_enable_clk);
1372
1373void sdhci_set_clock(struct sdhci_host *host, unsigned int clock)
1374{
1375 u16 clk;
1376
1377 host->mmc->actual_clock = 0;
1378
1379 sdhci_writew(host, 0, SDHCI_CLOCK_CONTROL);
1380
1381 if (clock == 0)
1382 return;
1383
1384 clk = sdhci_calc_clk(host, clock, &host->mmc->actual_clock);
1385 sdhci_enable_clk(host, clk);
1386}
Russell King17710592014-04-25 12:58:55 +01001387EXPORT_SYMBOL_GPL(sdhci_set_clock);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001388
Adrian Hunter1dceb042016-03-29 12:45:43 +03001389static void sdhci_set_power_reg(struct sdhci_host *host, unsigned char mode,
1390 unsigned short vdd)
Pierre Ossman146ad662006-06-30 02:22:23 -07001391{
Tim Kryger3a48edc2014-06-13 10:13:56 -07001392 struct mmc_host *mmc = host->mmc;
Adrian Hunter1dceb042016-03-29 12:45:43 +03001393
1394 spin_unlock_irq(&host->lock);
1395 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, vdd);
1396 spin_lock_irq(&host->lock);
1397
1398 if (mode != MMC_POWER_OFF)
1399 sdhci_writeb(host, SDHCI_POWER_ON, SDHCI_POWER_CONTROL);
1400 else
1401 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
1402}
1403
Adrian Hunter606d3132016-10-05 12:11:22 +03001404void sdhci_set_power_noreg(struct sdhci_host *host, unsigned char mode,
1405 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001406{
Giuseppe Cavallaro83642482010-09-28 10:41:28 +02001407 u8 pwr = 0;
Pierre Ossman146ad662006-06-30 02:22:23 -07001408
Russell King24fbb3c2014-04-25 13:00:06 +01001409 if (mode != MMC_POWER_OFF) {
1410 switch (1 << vdd) {
Pierre Ossmanae628902009-05-03 20:45:03 +02001411 case MMC_VDD_165_195:
1412 pwr = SDHCI_POWER_180;
1413 break;
1414 case MMC_VDD_29_30:
1415 case MMC_VDD_30_31:
1416 pwr = SDHCI_POWER_300;
1417 break;
1418 case MMC_VDD_32_33:
1419 case MMC_VDD_33_34:
1420 pwr = SDHCI_POWER_330;
1421 break;
1422 default:
Adrian Hunter9d5de932015-11-26 14:00:46 +02001423 WARN(1, "%s: Invalid vdd %#x\n",
1424 mmc_hostname(host->mmc), vdd);
1425 break;
Pierre Ossmanae628902009-05-03 20:45:03 +02001426 }
1427 }
1428
1429 if (host->pwr == pwr)
Russell Kinge921a8b2014-04-25 13:00:01 +01001430 return;
Pierre Ossman146ad662006-06-30 02:22:23 -07001431
Pierre Ossmanae628902009-05-03 20:45:03 +02001432 host->pwr = pwr;
1433
1434 if (pwr == 0) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001435 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Adrian Hunterf0710a52013-05-06 12:17:32 +03001436 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1437 sdhci_runtime_pm_bus_off(host);
Russell Kinge921a8b2014-04-25 13:00:01 +01001438 } else {
1439 /*
1440 * Spec says that we should clear the power reg before setting
1441 * a new value. Some controllers don't seem to like this though.
1442 */
1443 if (!(host->quirks & SDHCI_QUIRK_SINGLE_POWER_WRITE))
1444 sdhci_writeb(host, 0, SDHCI_POWER_CONTROL);
Darren Salt9e9dc5f2007-01-27 15:32:31 +01001445
Russell Kinge921a8b2014-04-25 13:00:01 +01001446 /*
1447 * At least the Marvell CaFe chip gets confused if we set the
1448 * voltage and set turn on power at the same time, so set the
1449 * voltage first.
1450 */
1451 if (host->quirks & SDHCI_QUIRK_NO_SIMULT_VDD_AND_POWER)
1452 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
Pierre Ossman146ad662006-06-30 02:22:23 -07001453
Russell Kinge921a8b2014-04-25 13:00:01 +01001454 pwr |= SDHCI_POWER_ON;
1455
Pierre Ossmanae628902009-05-03 20:45:03 +02001456 sdhci_writeb(host, pwr, SDHCI_POWER_CONTROL);
1457
Russell Kinge921a8b2014-04-25 13:00:01 +01001458 if (host->quirks2 & SDHCI_QUIRK2_CARD_ON_NEEDS_BUS_ON)
1459 sdhci_runtime_pm_bus_on(host);
Andres Salomone08c1692008-07-04 10:00:03 -07001460
Russell Kinge921a8b2014-04-25 13:00:01 +01001461 /*
1462 * Some controllers need an extra 10ms delay of 10ms before
1463 * they can apply clock after applying power
1464 */
1465 if (host->quirks & SDHCI_QUIRK_DELAY_AFTER_POWER)
1466 mdelay(10);
1467 }
Adrian Hunter1dceb042016-03-29 12:45:43 +03001468}
Adrian Hunter606d3132016-10-05 12:11:22 +03001469EXPORT_SYMBOL_GPL(sdhci_set_power_noreg);
Jisheng Zhang918f4cb2015-12-11 21:36:29 +08001470
Adrian Hunter606d3132016-10-05 12:11:22 +03001471void sdhci_set_power(struct sdhci_host *host, unsigned char mode,
1472 unsigned short vdd)
Adrian Hunter1dceb042016-03-29 12:45:43 +03001473{
Adrian Hunter606d3132016-10-05 12:11:22 +03001474 if (IS_ERR(host->mmc->supply.vmmc))
1475 sdhci_set_power_noreg(host, mode, vdd);
Adrian Hunter1dceb042016-03-29 12:45:43 +03001476 else
Adrian Hunter606d3132016-10-05 12:11:22 +03001477 sdhci_set_power_reg(host, mode, vdd);
Pierre Ossman146ad662006-06-30 02:22:23 -07001478}
Adrian Hunter606d3132016-10-05 12:11:22 +03001479EXPORT_SYMBOL_GPL(sdhci_set_power);
Pierre Ossman146ad662006-06-30 02:22:23 -07001480
Pierre Ossmand129bce2006-03-24 03:18:17 -08001481/*****************************************************************************\
1482 * *
1483 * MMC callbacks *
1484 * *
1485\*****************************************************************************/
1486
1487static void sdhci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1488{
1489 struct sdhci_host *host;
Shawn Guo505a8682012-12-11 15:23:42 +08001490 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001491 unsigned long flags;
1492
1493 host = mmc_priv(mmc);
1494
Scott Branden04e079cf2015-03-10 11:35:10 -07001495 /* Firstly check card presence */
Adrian Hunter8d28b7a2016-02-09 16:12:36 +02001496 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01001497
Pierre Ossmand129bce2006-03-24 03:18:17 -08001498 spin_lock_irqsave(&host->lock, flags);
1499
Adrian Hunter061d17a2016-04-12 14:25:09 +03001500 sdhci_led_activate(host);
Andrei Warkentine89d4562011-05-23 15:06:37 -05001501
1502 /*
1503 * Ensure we don't send the STOP for non-SET_BLOCK_COUNTED
1504 * requests if Auto-CMD12 is enabled.
1505 */
Adrian Hunter0293d502016-06-29 16:24:35 +03001506 if (sdhci_auto_cmd12(host, mrq)) {
Jerry Huangc4512f72010-08-10 18:01:59 -07001507 if (mrq->stop) {
1508 mrq->data->stop = NULL;
1509 mrq->stop = NULL;
1510 }
1511 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001512
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03001513 if (!present || host->flags & SDHCI_DEVICE_DEAD) {
Adrian Huntera4c73ab2016-06-29 16:24:25 +03001514 mrq->cmd->error = -ENOMEDIUM;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03001515 sdhci_finish_mrq(host, mrq);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301516 } else {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05001517 if (mrq->sbc && !(host->flags & SDHCI_AUTO_CMD23))
Andrei Warkentine89d4562011-05-23 15:06:37 -05001518 sdhci_send_command(host, mrq->sbc);
1519 else
1520 sdhci_send_command(host, mrq->cmd);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05301521 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001522
Pierre Ossman5f25a662006-10-04 02:15:39 -07001523 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001524 spin_unlock_irqrestore(&host->lock, flags);
1525}
1526
Russell King2317f562014-04-25 12:57:07 +01001527void sdhci_set_bus_width(struct sdhci_host *host, int width)
1528{
1529 u8 ctrl;
1530
1531 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
1532 if (width == MMC_BUS_WIDTH_8) {
1533 ctrl &= ~SDHCI_CTRL_4BITBUS;
1534 if (host->version >= SDHCI_SPEC_300)
1535 ctrl |= SDHCI_CTRL_8BITBUS;
1536 } else {
1537 if (host->version >= SDHCI_SPEC_300)
1538 ctrl &= ~SDHCI_CTRL_8BITBUS;
1539 if (width == MMC_BUS_WIDTH_4)
1540 ctrl |= SDHCI_CTRL_4BITBUS;
1541 else
1542 ctrl &= ~SDHCI_CTRL_4BITBUS;
1543 }
1544 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1545}
1546EXPORT_SYMBOL_GPL(sdhci_set_bus_width);
1547
Russell King96d7b782014-04-25 12:59:26 +01001548void sdhci_set_uhs_signaling(struct sdhci_host *host, unsigned timing)
1549{
1550 u16 ctrl_2;
1551
1552 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1553 /* Select Bus Speed Mode for host */
1554 ctrl_2 &= ~SDHCI_CTRL_UHS_MASK;
1555 if ((timing == MMC_TIMING_MMC_HS200) ||
1556 (timing == MMC_TIMING_UHS_SDR104))
1557 ctrl_2 |= SDHCI_CTRL_UHS_SDR104;
1558 else if (timing == MMC_TIMING_UHS_SDR12)
1559 ctrl_2 |= SDHCI_CTRL_UHS_SDR12;
1560 else if (timing == MMC_TIMING_UHS_SDR25)
1561 ctrl_2 |= SDHCI_CTRL_UHS_SDR25;
1562 else if (timing == MMC_TIMING_UHS_SDR50)
1563 ctrl_2 |= SDHCI_CTRL_UHS_SDR50;
1564 else if ((timing == MMC_TIMING_UHS_DDR50) ||
1565 (timing == MMC_TIMING_MMC_DDR52))
1566 ctrl_2 |= SDHCI_CTRL_UHS_DDR50;
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001567 else if (timing == MMC_TIMING_MMC_HS400)
1568 ctrl_2 |= SDHCI_CTRL_HS400; /* Non-standard */
Russell King96d7b782014-04-25 12:59:26 +01001569 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
1570}
1571EXPORT_SYMBOL_GPL(sdhci_set_uhs_signaling);
1572
Dong Aishengded97e02016-04-16 01:29:25 +08001573static void sdhci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001574{
Dong Aishengded97e02016-04-16 01:29:25 +08001575 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001576 unsigned long flags;
1577 u8 ctrl;
1578
Pierre Ossmand129bce2006-03-24 03:18:17 -08001579 spin_lock_irqsave(&host->lock, flags);
1580
Adrian Hunterceb61432011-12-27 15:48:41 +02001581 if (host->flags & SDHCI_DEVICE_DEAD) {
1582 spin_unlock_irqrestore(&host->lock, flags);
Tim Kryger3a48edc2014-06-13 10:13:56 -07001583 if (!IS_ERR(mmc->supply.vmmc) &&
1584 ios->power_mode == MMC_POWER_OFF)
Markus Mayer4e743f12014-07-03 13:27:42 -07001585 mmc_regulator_set_ocr(mmc, mmc->supply.vmmc, 0);
Adrian Hunterceb61432011-12-27 15:48:41 +02001586 return;
1587 }
Pierre Ossman1e728592008-04-16 19:13:13 +02001588
Pierre Ossmand129bce2006-03-24 03:18:17 -08001589 /*
1590 * Reset the chip on each power off.
1591 * Should clear out any weird states.
1592 */
1593 if (ios->power_mode == MMC_POWER_OFF) {
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001594 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Anton Vorontsov7260cf52009-03-17 00:13:48 +03001595 sdhci_reinit(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001596 }
1597
Kevin Liu52983382013-01-31 11:31:37 +08001598 if (host->version >= SDHCI_SPEC_300 &&
Dong Aisheng372c4632013-10-18 19:48:50 +08001599 (ios->power_mode == MMC_POWER_UP) &&
1600 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN))
Kevin Liu52983382013-01-31 11:31:37 +08001601 sdhci_enable_preset_value(host, false);
1602
Russell King373073e2014-04-25 12:58:45 +01001603 if (!ios->clock || ios->clock != host->clock) {
Russell King17710592014-04-25 12:58:55 +01001604 host->ops->set_clock(host, ios->clock);
Russell King373073e2014-04-25 12:58:45 +01001605 host->clock = ios->clock;
Aisheng Dong03d6f5f2014-08-27 15:26:32 +08001606
1607 if (host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK &&
1608 host->clock) {
1609 host->timeout_clk = host->mmc->actual_clock ?
1610 host->mmc->actual_clock / 1000 :
1611 host->clock / 1000;
1612 host->mmc->max_busy_timeout =
1613 host->ops->get_max_timeout_count ?
1614 host->ops->get_max_timeout_count(host) :
1615 1 << 27;
1616 host->mmc->max_busy_timeout /= host->timeout_clk;
1617 }
Russell King373073e2014-04-25 12:58:45 +01001618 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08001619
Adrian Hunter606d3132016-10-05 12:11:22 +03001620 if (host->ops->set_power)
1621 host->ops->set_power(host, ios->power_mode, ios->vdd);
1622 else
1623 sdhci_set_power(host, ios->power_mode, ios->vdd);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001624
Philip Rakity643a81f2010-09-23 08:24:32 -07001625 if (host->ops->platform_send_init_74_clocks)
1626 host->ops->platform_send_init_74_clocks(host, ios->power_mode);
1627
Russell King2317f562014-04-25 12:57:07 +01001628 host->ops->set_bus_width(host, ios->bus_width);
Philip Rakity15ec4462010-11-19 16:48:39 -05001629
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03001630 ctrl = sdhci_readb(host, SDHCI_HOST_CONTROL);
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001631
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001632 if ((ios->timing == MMC_TIMING_SD_HS ||
Jaehoon Chung273c5412016-10-07 14:08:43 +09001633 ios->timing == MMC_TIMING_MMC_HS ||
1634 ios->timing == MMC_TIMING_MMC_HS400 ||
1635 ios->timing == MMC_TIMING_MMC_HS200 ||
1636 ios->timing == MMC_TIMING_MMC_DDR52 ||
1637 ios->timing == MMC_TIMING_UHS_SDR50 ||
1638 ios->timing == MMC_TIMING_UHS_SDR104 ||
1639 ios->timing == MMC_TIMING_UHS_DDR50 ||
1640 ios->timing == MMC_TIMING_UHS_SDR25)
Philip Rakity3ab9c8d2010-10-06 11:57:23 -07001641 && !(host->quirks & SDHCI_QUIRK_NO_HISPD_BIT))
Pierre Ossmancd9277c2007-02-18 12:07:47 +01001642 ctrl |= SDHCI_CTRL_HISPD;
1643 else
1644 ctrl &= ~SDHCI_CTRL_HISPD;
1645
Arindam Nathd6d50a12011-05-05 12:18:59 +05301646 if (host->version >= SDHCI_SPEC_300) {
Arindam Nath49c468f2011-05-05 12:19:01 +05301647 u16 clk, ctrl_2;
Arindam Nath49c468f2011-05-05 12:19:01 +05301648
Russell Kingda91a8f2014-04-25 13:00:12 +01001649 if (!host->preset_enabled) {
Arindam Nath758535c2011-05-05 12:19:00 +05301650 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301651 /*
1652 * We only need to set Driver Strength if the
1653 * preset value enable is not set.
1654 */
Russell Kingda91a8f2014-04-25 13:00:12 +01001655 ctrl_2 = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301656 ctrl_2 &= ~SDHCI_CTRL_DRV_TYPE_MASK;
1657 if (ios->drv_type == MMC_SET_DRIVER_TYPE_A)
1658 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_A;
Petri Gynther43e943a2015-05-20 14:35:00 -07001659 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_B)
1660 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
Arindam Nathd6d50a12011-05-05 12:18:59 +05301661 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_C)
1662 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_C;
Petri Gynther43e943a2015-05-20 14:35:00 -07001663 else if (ios->drv_type == MMC_SET_DRIVER_TYPE_D)
1664 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_D;
1665 else {
Marek Vasut2e4456f2015-11-18 10:47:02 +01001666 pr_warn("%s: invalid driver type, default to driver type B\n",
1667 mmc_hostname(mmc));
Petri Gynther43e943a2015-05-20 14:35:00 -07001668 ctrl_2 |= SDHCI_CTRL_DRV_TYPE_B;
1669 }
Arindam Nathd6d50a12011-05-05 12:18:59 +05301670
1671 sdhci_writew(host, ctrl_2, SDHCI_HOST_CONTROL2);
Arindam Nath758535c2011-05-05 12:19:00 +05301672 } else {
1673 /*
1674 * According to SDHC Spec v3.00, if the Preset Value
1675 * Enable in the Host Control 2 register is set, we
1676 * need to reset SD Clock Enable before changing High
1677 * Speed Enable to avoid generating clock gliches.
1678 */
Arindam Nath758535c2011-05-05 12:19:00 +05301679
1680 /* Reset SD Clock Enable */
1681 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1682 clk &= ~SDHCI_CLOCK_CARD_EN;
1683 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1684
1685 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
1686
1687 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001688 host->ops->set_clock(host, host->clock);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301689 }
Arindam Nath49c468f2011-05-05 12:19:01 +05301690
Arindam Nath49c468f2011-05-05 12:19:01 +05301691 /* Reset SD Clock Enable */
1692 clk = sdhci_readw(host, SDHCI_CLOCK_CONTROL);
1693 clk &= ~SDHCI_CLOCK_CARD_EN;
1694 sdhci_writew(host, clk, SDHCI_CLOCK_CONTROL);
1695
Russell King96d7b782014-04-25 12:59:26 +01001696 host->ops->set_uhs_signaling(host, ios->timing);
Russell Kingd975f122014-04-25 12:59:31 +01001697 host->timing = ios->timing;
Arindam Nath49c468f2011-05-05 12:19:01 +05301698
Kevin Liu52983382013-01-31 11:31:37 +08001699 if (!(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN) &&
1700 ((ios->timing == MMC_TIMING_UHS_SDR12) ||
1701 (ios->timing == MMC_TIMING_UHS_SDR25) ||
1702 (ios->timing == MMC_TIMING_UHS_SDR50) ||
1703 (ios->timing == MMC_TIMING_UHS_SDR104) ||
Jisheng Zhang0dafa602015-08-18 16:21:39 +08001704 (ios->timing == MMC_TIMING_UHS_DDR50) ||
1705 (ios->timing == MMC_TIMING_MMC_DDR52))) {
Kevin Liu52983382013-01-31 11:31:37 +08001706 u16 preset;
1707
1708 sdhci_enable_preset_value(host, true);
1709 preset = sdhci_get_preset_value(host);
1710 ios->drv_type = (preset & SDHCI_PRESET_DRV_MASK)
1711 >> SDHCI_PRESET_DRV_SHIFT;
1712 }
1713
Arindam Nath49c468f2011-05-05 12:19:01 +05301714 /* Re-enable SD Clock */
Russell King17710592014-04-25 12:58:55 +01001715 host->ops->set_clock(host, host->clock);
Arindam Nath758535c2011-05-05 12:19:00 +05301716 } else
1717 sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
Arindam Nathd6d50a12011-05-05 12:18:59 +05301718
Leandro Dorileob8352262007-07-25 23:47:04 +02001719 /*
1720 * Some (ENE) controllers go apeshit on some ios operation,
1721 * signalling timeout and CRC errors even on CMD0. Resetting
1722 * it on each ios seems to solve the problem.
1723 */
Mohammad Jamalc63705e2015-01-13 20:47:24 +05301724 if (host->quirks & SDHCI_QUIRK_RESET_CMD_DATA_ON_IOS)
Russell King03231f92014-04-25 12:57:12 +01001725 sdhci_do_reset(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA);
Leandro Dorileob8352262007-07-25 23:47:04 +02001726
Pierre Ossman5f25a662006-10-04 02:15:39 -07001727 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08001728 spin_unlock_irqrestore(&host->lock, flags);
1729}
1730
Dong Aishengded97e02016-04-16 01:29:25 +08001731static int sdhci_get_cd(struct mmc_host *mmc)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001732{
1733 struct sdhci_host *host = mmc_priv(mmc);
Dong Aishengded97e02016-04-16 01:29:25 +08001734 int gpio_cd = mmc_gpio_get_cd(mmc);
Kevin Liu94144a42013-02-28 17:35:53 +08001735
1736 if (host->flags & SDHCI_DEVICE_DEAD)
1737 return 0;
1738
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001739 /* If nonremovable, assume that the card is always present. */
Jaehoon Chung860951c2016-06-21 10:13:26 +09001740 if (!mmc_card_is_removable(host->mmc))
Kevin Liu94144a42013-02-28 17:35:53 +08001741 return 1;
1742
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001743 /*
1744 * Try slot gpio detect, if defined it take precedence
1745 * over build in controller functionality
1746 */
Arnd Bergmann287980e2016-05-27 23:23:25 +02001747 if (gpio_cd >= 0)
Kevin Liu94144a42013-02-28 17:35:53 +08001748 return !!gpio_cd;
1749
Ivan T. Ivanov88af5652015-07-06 15:16:19 +03001750 /* If polling, assume that the card is always present. */
1751 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION)
1752 return 1;
1753
Kevin Liu94144a42013-02-28 17:35:53 +08001754 /* Host native card detect */
1755 return !!(sdhci_readl(host, SDHCI_PRESENT_STATE) & SDHCI_CARD_PRESENT);
1756}
1757
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001758static int sdhci_check_ro(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08001759{
Pierre Ossmand129bce2006-03-24 03:18:17 -08001760 unsigned long flags;
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001761 int is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001762
Pierre Ossmand129bce2006-03-24 03:18:17 -08001763 spin_lock_irqsave(&host->lock, flags);
1764
Pierre Ossman1e728592008-04-16 19:13:13 +02001765 if (host->flags & SDHCI_DEVICE_DEAD)
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001766 is_readonly = 0;
1767 else if (host->ops->get_ro)
1768 is_readonly = host->ops->get_ro(host);
Pierre Ossman1e728592008-04-16 19:13:13 +02001769 else
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001770 is_readonly = !(sdhci_readl(host, SDHCI_PRESENT_STATE)
1771 & SDHCI_WRITE_PROTECT);
Pierre Ossmand129bce2006-03-24 03:18:17 -08001772
1773 spin_unlock_irqrestore(&host->lock, flags);
1774
Wolfram Sang2dfb5792010-10-15 12:21:01 +02001775 /* This quirk needs to be replaced by a callback-function later */
1776 return host->quirks & SDHCI_QUIRK_INVERTED_WRITE_PROTECT ?
1777 !is_readonly : is_readonly;
Pierre Ossmand129bce2006-03-24 03:18:17 -08001778}
1779
Takashi Iwai82b0e232011-04-21 20:26:38 +02001780#define SAMPLE_COUNT 5
1781
Dong Aishengded97e02016-04-16 01:29:25 +08001782static int sdhci_get_ro(struct mmc_host *mmc)
Takashi Iwai82b0e232011-04-21 20:26:38 +02001783{
Dong Aishengded97e02016-04-16 01:29:25 +08001784 struct sdhci_host *host = mmc_priv(mmc);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001785 int i, ro_count;
1786
Takashi Iwai82b0e232011-04-21 20:26:38 +02001787 if (!(host->quirks & SDHCI_QUIRK_UNSTABLE_RO_DETECT))
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001788 return sdhci_check_ro(host);
Takashi Iwai82b0e232011-04-21 20:26:38 +02001789
1790 ro_count = 0;
1791 for (i = 0; i < SAMPLE_COUNT; i++) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001792 if (sdhci_check_ro(host)) {
Takashi Iwai82b0e232011-04-21 20:26:38 +02001793 if (++ro_count > SAMPLE_COUNT / 2)
1794 return 1;
1795 }
1796 msleep(30);
1797 }
1798 return 0;
1799}
1800
Adrian Hunter20758b62011-08-29 16:42:12 +03001801static void sdhci_hw_reset(struct mmc_host *mmc)
1802{
1803 struct sdhci_host *host = mmc_priv(mmc);
1804
1805 if (host->ops && host->ops->hw_reset)
1806 host->ops->hw_reset(host);
1807}
1808
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001809static void sdhci_enable_sdio_irq_nolock(struct sdhci_host *host, int enable)
1810{
Russell Kingbe138552014-04-25 12:55:56 +01001811 if (!(host->flags & SDHCI_DEVICE_DEAD)) {
Russell Kingef104332014-04-25 12:55:41 +01001812 if (enable)
Russell Kingb537f942014-04-25 12:56:01 +01001813 host->ier |= SDHCI_INT_CARD_INT;
Russell Kingef104332014-04-25 12:55:41 +01001814 else
Russell Kingb537f942014-04-25 12:56:01 +01001815 host->ier &= ~SDHCI_INT_CARD_INT;
1816
1817 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
1818 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell Kingef104332014-04-25 12:55:41 +01001819 mmiowb();
1820 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001821}
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001822
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001823static void sdhci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1824{
1825 struct sdhci_host *host = mmc_priv(mmc);
1826 unsigned long flags;
1827
1828 spin_lock_irqsave(&host->lock, flags);
Russell Kingef104332014-04-25 12:55:41 +01001829 if (enable)
1830 host->flags |= SDHCI_SDIO_IRQ_ENABLED;
1831 else
1832 host->flags &= ~SDHCI_SDIO_IRQ_ENABLED;
1833
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03001834 sdhci_enable_sdio_irq_nolock(host, enable);
Pierre Ossmanf75979b2007-09-04 07:59:18 +02001835 spin_unlock_irqrestore(&host->lock, flags);
1836}
1837
Dong Aishengded97e02016-04-16 01:29:25 +08001838static int sdhci_start_signal_voltage_switch(struct mmc_host *mmc,
1839 struct mmc_ios *ios)
Philip Rakity6231f3d2012-07-23 15:56:23 -07001840{
Dong Aishengded97e02016-04-16 01:29:25 +08001841 struct sdhci_host *host = mmc_priv(mmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07001842 u16 ctrl;
Kevin Liu20b92a32012-12-17 19:29:26 +08001843 int ret;
Philip Rakity6231f3d2012-07-23 15:56:23 -07001844
1845 /*
1846 * Signal Voltage Switching is only applicable for Host Controllers
1847 * v3.00 and above.
1848 */
1849 if (host->version < SDHCI_SPEC_300)
1850 return 0;
1851
Philip Rakity6231f3d2012-07-23 15:56:23 -07001852 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Kevin Liu20b92a32012-12-17 19:29:26 +08001853
Fabio Estevam21f59982013-02-14 10:35:03 -02001854 switch (ios->signal_voltage) {
Kevin Liu20b92a32012-12-17 19:29:26 +08001855 case MMC_SIGNAL_VOLTAGE_330:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001856 if (!(host->flags & SDHCI_SIGNALING_330))
1857 return -EINVAL;
Kevin Liu20b92a32012-12-17 19:29:26 +08001858 /* Set 1.8V Signal Enable in the Host Control2 register to 0 */
1859 ctrl &= ~SDHCI_CTRL_VDD_180;
1860 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1861
Tim Kryger3a48edc2014-06-13 10:13:56 -07001862 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001863 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001864 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001865 pr_warn("%s: Switching to 3.3V signalling voltage failed\n",
1866 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001867 return -EIO;
1868 }
1869 }
1870 /* Wait for 5ms */
1871 usleep_range(5000, 5500);
1872
1873 /* 3.3V regulator output should be stable within 5 ms */
1874 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1875 if (!(ctrl & SDHCI_CTRL_VDD_180))
1876 return 0;
1877
Joe Perches66061102014-09-12 14:56:56 -07001878 pr_warn("%s: 3.3V regulator output did not became stable\n",
1879 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001880
1881 return -EAGAIN;
1882 case MMC_SIGNAL_VOLTAGE_180:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001883 if (!(host->flags & SDHCI_SIGNALING_180))
1884 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001885 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001886 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001887 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001888 pr_warn("%s: Switching to 1.8V signalling voltage failed\n",
1889 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001890 return -EIO;
1891 }
1892 }
1893
1894 /*
1895 * Enable 1.8V Signal Enable in the Host Control2
1896 * register
1897 */
1898 ctrl |= SDHCI_CTRL_VDD_180;
1899 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
1900
Vincent Yang9d967a62015-01-20 16:05:15 +08001901 /* Some controller need to do more when switching */
1902 if (host->ops->voltage_switch)
1903 host->ops->voltage_switch(host);
1904
Kevin Liu20b92a32012-12-17 19:29:26 +08001905 /* 1.8V regulator output should be stable within 5 ms */
1906 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
1907 if (ctrl & SDHCI_CTRL_VDD_180)
1908 return 0;
1909
Joe Perches66061102014-09-12 14:56:56 -07001910 pr_warn("%s: 1.8V regulator output did not became stable\n",
1911 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001912
1913 return -EAGAIN;
1914 case MMC_SIGNAL_VOLTAGE_120:
Adrian Hunter8cb851a2016-06-29 16:24:16 +03001915 if (!(host->flags & SDHCI_SIGNALING_120))
1916 return -EINVAL;
Tim Kryger3a48edc2014-06-13 10:13:56 -07001917 if (!IS_ERR(mmc->supply.vqmmc)) {
Dong Aisheng761daa32016-07-12 15:46:10 +08001918 ret = mmc_regulator_set_vqmmc(mmc, ios);
Kevin Liu20b92a32012-12-17 19:29:26 +08001919 if (ret) {
Joe Perches66061102014-09-12 14:56:56 -07001920 pr_warn("%s: Switching to 1.2V signalling voltage failed\n",
1921 mmc_hostname(mmc));
Kevin Liu20b92a32012-12-17 19:29:26 +08001922 return -EIO;
1923 }
1924 }
1925 return 0;
1926 default:
Arindam Nathf2119df2011-05-05 12:18:57 +05301927 /* No signal voltage switch required */
1928 return 0;
Kevin Liu20b92a32012-12-17 19:29:26 +08001929 }
Arindam Nathf2119df2011-05-05 12:18:57 +05301930}
1931
Kevin Liu20b92a32012-12-17 19:29:26 +08001932static int sdhci_card_busy(struct mmc_host *mmc)
1933{
1934 struct sdhci_host *host = mmc_priv(mmc);
1935 u32 present_state;
1936
Adrian Huntere613cc42016-06-23 14:00:58 +03001937 /* Check whether DAT[0] is 0 */
Kevin Liu20b92a32012-12-17 19:29:26 +08001938 present_state = sdhci_readl(host, SDHCI_PRESENT_STATE);
Kevin Liu20b92a32012-12-17 19:29:26 +08001939
Adrian Huntere613cc42016-06-23 14:00:58 +03001940 return !(present_state & SDHCI_DATA_0_LVL_MASK);
Kevin Liu20b92a32012-12-17 19:29:26 +08001941}
1942
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001943static int sdhci_prepare_hs400_tuning(struct mmc_host *mmc, struct mmc_ios *ios)
1944{
1945 struct sdhci_host *host = mmc_priv(mmc);
1946 unsigned long flags;
1947
1948 spin_lock_irqsave(&host->lock, flags);
1949 host->flags |= SDHCI_HS400_TUNING;
1950 spin_unlock_irqrestore(&host->lock, flags);
1951
1952 return 0;
1953}
1954
Girish K S069c9f12012-01-06 09:56:39 +05301955static int sdhci_execute_tuning(struct mmc_host *mmc, u32 opcode)
Arindam Nathb513ea22011-05-05 12:19:04 +05301956{
Russell King4b6f37d2014-04-25 12:59:36 +01001957 struct sdhci_host *host = mmc_priv(mmc);
Arindam Nathb513ea22011-05-05 12:19:04 +05301958 u16 ctrl;
Arindam Nathb513ea22011-05-05 12:19:04 +05301959 int tuning_loop_counter = MAX_TUNING_LOOP;
Arindam Nathb513ea22011-05-05 12:19:04 +05301960 int err = 0;
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001961 unsigned long flags;
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001962 unsigned int tuning_count = 0;
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001963 bool hs400_tuning;
Arindam Nathb513ea22011-05-05 12:19:04 +05301964
Aisheng Dong2b35bd82013-12-23 19:13:04 +08001965 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05301966
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001967 hs400_tuning = host->flags & SDHCI_HS400_TUNING;
1968 host->flags &= ~SDHCI_HS400_TUNING;
1969
Adrian Hunter38e40bf2014-12-05 19:25:30 +02001970 if (host->tuning_mode == SDHCI_TUNING_MODE_1)
1971 tuning_count = host->tuning_count;
1972
Arindam Nathb513ea22011-05-05 12:19:04 +05301973 /*
Weijun Yang9faac7b2015-10-04 12:04:12 +00001974 * The Host Controller needs tuning in case of SDR104 and DDR50
1975 * mode, and for SDR50 mode when Use Tuning for SDR50 is set in
1976 * the Capabilities register.
Girish K S069c9f12012-01-06 09:56:39 +05301977 * If the Host Controller supports the HS200 mode then the
1978 * tuning function has to be executed.
Arindam Nathb513ea22011-05-05 12:19:04 +05301979 */
Russell King4b6f37d2014-04-25 12:59:36 +01001980 switch (host->timing) {
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001981 /* HS400 tuning is done in HS200 mode */
Adrian Huntere9fb05d2014-11-06 15:19:06 +02001982 case MMC_TIMING_MMC_HS400:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001983 err = -EINVAL;
1984 goto out_unlock;
1985
Russell King4b6f37d2014-04-25 12:59:36 +01001986 case MMC_TIMING_MMC_HS200:
Adrian Hunterb5540ce2014-12-05 19:25:31 +02001987 /*
1988 * Periodic re-tuning for HS400 is not expected to be needed, so
1989 * disable it here.
1990 */
1991 if (hs400_tuning)
1992 tuning_count = 0;
1993 break;
1994
Russell King4b6f37d2014-04-25 12:59:36 +01001995 case MMC_TIMING_UHS_SDR104:
Weijun Yang9faac7b2015-10-04 12:04:12 +00001996 case MMC_TIMING_UHS_DDR50:
Russell King4b6f37d2014-04-25 12:59:36 +01001997 break;
Girish K S069c9f12012-01-06 09:56:39 +05301998
Russell King4b6f37d2014-04-25 12:59:36 +01001999 case MMC_TIMING_UHS_SDR50:
Adrian Hunter4228b212016-04-20 09:24:03 +03002000 if (host->flags & SDHCI_SDR50_NEEDS_TUNING)
Russell King4b6f37d2014-04-25 12:59:36 +01002001 break;
2002 /* FALLTHROUGH */
2003
2004 default:
Adrian Hunterd519c862014-12-05 19:25:29 +02002005 goto out_unlock;
Arindam Nathb513ea22011-05-05 12:19:04 +05302006 }
2007
Dong Aisheng45251812013-09-13 19:11:30 +08002008 if (host->ops->platform_execute_tuning) {
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002009 spin_unlock_irqrestore(&host->lock, flags);
Dong Aisheng45251812013-09-13 19:11:30 +08002010 err = host->ops->platform_execute_tuning(host, opcode);
Dong Aisheng45251812013-09-13 19:11:30 +08002011 return err;
2012 }
2013
Russell King4b6f37d2014-04-25 12:59:36 +01002014 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2015 ctrl |= SDHCI_CTRL_EXEC_TUNING;
Vincent Yang67d0d042015-01-20 16:05:16 +08002016 if (host->quirks2 & SDHCI_QUIRK2_TUNING_WORK_AROUND)
2017 ctrl |= SDHCI_CTRL_TUNED_CLK;
Arindam Nathb513ea22011-05-05 12:19:04 +05302018 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2019
2020 /*
2021 * As per the Host Controller spec v3.00, tuning command
2022 * generates Buffer Read Ready interrupt, so enable that.
2023 *
2024 * Note: The spec clearly says that when tuning sequence
2025 * is being performed, the controller does not generate
2026 * interrupts other than Buffer Read Ready interrupt. But
2027 * to make sure we don't hit a controller bug, we _only_
2028 * enable Buffer Read Ready interrupt here.
2029 */
Russell Kingb537f942014-04-25 12:56:01 +01002030 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
2031 sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_SIGNAL_ENABLE);
Arindam Nathb513ea22011-05-05 12:19:04 +05302032
2033 /*
2034 * Issue CMD19 repeatedly till Execute Tuning is set to 0 or the number
Simon Horman1473bdd2016-05-13 13:24:31 +09002035 * of loops reaches 40 times.
Arindam Nathb513ea22011-05-05 12:19:04 +05302036 */
Arindam Nathb513ea22011-05-05 12:19:04 +05302037 do {
2038 struct mmc_command cmd = {0};
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002039 struct mmc_request mrq = {NULL};
Arindam Nathb513ea22011-05-05 12:19:04 +05302040
Girish K S069c9f12012-01-06 09:56:39 +05302041 cmd.opcode = opcode;
Arindam Nathb513ea22011-05-05 12:19:04 +05302042 cmd.arg = 0;
2043 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
2044 cmd.retries = 0;
2045 cmd.data = NULL;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002046 cmd.mrq = &mrq;
Arindam Nathb513ea22011-05-05 12:19:04 +05302047 cmd.error = 0;
2048
Al Cooper7ce45e92014-05-09 11:34:07 -04002049 if (tuning_loop_counter-- == 0)
2050 break;
2051
Arindam Nathb513ea22011-05-05 12:19:04 +05302052 mrq.cmd = &cmd;
Arindam Nathb513ea22011-05-05 12:19:04 +05302053
2054 /*
2055 * In response to CMD19, the card sends 64 bytes of tuning
2056 * block to the Host Controller. So we set the block size
2057 * to 64 here.
2058 */
Girish K S069c9f12012-01-06 09:56:39 +05302059 if (cmd.opcode == MMC_SEND_TUNING_BLOCK_HS200) {
2060 if (mmc->ios.bus_width == MMC_BUS_WIDTH_8)
2061 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 128),
2062 SDHCI_BLOCK_SIZE);
2063 else if (mmc->ios.bus_width == MMC_BUS_WIDTH_4)
2064 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2065 SDHCI_BLOCK_SIZE);
2066 } else {
2067 sdhci_writew(host, SDHCI_MAKE_BLKSZ(7, 64),
2068 SDHCI_BLOCK_SIZE);
2069 }
Arindam Nathb513ea22011-05-05 12:19:04 +05302070
2071 /*
2072 * The tuning block is sent by the card to the host controller.
2073 * So we set the TRNS_READ bit in the Transfer Mode register.
2074 * This also takes care of setting DMA Enable and Multi Block
2075 * Select in the same register to 0.
2076 */
2077 sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
2078
2079 sdhci_send_command(host, &cmd);
2080
2081 host->cmd = NULL;
Adrian Hunter07c161b2016-06-29 16:24:38 +03002082 sdhci_del_timer(host, &mrq);
Arindam Nathb513ea22011-05-05 12:19:04 +05302083
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002084 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302085 /* Wait for Buffer Read Ready interrupt */
Christopher Freeman622b5f32016-08-17 13:34:27 -04002086 wait_event_timeout(host->buf_ready_int,
Arindam Nathb513ea22011-05-05 12:19:04 +05302087 (host->tuning_done == 1),
2088 msecs_to_jiffies(50));
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002089 spin_lock_irqsave(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302090
2091 if (!host->tuning_done) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002092 pr_info(DRIVER_NAME ": Timeout waiting for Buffer Read Ready interrupt during tuning procedure, falling back to fixed sampling clock\n");
Arindam Nathb513ea22011-05-05 12:19:04 +05302093 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2094 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2095 ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
2096 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
2097
2098 err = -EIO;
2099 goto out;
2100 }
2101
2102 host->tuning_done = 0;
2103
2104 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
Nick Sanders197160d2014-05-06 18:52:38 -07002105
2106 /* eMMC spec does not require a delay between tuning cycles */
2107 if (opcode == MMC_SEND_TUNING_BLOCK)
2108 mdelay(1);
Arindam Nathb513ea22011-05-05 12:19:04 +05302109 } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
2110
2111 /*
2112 * The Host Driver has exhausted the maximum number of loops allowed,
2113 * so use fixed sampling frequency.
2114 */
Al Cooper7ce45e92014-05-09 11:34:07 -04002115 if (tuning_loop_counter < 0) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302116 ctrl &= ~SDHCI_CTRL_TUNED_CLK;
2117 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Al Cooper7ce45e92014-05-09 11:34:07 -04002118 }
2119 if (!(ctrl & SDHCI_CTRL_TUNED_CLK)) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002120 pr_info(DRIVER_NAME ": Tuning procedure failed, falling back to fixed sampling clock\n");
Dong Aisheng114f2bf2013-10-18 19:48:45 +08002121 err = -EIO;
Arindam Nathb513ea22011-05-05 12:19:04 +05302122 }
2123
2124out:
Adrian Hunter38e40bf2014-12-05 19:25:30 +02002125 if (tuning_count) {
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002126 /*
2127 * In case tuning fails, host controllers which support
2128 * re-tuning can try tuning again at a later time, when the
2129 * re-tuning timer expires. So for these controllers, we
2130 * return 0. Since there might be other controllers who do not
2131 * have this capability, we return error for them.
2132 */
2133 err = 0;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302134 }
2135
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002136 host->mmc->retune_period = err ? 0 : tuning_count;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302137
Russell Kingb537f942014-04-25 12:56:01 +01002138 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2139 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunterd519c862014-12-05 19:25:29 +02002140out_unlock:
Aisheng Dong2b35bd82013-12-23 19:13:04 +08002141 spin_unlock_irqrestore(&host->lock, flags);
Arindam Nathb513ea22011-05-05 12:19:04 +05302142 return err;
2143}
2144
Adrian Huntercb849642015-02-06 14:12:59 +02002145static int sdhci_select_drive_strength(struct mmc_card *card,
2146 unsigned int max_dtr, int host_drv,
2147 int card_drv, int *drv_type)
2148{
2149 struct sdhci_host *host = mmc_priv(card->host);
2150
2151 if (!host->ops->select_drive_strength)
2152 return 0;
2153
2154 return host->ops->select_drive_strength(host, card, max_dtr, host_drv,
2155 card_drv, drv_type);
2156}
Kevin Liu52983382013-01-31 11:31:37 +08002157
2158static void sdhci_enable_preset_value(struct sdhci_host *host, bool enable)
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302159{
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302160 /* Host Controller v3.00 defines preset value registers */
2161 if (host->version < SDHCI_SPEC_300)
2162 return;
2163
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302164 /*
2165 * We only enable or disable Preset Value if they are not already
2166 * enabled or disabled respectively. Otherwise, we bail out.
2167 */
Russell Kingda91a8f2014-04-25 13:00:12 +01002168 if (host->preset_enabled != enable) {
2169 u16 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
2170
2171 if (enable)
2172 ctrl |= SDHCI_CTRL_PRESET_VAL_ENABLE;
2173 else
2174 ctrl &= ~SDHCI_CTRL_PRESET_VAL_ENABLE;
2175
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302176 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Russell Kingda91a8f2014-04-25 13:00:12 +01002177
2178 if (enable)
2179 host->flags |= SDHCI_PV_ENABLED;
2180 else
2181 host->flags &= ~SDHCI_PV_ENABLED;
2182
2183 host->preset_enabled = enable;
Arindam Nath4d55c5a2011-05-05 12:19:05 +05302184 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002185}
2186
Haibo Chen348487c2014-12-09 17:04:05 +08002187static void sdhci_post_req(struct mmc_host *mmc, struct mmc_request *mrq,
2188 int err)
2189{
2190 struct sdhci_host *host = mmc_priv(mmc);
2191 struct mmc_data *data = mrq->data;
2192
Russell Kingf48f0392016-01-26 13:40:32 +00002193 if (data->host_cookie != COOKIE_UNMAPPED)
Russell King771a3dc2016-01-26 13:40:53 +00002194 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2195 data->flags & MMC_DATA_WRITE ?
2196 DMA_TO_DEVICE : DMA_FROM_DEVICE);
2197
2198 data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002199}
2200
Linus Walleijd3c6aac2016-11-23 11:02:24 +01002201static void sdhci_pre_req(struct mmc_host *mmc, struct mmc_request *mrq)
Haibo Chen348487c2014-12-09 17:04:05 +08002202{
2203 struct sdhci_host *host = mmc_priv(mmc);
2204
Haibo Chend31911b2015-08-25 10:02:11 +08002205 mrq->data->host_cookie = COOKIE_UNMAPPED;
Haibo Chen348487c2014-12-09 17:04:05 +08002206
2207 if (host->flags & SDHCI_REQ_USE_DMA)
Russell King94538e52016-01-26 13:40:37 +00002208 sdhci_pre_dma_transfer(host, mrq->data, COOKIE_PRE_MAPPED);
Haibo Chen348487c2014-12-09 17:04:05 +08002209}
2210
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002211static inline bool sdhci_has_requests(struct sdhci_host *host)
2212{
2213 return host->cmd || host->data_cmd;
2214}
2215
2216static void sdhci_error_out_mrqs(struct sdhci_host *host, int err)
2217{
2218 if (host->data_cmd) {
2219 host->data_cmd->error = err;
2220 sdhci_finish_mrq(host, host->data_cmd->mrq);
2221 }
2222
2223 if (host->cmd) {
2224 host->cmd->error = err;
2225 sdhci_finish_mrq(host, host->cmd->mrq);
2226 }
2227}
2228
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002229static void sdhci_card_event(struct mmc_host *mmc)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002230{
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002231 struct sdhci_host *host = mmc_priv(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002232 unsigned long flags;
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002233 int present;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002234
Christian Daudt722e1282013-06-20 14:26:36 -07002235 /* First check if client has provided their own card event */
2236 if (host->ops->card_event)
2237 host->ops->card_event(host);
2238
Adrian Hunterd3940f22016-06-29 16:24:14 +03002239 present = mmc->ops->get_cd(mmc);
Krzysztof Kozlowski28367662015-01-05 10:50:15 +01002240
Pierre Ossmand129bce2006-03-24 03:18:17 -08002241 spin_lock_irqsave(&host->lock, flags);
2242
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002243 /* Check sdhci_has_requests() first in case we are runtime suspended */
2244 if (sdhci_has_requests(host) && !present) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302245 pr_err("%s: Card removed during transfer!\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002246 mmc_hostname(host->mmc));
Girish K Sa3c76eb2011-10-11 11:44:09 +05302247 pr_err("%s: Resetting controller.\n",
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002248 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002249
Russell King03231f92014-04-25 12:57:12 +01002250 sdhci_do_reset(host, SDHCI_RESET_CMD);
2251 sdhci_do_reset(host, SDHCI_RESET_DATA);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002252
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03002253 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002254 }
2255
2256 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002257}
2258
2259static const struct mmc_host_ops sdhci_ops = {
2260 .request = sdhci_request,
Haibo Chen348487c2014-12-09 17:04:05 +08002261 .post_req = sdhci_post_req,
2262 .pre_req = sdhci_pre_req,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002263 .set_ios = sdhci_set_ios,
Kevin Liu94144a42013-02-28 17:35:53 +08002264 .get_cd = sdhci_get_cd,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002265 .get_ro = sdhci_get_ro,
2266 .hw_reset = sdhci_hw_reset,
2267 .enable_sdio_irq = sdhci_enable_sdio_irq,
2268 .start_signal_voltage_switch = sdhci_start_signal_voltage_switch,
Adrian Hunterb5540ce2014-12-05 19:25:31 +02002269 .prepare_hs400_tuning = sdhci_prepare_hs400_tuning,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002270 .execute_tuning = sdhci_execute_tuning,
Adrian Huntercb849642015-02-06 14:12:59 +02002271 .select_drive_strength = sdhci_select_drive_strength,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002272 .card_event = sdhci_card_event,
Kevin Liu20b92a32012-12-17 19:29:26 +08002273 .card_busy = sdhci_card_busy,
Guennadi Liakhovetski71e69212012-12-04 16:51:40 +01002274};
2275
2276/*****************************************************************************\
2277 * *
2278 * Tasklets *
2279 * *
2280\*****************************************************************************/
2281
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002282static bool sdhci_request_done(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002283{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002284 unsigned long flags;
2285 struct mmc_request *mrq;
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002286 int i;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002287
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002288 spin_lock_irqsave(&host->lock, flags);
2289
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002290 for (i = 0; i < SDHCI_MAX_MRQS; i++) {
2291 mrq = host->mrqs_done[i];
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002292 if (mrq)
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002293 break;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002294 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002295
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002296 if (!mrq) {
2297 spin_unlock_irqrestore(&host->lock, flags);
2298 return true;
2299 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002300
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002301 sdhci_del_timer(host, mrq);
2302
Pierre Ossmand129bce2006-03-24 03:18:17 -08002303 /*
Russell King054cedf2016-01-26 13:40:42 +00002304 * Always unmap the data buffers if they were mapped by
2305 * sdhci_prepare_data() whenever we finish with a request.
2306 * This avoids leaking DMA mappings on error.
2307 */
2308 if (host->flags & SDHCI_REQ_USE_DMA) {
2309 struct mmc_data *data = mrq->data;
2310
2311 if (data && data->host_cookie == COOKIE_MAPPED) {
2312 dma_unmap_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
2313 (data->flags & MMC_DATA_READ) ?
2314 DMA_FROM_DEVICE : DMA_TO_DEVICE);
2315 data->host_cookie = COOKIE_UNMAPPED;
2316 }
2317 }
2318
2319 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08002320 * The controller needs a reset of internal state machines
2321 * upon error conditions.
2322 */
Adrian Hunter0cc563c2016-06-29 16:24:28 +03002323 if (sdhci_needs_reset(host, mrq)) {
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002324 /*
2325 * Do not finish until command and data lines are available for
2326 * reset. Note there can only be one other mrq, so it cannot
2327 * also be in mrqs_done, otherwise host->cmd and host->data_cmd
2328 * would both be null.
2329 */
2330 if (host->cmd || host->data_cmd) {
2331 spin_unlock_irqrestore(&host->lock, flags);
2332 return true;
2333 }
2334
Pierre Ossman645289d2006-06-30 02:22:33 -07002335 /* Some controllers need this kick or reset won't work here */
Andy Shevchenko8213af32013-01-07 16:31:08 +02002336 if (host->quirks & SDHCI_QUIRK_CLOCK_BEFORE_RESET)
Pierre Ossman645289d2006-06-30 02:22:33 -07002337 /* This is to force an update */
Russell King17710592014-04-25 12:58:55 +01002338 host->ops->set_clock(host, host->clock);
Pierre Ossman645289d2006-06-30 02:22:33 -07002339
2340 /* Spec says we should do both at the same time, but Ricoh
2341 controllers do not like that. */
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002342 sdhci_do_reset(host, SDHCI_RESET_CMD);
2343 sdhci_do_reset(host, SDHCI_RESET_DATA);
Adrian Huntered1563d2016-06-29 16:24:29 +03002344
2345 host->pending_reset = false;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002346 }
2347
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002348 if (!sdhci_has_requests(host))
2349 sdhci_led_deactivate(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002350
Adrian Hunter6ebebea2016-11-02 15:49:08 +02002351 host->mrqs_done[i] = NULL;
2352
Pierre Ossman5f25a662006-10-04 02:15:39 -07002353 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002354 spin_unlock_irqrestore(&host->lock, flags);
2355
2356 mmc_request_done(host->mmc, mrq);
Adrian Hunter4e9f8fe2016-06-29 16:24:34 +03002357
2358 return false;
2359}
2360
2361static void sdhci_tasklet_finish(unsigned long param)
2362{
2363 struct sdhci_host *host = (struct sdhci_host *)param;
2364
2365 while (!sdhci_request_done(host))
2366 ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002367}
2368
2369static void sdhci_timeout_timer(unsigned long data)
2370{
2371 struct sdhci_host *host;
2372 unsigned long flags;
2373
2374 host = (struct sdhci_host*)data;
2375
2376 spin_lock_irqsave(&host->lock, flags);
2377
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002378 if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
2379 pr_err("%s: Timeout waiting for hardware cmd interrupt.\n",
2380 mmc_hostname(host->mmc));
2381 sdhci_dumpregs(host);
2382
2383 host->cmd->error = -ETIMEDOUT;
2384 sdhci_finish_mrq(host, host->cmd->mrq);
2385 }
2386
2387 mmiowb();
2388 spin_unlock_irqrestore(&host->lock, flags);
2389}
2390
2391static void sdhci_timeout_data_timer(unsigned long data)
2392{
2393 struct sdhci_host *host;
2394 unsigned long flags;
2395
2396 host = (struct sdhci_host *)data;
2397
2398 spin_lock_irqsave(&host->lock, flags);
2399
2400 if (host->data || host->data_cmd ||
2401 (host->cmd && sdhci_data_line_cmd(host->cmd))) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01002402 pr_err("%s: Timeout waiting for hardware interrupt.\n",
2403 mmc_hostname(host->mmc));
Pierre Ossmand129bce2006-03-24 03:18:17 -08002404 sdhci_dumpregs(host);
2405
2406 if (host->data) {
Pierre Ossman17b04292007-07-22 22:18:46 +02002407 host->data->error = -ETIMEDOUT;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002408 sdhci_finish_data(host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002409 } else if (host->data_cmd) {
2410 host->data_cmd->error = -ETIMEDOUT;
2411 sdhci_finish_mrq(host, host->data_cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002412 } else {
Adrian Hunterd7422fb2016-06-29 16:24:33 +03002413 host->cmd->error = -ETIMEDOUT;
2414 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002415 }
2416 }
2417
Pierre Ossman5f25a662006-10-04 02:15:39 -07002418 mmiowb();
Pierre Ossmand129bce2006-03-24 03:18:17 -08002419 spin_unlock_irqrestore(&host->lock, flags);
2420}
2421
2422/*****************************************************************************\
2423 * *
2424 * Interrupt handling *
2425 * *
2426\*****************************************************************************/
2427
Adrian Hunterfc605f12016-10-05 12:11:21 +03002428static void sdhci_cmd_irq(struct sdhci_host *host, u32 intmask)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002429{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002430 if (!host->cmd) {
Adrian Huntered1563d2016-06-29 16:24:29 +03002431 /*
2432 * SDHCI recovers from errors by resetting the cmd and data
2433 * circuits. Until that is done, there very well might be more
2434 * interrupts, so ignore them in that case.
2435 */
2436 if (host->pending_reset)
2437 return;
Marek Vasut2e4456f2015-11-18 10:47:02 +01002438 pr_err("%s: Got command interrupt 0x%08x even though no command operation was in progress.\n",
2439 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002440 sdhci_dumpregs(host);
2441 return;
2442 }
2443
Russell Kingec014cb2016-01-26 13:39:39 +00002444 if (intmask & (SDHCI_INT_TIMEOUT | SDHCI_INT_CRC |
2445 SDHCI_INT_END_BIT | SDHCI_INT_INDEX)) {
2446 if (intmask & SDHCI_INT_TIMEOUT)
2447 host->cmd->error = -ETIMEDOUT;
2448 else
2449 host->cmd->error = -EILSEQ;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002450
Russell King71fcbda2016-01-26 13:39:45 +00002451 /*
2452 * If this command initiates a data phase and a response
2453 * CRC error is signalled, the card can start transferring
2454 * data - the card may have received the command without
2455 * error. We must not terminate the mmc_request early.
2456 *
2457 * If the card did not receive the command or returned an
2458 * error which prevented it sending data, the data phase
2459 * will time out.
2460 */
2461 if (host->cmd->data &&
2462 (intmask & (SDHCI_INT_CRC | SDHCI_INT_TIMEOUT)) ==
2463 SDHCI_INT_CRC) {
2464 host->cmd = NULL;
2465 return;
2466 }
2467
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002468 sdhci_finish_mrq(host, host->cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002469 return;
2470 }
2471
Pierre Ossmane8095172008-07-25 01:09:08 +02002472 if (intmask & SDHCI_INT_RESPONSE)
Pierre Ossman43b58b32007-07-25 23:15:27 +02002473 sdhci_finish_command(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002474}
2475
George G. Davis0957c332010-02-18 12:32:12 -05002476#ifdef CONFIG_MMC_DEBUG
Adrian Hunter08621b12014-11-04 12:42:38 +02002477static void sdhci_adma_show_error(struct sdhci_host *host)
Ben Dooks6882a8c2009-06-14 13:52:38 +01002478{
2479 const char *name = mmc_hostname(host->mmc);
Adrian Hunter1c3d5f62014-11-04 12:42:41 +02002480 void *desc = host->adma_table;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002481
2482 sdhci_dumpregs(host);
2483
2484 while (true) {
Adrian Huntere57a5f62014-11-04 12:42:46 +02002485 struct sdhci_adma2_64_desc *dma_desc = desc;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002486
Adrian Huntere57a5f62014-11-04 12:42:46 +02002487 if (host->flags & SDHCI_USE_64_BIT_DMA)
2488 DBG("%s: %p: DMA 0x%08x%08x, LEN 0x%04x, Attr=0x%02x\n",
2489 name, desc, le32_to_cpu(dma_desc->addr_hi),
2490 le32_to_cpu(dma_desc->addr_lo),
2491 le16_to_cpu(dma_desc->len),
2492 le16_to_cpu(dma_desc->cmd));
2493 else
2494 DBG("%s: %p: DMA 0x%08x, LEN 0x%04x, Attr=0x%02x\n",
2495 name, desc, le32_to_cpu(dma_desc->addr_lo),
2496 le16_to_cpu(dma_desc->len),
2497 le16_to_cpu(dma_desc->cmd));
Ben Dooks6882a8c2009-06-14 13:52:38 +01002498
Adrian Hunter76fe3792014-11-04 12:42:42 +02002499 desc += host->desc_sz;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002500
Adrian Hunter05452302014-11-04 12:42:45 +02002501 if (dma_desc->cmd & cpu_to_le16(ADMA2_END))
Ben Dooks6882a8c2009-06-14 13:52:38 +01002502 break;
2503 }
2504}
2505#else
Adrian Hunter08621b12014-11-04 12:42:38 +02002506static void sdhci_adma_show_error(struct sdhci_host *host) { }
Ben Dooks6882a8c2009-06-14 13:52:38 +01002507#endif
2508
Pierre Ossmand129bce2006-03-24 03:18:17 -08002509static void sdhci_data_irq(struct sdhci_host *host, u32 intmask)
2510{
Girish K S069c9f12012-01-06 09:56:39 +05302511 u32 command;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002512
Arindam Nathb513ea22011-05-05 12:19:04 +05302513 /* CMD19 generates _only_ Buffer Read Ready interrupt */
2514 if (intmask & SDHCI_INT_DATA_AVAIL) {
Girish K S069c9f12012-01-06 09:56:39 +05302515 command = SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND));
2516 if (command == MMC_SEND_TUNING_BLOCK ||
2517 command == MMC_SEND_TUNING_BLOCK_HS200) {
Arindam Nathb513ea22011-05-05 12:19:04 +05302518 host->tuning_done = 1;
2519 wake_up(&host->buf_ready_int);
2520 return;
2521 }
2522 }
2523
Pierre Ossmand129bce2006-03-24 03:18:17 -08002524 if (!host->data) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002525 struct mmc_command *data_cmd = host->data_cmd;
2526
Pierre Ossmand129bce2006-03-24 03:18:17 -08002527 /*
Pierre Ossmane8095172008-07-25 01:09:08 +02002528 * The "data complete" interrupt is also used to
2529 * indicate that a busy state has ended. See comment
2530 * above in sdhci_cmd_irq().
Pierre Ossmand129bce2006-03-24 03:18:17 -08002531 */
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002532 if (data_cmd && (data_cmd->flags & MMC_RSP_BUSY)) {
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002533 if (intmask & SDHCI_INT_DATA_TIMEOUT) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002534 host->data_cmd = NULL;
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002535 data_cmd->error = -ETIMEDOUT;
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002536 sdhci_finish_mrq(host, data_cmd->mrq);
Matthieu CASTETc5abd5e2014-08-14 16:03:17 +02002537 return;
2538 }
Pierre Ossmane8095172008-07-25 01:09:08 +02002539 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter69b962a2016-11-02 15:49:09 +02002540 host->data_cmd = NULL;
Chanho Mine99783a2014-08-30 12:40:40 +09002541 /*
2542 * Some cards handle busy-end interrupt
2543 * before the command completed, so make
2544 * sure we do things in the proper order.
2545 */
Adrian Hunterea968022016-06-29 16:24:24 +03002546 if (host->cmd == data_cmd)
2547 return;
2548
Adrian Huntera6d3bdd2016-06-29 16:24:27 +03002549 sdhci_finish_mrq(host, data_cmd->mrq);
Pierre Ossmane8095172008-07-25 01:09:08 +02002550 return;
2551 }
2552 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002553
Adrian Huntered1563d2016-06-29 16:24:29 +03002554 /*
2555 * SDHCI recovers from errors by resetting the cmd and data
2556 * circuits. Until that is done, there very well might be more
2557 * interrupts, so ignore them in that case.
2558 */
2559 if (host->pending_reset)
2560 return;
2561
Marek Vasut2e4456f2015-11-18 10:47:02 +01002562 pr_err("%s: Got data interrupt 0x%08x even though no data operation was in progress.\n",
2563 mmc_hostname(host->mmc), (unsigned)intmask);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002564 sdhci_dumpregs(host);
2565
2566 return;
2567 }
2568
2569 if (intmask & SDHCI_INT_DATA_TIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +02002570 host->data->error = -ETIMEDOUT;
Aries Lee22113ef2010-12-15 08:14:24 +01002571 else if (intmask & SDHCI_INT_DATA_END_BIT)
2572 host->data->error = -EILSEQ;
2573 else if ((intmask & SDHCI_INT_DATA_CRC) &&
2574 SDHCI_GET_CMD(sdhci_readw(host, SDHCI_COMMAND))
2575 != MMC_BUS_TEST_R)
Pierre Ossman17b04292007-07-22 22:18:46 +02002576 host->data->error = -EILSEQ;
Ben Dooks6882a8c2009-06-14 13:52:38 +01002577 else if (intmask & SDHCI_INT_ADMA_ERROR) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05302578 pr_err("%s: ADMA error\n", mmc_hostname(host->mmc));
Adrian Hunter08621b12014-11-04 12:42:38 +02002579 sdhci_adma_show_error(host);
Pierre Ossman2134a922008-06-28 18:28:51 +02002580 host->data->error = -EIO;
Haijun Zhanga4071fb2012-12-04 10:41:28 +08002581 if (host->ops->adma_workaround)
2582 host->ops->adma_workaround(host, intmask);
Ben Dooks6882a8c2009-06-14 13:52:38 +01002583 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002584
Pierre Ossman17b04292007-07-22 22:18:46 +02002585 if (host->data->error)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002586 sdhci_finish_data(host);
2587 else {
Pierre Ossmana406f5a2006-07-02 16:50:59 +01002588 if (intmask & (SDHCI_INT_DATA_AVAIL | SDHCI_INT_SPACE_AVAIL))
Pierre Ossmand129bce2006-03-24 03:18:17 -08002589 sdhci_transfer_pio(host);
2590
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002591 /*
2592 * We currently don't do anything fancy with DMA
2593 * boundaries, but as we can't disable the feature
2594 * we need to at least restart the transfer.
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002595 *
2596 * According to the spec sdhci_readl(host, SDHCI_DMA_ADDRESS)
2597 * should return a valid address to continue from, but as
2598 * some controllers are faulty, don't trust them.
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002599 */
Mikko Vinnif6a03cb2011-04-12 09:36:18 -04002600 if (intmask & SDHCI_INT_DMA_END) {
2601 u32 dmastart, dmanow;
2602 dmastart = sg_dma_address(host->data->sg);
2603 dmanow = dmastart + host->data->bytes_xfered;
2604 /*
2605 * Force update to the next DMA block boundary.
2606 */
2607 dmanow = (dmanow &
2608 ~(SDHCI_DEFAULT_BOUNDARY_SIZE - 1)) +
2609 SDHCI_DEFAULT_BOUNDARY_SIZE;
2610 host->data->bytes_xfered = dmanow - dmastart;
2611 DBG("%s: DMA base 0x%08x, transferred 0x%06x bytes,"
2612 " next 0x%08x\n",
2613 mmc_hostname(host->mmc), dmastart,
2614 host->data->bytes_xfered, dmanow);
2615 sdhci_writel(host, dmanow, SDHCI_DMA_ADDRESS);
2616 }
Pierre Ossman6ba736a2007-05-13 22:39:23 +02002617
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002618 if (intmask & SDHCI_INT_DATA_END) {
Adrian Hunter7c89a3d2016-06-29 16:24:23 +03002619 if (host->cmd == host->data_cmd) {
Pierre Ossmane538fbe2007-08-12 16:46:32 +02002620 /*
2621 * Data managed to finish before the
2622 * command completed. Make sure we do
2623 * things in the proper order.
2624 */
2625 host->data_early = 1;
2626 } else {
2627 sdhci_finish_data(host);
2628 }
2629 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08002630 }
2631}
2632
David Howells7d12e782006-10-05 14:55:46 +01002633static irqreturn_t sdhci_irq(int irq, void *dev_id)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002634{
Russell King781e9892014-04-25 12:55:46 +01002635 irqreturn_t result = IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002636 struct sdhci_host *host = dev_id;
Russell King41005002014-04-25 12:55:36 +01002637 u32 intmask, mask, unexpected = 0;
Russell King781e9892014-04-25 12:55:46 +01002638 int max_loops = 16;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002639
2640 spin_lock(&host->lock);
2641
Russell Kingbe138552014-04-25 12:55:56 +01002642 if (host->runtime_suspended && !sdhci_sdio_irq_enabled(host)) {
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002643 spin_unlock(&host->lock);
Adrian Hunter655bca72014-03-11 10:09:36 +02002644 return IRQ_NONE;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002645 }
2646
Anton Vorontsov4e4141a2009-03-17 00:13:46 +03002647 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Mark Lord62df67a52007-03-06 13:30:13 +01002648 if (!intmask || intmask == 0xffffffff) {
Pierre Ossmand129bce2006-03-24 03:18:17 -08002649 result = IRQ_NONE;
2650 goto out;
2651 }
2652
Russell King41005002014-04-25 12:55:36 +01002653 do {
2654 /* Clear selected interrupts. */
2655 mask = intmask & (SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2656 SDHCI_INT_BUS_POWER);
2657 sdhci_writel(host, mask, SDHCI_INT_STATUS);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002658
Russell King41005002014-04-25 12:55:36 +01002659 DBG("*** %s got interrupt: 0x%08x\n",
2660 mmc_hostname(host->mmc), intmask);
2661
2662 if (intmask & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
2663 u32 present = sdhci_readl(host, SDHCI_PRESENT_STATE) &
2664 SDHCI_CARD_PRESENT;
2665
2666 /*
2667 * There is a observation on i.mx esdhc. INSERT
2668 * bit will be immediately set again when it gets
2669 * cleared, if a card is inserted. We have to mask
2670 * the irq to prevent interrupt storm which will
2671 * freeze the system. And the REMOVE gets the
2672 * same situation.
2673 *
2674 * More testing are needed here to ensure it works
2675 * for other platforms though.
2676 */
Russell Kingb537f942014-04-25 12:56:01 +01002677 host->ier &= ~(SDHCI_INT_CARD_INSERT |
2678 SDHCI_INT_CARD_REMOVE);
2679 host->ier |= present ? SDHCI_INT_CARD_REMOVE :
2680 SDHCI_INT_CARD_INSERT;
2681 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2682 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Russell King41005002014-04-25 12:55:36 +01002683
2684 sdhci_writel(host, intmask & (SDHCI_INT_CARD_INSERT |
2685 SDHCI_INT_CARD_REMOVE), SDHCI_INT_STATUS);
Russell King3560db82014-04-25 12:55:51 +01002686
2687 host->thread_isr |= intmask & (SDHCI_INT_CARD_INSERT |
2688 SDHCI_INT_CARD_REMOVE);
2689 result = IRQ_WAKE_THREAD;
Russell King41005002014-04-25 12:55:36 +01002690 }
2691
2692 if (intmask & SDHCI_INT_CMD_MASK)
Adrian Hunterfc605f12016-10-05 12:11:21 +03002693 sdhci_cmd_irq(host, intmask & SDHCI_INT_CMD_MASK);
Russell King41005002014-04-25 12:55:36 +01002694
2695 if (intmask & SDHCI_INT_DATA_MASK)
2696 sdhci_data_irq(host, intmask & SDHCI_INT_DATA_MASK);
2697
2698 if (intmask & SDHCI_INT_BUS_POWER)
2699 pr_err("%s: Card is consuming too much power!\n",
2700 mmc_hostname(host->mmc));
2701
Dong Aishengf37b20e2016-07-12 15:46:17 +08002702 if (intmask & SDHCI_INT_RETUNE)
2703 mmc_retune_needed(host->mmc);
2704
Russell King781e9892014-04-25 12:55:46 +01002705 if (intmask & SDHCI_INT_CARD_INT) {
2706 sdhci_enable_sdio_irq_nolock(host, false);
2707 host->thread_isr |= SDHCI_INT_CARD_INT;
2708 result = IRQ_WAKE_THREAD;
2709 }
Russell King41005002014-04-25 12:55:36 +01002710
2711 intmask &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2712 SDHCI_INT_CMD_MASK | SDHCI_INT_DATA_MASK |
2713 SDHCI_INT_ERROR | SDHCI_INT_BUS_POWER |
Dong Aishengf37b20e2016-07-12 15:46:17 +08002714 SDHCI_INT_RETUNE | SDHCI_INT_CARD_INT);
Russell King41005002014-04-25 12:55:36 +01002715
2716 if (intmask) {
2717 unexpected |= intmask;
2718 sdhci_writel(host, intmask, SDHCI_INT_STATUS);
2719 }
2720
Russell King781e9892014-04-25 12:55:46 +01002721 if (result == IRQ_NONE)
2722 result = IRQ_HANDLED;
Russell King41005002014-04-25 12:55:36 +01002723
2724 intmask = sdhci_readl(host, SDHCI_INT_STATUS);
Russell King41005002014-04-25 12:55:36 +01002725 } while (intmask && --max_loops);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002726out:
2727 spin_unlock(&host->lock);
2728
Alexander Stein6379b232012-03-14 09:52:10 +01002729 if (unexpected) {
2730 pr_err("%s: Unexpected interrupt 0x%08x.\n",
2731 mmc_hostname(host->mmc), unexpected);
2732 sdhci_dumpregs(host);
2733 }
Pierre Ossmanf75979b2007-09-04 07:59:18 +02002734
Pierre Ossmand129bce2006-03-24 03:18:17 -08002735 return result;
2736}
2737
Russell King781e9892014-04-25 12:55:46 +01002738static irqreturn_t sdhci_thread_irq(int irq, void *dev_id)
2739{
2740 struct sdhci_host *host = dev_id;
2741 unsigned long flags;
2742 u32 isr;
2743
2744 spin_lock_irqsave(&host->lock, flags);
2745 isr = host->thread_isr;
2746 host->thread_isr = 0;
2747 spin_unlock_irqrestore(&host->lock, flags);
2748
Russell King3560db82014-04-25 12:55:51 +01002749 if (isr & (SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE)) {
Adrian Hunterd3940f22016-06-29 16:24:14 +03002750 struct mmc_host *mmc = host->mmc;
2751
2752 mmc->ops->card_event(mmc);
2753 mmc_detect_change(mmc, msecs_to_jiffies(200));
Russell King3560db82014-04-25 12:55:51 +01002754 }
2755
Russell King781e9892014-04-25 12:55:46 +01002756 if (isr & SDHCI_INT_CARD_INT) {
2757 sdio_run_irqs(host->mmc);
2758
2759 spin_lock_irqsave(&host->lock, flags);
2760 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
2761 sdhci_enable_sdio_irq_nolock(host, true);
2762 spin_unlock_irqrestore(&host->lock, flags);
2763 }
2764
2765 return isr ? IRQ_HANDLED : IRQ_NONE;
2766}
2767
Pierre Ossmand129bce2006-03-24 03:18:17 -08002768/*****************************************************************************\
2769 * *
2770 * Suspend/resume *
2771 * *
2772\*****************************************************************************/
2773
2774#ifdef CONFIG_PM
Ludovic Desroches84d62602016-05-13 15:16:02 +02002775/*
2776 * To enable wakeup events, the corresponding events have to be enabled in
2777 * the Interrupt Status Enable register too. See 'Table 1-6: Wakeup Signal
2778 * Table' in the SD Host Controller Standard Specification.
2779 * It is useless to restore SDHCI_INT_ENABLE state in
2780 * sdhci_disable_irq_wakeups() since it will be set by
2781 * sdhci_enable_card_detection() or sdhci_init().
2782 */
Kevin Liuad080d72013-01-05 17:21:33 +08002783void sdhci_enable_irq_wakeups(struct sdhci_host *host)
2784{
2785 u8 val;
2786 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2787 | SDHCI_WAKE_ON_INT;
Ludovic Desroches84d62602016-05-13 15:16:02 +02002788 u32 irq_val = SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE |
2789 SDHCI_INT_CARD_INT;
Kevin Liuad080d72013-01-05 17:21:33 +08002790
2791 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2792 val |= mask ;
2793 /* Avoid fake wake up */
Ludovic Desroches84d62602016-05-13 15:16:02 +02002794 if (host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) {
Kevin Liuad080d72013-01-05 17:21:33 +08002795 val &= ~(SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002796 irq_val &= ~(SDHCI_INT_CARD_INSERT | SDHCI_INT_CARD_REMOVE);
2797 }
Kevin Liuad080d72013-01-05 17:21:33 +08002798 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
Ludovic Desroches84d62602016-05-13 15:16:02 +02002799 sdhci_writel(host, irq_val, SDHCI_INT_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002800}
2801EXPORT_SYMBOL_GPL(sdhci_enable_irq_wakeups);
2802
Fabio Estevam0b10f472014-08-30 14:53:13 -03002803static void sdhci_disable_irq_wakeups(struct sdhci_host *host)
Kevin Liuad080d72013-01-05 17:21:33 +08002804{
2805 u8 val;
2806 u8 mask = SDHCI_WAKE_ON_INSERT | SDHCI_WAKE_ON_REMOVE
2807 | SDHCI_WAKE_ON_INT;
2808
2809 val = sdhci_readb(host, SDHCI_WAKE_UP_CONTROL);
2810 val &= ~mask;
2811 sdhci_writeb(host, val, SDHCI_WAKE_UP_CONTROL);
2812}
Pierre Ossmand129bce2006-03-24 03:18:17 -08002813
Manuel Lauss29495aa2011-11-03 11:09:45 +01002814int sdhci_suspend_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002815{
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002816 sdhci_disable_card_detection(host);
2817
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002818 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08002819 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
2820 mmc_retune_needed(host->mmc);
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05302821
Kevin Liuad080d72013-01-05 17:21:33 +08002822 if (!device_may_wakeup(mmc_dev(host->mmc))) {
Russell Kingb537f942014-04-25 12:56:01 +01002823 host->ier = 0;
2824 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
2825 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Kevin Liuad080d72013-01-05 17:21:33 +08002826 free_irq(host->irq, host);
2827 } else {
2828 sdhci_enable_irq_wakeups(host);
2829 enable_irq_wake(host->irq);
2830 }
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002831 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002832}
2833
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002834EXPORT_SYMBOL_GPL(sdhci_suspend_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002835
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002836int sdhci_resume_host(struct sdhci_host *host)
2837{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002838 struct mmc_host *mmc = host->mmc;
Ulf Hansson4ee14ec2013-09-25 14:15:24 +02002839 int ret = 0;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002840
Richard Röjforsa13abc72009-09-22 16:45:30 -07002841 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002842 if (host->ops->enable_dma)
2843 host->ops->enable_dma(host);
2844 }
2845
Adrian Hunter6308d292012-02-07 14:48:54 +02002846 if ((host->mmc->pm_flags & MMC_PM_KEEP_POWER) &&
2847 (host->quirks2 & SDHCI_QUIRK2_HOST_OFF_CARD_ON)) {
2848 /* Card keeps power but host controller does not */
2849 sdhci_init(host, 0);
2850 host->pwr = 0;
2851 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002852 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter6308d292012-02-07 14:48:54 +02002853 } else {
2854 sdhci_init(host, (host->mmc->pm_flags & MMC_PM_KEEP_POWER));
2855 mmiowb();
2856 }
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002857
Haibo Chen14a7b41642015-09-15 18:32:58 +08002858 if (!device_may_wakeup(mmc_dev(host->mmc))) {
2859 ret = request_threaded_irq(host->irq, sdhci_irq,
2860 sdhci_thread_irq, IRQF_SHARED,
2861 mmc_hostname(host->mmc), host);
2862 if (ret)
2863 return ret;
2864 } else {
2865 sdhci_disable_irq_wakeups(host);
2866 disable_irq_wake(host->irq);
2867 }
2868
Anton Vorontsov7260cf52009-03-17 00:13:48 +03002869 sdhci_enable_card_detection(host);
2870
Nicolas Pitre2f4cbb32010-03-05 13:43:32 -08002871 return ret;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002872}
2873
2874EXPORT_SYMBOL_GPL(sdhci_resume_host);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002875
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002876int sdhci_runtime_suspend_host(struct sdhci_host *host)
2877{
2878 unsigned long flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002879
Adrian Hunter66c39dfc2015-05-07 13:10:21 +03002880 mmc_retune_timer_stop(host->mmc);
Dong Aishengf37b20e2016-07-12 15:46:17 +08002881 if (host->tuning_mode != SDHCI_TUNING_MODE_3)
2882 mmc_retune_needed(host->mmc);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002883
2884 spin_lock_irqsave(&host->lock, flags);
Russell Kingb537f942014-04-25 12:56:01 +01002885 host->ier &= SDHCI_INT_CARD_INT;
2886 sdhci_writel(host, host->ier, SDHCI_INT_ENABLE);
2887 sdhci_writel(host, host->ier, SDHCI_SIGNAL_ENABLE);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002888 spin_unlock_irqrestore(&host->lock, flags);
2889
Russell King781e9892014-04-25 12:55:46 +01002890 synchronize_hardirq(host->irq);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002891
2892 spin_lock_irqsave(&host->lock, flags);
2893 host->runtime_suspended = true;
2894 spin_unlock_irqrestore(&host->lock, flags);
2895
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002896 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002897}
2898EXPORT_SYMBOL_GPL(sdhci_runtime_suspend_host);
2899
2900int sdhci_runtime_resume_host(struct sdhci_host *host)
2901{
Adrian Hunterd3940f22016-06-29 16:24:14 +03002902 struct mmc_host *mmc = host->mmc;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002903 unsigned long flags;
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002904 int host_flags = host->flags;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002905
2906 if (host_flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
2907 if (host->ops->enable_dma)
2908 host->ops->enable_dma(host);
2909 }
2910
2911 sdhci_init(host, 0);
2912
2913 /* Force clock and power re-program */
2914 host->pwr = 0;
2915 host->clock = 0;
Adrian Hunterd3940f22016-06-29 16:24:14 +03002916 mmc->ops->start_signal_voltage_switch(mmc, &mmc->ios);
2917 mmc->ops->set_ios(mmc, &mmc->ios);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002918
Kevin Liu52983382013-01-31 11:31:37 +08002919 if ((host_flags & SDHCI_PV_ENABLED) &&
2920 !(host->quirks2 & SDHCI_QUIRK2_PRESET_VALUE_BROKEN)) {
2921 spin_lock_irqsave(&host->lock, flags);
2922 sdhci_enable_preset_value(host, true);
2923 spin_unlock_irqrestore(&host->lock, flags);
2924 }
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002925
Adrian Hunter086b0dd2016-11-02 15:49:11 +02002926 if ((mmc->caps2 & MMC_CAP2_HS400_ES) &&
2927 mmc->ops->hs400_enhanced_strobe)
2928 mmc->ops->hs400_enhanced_strobe(mmc, &mmc->ios);
2929
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002930 spin_lock_irqsave(&host->lock, flags);
2931
2932 host->runtime_suspended = false;
2933
2934 /* Enable SDIO IRQ */
Russell Kingef104332014-04-25 12:55:41 +01002935 if (host->flags & SDHCI_SDIO_IRQ_ENABLED)
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002936 sdhci_enable_sdio_irq_nolock(host, true);
2937
2938 /* Enable Card Detection */
2939 sdhci_enable_card_detection(host);
2940
2941 spin_unlock_irqrestore(&host->lock, flags);
2942
Markus Pargmann8a125ba2014-06-04 15:24:29 +02002943 return 0;
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002944}
2945EXPORT_SYMBOL_GPL(sdhci_runtime_resume_host);
2946
Rafael J. Wysocki162d6f92014-12-05 03:05:33 +01002947#endif /* CONFIG_PM */
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03002948
Pierre Ossmand129bce2006-03-24 03:18:17 -08002949/*****************************************************************************\
2950 * *
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002951 * Device allocation/registration *
Pierre Ossmand129bce2006-03-24 03:18:17 -08002952 * *
2953\*****************************************************************************/
2954
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002955struct sdhci_host *sdhci_alloc_host(struct device *dev,
2956 size_t priv_size)
Pierre Ossmand129bce2006-03-24 03:18:17 -08002957{
Pierre Ossmand129bce2006-03-24 03:18:17 -08002958 struct mmc_host *mmc;
2959 struct sdhci_host *host;
2960
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002961 WARN_ON(dev == NULL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002962
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002963 mmc = mmc_alloc_host(sizeof(struct sdhci_host) + priv_size, dev);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002964 if (!mmc)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002965 return ERR_PTR(-ENOMEM);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002966
2967 host = mmc_priv(mmc);
2968 host->mmc = mmc;
Adrian Hunterbf60e592016-02-09 16:12:35 +02002969 host->mmc_host_ops = sdhci_ops;
2970 mmc->ops = &host->mmc_host_ops;
Pierre Ossmand129bce2006-03-24 03:18:17 -08002971
Adrian Hunter8cb851a2016-06-29 16:24:16 +03002972 host->flags = SDHCI_SIGNALING_330;
2973
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002974 return host;
2975}
Pierre Ossman8a4da142006-10-04 02:15:40 -07002976
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01002977EXPORT_SYMBOL_GPL(sdhci_alloc_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08002978
Alexandre Courbot7b913692016-03-07 11:07:55 +09002979static int sdhci_set_dma_mask(struct sdhci_host *host)
2980{
2981 struct mmc_host *mmc = host->mmc;
2982 struct device *dev = mmc_dev(mmc);
2983 int ret = -EINVAL;
2984
2985 if (host->quirks2 & SDHCI_QUIRK2_BROKEN_64_BIT_DMA)
2986 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2987
2988 /* Try 64-bit mask if hardware is capable of it */
2989 if (host->flags & SDHCI_USE_64_BIT_DMA) {
2990 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64));
2991 if (ret) {
2992 pr_warn("%s: Failed to set 64-bit DMA mask.\n",
2993 mmc_hostname(mmc));
2994 host->flags &= ~SDHCI_USE_64_BIT_DMA;
2995 }
2996 }
2997
2998 /* 32-bit mask as default & fallback */
2999 if (ret) {
3000 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
3001 if (ret)
3002 pr_warn("%s: Failed to set 32-bit DMA mask.\n",
3003 mmc_hostname(mmc));
3004 }
3005
3006 return ret;
3007}
3008
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003009void __sdhci_read_caps(struct sdhci_host *host, u16 *ver, u32 *caps, u32 *caps1)
3010{
3011 u16 v;
Zach Brown92e0c442016-11-02 10:26:16 -05003012 u64 dt_caps_mask = 0;
3013 u64 dt_caps = 0;
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003014
3015 if (host->read_caps)
3016 return;
3017
3018 host->read_caps = true;
3019
3020 if (debug_quirks)
3021 host->quirks = debug_quirks;
3022
3023 if (debug_quirks2)
3024 host->quirks2 = debug_quirks2;
3025
3026 sdhci_do_reset(host, SDHCI_RESET_ALL);
3027
Zach Brown92e0c442016-11-02 10:26:16 -05003028 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3029 "sdhci-caps-mask", &dt_caps_mask);
3030 of_property_read_u64(mmc_dev(host->mmc)->of_node,
3031 "sdhci-caps", &dt_caps);
3032
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003033 v = ver ? *ver : sdhci_readw(host, SDHCI_HOST_VERSION);
3034 host->version = (v & SDHCI_SPEC_VER_MASK) >> SDHCI_SPEC_VER_SHIFT;
3035
3036 if (host->quirks & SDHCI_QUIRK_MISSING_CAPS)
3037 return;
3038
Zach Brown92e0c442016-11-02 10:26:16 -05003039 if (caps) {
3040 host->caps = *caps;
3041 } else {
3042 host->caps = sdhci_readl(host, SDHCI_CAPABILITIES);
3043 host->caps &= ~lower_32_bits(dt_caps_mask);
3044 host->caps |= lower_32_bits(dt_caps);
3045 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003046
3047 if (host->version < SDHCI_SPEC_300)
3048 return;
3049
Zach Brown92e0c442016-11-02 10:26:16 -05003050 if (caps1) {
3051 host->caps1 = *caps1;
3052 } else {
3053 host->caps1 = sdhci_readl(host, SDHCI_CAPABILITIES_1);
3054 host->caps1 &= ~upper_32_bits(dt_caps_mask);
3055 host->caps1 |= upper_32_bits(dt_caps);
3056 }
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003057}
3058EXPORT_SYMBOL_GPL(__sdhci_read_caps);
3059
Adrian Hunter52f53362016-06-29 16:24:15 +03003060int sdhci_setup_host(struct sdhci_host *host)
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003061{
3062 struct mmc_host *mmc;
Arindam Nathf2119df2011-05-05 12:18:57 +05303063 u32 max_current_caps;
3064 unsigned int ocr_avail;
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003065 unsigned int override_timeout_clk;
Dong Aisheng59241752015-07-22 20:53:07 +08003066 u32 max_clk;
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003067 int ret;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003068
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003069 WARN_ON(host == NULL);
3070 if (host == NULL)
3071 return -EINVAL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003072
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003073 mmc = host->mmc;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003074
Jon Hunterefba1422016-07-12 14:53:36 +01003075 /*
3076 * If there are external regulators, get them. Note this must be done
3077 * early before resetting the host and reading the capabilities so that
3078 * the host can take the appropriate action if regulators are not
3079 * available.
3080 */
3081 ret = mmc_regulator_get_supply(mmc);
3082 if (ret == -EPROBE_DEFER)
3083 return ret;
3084
Adrian Hunter6132a3b2016-06-29 16:24:18 +03003085 sdhci_read_caps(host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003086
Adrian Hunterf5fa92e2014-09-24 10:27:32 +03003087 override_timeout_clk = host->timeout_clk;
3088
Zhangfei Gao85105c52010-08-06 07:10:01 +08003089 if (host->version > SDHCI_SPEC_300) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003090 pr_err("%s: Unknown controller version (%d). You may experience problems.\n",
3091 mmc_hostname(mmc), host->version);
Pierre Ossman4a965502006-06-30 02:22:29 -07003092 }
3093
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003094 if (host->quirks & SDHCI_QUIRK_FORCE_DMA)
Richard Röjforsa13abc72009-09-22 16:45:30 -07003095 host->flags |= SDHCI_USE_SDMA;
Adrian Hunter28da3582016-06-29 16:24:17 +03003096 else if (!(host->caps & SDHCI_CAN_DO_SDMA))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003097 DBG("Controller doesn't have SDMA capability\n");
Pierre Ossman67435272006-06-30 02:22:31 -07003098 else
Richard Röjforsa13abc72009-09-22 16:45:30 -07003099 host->flags |= SDHCI_USE_SDMA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003100
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003101 if ((host->quirks & SDHCI_QUIRK_BROKEN_DMA) &&
Richard Röjforsa13abc72009-09-22 16:45:30 -07003102 (host->flags & SDHCI_USE_SDMA)) {
Rolf Eike Beercee687c2007-11-02 15:22:30 +01003103 DBG("Disabling DMA as it is marked broken\n");
Richard Röjforsa13abc72009-09-22 16:45:30 -07003104 host->flags &= ~SDHCI_USE_SDMA;
Feng Tang7c168e32007-09-30 12:44:18 +02003105 }
3106
Arindam Nathf2119df2011-05-05 12:18:57 +05303107 if ((host->version >= SDHCI_SPEC_200) &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003108 (host->caps & SDHCI_CAN_DO_ADMA2))
Richard Röjforsa13abc72009-09-22 16:45:30 -07003109 host->flags |= SDHCI_USE_ADMA;
Pierre Ossman2134a922008-06-28 18:28:51 +02003110
3111 if ((host->quirks & SDHCI_QUIRK_BROKEN_ADMA) &&
3112 (host->flags & SDHCI_USE_ADMA)) {
3113 DBG("Disabling ADMA as it is marked broken\n");
3114 host->flags &= ~SDHCI_USE_ADMA;
3115 }
3116
Adrian Huntere57a5f62014-11-04 12:42:46 +02003117 /*
3118 * It is assumed that a 64-bit capable device has set a 64-bit DMA mask
3119 * and *must* do 64-bit DMA. A driver has the opportunity to change
3120 * that during the first call to ->enable_dma(). Similarly
3121 * SDHCI_QUIRK2_BROKEN_64_BIT_DMA must be left to the drivers to
3122 * implement.
3123 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003124 if (host->caps & SDHCI_CAN_64BIT)
Adrian Huntere57a5f62014-11-04 12:42:46 +02003125 host->flags |= SDHCI_USE_64_BIT_DMA;
3126
Richard Röjforsa13abc72009-09-22 16:45:30 -07003127 if (host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA)) {
Alexandre Courbot7b913692016-03-07 11:07:55 +09003128 ret = sdhci_set_dma_mask(host);
3129
3130 if (!ret && host->ops->enable_dma)
3131 ret = host->ops->enable_dma(host);
3132
3133 if (ret) {
3134 pr_warn("%s: No suitable DMA available - falling back to PIO\n",
3135 mmc_hostname(mmc));
3136 host->flags &= ~(SDHCI_USE_SDMA | SDHCI_USE_ADMA);
3137
3138 ret = 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003139 }
3140 }
3141
Adrian Huntere57a5f62014-11-04 12:42:46 +02003142 /* SDMA does not support 64-bit DMA */
3143 if (host->flags & SDHCI_USE_64_BIT_DMA)
3144 host->flags &= ~SDHCI_USE_SDMA;
3145
Pierre Ossman2134a922008-06-28 18:28:51 +02003146 if (host->flags & SDHCI_USE_ADMA) {
Russell Kinge66e61c2016-01-26 13:39:55 +00003147 dma_addr_t dma;
3148 void *buf;
3149
Pierre Ossman2134a922008-06-28 18:28:51 +02003150 /*
Adrian Hunter76fe3792014-11-04 12:42:42 +02003151 * The DMA descriptor table size is calculated as the maximum
3152 * number of segments times 2, to allow for an alignment
3153 * descriptor for each segment, plus 1 for a nop end descriptor,
3154 * all multipled by the descriptor size.
Pierre Ossman2134a922008-06-28 18:28:51 +02003155 */
Adrian Huntere57a5f62014-11-04 12:42:46 +02003156 if (host->flags & SDHCI_USE_64_BIT_DMA) {
3157 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3158 SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003159 host->desc_sz = SDHCI_ADMA2_64_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003160 } else {
3161 host->adma_table_sz = (SDHCI_MAX_SEGS * 2 + 1) *
3162 SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003163 host->desc_sz = SDHCI_ADMA2_32_DESC_SZ;
Adrian Huntere57a5f62014-11-04 12:42:46 +02003164 }
Russell Kinge66e61c2016-01-26 13:39:55 +00003165
Adrian Hunter04a5ae62015-11-26 14:00:49 +02003166 host->align_buffer_sz = SDHCI_MAX_SEGS * SDHCI_ADMA2_ALIGN;
Russell Kinge66e61c2016-01-26 13:39:55 +00003167 buf = dma_alloc_coherent(mmc_dev(mmc), host->align_buffer_sz +
3168 host->adma_table_sz, &dma, GFP_KERNEL);
3169 if (!buf) {
Joe Perches66061102014-09-12 14:56:56 -07003170 pr_warn("%s: Unable to allocate ADMA buffers - falling back to standard DMA\n",
Pierre Ossman2134a922008-06-28 18:28:51 +02003171 mmc_hostname(mmc));
3172 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003173 } else if ((dma + host->align_buffer_sz) &
3174 (SDHCI_ADMA2_DESC_ALIGN - 1)) {
Joe Perches66061102014-09-12 14:56:56 -07003175 pr_warn("%s: unable to allocate aligned ADMA descriptor\n",
3176 mmc_hostname(mmc));
Russell Kingd1e49f72014-04-25 12:58:34 +01003177 host->flags &= ~SDHCI_USE_ADMA;
Russell Kinge66e61c2016-01-26 13:39:55 +00003178 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3179 host->adma_table_sz, buf, dma);
3180 } else {
3181 host->align_buffer = buf;
3182 host->align_addr = dma;
Russell Kingedd63fc2016-01-26 13:39:50 +00003183
Russell Kinge66e61c2016-01-26 13:39:55 +00003184 host->adma_table = buf + host->align_buffer_sz;
3185 host->adma_addr = dma + host->align_buffer_sz;
3186 }
Pierre Ossman2134a922008-06-28 18:28:51 +02003187 }
3188
Pierre Ossman76591502008-07-21 00:32:11 +02003189 /*
3190 * If we use DMA, then it's up to the caller to set the DMA
3191 * mask, but PIO does not need the hw shim so we set a new
3192 * mask here in that case.
3193 */
Richard Röjforsa13abc72009-09-22 16:45:30 -07003194 if (!(host->flags & (SDHCI_USE_SDMA | SDHCI_USE_ADMA))) {
Pierre Ossman76591502008-07-21 00:32:11 +02003195 host->dma_mask = DMA_BIT_MASK(64);
Markus Mayer4e743f12014-07-03 13:27:42 -07003196 mmc_dev(mmc)->dma_mask = &host->dma_mask;
Pierre Ossman76591502008-07-21 00:32:11 +02003197 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003198
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003199 if (host->version >= SDHCI_SPEC_300)
Adrian Hunter28da3582016-06-29 16:24:17 +03003200 host->max_clk = (host->caps & SDHCI_CLOCK_V3_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003201 >> SDHCI_CLOCK_BASE_SHIFT;
3202 else
Adrian Hunter28da3582016-06-29 16:24:17 +03003203 host->max_clk = (host->caps & SDHCI_CLOCK_BASE_MASK)
Zhangfei Gaoc4687d52010-08-20 14:02:36 -04003204 >> SDHCI_CLOCK_BASE_SHIFT;
3205
Pierre Ossmand129bce2006-03-24 03:18:17 -08003206 host->max_clk *= 1000000;
Anton Vorontsovf27f47e2010-05-26 14:41:53 -07003207 if (host->max_clk == 0 || host->quirks &
3208 SDHCI_QUIRK_CAP_CLOCK_BASE_BROKEN) {
Ben Dooks4240ff02009-03-17 00:13:57 +03003209 if (!host->ops->get_max_clock) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003210 pr_err("%s: Hardware doesn't specify base clock frequency.\n",
3211 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003212 ret = -ENODEV;
3213 goto undma;
Ben Dooks4240ff02009-03-17 00:13:57 +03003214 }
3215 host->max_clk = host->ops->get_max_clock(host);
3216 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003217
3218 /*
Arindam Nathc3ed3872011-05-05 12:19:06 +05303219 * In case of Host Controller v3.00, find out whether clock
3220 * multiplier is supported.
3221 */
Adrian Hunter28da3582016-06-29 16:24:17 +03003222 host->clk_mul = (host->caps1 & SDHCI_CLOCK_MUL_MASK) >>
Arindam Nathc3ed3872011-05-05 12:19:06 +05303223 SDHCI_CLOCK_MUL_SHIFT;
3224
3225 /*
3226 * In case the value in Clock Multiplier is 0, then programmable
3227 * clock mode is not supported, otherwise the actual clock
3228 * multiplier is one more than the value of Clock Multiplier
3229 * in the Capabilities Register.
3230 */
3231 if (host->clk_mul)
3232 host->clk_mul += 1;
3233
3234 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003235 * Set host parameters.
3236 */
Dong Aisheng59241752015-07-22 20:53:07 +08003237 max_clk = host->max_clk;
3238
Marek Szyprowskice5f0362010-08-10 18:01:56 -07003239 if (host->ops->get_min_clock)
Anton Vorontsova9e58f22009-07-29 15:04:16 -07003240 mmc->f_min = host->ops->get_min_clock(host);
Arindam Nathc3ed3872011-05-05 12:19:06 +05303241 else if (host->version >= SDHCI_SPEC_300) {
3242 if (host->clk_mul) {
3243 mmc->f_min = (host->max_clk * host->clk_mul) / 1024;
Dong Aisheng59241752015-07-22 20:53:07 +08003244 max_clk = host->max_clk * host->clk_mul;
Arindam Nathc3ed3872011-05-05 12:19:06 +05303245 } else
3246 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_300;
3247 } else
Zhangfei Gao03975262010-09-20 15:15:18 -04003248 mmc->f_min = host->max_clk / SDHCI_MAX_DIV_SPEC_200;
Philip Rakity15ec4462010-11-19 16:48:39 -05003249
Adrian Hunterd310ae42016-04-12 14:25:07 +03003250 if (!mmc->f_max || mmc->f_max > max_clk)
Dong Aisheng59241752015-07-22 20:53:07 +08003251 mmc->f_max = max_clk;
3252
Aisheng Dong28aab052014-08-27 15:26:31 +08003253 if (!(host->quirks & SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK)) {
Adrian Hunter28da3582016-06-29 16:24:17 +03003254 host->timeout_clk = (host->caps & SDHCI_TIMEOUT_CLK_MASK) >>
Aisheng Dong28aab052014-08-27 15:26:31 +08003255 SDHCI_TIMEOUT_CLK_SHIFT;
3256 if (host->timeout_clk == 0) {
3257 if (host->ops->get_timeout_clock) {
3258 host->timeout_clk =
3259 host->ops->get_timeout_clock(host);
3260 } else {
3261 pr_err("%s: Hardware doesn't specify timeout clock frequency.\n",
3262 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003263 ret = -ENODEV;
3264 goto undma;
Aisheng Dong28aab052014-08-27 15:26:31 +08003265 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003266 }
Andy Shevchenko272308c2011-08-03 18:36:00 +03003267
Adrian Hunter28da3582016-06-29 16:24:17 +03003268 if (host->caps & SDHCI_TIMEOUT_CLK_UNIT)
Aisheng Dong28aab052014-08-27 15:26:31 +08003269 host->timeout_clk *= 1000;
Andy Shevchenko272308c2011-08-03 18:36:00 +03003270
Adrian Hunter99513622016-03-07 13:33:55 +02003271 if (override_timeout_clk)
3272 host->timeout_clk = override_timeout_clk;
3273
Aisheng Dong28aab052014-08-27 15:26:31 +08003274 mmc->max_busy_timeout = host->ops->get_max_timeout_count ?
Aisheng Donga6ff5ae2014-08-27 15:26:27 +08003275 host->ops->get_max_timeout_count(host) : 1 << 27;
Aisheng Dong28aab052014-08-27 15:26:31 +08003276 mmc->max_busy_timeout /= host->timeout_clk;
3277 }
Adrian Hunter58d12462011-06-28 17:16:03 +03003278
Andrei Warkentine89d4562011-05-23 15:06:37 -05003279 mmc->caps |= MMC_CAP_SDIO_IRQ | MMC_CAP_ERASE | MMC_CAP_CMD23;
Russell King781e9892014-04-25 12:55:46 +01003280 mmc->caps2 |= MMC_CAP2_SDIO_IRQ_NOTHREAD;
Andrei Warkentine89d4562011-05-23 15:06:37 -05003281
3282 if (host->quirks & SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12)
3283 host->flags |= SDHCI_AUTO_CMD12;
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003284
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003285 /* Auto-CMD23 stuff only works in ADMA or PIO. */
Andrei Warkentin4f3d3e92011-05-25 10:42:50 -04003286 if ((host->version >= SDHCI_SPEC_300) &&
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003287 ((host->flags & SDHCI_USE_ADMA) ||
Scott Branden3bfa6f02015-02-09 16:06:28 -08003288 !(host->flags & SDHCI_USE_SDMA)) &&
3289 !(host->quirks2 & SDHCI_QUIRK2_ACMD23_BROKEN)) {
Andrei Warkentin8edf63712011-05-23 15:06:39 -05003290 host->flags |= SDHCI_AUTO_CMD23;
3291 DBG("%s: Auto-CMD23 available\n", mmc_hostname(mmc));
3292 } else {
3293 DBG("%s: Auto-CMD23 unavailable\n", mmc_hostname(mmc));
3294 }
3295
Philip Rakity15ec4462010-11-19 16:48:39 -05003296 /*
3297 * A controller may support 8-bit width, but the board itself
3298 * might not have the pins brought out. Boards that support
3299 * 8-bit width must set "mmc->caps |= MMC_CAP_8_BIT_DATA;" in
3300 * their platform code before calling sdhci_add_host(), and we
3301 * won't assume 8-bit width for hosts without that CAP.
3302 */
Anton Vorontsov5fe23c72009-06-18 00:14:08 +04003303 if (!(host->quirks & SDHCI_QUIRK_FORCE_1_BIT_DATA))
Philip Rakity15ec4462010-11-19 16:48:39 -05003304 mmc->caps |= MMC_CAP_4_BIT_DATA;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003305
Jerry Huang63ef5d82012-10-25 13:47:19 +08003306 if (host->quirks2 & SDHCI_QUIRK2_HOST_NO_CMD23)
3307 mmc->caps &= ~MMC_CAP_CMD23;
3308
Adrian Hunter28da3582016-06-29 16:24:17 +03003309 if (host->caps & SDHCI_CAN_DO_HISPD)
Zhangfei Gaoa29e7e12010-08-16 21:15:32 -04003310 mmc->caps |= MMC_CAP_SD_HIGHSPEED | MMC_CAP_MMC_HIGHSPEED;
Pierre Ossmancd9277c2007-02-18 12:07:47 +01003311
Jaehoon Chung176d1ed2010-09-27 09:42:20 +01003312 if ((host->quirks & SDHCI_QUIRK_BROKEN_CARD_DETECTION) &&
Jaehoon Chung860951c2016-06-21 10:13:26 +09003313 mmc_card_is_removable(mmc) &&
Arnd Bergmann287980e2016-05-27 23:23:25 +02003314 mmc_gpio_get_cd(host->mmc) < 0)
Anton Vorontsov68d1fb72009-03-17 00:13:52 +03003315 mmc->caps |= MMC_CAP_NEEDS_POLL;
3316
Philip Rakity6231f3d2012-07-23 15:56:23 -07003317 /* If vqmmc regulator and no 1.8V signalling, then there's no UHS */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003318 if (!IS_ERR(mmc->supply.vqmmc)) {
3319 ret = regulator_enable(mmc->supply.vqmmc);
3320 if (!regulator_is_supported_voltage(mmc->supply.vqmmc, 1700000,
3321 1950000))
Adrian Hunter28da3582016-06-29 16:24:17 +03003322 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 |
3323 SDHCI_SUPPORT_SDR50 |
3324 SDHCI_SUPPORT_DDR50);
Chris Balla3361ab2013-03-11 17:51:53 -04003325 if (ret) {
3326 pr_warn("%s: Failed to enable vqmmc regulator: %d\n",
3327 mmc_hostname(mmc), ret);
Adrian Hunter4bb74312014-11-06 15:19:04 +02003328 mmc->supply.vqmmc = ERR_PTR(-EINVAL);
Chris Balla3361ab2013-03-11 17:51:53 -04003329 }
Kevin Liu8363c372012-11-17 17:55:51 -05003330 }
Philip Rakity6231f3d2012-07-23 15:56:23 -07003331
Adrian Hunter28da3582016-06-29 16:24:17 +03003332 if (host->quirks2 & SDHCI_QUIRK2_NO_1_8_V) {
3333 host->caps1 &= ~(SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3334 SDHCI_SUPPORT_DDR50);
3335 }
Daniel Drake6a661802012-11-25 13:01:19 -05003336
Al Cooper4188bba2012-03-16 15:54:17 -04003337 /* Any UHS-I mode in caps implies SDR12 and SDR25 support. */
Adrian Hunter28da3582016-06-29 16:24:17 +03003338 if (host->caps1 & (SDHCI_SUPPORT_SDR104 | SDHCI_SUPPORT_SDR50 |
3339 SDHCI_SUPPORT_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303340 mmc->caps |= MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25;
3341
3342 /* SDR104 supports also implies SDR50 support */
Adrian Hunter28da3582016-06-29 16:24:17 +03003343 if (host->caps1 & SDHCI_SUPPORT_SDR104) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303344 mmc->caps |= MMC_CAP_UHS_SDR104 | MMC_CAP_UHS_SDR50;
Giuseppe CAVALLARO156e14b2013-06-12 08:16:38 +02003345 /* SD3.0: SDR104 is supported so (for eMMC) the caps2
3346 * field can be promoted to support HS200.
3347 */
Adrian Hunter549c0b12014-11-06 15:19:05 +02003348 if (!(host->quirks2 & SDHCI_QUIRK2_BROKEN_HS200))
David Cohen13868bf2013-10-29 10:58:26 -07003349 mmc->caps2 |= MMC_CAP2_HS200;
Adrian Hunter28da3582016-06-29 16:24:17 +03003350 } else if (host->caps1 & SDHCI_SUPPORT_SDR50) {
Arindam Nathf2119df2011-05-05 12:18:57 +05303351 mmc->caps |= MMC_CAP_UHS_SDR50;
Adrian Hunter28da3582016-06-29 16:24:17 +03003352 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303353
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003354 if (host->quirks2 & SDHCI_QUIRK2_CAPS_BIT63_FOR_HS400 &&
Adrian Hunter28da3582016-06-29 16:24:17 +03003355 (host->caps1 & SDHCI_SUPPORT_HS400))
Adrian Huntere9fb05d2014-11-06 15:19:06 +02003356 mmc->caps2 |= MMC_CAP2_HS400;
3357
Adrian Hunter549c0b12014-11-06 15:19:05 +02003358 if ((mmc->caps2 & MMC_CAP2_HSX00_1_2V) &&
3359 (IS_ERR(mmc->supply.vqmmc) ||
3360 !regulator_is_supported_voltage(mmc->supply.vqmmc, 1100000,
3361 1300000)))
3362 mmc->caps2 &= ~MMC_CAP2_HSX00_1_2V;
3363
Adrian Hunter28da3582016-06-29 16:24:17 +03003364 if ((host->caps1 & SDHCI_SUPPORT_DDR50) &&
3365 !(host->quirks2 & SDHCI_QUIRK2_BROKEN_DDR50))
Arindam Nathf2119df2011-05-05 12:18:57 +05303366 mmc->caps |= MMC_CAP_UHS_DDR50;
3367
Girish K S069c9f12012-01-06 09:56:39 +05303368 /* Does the host need tuning for SDR50? */
Adrian Hunter28da3582016-06-29 16:24:17 +03003369 if (host->caps1 & SDHCI_USE_SDR50_TUNING)
Arindam Nathb513ea22011-05-05 12:19:04 +05303370 host->flags |= SDHCI_SDR50_NEEDS_TUNING;
3371
Arindam Nathd6d50a12011-05-05 12:18:59 +05303372 /* Driver Type(s) (A, C, D) supported by the host */
Adrian Hunter28da3582016-06-29 16:24:17 +03003373 if (host->caps1 & SDHCI_DRIVER_TYPE_A)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303374 mmc->caps |= MMC_CAP_DRIVER_TYPE_A;
Adrian Hunter28da3582016-06-29 16:24:17 +03003375 if (host->caps1 & SDHCI_DRIVER_TYPE_C)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303376 mmc->caps |= MMC_CAP_DRIVER_TYPE_C;
Adrian Hunter28da3582016-06-29 16:24:17 +03003377 if (host->caps1 & SDHCI_DRIVER_TYPE_D)
Arindam Nathd6d50a12011-05-05 12:18:59 +05303378 mmc->caps |= MMC_CAP_DRIVER_TYPE_D;
3379
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303380 /* Initial value for re-tuning timer count */
Adrian Hunter28da3582016-06-29 16:24:17 +03003381 host->tuning_count = (host->caps1 & SDHCI_RETUNING_TIMER_COUNT_MASK) >>
3382 SDHCI_RETUNING_TIMER_COUNT_SHIFT;
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303383
3384 /*
3385 * In case Re-tuning Timer is not disabled, the actual value of
3386 * re-tuning timer will be 2 ^ (n - 1).
3387 */
3388 if (host->tuning_count)
3389 host->tuning_count = 1 << (host->tuning_count - 1);
3390
3391 /* Re-tuning mode supported by the Host Controller */
Adrian Hunter28da3582016-06-29 16:24:17 +03003392 host->tuning_mode = (host->caps1 & SDHCI_RETUNING_MODE_MASK) >>
Arindam Nathcf2b5ee2011-05-05 12:19:07 +05303393 SDHCI_RETUNING_MODE_SHIFT;
3394
Takashi Iwai8f230f42010-12-08 10:04:30 +01003395 ocr_avail = 0;
Philip Rakitybad37e12012-05-27 18:36:44 -07003396
Arindam Nathf2119df2011-05-05 12:18:57 +05303397 /*
3398 * According to SD Host Controller spec v3.00, if the Host System
3399 * can afford more than 150mA, Host Driver should set XPC to 1. Also
3400 * the value is meaningful only if Voltage Support in the Capabilities
3401 * register is set. The actual current value is 4 times the register
3402 * value.
3403 */
3404 max_current_caps = sdhci_readl(host, SDHCI_MAX_CURRENT);
Tim Kryger3a48edc2014-06-13 10:13:56 -07003405 if (!max_current_caps && !IS_ERR(mmc->supply.vmmc)) {
Chuanxiao.Dongae906032014-08-01 14:00:13 +08003406 int curr = regulator_get_current_limit(mmc->supply.vmmc);
Philip Rakitybad37e12012-05-27 18:36:44 -07003407 if (curr > 0) {
3408
3409 /* convert to SDHCI_MAX_CURRENT format */
3410 curr = curr/1000; /* convert to mA */
3411 curr = curr/SDHCI_MAX_CURRENT_MULTIPLIER;
3412
3413 curr = min_t(u32, curr, SDHCI_MAX_CURRENT_LIMIT);
3414 max_current_caps =
3415 (curr << SDHCI_MAX_CURRENT_330_SHIFT) |
3416 (curr << SDHCI_MAX_CURRENT_300_SHIFT) |
3417 (curr << SDHCI_MAX_CURRENT_180_SHIFT);
3418 }
3419 }
Arindam Nathf2119df2011-05-05 12:18:57 +05303420
Adrian Hunter28da3582016-06-29 16:24:17 +03003421 if (host->caps & SDHCI_CAN_VDD_330) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003422 ocr_avail |= MMC_VDD_32_33 | MMC_VDD_33_34;
Arindam Nathf2119df2011-05-05 12:18:57 +05303423
Aaron Lu55c46652012-07-04 13:31:48 +08003424 mmc->max_current_330 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303425 SDHCI_MAX_CURRENT_330_MASK) >>
3426 SDHCI_MAX_CURRENT_330_SHIFT) *
3427 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303428 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003429 if (host->caps & SDHCI_CAN_VDD_300) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003430 ocr_avail |= MMC_VDD_29_30 | MMC_VDD_30_31;
Arindam Nathf2119df2011-05-05 12:18:57 +05303431
Aaron Lu55c46652012-07-04 13:31:48 +08003432 mmc->max_current_300 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303433 SDHCI_MAX_CURRENT_300_MASK) >>
3434 SDHCI_MAX_CURRENT_300_SHIFT) *
3435 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303436 }
Adrian Hunter28da3582016-06-29 16:24:17 +03003437 if (host->caps & SDHCI_CAN_VDD_180) {
Takashi Iwai8f230f42010-12-08 10:04:30 +01003438 ocr_avail |= MMC_VDD_165_195;
3439
Aaron Lu55c46652012-07-04 13:31:48 +08003440 mmc->max_current_180 = ((max_current_caps &
Arindam Nathf2119df2011-05-05 12:18:57 +05303441 SDHCI_MAX_CURRENT_180_MASK) >>
3442 SDHCI_MAX_CURRENT_180_SHIFT) *
3443 SDHCI_MAX_CURRENT_MULTIPLIER;
Arindam Nathf2119df2011-05-05 12:18:57 +05303444 }
3445
Ulf Hansson5fd26c72015-06-05 11:40:08 +02003446 /* If OCR set by host, use it instead. */
3447 if (host->ocr_mask)
3448 ocr_avail = host->ocr_mask;
3449
3450 /* If OCR set by external regulators, give it highest prio. */
Tim Kryger3a48edc2014-06-13 10:13:56 -07003451 if (mmc->ocr_avail)
Tim Kryger52221612014-06-25 00:25:34 -07003452 ocr_avail = mmc->ocr_avail;
Tim Kryger3a48edc2014-06-13 10:13:56 -07003453
Takashi Iwai8f230f42010-12-08 10:04:30 +01003454 mmc->ocr_avail = ocr_avail;
3455 mmc->ocr_avail_sdio = ocr_avail;
3456 if (host->ocr_avail_sdio)
3457 mmc->ocr_avail_sdio &= host->ocr_avail_sdio;
3458 mmc->ocr_avail_sd = ocr_avail;
3459 if (host->ocr_avail_sd)
3460 mmc->ocr_avail_sd &= host->ocr_avail_sd;
3461 else /* normal SD controllers don't support 1.8V */
3462 mmc->ocr_avail_sd &= ~MMC_VDD_165_195;
3463 mmc->ocr_avail_mmc = ocr_avail;
3464 if (host->ocr_avail_mmc)
3465 mmc->ocr_avail_mmc &= host->ocr_avail_mmc;
Pierre Ossman146ad662006-06-30 02:22:23 -07003466
3467 if (mmc->ocr_avail == 0) {
Marek Vasut2e4456f2015-11-18 10:47:02 +01003468 pr_err("%s: Hardware doesn't report any support voltages.\n",
3469 mmc_hostname(mmc));
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003470 ret = -ENODEV;
3471 goto unreg;
Pierre Ossman146ad662006-06-30 02:22:23 -07003472 }
3473
Adrian Hunter8cb851a2016-06-29 16:24:16 +03003474 if ((mmc->caps & (MMC_CAP_UHS_SDR12 | MMC_CAP_UHS_SDR25 |
3475 MMC_CAP_UHS_SDR50 | MMC_CAP_UHS_SDR104 |
3476 MMC_CAP_UHS_DDR50 | MMC_CAP_1_8V_DDR)) ||
3477 (mmc->caps2 & (MMC_CAP2_HS200_1_8V_SDR | MMC_CAP2_HS400_1_8V)))
3478 host->flags |= SDHCI_SIGNALING_180;
3479
3480 if (mmc->caps2 & MMC_CAP2_HSX00_1_2V)
3481 host->flags |= SDHCI_SIGNALING_120;
3482
Pierre Ossmand129bce2006-03-24 03:18:17 -08003483 spin_lock_init(&host->lock);
3484
3485 /*
Pierre Ossman2134a922008-06-28 18:28:51 +02003486 * Maximum number of segments. Depends on if the hardware
3487 * can do scatter/gather or not.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003488 */
Pierre Ossman2134a922008-06-28 18:28:51 +02003489 if (host->flags & SDHCI_USE_ADMA)
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003490 mmc->max_segs = SDHCI_MAX_SEGS;
Richard Röjforsa13abc72009-09-22 16:45:30 -07003491 else if (host->flags & SDHCI_USE_SDMA)
Martin K. Petersena36274e2010-09-10 01:33:59 -04003492 mmc->max_segs = 1;
Pierre Ossman2134a922008-06-28 18:28:51 +02003493 else /* PIO */
Adrian Hunter4fb213f2014-11-04 12:42:43 +02003494 mmc->max_segs = SDHCI_MAX_SEGS;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003495
3496 /*
Adrian Hunterac005312014-12-05 19:25:28 +02003497 * Maximum number of sectors in one transfer. Limited by SDMA boundary
3498 * size (512KiB). Note some tuning modes impose a 4MiB limit, but this
3499 * is less anyway.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003500 */
Pierre Ossman55db8902006-11-21 17:55:45 +01003501 mmc->max_req_size = 524288;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003502
3503 /*
3504 * Maximum segment size. Could be one segment with the maximum number
Pierre Ossman2134a922008-06-28 18:28:51 +02003505 * of bytes. When doing hardware scatter/gather, each entry cannot
3506 * be larger than 64 KiB though.
Pierre Ossmand129bce2006-03-24 03:18:17 -08003507 */
Olof Johansson30652aa2011-01-01 18:37:32 -06003508 if (host->flags & SDHCI_USE_ADMA) {
3509 if (host->quirks & SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC)
3510 mmc->max_seg_size = 65535;
3511 else
3512 mmc->max_seg_size = 65536;
3513 } else {
Pierre Ossman2134a922008-06-28 18:28:51 +02003514 mmc->max_seg_size = mmc->max_req_size;
Olof Johansson30652aa2011-01-01 18:37:32 -06003515 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003516
3517 /*
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003518 * Maximum block size. This varies from controller to controller and
3519 * is specified in the capabilities register.
3520 */
Anton Vorontsov0633f652009-03-17 00:14:03 +03003521 if (host->quirks & SDHCI_QUIRK_FORCE_BLK_SZ_2048) {
3522 mmc->max_blk_size = 2;
3523 } else {
Adrian Hunter28da3582016-06-29 16:24:17 +03003524 mmc->max_blk_size = (host->caps & SDHCI_MAX_BLOCK_MASK) >>
Anton Vorontsov0633f652009-03-17 00:14:03 +03003525 SDHCI_MAX_BLOCK_SHIFT;
3526 if (mmc->max_blk_size >= 3) {
Joe Perches66061102014-09-12 14:56:56 -07003527 pr_warn("%s: Invalid maximum block size, assuming 512 bytes\n",
3528 mmc_hostname(mmc));
Anton Vorontsov0633f652009-03-17 00:14:03 +03003529 mmc->max_blk_size = 0;
3530 }
3531 }
3532
3533 mmc->max_blk_size = 512 << mmc->max_blk_size;
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +01003534
3535 /*
Pierre Ossman55db8902006-11-21 17:55:45 +01003536 * Maximum block count.
3537 */
Ben Dooks1388eef2009-06-14 12:40:53 +01003538 mmc->max_blk_count = (host->quirks & SDHCI_QUIRK_NO_MULTIBLOCK) ? 1 : 65535;
Pierre Ossman55db8902006-11-21 17:55:45 +01003539
Adrian Hunter52f53362016-06-29 16:24:15 +03003540 return 0;
3541
3542unreg:
3543 if (!IS_ERR(mmc->supply.vqmmc))
3544 regulator_disable(mmc->supply.vqmmc);
3545undma:
3546 if (host->align_buffer)
3547 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3548 host->adma_table_sz, host->align_buffer,
3549 host->align_addr);
3550 host->adma_table = NULL;
3551 host->align_buffer = NULL;
3552
3553 return ret;
3554}
3555EXPORT_SYMBOL_GPL(sdhci_setup_host);
3556
3557int __sdhci_add_host(struct sdhci_host *host)
3558{
3559 struct mmc_host *mmc = host->mmc;
3560 int ret;
3561
Pierre Ossman55db8902006-11-21 17:55:45 +01003562 /*
Pierre Ossmand129bce2006-03-24 03:18:17 -08003563 * Init tasklets.
3564 */
Pierre Ossmand129bce2006-03-24 03:18:17 -08003565 tasklet_init(&host->finish_tasklet,
3566 sdhci_tasklet_finish, (unsigned long)host);
3567
Al Viroe4cad1b2006-10-10 22:47:07 +01003568 setup_timer(&host->timer, sdhci_timeout_timer, (unsigned long)host);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003569 setup_timer(&host->data_timer, sdhci_timeout_data_timer,
3570 (unsigned long)host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003571
Adrian Hunter250fb7b42014-12-05 19:41:10 +02003572 init_waitqueue_head(&host->buf_ready_int);
Arindam Nathb513ea22011-05-05 12:19:04 +05303573
Shawn Guo2af502c2013-07-05 14:38:55 +08003574 sdhci_init(host, 0);
3575
Russell King781e9892014-04-25 12:55:46 +01003576 ret = request_threaded_irq(host->irq, sdhci_irq, sdhci_thread_irq,
3577 IRQF_SHARED, mmc_hostname(mmc), host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003578 if (ret) {
3579 pr_err("%s: Failed to request IRQ %d: %d\n",
3580 mmc_hostname(mmc), host->irq, ret);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003581 goto untasklet;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003582 }
Pierre Ossmand129bce2006-03-24 03:18:17 -08003583
Pierre Ossmand129bce2006-03-24 03:18:17 -08003584#ifdef CONFIG_MMC_DEBUG
3585 sdhci_dumpregs(host);
3586#endif
3587
Adrian Hunter061d17a2016-04-12 14:25:09 +03003588 ret = sdhci_led_register(host);
Mark Brown0fc81ee2012-07-02 14:26:15 +01003589 if (ret) {
3590 pr_err("%s: Failed to register LED device: %d\n",
3591 mmc_hostname(mmc), ret);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003592 goto unirq;
Mark Brown0fc81ee2012-07-02 14:26:15 +01003593 }
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003594
Pierre Ossman5f25a662006-10-04 02:15:39 -07003595 mmiowb();
3596
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003597 ret = mmc_add_host(mmc);
3598 if (ret)
3599 goto unled;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003600
Girish K Sa3c76eb2011-10-11 11:44:09 +05303601 pr_info("%s: SDHCI controller on %s [%s] using %s\n",
Kay Sieversd1b26862008-11-08 21:37:46 +01003602 mmc_hostname(mmc), host->hw_name, dev_name(mmc_dev(mmc)),
Adrian Huntere57a5f62014-11-04 12:42:46 +02003603 (host->flags & SDHCI_USE_ADMA) ?
3604 (host->flags & SDHCI_USE_64_BIT_DMA) ? "ADMA 64-bit" : "ADMA" :
Richard Röjforsa13abc72009-09-22 16:45:30 -07003605 (host->flags & SDHCI_USE_SDMA) ? "DMA" : "PIO");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003606
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003607 sdhci_enable_card_detection(host);
3608
Pierre Ossmand129bce2006-03-24 03:18:17 -08003609 return 0;
3610
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003611unled:
Adrian Hunter061d17a2016-04-12 14:25:09 +03003612 sdhci_led_unregister(host);
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003613unirq:
Russell King03231f92014-04-25 12:57:12 +01003614 sdhci_do_reset(host, SDHCI_RESET_ALL);
Russell Kingb537f942014-04-25 12:56:01 +01003615 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3616 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003617 free_irq(host->irq, host);
Pierre Ossman8ef1a142006-06-30 02:22:21 -07003618untasklet:
Pierre Ossmand129bce2006-03-24 03:18:17 -08003619 tasklet_kill(&host->finish_tasklet);
Adrian Hunter52f53362016-06-29 16:24:15 +03003620
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003621 if (!IS_ERR(mmc->supply.vqmmc))
3622 regulator_disable(mmc->supply.vqmmc);
Adrian Hunter52f53362016-06-29 16:24:15 +03003623
Adrian Huntereb5c20d2016-04-12 14:25:08 +03003624 if (host->align_buffer)
3625 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3626 host->adma_table_sz, host->align_buffer,
3627 host->align_addr);
3628 host->adma_table = NULL;
3629 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003630
3631 return ret;
3632}
Adrian Hunter52f53362016-06-29 16:24:15 +03003633EXPORT_SYMBOL_GPL(__sdhci_add_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003634
Adrian Hunter52f53362016-06-29 16:24:15 +03003635int sdhci_add_host(struct sdhci_host *host)
3636{
3637 int ret;
3638
3639 ret = sdhci_setup_host(host);
3640 if (ret)
3641 return ret;
3642
3643 return __sdhci_add_host(host);
3644}
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003645EXPORT_SYMBOL_GPL(sdhci_add_host);
3646
Pierre Ossman1e728592008-04-16 19:13:13 +02003647void sdhci_remove_host(struct sdhci_host *host, int dead)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003648{
Tim Kryger3a48edc2014-06-13 10:13:56 -07003649 struct mmc_host *mmc = host->mmc;
Pierre Ossman1e728592008-04-16 19:13:13 +02003650 unsigned long flags;
3651
3652 if (dead) {
3653 spin_lock_irqsave(&host->lock, flags);
3654
3655 host->flags |= SDHCI_DEVICE_DEAD;
3656
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003657 if (sdhci_has_requests(host)) {
Girish K Sa3c76eb2011-10-11 11:44:09 +05303658 pr_err("%s: Controller removed during "
Markus Mayer4e743f12014-07-03 13:27:42 -07003659 " transfer!\n", mmc_hostname(mmc));
Adrian Hunter5d0d11c2016-06-29 16:24:31 +03003660 sdhci_error_out_mrqs(host, -ENOMEDIUM);
Pierre Ossman1e728592008-04-16 19:13:13 +02003661 }
3662
3663 spin_unlock_irqrestore(&host->lock, flags);
3664 }
3665
Anton Vorontsov7260cf52009-03-17 00:13:48 +03003666 sdhci_disable_card_detection(host);
3667
Markus Mayer4e743f12014-07-03 13:27:42 -07003668 mmc_remove_host(mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003669
Adrian Hunter061d17a2016-04-12 14:25:09 +03003670 sdhci_led_unregister(host);
Pierre Ossman2f730fe2008-03-17 10:29:38 +01003671
Pierre Ossman1e728592008-04-16 19:13:13 +02003672 if (!dead)
Russell King03231f92014-04-25 12:57:12 +01003673 sdhci_do_reset(host, SDHCI_RESET_ALL);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003674
Russell Kingb537f942014-04-25 12:56:01 +01003675 sdhci_writel(host, 0, SDHCI_INT_ENABLE);
3676 sdhci_writel(host, 0, SDHCI_SIGNAL_ENABLE);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003677 free_irq(host->irq, host);
3678
3679 del_timer_sync(&host->timer);
Adrian Hunterd7422fb2016-06-29 16:24:33 +03003680 del_timer_sync(&host->data_timer);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003681
Pierre Ossmand129bce2006-03-24 03:18:17 -08003682 tasklet_kill(&host->finish_tasklet);
Pierre Ossman2134a922008-06-28 18:28:51 +02003683
Tim Kryger3a48edc2014-06-13 10:13:56 -07003684 if (!IS_ERR(mmc->supply.vqmmc))
3685 regulator_disable(mmc->supply.vqmmc);
Philip Rakity6231f3d2012-07-23 15:56:23 -07003686
Russell Kingedd63fc2016-01-26 13:39:50 +00003687 if (host->align_buffer)
Russell Kinge66e61c2016-01-26 13:39:55 +00003688 dma_free_coherent(mmc_dev(mmc), host->align_buffer_sz +
3689 host->adma_table_sz, host->align_buffer,
3690 host->align_addr);
Pierre Ossman2134a922008-06-28 18:28:51 +02003691
Adrian Hunter4efaa6f2014-11-04 12:42:39 +02003692 host->adma_table = NULL;
Pierre Ossman2134a922008-06-28 18:28:51 +02003693 host->align_buffer = NULL;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003694}
3695
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003696EXPORT_SYMBOL_GPL(sdhci_remove_host);
3697
3698void sdhci_free_host(struct sdhci_host *host)
Pierre Ossmand129bce2006-03-24 03:18:17 -08003699{
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003700 mmc_free_host(host->mmc);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003701}
3702
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003703EXPORT_SYMBOL_GPL(sdhci_free_host);
Pierre Ossmand129bce2006-03-24 03:18:17 -08003704
3705/*****************************************************************************\
3706 * *
3707 * Driver init/exit *
3708 * *
3709\*****************************************************************************/
3710
3711static int __init sdhci_drv_init(void)
3712{
Girish K Sa3c76eb2011-10-11 11:44:09 +05303713 pr_info(DRIVER_NAME
Pierre Ossman52fbf9c2007-02-09 08:23:41 +01003714 ": Secure Digital Host Controller Interface driver\n");
Girish K Sa3c76eb2011-10-11 11:44:09 +05303715 pr_info(DRIVER_NAME ": Copyright(c) Pierre Ossman\n");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003716
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003717 return 0;
Pierre Ossmand129bce2006-03-24 03:18:17 -08003718}
3719
3720static void __exit sdhci_drv_exit(void)
3721{
Pierre Ossmand129bce2006-03-24 03:18:17 -08003722}
3723
3724module_init(sdhci_drv_init);
3725module_exit(sdhci_drv_exit);
3726
Pierre Ossmandf673b22006-06-30 02:22:31 -07003727module_param(debug_quirks, uint, 0444);
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003728module_param(debug_quirks2, uint, 0444);
Pierre Ossman67435272006-06-30 02:22:31 -07003729
Pierre Ossman32710e82009-04-08 20:14:54 +02003730MODULE_AUTHOR("Pierre Ossman <pierre@ossman.eu>");
Pierre Ossmanb8c86fc2008-03-18 17:35:49 +01003731MODULE_DESCRIPTION("Secure Digital Host Controller Interface core driver");
Pierre Ossmand129bce2006-03-24 03:18:17 -08003732MODULE_LICENSE("GPL");
Pierre Ossman67435272006-06-30 02:22:31 -07003733
Pierre Ossmandf673b22006-06-30 02:22:31 -07003734MODULE_PARM_DESC(debug_quirks, "Force certain quirks.");
Adrian Hunter66fd8ad2011-10-03 15:33:34 +03003735MODULE_PARM_DESC(debug_quirks2, "Force certain other quirks.");